-/*******************************************************************************\r
- * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
- * in 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.actions;\r
-\r
-import java.util.ArrayList;\r
-import java.util.Collections;\r
-import java.util.List;\r
-import java.util.concurrent.atomic.AtomicReference;\r
-\r
-import org.eclipse.jface.action.Action;\r
-import org.eclipse.jface.action.ContributionItem;\r
-import org.eclipse.jface.action.IContributionItem;\r
-import org.eclipse.jface.resource.ImageDescriptor;\r
-import org.eclipse.jface.resource.JFaceResources;\r
-import org.eclipse.jface.resource.LocalResourceManager;\r
-import org.eclipse.jface.resource.ResourceManager;\r
-import org.eclipse.swt.SWT;\r
-import org.eclipse.swt.events.SelectionEvent;\r
-import org.eclipse.swt.events.SelectionListener;\r
-import org.eclipse.swt.widgets.Menu;\r
-import org.eclipse.swt.widgets.MenuItem;\r
-import org.simantics.Simantics;\r
-import org.simantics.db.ReadGraph;\r
-import org.simantics.db.Resource;\r
-import org.simantics.db.WriteGraph;\r
-import org.simantics.db.common.NamedResource;\r
-import org.simantics.db.common.request.ObjectsWithType;\r
-import org.simantics.db.common.request.WriteRequest;\r
-import org.simantics.db.exception.DatabaseException;\r
-import org.simantics.layer0.Layer0;\r
-import org.simantics.project.IProject;\r
-import org.simantics.simulation.experiment.IExperiment;\r
-import org.simantics.simulation.ontology.SimulationResource;\r
-import org.simantics.simulation.project.IExperimentManager;\r
-import org.simantics.ui.contribution.DynamicMenuContribution;\r
-import org.simantics.ui.utils.ResourceAdaptionUtils;\r
-\r
-/**\r
- * @author Tuukka Lehtonen\r
- */\r
-public class SetInitialState extends DynamicMenuContribution {\r
-\r
- private ResourceManager resourceManager;\r
-\r
- public SetInitialState() {\r
- resourceManager = new LocalResourceManager(JFaceResources.getResources());\r
- }\r
-\r
- @Override\r
- public void dispose() {\r
- if (resourceManager != null) {\r
- resourceManager.dispose();\r
- resourceManager = null;\r
- }\r
- super.dispose();\r
- }\r
-\r
- @Override\r
- protected boolean preAcceptSelection(Object[] selection) {\r
- return selection != null && selection.length == 1;\r
- }\r
-\r
- @Override\r
- protected IContributionItem[] getContributionItems(ReadGraph graph, Object[] selection) throws DatabaseException {\r
- if (selection.length != 1)\r
- return NONE;\r
-\r
- final Resource experiment = ResourceAdaptionUtils.adaptToResource(selection[0]);\r
- if (experiment == null)\r
- return NONE;\r
-\r
- Layer0 L0 = Layer0.getInstance(graph);\r
- SimulationResource SIMU = SimulationResource.getInstance(graph);\r
- if (!graph.isInstanceOf(experiment, SIMU.Experiment))\r
- return NONE;\r
-\r
- // Check that the experiment is not currently active.\r
- IProject project = Simantics.peekProject();\r
- if (project != null) {\r
- IExperimentManager expMan = project.getHint(IExperimentManager.KEY_EXPERIMENT_MANAGER);\r
- if (expMan != null) {\r
- for (IExperiment exp : new IExperiment[] { expMan.getActiveExperiment() }) {\r
- if (exp != null && experiment.equals(exp.getResource()))\r
- return NONE;\r
- }\r
- }\r
- }\r
-\r
- final List<NamedResource> states = new ArrayList<NamedResource>();\r
- Resource model = graph.getSingleObject(experiment, L0.PartOf);\r
- for (Resource obj : graph.syncRequest(new ObjectsWithType(model, L0.ConsistsOf, SIMU.State))) {\r
- String name = graph.adapt(obj, String.class);\r
- states.add(new NamedResource(name, obj));\r
- }\r
-\r
- final AtomicReference<NamedResource> currentInitialState = new AtomicReference<NamedResource>();\r
- Resource currentState = graph.getPossibleObject(experiment, SIMU.HasInitialState);\r
- if (currentState != null) {\r
- for (NamedResource state : states) {\r
- if (state.getResource().equals(currentState))\r
- currentInitialState.set(state);\r
- }\r
- }\r
-\r
- // Sort the open with actions in ascending lexicographical order.\r
- Collections.sort(states);\r
- if (states.isEmpty())\r
- return NONE;\r
-\r
- return new IContributionItem[] {\r
- new ContributionItem() {\r
- @Override\r
- public void fill(Menu menu, int index) {\r
- MenuItem setInitialState = new MenuItem(menu, SWT.CASCADE, index);\r
- setInitialState.setText("Set Initial State");\r
- Menu subMenu = new Menu(menu);\r
- setInitialState.setMenu(subMenu);\r
-\r
- for (NamedResource state : states) {\r
- addMenuItem(subMenu, new Adapter(experiment, state), state.equals(currentInitialState.get()));\r
- }\r
- }\r
- }\r
- };\r
- }\r
-\r
- private void addMenuItem(Menu subMenu, Adapter adapter, boolean selected) {\r
- MenuItem item = new MenuItem(subMenu, SWT.CHECK);\r
- item.setText(adapter.getText());\r
- item.setSelection(selected);\r
- ImageDescriptor imgDesc = adapter.getImageDescriptor();\r
- if (imgDesc != null)\r
- item.setImage(resourceManager.createImage(imgDesc));\r
- item.addSelectionListener(adapter);\r
- }\r
-\r
- static class Adapter extends Action implements SelectionListener {\r
- private final Resource experiment;\r
- private final NamedResource state;\r
-\r
- public Adapter(Resource experiment, NamedResource state) {\r
- super(state.getName());\r
- this.experiment = experiment;\r
- this.state = state;\r
- }\r
-\r
- @Override\r
- public void widgetDefaultSelected(SelectionEvent e) {\r
- widgetSelected(e);\r
- }\r
-\r
- @Override\r
- public void widgetSelected(SelectionEvent e) {\r
- run();\r
- }\r
-\r
- @Override\r
- public void run() {\r
- Simantics.getSession().asyncRequest(new WriteRequest() {\r
- @Override\r
- public void perform(WriteGraph wg) throws DatabaseException {\r
- SimulationResource SIMU = SimulationResource.getInstance(wg);\r
- wg.deny(experiment, SIMU.HasInitialState);\r
- wg.claim(experiment, SIMU.HasInitialState, state.getResource());\r
- }\r
- });\r
- }\r
- }\r
-\r
-}\r
+/*******************************************************************************
+ * Copyright (c) 2007, 2010 Association for Decentralized Information Management
+ * in Industry THTH ry.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * VTT Technical Research Centre of Finland - initial API and implementation
+ *******************************************************************************/
+package org.simantics.modeling.ui.actions;
+
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+import java.util.concurrent.atomic.AtomicReference;
+
+import org.eclipse.jface.action.Action;
+import org.eclipse.jface.action.ContributionItem;
+import org.eclipse.jface.action.IContributionItem;
+import org.eclipse.jface.resource.ImageDescriptor;
+import org.eclipse.jface.resource.JFaceResources;
+import org.eclipse.jface.resource.LocalResourceManager;
+import org.eclipse.jface.resource.ResourceManager;
+import org.eclipse.swt.SWT;
+import org.eclipse.swt.events.SelectionEvent;
+import org.eclipse.swt.events.SelectionListener;
+import org.eclipse.swt.widgets.Menu;
+import org.eclipse.swt.widgets.MenuItem;
+import org.simantics.Simantics;
+import org.simantics.db.ReadGraph;
+import org.simantics.db.Resource;
+import org.simantics.db.WriteGraph;
+import org.simantics.db.common.NamedResource;
+import org.simantics.db.common.request.ObjectsWithType;
+import org.simantics.db.common.request.WriteRequest;
+import org.simantics.db.exception.DatabaseException;
+import org.simantics.layer0.Layer0;
+import org.simantics.project.IProject;
+import org.simantics.simulation.experiment.IExperiment;
+import org.simantics.simulation.ontology.SimulationResource;
+import org.simantics.simulation.project.IExperimentManager;
+import org.simantics.ui.contribution.DynamicMenuContribution;
+import org.simantics.ui.utils.ResourceAdaptionUtils;
+
+/**
+ * @author Tuukka Lehtonen
+ */
+public class SetInitialState extends DynamicMenuContribution {
+
+ private ResourceManager resourceManager;
+
+ public SetInitialState() {
+ resourceManager = new LocalResourceManager(JFaceResources.getResources());
+ }
+
+ @Override
+ public void dispose() {
+ if (resourceManager != null) {
+ resourceManager.dispose();
+ resourceManager = null;
+ }
+ super.dispose();
+ }
+
+ @Override
+ protected boolean preAcceptSelection(Object[] selection) {
+ return selection != null && selection.length == 1;
+ }
+
+ @Override
+ protected IContributionItem[] getContributionItems(ReadGraph graph, Object[] selection) throws DatabaseException {
+ if (selection.length != 1)
+ return NONE;
+
+ final Resource experiment = ResourceAdaptionUtils.adaptToResource(selection[0]);
+ if (experiment == null)
+ return NONE;
+
+ Layer0 L0 = Layer0.getInstance(graph);
+ SimulationResource SIMU = SimulationResource.getInstance(graph);
+ if (!graph.isInstanceOf(experiment, SIMU.Experiment))
+ return NONE;
+
+ // Check that the experiment is not currently active.
+ IProject project = Simantics.peekProject();
+ if (project != null) {
+ IExperimentManager expMan = project.getHint(IExperimentManager.KEY_EXPERIMENT_MANAGER);
+ if (expMan != null) {
+ for (IExperiment exp : new IExperiment[] { expMan.getActiveExperiment() }) {
+ if (exp != null && experiment.equals(exp.getResource()))
+ return NONE;
+ }
+ }
+ }
+
+ final List<NamedResource> states = new ArrayList<NamedResource>();
+ Resource model = graph.getSingleObject(experiment, L0.PartOf);
+ for (Resource obj : graph.syncRequest(new ObjectsWithType(model, L0.ConsistsOf, SIMU.State))) {
+ String name = graph.adapt(obj, String.class);
+ states.add(new NamedResource(name, obj));
+ }
+
+ final AtomicReference<NamedResource> currentInitialState = new AtomicReference<NamedResource>();
+ Resource currentState = graph.getPossibleObject(experiment, SIMU.HasInitialState);
+ if (currentState != null) {
+ for (NamedResource state : states) {
+ if (state.getResource().equals(currentState))
+ currentInitialState.set(state);
+ }
+ }
+
+ // Sort the open with actions in ascending lexicographical order.
+ Collections.sort(states);
+ if (states.isEmpty())
+ return NONE;
+
+ return new IContributionItem[] {
+ new ContributionItem() {
+ @Override
+ public void fill(Menu menu, int index) {
+ MenuItem setInitialState = new MenuItem(menu, SWT.CASCADE, index);
+ setInitialState.setText(Messages.SetInitialState_SetInitialState);
+ Menu subMenu = new Menu(menu);
+ setInitialState.setMenu(subMenu);
+
+ for (NamedResource state : states) {
+ addMenuItem(subMenu, new Adapter(experiment, state), state.equals(currentInitialState.get()));
+ }
+ }
+ }
+ };
+ }
+
+ private void addMenuItem(Menu subMenu, Adapter adapter, boolean selected) {
+ MenuItem item = new MenuItem(subMenu, SWT.CHECK);
+ item.setText(adapter.getText());
+ item.setSelection(selected);
+ ImageDescriptor imgDesc = adapter.getImageDescriptor();
+ if (imgDesc != null)
+ item.setImage(resourceManager.createImage(imgDesc));
+ item.addSelectionListener(adapter);
+ }
+
+ static class Adapter extends Action implements SelectionListener {
+ private final Resource experiment;
+ private final NamedResource state;
+
+ public Adapter(Resource experiment, NamedResource state) {
+ super(state.getName());
+ this.experiment = experiment;
+ this.state = state;
+ }
+
+ @Override
+ public void widgetDefaultSelected(SelectionEvent e) {
+ widgetSelected(e);
+ }
+
+ @Override
+ public void widgetSelected(SelectionEvent e) {
+ run();
+ }
+
+ @Override
+ public void run() {
+ Simantics.getSession().asyncRequest(new WriteRequest() {
+ @Override
+ public void perform(WriteGraph wg) throws DatabaseException {
+ SimulationResource SIMU = SimulationResource.getInstance(wg);
+ wg.deny(experiment, SIMU.HasInitialState);
+ wg.claim(experiment, SIMU.HasInitialState, state.getResource());
+ }
+ });
+ }
+ }
+
+}