1 26 30 31 package org.omg.lifl.eclipse.plugin.project.OpenCCM; 32 33 import java.lang.reflect.InvocationTargetException ; 34 35 import org.eclipse.core.resources.IFile; 36 import org.eclipse.core.resources.IResource; 37 import org.eclipse.core.runtime.CoreException; 38 import org.eclipse.core.runtime.IConfigurationElement; 39 import org.eclipse.core.runtime.IExecutableExtension; 40 import org.eclipse.core.runtime.IStatus; 41 import org.eclipse.jface.dialogs.ErrorDialog; 42 import org.eclipse.jface.dialogs.IDialogConstants; 43 import org.eclipse.jface.dialogs.MessageDialog; 44 import org.eclipse.jface.operation.IRunnableWithProgress; 45 import org.eclipse.jface.resource.ImageDescriptor; 46 import org.eclipse.swt.widgets.Display; 47 import org.eclipse.ui.INewWizard; 48 import org.eclipse.ui.IWorkbenchPage; 49 import org.eclipse.ui.IWorkbenchWindow; 50 import org.eclipse.ui.PartInitException; 51 import org.eclipse.ui.actions.WorkspaceModifyDelegatingOperation; 52 import org.eclipse.ui.dialogs.IOverwriteQuery; 53 import org.eclipse.ui.wizards.newresource.BasicNewProjectResourceWizard; 54 import org.eclipse.ui.wizards.newresource.BasicNewResourceWizard; 55 import org.omg.lifl.eclipse.plugin.project.OpenCCM.utils.ProjectMessages; 56 57 61 public class ProjectCreationWizard 62 extends BasicNewResourceWizard 63 implements INewWizard, IExecutableExtension { 64 65 private IConfigurationElement fConfigElement; 66 67 private ProjectCreationWizardPage[] fPages; 68 69 public ProjectCreationWizard() { 70 super(); 71 setDialogSettings(MainPlugin.getDefault().getDialogSettings()); 72 setWindowTitle( 73 ProjectMessages.getString("ProjectCreationWizard.title")); 74 setNeedsProgressMonitor(true); 75 } 76 77 80 protected void initializeDefaultPageImageDescriptor() { 81 if (fConfigElement != null) { 82 String banner = fConfigElement.getAttribute("banner"); 83 if (banner != null) { 84 ImageDescriptor desc = 85 MainPlugin.getDefault().getImageDescriptor(banner); 86 setDefaultPageImageDescriptor(desc); 87 } 88 } 89 } 90 91 94 public void addPages() { 95 super.addPages(); 96 97 IConfigurationElement[] children = 98 fConfigElement.getChildren("projectsetup"); 99 if (children == null || children.length == 0) { 100 MainPlugin.log( 101 "descriptor must contain one ore more projectsetup tags"); 102 return; 103 } 104 105 fPages = new ProjectCreationWizardPage[children.length]; 106 107 for (int i = 0; i < children.length; i++) { 108 IConfigurationElement curr = children[i]; 109 fPages[i] = new ProjectCreationWizardPage(i, children[i]); 110 addPage(fPages[i]); 111 } 112 } 113 114 117 public boolean performFinish() { 118 ProjectCreationOperation runnable = 119 new ProjectCreationOperation(fPages, new ImportOverwriteQuery()); 120 121 IRunnableWithProgress op = 122 new WorkspaceModifyDelegatingOperation(runnable); 123 try { 124 getContainer().run(false, true, op); 125 } catch (InvocationTargetException e) { 126 handleException(e.getTargetException()); 127 return false; 128 } catch (InterruptedException e) { 129 return false; 130 } 131 BasicNewProjectResourceWizard.updatePerspective(fConfigElement); 132 IResource res = runnable.getElementToOpen(); 133 if (res != null) { 134 openResource(res); 135 } 136 return true; 137 } 138 139 142 private void openResource(final IResource resource) { 143 if (resource.getType() != IResource.FILE) { 144 return; 145 } 146 IWorkbenchWindow window = 147 MainPlugin.getDefault().getWorkbench().getActiveWorkbenchWindow(); 148 if (window == null) { 149 return; 150 } 151 final IWorkbenchPage activePage = window.getActivePage(); 152 if (activePage != null) { 153 final Display display = getShell().getDisplay(); 154 display.asyncExec(new Runnable () { 155 public void run() { 156 try { 157 activePage.openEditor((IFile) resource); 158 } catch (PartInitException e) { 159 MainPlugin.log(e); 160 } 161 } 162 }); 163 selectAndReveal(resource); 164 } 165 } 166 167 170 private void handleException(Throwable target) { 171 String title = 172 ProjectMessages.getString("ProjectCreationWizard.op_error.title"); 173 String message = 174 ProjectMessages.getString("ProjectCreationWizard.op_error.message"); 175 if (target instanceof CoreException) { 176 IStatus status = ((CoreException) target).getStatus(); 177 ErrorDialog.openError(getShell(), title, message, status); 178 MainPlugin.log(status); 179 } else { 180 MessageDialog.openError(getShell(), title, target.getMessage()); 181 MainPlugin.log(target); 182 } 183 } 184 185 188 public void setInitializationData( 189 IConfigurationElement cfig, 190 String propertyName, 191 Object data) { 192 fConfigElement = cfig; 193 194 } 195 196 198 private class ImportOverwriteQuery implements IOverwriteQuery { 199 public String queryOverwrite(String file) { 200 String [] returnCodes = { YES, NO, ALL, CANCEL }; 201 int returnVal = openDialog(file); 202 return returnVal < 0 ? CANCEL : returnCodes[returnVal]; 203 } 204 205 private int openDialog(final String file) { 206 final int[] result = { IDialogConstants.CANCEL_ID }; 207 getShell().getDisplay().syncExec(new Runnable () { 208 public void run() { 209 String title = 210 ProjectMessages.getString( 211 "ProjectCreationWizard.overwritequery.title"); 212 String msg = 213 ProjectMessages.getFormattedString( 214 "ProjectCreationWizard.overwritequery.message", 215 file); 216 String [] options = 217 { 218 IDialogConstants.YES_LABEL, 219 IDialogConstants.NO_LABEL, 220 IDialogConstants.YES_TO_ALL_LABEL, 221 IDialogConstants.CANCEL_LABEL }; 222 MessageDialog dialog = 223 new MessageDialog( 224 getShell(), 225 title, 226 null, 227 msg, 228 MessageDialog.QUESTION, 229 options, 230 0); 231 result[0] = dialog.open(); 232 } 233 }); 234 return result[0]; 235 } 236 } 237 238 } 239 | Popular Tags |