001: package newprocess.diagram.part;
002:
003: import java.io.IOException;
004: import java.util.ArrayList;
005: import java.util.Collections;
006: import java.util.HashMap;
007: import java.util.Iterator;
008: import java.util.List;
009: import java.util.Map;
010: import java.util.Set;
011:
012: import newprocess.Globals;
013: import newprocess.Loader;
014: import newprocess.NewprocessFactory;
015: import newprocess.NewprocessPackage;
016: import newprocess.Process;
017: import newprocess.diagram.edit.parts.ProcessEditPart;
018:
019: import org.eclipse.core.commands.ExecutionException;
020: import org.eclipse.core.commands.operations.OperationHistoryFactory;
021: import org.eclipse.core.resources.IFile;
022: import org.eclipse.core.resources.IResource;
023: import org.eclipse.core.resources.ResourcesPlugin;
024: import org.eclipse.core.runtime.CoreException;
025: import org.eclipse.core.runtime.IAdaptable;
026: import org.eclipse.core.runtime.IPath;
027: import org.eclipse.core.runtime.IProgressMonitor;
028: import org.eclipse.core.runtime.NullProgressMonitor;
029: import org.eclipse.core.runtime.Path;
030: import org.eclipse.core.runtime.SubProgressMonitor;
031: import org.eclipse.emf.common.util.URI;
032: import org.eclipse.emf.ecore.EClass;
033: import org.eclipse.emf.ecore.EObject;
034: import org.eclipse.emf.ecore.EReference;
035: import org.eclipse.emf.ecore.resource.Resource;
036: import org.eclipse.emf.ecore.xmi.XMIResource;
037: import org.eclipse.emf.ecore.xmi.XMLResource;
038: import org.eclipse.emf.edit.ui.util.EditUIUtil;
039: import org.eclipse.emf.transaction.TransactionalEditingDomain;
040: import org.eclipse.gef.EditPart;
041: import org.eclipse.gmf.runtime.common.core.command.CommandResult;
042: import org.eclipse.gmf.runtime.diagram.core.services.ViewService;
043: import org.eclipse.gmf.runtime.diagram.ui.editparts.DiagramEditPart;
044: import org.eclipse.gmf.runtime.diagram.ui.editparts.IGraphicalEditPart;
045: import org.eclipse.gmf.runtime.diagram.ui.editparts.IPrimaryEditPart;
046: import org.eclipse.gmf.runtime.diagram.ui.parts.IDiagramGraphicalViewer;
047: import org.eclipse.gmf.runtime.diagram.ui.parts.IDiagramWorkbenchPart;
048: import org.eclipse.gmf.runtime.emf.commands.core.command.AbstractTransactionalCommand;
049: import org.eclipse.gmf.runtime.emf.core.GMFEditingDomainFactory;
050: import org.eclipse.gmf.runtime.emf.core.util.EMFCoreUtil;
051: import org.eclipse.gmf.runtime.notation.Diagram;
052: import org.eclipse.gmf.runtime.notation.View;
053: import org.eclipse.ui.PartInitException;
054:
055: /**
056: * @generated
057: */
058: public class New_processDiagramEditorUtil {
059:
060: /**
061: * @generated
062: */
063: public static boolean openDiagram(Resource diagram)
064: throws PartInitException {
065: return EditUIUtil.openEditor((EObject) diagram.getContents()
066: .get(0));
067: }
068:
069: /**
070: * @generated
071: */
072: private static void setCharset(URI uri) {
073: IFile file = getFile(uri);
074: if (file == null) {
075: return;
076: }
077: try {
078: file.setCharset("UTF-8", new NullProgressMonitor()); //$NON-NLS-1$
079: } catch (CoreException e) {
080: New_processDiagramEditorPlugin
081: .getInstance()
082: .logError(
083: "Unable to set charset for file " + file.getFullPath(), e); //$NON-NLS-1$
084: }
085: }
086:
087: /**
088: * @generated
089: */
090: public static IFile getFile(URI uri) {
091: if (uri.toString().startsWith("platform:/resource")) { //$NON-NLS-1$
092: String path = uri.toString().substring(
093: "platform:/resource".length()); //$NON-NLS-1$
094: IResource workspaceResource = ResourcesPlugin
095: .getWorkspace().getRoot()
096: .findMember(new Path(path));
097: if (workspaceResource instanceof IFile) {
098: return (IFile) workspaceResource;
099: }
100: }
101: return null;
102: }
103:
104: /**
105: * @generated
106: */
107: public static boolean exists(IPath path) {
108: return ResourcesPlugin.getWorkspace().getRoot().exists(path);
109: }
110:
111: /**
112: * <p>
113: * This method should be called within a workspace modify operation since it creates resources.
114: * </p>
115: * @generated
116: * @return the created resource, or <code>null</code> if the resource was not created
117: */
118: public static final Resource createDiagram(URI diagramURI,
119: URI modelURI, IProgressMonitor progressMonitor) {
120: TransactionalEditingDomain editingDomain = GMFEditingDomainFactory.INSTANCE
121: .createEditingDomain();
122: progressMonitor
123: .beginTask("Creating diagram and model files", 3);
124: final Resource diagramResource = editingDomain.getResourceSet()
125: .createResource(diagramURI);
126: final Resource modelResource = editingDomain.getResourceSet()
127: .createResource(modelURI);
128: final String diagramName = diagramURI.lastSegment();
129: AbstractTransactionalCommand command = new AbstractTransactionalCommand(
130: editingDomain,
131: "Creating diagram and model", Collections.EMPTY_LIST) { //$NON-NLS-1$
132: protected CommandResult doExecuteWithResult(
133: IProgressMonitor monitor, IAdaptable info)
134: throws ExecutionException {
135: Process model = createInitialModel();
136: attachModelToResource(model, modelResource);
137: Diagram diagram = ViewService
138: .createDiagram(
139: model,
140: ProcessEditPart.MODEL_ID,
141: New_processDiagramEditorPlugin.DIAGRAM_PREFERENCES_HINT);
142: if (diagram != null) {
143: diagramResource.getContents().add(diagram);
144: diagram.setName(diagramName);
145: diagram.setElement(model);
146: }
147: try {
148: Map options = new HashMap();
149: options.put(XMIResource.OPTION_ENCODING, "UTF-8"); //$NON-NLS-1$
150: modelResource.save(options);
151: diagramResource.save(options);
152: } catch (IOException e) {
153:
154: New_processDiagramEditorPlugin
155: .getInstance()
156: .logError(
157: "Unable to store model and diagram resources", e); //$NON-NLS-1$
158: }
159: return CommandResult.newOKCommandResult();
160: }
161: };
162: try {
163: OperationHistoryFactory.getOperationHistory().execute(
164: command,
165: new SubProgressMonitor(progressMonitor, 1), null);
166: } catch (ExecutionException e) {
167: New_processDiagramEditorPlugin.getInstance().logError(
168: "Unable to create model and diagram", e); //$NON-NLS-1$
169: }
170: setCharset(modelURI);
171: setCharset(diagramURI);
172: return diagramResource;
173: }
174:
175: /**
176: * Create a new instance of domain element associated with canvas.
177: * <!-- begin-user-doc -->
178: * <!-- end-user-doc -->
179: * @generated NOT
180: * @author sh
181: */
182: private static Process createInitialModel() {
183: //return NewprocessFactory.eINSTANCE.createProcess();
184: // ************* custom *****************************
185: Process process = NewprocessFactory.eINSTANCE.createProcess();
186:
187: // create the Globals
188: EReference globalsContainer = NewprocessPackage.eINSTANCE
189: .getProcess_HasGlobals();
190: Globals globals = NewprocessFactory.eINSTANCE.createGlobals();
191: EClass globalsClass = globals.eClass();
192: EObject globalsNode = EMFCoreUtil.create(process,
193: globalsContainer, globalsClass);
194:
195: // create the Loader
196: EReference loaderContainer = NewprocessPackage.eINSTANCE
197: .getGlobals_HasLoader();
198: Loader loader = NewprocessFactory.eINSTANCE.createLoader();
199: EClass loaderEClass = loader.eClass();
200: EMFCoreUtil.create(globalsNode, loaderContainer, loaderEClass);
201: return process;
202: }
203:
204: /**
205: * Store model element in the resource.
206: * <!-- begin-user-doc -->
207: * <!-- end-user-doc -->
208: * @generated
209: */
210: private static void attachModelToResource(Process model,
211: Resource resource) {
212: resource.getContents().add(model);
213: }
214:
215: /**
216: * @generated
217: */
218: public static void selectElementsInDiagram(
219: IDiagramWorkbenchPart diagramPart,
220: List/*EditPart*/editParts) {
221: diagramPart.getDiagramGraphicalViewer().deselectAll();
222:
223: EditPart firstPrimary = null;
224: for (Iterator it = editParts.iterator(); it.hasNext();) {
225: EditPart nextPart = (EditPart) it.next();
226: diagramPart.getDiagramGraphicalViewer().appendSelection(
227: nextPart);
228: if (firstPrimary == null
229: && nextPart instanceof IPrimaryEditPart) {
230: firstPrimary = nextPart;
231: }
232: }
233:
234: if (!editParts.isEmpty()) {
235: diagramPart.getDiagramGraphicalViewer().reveal(
236: firstPrimary != null ? firstPrimary
237: : (EditPart) editParts.get(0));
238: }
239: }
240:
241: /**
242: * @generated
243: */
244: public static View findView(DiagramEditPart diagramEditPart,
245: EObject targetElement,
246: LazyElement2ViewMap lazyElement2ViewMap) {
247: boolean hasStructuralURI = false;
248: if (targetElement.eResource() instanceof XMLResource) {
249: hasStructuralURI = ((XMLResource) targetElement.eResource())
250: .getID(targetElement) == null;
251: }
252:
253: View view = null;
254: if (hasStructuralURI
255: && !lazyElement2ViewMap.getElement2ViewMap().isEmpty()) {
256: view = (View) lazyElement2ViewMap.getElement2ViewMap().get(
257: targetElement);
258: } else if (findElementsInDiagramByID(diagramEditPart,
259: targetElement, lazyElement2ViewMap.editPartTmpHolder) > 0) {
260: EditPart editPart = (EditPart) lazyElement2ViewMap.editPartTmpHolder
261: .get(0);
262: lazyElement2ViewMap.editPartTmpHolder.clear();
263: view = editPart.getModel() instanceof View ? (View) editPart
264: .getModel()
265: : null;
266: }
267:
268: return (view == null) ? diagramEditPart.getDiagramView() : view;
269: }
270:
271: /**
272: * @generated
273: */
274: private static int findElementsInDiagramByID(
275: DiagramEditPart diagramPart, EObject element,
276: List editPartCollector) {
277: IDiagramGraphicalViewer viewer = (IDiagramGraphicalViewer) diagramPart
278: .getViewer();
279: final int intialNumOfEditParts = editPartCollector.size();
280:
281: if (element instanceof View) { // support notation element lookup
282: EditPart editPart = (EditPart) viewer.getEditPartRegistry()
283: .get(element);
284: if (editPart != null) {
285: editPartCollector.add(editPart);
286: return 1;
287: }
288: }
289:
290: String elementID = EMFCoreUtil.getProxyID(element);
291: List associatedParts = viewer.findEditPartsForElement(
292: elementID, IGraphicalEditPart.class);
293: // perform the possible hierarchy disjoint -> take the top-most parts only
294: for (Iterator editPartIt = associatedParts.iterator(); editPartIt
295: .hasNext();) {
296: EditPart nextPart = (EditPart) editPartIt.next();
297: EditPart parentPart = nextPart.getParent();
298: while (parentPart != null
299: && !associatedParts.contains(parentPart)) {
300: parentPart = parentPart.getParent();
301: }
302: if (parentPart == null) {
303: editPartCollector.add(nextPart);
304: }
305: }
306:
307: if (intialNumOfEditParts == editPartCollector.size()) {
308: if (!associatedParts.isEmpty()) {
309: editPartCollector
310: .add(associatedParts.iterator().next());
311: } else {
312: if (element.eContainer() != null) {
313: return findElementsInDiagramByID(diagramPart,
314: element.eContainer(), editPartCollector);
315: }
316: }
317: }
318: return editPartCollector.size() - intialNumOfEditParts;
319: }
320:
321: /**
322: * @generated
323: */
324: public static class LazyElement2ViewMap {
325: /**
326: * @generated
327: */
328: private Map element2ViewMap;
329: /**
330: * @generated
331: */
332: private View scope;
333: /**
334: * @generated
335: */
336: private Set elementSet;
337: /**
338: * @generated
339: */
340: public final List editPartTmpHolder = new ArrayList();
341:
342: /**
343: * @generated
344: */
345: public LazyElement2ViewMap(View scope, Set elements) {
346: this .scope = scope;
347: this .elementSet = elements;
348: }
349:
350: /**
351: * @generated
352: */
353: public final Map getElement2ViewMap() {
354: if (element2ViewMap == null) {
355: element2ViewMap = new HashMap();
356: // map possible notation elements to itself as these can't be found by view.getElement()
357: for (Iterator it = elementSet.iterator(); it.hasNext();) {
358: EObject element = (EObject) it.next();
359: if (element instanceof View) {
360: View view = (View) element;
361: if (view.getDiagram() == scope.getDiagram()) {
362: element2ViewMap.put(element, element); // take only those that part of our diagram
363: }
364: }
365: }
366:
367: buildElement2ViewMap(scope, element2ViewMap, elementSet);
368: }
369: return element2ViewMap;
370: }
371:
372: /**
373: * @generated
374: */
375: static Map buildElement2ViewMap(View parentView,
376: Map element2ViewMap, Set elements) {
377: if (elements.size() == element2ViewMap.size())
378: return element2ViewMap;
379:
380: if (parentView.isSetElement()
381: && !element2ViewMap.containsKey(parentView
382: .getElement())
383: && elements.contains(parentView.getElement())) {
384: element2ViewMap
385: .put(parentView.getElement(), parentView);
386: if (elements.size() == element2ViewMap.size())
387: return element2ViewMap;
388: }
389:
390: for (Iterator it = parentView.getChildren().iterator(); it
391: .hasNext();) {
392: buildElement2ViewMap((View) it.next(), element2ViewMap,
393: elements);
394: if (elements.size() == element2ViewMap.size())
395: return element2ViewMap;
396: }
397: for (Iterator it = parentView.getSourceEdges().iterator(); it
398: .hasNext();) {
399: buildElement2ViewMap((View) it.next(), element2ViewMap,
400: elements);
401: if (elements.size() == element2ViewMap.size())
402: return element2ViewMap;
403: }
404: for (Iterator it = parentView.getSourceEdges().iterator(); it
405: .hasNext();) {
406: buildElement2ViewMap((View) it.next(), element2ViewMap,
407: elements);
408: if (elements.size() == element2ViewMap.size())
409: return element2ViewMap;
410: }
411: return element2ViewMap;
412: }
413: } //LazyElement2ViewMap
414: }
|