View Javadoc

1   /*
2    WSMO Studio - a Semantic Web Service Editor
3    Copyright (c) 2004-2007, 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-2007</p>
22   * <p>Company: Ontotext Lab. / SIRMA </p>
23   */
24  
25  package org.wsmostudio.grounding.sawsdl.ui;
26  
27  import java.util.*;
28  import java.util.List;
29  
30  import org.eclipse.jface.action.*;
31  import org.eclipse.jface.viewers.*;
32  import org.eclipse.swt.SWT;
33  import org.eclipse.swt.dnd.*;
34  import org.eclipse.swt.events.*;
35  import org.eclipse.swt.widgets.*;
36  import org.eclipse.ui.part.ViewPart;
37  import org.omwg.ontology.Ontology;
38  import org.wsmo.common.Entity;
39  import org.wsmostudio.runtime.*;
40  import org.wsmostudio.ui.GUIHelper;
41  import org.wsmostudio.ui.dnd.*;
42  import org.wsmostudio.ui.dnd.Clipboard;
43  import org.wsmostudio.ui.views.navigator.*;
44  import org.wsmostudio.ui.views.navigator.actions.OpenTypeAction;
45  
46  public class OntologyViewer extends ViewPart {
47  
48      public static final String VIEW_ID = "org.wsmostudio.grounding.sawsdl.ontoView";
49      private TreeViewer treeViewer;
50      
51      @Override
52      public void createPartControl(Composite parent) {
53          WsmoImageRegistry.declareStudioImages();
54          treeViewer = new TreeViewer(parent);
55          treeViewer.setContentProvider(new WSMOContentProvider(WSMOContentProvider.NO_FILTER));//CONCEPT_MASK));
56          treeViewer.setLabelProvider(new WSMOLabelProvider());
57  
58          treeViewer.setInput(WSMORuntime.getRuntime());
59          createContextMenu();
60          treeViewer.getTree().addKeyListener(new KeyAdapter() {
61              public void keyReleased(KeyEvent e) {
62                  if (e.keyCode == SWT.F5) {
63                      treeViewer.refresh();
64                  }
65              }
66          });
67          treeViewer.getTree().addMouseListener(new MouseAdapter() {
68              public void mouseDoubleClick(MouseEvent e) {
69                  TreeItem[] sel = treeViewer.getTree().getSelection();
70                  if (sel != null && sel.length > 0) {
71                      if (sel[0].getItems().length > 0) {
72                          sel[0].setExpanded(!sel[0].getExpanded());
73                          treeViewer.refresh(sel[0].getData());
74                      }
75                  }
76              }
77          });
78  
79          initDNDSource();
80      }
81      
82      private void createContextMenu() {
83      	final MenuManager menuMgr = new MenuManager();
84      	menuMgr.setRemoveAllWhenShown(true);
85      	menuMgr.addMenuListener(new IMenuListener() {
86      		public void menuAboutToShow(IMenuManager mgr) {
87      			fillContextMenu(menuMgr);
88      		}
89      	});
90      	treeViewer.getTree().setMenu(menuMgr.createContextMenu(treeViewer.getTree()));
91      }
92  
93      private void fillContextMenu(MenuManager menuMgr) {
94  
95      	if (false == org.wsmostudio.ui.GUIHelper.containsCursor(treeViewer.getTree())) {
96      		return;
97      	}
98      	final Object selection = 
99              ((IStructuredSelection)treeViewer.getSelection()).getFirstElement();
100         if (selection == null) {
101             return;
102         }
103 
104         if (((ITreeContentProvider)treeViewer.getContentProvider()).hasChildren(selection)) {
105             menuMgr.add(new Action("Expand Subtree") {
106             	public void run() {
107             		treeViewer.expandToLevel(selection, TreeViewer.ALL_LEVELS);
108             	}
109             });
110         }
111         
112         if (selection instanceof Ontology) {
113             menuMgr.add(new Separator());
114             menuMgr.add(
115                     new OpenTypeAction(
116                             "Find Entity ...", treeViewer, (Ontology)selection));
117             menuMgr.add(new Separator());
118             menuMgr.add(new Action("Details") {
119             	public void run() {
120         			GUIHelper.showOntoStatistics(
121         					(Ontology)selection, 
122         					getSite().getShell());
123             	}
124             });
125         }
126     }
127 
128     @Override
129     public void setFocus() {
130         // TODO Auto-generated method stub
131 
132     }
133     
134     private void initDNDSource() {
135         DragSource source = new DragSource(treeViewer.getTree(), DND.DROP_LINK); 
136         Transfer[] types = new Transfer[] {WSMOTransfer.getInstance()}; 
137         source.setTransfer(types); 
138            
139         source.addDragListener(new DragSourceListener() { 
140             public void dragStart(DragSourceEvent event) { 
141                 IStructuredSelection sel = (IStructuredSelection)treeViewer.getSelection();
142                 if (sel == null 
143                         || sel.isEmpty()) {
144                     event.doit = false;
145                 }
146                 List<Entity> selection = new LinkedList<Entity>();
147                 for(Iterator it = sel.iterator(); it.hasNext();) {
148                     Object selObj = it.next();
149                     if (selObj instanceof Entity) {
150                         selection.add((Entity)selObj);
151                     }
152                 }
153                 Clipboard.getInstance().setContent(selection);
154             } 
155             public void dragSetData(DragSourceEvent event) { 
156               if (WSMOTransfer.getInstance().isSupportedType(event.dataType)) { 
157                    event.data = Clipboard.getInstance().getContent(); 
158               } 
159             } 
160             public void dragFinished(DragSourceEvent event) { 
161                 Clipboard.getInstance().clear();
162             }
163         }); 
164     }
165 }
166 
167 /*
168  * $Log$
169  * Revision 1.8  2007/07/19 12:54:07  alex_simov
170  * refactoring in WSMO UI plug-in
171  *
172  * Revision 1.7  2007/04/20 14:54:50  alex_simov
173  * Find entity in ontology functionality added
174  *
175  * Revision 1.6  2007/01/04 14:01:38  alex_simov
176  * no message
177  *
178  * Revision 1.5  2006/11/21 09:43:58  alex_simov
179  * The restriction for using only WSMO concepts in the annotation is removed.
180  *
181  * Revision 1.4  2006/09/04 15:23:33  alex_simov
182  * bugfix[1531709]: Context menus relied on right button mouse click instead of
183  * being registered as dedicated context menus for the corresponding UI
184  * controls
185  *
186  * Revision 1.3  2006/07/11 15:56:56  alex_simov
187  * bugfix: 'Details' context action must appear only on ontologies
188  *
189  * Revision 1.2  2006/07/06 14:43:31  alex_simov
190  * copyright header and footer added
191  *
192  */