1 17 package org.eclipse.emf.codegen.ecore.genmodel.presentation; 18 19 20 import java.util.Collection ; 21 import java.util.Iterator ; 22 23 import org.eclipse.core.runtime.CoreException; 24 import org.eclipse.core.runtime.IProgressMonitor; 25 import org.eclipse.core.runtime.SubProgressMonitor; 26 import org.eclipse.emf.codegen.ecore.genmodel.GenBase; 27 import org.eclipse.emf.codegen.ecore.genmodel.provider.GenModelEditPlugin; 28 import org.eclipse.emf.common.ui.viewer.IViewerProvider; 29 import org.eclipse.emf.edit.ui.action.EditingDomainActionBarContributor; 30 import org.eclipse.jface.action.Action; 31 import org.eclipse.jface.action.IAction; 32 import org.eclipse.jface.action.IContributionItem; 33 import org.eclipse.jface.action.IMenuManager; 34 import org.eclipse.jface.action.IToolBarManager; 35 import org.eclipse.jface.action.MenuManager; 36 import org.eclipse.jface.action.Separator; 37 import org.eclipse.jface.dialogs.ProgressMonitorDialog; 38 import org.eclipse.jface.viewers.ISelection; 39 import org.eclipse.jface.viewers.ISelectionChangedListener; 40 import org.eclipse.jface.viewers.ISelectionProvider; 41 import org.eclipse.jface.viewers.IStructuredSelection; 42 import org.eclipse.jface.viewers.SelectionChangedEvent; 43 import org.eclipse.jface.viewers.Viewer; 44 import org.eclipse.ui.IEditorPart; 45 import org.eclipse.ui.PartInitException; 46 import org.eclipse.ui.actions.WorkspaceModifyOperation; 47 57 58 64 public class GenModelActionBarContributor 65 extends EditingDomainActionBarContributor 66 implements ISelectionChangedListener 67 { 68 71 protected IEditorPart activeEditorPart; 72 73 76 protected ISelection getActiveEditorSelection() 77 { 78 return activeEditorPart == null ? null : 79 ((GenModelEditor)activeEditorPart).getSelection(); 80 } 81 82 85 protected ISelectionProvider selectionProvider; 86 87 93 protected IAction showPropertiesViewAction = 94 new Action(GenModelEditPlugin.INSTANCE.getString("_UI_ShowPropertiesView_menu_item")) 95 { 96 public void run() 97 { 98 try 99 { 100 getPage().showView("org.eclipse.ui.views.PropertySheet"); 101 } 102 catch (PartInitException exception) 103 { 104 GenModelEditPlugin.INSTANCE.log(exception); 105 } 106 } 107 }; 108 109 116 protected IAction refreshViewerAction = 117 new Action(GenModelEditPlugin.INSTANCE.getString("_UI_RefreshViewer_menu_item")) 118 { 119 public boolean isEnabled() 120 { 121 return activeEditorPart instanceof IViewerProvider; 122 } 123 124 public void run() 125 { 126 if (activeEditorPart instanceof IViewerProvider) 127 { 128 Viewer viewer = ((IViewerProvider)activeEditorPart).getViewer(); 129 if (viewer != null) 130 { 131 viewer.refresh(); 132 } 133 } 134 } 135 }; 136 137 140 protected IMenuManager generateMenuManager; 141 142 protected IAction generateAction = new GenerateAction(GenModelEditPlugin.INSTANCE.getString("_UI_GenerateModel_menu_item")) 143 { 144 protected boolean canGenerate(GenBase genObject) 145 { 146 return genObject.canGenerate(); 147 } 148 149 protected void generate(GenBase genObject, IProgressMonitor progressMonitor) 150 { 151 genObject.generate(progressMonitor); 152 } 153 }; 154 155 protected IAction generateEditAction = new GenerateAction(GenModelEditPlugin.INSTANCE.getString("_UI_GenerateEdit_menu_item")) 156 { 157 protected boolean canGenerate(GenBase genObject) 158 { 159 return genObject.canGenerateEdit(); 160 } 161 162 protected void generate(GenBase genObject, IProgressMonitor progressMonitor) 163 { 164 genObject.generateEdit(progressMonitor); 165 } 166 }; 167 168 protected IAction generateEditorAction = new GenerateAction(GenModelEditPlugin.INSTANCE.getString("_UI_GenerateEditor_menu_item")) 169 { 170 protected boolean canGenerate(GenBase genObject) 171 { 172 return genObject.canGenerateEditor(); 173 } 174 175 protected void generate(GenBase genObject, IProgressMonitor progressMonitor) 176 { 177 genObject.generateEditor(progressMonitor); 178 } 179 }; 180 181 protected IAction generateTestsAction = new GenerateAction(GenModelEditPlugin.INSTANCE.getString("_UI_GenerateTests_menu_item")) 182 { 183 protected boolean canGenerate(GenBase genObject) 184 { 185 return genObject.canGenerateTests(); 186 } 187 188 protected void generate(GenBase genObject, IProgressMonitor progressMonitor) 189 { 190 genObject.generateTests(progressMonitor); 191 } 192 }; 193 194 protected IAction generateSchemaAction = new GenerateAction(GenModelEditPlugin.INSTANCE.getString("_UI_GenerateSchema_menu_item")) 195 { 196 protected boolean canGenerate(GenBase genObject) 197 { 198 return genObject.canGenerateSchema(); 199 } 200 201 protected void generate(GenBase genObject, IProgressMonitor progressMonitor) 202 { 203 progressMonitor.beginTask("", 1); 204 genObject.generateSchema(new SubProgressMonitor(progressMonitor, 1)); 205 } 206 }; 207 208 protected IAction generateAllAction = new GenerateAction(GenModelEditPlugin.INSTANCE.getString("_UI_GenerateAll_menu_item")) 209 { 210 protected boolean canGenerate(GenBase genObject) 211 { 212 return genObject.canGenerate() || genObject.canGenerateEdit() || 213 genObject.canGenerateEditor(); 214 } 215 216 protected void generate(GenBase genObject, IProgressMonitor progressMonitor) 217 { 218 progressMonitor.beginTask("", 4); 219 genObject.generate(new SubProgressMonitor(progressMonitor, 1)); 220 genObject.generateEdit(new SubProgressMonitor(progressMonitor, 1)); 221 genObject.generateEditor(new SubProgressMonitor(progressMonitor, 1)); 222 genObject.generateTests(new SubProgressMonitor(progressMonitor, 1)); 223 } 224 }; 225 226 229 protected abstract class GenerateAction extends Action 230 { 231 public GenerateAction(String text) 232 { 233 super(text); 234 } 235 236 protected abstract boolean canGenerate(GenBase genObject); 237 protected abstract void generate(GenBase genObject, IProgressMonitor progressMonitor); 238 239 public boolean isEnabled() 240 { 241 ISelection s = getActiveEditorSelection(); 242 if (!(s instanceof IStructuredSelection)) 243 { 244 return false; 245 } 246 247 IStructuredSelection ss = (IStructuredSelection) s; 248 if (ss.size() == 0) 249 { 250 return false; 251 } 252 253 for (Iterator iter = ss.iterator(); iter.hasNext(); ) 254 { 255 Object selected = iter.next(); 256 if (!(selected instanceof GenBase) || !canGenerate((GenBase)selected)) 257 { 258 return false; 259 } 260 } 261 return true; 262 } 263 264 public void run() 265 { 266 WorkspaceModifyOperation operation = new WorkspaceModifyOperation() 268 { 269 protected void execute(IProgressMonitor progressMonitor) throws CoreException 272 { 273 Collection selection = ((IStructuredSelection)getActiveEditorSelection()).toList(); 274 progressMonitor.beginTask("", selection.size()); 275 try 276 { 277 for (Iterator iter = selection.iterator(); iter.hasNext(); ) 278 { 279 generate((GenBase)iter.next(), new SubProgressMonitor(progressMonitor, 1)); 280 } 281 } 282 catch (Exception exception) 283 { 284 GenModelEditPlugin.INSTANCE.log(exception); 285 } 286 progressMonitor.done(); 287 } 288 }; 289 290 try 294 { 295 new ProgressMonitorDialog(activeEditorPart.getSite().getShell()).run(true, false, operation); 296 } 297 catch (Exception exception) 298 { 299 GenModelEditPlugin.INSTANCE.log(exception); 302 } 303 } 304 } 305 306 309 public GenModelActionBarContributor() 310 { 311 } 312 313 316 public void contributeToMenu(IMenuManager menuManager) 317 { 318 super.contributeToMenu(menuManager); 319 320 generateMenuManager = 321 new MenuManager(GenModelEditPlugin.INSTANCE.getString("_UI_Generate_menu"), "org.eclipse.emf.codegen.ecore.genmodelMenuID"); 322 menuManager.insertAfter("additions", generateMenuManager); 323 generateMenuManager.add(generateAction); 324 generateMenuManager.add(generateEditAction); 325 generateMenuManager.add(generateEditorAction); 326 generateMenuManager.add(generateTestsAction); 327 generateMenuManager.add(generateAllAction); 328 329 generateMenuManager.add(new Separator("schema-actions")); 330 generateMenuManager.add(generateSchemaAction); 331 332 generateMenuManager.add(new Separator("global-actions")); 333 } 334 335 338 public void contributeToToolBar(IToolBarManager toolBarManager) 339 { 340 toolBarManager.add(new Separator("genmodel-settings")); 341 toolBarManager.add(new Separator("genmodel-additions")); 342 } 343 344 347 public void setActiveEditor(IEditorPart part) 348 { 349 super.setActiveEditor(part); 350 activeEditorPart = part; 351 352 if (selectionProvider != null) 355 { 356 selectionProvider.removeSelectionChangedListener(this); 357 } 358 if (part == null) 359 { 360 selectionProvider = null; 361 } 362 else 363 { 364 selectionProvider = part.getSite().getSelectionProvider(); 365 selectionProvider.addSelectionChangedListener(this); 366 367 if (selectionProvider.getSelection() != null) 370 { 371 selectionChanged(new SelectionChangedEvent(selectionProvider, selectionProvider.getSelection())); 372 } 373 } 374 } 375 376 380 public void selectionChanged(SelectionChangedEvent event) 381 { 382 IContributionItem[] items = generateMenuManager.getItems(); 383 for (int i = 0, len = items.length; i < len; i++) items[i].update(); 384 } 385 386 389 public void menuAboutToShow(IMenuManager menuManager) 390 { 391 generateAllAction.setEnabled(generateAllAction.isEnabled()); 392 generateSchemaAction.setEnabled(generateSchemaAction.isEnabled()); 393 generateTestsAction.setEnabled(generateTestsAction.isEnabled()); 394 generateEditorAction.setEnabled(generateEditorAction.isEnabled()); 395 generateEditAction.setEnabled(generateEditAction.isEnabled()); 396 generateAction.setEnabled(generateAction.isEnabled()); 397 refreshViewerAction.setEnabled(refreshViewerAction.isEnabled()); 398 399 super.menuAboutToShow(menuManager); 400 menuManager.insertBefore("additions", new Separator("generate-actions")); 401 menuManager.insertAfter("generate-actions", generateAllAction); 402 menuManager.insertAfter("generate-actions", generateTestsAction); 403 menuManager.insertAfter("generate-actions", generateEditorAction); 404 menuManager.insertAfter("generate-actions", generateEditAction); 405 menuManager.insertAfter("generate-actions", generateAction); 406 407 menuManager.insertBefore("additions", new Separator("schema-actions")); 408 menuManager.insertAfter("schema-actions", generateSchemaAction); 409 } 410 411 417 protected void addGlobalActions(IMenuManager menuManager) 418 { 419 menuManager.insertAfter("additions-end", new Separator("ui-actions")); 420 menuManager.insertAfter("ui-actions", showPropertiesViewAction); 421 422 refreshViewerAction.setEnabled(refreshViewerAction.isEnabled()); 423 menuManager.insertAfter("ui-actions", refreshViewerAction); 424 425 super.addGlobalActions(menuManager); 426 } 427 428 } 429 | Popular Tags |