1 /*******************************************************************************
\r
2 * Copyright (c) 2012 Association for Decentralized Information Management in
\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
10 * VTT Technical Research Centre of Finland - initial API and implementation
\r
11 *******************************************************************************/
\r
12 package org.simantics.modeling.ui.typicals;
\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
33 * Creates a new master typical diagram and its corresponding composites.
\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
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
46 * Clones symbol contributions from the sources specified by the model through
\r
47 * {@link ModelingResources#StructuralModel_CloneTypicalDiagramSymbolContributionsFrom}
\r
50 * @author Tuukka Lehtonen
\r
52 public class NewMasterTypicalDiagram implements ActionFactory {
\r
55 public Runnable create(Object target_) {
\r
56 final Resource target = (Resource)target_;
\r
57 return new Runnable() {
\r
61 PlatformUI.getWorkbench().getProgressService().busyCursorWhile(new IRunnableWithProgress() {
\r
63 public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
\r
65 Resource composite = TypicalUtil.newMasterTypical(target);
\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
76 } catch (InvocationTargetException e) {
\r
77 ErrorLogger.defaultLogError(e);
\r
78 } catch (InterruptedException e) {
\r
79 ErrorLogger.defaultLogError(e);
\r
85 private static void performDefaultAction(final Session session, final Object input) {
\r
86 SWTUtils.asyncExec(PlatformUI.getWorkbench().getDisplay(), new Runnable() {
\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