carbidecpp22devenv/plugins/org.eclipse.gmf.templates.legacy_1.0.0.v20090614-0839/codegen.lite.templates/xpt/editor/DiagramEditorUtil.xpt
changeset 422 033392511bf7
equal deleted inserted replaced
421:631a44165bcf 422:033392511bf7
       
     1 /*
       
     2  * Copyright (c) 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  *    bblajer (Borland) - initial API and implementation
       
    11  */
       
    12 
       
    13 «IMPORT "http://www.eclipse.org/gmf/2008/GenModel"»
       
    14 «IMPORT "http://www.eclipse.org/emf/2002/Ecore"»
       
    15 «IMPORT "http://www.eclipse.org/emf/2002/GenModel"»
       
    16 «EXTENSION xpt::GenModelUtils»
       
    17 «EXTENSION xpt::editor::i18n»
       
    18 
       
    19 «DEFINE DiagramEditorUtil FOR gmfgen::GenDiagram-»
       
    20 «EXPAND xpt::Common::copyright FOR editorGen»
       
    21 package «editorGen.editor.packageName»;
       
    22 
       
    23 «EXPAND xpt::Common::generatedClassComment»
       
    24 public class «diagramEditorUtilClassName» «EXPAND supertypes» {
       
    25 	«EXPAND openFilePathDialog-»
       
    26 «IF editorGen.editor.eclipseEditor-»
       
    27 	«EXPAND openEditor-»
       
    28 «ELSE-»
       
    29 	«EXPAND showView-»
       
    30 «ENDIF-»
       
    31 	«EXPAND initializeDiagram-»
       
    32 «IF !editorGen.sameFileForDiagramAndModel-»
       
    33 	«EXPAND convertURIs-»
       
    34 «ENDIF-»
       
    35 	«EXPAND isReadOnly-»
       
    36 «IF editorGen.application == null-»
       
    37 	«EXPAND setCharset-»
       
    38 «ENDIF-»
       
    39 	«EXPAND saveOptions-»
       
    40 	«EXPAND isKnownShortcutModelID-»
       
    41 	«EXPAND additions-»
       
    42 }
       
    43 «ENDDEFINE»
       
    44 
       
    45 «DEFINE supertypes FOR gmfgen::GenDiagram»«ENDDEFINE»
       
    46 
       
    47 «DEFINE openFilePathDialog FOR gmfgen::GenDiagram-»
       
    48 «EXPAND xpt::Common::generatedMemberComment»
       
    49 public static String openFilePathDialog(org.eclipse.swt.widgets.Shell shell, String fileExtensionFilter, int style) {
       
    50 	org.eclipse.swt.widgets.FileDialog fileDialog = new org.eclipse.swt.widgets.FileDialog(shell, style);
       
    51 	fileDialog.setFilterExtensions(new String[]{fileExtensionFilter});
       
    52 
       
    53 	fileDialog.open();
       
    54 	if (fileDialog.getFileName() != null && fileDialog.getFileName().length() > 0) {
       
    55 		return fileDialog.getFilterPath() + java.io.File.separator + fileDialog.getFileName();
       
    56 	}
       
    57 	else {
       
    58 		return null;
       
    59 	}
       
    60 }
       
    61 «ENDDEFINE»
       
    62 
       
    63 «DEFINE openEditor FOR gmfgen::GenDiagram-»
       
    64 «EXPAND xpt::Common::generatedMemberComment»
       
    65 public static org.eclipse.ui.IEditorPart openEditor(org.eclipse.emf.common.util.URI fileURI) {
       
    66 	org.eclipse.ui.IWorkbench workbench = org.eclipse.ui.PlatformUI.getWorkbench();
       
    67 	org.eclipse.ui.IWorkbenchWindow workbenchWindow = workbench.getActiveWorkbenchWindow();
       
    68 	org.eclipse.ui.IWorkbenchPage page = workbenchWindow.getActivePage();
       
    69 
       
    70 	org.eclipse.ui.IEditorDescriptor editorDescriptor = workbench.getEditorRegistry().getDefaultEditor(fileURI.lastSegment());
       
    71 	if (editorDescriptor == null) {
       
    72 		org.eclipse.jface.dialogs.MessageDialog.openError(
       
    73 		workbenchWindow.getShell(),
       
    74 		«EXPAND xpt::Externalizer::accessorCall(i18nKeyForDiagramEditorUtilErrorNoDescriptor().titleKey()) FOR editorGen»,
       
    75 		java.text.MessageFormat.format(«EXPAND xpt::Externalizer::accessorCall(i18nKeyForDiagramEditorUtilErrorNoDescriptor().messageKey()) FOR editorGen», new Object[] {fileURI.toFileString()}));
       
    76 		return null;
       
    77 	} else {
       
    78 		try {
       
    79 			return page.openEditor(new org.eclipse.emf.common.ui.URIEditorInput(fileURI), editorDescriptor.getId());
       
    80 		} catch (org.eclipse.ui.PartInitException exception) {
       
    81 			org.eclipse.jface.dialogs.MessageDialog.openError(
       
    82 				workbenchWindow.getShell(),
       
    83 				«EXPAND xpt::Externalizer::accessorCall(i18nKeyForDiagramEditorUtilErrorOpeningEditor()) FOR editorGen»,	
       
    84 				exception.getMessage());
       
    85 			return null;
       
    86 		}
       
    87 	}
       
    88 }
       
    89 «ENDDEFINE»
       
    90 
       
    91 «DEFINE showView FOR gmfgen::GenDiagram-»
       
    92 «EXPAND xpt::Common::generatedMemberComment»
       
    93 public static org.eclipse.ui.IViewPart showView(org.eclipse.emf.common.util.URI fileURI) {
       
    94 	org.eclipse.ui.IWorkbench workbench = org.eclipse.ui.PlatformUI.getWorkbench();
       
    95 	org.eclipse.ui.IWorkbenchWindow workbenchWindow = workbench.getActiveWorkbenchWindow();
       
    96 	org.eclipse.ui.IWorkbenchPage page = workbenchWindow.getActivePage();
       
    97 
       
    98 	try {
       
    99 		org.eclipse.gmf.runtime.lite.parts.DiagramViewPart diagramView = (org.eclipse.gmf.runtime.lite.parts.DiagramViewPart) page.showView(«editorGen.editor.getQualifiedClassName()».ID);
       
   100 		if (!diagramView.showDiagram(null, fileURI)) {
       
   101 			return null;
       
   102 		}
       
   103 		return diagramView;
       
   104 	} catch (org.eclipse.ui.PartInitException exception) {
       
   105 		org.eclipse.jface.dialogs.MessageDialog.openError(
       
   106 			workbenchWindow.getShell(),
       
   107 			«EXPAND xpt::Externalizer::accessorCall(i18nKeyForDiagramEditorUtilErrorOpeningView()) FOR editorGen», 
       
   108 			exception.getMessage());
       
   109 		return null;
       
   110 	}
       
   111 }
       
   112 «ENDDEFINE»
       
   113 
       
   114 «DEFINE convertURIs FOR gmfgen::GenDiagram-»
       
   115 «EXPAND xpt::Common::generatedMemberComment»
       
   116 public static org.eclipse.emf.common.util.URI getDiagramFileURI(org.eclipse.emf.common.util.URI modelFileURI) {
       
   117 	if (modelFileURI == null || !modelFileURI.isFile()) {
       
   118 		return null;
       
   119 	}
       
   120 	return modelFileURI.trimFileExtension().appendFileExtension("«editorGen.diagramFileExtension»");	«EXPAND xpt::Common::nonNLS»
       
   121 }
       
   122 
       
   123 «EXPAND xpt::Common::generatedMemberComment»
       
   124 public static org.eclipse.emf.common.util.URI getModelFileURI(org.eclipse.emf.common.util.URI diagramFileURI) {
       
   125 	if (diagramFileURI == null || !diagramFileURI.isFile()) {
       
   126 		return null;
       
   127 	}
       
   128 	return diagramFileURI.trimFileExtension().appendFileExtension("«editorGen.domainFileExtension»");	«EXPAND xpt::Common::nonNLS»
       
   129 }
       
   130 «ENDDEFINE»
       
   131 
       
   132 «DEFINE isReadOnly FOR gmfgen::GenDiagram-»
       
   133 «EXPAND xpt::Common::generatedMemberComment("Returns whether the given element is read only in its editing domain.")»
       
   134 public static boolean isReadOnly(org.eclipse.emf.ecore.EObject element) {
       
   135 	if (element == null) {
       
   136 		return true;
       
   137 	}
       
   138 	org.eclipse.emf.ecore.resource.Resource resource = element.eResource();
       
   139 	if (resource == null) {
       
   140 		return false;
       
   141 	}
       
   142 	return isReadOnly(resource);
       
   143 }
       
   144 
       
   145 «EXPAND xpt::Common::generatedMemberComment»
       
   146 public static boolean isReadOnly(org.eclipse.emf.ecore.resource.Resource resource) {
       
   147 	org.eclipse.emf.transaction.TransactionalEditingDomain editingDomain = org.eclipse.emf.transaction.util.TransactionUtil.getEditingDomain(resource);
       
   148 	if (editingDomain == null) {
       
   149 		return false;
       
   150 	}
       
   151 	return editingDomain.isReadOnly(resource);
       
   152 }
       
   153 «ENDDEFINE»
       
   154 
       
   155 «DEFINE initializeDiagram FOR gmfgen::GenDiagram-»
       
   156 «EXPAND xpt::Common::generatedMemberComment»
       
   157 public static org.eclipse.gmf.runtime.notation.Diagram createDiagramFor(org.eclipse.emf.ecore.EObject diagramRoot) {
       
   158 	if («getEditPartQualifiedClassName()».VISUAL_ID != «getVisualIDRegistryQualifiedClassName()».getDiagramVisualID(diagramRoot)) {
       
   159 		return null;
       
   160 	}
       
   161 	org.eclipse.gmf.runtime.notation.Diagram result = org.eclipse.gmf.runtime.notation.NotationFactory.eINSTANCE.createDiagram();
       
   162 	result.setElement(diagramRoot);
       
   163 	«getNotationViewFactoryQualifiedClassName()».INSTANCE.decorateView(result);
       
   164 	new DiagramInitializer().initDiagramContents(result);
       
   165 	return result;
       
   166 }
       
   167 
       
   168 «EXPAND xpt::editor::DiagramInitializer::DiagramInitializer-»
       
   169 «ENDDEFINE»
       
   170 
       
   171 «DEFINE setCharset FOR gmfgen::GenDiagram-»
       
   172 «EXPAND xpt::Common::generatedMemberComment»
       
   173 public static void setCharset(org.eclipse.core.resources.IFile file) {
       
   174 	if (file == null) {
       
   175 		return;
       
   176 	}
       
   177 	try {
       
   178 		file.setCharset("UTF-8", new org.eclipse.core.runtime.NullProgressMonitor()); «EXPAND xpt::Common::nonNLS»
       
   179 	} catch (org.eclipse.core.runtime.CoreException e) {
       
   180 		«editorGen.plugin.getActivatorQualifiedClassName()».getInstance().logError("Unable to set charset for file " + file.getFullPath(), e); «EXPAND xpt::Common::nonNLS»
       
   181 	}
       
   182 }	
       
   183 «ENDDEFINE»
       
   184 
       
   185 «DEFINE saveOptions FOR gmfgen::GenDiagram-»
       
   186 «EXPAND xpt::Common::generatedMemberComment»
       
   187 public static java.util.Map getSaveOptions() {
       
   188 	java.util.Map saveOptions = new java.util.HashMap();
       
   189 	saveOptions.put(org.eclipse.emf.ecore.xmi.XMLResource.OPTION_ENCODING, "UTF-8"); «EXPAND xpt::Common::nonNLS»
       
   190 	saveOptions.put(org.eclipse.emf.ecore.xmi.XMLResource.OPTION_RECORD_UNKNOWN_FEATURE, Boolean.TRUE);
       
   191 	saveOptions.put(org.eclipse.emf.ecore.resource.Resource.OPTION_SAVE_ONLY_IF_CHANGED, org.eclipse.emf.ecore.resource.Resource.OPTION_SAVE_ONLY_IF_CHANGED_MEMORY_BUFFER);
       
   192 	return saveOptions;
       
   193 }
       
   194 «ENDDEFINE»
       
   195 
       
   196 «DEFINE isKnownShortcutModelID FOR gmfgen::GenDiagram-»
       
   197 «IF containsShortcutsTo.size() > 0-»
       
   198 «EXPAND xpt::Common::generatedMemberComment»
       
   199 public static boolean isKnownShortcutModelID(String modelID) {
       
   200 	«EXPAND acceptModelID(this) FOREACH containsShortcutsTo-»
       
   201 	return false;
       
   202 }
       
   203 «ENDIF-»
       
   204 «ENDDEFINE»
       
   205 
       
   206 «DEFINE acceptModelID(gmfgen::GenDiagram diagram) FOR String-»
       
   207 «IF this == diagram.editorGen.modelID-»
       
   208 if («diagram.getEditPartQualifiedClassName()».MODEL_ID.equals(modelID)) {
       
   209 «ELSE-»
       
   210 if ("«this»".equals(modelID)) {	«EXPAND xpt::Common::nonNLS»
       
   211 «ENDIF-»
       
   212 	return true;
       
   213 }
       
   214 «ENDDEFINE»
       
   215 
       
   216 «DEFINE additions FOR gmfgen::GenDiagram»«ENDDEFINE»
       
   217 
       
   218 «DEFINE i18nAccessors FOR gmfgen::GenDiagram-»
       
   219 «IF editorGen.editor.eclipseEditor-»
       
   220 «EXPAND xpt::Externalizer::accessorField(i18nKeyForDiagramEditorUtilErrorOpeningEditor())-»
       
   221 «EXPAND xpt::Externalizer::accessorField(i18nKeyForDiagramEditorUtilErrorNoDescriptor().titleKey())-»
       
   222 «EXPAND xpt::Externalizer::accessorField(i18nKeyForDiagramEditorUtilErrorNoDescriptor().messageKey())-»
       
   223 «ELSE-»
       
   224 «EXPAND xpt::Externalizer::accessorField(i18nKeyForDiagramEditorUtilErrorOpeningView())-»
       
   225 «ENDIF-»
       
   226 «ENDDEFINE»
       
   227 
       
   228 «DEFINE i18nValues FOR gmfgen::GenDiagram-»
       
   229 «IF editorGen.editor.eclipseEditor-»
       
   230 «EXPAND xpt::Externalizer::messageEntry(i18nKeyForDiagramEditorUtilErrorOpeningEditor(), "Error Opening Editor")-»
       
   231 «EXPAND xpt::Externalizer::messageEntry(i18nKeyForDiagramEditorUtilErrorNoDescriptor().titleKey(), "Error")-»
       
   232 «EXPAND xpt::Externalizer::messageEntry(i18nKeyForDiagramEditorUtilErrorNoDescriptor().messageKey(), "There is no editor registered for the file {0}")-»
       
   233 «ELSE-»
       
   234 «EXPAND xpt::Externalizer::messageEntry(i18nKeyForDiagramEditorUtilErrorOpeningView(), "Error Opening View")-»
       
   235 «ENDIF-»
       
   236 «ENDDEFINE»