1 17 package org.eclipse.emf.codegen.presentation; 18 19 20 import java.lang.reflect.InvocationTargetException ; 21 import java.util.ArrayList ; 22 import java.util.Arrays ; 23 import java.util.Iterator ; 24 import java.util.List ; 25 26 import org.eclipse.core.resources.IProject; 27 import org.eclipse.core.resources.ResourcesPlugin; 28 import org.eclipse.core.runtime.CoreException; 29 import org.eclipse.core.runtime.IProgressMonitor; 30 import org.eclipse.jdt.core.JavaCore; 31 import org.eclipse.jface.operation.IRunnableWithProgress; 32 import org.eclipse.jface.viewers.ArrayContentProvider; 33 import org.eclipse.jface.viewers.CheckStateChangedEvent; 34 import org.eclipse.jface.viewers.CheckboxTableViewer; 35 import org.eclipse.jface.viewers.ColumnWeightData; 36 import org.eclipse.jface.viewers.ICheckStateListener; 37 import org.eclipse.jface.viewers.IStructuredSelection; 38 import org.eclipse.jface.viewers.LabelProvider; 39 import org.eclipse.jface.viewers.TableLayout; 40 import org.eclipse.jface.wizard.Wizard; 41 import org.eclipse.jface.wizard.WizardPage; 42 import org.eclipse.swt.SWT; 43 import org.eclipse.swt.events.SelectionAdapter; 44 import org.eclipse.swt.events.SelectionEvent; 45 import org.eclipse.swt.graphics.Image; 46 import org.eclipse.swt.layout.GridData; 47 import org.eclipse.swt.layout.GridLayout; 48 import org.eclipse.swt.layout.RowLayout; 49 import org.eclipse.swt.widgets.Button; 50 import org.eclipse.swt.widgets.Composite; 51 import org.eclipse.swt.widgets.Label; 52 import org.eclipse.swt.widgets.Table; 53 import org.eclipse.swt.widgets.TableColumn; 54 import org.eclipse.ui.INewWizard; 55 import org.eclipse.ui.IWorkbench; 56 import org.eclipse.ui.actions.WorkspaceModifyOperation; 57 import org.eclipse.ui.ide.IDE; 58 59 import org.eclipse.emf.codegen.jet.JETAddNatureOperation; 60 import org.eclipse.emf.codegen.jet.JETNature; 61 62 63 public class ConvertToJETProjectWizard extends Wizard implements INewWizard 64 { 65 protected IWorkbench workbench; 66 protected List projectsToConvert = new ArrayList (); 67 protected ConversionPage conversionPage; 68 69 public ConvertToJETProjectWizard() 70 { 71 setWindowTitle(CodeGenUIPlugin.getPlugin().getString("_UI_JETConvertProject_label")); 72 setNeedsProgressMonitor(true); 73 } 74 75 public void init(IWorkbench workbench, IStructuredSelection selection) 76 { 77 this.workbench = workbench; 78 for (Iterator i = selection.iterator(); i.hasNext(); ) 79 { 80 Object object = i.next(); 81 if (object instanceof IProject) 82 { 83 IProject project = (IProject)object; 84 if (project.isOpen() && JETNature.getRuntime(project) == null) 85 { 86 projectsToConvert.add(project); 87 } 88 } 89 } 90 } 91 92 public void addPages() 93 { 94 super.addPages(); 95 conversionPage = new ConversionPage(); 96 addPage(conversionPage); 97 } 98 99 public boolean performFinish() 100 { 101 IRunnableWithProgress operation = 102 new WorkspaceModifyOperation() 103 { 104 public void execute(IProgressMonitor monitor) 105 { 106 try 107 { 108 JETAddNatureOperation addNature = new JETAddNatureOperation(projectsToConvert); 109 addNature.run(monitor); 110 } 111 catch (CoreException e) 112 { 113 CodeGenUIPlugin.write(e); 114 } 115 } 116 }; 117 118 try 119 { 120 getContainer().run(false, true, operation); 121 } 122 catch (InterruptedException exception) 123 { 124 CodeGenUIPlugin.write(exception); 125 } 126 catch (InvocationTargetException exception) 127 { 128 CodeGenUIPlugin.write(exception); 129 } 130 return true; 131 } 132 133 public class ConversionPage extends WizardPage 134 { 135 public ConversionPage() 136 { 137 super("JETConversionPage"); 138 139 setTitle(CodeGenUIPlugin.getPlugin().getString("_UI_JETConvertProject_label")); 140 setDefaultPageImageDescriptor(CodeGenUIPlugin.getPlugin().getImage("full/wizban/ConvertToJETProjectWizard")); 141 setDescription(CodeGenUIPlugin.getPlugin().getString("_UI_JETConvertProject_message")); 142 } 143 144 public boolean isPageComplete() 145 { 146 return !projectsToConvert.isEmpty(); 147 } 148 149 public void createControl(Composite parent) 150 { 151 Composite composite = new Composite(parent, SWT.NONE); 152 { 153 GridLayout layout = new GridLayout(); 154 layout.numColumns = 2; 155 layout.verticalSpacing = 12; 156 composite.setLayout(layout); 157 158 GridData data = new GridData(); 159 data.verticalAlignment = GridData.FILL; 160 data.grabExcessVerticalSpace = true; 161 data.horizontalAlignment = GridData.FILL; 162 composite.setLayoutData(data); 163 } 164 165 Label projectsLabel = new Label(composite, SWT.LEFT); 166 projectsLabel.setText(CodeGenUIPlugin.getPlugin().getString("_UI_Projects_label")); 167 { 168 GridData data = new GridData(); 169 data.horizontalAlignment = GridData.FILL; 170 projectsLabel.setLayoutData(data); 171 } 172 173 Composite selectionComposite = new Composite(composite, SWT.NONE); 174 { 175 GridData data = new GridData(); 176 data.horizontalAlignment = GridData.END; 177 selectionComposite.setLayoutData(data); 178 179 RowLayout layout = new RowLayout(); 180 layout.justify = true; 181 layout.pack = true; 182 layout.spacing = 15; 183 selectionComposite.setLayout(layout); 184 } 185 186 Button selectAllButton = new Button(selectionComposite, SWT.PUSH); 187 selectAllButton.setText(CodeGenUIPlugin.getPlugin().getString("_UI_SelectAll_label")); 188 189 Button deselectAllButton = new Button(selectionComposite, SWT.PUSH); 190 deselectAllButton.setText(CodeGenUIPlugin.getPlugin().getString("_UI_DeselectAll_label")); 191 192 Table projectsTable = new Table(composite, SWT.CHECK | SWT.BORDER); 193 final CheckboxTableViewer projectsCheckboxTableViewer = new CheckboxTableViewer(projectsTable); 194 { 195 GridData data = new GridData(); 196 data.verticalAlignment = GridData.FILL; 197 data.grabExcessHorizontalSpace = true; 198 data.grabExcessVerticalSpace = false; 199 data.heightHint = 250; 200 data.horizontalAlignment = GridData.FILL; 201 data.horizontalSpan = 2; 202 projectsTable.setLayoutData(data); 203 } 204 205 { 206 TableLayout layout = new TableLayout(); 207 TableColumn projectColumn = new TableColumn(projectsTable, SWT.NONE); 208 layout.addColumnData(new ColumnWeightData(1, true)); 209 projectColumn.setResizable(true); 210 projectsTable.setLayout(layout); 211 } 212 213 projectsCheckboxTableViewer.setColumnProperties(new String [] {"a"}); 214 projectsCheckboxTableViewer.setContentProvider(new ArrayContentProvider()); 215 projectsCheckboxTableViewer.setLabelProvider 216 (new LabelProvider() 217 { 218 public Image getImage(Object o) 219 { 220 return workbench.getSharedImages().getImage(IDE.SharedImages.IMG_OBJ_PROJECT); 221 } 222 223 public String getText(Object o) 224 { 225 return ((IProject)o).getName(); 226 } 227 }); 228 229 projectsCheckboxTableViewer.addCheckStateListener 230 (new ICheckStateListener() 231 { 232 public void checkStateChanged(CheckStateChangedEvent event) 233 { 234 projectsToConvert.clear(); 235 projectsToConvert.addAll(Arrays.asList(projectsCheckboxTableViewer.getCheckedElements())); 236 setPageComplete(isPageComplete()); 237 } 238 }); 239 240 final List projects = new ArrayList (Arrays.asList(ResourcesPlugin.getWorkspace().getRoot().getProjects())); 241 for (Iterator i = projects.iterator(); i.hasNext(); ) 242 { 243 IProject project = (IProject)i.next(); 244 boolean isJavaProject = false; 245 try 246 { 247 isJavaProject = project.getNature(JavaCore.NATURE_ID) != null; 248 } 249 catch (CoreException e) 250 { 251 } 252 253 if (!project.isOpen() || JETNature.getRuntime(project) != null || !isJavaProject) 254 { 255 i.remove(); 256 } 257 } 258 projectsCheckboxTableViewer.setInput(projects.toArray()); 259 projectsCheckboxTableViewer.setCheckedElements(projectsToConvert.toArray()); 260 261 selectAllButton.addSelectionListener 262 (new SelectionAdapter() 263 { 264 public void widgetSelected(SelectionEvent event) 265 { 266 projectsToConvert.addAll(projects); 267 projectsCheckboxTableViewer.setCheckedElements(projects.toArray()); 268 setPageComplete(isPageComplete()); 269 } 270 }); 271 deselectAllButton.addSelectionListener 272 (new SelectionAdapter() 273 { 274 public void widgetSelected(SelectionEvent event) 275 { 276 projectsCheckboxTableViewer.setCheckedElements(new Object [0]); 277 projectsToConvert.clear(); 278 setPageComplete(isPageComplete()); 279 } 280 }); 281 282 setControl(composite); 283 284 setPageComplete(isPageComplete()); 285 } 286 } 287 } 288 | Popular Tags |