1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 /***
19 * <p>Title: WSMO Studio</p>
20 * <p>Description: Semantic Web Service Editor</p>
21 * <p>Copyright: Copyright (c) 2004-2006</p>
22 * <p>Company: OntoText Lab. / SIRMA </p>
23 */
24
25 package org.wsmostudio.ui.editors.common;
26
27 import java.util.*;
28
29 import org.eclipse.jface.dialogs.*;
30 import org.eclipse.swt.SWT;
31 import org.eclipse.swt.events.*;
32 import org.eclipse.swt.layout.*;
33 import org.eclipse.swt.widgets.*;
34 import org.omwg.ontology.Variable;
35 import org.wsmo.common.exception.InvalidModelException;
36 import org.wsmostudio.runtime.*;
37 import org.wsmostudio.ui.Utils;
38 import org.wsmostudio.ui.editors.model.CapabilityModel;
39
40 /***
41 * A common purpose GUI component, designed to maintain a set of WSMO-API (Shared)
42 * Variables. It is a sub-component of the Capability Editor.
43 * The supported user operation are addition, renaming and removal.
44 *
45 * @author not attributable
46 * @version $Revision: 1.15 $ $Date: 2006/01/25 13:00:56 $
47 */
48
49 public class SharedVarsPanel implements SelectionListener {
50
51 private Table varsList;
52 private CapabilityModel model;
53
54 public SharedVarsPanel(TabFolder parentFolder,
55 CapabilityModel model) {
56
57 this.model = model;
58
59 TabItem mainItem = new TabItem(parentFolder, SWT.BORDER);
60 parentFolder.setLayoutData(new GridData(GridData.FILL_BOTH));
61 mainItem.setText("Shared Variables");
62 parentFolder.setLayout(new GridLayout(1, false));
63
64 createTable(parentFolder);
65
66 mainItem.setControl(varsList);
67 }
68
69 private void createTable(Composite parent) {
70 varsList = new Table(parent, SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL);
71 varsList.setLayoutData(new GridData(GridData.FILL_BOTH));
72 varsList.setLinesVisible(false);
73 Set<Variable> vars = model.getCapability().listSharedVariables();
74 for(Variable tempVar : vars) {
75 TableItem item = Utils.createTableItem(varsList,
76 tempVar,
77 WsmoImageRegistry.VARIABLE_ICON);
78 item.setText(Utils.getEntityLocalName(tempVar.getName()));
79 }
80 varsList.addMouseListener(new MouseAdapter() {
81 public void mouseUp(MouseEvent e) {
82 if (e.button != 3) {
83 return;
84 }
85 showContextMenu(e);
86 }
87
88 });
89
90 }
91
92 private void showContextMenu(MouseEvent e) {
93 Menu contextMenu = new Menu(varsList.getShell(), SWT.POP_UP);
94
95 MenuItem item = new MenuItem(contextMenu, SWT.PUSH);
96 item.setText("Add Variable");
97 item.addSelectionListener(this);
98
99 TableItem[] sel = varsList.getSelection();
100 if (sel != null
101 && sel.length > 0
102 && sel[0].getBounds(0).contains(e.x, e.y)) {
103 item = new MenuItem(contextMenu, SWT.SEPARATOR);
104
105 item = new MenuItem(contextMenu, SWT.PUSH);
106 item.setText("Rename Variable");
107 item.addSelectionListener(this);
108
109 item = new MenuItem(contextMenu, SWT.SEPARATOR);
110
111 item = new MenuItem(contextMenu, SWT.PUSH);
112 item.setText("Remove Variable");
113 item.addSelectionListener(this);
114 }
115
116 contextMenu.setLocation(varsList.toDisplay(e.x, e.y));
117 contextMenu.setVisible(true);
118 }
119
120 public void dispose() {
121 varsList.dispose();
122 }
123 public void widgetSelected(SelectionEvent e) {
124 String action = ((MenuItem)e.widget).getText();
125 if (action.equals("Remove Variable")) {
126 TableItem[] sel = varsList.getSelection();
127 Variable var = (Variable)sel[0].getData();
128 try {
129 model.removeSharedVariable(var);
130 }
131 catch(InvalidModelException ime) {
132 LogManager.logError(ime);
133 }
134 return;
135 }
136 if (action.equals("Rename Variable")) {
137 TableItem[] sel = varsList.getSelection();
138 Variable oldVar = (Variable)sel[0].getData();
139 InputDialog iDialog = new InputDialog(
140 varsList.getShell(),
141 "Rename Variable",
142 "New Name:",
143 oldVar.getName(),
144 new IInputValidator() {
145 public String isValid(String text) {
146 for(int i = 0; i < text.length(); i++) {
147 if (false == Character.isLetterOrDigit(text.charAt(i))) {
148 return "Invalid character at position "
149 + i + " ('" + text.charAt(i) + "'). Alphanumerical expression expected";
150 }
151 }
152 return null;
153 }
154 });
155 iDialog.open();
156 String newName = iDialog.getValue();
157 if (newName == null
158 || newName.trim().length() == 0) {
159 return;
160 }
161 if (newName.equals(oldVar.getName())) {
162 return;
163 }
164 Variable newVar = WSMORuntime.getRuntime()
165 .getWsmoFactory()
166 .createVariable(newName);
167 if (model.getCapability().listSharedVariables().contains(newVar)) {
168 MessageDialog.openError(varsList.getShell(),
169 "Name Clash",
170 "Variable name '" + newVar.getName() + "' already in use!");
171 return;
172 }
173 try {
174 model.removeSharedVariable(oldVar);
175 model.addSharedVariable(newVar);
176 }
177 catch(InvalidModelException ime) {
178 LogManager.logError(ime);
179 }
180 return;
181 }
182
183 if (action.equals("Add Variable")) {
184 InputDialog iDialog = new InputDialog(
185 varsList.getShell(),
186 "New Variable",
187 "Name:",
188 null,
189 new IInputValidator() {
190 public String isValid(String text) {
191 for(int i = 0; i < text.length(); i++) {
192 if (false == Character.isLetterOrDigit(text.charAt(i))) {
193 return "Invalid character at position "
194 + i + " ('" + text.charAt(i) + "'). Alphanumerical expression expected";
195 }
196 }
197 return null;
198 }
199 });
200 iDialog.open();
201 String newName = iDialog.getValue();
202 if (newName == null
203 || newName.trim().length() == 0) {
204 return;
205 }
206 Variable newVar = WSMORuntime.getRuntime()
207 .getWsmoFactory()
208 .createVariable(newName);
209 if (model.getCapability().listSharedVariables().contains(newVar)) {
210 MessageDialog.openError(varsList.getShell(),
211 "Name Clash",
212 "Variable name '" + newVar.getName() + "' already in use!");
213 return;
214 }
215 try {
216 model.addSharedVariable(newVar);
217 }
218 catch(InvalidModelException ime) {
219 LogManager.logError(ime);
220 }
221 return;
222 }
223
224 }
225 public void widgetDefaultSelected(SelectionEvent e) {
226 }
227
228 public void reloadVariables() {
229 Set<Variable> toDoVars = new HashSet<Variable>(
230 model.getCapability().listSharedVariables());
231 TableItem[] items = varsList.getItems();
232 for (int i = 0; i < items.length; i++) {
233 Variable temp = (Variable)items[i].getData();
234 if (toDoVars.contains(temp)) {
235 toDoVars.remove(temp);
236 }
237 else {
238 items[i].dispose();
239 }
240 }
241 for(Variable newVar : toDoVars) {
242 TableItem item = Utils.createTableItem(varsList,
243 newVar,
244 WsmoImageRegistry.VARIABLE_ICON);
245 item.setText(Utils.getEntityLocalName(newVar.getName()));
246 }
247 }
248 }
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280