]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/actions/SetInitialState.java
bc0b5cbe0d7cf88e2f95fcc1682bb29f3372fa73
[simantics/platform.git] / bundles / org.simantics.modeling.ui / src / org / simantics / modeling / ui / actions / SetInitialState.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management
3  * in 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.actions;
13
14 import java.util.ArrayList;
15 import java.util.Collections;
16 import java.util.List;
17 import java.util.concurrent.atomic.AtomicReference;
18
19 import org.eclipse.jface.action.Action;
20 import org.eclipse.jface.action.ContributionItem;
21 import org.eclipse.jface.action.IContributionItem;
22 import org.eclipse.jface.resource.ImageDescriptor;
23 import org.eclipse.jface.resource.JFaceResources;
24 import org.eclipse.jface.resource.LocalResourceManager;
25 import org.eclipse.jface.resource.ResourceManager;
26 import org.eclipse.swt.SWT;
27 import org.eclipse.swt.events.SelectionEvent;
28 import org.eclipse.swt.events.SelectionListener;
29 import org.eclipse.swt.widgets.Menu;
30 import org.eclipse.swt.widgets.MenuItem;
31 import org.simantics.Simantics;
32 import org.simantics.db.ReadGraph;
33 import org.simantics.db.Resource;
34 import org.simantics.db.WriteGraph;
35 import org.simantics.db.common.NamedResource;
36 import org.simantics.db.common.request.ObjectsWithType;
37 import org.simantics.db.common.request.WriteRequest;
38 import org.simantics.db.exception.DatabaseException;
39 import org.simantics.layer0.Layer0;
40 import org.simantics.project.IProject;
41 import org.simantics.simulation.experiment.IExperiment;
42 import org.simantics.simulation.ontology.SimulationResource;
43 import org.simantics.simulation.project.IExperimentManager;
44 import org.simantics.ui.contribution.DynamicMenuContribution;
45 import org.simantics.ui.utils.ResourceAdaptionUtils;
46
47 /**
48  * @author Tuukka Lehtonen
49  */
50 public class SetInitialState extends DynamicMenuContribution {
51
52     private ResourceManager resourceManager;
53
54     public SetInitialState() {
55         resourceManager = new LocalResourceManager(JFaceResources.getResources());
56     }
57
58     @Override
59     public void dispose() {
60         if (resourceManager != null) {
61             resourceManager.dispose();
62             resourceManager = null;
63         }
64         super.dispose();
65     }
66
67     @Override
68     protected boolean preAcceptSelection(Object[] selection) {
69         return selection != null && selection.length == 1;
70     }
71
72     @Override
73     protected IContributionItem[] getContributionItems(ReadGraph graph, Object[] selection) throws DatabaseException {
74         if (selection.length != 1)
75             return NONE;
76
77         final Resource experiment = ResourceAdaptionUtils.adaptToResource(selection[0]);
78         if (experiment == null)
79             return NONE;
80
81         Layer0 L0 = Layer0.getInstance(graph);
82         SimulationResource SIMU = SimulationResource.getInstance(graph);
83         if (!graph.isInstanceOf(experiment, SIMU.Experiment))
84             return NONE;
85
86         // Check that the experiment is not currently active.
87         IProject project = Simantics.peekProject();
88         if (project != null) {
89             IExperimentManager expMan = project.getHint(IExperimentManager.KEY_EXPERIMENT_MANAGER);
90             if (expMan != null) {
91                 for (IExperiment exp : new IExperiment[] { expMan.getActiveExperiment() }) {
92                     if (exp != null && experiment.equals(exp.getResource()))
93                         return NONE;
94                 }
95             }
96         }
97
98         final List<NamedResource> states = new ArrayList<NamedResource>();
99         Resource model = graph.getSingleObject(experiment, L0.PartOf);
100         for (Resource obj : graph.syncRequest(new ObjectsWithType(model, L0.ConsistsOf, SIMU.State))) {
101             String name = graph.adapt(obj, String.class);
102             states.add(new NamedResource(name, obj));
103         }
104
105         final AtomicReference<NamedResource> currentInitialState = new AtomicReference<NamedResource>();
106         Resource currentState = graph.getPossibleObject(experiment, SIMU.HasInitialState);
107         if (currentState != null) {
108             for (NamedResource state : states) {
109                 if (state.getResource().equals(currentState))
110                     currentInitialState.set(state);
111             }
112         }
113
114         // Sort the open with actions in ascending lexicographical order.
115         Collections.sort(states);
116         if (states.isEmpty())
117             return NONE;
118
119         return new IContributionItem[] {
120                 new ContributionItem() {
121                     @Override
122                     public void fill(Menu menu, int index) {
123                         MenuItem setInitialState = new MenuItem(menu, SWT.CASCADE, index);
124                         setInitialState.setText("Set Initial State");
125                         Menu subMenu = new Menu(menu);
126                         setInitialState.setMenu(subMenu);
127
128                         for (NamedResource state : states) {
129                             addMenuItem(subMenu, new Adapter(experiment, state), state.equals(currentInitialState.get()));
130                         }
131                     }
132                 }
133         };
134     }
135
136     private void addMenuItem(Menu subMenu, Adapter adapter, boolean selected) {
137         MenuItem item = new MenuItem(subMenu, SWT.CHECK);
138         item.setText(adapter.getText());
139         item.setSelection(selected);
140         ImageDescriptor imgDesc = adapter.getImageDescriptor();
141         if (imgDesc != null)
142             item.setImage(resourceManager.createImage(imgDesc));
143         item.addSelectionListener(adapter);
144     }
145
146     static class Adapter extends Action implements SelectionListener {
147         private final Resource experiment;
148         private final NamedResource state;
149
150         public Adapter(Resource experiment, NamedResource state) {
151             super(state.getName());
152             this.experiment = experiment;
153             this.state = state;
154         }
155
156         @Override
157         public void widgetDefaultSelected(SelectionEvent e) {
158             widgetSelected(e);
159         }
160
161         @Override
162         public void widgetSelected(SelectionEvent e) {
163             run();
164         }
165
166         @Override
167         public void run() {
168             Simantics.getSession().asyncRequest(new WriteRequest() {
169                 @Override
170                 public void perform(WriteGraph wg) throws DatabaseException {
171                     SimulationResource SIMU = SimulationResource.getInstance(wg);
172                     wg.deny(experiment, SIMU.HasInitialState);
173                     wg.claim(experiment, SIMU.HasInitialState, state.getResource());
174                 }
175             });
176         }
177     }
178
179 }