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.grounding.sawsdl.ui;
26  
27  import org.eclipse.jface.resource.JFaceResources;
28  import org.eclipse.jface.viewers.*;
29  import org.eclipse.swt.SWT;
30  import org.eclipse.swt.events.*;
31  import org.eclipse.swt.graphics.*;
32  import org.eclipse.swt.layout.*;
33  import org.eclipse.swt.widgets.*;
34  import org.w3c.dom.Element;
35  import org.wsmostudio.grounding.sawsdl.ui.editor.*;
36  import org.wsmostudio.runtime.WsmoImageRegistry;
37  import org.wsmostudio.ui.editors.common.IWSMOSelectionValidator;
38  
39  import com.ontotext.wsmo4j.grounding.sawsdl.WSDLUtils;
40  
41  public class WSDLElementChooser {
42  
43      private TreeViewer treeViewer;
44      private Shell shell;
45      private String result = null;
46      private IWSMOSelectionValidator filter;
47      
48      public static WSDLElementChooser createInputsChooser(Shell parentShell,
49                                                     Object rootElement) {
50          WSDLElementChooser chooser = new WSDLElementChooser(parentShell, 
51                                                rootElement, 
52                                                WSDLTreeContentProvider.INPUT_MSG_MASK);
53          chooser.setFilter(new IWSMOSelectionValidator() {
54              public String isValid(Object selection) {
55                  if (selection instanceof Element
56                          && false == Utils.getLocalName((Element)selection).equals("input")) {
57                      return "Please select an Input message";
58                  }
59                  return null; // i.e. no error
60              }
61          });
62          return chooser;
63      }
64      public static WSDLElementChooser createOutputsChooser(Shell parentShell,
65              Object rootElement) {
66          WSDLElementChooser chooser = new WSDLElementChooser(parentShell, 
67                  rootElement, 
68                  WSDLTreeContentProvider.OUTPUT_MSG_MASK);
69          chooser.setFilter(new IWSMOSelectionValidator() {
70              public String isValid(Object selection) {
71                  if (selection instanceof Element
72                          && false == Utils.getLocalName((Element)selection).equals("output")) {
73                      return "Please select an Output message";
74                  }
75                  return null; // i.e. no error
76              }
77          });
78          return chooser;
79      }
80  
81      public WSDLElementChooser(Shell parentShell, Object rootElement, int contentMask) {
82          this(parentShell, SWT.SINGLE, rootElement, new WSDLTreeContentProvider(contentMask));
83      }
84  
85      public WSDLElementChooser(Shell parentShell, 
86                          int treeStyle, 
87                          Object rootElement, 
88                          ITreeContentProvider contentProvider) { 
89           
90           shell = new Shell(parentShell, SWT.DIALOG_TRIM | SWT.RESIZE | SWT.APPLICATION_MODAL);
91           Image icon = JFaceResources.getImageRegistry().get(WsmoImageRegistry.DEFAULT_WINDOW_ICON);
92           shell.setImage(icon);
93           shell.setText("Choose WSDL Element");
94           shell.setSize(450, 450);
95           shell.setLayout(new GridLayout(1, false));
96  
97           treeViewer = new TreeViewer(shell, treeStyle);
98           
99           treeViewer.setContentProvider(contentProvider);
100          WSDLTreeLabelProvider labProvider = new WSDLTreeLabelProvider(null);
101          labProvider.setShowExtensionInfo(false);
102          treeViewer.setLabelProvider(labProvider);
103          treeViewer.setInput(rootElement);
104          treeViewer.getTree().addMouseListener(new MouseAdapter() {
105              public void mouseDoubleClick(MouseEvent e) {
106                  TreeItem[] sel = treeViewer.getTree().getSelection();
107                  if (!checkSelectionData()) {
108                      if (sel[0].getItems().length > 0) {
109                          sel[0].setExpanded(!sel[0].getExpanded());
110                          treeViewer.refresh(sel[0].getData());
111                      }
112                      return;
113                  }
114                  generateResult();
115                  shell.dispose();
116              }
117              
118              public void mouseUp(MouseEvent e) {
119                  if (e.button != 3) {
120                      return;
121                  }
122 //                 showContextMenu(e);
123              }
124          });
125          treeViewer.getTree().setLayoutData(new GridData(GridData.FILL_BOTH));
126          
127          createControlArea(shell);
128          
129          treeViewer.getTree().addSelectionListener(new SelectionAdapter() {
130              public void widgetSelected(SelectionEvent e) {
131                  checkSelectionData();
132              }
133          });
134          Point pLocation = parentShell.getLocation();
135          Point pSize = parentShell.getSize();
136          shell.setLocation(pLocation.x + pSize.x / 2 - shell.getSize().x / 2,
137                  pLocation.y + pSize.y / 2 - shell.getSize().y / 2);
138     }
139     
140     public void setDialogTitle(String title) {
141         shell.setText(title);
142     }
143     
144     private void createControlArea(Composite mainContainer) {
145         Composite buttons = new Composite(mainContainer, SWT.NONE);
146         buttons.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_END));
147         FillLayout layout = new FillLayout(SWT.HORIZONTAL);
148         layout.spacing = 3;
149         buttons.setLayout(layout);
150 
151         Button okButton = new Button(buttons, SWT.PUSH);
152         okButton.setText("Select");
153         okButton.setEnabled(false);
154         okButton.addSelectionListener(new SelectionAdapter() {
155             public void widgetSelected(SelectionEvent e) {
156                 generateResult();
157                 shell.dispose();
158             }
159         });
160         shell.setDefaultButton(okButton);
161         
162         Button noButton = new Button(buttons, SWT.PUSH);
163         noButton.setText("Cancel");
164         noButton.addSelectionListener(new SelectionAdapter() {
165             public void widgetSelected(SelectionEvent e) {
166                 shell.dispose();
167             }
168         });
169     }
170     
171     private void generateResult() {
172         Element selection = (Element)
173             ((StructuredSelection)treeViewer.getSelection()).getFirstElement();
174         String localName = Utils.getLocalName(selection);
175         if (localName.equals("element")) {
176             result = WSDLUtils.ELEMENT_DECLARATION_NS + '(' + selection.getAttribute("name") + ')';
177         }
178         else if (localName.equals("complexType")) {
179             result = WSDLUtils.TYPE_DEFINITION_NS + '(' + selection.getAttribute("name") + ')';
180         }
181         else if (localName.equals("interface")) {
182             result = WSDLUtils.INTERFACE_NS + '(' + selection.getAttribute("name") + ')';
183         }
184         else if (localName.equals("operation")) {
185             result = WSDLUtils.OPERATION_NS + '(' 
186                     + ((Element)selection.getParentNode()).getAttribute("name") //interface name
187                     + '/' + selection.getAttribute("name") + ')'; // operation name
188         }
189         else if (localName.equals("input") 
190                 || localName.equals("output")) {
191             result = WSDLUtils.MESSAGE_NS + '(' 
192                     + ((Element)selection.getParentNode()).getAttribute("name") // interface name
193                     + '/' + ((Element)selection.getParentNode()).getAttribute("name") // operation name
194                     + '/' + selection.getAttribute("messageLabel") + ')'; // message name
195         }
196     }
197     
198     public String open() {
199         shell.open();
200         treeViewer.expandAll();
201         Display display = shell.getDisplay();
202         while (!shell.isDisposed()) {
203             if (!display.readAndDispatch()) {
204                 display.sleep();
205             }
206         }
207         return result;
208     }
209     
210     public void dispose() {
211         treeViewer.getTree().dispose();
212         shell.dispose();
213         result = null;
214     }
215     
216     public void setFilter(IWSMOSelectionValidator filter) {
217         this.filter = filter;
218     }
219     
220     private boolean checkSelectionData() {
221         if (filter == null) {
222             shell.getDefaultButton().setEnabled(true);
223             return true;
224         }
225         TreeItem[] sel = treeViewer.getTree().getSelection();
226         String error = null;
227         if (sel.length > 0) {
228             for (int i = 0; i < sel.length; i++) {
229                 error = filter.isValid(sel[i].getData());
230                 if (error != null && error.trim().length() > 0) {
231                     break;
232                 }
233             }
234         }
235         shell.getDefaultButton().setEnabled(error == null 
236                                          || error.trim().length() == 0);
237         return shell.getDefaultButton().isEnabled();
238     }
239 
240 }
241 
242 /*
243  * $Log$
244  * Revision 1.4  2007/04/25 16:53:37  alex_simov
245  * no message
246  *
247  * Revision 1.3  2006/07/20 15:11:51  alex_simov
248  * ui fixes
249  *
250  * Revision 1.2  2006/07/18 13:33:05  alex_simov
251  * bugfix/workaround [1484820] : usage of FillLayout replaced by GridLayout,
252  * previously causing empty dialog windows under Linux - gtk
253  *
254  * Revision 1.1  2006/07/05 15:37:28  alex_simov
255  * no message
256  *
257  * Revision 1.4  2006/07/03 15:38:12  alex_simov
258  * no message
259  *
260  * Revision 1.3  2006/06/22 11:47:07  alex_simov
261  * chooser generates String fragment identifiers
262  *
263  * Revision 1.2  2006/06/21 12:53:20  alex_simov
264  * wsdl4j object model repalced by xml DOM
265  *
266  * Revision 1.1  2006/06/06 13:15:27  alex_simov
267  * no message
268  *
269  */