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.wsmo.common.IRI;
66  
67  import org.wsmo.mediator.Mediator;
68  
69  import org.wsmostudio.runtime.WSMORuntime;
70  
71  import org.wsmostudio.ui.Utils;
72  
73  import org.wsmostudio.ui.editors.model.TopEntityModel;
74  
75  import org.wsmostudio.ui.views.navigator.WSMOContentProvider;
76  
77  
78  
79  /***
80  
81   * A common purpose GUI component, designed to maintain a set of WSMO-API Mediator
82  
83   * references. It is a sub-component of all WSMO editors whose target input objects
84  
85   * have a "usesMediator" property. The supported user operation are inclusion and
86  
87   * exclusion of references. 
88  
89   *
90  
91   * @author not attributable
92  
93   * @version $Revision: 1.12 $ $Date: 2006/04/06 14:04:25 $
94  
95   */
96  
97  
98  
99  public class UsedMediatorsPanel implements SelectionListener {
100 
101     
102 
103     private Table mediatorsTable;
104 
105     
106 
107     private TopEntityModel model;
108 
109     
110 
111     public UsedMediatorsPanel(TabFolder parentFolder,
112 
113             TopEntityModel teModel) {
114 
115         
116 
117         this.model = teModel;
118 
119 
120 
121         TabItem mainItem = new TabItem(parentFolder, SWT.BORDER);
122 
123         parentFolder.setLayoutData(new GridData(GridData.FILL_BOTH));
124 
125         mainItem.setText("Used Mediators");
126 
127         parentFolder.setLayout(new GridLayout(1, false));
128 
129         
130 
131         createTable(parentFolder);
132 
133         
134 
135         mainItem.setControl(mediatorsTable);
136 
137     }
138 
139     
140 
141     private void createTable(Composite parent) {
142 
143         mediatorsTable = new Table(parent, SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL);
144 
145         mediatorsTable.setLayoutData(new GridData(GridData.FILL_BOTH));
146 
147         mediatorsTable.setLinesVisible(false);
148 
149         Set usedMedis = model.getTopEntity().listMediators();
150 
151         for(Iterator it = usedMedis.iterator(); it.hasNext();) {
152 
153             IRI mediatorID = (IRI)it.next();
154 
155             Mediator newMediator = 
156 
157                 WSMORuntime.getCache().findMediator(
158 
159                         mediatorID, 
160 
161                         Utils.getPreferredMediatorType(model.getTopEntity()));
162 
163             
164 
165             Utils.createTableItem(mediatorsTable, 
166 
167                     mediatorID, 
168 
169                     Utils.getMediatorType(newMediator));
170 
171         }
172 
173         mediatorsTable.addMouseListener(new MouseAdapter() {
174 
175             public void mouseUp(MouseEvent e) {
176 
177                 if (e.button != 3) {
178 
179                     return;
180 
181                 }
182 
183                 showContextMenu(e);
184 
185             }
186 
187             
188 
189         });
190 
191     	
192 
193     }
194 
195     
196 
197     private void showContextMenu(MouseEvent e) {
198 
199         Menu contextMenu = new Menu(mediatorsTable.getShell(), SWT.POP_UP);
200 
201 
202 
203         MenuItem item = new MenuItem(contextMenu, SWT.PUSH);
204 
205         item.setText("Add Mediator");
206 
207         item.addSelectionListener(this);
208 
209     
210 
211         TableItem[] sel = mediatorsTable.getSelection();
212 
213         if (sel != null 
214 
215                 && sel.length > 0 
216 
217                 && sel[0].getBounds(0).contains(e.x, e.y)) {
218 
219             new MenuItem(contextMenu, SWT.SEPARATOR);
220 
221             
222 
223             item = new MenuItem(contextMenu, SWT.PUSH);
224 
225             item.setText("Remove Mediator");
226 
227             item.addSelectionListener(this);
228 
229         }
230 
231         
232 
233         contextMenu.setLocation(mediatorsTable.toDisplay(e.x, e.y));
234 
235         contextMenu.setVisible(true);
236 
237     }
238 
239     
240 
241     public void dispose() {
242 
243         mediatorsTable.dispose();
244 
245     }
246 
247     public void widgetSelected(SelectionEvent e) {
248 
249         String action = ((MenuItem)e.widget).getText();
250 
251         if (action.equals("Remove Mediator")) {
252 
253             TableItem[] sel = mediatorsTable.getSelection();
254 
255             IRI oldMedi = (IRI)sel[0].getData();
256 
257             model.removeMediator(oldMedi);
258 
259             return;
260 
261         }
262 
263         if (action.equals("Add Mediator")) {
264 
265             WSMOChooser chooser = new WSMOChooser(mediatorsTable.getShell(),
266 
267                                               WSMORuntime.getRuntime(), 
268 
269                                               WSMOContentProvider.MEDIATOR_MASK);
270 
271 
272 
273             
274 
275             Mediator newMediator = (Mediator)chooser.open();
276 
277             if (newMediator == null 
278 
279                     || newMediator.equals(model.getTopEntity())) {
280 
281                 return;
282 
283             }
284 
285             if (model.getTopEntity().listMediators().contains(newMediator.getIdentifier())) {
286 
287                 return; // take no action - its already there ...
288 
289             }
290 
291             
292 
293             model.addMediator((IRI)newMediator.getIdentifier());
294 
295             return;
296 
297         }
298 
299         
300 
301     }
302 
303     public void widgetDefaultSelected(SelectionEvent e) {
304 
305     }
306 
307     
308 
309     public void reloadMediators() {
310 
311         Set<IRI> toDoMediators = new HashSet<IRI>(model.getTopEntity().listMediators());
312 
313         TableItem[] items = mediatorsTable.getItems();
314 
315         for (int i = 0; i < items.length; i++) {
316 
317             IRI temp = (IRI)items[i].getData();
318 
319             if (toDoMediators.contains(temp)) {
320 
321                 toDoMediators.remove(temp);
322 
323             }
324 
325             else {
326 
327                 items[i].dispose();
328 
329             }
330 
331         }
332 
333         for(IRI newMediatorID : toDoMediators) {
334 
335             Mediator newMediator = 
336 
337                 WSMORuntime.getCache().findMediator(
338 
339                         newMediatorID, 
340 
341                         Utils.getPreferredMediatorType(model.getTopEntity()));
342 
343             Utils.createTableItem(mediatorsTable, 
344 
345                     newMediatorID, 
346 
347                     Utils.getMediatorType(newMediator));
348 
349         }
350 
351     }
352 
353 
354 
355 }
356 
357 
358 
359 /*
360 
361  * $Log: UsedMediatorsPanel.java,v $
362  * Revision 1.12  2006/04/06 14:04:25  alex_simov
363  * wsmo-api update: mediators are now referred by IRIs instead of Object handles
364  *
365  * Revision 1.11  2006/01/09 12:51:13  alex_simov
366  * Copyright message in header updated
367  *
368  * Revision 1.10  2005/11/25 14:31:03  alex_simov
369  * runtime code refactoring
370  *
371  * Revision 1.9  2005/11/09 16:16:27  alex_simov
372  * UIModels notification improved
373  *
374  * Revision 1.8  2005/11/02 14:50:57  alex_simov
375  * moving to MVC architecture cont'd
376  *
377  * Revision 1.7  2005/09/30 12:13:53  alex_simov
378  * unused variable 'item' assignment
379  *
380  * Revision 1.6  2005/08/08 12:15:33  alex_simov
381  * Utils.createTableItem() used where appropriate
382  *
383  * Revision 1.5  2005/07/29 15:08:02  alex_simov
384  * added javadoc: class description, footer
385  *
386 
387  *
388 
389  */
390