]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/typicals/NewMasterTypicalDiagram.java
Merge "Testing SonarQube with Simantics Platform SDK"
[simantics/platform.git] / bundles / org.simantics.modeling.ui / src / org / simantics / modeling / ui / typicals / NewMasterTypicalDiagram.java
1 /*******************************************************************************\r
2  * Copyright (c) 2012 Association for Decentralized Information Management in\r
3  * Industry THTH ry.\r
4  * All rights reserved. This program and the accompanying materials\r
5  * are made available under the terms of the Eclipse Public License v1.0\r
6  * which accompanies this distribution, and is available at\r
7  * http://www.eclipse.org/legal/epl-v10.html\r
8  *\r
9  * Contributors:\r
10  *     VTT Technical Research Centre of Finland - initial API and implementation\r
11  *******************************************************************************/\r
12 package org.simantics.modeling.ui.typicals;\r
13 \r
14 import java.lang.reflect.InvocationTargetException;\r
15 import org.eclipse.core.runtime.IProgressMonitor;\r
16 import org.eclipse.jface.operation.IRunnableWithProgress;\r
17 import org.eclipse.swt.widgets.Shell;\r
18 import org.eclipse.ui.IWorkbenchWindow;\r
19 import org.eclipse.ui.PlatformUI;\r
20 import org.simantics.Simantics;\r
21 import org.simantics.browsing.ui.common.ErrorLogger;\r
22 import org.simantics.db.Resource;\r
23 import org.simantics.db.Session;\r
24 import org.simantics.db.exception.DatabaseException;\r
25 import org.simantics.db.layer0.adapter.ActionFactory;\r
26 import org.simantics.modeling.ModelingResources;\r
27 import org.simantics.modeling.typicals.TypicalUtil;\r
28 import org.simantics.ui.workbench.action.ChooseActionRequest;\r
29 import org.simantics.utils.ui.SWTUtils;\r
30 import org.simantics.utils.ui.workbench.WorkbenchUtils;\r
31 \r
32 /**\r
33  * Creates a new master typical diagram and its corresponding composites.\r
34  * \r
35  * <p>\r
36  * The created typical composite and diagram type are specified by the model\r
37  * using {@link ModelingResources#StructuralModel_HasTypicalCompositeBaseType}\r
38  * and {@link ModelingResources#StructuralModel_HasTypicalDiagramBaseType}.\r
39  * \r
40  * <p>\r
41  * Marks the created master composite with the model-specified master composite\r
42  * type to support searching. The master type is specified by the model using\r
43  * {@link ModelingResources#StructuralModel_HasMasterTypicalCompositeType}.\r
44  * \r
45  * <p>\r
46  * Clones symbol contributions from the sources specified by the model through\r
47  * {@link ModelingResources#StructuralModel_CloneTypicalDiagramSymbolContributionsFrom}\r
48  * .\r
49  * \r
50  * @author Tuukka Lehtonen\r
51  */\r
52 public class NewMasterTypicalDiagram implements ActionFactory {\r
53 \r
54     @Override\r
55     public Runnable create(Object target_) {\r
56         final Resource target = (Resource)target_;\r
57         return new Runnable() {\r
58             @Override\r
59             public void run() {\r
60                 try {\r
61                         PlatformUI.getWorkbench().getProgressService().busyCursorWhile(new IRunnableWithProgress() {\r
62                                 @Override\r
63                         public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {\r
64                                         try {\r
65                                                 Resource composite = TypicalUtil.newMasterTypical(target);\r
66                                                 \r
67                                 // Perform default action on the newly created diagram\r
68                                 performDefaultAction(Simantics.getSession(), composite);\r
69                                         } catch (DatabaseException e) {\r
70                                                 throw new InvocationTargetException(e);\r
71                                         } finally {\r
72                                                 monitor.done();\r
73                                         }\r
74                                 }\r
75                         });\r
76                 } catch (InvocationTargetException e) {\r
77                         ErrorLogger.defaultLogError(e.getCause());\r
78                 } catch (InterruptedException e) {\r
79                         ErrorLogger.defaultLogError(e);\r
80                 }\r
81             }\r
82         };\r
83     };\r
84 \r
85     private static void performDefaultAction(final Session session, final Object input) {\r
86         SWTUtils.asyncExec(PlatformUI.getWorkbench().getDisplay(), new Runnable() {\r
87             @Override\r
88             public void run() {\r
89                 IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();\r
90                 Shell shell = window != null ? window.getShell() : null;\r
91                 session.asyncRequest(new ChooseActionRequest(shell, null, input, WorkbenchUtils.getCurrentPerspectiveId(), false, false, true));\r
92             }\r
93         });\r
94     }\r
95 \r
96 }\r