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.semanticgov.ui.wsdl;
26  
27  
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.semanticgov.ui.Activator;
36  import org.wsmostudio.runtime.WsmoImageRegistry;
37  import org.wsmostudio.ui.editors.common.IWSMOSelectionValidator;
38  
39  public class WSDLElementChooser {
40  
41      private TableViewer tableViewer;
42      private Shell shell;
43      private Object result = null;
44      private IWSMOSelectionValidator filter;
45      
46      private IStructuredContentProvider contentProvider = new IStructuredContentProvider() {
47  
48  		public Object[] getElements(Object inputElement) {
49  			return WSDLCache.getCache().listWSDLURIs().toArray();
50  		}
51  		public void dispose() {
52  		}
53  		public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
54  		}
55      };
56      
57  /*    public static WSDLElementChooser createInputsChooser(Shell parentShell,
58                                                     Object rootElement) {
59          WSDLElementChooser chooser = new WSDLElementChooser(parentShell, 
60                                                rootElement);
61          chooser.setFilter(new IWSMOSelectionValidator() {
62              public String isValid(Object selection) {
63                  if (false == selection instanceof Input) {
64                      return "Please select an Input message";
65                  }
66                  return null; // i.e. no error
67              }
68          });
69          return chooser;
70      }
71      public static WSDLElementChooser createOutputsChooser(Shell parentShell,
72              Object rootElement) {
73          WSDLElementChooser chooser = new WSDLElementChooser(parentShell, 
74                  rootElement);
75          chooser.setFilter(new IWSMOSelectionValidator() {
76              public String isValid(Object selection) {
77                  if (false == selection instanceof Output) {
78                      return "Please select an Output message";
79                  }
80                  return null; // i.e. no error
81              }
82          });
83          return chooser;
84      }*/
85  
86      public WSDLElementChooser(Shell parentShell, Object rootElement) {
87          this(parentShell, SWT.SINGLE, rootElement);
88      }
89  
90      public WSDLElementChooser(Shell parentShell, 
91                          int treeStyle, 
92                          Object rootElement) { 
93           
94           shell = new Shell(parentShell, SWT.DIALOG_TRIM | SWT.RESIZE | SWT.APPLICATION_MODAL);
95           Image icon = JFaceResources.getImageRegistry().get(WsmoImageRegistry.DEFAULT_WINDOW_ICON);
96           shell.setImage(icon);
97           shell.setText("Choose WSDL Element");
98           shell.setSize(450, 450);
99           shell.setLayout(new FillLayout(SWT.VERTICAL));
100          Point pLocation = parentShell.getLocation();
101          Point pSize = parentShell.getSize();
102          shell.setLocation(pLocation.x + pSize.x / 2 - shell.getSize().x / 2,
103                  pLocation.y + pSize.y / 2 - shell.getSize().y / 2);
104 
105          Composite mainContainer = new Composite(shell, SWT.EMBEDDED);
106          mainContainer.setLayout(new GridLayout(1, false));
107          tableViewer = new TableViewer(mainContainer, treeStyle);
108          
109          tableViewer.setContentProvider(contentProvider);
110          tableViewer.setLabelProvider(new LabelProvider() {
111         	 public Image getImage(Object element) {
112         		 return Activator.getWSDLLogo();
113         	 }
114          });
115          tableViewer.setInput(rootElement);
116          tableViewer.getTable().addMouseListener(new MouseAdapter() {
117              public void mouseDoubleClick(MouseEvent e) {
118                  generateResult();
119                  shell.dispose();
120              }
121              
122              public void mouseUp(MouseEvent e) {
123                  if (e.button != 3) {
124                      return;
125                  }
126              }
127          });
128          tableViewer.getTable().setLayoutData(new GridData(GridData.FILL_BOTH));
129          
130          createControlArea(mainContainer);
131          
132          tableViewer.getTable().addSelectionListener(new SelectionAdapter() {
133              public void widgetSelected(SelectionEvent e) {
134                  checkSelectionData();
135              }
136          });
137     }
138     
139     public void setDialogTitle(String title) {
140         shell.setText(title);
141     }
142     
143     private void createControlArea(Composite mainContainer) {
144         Composite buttons = new Composite(mainContainer, SWT.EMBEDDED);
145         buttons.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_END));
146         GridLayout fLayout = new GridLayout(2, true);//SWT.HORIZONTAL);
147         fLayout.horizontalSpacing = 3;
148         fLayout.marginWidth = 0;
149 
150         buttons.setLayout(fLayout);
151 
152         Button okButton = new Button(buttons, SWT.PUSH);
153         okButton.setText("Select");
154         okButton.setLayoutData(new GridData(SWT.CENTER));
155         okButton.setEnabled(false);
156         okButton.addSelectionListener(new SelectionAdapter() {
157             public void widgetSelected(SelectionEvent e) {
158                 generateResult();
159                 shell.dispose();
160             }
161         });
162         shell.setDefaultButton(okButton);
163         
164         Button noButton = new Button(buttons, SWT.PUSH);
165         noButton.setText("Cancel");
166         noButton.setLayoutData(new GridData(SWT.RIGHT));
167         noButton.addSelectionListener(new SelectionAdapter() {
168             public void widgetSelected(SelectionEvent e) {
169                 shell.dispose();
170             }
171         });
172     }
173     
174     private void generateResult() {
175         if ((this.tableViewer.getTable().getStyle() & SWT.MULTI) == SWT.MULTI) {
176             TableItem[] sel = this.tableViewer.getTable().getSelection();
177             Object[] resultBuffer = new Object[sel.length];
178             for (int i = 0; i < sel.length; i++) {
179                 resultBuffer[i] = sel[i].getData();
180             }
181             result = resultBuffer;
182         }
183         else {
184             result = ((StructuredSelection)tableViewer.getSelection()).getFirstElement();
185         }
186     }
187     
188     public Object open() {
189         shell.open();
190         Display display = shell.getDisplay();
191         while (!shell.isDisposed()) {
192             if (!display.readAndDispatch()) {
193                 display.sleep();
194             }
195         }
196         return result;
197     }
198     
199     public void dispose() {
200         tableViewer.getTable().dispose();
201         shell.dispose();
202         result = null;
203     }
204     
205     public void setFilter(IWSMOSelectionValidator filter) {
206         this.filter = filter;
207     }
208     
209     private boolean checkSelectionData() {
210         if (filter == null) {
211             shell.getDefaultButton().setEnabled(true);
212             return true;
213         }
214         TableItem[] sel = tableViewer.getTable().getSelection();
215         String error = null;
216         if (sel.length > 0) {
217             for (int i = 0; i < sel.length; i++) {
218                 error = filter.isValid(sel[i].getData());
219                 if (error != null && error.trim().length() > 0) {
220                     break;
221                 }
222             }
223         }
224         shell.getDefaultButton().setEnabled(error == null 
225                                          || error.trim().length() == 0);
226         return shell.getDefaultButton().isEnabled();
227     }
228 
229 }
230