1 11 package org.eclipse.jdt.internal.ui.jarpackager; 12 13 import java.lang.reflect.InvocationTargetException ; 14 import java.util.ArrayList ; 15 import java.util.Iterator ; 16 import java.util.List ; 17 18 import org.eclipse.core.runtime.Assert; 19 import org.eclipse.core.runtime.CoreException; 20 import org.eclipse.core.runtime.IStatus; 21 22 import org.eclipse.core.resources.IProject; 23 import org.eclipse.core.resources.IResource; 24 25 import org.eclipse.jface.dialogs.ErrorDialog; 26 import org.eclipse.jface.dialogs.IDialogSettings; 27 import org.eclipse.jface.viewers.ISelection; 28 import org.eclipse.jface.viewers.IStructuredSelection; 29 import org.eclipse.jface.viewers.StructuredSelection; 30 import org.eclipse.jface.wizard.IWizardPage; 31 import org.eclipse.jface.wizard.Wizard; 32 33 import org.eclipse.ui.IExportWizard; 34 import org.eclipse.ui.IWorkbench; 35 36 import org.eclipse.jdt.core.IClassFile; 37 import org.eclipse.jdt.core.ICompilationUnit; 38 import org.eclipse.jdt.core.IJavaElement; 39 import org.eclipse.jdt.core.IOpenable; 40 import org.eclipse.jdt.core.IPackageFragmentRoot; 41 import org.eclipse.jdt.core.JavaCore; 42 43 import org.eclipse.jdt.internal.corext.util.JavaModelUtil; 44 45 import org.eclipse.jdt.ui.jarpackager.IJarExportRunnable; 46 import org.eclipse.jdt.ui.jarpackager.JarPackageData; 47 48 import org.eclipse.jdt.internal.ui.JavaPlugin; 49 import org.eclipse.jdt.internal.ui.JavaPluginImages; 50 import org.eclipse.jdt.internal.ui.util.ExceptionHandler; 51 52 72 public class JarPackageWizard extends Wizard implements IExportWizard { 73 74 private static String DIALOG_SETTINGS_KEY= "JarPackageWizard"; 76 private boolean fHasNewDialogSettings; 77 78 private boolean fInitializeFromJarPackage; 79 80 private JarOptionsPage fJarOptionsWizardPage; 81 82 private JarPackageData fJarPackage; 83 84 private JarPackageWizardPage fJarPackageWizardPage; 85 86 private IStructuredSelection fSelection; 87 88 91 public JarPackageWizard() { 92 IDialogSettings workbenchSettings= JavaPlugin.getDefault().getDialogSettings(); 93 IDialogSettings section= workbenchSettings.getSection(DIALOG_SETTINGS_KEY); 94 if (section == null) 95 fHasNewDialogSettings= true; 96 else { 97 fHasNewDialogSettings= false; 98 setDialogSettings(section); 99 } 100 } 101 102 private void addJavaElement(List selectedElements, IJavaElement je) { 103 if (je.getElementType() == IJavaElement.COMPILATION_UNIT) 104 selectedElements.add(je); 105 else if (je.getElementType() == IJavaElement.CLASS_FILE) 106 selectedElements.add(je); 107 else if (je.getElementType() == IJavaElement.JAVA_PROJECT) 108 selectedElements.add(je); 109 else if (je.getElementType() == IJavaElement.PACKAGE_FRAGMENT) { 110 if (!JavaModelUtil.getPackageFragmentRoot(je).isArchive()) 111 selectedElements.add(je); 112 } else if (je.getElementType() == IJavaElement.PACKAGE_FRAGMENT_ROOT) { 113 if (!((IPackageFragmentRoot) je).isArchive()) 114 selectedElements.add(je); 115 } else { 116 IOpenable openable= je.getOpenable(); 117 if (openable instanceof ICompilationUnit) 118 selectedElements.add(((ICompilationUnit) openable).getPrimary()); 119 else if (openable instanceof IClassFile && !JavaModelUtil.getPackageFragmentRoot(je).isArchive()) 120 selectedElements.add(openable); 121 } 122 } 123 124 127 public void addPages() { 128 super.addPages(); 129 fJarPackageWizardPage= new JarPackageWizardPage(fJarPackage, fSelection); 130 addPage(fJarPackageWizardPage); 131 fJarOptionsWizardPage= new JarOptionsPage(fJarPackage); 132 addPage(fJarOptionsWizardPage); 133 addPage(new JarManifestWizardPage(fJarPackage)); 134 } 135 136 private void addProject(List selectedElements, IProject project) { 137 try { 138 if (project.hasNature(JavaCore.NATURE_ID)) 139 selectedElements.add(JavaCore.create(project)); 140 } catch (CoreException ex) { 141 } 143 } 144 145 private void addResource(List selectedElements, IResource resource) { 146 IJavaElement je= JavaCore.create(resource); 147 if (je != null && je.exists() && je.getElementType() == IJavaElement.COMPILATION_UNIT) 148 selectedElements.add(je); 149 else 150 selectedElements.add(resource); 151 } 152 153 158 protected boolean executeExportOperation(IJarExportRunnable op) { 159 try { 160 getContainer().run(true, true, op); 161 } catch (InterruptedException e) { 162 return false; 163 } catch (InvocationTargetException ex) { 164 if (ex.getTargetException() != null) { 165 ExceptionHandler.handle(ex, getShell(), JarPackagerMessages.JarPackageWizard_jarExportError_title, JarPackagerMessages.JarPackageWizard_jarExportError_message); 166 return false; 167 } 168 } 169 IStatus status= op.getStatus(); 170 if (!status.isOK()) { 171 ErrorDialog.openError(getShell(), JarPackagerMessages.JarPackageWizard_jarExport_title, null, status); 172 return !(status.matches(IStatus.ERROR)); 173 } 174 return true; 175 } 176 177 public IWizardPage getNextPage(IWizardPage page) { 178 if (page == fJarPackageWizardPage && !fJarPackage.isRefactoringAware()) 179 return fJarOptionsWizardPage; 180 return super.getNextPage(page); 181 } 182 183 public IWizardPage getPreviousPage(IWizardPage page) { 184 if (page == fJarOptionsWizardPage && !fJarPackage.isRefactoringAware()) 185 return fJarPackageWizardPage; 186 return super.getPreviousPage(page); 187 } 188 189 198 protected IStructuredSelection getValidSelection() { 199 ISelection currentSelection= JavaPlugin.getActiveWorkbenchWindow().getSelectionService().getSelection(); 200 if (currentSelection instanceof IStructuredSelection) { 201 IStructuredSelection structuredSelection= (IStructuredSelection) currentSelection; 202 List selectedElements= new ArrayList (structuredSelection.size()); 203 Iterator iter= structuredSelection.iterator(); 204 while (iter.hasNext()) { 205 Object selectedElement= iter.next(); 206 if (selectedElement instanceof IProject) 207 addProject(selectedElements, (IProject) selectedElement); 208 else if (selectedElement instanceof IResource) 209 addResource(selectedElements, (IResource) selectedElement); 210 else if (selectedElement instanceof IJavaElement) 211 addJavaElement(selectedElements, (IJavaElement) selectedElement); 212 } 213 return new StructuredSelection(selectedElements); 214 } else 215 return StructuredSelection.EMPTY; 216 } 217 218 221 public void init(IWorkbench workbench, IStructuredSelection selection) { 222 fSelection= getValidSelection(); 224 fJarPackage= new JarPackageData(); 225 setInitializeFromJarPackage(false); 226 setWindowTitle(JarPackagerMessages.JarPackageWizard_windowTitle); 227 setDefaultPageImageDescriptor(JavaPluginImages.DESC_WIZBAN_JAR_PACKAGER); 228 setNeedsProgressMonitor(true); 229 } 230 231 239 public void init(IWorkbench workbench, JarPackageData jarPackage) { 240 Assert.isNotNull(workbench); 241 Assert.isNotNull(jarPackage); 242 fJarPackage= jarPackage; 243 setInitializeFromJarPackage(true); 244 fSelection= new StructuredSelection(fJarPackage.getElements()); 245 setWindowTitle(JarPackagerMessages.JarPackageWizard_windowTitle); 246 setDefaultPageImageDescriptor(JavaPluginImages.DESC_WIZBAN_JAR_PACKAGER); 247 setNeedsProgressMonitor(true); 248 } 249 250 boolean isInitializingFromJarPackage() { 251 return fInitializeFromJarPackage; 252 } 253 254 257 public boolean performFinish() { 258 fJarPackage.setElements(fJarPackageWizardPage.getSelectedElementsWithoutContainedChildren()); 259 260 if (!executeExportOperation(fJarPackage.createJarExportRunnable(getShell()))) 261 return false; 262 263 if (fHasNewDialogSettings) { 265 IDialogSettings workbenchSettings= JavaPlugin.getDefault().getDialogSettings(); 266 IDialogSettings section= workbenchSettings.getSection(DIALOG_SETTINGS_KEY); 267 section= workbenchSettings.addNewSection(DIALOG_SETTINGS_KEY); 268 setDialogSettings(section); 269 } 270 IWizardPage[] pages= getPages(); 271 for (int i= 0; i < getPageCount(); i++) { 272 IWizardPage page= pages[i]; 273 if (page instanceof IJarPackageWizardPage) 274 ((IJarPackageWizardPage) page).finish(); 275 } 276 return true; 277 } 278 279 void setInitializeFromJarPackage(boolean state) { 280 fInitializeFromJarPackage= state; 281 } 282 } | Popular Tags |