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;
50  
51  
52  
53  import java.util.*;
54  
55  
56  
57  import org.eclipse.swt.SWT;
58  
59  import org.eclipse.swt.custom.SashForm;
60  
61  import org.eclipse.swt.events.*;
62  
63  import org.eclipse.swt.layout.*;
64  
65  import org.eclipse.swt.widgets.*;
66  
67  import org.eclipse.ui.*;
68  
69  import org.wsmo.common.*;
70  
71  import org.wsmo.common.exception.InvalidModelException;
72  
73  import org.wsmostudio.runtime.*;
74  
75  import org.wsmostudio.ui.Utils;
76  
77  import org.wsmostudio.ui.editors.common.*;
78  
79  import org.wsmostudio.ui.editors.model.MediatorModel;
80  
81  import org.wsmostudio.ui.views.navigator.WSMOContentProvider;
82  
83  
84  
85  /***
86  
87   * An editor component, subclass of WSMOEditor, capable to handle with WSMO-API 
88  
89   * Mediator objects.
90  
91   * This editor appears as a part of the WSMO Studio workbench. It is the default
92  
93   * editor implementation for this kind of objects and it can be replaced by 
94  
95   * extending extension points <code>org.wsmostudio.ui.editors</code> and 
96  
97   * <code>org.eclipse.ui.editors</code>.
98  
99   *
100 
101  * @author not attributable
102 
103  * @version $Revision: 1.13 $ $Date: 2006/04/06 14:04:25 $
104 
105  */
106 
107 
108 
109 public class MediatorEditor extends WSMOEditor implements SelectionListener {
110 
111 
112 
113     private NFPPanel nfpPanel;
114 
115     private NamespacesPanel nsPanel;
116 
117     private ImportOntologyPanel ontologiesPanel;
118 
119     private UsedMediatorsPanel mediatorsPanel;
120 
121     
122 
123     private MediatorModel model;
124 
125     
126 
127     private Text targetField, medServiceField;
128 
129     private Table sourcesList;
130 
131     
132 
133     private boolean dirtyContent = false;
134 
135     
136 
137     private TabFolder medsAndOntosFolder;
138 
139     
140 
141     public void createPartControl(Composite parent) {
142 
143         GridLayout mainLayout = new GridLayout(1, false);
144 
145         mainLayout.verticalSpacing = 0;
146 
147         parent.setLayout(mainLayout);
148 
149         
150 
151         nfpPanel = new NFPPanel(parent, model);
152 
153         
154 
155         SashForm splitter = new SashForm(parent, SWT.VERTICAL );
156 
157         splitter.setLayoutData(new GridData(GridData.FILL_BOTH));
158 
159 
160 
161         nsPanel = new NamespacesPanel(splitter, model);
162 
163         
164 
165         medsAndOntosFolder = new TabFolder(splitter, SWT.NONE);
166 
167         ontologiesPanel = new ImportOntologyPanel(
168 
169                 medsAndOntosFolder, 
170 
171                 model);
172 
173         
174 
175         mediatorsPanel = new UsedMediatorsPanel(medsAndOntosFolder, model);
176 
177         
178 
179         createSourceTargetPanel(splitter);
180 
181         splitter.setWeights(new int[] { 2, 2, 3 });
182 
183         
184 
185     }
186 
187     
188 
189     public void init(IEditorSite site, IEditorInput input) throws PartInitException {
190 
191         super.init(site, input);
192 
193         model = (MediatorModel)input.getAdapter(MediatorModel.class);
194 
195         setTitleImage(WsmoImageRegistry.getImageForObject(model.getMediator()));
196 
197 
198 
199     }
200 
201     
202 
203     @SuppressWarnings("unchecked")
204 
205     protected void updateFromModel() {
206 
207         nsPanel.reloadNSInfo();
208 
209         ontologiesPanel.reloadOntologies();
210 
211         mediatorsPanel.reloadMediators();
212 
213         targetField.setText((model.getMediator().getTarget() != null) ?
214 
215                 model.getMediator().getTarget().toString()
216 
217                 : "");
218 
219 
220 
221         if (false == dirtyContent) {
222 
223             medServiceField.setText((model.getMediator().getMediationService() != null) ? 
224 
225                     model.getMediator().getMediationService().toString()
226 
227                     : "");
228 
229         }
230 
231         refreshSourcesList();
232 
233     }
234 
235     
236 
237     @SuppressWarnings("unchecked")
238 
239     private void refreshSourcesList() {
240 
241         Set<IRI> toDoSources = new HashSet<IRI>(model.getMediator().listSources());
242 
243         TableItem[] items = sourcesList.getItems();
244 
245         for (int i = 0; i < items.length; i++) {
246 
247             IRI temp = (IRI)items[i].getData();
248 
249             if (toDoSources.contains(temp)) {
250 
251                 toDoSources.remove(temp);
252 
253             }
254 
255             else {
256 
257                 items[i].dispose();
258 
259             }
260 
261         }
262 
263         for(IRI newSrcID : toDoSources) {
264 
265             TopEntity te = WSMORuntime.getCache().findTopEntity(newSrcID);
266 
267             Utils.createTableItem(sourcesList,
268 
269                     newSrcID,
270 
271                     (te != null) ? WsmoImageRegistry.getImageNameForObject(te) : null);
272 
273         }
274 
275     }
276 
277 
278 
279     private void createSourceTargetPanel(Composite parent) {
280 
281         Group mainPanel = new Group(parent, SWT.NONE);
282 
283         mainPanel.setText("Sources && Target");
284 
285         mainPanel.setLayout(new GridLayout(3, false));
286 
287         mainPanel.setLayoutData(new GridData(GridData.FILL_BOTH));
288 
289         
290 
291         new Label(mainPanel, SWT.NONE).setText("Target : ");
292 
293 
294 
295         targetField = new Text(mainPanel, SWT.SINGLE | SWT.BORDER | SWT.READ_ONLY);
296 
297         
298 
299         if (model.getMediator().getTarget() != null) {
300 
301             targetField.setText(model.getMediator().getTarget().toString());
302 
303         }
304 
305         targetField.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
306 
307         Button selectTarget = new Button(mainPanel, SWT.PUSH);
308 
309         selectTarget.setText("Select");
310 
311         selectTarget.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_END));
312 
313         selectTarget.addSelectionListener(new SelectionAdapter() {
314 
315             public void widgetSelected(SelectionEvent e) {
316 
317                 doSelectTarget();
318 
319             }
320 
321         });
322 
323 
324 
325         new Label(mainPanel, SWT.NONE).setText("Service :");
326 
327 
328 
329         medServiceField = new Text(mainPanel, SWT.SINGLE | SWT.BORDER);
330 
331         medServiceField.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
332 
333         if (model.getMediator().getMediationService() != null) {
334 
335             medServiceField.setText(model.getMediator().getMediationService().toString());
336 
337         }
338 
339         medServiceField.addModifyListener(new ModifyListener() {
340 
341             public void modifyText(ModifyEvent e) {
342 
343                 dirtyContent = true;
344 
345                 model.setDirty();
346 
347             }
348 
349         });
350 
351 
352 
353 //        Button selectMedService = new Button(mainPanel, SWT.PUSH);
354 
355         new Label(mainPanel, SWT.NONE).setLayoutData(
356 
357                 new GridData(GridData.HORIZONTAL_ALIGN_END));        
358 
359  //       selectMedService.setText("Select");
360 
361   //      selectMedService.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_END));
362 
363   //      selectMedService.addSelectionListener(new SelectionAdapter() {
364 
365    //         public void widgetSelected(SelectionEvent e) {
366 
367     //            doSelectMediationService();
368 
369      //       }
370 
371       //  });
372 
373         
374 
375         Label ifaceLab = new Label(mainPanel, SWT.NONE);
376 
377         ifaceLab.setText("Sources :");
378 
379         ifaceLab.setLayoutData(new GridData(GridData.VERTICAL_ALIGN_BEGINNING));
380 
381         initSourcesData(mainPanel);
382 
383 
384 
385     }
386 
387     
388 
389     private void initSourcesData(Composite parent) {
390 
391         sourcesList = new Table(parent, SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL);
392 
393         GridData cellData = new GridData(GridData.FILL_BOTH|GridData.GRAB_HORIZONTAL);
394 
395         sourcesList.setLayoutData(cellData);
396 
397         sourcesList.setLinesVisible(false);
398 
399         Set sources = model.getMediator().listSources();
400 
401         for(Iterator it = sources.iterator(); it.hasNext();) {
402 
403             IRI teID = (IRI)it.next();
404 
405             TopEntity te = WSMORuntime.getCache().findTopEntity(teID);
406 
407             Utils.createTableItem(sourcesList, 
408 
409                     teID,
410 
411                     (te != null) ? WsmoImageRegistry.getImageNameForObject(te) : null);
412 
413         }
414 
415         sourcesList.addMouseListener(new MouseAdapter() {
416 
417             public void mouseUp(MouseEvent e) {
418 
419                 if (e.button != 3) {
420 
421                     return;
422 
423                 }
424 
425                 showContextMenu(e);
426 
427             }
428 
429         });
430 
431 
432 
433     }
434 
435 
436 
437     private void showContextMenu(MouseEvent e) {
438 
439         Menu contextMenu = new Menu(sourcesList.getShell(), SWT.POP_UP);
440 
441 
442 
443         MenuItem item = new MenuItem(contextMenu, SWT.PUSH);
444 
445         item.setText("Add Source");
446 
447         item.addSelectionListener(this);
448 
449     
450 
451         TableItem[] sel = sourcesList.getSelection();
452 
453         if (sel != null 
454 
455                 && sel.length > 0 
456 
457                 && sel[0].getBounds(0).contains(e.x, e.y)) {
458 
459             new MenuItem(contextMenu, SWT.SEPARATOR);
460 
461             
462 
463             item = new MenuItem(contextMenu, SWT.PUSH);
464 
465             item.setText("Remove Source");
466 
467             item.addSelectionListener(this);
468 
469         }
470 
471         contextMenu.setLocation(sourcesList.toDisplay(e.x, e.y));
472 
473         contextMenu.setVisible(true);
474 
475     }
476 
477     
478 
479     private void doSelectTarget() {
480 
481         WSMOChooser iChooser = new WSMOChooser(
482 
483                                              sourcesList.getShell(),
484 
485                                              WSMORuntime.getRuntime(),
486 
487                                              WSMOContentProvider.TOP_ENTITY_MASK);
488 
489         iChooser.setFilter(new IWSMOSelectionValidator() {
490 
491             public String isValid(Object tested) {
492 
493                 if (tested instanceof TopEntity) {
494 
495                     return null;
496 
497                 }
498 
499                 return "A Mediateable selection expected!";
500 
501             }
502 
503         });
504 
505 
506 
507         TopEntity newTarget = (TopEntity)iChooser.open();
508 
509         if (newTarget == null
510 
511                 || (model.getMediator().getTarget()!= null
512 
513                     && model.getMediator().getTarget().equals(newTarget.getIdentifier()))) {
514 
515             return; // take no action
516 
517         }
518 
519         try {
520 
521             model.setTarget((IRI)newTarget.getIdentifier());
522 
523         }
524 
525         catch(InvalidModelException ime) {
526 
527             LogManager.logError(ime);
528 
529         }
530 
531     }
532 
533     
534 
535     public void dispose() {
536 
537         nsPanel.dispose();
538 
539         nfpPanel.dispose();
540 
541         super.dispose();
542 
543     }
544 
545 
546 
547     protected void doTryToSave() throws Exception {
548 
549 
550 
551         nsPanel.doUpdate();
552 
553         if (true == dirtyContent) { 
554 
555             if (medServiceField.getText().trim().length() == 0) {
556 
557                 if (model.getMediator().getMediationService() != null) {
558 
559                     model.setMediationService(null);
560 
561                 }
562 
563             }
564 
565             else if (model.getMediator().getMediationService() == null
566 
567                     || false == model.getMediator().getMediationService().toString().equals(
568 
569                             medServiceField.getText())) {
570 
571                 model.setMediationService(
572 
573                         WSMORuntime.getRuntime().getWsmoFactory().createIRI(
574 
575                                 medServiceField.getText()));
576 
577             }
578 
579             dirtyContent = false;
580 
581         }
582 
583         
584 
585         WSMORuntime.getRuntime().doUpdateEntity(model.getMediator());
586 
587     }
588 
589 
590 
591     public void widgetSelected(SelectionEvent e) {
592 
593         String action = ((MenuItem)e.widget).getText();
594 
595         
596 
597         if (action.equals("Remove Source")) {
598 
599             TableItem[] sel = sourcesList.getSelection();
600 
601             IRI entry = (IRI)sel[0].getData();
602 
603             try {
604 
605                 model.removeSource(entry);
606 
607             }
608 
609             catch(InvalidModelException ime) {
610 
611                 LogManager.logError(ime);
612 
613             }
614 
615             return;
616 
617         }
618 
619         
620 
621         if (action.equals("Add Source")) {
622 
623             WSMOChooser iChooser = new WSMOChooser(sourcesList.getShell(),
624 
625                                                    WSMORuntime.getRuntime(),
626 
627                                                    WSMOContentProvider.TOP_ENTITY_MASK);
628 
629             iChooser.setFilter(new IWSMOSelectionValidator() {
630 
631                 public String isValid(Object tested) {
632 
633                     if (tested instanceof TopEntity) {
634 
635                         return null;
636 
637                     }
638 
639                     return "A Mediateable selection expected!";
640 
641                 }
642 
643             });
644 
645             
646 
647             TopEntity newMedi = (TopEntity)iChooser.open();
648 
649             if (newMedi == null 
650 
651                     || model.getMediator().listSources().contains(newMedi.getIdentifier())) {
652 
653                 return; // take no action
654 
655             }
656 
657             try {
658 
659                 model.addSource((IRI)newMedi.getIdentifier());
660 
661             }
662 
663             catch(InvalidModelException ime) {
664 
665                 LogManager.logError(ime);
666 
667             }
668 
669             return;
670 
671         }
672 
673         
674 
675     }
676 
677     public void widgetDefaultSelected(SelectionEvent e) {
678 
679         
680 
681     }
682 
683     
684 
685 }
686 
687 
688 
689 /*
690 
691  * $Log: MediatorEditor.java,v $
692  * Revision 1.13  2006/04/06 14:04:25  alex_simov
693  * wsmo-api update: mediators are now referred by IRIs instead of Object handles
694  *
695  * Revision 1.12  2006/01/09 12:51:13  alex_simov
696  * Copyright message in header updated
697  *
698  * Revision 1.11  2005/11/09 16:17:39  alex_simov
699  * UIModels notification improved
700  *
701  * Revision 1.10  2005/11/02 14:50:57  alex_simov
702  * moving to MVC architecture cont'd
703  *
704  * Revision 1.9  2005/09/30 12:13:53  alex_simov
705  * unused variable 'item' assignment
706  *
707  * Revision 1.8  2005/09/16 14:25:11  alex_simov
708  * Identifier.asString() removed, use Object.toString() instead
709  *
710  * Revision 1.7  2005/08/08 12:19:48  alex_simov
711  * Utils.createTableItem() used where appropriate
712  *
713  * Revision 1.6  2005/08/02 10:33:20  alex_simov
714  * minor code and/or javadoc fixes
715  *
716  * Revision 1.5  2005/07/29 15:08:02  alex_simov
717  * added javadoc: class description, footer
718  *
719 
720  *
721 
722  */
723