1 11 package org.eclipse.jdt.internal.ui.wizards; 12 13 import java.util.Iterator ; 14 15 import org.eclipse.swt.widgets.Shell; 16 17 import org.eclipse.jface.action.Action; 18 import org.eclipse.jface.action.IAction; 19 import org.eclipse.jface.dialogs.MessageDialog; 20 import org.eclipse.jface.viewers.ISelection; 21 import org.eclipse.jface.viewers.IStructuredSelection; 22 import org.eclipse.jface.window.Window; 23 import org.eclipse.jface.wizard.Wizard; 24 import org.eclipse.jface.wizard.WizardDialog; 25 26 import org.eclipse.core.resources.IWorkspace; 27 import org.eclipse.core.resources.ResourcesPlugin; 28 import org.eclipse.core.runtime.CoreException; 29 30 import org.eclipse.ui.IWorkbench; 31 import org.eclipse.ui.IWorkbenchWindow; 32 import org.eclipse.ui.IWorkbenchWindowActionDelegate; 33 import org.eclipse.ui.IWorkbenchWizard; 34 import org.eclipse.ui.actions.NewProjectAction; 35 36 import org.eclipse.jdt.core.IJavaElement; 37 import org.eclipse.jdt.core.IPackageFragmentRoot; 38 import org.eclipse.jdt.internal.corext.util.JavaModelUtil; 39 import org.eclipse.jdt.internal.ui.JavaPlugin; 40 import org.eclipse.jdt.internal.ui.util.ExceptionHandler; 41 import org.eclipse.jdt.internal.ui.util.PixelConverter; 42 43 public abstract class AbstractOpenWizardAction extends Action implements IWorkbenchWindowActionDelegate { 44 45 private Class [] fActivatedOnTypes; 46 private boolean fAcceptEmptySelection; 47 private boolean fNoChecking; 48 49 54 public AbstractOpenWizardAction(String label, boolean acceptEmptySelection) { 55 this(label, null, acceptEmptySelection); 56 } 57 58 65 public AbstractOpenWizardAction(String label, Class [] activatedOnTypes, boolean acceptEmptySelection) { 66 super(label); 67 fActivatedOnTypes= activatedOnTypes; 68 fAcceptEmptySelection= acceptEmptySelection; 69 fNoChecking= false; 70 } 71 72 76 protected AbstractOpenWizardAction() { 77 fActivatedOnTypes= null; 78 fAcceptEmptySelection= true; 79 fNoChecking= true; 80 } 81 82 protected IWorkbench getWorkbench() { 83 return JavaPlugin.getDefault().getWorkbench(); 84 } 85 86 private boolean isOfAcceptedType(Object obj) { 87 if (fActivatedOnTypes != null) { 88 for (int i= 0; i < fActivatedOnTypes.length; i++) { 89 if (fActivatedOnTypes[i].isInstance(obj)) { 90 return true; 91 } 92 } 93 return false; 94 } 95 return true; 96 } 97 98 99 private boolean isEnabled(IStructuredSelection selection) { 100 Iterator iter= selection.iterator(); 101 while (iter.hasNext()) { 102 Object obj= iter.next(); 103 if (!isOfAcceptedType(obj) || !shouldAcceptElement(obj)) { 104 return false; 105 } 106 } 107 return true; 108 } 109 110 114 protected boolean shouldAcceptElement(Object obj) { 115 return true; 116 } 117 118 122 abstract protected Wizard createWizard() throws CoreException; 123 124 125 protected IStructuredSelection getCurrentSelection() { 126 IWorkbenchWindow window= JavaPlugin.getActiveWorkbenchWindow(); 127 if (window != null) { 128 ISelection selection= window.getSelectionService().getSelection(); 129 if (selection instanceof IStructuredSelection) { 130 return (IStructuredSelection) selection; 131 } 132 133 } 134 return null; 135 } 136 137 140 public void run() { 141 if (!fNoChecking && !canActionBeAdded()) { 142 return; 143 } 144 if (!checkWorkspaceNotEmpty()) { 145 return; 146 } 147 Shell shell= JavaPlugin.getActiveWorkbenchShell(); 148 try { 149 Wizard wizard= createWizard(); 150 if (wizard instanceof IWorkbenchWizard) { 151 ((IWorkbenchWizard)wizard).init(getWorkbench(), getCurrentSelection()); 152 } 153 154 WizardDialog dialog= new WizardDialog(shell, wizard); 155 PixelConverter converter= new PixelConverter(JavaPlugin.getActiveWorkbenchShell()); 156 157 dialog.setMinimumPageSize(converter.convertWidthInCharsToPixels(70), converter.convertHeightInCharsToPixels(20)); 158 dialog.create(); 159 int res= dialog.open(); 160 161 notifyResult(res == Window.OK); 162 } catch (CoreException e) { 163 String title= NewWizardMessages.AbstractOpenWizardAction_createerror_title; 164 String message= NewWizardMessages.AbstractOpenWizardAction_createerror_message; 165 ExceptionHandler.handle(e, shell, title, message); 166 } 167 } 168 169 172 public boolean canActionBeAdded() { 173 IStructuredSelection selection= getCurrentSelection(); 174 if (selection == null || selection.isEmpty()) { 175 return fAcceptEmptySelection; 176 } 177 return isEnabled(selection); 178 } 179 180 183 public void run(IAction action) { 184 run(); 185 } 186 187 190 public void dispose() { 191 } 192 193 196 public void init(IWorkbenchWindow window) { 197 } 198 199 202 public void selectionChanged(IAction action, ISelection selection) { 203 } 205 206 protected boolean checkWorkspaceNotEmpty() { 207 IWorkspace workspace= ResourcesPlugin.getWorkspace(); 208 if (workspace.getRoot().getProjects().length == 0) { 209 Shell shell= JavaPlugin.getActiveWorkbenchShell(); 210 String title= NewWizardMessages.AbstractOpenWizardAction_noproject_title; 211 String message= NewWizardMessages.AbstractOpenWizardAction_noproject_message; 212 if (MessageDialog.openQuestion(shell, title, message)) { 213 IWorkbenchWindow window= JavaPlugin.getActiveWorkbenchWindow(); 214 (new NewProjectAction(window)).run(); 215 return workspace.getRoot().getProjects().length != 0; 216 } 217 return false; 218 } 219 return true; 220 } 221 222 protected static boolean isOnBuildPath(Object obj) { 223 if (obj instanceof IJavaElement) { 224 IJavaElement elem= (IJavaElement)obj; 225 return elem.getJavaProject().isOnClasspath(elem); 226 } 227 return false; 228 } 229 230 protected static boolean isInArchive(Object obj) { 231 if (obj instanceof IJavaElement) { 232 IPackageFragmentRoot root= JavaModelUtil.getPackageFragmentRoot((IJavaElement)obj); 233 return (root != null) && root.isArchive(); 234 } 235 return false; 236 } 237 } 238 | Popular Tags |