001: /*******************************************************************************
002: * Copyright (c) 2000, 2006 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.jdt.internal.ui.jarpackager;
011:
012: import java.io.IOException;
013:
014: import org.eclipse.core.runtime.Assert;
015: import org.eclipse.core.runtime.CoreException;
016:
017: import org.eclipse.core.resources.IFile;
018:
019: import org.eclipse.swt.widgets.Shell;
020:
021: import org.eclipse.jface.action.IAction;
022: import org.eclipse.jface.dialogs.ErrorDialog;
023: import org.eclipse.jface.dialogs.MessageDialog;
024: import org.eclipse.jface.wizard.WizardDialog;
025:
026: import org.eclipse.jdt.ui.jarpackager.IJarDescriptionReader;
027: import org.eclipse.jdt.ui.jarpackager.JarPackageData;
028:
029: import org.xml.sax.SAXException;
030:
031: /**
032: * This action delegate opens the JAR Package Wizard and initializes
033: * it with the selected JAR package description.
034: */
035: public class OpenJarPackageWizardActionDelegate extends
036: JarPackageActionDelegate {
037:
038: private IJarDescriptionReader fReader;
039:
040: /*
041: * @see IActionDelegate
042: */
043: public void run(IAction action) {
044: Shell parent = getShell();
045: JarPackageData jarPackage = null;
046: String errorDetail = null;
047: try {
048: jarPackage = readJarPackage(getDescriptionFile(getSelection()));
049: } catch (IOException ex) {
050: errorDetail = ex.getLocalizedMessage();
051: MessageDialog
052: .openError(
053: parent,
054: JarPackagerMessages.OpenJarPackageWizardDelegate_error_openJarPackager_title,
055: JarPackagerMessages.OpenJarPackageWizardDelegate_error_openJarPackager_message
056: + errorDetail);
057: return;
058: } catch (CoreException ex) {
059: errorDetail = ex.getLocalizedMessage();
060: MessageDialog
061: .openError(
062: parent,
063: JarPackagerMessages.OpenJarPackageWizardDelegate_error_openJarPackager_title,
064: JarPackagerMessages.OpenJarPackageWizardDelegate_error_openJarPackager_message
065: + errorDetail);
066: return;
067: } catch (SAXException ex) {
068: errorDetail = JarPackagerMessages.OpenJarPackageWizardDelegate_badXmlFormat
069: + ex.getLocalizedMessage();
070: MessageDialog
071: .openError(
072: parent,
073: JarPackagerMessages.OpenJarPackageWizardDelegate_error_openJarPackager_title,
074: JarPackagerMessages.OpenJarPackageWizardDelegate_error_openJarPackager_message
075: + errorDetail);
076: return;
077: }
078:
079: if (fReader != null && !fReader.getStatus().isOK())
080: ErrorDialog
081: .openError(
082: parent,
083: JarPackagerMessages.OpenJarPackageWizardDelegate_jarDescriptionReaderWarnings_title,
084: null, fReader.getStatus());
085: JarPackageWizard wizard = new JarPackageWizard();
086: wizard.init(getWorkbench(), jarPackage);
087: WizardDialog dialog = new WizardDialog(parent, wizard);
088: dialog.create();
089: dialog.open();
090: }
091:
092: /**
093: * Reads the JAR package spec from file.
094: */
095: private JarPackageData readJarPackage(IFile description)
096: throws CoreException, IOException, SAXException {
097: Assert.isLegal(description.isAccessible());
098: Assert.isNotNull(description.getFileExtension());
099: Assert.isLegal(description.getFileExtension().equals(
100: JarPackagerUtil.DESCRIPTION_EXTENSION));
101: JarPackageData jarPackage = new JarPackageData();
102: try {
103: fReader = jarPackage.createJarDescriptionReader(description
104: .getContents());
105: fReader.read(jarPackage);
106: } finally {
107: if (fReader != null)
108: fReader.close();
109: }
110: return jarPackage;
111: }
112: }
|