1 11 12 package org.eclipse.jdt.ui.actions; 13 14 import org.eclipse.core.runtime.CoreException; 15 16 import org.eclipse.core.resources.IWorkspaceRoot; 17 import org.eclipse.core.resources.ResourcesPlugin; 18 19 import org.eclipse.swt.widgets.Shell; 20 21 import org.eclipse.jface.action.Action; 22 import org.eclipse.jface.dialogs.MessageDialog; 23 import org.eclipse.jface.resource.JFaceResources; 24 import org.eclipse.jface.viewers.ISelection; 25 import org.eclipse.jface.viewers.IStructuredSelection; 26 import org.eclipse.jface.viewers.StructuredSelection; 27 import org.eclipse.jface.window.Window; 28 import org.eclipse.jface.wizard.WizardDialog; 29 30 import org.eclipse.ui.INewWizard; 31 import org.eclipse.ui.IWorkbenchWindow; 32 import org.eclipse.ui.PlatformUI; 33 import org.eclipse.ui.actions.NewProjectAction; 34 35 import org.eclipse.jdt.core.IJavaElement; 36 37 import org.eclipse.jdt.internal.ui.JavaPlugin; 38 import org.eclipse.jdt.internal.ui.util.ExceptionHandler; 39 import org.eclipse.jdt.internal.ui.util.PixelConverter; 40 import org.eclipse.jdt.internal.ui.wizards.NewElementWizard; 41 import org.eclipse.jdt.internal.ui.wizards.NewWizardMessages; 42 43 51 public abstract class AbstractOpenWizardAction extends Action { 52 53 private Shell fShell; 54 private IStructuredSelection fSelection; 55 private IJavaElement fCreatedElement; 56 57 60 protected AbstractOpenWizardAction() { 61 fShell= null; 62 fSelection= null; 63 fCreatedElement= null; 64 } 65 66 69 public void run() { 70 Shell shell= getShell(); 71 if (!doCreateProjectFirstOnEmptyWorkspace(shell)) { 72 return; 73 } 74 try { 75 INewWizard wizard= createWizard(); 76 wizard.init(PlatformUI.getWorkbench(), getSelection()); 77 78 WizardDialog dialog= new WizardDialog(shell, wizard); 79 PixelConverter converter= new PixelConverter(JFaceResources.getDialogFont()); 80 dialog.setMinimumPageSize(converter.convertWidthInCharsToPixels(70), converter.convertHeightInCharsToPixels(20)); 81 dialog.create(); 82 int res= dialog.open(); 83 if (res == Window.OK && wizard instanceof NewElementWizard) { 84 fCreatedElement= ((NewElementWizard)wizard).getCreatedElement(); 85 } 86 87 notifyResult(res == Window.OK); 88 } catch (CoreException e) { 89 String title= NewWizardMessages.AbstractOpenWizardAction_createerror_title; 90 String message= NewWizardMessages.AbstractOpenWizardAction_createerror_message; 91 ExceptionHandler.handle(e, shell, title, message); 92 } 93 } 94 95 100 abstract protected INewWizard createWizard() throws CoreException; 101 102 107 protected IStructuredSelection getSelection() { 108 if (fSelection == null) { 109 return evaluateCurrentSelection(); 110 } 111 return fSelection; 112 } 113 114 private IStructuredSelection evaluateCurrentSelection() { 115 IWorkbenchWindow window= JavaPlugin.getActiveWorkbenchWindow(); 116 if (window != null) { 117 ISelection selection= window.getSelectionService().getSelection(); 118 if (selection instanceof IStructuredSelection) { 119 return (IStructuredSelection) selection; 120 } 121 } 122 return StructuredSelection.EMPTY; 123 } 124 125 129 public void setSelection(IStructuredSelection selection) { 130 fSelection= selection; 131 } 132 133 138 protected Shell getShell() { 139 if (fShell == null) { 140 return JavaPlugin.getActiveWorkbenchShell(); 141 } 142 return fShell; 143 } 144 145 149 public void setShell(Shell shell) { 150 fShell= shell; 151 } 152 153 159 protected boolean doCreateProjectFirstOnEmptyWorkspace(Shell shell) { 160 IWorkspaceRoot workspaceRoot= ResourcesPlugin.getWorkspace().getRoot(); 161 if (workspaceRoot.getProjects().length == 0) { 162 String title= NewWizardMessages.AbstractOpenWizardAction_noproject_title; 163 String message= NewWizardMessages.AbstractOpenWizardAction_noproject_message; 164 if (MessageDialog.openQuestion(shell, title, message)) { 165 new NewProjectAction().run(); 166 return workspaceRoot.getProjects().length != 0; 167 } 168 return false; 169 } 170 return true; 171 } 172 173 177 public IJavaElement getCreatedElement() { 178 return fCreatedElement; 179 } 180 181 182 } 183 | Popular Tags |