View Javadoc
1   /*
2    WSMO Studio - a Semantic Web Service Editor
3    Copyright (c) 2004-2006, OntoText Lab. / SIRMA Group
4    
5    This library is free software; you can redistribute it and/or modify it under
6    the terms of the GNU Lesser General Public License as published by the Free
7    Software Foundation; either version 2.1 of the License, or (at your option)
8    any later version.
9    This library is distributed in the hope that it will be useful, but WITHOUT
10   ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   details.
13   You should have received a copy of the GNU Lesser General Public License along
14   with this library; if not, write to the Free Software Foundation, Inc.,
15   59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
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; // take no action
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; // take no action
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  * $Log: SharedVarsPanel.java,v $
252  * Revision 1.15  2006/01/25 13:00:56  alex_simov
253  * Input validator added when creating/renaming variable
254  *
255  * Revision 1.14  2006/01/09 12:51:13  alex_simov
256  * Copyright message in header updated
257  *
258  * Revision 1.13  2005/11/09 16:16:27  alex_simov
259  * UIModels notification improved
260  *
261  * Revision 1.12  2005/11/02 14:50:57  alex_simov
262  * moving to MVC architecture cont'd
263  *
264  * Revision 1.11  2005/09/08 16:46:25  alex_simov
265  * Migrating to Java 1.5
266  *
267  * Revision 1.10  2005/09/02 11:58:41  alex_simov
268  * bugfix: [1280458] WSMO Navigator changes notification improved
269  *
270  * Revision 1.9  2005/08/31 14:56:40  alex_simov
271  * fixes after wsmo4j changes
272  *
273  * Revision 1.8  2005/08/08 12:14:09  alex_simov
274  * refactoring: prepareLabel(String) moved to Utils.getEntityLocalName(String)
275  *
276  * Revision 1.7  2005/07/29 15:08:02  alex_simov
277  * added javadoc: class description, footer
278  *
279  *
280  */