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