View Javadoc
1   /*
2   
3    WSMO Studio - a Semantic Web Service Editor
4   
5    Copyright (c) 2004-2006, OntoText Lab. / SIRMA Group
6   
7    
8   
9    This library is free software; you can redistribute it and/or modify it under
10  
11   the terms of the GNU Lesser General Public License as published by the Free
12  
13   Software Foundation; either version 2.1 of the License, or (at your option)
14  
15   any later version.
16  
17   This library is distributed in the hope that it will be useful, but WITHOUT
18  
19   ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
20  
21   FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
22  
23   details.
24  
25   You should have received a copy of the GNU Lesser General Public License along
26  
27   with this library; if not, write to the Free Software Foundation, Inc.,
28  
29   59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
30  
31   */
32  
33  
34  
35  /***
36  
37   * <p>Title: WSMO Studio</p>
38  
39   * <p>Description: Semantic Web Service Editor</p>
40  
41   * <p>Copyright:  Copyright (c) 2004-2006</p>
42  
43   * <p>Company: OntoText Lab. / SIRMA </p>
44  
45   */
46  
47  
48  
49  package org.wsmostudio.ui.editors.common;
50  
51  
52  
53  import java.util.*;
54  
55  
56  
57  import org.eclipse.swt.SWT;
58  
59  import org.eclipse.swt.events.*;
60  
61  import org.eclipse.swt.layout.*;
62  
63  import org.eclipse.swt.widgets.*;
64  
65  import org.omwg.ontology.Ontology;
66  
67  import org.wsmostudio.runtime.*;
68  
69  import org.wsmostudio.ui.Utils;
70  
71  import org.wsmostudio.ui.editors.model.TopEntityModel;
72  
73  import org.wsmostudio.ui.views.navigator.WSMOContentProvider;
74  
75  
76  
77  /***
78  
79   * A common purpose GUI component, designed to maintain a set of WSMO-API Ontology
80  
81   * references. It is a sub-component of all WSMO editors whose target input objects
82  
83   * have an "importsOntology" property. The supported user operation are inclusion and
84  
85   * exclusion of references. 
86  
87   *
88  
89   * @author not attributable
90  
91   * @version $Revision: 1.10 $ $Date: 2006/04/18 07:47:52 $
92  
93   */
94  
95  
96  
97  public class ImportOntologyPanel implements SelectionListener {
98  
99      
100 
101     private Table ontosTable;
102 
103     private TopEntityModel uiModel;
104 
105     
106 
107     public ImportOntologyPanel(TabFolder parentFolder,
108 
109             TopEntityModel uiModel) {
110 
111         
112 
113         this.uiModel = uiModel;
114 
115 
116 
117         TabItem mainItem = new TabItem(parentFolder, SWT.NONE);
118 
119 
120 
121         parentFolder.setLayoutData(new GridData(GridData.FILL_BOTH));
122 
123         mainItem.setText("Import Ontologies");
124 
125         parentFolder.setLayout(new GridLayout(1, false));
126 
127         
128 
129         createTable(parentFolder);
130 
131         
132 
133         mainItem.setControl(ontosTable);
134 
135     }
136 
137     
138 
139     private void createTable(Composite parent) {
140 
141         ontosTable = new Table(parent, SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL);
142 
143         ontosTable.setLayoutData(new GridData(GridData.FILL_BOTH));
144 
145         ontosTable.setLinesVisible(false);
146 
147         if (this.uiModel != null) {
148 
149             Set impOntos = uiModel.getTopEntity().listOntologies();
150 
151             for(Iterator it = impOntos.iterator(); it.hasNext();) {
152 
153                 Utils.createTableItem(ontosTable, 
154 
155                         (Ontology)it.next(), 
156 
157                         WsmoImageRegistry.ONTOLOGY_ICON);
158 
159             }
160 
161         }
162 
163         ontosTable.addMouseListener(new MouseAdapter() {
164 
165             public void mouseUp(MouseEvent e) {
166 
167                 if (e.button != 3) {
168 
169                     return;
170 
171                 }
172 
173                 showContextMenu(e);
174 
175             }
176 
177         });
178 
179     }
180 
181     
182 
183     private void showContextMenu(MouseEvent e) {
184 
185         if (this.uiModel == null) {
186 
187             return;
188 
189         }
190 
191         Menu contextMenu = new Menu(ontosTable.getShell(), SWT.POP_UP);
192 
193 
194 
195         MenuItem item = new MenuItem(contextMenu, SWT.PUSH);
196 
197         item.setText("Add Ontology");
198 
199         item.addSelectionListener(this);
200 
201     
202 
203         TableItem[] sel = ontosTable.getSelection();
204 
205         if (sel != null 
206 
207                 && sel.length > 0 
208 
209                 && sel[0].getBounds(0).contains(e.x, e.y)) {
210 
211             new MenuItem(contextMenu, SWT.SEPARATOR);
212 
213             
214 
215             item = new MenuItem(contextMenu, SWT.PUSH);
216 
217             item.setText("Remove Ontology");
218 
219             item.addSelectionListener(this);
220 
221         }
222 
223         
224 
225         contextMenu.setLocation(ontosTable.toDisplay(e.x, e.y));
226 
227         contextMenu.setVisible(true);
228 
229     }
230 
231     
232 
233     public void dispose() {
234 
235         ontosTable.dispose();
236 
237     }
238 
239     
240 
241     public void widgetSelected(SelectionEvent e) {
242 
243         String action = ((MenuItem)e.widget).getText();
244 
245         if (action.equals("Remove Ontology")) {
246 
247             TableItem[] sel = ontosTable.getSelection();
248 
249             Ontology oldOnto = (Ontology)sel[0].getData();
250 
251             uiModel.removeOntology(oldOnto);
252 
253             return;
254 
255         }
256 
257         if (action.equals("Add Ontology")) {
258 
259             WSMOChooser chooser = new WSMOChooser(ontosTable.getShell(),
260 
261                                               WSMORuntime.getRuntime(), 
262 
263                                               WSMOContentProvider.ONTOLOGY_MASK);
264 
265 
266 
267             Ontology newOntology = (Ontology)chooser.open();
268 
269             if (newOntology == null 
270 
271                     || newOntology.equals(uiModel.getTopEntity())) {
272 
273                 return;
274 
275             }
276 
277             if (uiModel.getTopEntity().listOntologies().contains(newOntology)) {
278 
279                 return; // take no action - its already there ...
280 
281             }
282 
283             uiModel.addOntology(newOntology);
284 
285             return;
286 
287         }
288 
289         
290 
291     }
292 
293     
294 
295     public void widgetDefaultSelected(SelectionEvent e) {
296 
297     }
298 
299     
300 
301     public void setUIModel(TopEntityModel newModel) {
302 
303         this.uiModel = newModel;
304 
305         reloadOntologies();
306 
307     }
308 
309     
310 
311     public void reloadOntologies() {
312 
313         
314 
315         if (this.uiModel == null) {
316 
317             ontosTable.removeAll();
318 
319             return;
320 
321         }
322 
323         
324 
325         Set<Ontology> toDoOntos = new HashSet<Ontology>(
326 
327                                       uiModel.getTopEntity().listOntologies());
328 
329         TableItem[] items = ontosTable.getItems();
330 
331         for (int i = 0; i < items.length; i++) {
332 
333             Ontology temp = (Ontology)items[i].getData();
334 
335             if (toDoOntos.contains(temp)) {
336 
337                 toDoOntos.remove(temp);
338 
339             }
340 
341             else {
342 
343                 items[i].dispose();
344 
345             }
346 
347         }
348 
349         for(Ontology newOnto : toDoOntos) {
350 
351             Utils.createTableItem(ontosTable, 
352 
353                     newOnto, 
354 
355                     WsmoImageRegistry.ONTOLOGY_ICON);
356 
357         }
358 
359     }
360 
361 
362 
363 }
364 
365 
366 
367 /*
368 
369  * $Log: ImportOntologyPanel.java,v $
370  * Revision 1.10  2006/04/18 07:47:52  alex_simov
371  * the component now handles null-inputs correctly
372  *
373  * Revision 1.9  2006/01/09 12:51:13  alex_simov
374  * Copyright message in header updated
375  *
376  * Revision 1.8  2005/11/09 16:16:27  alex_simov
377  * UIModels notification improved
378  *
379  * Revision 1.7  2005/11/02 14:50:57  alex_simov
380  * moving to MVC architecture cont'd
381  *
382  * Revision 1.6  2005/09/30 12:13:53  alex_simov
383  * unused variable 'item' assignment
384  *
385  * Revision 1.5  2005/08/08 12:15:33  alex_simov
386  * Utils.createTableItem() used where appropriate
387  *
388  * Revision 1.4  2005/07/29 15:08:02  alex_simov
389  * added javadoc: class description, footer
390  *
391 
392  *
393 
394  */
395