1 /*******************************************************************************
\r
2 * Copyright (c) 2010 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.sysdyn.ui.properties.widgets;
\r
14 import java.util.ArrayList;
\r
15 import java.util.Collection;
\r
16 import java.util.Collections;
\r
17 import java.util.HashSet;
\r
18 import java.util.concurrent.CopyOnWriteArrayList;
\r
20 import org.eclipse.jface.layout.GridDataFactory;
\r
21 import org.eclipse.jface.layout.GridLayoutFactory;
\r
22 import org.eclipse.jface.viewers.IStructuredSelection;
\r
23 import org.eclipse.swt.SWT;
\r
24 import org.eclipse.swt.events.FocusListener;
\r
25 import org.eclipse.swt.events.MouseListener;
\r
26 import org.eclipse.swt.widgets.Composite;
\r
27 import org.eclipse.swt.widgets.TabFolder;
\r
28 import org.eclipse.swt.widgets.TabItem;
\r
29 import org.eclipse.swt.widgets.Table;
\r
30 import org.eclipse.swt.widgets.TableItem;
\r
31 import org.simantics.browsing.ui.swt.widgets.impl.Widget;
\r
32 import org.simantics.browsing.ui.swt.widgets.impl.WidgetSupport;
\r
33 import org.simantics.db.AsyncReadGraph;
\r
34 import org.simantics.db.ReadGraph;
\r
35 import org.simantics.db.Resource;
\r
36 import org.simantics.db.common.request.ObjectsWithType;
\r
37 import org.simantics.db.common.utils.NameUtils;
\r
38 import org.simantics.db.exception.DatabaseException;
\r
39 import org.simantics.db.management.ISessionContext;
\r
40 import org.simantics.db.procedure.AsyncListener;
\r
41 import org.simantics.db.request.Read;
\r
42 import org.simantics.layer0.Layer0;
\r
43 import org.simantics.sysdyn.SysdynResource;
\r
44 import org.simantics.ui.SimanticsUI;
\r
45 import org.simantics.utils.ui.ISelectionUtils;
\r
47 public class ShortcutTabWidget implements Widget {
\r
49 TabFolder tabFolder;
\r
52 Table variableTable;
\r
53 Table functionTable;
\r
54 Composite composite;
\r
57 CopyOnWriteArrayList<Runnable> dependencyListeners =
\r
58 new CopyOnWriteArrayList<Runnable>();
\r
60 public ShortcutTabWidget(Composite parent, WidgetSupport support, int style) {
\r
62 support.register(this);
\r
64 composite = new Composite(parent, style);
\r
65 GridLayoutFactory.fillDefaults().numColumns(2).applyTo(composite);
\r
67 tabFolder = new TabFolder (composite, SWT.NONE);
\r
68 GridDataFactory.fillDefaults().grab(false, true).applyTo(tabFolder);
\r
69 GridLayoutFactory.fillDefaults().applyTo(tabFolder);
\r
70 variables = new TabItem(tabFolder, SWT.NULL);
\r
71 variables.setText("Variables");
\r
72 variableTable = new Table (tabFolder, SWT.SINGLE | SWT.BORDER | SWT.FULL_SELECTION);
\r
74 variables.setControl(variableTable);
\r
76 functions = new TabItem(tabFolder, SWT.NULL);
\r
77 functions.setText("Functions");
\r
79 functionTable = new Table (tabFolder, SWT.SINGLE | SWT.BORDER | SWT.FULL_SELECTION);
\r
80 TableItem item = new TableItem(functionTable, SWT.NONE);
\r
83 ArrayList<String> funktio = new ArrayList<String>();
\r
85 funktio = SimanticsUI.getSession().syncRequest(new Read<ArrayList<String>>() {
\r
87 public ArrayList<String> perform(ReadGraph graph)
\r
88 throws DatabaseException {
\r
89 SysdynResource sr = SysdynResource.getInstance(graph);
\r
90 Layer0 l0 = Layer0.getInstance(graph);
\r
91 ArrayList<String> funktiot = new ArrayList<String>();
\r
93 Resource funktionlibrary = graph.getPossibleResource(SysdynResource.URIs.Built$in_Functions);
\r
94 for(Resource r : graph.syncRequest(new ObjectsWithType(funktionlibrary, l0.ConsistsOf, sr.SysdynModelicaFunction))) {
\r
95 String name = NameUtils.getSafeName(graph, r);
\r
99 Resource subfunktionlibrary = graph.getPossibleResource(SysdynResource.URIs.Built$in_Functions_Vensim_Functions);
\r
100 for(Resource r : graph.syncRequest(new ObjectsWithType(subfunktionlibrary, l0.ConsistsOf, sr.SysdynModelicaFunction))) {
\r
101 String name = NameUtils.getSafeName(graph, r);
\r
102 funktiot.add(name);
\r
109 catch (DatabaseException e) {
\r
110 e.printStackTrace();
\r
113 Collections.sort(funktio);
\r
115 for(int a=0; a < funktio.size(); a++){
\r
116 item.setText(funktio.get(a)+"()");
\r
117 item.setData(funktio.get(a)+"({})");
\r
118 item = new TableItem(functionTable, SWT.NONE);
\r
121 // TableItem item = new TableItem(functionTable, SWT.NONE);
\r
122 // item.setText("min()");
\r
123 // item.setData("min({ })");
\r
124 // item = new TableItem(functionTable, SWT.NONE);
\r
125 // item.setText("max()");
\r
126 // item.setData("max({ })");
\r
127 // item = new TableItem(functionTable, SWT.NONE);
\r
128 // item.setText("abs()");
\r
129 // item.setData("abs({ })");
\r
130 // item = new TableItem(functionTable, SWT.NONE);
\r
131 // item.setText("if then else");
\r
132 // item.setData("if then else");
\r
133 // item = new TableItem(functionTable, SWT.NONE);
\r
134 // item.setText("xidz()");
\r
135 // item.setData("xidz( number, divisor , x)");
\r
136 // item = new TableItem(functionTable, SWT.NONE);
\r
137 // item.setText("interpolate()");
\r
138 // item.setData("interpolate( x, table)");
\r
139 // item = new TableItem(functionTable, SWT.NONE);
\r
140 // item.setText("delay()");
\r
141 // item.setData("delay( expression, delayTime)");
\r
142 functions.setControl(functionTable);
\r
145 public Composite getWidget() {
\r
150 public void setInput(ISessionContext context, Object input) {
\r
151 if(input instanceof IStructuredSelection) {
\r
152 final Resource variable = ISelectionUtils.filterSingleSelection((IStructuredSelection)input, Resource.class);
\r
153 if(variable != null) {
\r
155 SimanticsUI.getSession().asyncRequest(new Read<HashSet<String>>() {
\r
158 public HashSet<String> perform(ReadGraph graph)
\r
159 throws DatabaseException {
\r
160 return getDependencies(graph, variable);
\r
162 }, new AsyncListener<HashSet<String>>() {
\r
165 public void execute(AsyncReadGraph graph,
\r
166 HashSet<String> result) {
\r
168 final HashSet<String> dependencies = result;
\r
169 variableTable.getDisplay().asyncExec(new Runnable() {
\r
172 public void run() {
\r
173 if(variableTable.isDisposed()) return;
\r
175 TableItem[] items = variableTable.getItems();
\r
177 // Remove deleted dependencies and create the list of current dependencies (itemStrings)
\r
178 ArrayList<String> itemStrings = new ArrayList<String>();
\r
179 for(TableItem i : items) {
\r
180 String text = i.getText();
\r
181 if(dependencies.contains(text))
\r
182 itemStrings.add(text);
\r
184 variableTable.remove(variableTable.indexOf(i));
\r
187 // Add all new dependencies
\r
189 for(String d : dependencies) {
\r
190 if(!itemStrings.contains(d)) {
\r
191 item = new TableItem(variableTable, SWT.NONE);
\r
197 synchronized(dependencyListeners) {
\r
198 for(Runnable listener : dependencyListeners)
\r
206 public void exception(AsyncReadGraph graph,
\r
207 Throwable throwable) {
\r
208 throwable.printStackTrace();
\r
212 public boolean isDisposed() {
\r
213 return variableTable.isDisposed();
\r
220 // Returns the names of the related variables (dependencies)
\r
221 private HashSet<String> getDependencies(ReadGraph graph, Resource r) throws DatabaseException {
\r
222 HashSet<String> variables = new HashSet<String>();
\r
223 if(graph != null && r != null) {
\r
224 SysdynResource sr = SysdynResource.getInstance(graph);
\r
225 Layer0 l0 = Layer0.getInstance(graph);
\r
227 Collection<Resource> dependencies = graph.getObjects(r, sr.IsHeadOf);
\r
229 for(Resource d : dependencies) {
\r
230 if(graph.isInstanceOf(d, sr.Dependency)) {
\r
231 Resource tail = graph.getPossibleObject(d, sr.HasTail);
\r
233 Object name = graph.getPossibleRelatedValue(tail, l0.HasName);
\r
235 variables.add((String)name);
\r
243 public void addFocusListener(FocusListener listener) {
\r
244 this.functionTable.addFocusListener(listener);
\r
245 this.variableTable.addFocusListener(listener);
\r
248 public void addMouseListener(MouseListener listener) {
\r
249 this.functionTable.addMouseListener(listener);
\r
250 this.variableTable.addMouseListener(listener);
\r
253 public void addDependencyListener(Runnable listener) {
\r
254 synchronized(dependencyListeners) {
\r
255 dependencyListeners.add(listener);
\r
259 public void removeDependencyListener(Runnable listener) {
\r
260 synchronized(dependencyListeners) {
\r
261 dependencyListeners.remove(listener);
\r
265 public Table getVariableTable() {
\r
266 return variableTable;
\r