carbidecpp22devenv/plugins/org.eclipse.gmf.templates.legacy_1.0.0.v20090614-0839/codegen.templates/xpt/editor/CreateShortcutAction.xpt
changeset 422 033392511bf7
equal deleted inserted replaced
421:631a44165bcf 422:033392511bf7
       
     1 /*
       
     2  * Copyright (c) 2006,2007 Borland Software Corporation
       
     3  * 
       
     4  * All rights reserved. This program and the accompanying materials
       
     5  * are made available under the terms of the Eclipse Public License v1.0
       
     6  * which accompanies this distribution, and is available at
       
     7  * http://www.eclipse.org/legal/epl-v10.html
       
     8  *
       
     9  * Contributors:
       
    10  *    Alexander Shatalin (Borland) - initial API and implementation
       
    11  */
       
    12 
       
    13 «IMPORT "http://www.eclipse.org/gmf/2008/GenModel"»
       
    14 «EXTENSION xpt::editor::Utils»
       
    15 
       
    16 «DEFINE CreateShortcutAction FOR gmfgen::GenDiagram-»
       
    17 	«EXPAND xpt::Common::copyright FOR editorGen-»
       
    18 package «editorGen.editor.packageName»;
       
    19 
       
    20 	«EXPAND xpt::Common::generatedClassComment»
       
    21 public class «createShortcutActionClassName» implements org.eclipse.ui.IObjectActionDelegate {
       
    22 
       
    23 	«EXPAND attributes-»
       
    24 
       
    25 	«EXPAND setActivePart-»
       
    26 	
       
    27 	«EXPAND selectionChanged-»
       
    28 	
       
    29 	«EXPAND isEnabled-»
       
    30 	
       
    31 	«EXPAND run-»
       
    32 
       
    33 	«EXPAND additions-»
       
    34 }
       
    35 «ENDDEFINE»
       
    36 
       
    37 «DEFINE attributes FOR gmfgen::GenDiagram-»
       
    38 	«EXPAND xpt::Common::generatedMemberComment»
       
    39 private «getEditPartQualifiedClassName()» mySelectedElement;
       
    40 	
       
    41 	«EXPAND xpt::Common::generatedMemberComment»
       
    42 private org.eclipse.swt.widgets.Shell myShell;
       
    43 «ENDDEFINE»
       
    44 
       
    45 «DEFINE setActivePart FOR gmfgen::GenDiagram-»
       
    46 	«EXPAND xpt::Common::generatedMemberComment»
       
    47 public void setActivePart(org.eclipse.jface.action.IAction action, org.eclipse.ui.IWorkbenchPart targetPart) {
       
    48 	myShell = targetPart.getSite().getShell();
       
    49 }
       
    50 «ENDDEFINE»
       
    51 
       
    52 «DEFINE selectionChanged FOR gmfgen::GenDiagram-»
       
    53 	«EXPAND xpt::Common::generatedMemberComment»
       
    54 public void selectionChanged(org.eclipse.jface.action.IAction action, org.eclipse.jface.viewers.ISelection selection) {
       
    55 	mySelectedElement = null;
       
    56 	if (selection instanceof org.eclipse.jface.viewers.IStructuredSelection) {
       
    57 		org.eclipse.jface.viewers.IStructuredSelection structuredSelection = (org.eclipse.jface.viewers.IStructuredSelection) selection;
       
    58 		if (structuredSelection.size() == 1 && structuredSelection.getFirstElement() instanceof «getEditPartQualifiedClassName()») {
       
    59 			mySelectedElement = («getEditPartQualifiedClassName()») structuredSelection.getFirstElement();
       
    60 		}
       
    61 	}
       
    62 	action.setEnabled(isEnabled());
       
    63 }
       
    64 «ENDDEFINE»
       
    65 	
       
    66 «DEFINE isEnabled FOR gmfgen::GenDiagram-»
       
    67 	«EXPAND xpt::Common::generatedMemberComment»
       
    68 private boolean isEnabled() {
       
    69 	return mySelectedElement != null;
       
    70 }
       
    71 «ENDDEFINE»
       
    72 
       
    73 «DEFINE run FOR gmfgen::GenDiagram-»
       
    74 	«EXPAND xpt::Common::generatedMemberComment»
       
    75 public void run(org.eclipse.jface.action.IAction action) {
       
    76 	final org.eclipse.gmf.runtime.notation.View view = (org.eclipse.gmf.runtime.notation.View) mySelectedElement.getModel();
       
    77 	«IF null == editorGen.application-»
       
    78 	«getElementChooserQualifiedClassName()» elementChooser = new «getElementChooserQualifiedClassName()»(myShell, view);
       
    79 	int result = elementChooser.open();
       
    80 	if (result != org.eclipse.jface.window.Window.OK) {
       
    81 		return;
       
    82 	}
       
    83 	org.eclipse.emf.common.util.URI selectedModelElementURI = elementChooser.getSelectedModelElementURI();
       
    84 	final org.eclipse.emf.ecore.EObject selectedElement;
       
    85 	try {
       
    86 		selectedElement = mySelectedElement.getEditingDomain().getResourceSet().getEObject(selectedModelElementURI, true);
       
    87 	} catch (org.eclipse.emf.common.util.WrappedException e) {
       
    88 		«editorGen.plugin.getActivatorQualifiedClassName()».getInstance().logError("Exception while loading object: " + selectedModelElementURI.toString(), e); «EXPAND xpt::Common::nonNLS»
       
    89 		return;
       
    90 	}
       
    91 
       
    92 	if (selectedElement == null) {
       
    93 		return;
       
    94 	}
       
    95 	org.eclipse.gmf.runtime.diagram.ui.requests.CreateViewRequest.ViewDescriptor viewDescriptor = new org.eclipse.gmf.runtime.diagram.ui.requests.CreateViewRequest.ViewDescriptor(new org.eclipse.gmf.runtime.emf.core.util.EObjectAdapter(selectedElement), org.eclipse.gmf.runtime.notation.Node.class, null, «editorGen.plugin.getActivatorQualifiedClassName()».DIAGRAM_PREFERENCES_HINT);
       
    96 	org.eclipse.gmf.runtime.common.core.command.ICommand command = new org.eclipse.gmf.runtime.diagram.ui.commands.CreateCommand(mySelectedElement.getEditingDomain(), viewDescriptor, view);
       
    97 	command = command.compose(new «getCreateShortcutDecorationsCommandQualifiedClassName()»(mySelectedElement.getEditingDomain(), view, viewDescriptor));
       
    98 	try {
       
    99 		org.eclipse.core.commands.operations.OperationHistoryFactory.getOperationHistory().execute(command, new org.eclipse.core.runtime.NullProgressMonitor(), null);
       
   100 	} catch (org.eclipse.core.commands.ExecutionException e) {
       
   101 		«editorGen.plugin.getActivatorQualifiedClassName()».getInstance().logError("Unable to create shortcut", e); «EXPAND xpt::Common::nonNLS»
       
   102 	}
       
   103 	«ELSE-»
       
   104 	org.eclipse.emf.ecore.resource.Resource resource = «getDiagramEditorUtilQualifiedClassName()».openModel(myShell, «EXPAND xpt::Externalizer::accessorCall(titleKey(i18nKeyForOpenModel(this))) FOR editorGen», mySelectedElement.getEditingDomain());
       
   105 	if (resource == null || resource.getContents().isEmpty()) {
       
   106 		return;
       
   107 	}
       
   108 	«EXPAND xpt::editor::ShortcutCreationWizard::qualifiedClassName» wizard = new «EXPAND xpt::editor::ShortcutCreationWizard::qualifiedClassName»((org.eclipse.emf.ecore.EObject) resource.getContents().get(0), view, mySelectedElement.getEditingDomain());
       
   109 	wizard.setWindowTitle(«EXPAND xpt::Externalizer::accessorCall(titleKey(i18nKeyForCreateShortcus(this))) FOR editorGen»);
       
   110 	«getDiagramEditorUtilQualifiedClassName()».runWizard(myShell, wizard, "CreateShortcut"); «EXPAND xpt::Common::nonNLS»
       
   111 	«ENDIF-»
       
   112 	}
       
   113 «ENDDEFINE»
       
   114 
       
   115 «DEFINE additions FOR gmfgen::GenDiagram-»
       
   116 «ENDDEFINE»
       
   117 
       
   118 «DEFINE i18nValues FOR gmfgen::GenDiagram-»
       
   119 «IF null != editorGen.application-»
       
   120 «EXPAND xpt::Externalizer::messageEntry(titleKey(i18nKeyForOpenModel(this)), "Select model to reference")-»
       
   121 «EXPAND xpt::Externalizer::messageEntry(titleKey(i18nKeyForCreateShortcus(this)), "Create shortcut")-»
       
   122 «ENDIF-»
       
   123 «ENDDEFINE»
       
   124 
       
   125 «DEFINE i18nAccessors FOR gmfgen::GenDiagram-»
       
   126 	«IF null != editorGen.application-»
       
   127 		«EXPAND xpt::Externalizer::accessorField(titleKey(i18nKeyForOpenModel(this)))-»
       
   128 		«EXPAND xpt::Externalizer::accessorField(titleKey(i18nKeyForCreateShortcus(this)))-»
       
   129 	«ENDIF-»
       
   130 «ENDDEFINE»