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.wsmostudio.ui.editors.common;
26  
27  import java.util.*;
28  import java.util.List;
29  
30  import org.eclipse.jface.dialogs.MessageDialog;
31  import org.eclipse.jface.window.Window;
32  import org.eclipse.swt.SWT;
33  import org.eclipse.swt.dnd.*;
34  import org.eclipse.swt.events.*;
35  import org.eclipse.swt.layout.*;
36  import org.eclipse.swt.widgets.*;
37  import org.omwg.ontology.*;
38  import org.wsmo.common.*;
39  import org.wsmo.common.exception.InvalidModelException;
40  import org.wsmo.factory.WsmoFactory;
41  import org.wsmostudio.runtime.*;
42  import org.wsmostudio.ui.*;
43  import org.wsmostudio.ui.dnd.*;
44  import org.wsmostudio.ui.dnd.Clipboard;
45  import org.wsmostudio.ui.editors.model.ConceptModel;
46  
47  /***
48   * A helper GUI component, responsible for maintenance of a set of (WSMO-API) Instances
49   * of a certain WSMO-API Concept. It is a part of the Concept Editor component.
50   * The supported operations here are: creation, addition and removal of Instances.
51   *
52   * @author not attributable
53   * @version $Revision: 1.16 $ $Date: 2006/03/06 14:39:43 $
54   */
55  
56  public class InstancesContainerPanel implements SelectionListener {
57      
58      private Table instsTable;
59      
60      private ConceptModel model;
61      
62      public InstancesContainerPanel(TabFolder parentFolder,
63                                     ConceptModel model) {
64          this.model = model;
65  
66          TabItem mainItem = new TabItem(parentFolder, SWT.NONE);
67  
68          parentFolder.setLayoutData(new GridData(GridData.FILL_BOTH));
69          mainItem.setText("Instances");
70          parentFolder.setLayout(new GridLayout(1, false));
71          
72          createTable(parentFolder);
73          initDNDTarget();        
74          mainItem.setControl(instsTable);
75      }
76      
77      @SuppressWarnings("unchecked")
78      public void update() {
79          Set<Instance> toAddInstances = new HashSet<Instance>(
80                  model.getConcept().listInstances());
81  
82          TableItem[] items = instsTable.getItems();
83          for (int i = 0; i < items.length; i++) {
84              if (toAddInstances.contains(items[i].getData())) {
85                  toAddInstances.remove(items[i].getData());
86              }
87              else {
88                  items[i].dispose(); // the instance is removed
89              }
90          }
91          
92          // add the missing entries
93          for(Instance instance : toAddInstances) {
94              Utils.createTableItem(instsTable, 
95                      instance, 
96                      WsmoImageRegistry.INSTANCE_ICON);
97          }
98          
99      }
100     
101     private void createTable(Composite parent) {
102         instsTable = new Table(parent, SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL);
103         instsTable.setLayoutData(new GridData(GridData.FILL_BOTH));
104         instsTable.setLinesVisible(false);
105 
106         for(Iterator it = model.getConcept().listInstances().iterator(); it.hasNext();) {
107             Utils.createTableItem(instsTable, 
108                     (Instance)it.next(), 
109                     WsmoImageRegistry.INSTANCE_ICON);
110         }
111         instsTable.addMouseListener(new MouseAdapter() {
112             public void mouseUp(MouseEvent e) {
113                 if (e.button != 3) {
114                     return;
115                 }
116                 showContextMenu(e);
117             }
118         });
119     }
120     
121     private void showContextMenu(MouseEvent e) {
122         Menu contextMenu = new Menu(instsTable.getShell(), SWT.POP_UP);
123 
124         MenuItem item = new MenuItem(contextMenu, SWT.PUSH);
125         item.setText("Add Instance");
126         item.addSelectionListener(this);
127 
128         item = new MenuItem(contextMenu, SWT.SEPARATOR);
129 
130         item = new MenuItem(contextMenu, SWT.PUSH);
131         item.setText("Create Instance");
132         item.addSelectionListener(this);
133     
134         TableItem[] sel = instsTable.getSelection();
135         if (sel != null 
136                 && sel.length > 0 
137                 && sel[0].getBounds(0).contains(e.x, e.y)) {
138             item = new MenuItem(contextMenu, SWT.SEPARATOR);
139             
140             item = new MenuItem(contextMenu, SWT.PUSH);
141             item.setText("Remove Instance");
142             item.addSelectionListener(this);
143         }
144         
145         contextMenu.setLocation(instsTable.toDisplay(e.x, e.y));
146         contextMenu.setVisible(true);
147     }
148     
149     public void dispose() {
150         instsTable.dispose();
151     }
152     
153     public void widgetSelected(SelectionEvent e) {
154         String action = ((MenuItem)e.widget).getText();
155         if (action.equals("Create Instance")) {
156             WsmoFactory factory = WSMORuntime.getRuntime().getWsmoFactory();
157             IdentifierInputDialog iDialog = new IdentifierInputDialog(
158                     instsTable.getShell(), 
159                     "New Instance ID", 
160                     "Name:",
161                     model.getConcept().getOntology(),
162                     factory, 
163                     false);
164             
165             if (iDialog.open() != Window.OK) {
166                 return;
167             }
168             Identifier instanceID = iDialog.getIdentifier();
169             try {
170                 Instance instance = factory.createInstance(instanceID);
171                 model.addInstance(instance);
172             }
173             catch(InvalidModelException ime) {
174                 LogManager.logError("Error creating instance "
175                         + ((instanceID != null) ? instanceID.toString() : ""), 
176                         ime);
177             }
178             instsTable.redraw();
179             return;
180         }
181         
182         if (action.equals("Remove Instance")) {
183             TableItem[] sel = instsTable.getSelection();
184             Instance instance = (Instance)sel[0].getData();
185             try {
186                 model.removeInstance(instance);
187             }
188             catch(InvalidModelException ime) {
189                 LogManager.logError(ime);
190             }
191             return;
192         }
193         if (action.equals("Add Instance")) {
194             WSMOChooser chooser = WSMOChooser.createInstanceChooser(
195                     instsTable.getShell(),
196                     model.getConcept().getOntology()); 
197             Instance newInstance = (Instance)chooser.open();
198             if ((newInstance == null) 
199                     || (newInstance.listConcepts().contains(model.getConcept()))) {
200                 return;
201             }
202             try {
203                 model.getConcept().getOntology().addInstance(newInstance);
204                 model.addInstance(newInstance);
205             }
206             catch(InvalidModelException ime) {
207                 LogManager.logError(ime);
208             }
209             return;
210         }
211         
212     }
213 
214     private void initDNDTarget() {
215         DropTarget target = new DropTarget(instsTable, 
216                 DND.DROP_LINK | DND.DROP_DEFAULT); 
217           
218         final WSMOTransfer wsmoTransfer = WSMOTransfer.getInstance(); 
219         target.setTransfer(new Transfer[] {wsmoTransfer}); 
220           
221         target.addDropListener(new DropTargetAdapter() { 
222             public void dragEnter(DropTargetEvent event) { 
223                 if (true == Clipboard.getInstance().ensureContentType(Instance.class)) {
224                     event.detail = DND.DROP_LINK;
225                 }
226                 else {
227                     event.detail = DND.DROP_NONE;
228                 }
229             } 
230             public void dragOver(DropTargetEvent event) { 
231                 event.feedback = DND.FEEDBACK_SELECT | DND.FEEDBACK_SCROLL; 
232             } 
233             public void drop(DropTargetEvent event) { 
234                 if (wsmoTransfer.isSupportedType(event.currentDataType)) { 
235                     List<Entity> instances = Clipboard.getInstance().getContent();
236                     for(Entity entity : instances) {
237                         if (entity instanceof Instance) {
238                             try {
239                                 if (false == model.getConcept().listInstances().contains(entity)) {
240                                     model.addInstance((Instance)entity);
241                                 }
242                             }
243                             catch(InvalidModelException ime) {
244                                 MessageDialog.openError(instsTable.getShell(), 
245                                         "Error", 
246                                         ime.getMessage());
247                                 LogManager.logError(ime);
248                             }
249                         }
250                     }
251                 } 
252             } 
253         }); 
254     }
255     
256     public void widgetDefaultSelected(SelectionEvent e) {
257     }
258 }
259 
260 /*
261  * $Log: InstancesContainerPanel.java,v $
262  * Revision 1.16  2006/03/06 14:39:43  alex_simov
263  * Instances are now detached from the concept instead of being deleted
264  *
265  * Revision 1.15  2006/01/09 12:51:13  alex_simov
266  * Copyright message in header updated
267  *
268  * Revision 1.14  2005/11/15 15:54:40  alex_simov
269  * Drag-and-drop support added from WSMO Navigator to related (ontology) editors
270  *
271  * Revision 1.13  2005/11/09 16:16:27  alex_simov
272  * UIModels notification improved
273  *
274  * Revision 1.12  2005/11/02 08:57:03  alex_simov
275  * moving to a better Model-View-Contoller architecture
276  *
277  * Revision 1.11  2005/09/16 14:25:11  alex_simov
278  * Identifier.asString() removed, use Object.toString() instead
279  *
280  * Revision 1.10  2005/09/14 10:03:36  alex_simov
281  * new specialized Identifier input UI component used
282  *
283  * Revision 1.9  2005/09/08 16:46:25  alex_simov
284  * Migrating to Java 1.5
285  *
286  * Revision 1.8  2005/08/08 12:15:33  alex_simov
287  * Utils.createTableItem() used where appropriate
288  *
289  * Revision 1.7  2005/08/05 15:32:29  alex_simov
290  * editors input change notification mechanism added
291  *
292  * Revision 1.6  2005/08/02 10:33:21  alex_simov
293  * minor code and/or javadoc fixes
294  *
295  * Revision 1.5  2005/07/29 15:08:02  alex_simov
296  * added javadoc: class description, footer
297  *
298  *
299  */