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 org.eclipse.jface.action.*;
28  import org.eclipse.jface.resource.JFaceResources;
29  import org.eclipse.jface.viewers.*;
30  import org.eclipse.swt.SWT;
31  import org.eclipse.swt.events.*;
32  import org.eclipse.swt.graphics.*;
33  import org.eclipse.swt.layout.*;
34  import org.eclipse.swt.widgets.*;
35  import org.omwg.ontology.*;
36  import org.wsmo.service.*;
37  import org.wsmostudio.runtime.WsmoImageRegistry;
38  import org.wsmostudio.ui.GUIHelper;
39  import org.wsmostudio.ui.views.navigator.*;
40  
41  /***
42   * A common purpose GUI component, designed to support user selection of entities of
43   * certain type.
44   * The component supports various content filters which are set at construction time.
45   * Optionally a selection validator can be assigned to control runtime the correctness
46   * of the current selection.
47   * Additionally, a set of utility static methods create choosers with predefined 
48   * settings.
49   *
50   * @author not attributable
51   * @version $Revision: 1227 $ $Date: 2007-07-19 16:08:32 +0300 $
52   */
53  
54  public class WSMOChooser {
55  
56      private TreeViewer treeViewer;
57      private Shell shell;
58      private Object result = null;
59      private IWSMOSelectionValidator filter;
60      
61      public static WSMOChooser createConceptChooser(Shell parentShell,
62                                                     Object rootElement) {
63          WSMOChooser chooser = new WSMOChooser(parentShell, 
64                                                rootElement, 
65                                                WSMOContentProvider.CONCEPT_MASK);
66          chooser.setFilter(new IWSMOSelectionValidator() {
67              public String isValid(Object selection) {
68                  if (false == selection instanceof Concept) {
69                      return "Please select a Concept";
70                  }
71                  return null; // i.e. no error
72              }
73          });
74          return chooser;
75      }
76  
77      public static WSMOChooser createInstanceChooser(Shell parentShell,
78                                                      Object rootElement) {
79          return createInstanceChooser(parentShell, SWT.SINGLE, rootElement);
80      }
81  
82      public static WSMOChooser createInstanceChooser(Shell parentShell, 
83                                                      int treeStyle,
84                                                      Object rootElement) {
85          WSMOChooser chooser = new WSMOChooser(parentShell,
86                  treeStyle,
87                  rootElement, 
88                  new WSMOContentProvider(WSMOContentProvider.INSTANCE_MASK));
89          chooser.setFilter(new IWSMOSelectionValidator() {
90              public String isValid(Object selection) {
91                  if (false == selection instanceof Instance) {
92                      return "Please select a Instance";
93                  }
94                  return null; // i.e. no error
95              }
96          });
97          return chooser;
98      }
99      
100     public static WSMOChooser createInterfaceChooser(Shell parentShell,
101                                                      Object rootElement) {
102         WSMOChooser chooser =  new WSMOChooser(parentShell, 
103                 rootElement, 
104                 WSMOContentProvider.INTERFACE_MASK);
105         chooser.setFilter(new IWSMOSelectionValidator() {
106             public String isValid(Object selection) {
107                 if (false == selection instanceof Interface) {
108                     return "Please select an Interface";
109                 }
110                 return null; // i.e. no error
111             }
112         });
113         return chooser;
114     }
115     
116     public static WSMOChooser createCapabilityChooser(Shell parentShell,
117                                                      Object rootElement) {
118         WSMOChooser chooser =  new WSMOChooser(parentShell, 
119                 rootElement, 
120                 WSMOContentProvider.CAPABILITY_MASK);
121         chooser.setFilter(new IWSMOSelectionValidator() {
122             public String isValid(Object selection) {
123                 if (false == selection instanceof Capability) {
124                     return "Please select an Capability";
125                 }
126                 return null; // i.e. no error
127             }
128         });
129         return chooser;
130         
131     }
132 
133     public static WSMOChooser createRelationChooser(Shell parentShell,
134                                                     Object rootElement) {
135         WSMOChooser chooser = new WSMOChooser(parentShell, 
136                 rootElement, 
137                 WSMOContentProvider.RELATION_MASK);
138         chooser.setFilter(new IWSMOSelectionValidator() {
139             public String isValid(Object selection) {
140                 if (false == selection instanceof Relation) {
141                     return "Please select a Relation";
142                 }
143                 return null; // i.e. no error
144             }
145         });
146         return chooser;
147     }
148 
149     public static WSMOChooser createAttributesChooser(Shell parentShell,
150                                                       Object rootElement) {
151         WSMOChooser chooser = new WSMOChooser(parentShell,
152                                       SWT.SINGLE,
153                                       rootElement, 
154                                       new AttributesContentProvider());//WSMOContentProvider.ATTRIBUTE_MASK);
155         chooser.setFilter(new IWSMOSelectionValidator() {
156             public String isValid(Object selection) {
157                 if (false == selection instanceof Attribute) {
158                     return "Please select an Attribute";
159                 }
160                 return null; // i.e. no error
161             }
162         });
163         return chooser;
164     }
165     
166     public WSMOChooser(Shell parentShell, Object rootElement, int contentMask) {
167         this(parentShell, SWT.SINGLE, rootElement, new WSMOContentProvider(contentMask));
168     }
169 
170     public WSMOChooser(Shell parentShell, 
171                         int treeStyle, 
172                         Object rootElement, 
173                         ITreeContentProvider contentProvider) { 
174          shell = new Shell(parentShell, SWT.DIALOG_TRIM | SWT.RESIZE | SWT.SYSTEM_MODAL);
175          Image icon = JFaceResources.getImageRegistry().get(WsmoImageRegistry.DEFAULT_WINDOW_ICON);
176          shell.setImage(icon);
177          shell.setText("Choose Element");
178          shell.setLayout(new GridLayout(1, false));
179          treeViewer = new TreeViewer(shell, treeStyle);
180          
181          treeViewer.setContentProvider(contentProvider);
182          treeViewer.setLabelProvider(new WSMOLabelProvider());
183          treeViewer.setInput(rootElement);
184          treeViewer.getTree().addMouseListener(new MouseAdapter() {
185              public void mouseDoubleClick(MouseEvent e) {
186                  TreeItem[] sel = treeViewer.getTree().getSelection();
187                  if (!checkSelectionData()) {
188                      if (sel[0].getItems().length > 0) {
189                          sel[0].setExpanded(!sel[0].getExpanded());
190                          treeViewer.refresh(sel[0].getData());
191                      }
192                      return;
193                  }
194                  generateResult();
195                  shell.dispose();
196              }
197          });
198          treeViewer.getTree().setLayoutData(
199         		 new GridData(GridData.FILL_BOTH  | SWT.H_SCROLL | SWT.V_SCROLL));
200          createContextMenu(treeViewer.getTree());
201          
202          createControlArea(shell);
203          
204          treeViewer.getTree().addSelectionListener(new SelectionAdapter() {
205              public void widgetSelected(SelectionEvent e) {
206                  checkSelectionData();
207              }
208          });
209          shell.setSize(450, 450);
210 
211          Point pLocation = parentShell.getLocation();
212          Point pSize = parentShell.getSize();
213          shell.setLocation(pLocation.x + pSize.x / 2 - shell.getSize().x / 2,
214                  pLocation.y + pSize.y / 2 - shell.getSize().y / 2);
215 
216     }
217     
218     public void setDialogTitle(String title) {
219         shell.setText(title);
220     }
221     
222     private void createControlArea(Composite mainContainer) {
223         Composite buttons = new Composite(mainContainer, SWT.NONE);
224         buttons.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_END));
225         FillLayout layout = new FillLayout(SWT.HORIZONTAL);
226         layout.spacing = 3;
227         buttons.setLayout(layout);
228 
229         Button okButton = new Button(buttons, SWT.PUSH);
230         okButton.setText("Select");
231         okButton.setEnabled(false);
232         okButton.addSelectionListener(new SelectionAdapter() {
233             public void widgetSelected(SelectionEvent e) {
234                 generateResult();
235                 shell.dispose();
236             }
237         });
238         shell.setDefaultButton(okButton);
239         
240         Button noButton = new Button(buttons, SWT.PUSH);
241         noButton.setText("Cancel");
242         noButton.addSelectionListener(new SelectionAdapter() {
243             public void widgetSelected(SelectionEvent e) {
244                 shell.dispose();
245             }
246         });
247     }
248 
249     private void createContextMenu(Composite parentComp) {
250         final MenuManager menuMgr = new MenuManager();
251         menuMgr.setRemoveAllWhenShown(true);
252         menuMgr.addMenuListener(new IMenuListener() {
253                 public void menuAboutToShow(IMenuManager mgr) {
254                     fillContextMenu(menuMgr);
255                 }
256         });
257         
258         // Create menu.
259         parentComp.setMenu(menuMgr.createContextMenu(treeViewer.getTree()));
260     }
261 
262     private void fillContextMenu(MenuManager menuMgr) {
263         TreeItem[] sel = treeViewer.getTree().getSelection();
264         if (sel == null 
265         		|| sel.length == 0) {
266         	return;
267         }
268     	if (false == GUIHelper.containsCursor(sel[0].getBounds(), treeViewer.getTree())) {
269     		return;
270     	}
271         final Object target = sel[0].getData();
272         boolean putSeparator = false;
273         if (((ITreeContentProvider)treeViewer.getContentProvider()).hasChildren(target)) {
274         	menuMgr.add(new Action("Expand Subtree") {
275         		public void run() {
276         			doExpandSubtree();
277         		}
278         	});
279         	putSeparator = true;
280         }
281 
282         if (target instanceof Ontology) {
283             if (putSeparator) {
284                 menuMgr.add(new Separator());
285             }
286         	menuMgr.add(new Action("Details") {
287         		public void run() {
288                     GUIHelper.showOntoStatistics(
289                             (Ontology)target, 
290                             shell);
291         		}
292         	});
293         }
294     }
295 
296     private void generateResult() {
297         if ((treeViewer.getTree().getStyle() & SWT.MULTI) == SWT.MULTI) {
298             TreeItem[] sel = treeViewer.getTree().getSelection();
299             Object[] resultBuffer = new Object[sel.length];
300             for (int i = 0; i < sel.length; i++) {
301                 resultBuffer[i] = sel[i].getData();
302             }
303             result = resultBuffer;
304         }
305         else {
306             result = ((StructuredSelection)treeViewer.getSelection()).getFirstElement();
307         }
308     }
309     
310     public Object open() {
311         shell.open();
312         Display display = shell.getDisplay();
313         while (!shell.isDisposed()) {
314             if (!display.readAndDispatch()) {
315                 display.sleep();
316             }
317         }
318         return result;
319     }
320     
321     public void dispose() {
322         treeViewer.getTree().dispose();
323         shell.dispose();
324         result = null;
325     }
326     
327     public void setFilter(IWSMOSelectionValidator filter) {
328         this.filter = filter;
329     }
330     
331     private boolean checkSelectionData() {
332         if (filter == null) {
333             shell.getDefaultButton().setEnabled(true);
334             return true;
335         }
336         TreeItem[] sel = treeViewer.getTree().getSelection();
337         String error = null;
338         if (sel.length > 0) {
339             for (int i = 0; i < sel.length; i++) {
340                 error = filter.isValid(sel[i].getData());
341                 if (error != null && error.trim().length() > 0) {
342                     break;
343                 }
344             }
345         }
346         shell.getDefaultButton().setEnabled(error == null 
347                                          || error.trim().length() == 0);
348         return shell.getDefaultButton().isEnabled();
349     }
350     
351     private void doExpandSubtree() {
352         Object sel = ((IStructuredSelection)treeViewer.getSelection()).getFirstElement();
353         treeViewer.expandToLevel(sel, TreeViewer.ALL_LEVELS);
354         treeViewer.getTree().showSelection();
355     }
356     
357     public void expandTree() {
358         treeViewer.expandAll();
359     }
360     
361 }
362 
363 /*
364  * $Log$
365  * Revision 1.13  2007/07/19 13:08:31  alex_simov
366  * - rfe [1751326] Navigation through the editor tab
367  * - concepts/attributes which are from a different namespace are prefixed
368  *
369  * Revision 1.12  2006/08/01 11:21:12  alex_simov
370  * bugfix[1531709]: Context menus relied on right button mouse click instead of
371  * being registered as dedicated context menus for the corresponding UI
372  * controls
373  *
374  * Revision 1.11  2006/07/20 15:15:52  alex_simov
375  * ui fixes
376  *
377  * Revision 1.10  2006/07/18 13:27:14  alex_simov
378  * bugfix/workaround [1484820] : usage of FillLayout replaced by GridLayout,
379  * previously causing empty dialog windows under Linux - gtk
380  *
381  * Revision 1.9  2006/04/10 14:03:39  alex_simov
382  * The chooser now accepts custom content provider as parameter
383  *
384  * Revision 1.8  2006/04/07 15:24:19  alex_simov
385  * rfe[1465647]: Ontology statistics action added to WSMO Navigator
386  * (available also in WSMO choosers showing ontologies)
387  *
388  * Revision 1.7  2006/03/08 11:25:11  alex_simov
389  * ui fix: choosers dialog size increased
390  *
391  * Revision 1.6  2006/02/24 15:03:12  alex_simov
392  * chooser changed to allow multiple selection
393  *
394  * Revision 1.5  2006/01/09 12:51:13  alex_simov
395  * Copyright message in header updated
396  *
397  * Revision 1.4  2005/07/29 15:08:02  alex_simov
398  * added javadoc: class description, footer
399  *
400  *
401  */