]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/typicals/NewMasterTypicalDiagram.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.modeling.ui / src / org / simantics / modeling / ui / typicals / NewMasterTypicalDiagram.java
diff --git a/bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/typicals/NewMasterTypicalDiagram.java b/bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/typicals/NewMasterTypicalDiagram.java
new file mode 100644 (file)
index 0000000..e6dc21c
--- /dev/null
@@ -0,0 +1,96 @@
+/*******************************************************************************\r
+ * Copyright (c) 2012 Association for Decentralized Information Management in\r
+ * Industry THTH ry.\r
+ * All rights reserved. This program and the accompanying materials\r
+ * are made available under the terms of the Eclipse Public License v1.0\r
+ * which accompanies this distribution, and is available at\r
+ * http://www.eclipse.org/legal/epl-v10.html\r
+ *\r
+ * Contributors:\r
+ *     VTT Technical Research Centre of Finland - initial API and implementation\r
+ *******************************************************************************/\r
+package org.simantics.modeling.ui.typicals;\r
+\r
+import java.lang.reflect.InvocationTargetException;\r
+import org.eclipse.core.runtime.IProgressMonitor;\r
+import org.eclipse.jface.operation.IRunnableWithProgress;\r
+import org.eclipse.swt.widgets.Shell;\r
+import org.eclipse.ui.IWorkbenchWindow;\r
+import org.eclipse.ui.PlatformUI;\r
+import org.simantics.Simantics;\r
+import org.simantics.browsing.ui.common.ErrorLogger;\r
+import org.simantics.db.Resource;\r
+import org.simantics.db.Session;\r
+import org.simantics.db.exception.DatabaseException;\r
+import org.simantics.db.layer0.adapter.ActionFactory;\r
+import org.simantics.modeling.ModelingResources;\r
+import org.simantics.modeling.typicals.TypicalUtil;\r
+import org.simantics.ui.workbench.action.ChooseActionRequest;\r
+import org.simantics.utils.ui.SWTUtils;\r
+import org.simantics.utils.ui.workbench.WorkbenchUtils;\r
+\r
+/**\r
+ * Creates a new master typical diagram and its corresponding composites.\r
+ * \r
+ * <p>\r
+ * The created typical composite and diagram type are specified by the model\r
+ * using {@link ModelingResources#StructuralModel_HasTypicalCompositeBaseType}\r
+ * and {@link ModelingResources#StructuralModel_HasTypicalDiagramBaseType}.\r
+ * \r
+ * <p>\r
+ * Marks the created master composite with the model-specified master composite\r
+ * type to support searching. The master type is specified by the model using\r
+ * {@link ModelingResources#StructuralModel_HasMasterTypicalCompositeType}.\r
+ * \r
+ * <p>\r
+ * Clones symbol contributions from the sources specified by the model through\r
+ * {@link ModelingResources#StructuralModel_CloneTypicalDiagramSymbolContributionsFrom}\r
+ * .\r
+ * \r
+ * @author Tuukka Lehtonen\r
+ */\r
+public class NewMasterTypicalDiagram implements ActionFactory {\r
+\r
+    @Override\r
+    public Runnable create(Object target_) {\r
+        final Resource target = (Resource)target_;\r
+        return new Runnable() {\r
+            @Override\r
+            public void run() {\r
+               try {\r
+                       PlatformUI.getWorkbench().getProgressService().busyCursorWhile(new IRunnableWithProgress() {\r
+                               @Override\r
+                        public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {\r
+                                       try {\r
+                                               Resource composite = TypicalUtil.newMasterTypical(target);\r
+                                               \r
+                               // Perform default action on the newly created diagram\r
+                               performDefaultAction(Simantics.getSession(), composite);\r
+                                       } catch (DatabaseException e) {\r
+                                               throw new InvocationTargetException(e);\r
+                                       } finally {\r
+                                               monitor.done();\r
+                                       }\r
+                               }\r
+                       });\r
+               } catch (InvocationTargetException e) {\r
+                       ErrorLogger.defaultLogError(e);\r
+               } catch (InterruptedException e) {\r
+                       ErrorLogger.defaultLogError(e);\r
+               }\r
+            }\r
+        };\r
+    };\r
+\r
+    private static void performDefaultAction(final Session session, final Object input) {\r
+        SWTUtils.asyncExec(PlatformUI.getWorkbench().getDisplay(), new Runnable() {\r
+            @Override\r
+            public void run() {\r
+                IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();\r
+                Shell shell = window != null ? window.getShell() : null;\r
+                session.asyncRequest(new ChooseActionRequest(shell, null, input, WorkbenchUtils.getCurrentPerspectiveId(), false, false, true));\r
+            }\r
+        });\r
+    }\r
+\r
+}\r