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;
26  
27  import java.util.List;
28  
29  import org.eclipse.jface.dialogs.MessageDialog;
30  import org.eclipse.jface.resource.JFaceResources;
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.layout.*;
36  import org.eclipse.swt.widgets.*;
37  import org.eclipse.ui.*;
38  import org.omwg.ontology.*;
39  import org.wsmo.common.exception.InvalidModelException;
40  import org.wsmostudio.runtime.*;
41  import org.wsmostudio.ui.Utils;
42  import org.wsmostudio.ui.dnd.*;
43  import org.wsmostudio.ui.dnd.Clipboard;
44  import org.wsmostudio.ui.editors.common.*;
45  import org.wsmostudio.ui.editors.model.RelationInstanceModel;
46  import org.wsmostudio.ui.views.navigator.WSMOLabelProvider;
47  
48  /***
49   * An editor component, subclass of WSMOEditor, capable to handle with WSMO-API 
50   * Relation Instance objects.
51   * This editor appears as a part of the WSMO Studio workbench. It is the default
52   * editor implementation for this kind of objects and it can be replaced by 
53   * extending extension points <code>org.wsmostudio.ui.editors</code> and 
54   * <code>org.eclipse.ui.editors</code>.
55   *
56   * @author not attributable
57   * @version $Revision: 1.21 $ $Date: 2006/01/09 12:51:13 $
58   */
59  
60  public class RelationInstanceEditor extends WSMOEditor 
61          implements SelectionListener {
62  
63      private NFPPanel nfpPanel;
64      private RelationInstanceModel model;
65      private TabFolder paramValsFolder;
66      
67      private Text relationField;
68      private TreeViewer attrValsTable;
69      
70      public void createPartControl(Composite parent) {
71          parent.setLayout(new GridLayout(1, false));
72          
73          nfpPanel = new NFPPanel(parent, model);
74          
75          Group supRelPanel = new Group(parent, SWT.NONE);
76          supRelPanel.setLayout(new GridLayout(4, false));
77          supRelPanel.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
78          supRelPanel.setText("Relation");
79          new Label(supRelPanel, SWT.NONE).setImage(
80                  JFaceResources.getImageRegistry().get(
81                          WsmoImageRegistry.RELATION_ICON));
82          new Label(supRelPanel, SWT.NONE).setText("Identifier: ");
83          
84          relationField = new Text(supRelPanel, SWT.BORDER);
85          relationField.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
86          if (model.getInstance().getRelation() != null) {
87              relationField.setText(
88                      model.getInstance().getRelation().getIdentifier().toString());
89          }
90          
91          Button selectRel = new Button(supRelPanel, SWT.PUSH);
92          selectRel.setText("Select");
93          selectRel.addSelectionListener(new SelectionAdapter() {
94              public void widgetSelected(SelectionEvent e) {
95                  doSelectRelation();
96              }
97          });
98          
99          paramValsFolder = new TabFolder(parent, SWT.NONE);
100         paramValsFolder.setLayoutData(new GridData(GridData.FILL_BOTH));
101         createAttrValsView(paramValsFolder);
102         initDNDTargetValues();
103     }
104     
105     public void init(IEditorSite site, IEditorInput input) throws PartInitException {
106         super.init(site, input);
107         this.model = (RelationInstanceModel)input.getAdapter(RelationInstanceModel.class);
108     }
109 
110     public void dispose() {
111         nfpPanel.dispose();
112         super.dispose();
113     }
114 
115     protected void updateFromModel() {
116         attrValsTable.refresh(true);
117     }
118 
119     protected void doTryToSave() throws Exception {
120         WSMORuntime.getRuntime().doUpdateEntity(model.getInstance().getOntology());
121     }
122     
123     private void showContextMenu(MouseEvent e) {
124         TreeItem[] sel = attrValsTable.getTree().getSelection();
125         if (sel == null 
126                 || sel.length == 0 
127                 || false == sel[0].getBounds().contains(e.x, e.y)) {
128             return; // no menu needed
129         }
130         
131         Menu contextMenu = new Menu(getSite().getShell(), SWT.POP_UP);
132         MenuItem item;
133         
134         if (sel[0].getData() instanceof Parameter) {
135             item = new MenuItem(contextMenu, SWT.PUSH);
136             item.setText("Set Data Value");
137             item.addSelectionListener(this);
138 
139             item = new MenuItem(contextMenu, SWT.PUSH);
140             item.setText("Set Instance Value");
141             item.addSelectionListener(this);
142             
143             new MenuItem(contextMenu, SWT.SEPARATOR);
144 
145             item = new MenuItem(contextMenu, SWT.PUSH);
146             item.setText("Clear Value");
147             item.setEnabled(sel[0].getItemCount() > 0);
148             item.addSelectionListener(this);
149         }
150         else {
151             item = new MenuItem(contextMenu, SWT.PUSH);
152             item.setText("Clear Value");
153             item.addSelectionListener(this);
154         }
155         contextMenu.setLocation(attrValsTable.getTree().toDisplay(e.x, e.y));
156         contextMenu.setVisible(true);
157     }
158     
159     private void createAttrValsView(TabFolder parent) {
160         TabItem tabItem = new TabItem(parent, SWT.NONE);
161         tabItem.setText("Parameter Values");
162         
163         attrValsTable = new TreeViewer(parent, SWT.BORDER);
164         attrValsTable.setContentProvider(
165                 new ParamValueProvider(model.getInstance()));
166         attrValsTable.setLabelProvider(new WSMOLabelProvider() {
167             public String getText(Object element) {
168                 if (element instanceof Parameter) {
169                     return "parameter";
170                 }
171                 if (element instanceof DataValue) {
172                     return ((DataValue)element).toString();
173                 }
174                 return super.getText(element);
175             }
176         });
177         attrValsTable.setInput(model.getInstance());
178         attrValsTable.getTree().addMouseListener(new MouseAdapter() {
179             public void mouseUp(MouseEvent e) {
180                 if (e.button != 3) {
181                     return;
182                 }
183                 showContextMenu(e);
184             }
185         });
186         attrValsTable.expandToLevel(2);
187         tabItem.setControl(attrValsTable.getControl());
188     }
189     
190     private void doSelectRelation() {
191         WSMOChooser chooser = WSMOChooser.createRelationChooser(
192                 getSite().getShell(),
193                 WSMORuntime.getRuntime());
194         Relation newSuperRelation = (Relation)chooser.open();
195         if (newSuperRelation != null) {
196             try {
197                 model.setRelation(newSuperRelation);
198                 attrValsTable.refresh();
199                 relationField.setText(newSuperRelation
200                                           .getIdentifier()
201                                               .toString());
202             }
203             catch(InvalidModelException ime) {
204                 LogManager.logError(ime);
205             }
206         }
207     }
208 
209     public void widgetSelected(SelectionEvent event) {
210         String action = ((MenuItem)event.widget).getText();
211         if (action.equals("Clear Value")) {
212             TreeItem[] sel = attrValsTable.getTree().getSelection();
213             Parameter selectedParam = null;
214             if (false == sel[0].getData() instanceof Parameter) {
215                 selectedParam = (Parameter)sel[0].getParentItem().getData();
216             }
217             else {
218                 selectedParam = (Parameter)sel[0].getData();
219             }
220             int pos = model.getInstance().getRelation().listParameters().indexOf(selectedParam);
221             try {
222                 model.setParameterValue((byte)pos, null);
223             }
224             catch(InvalidModelException ime) {
225                 LogManager.logError(ime);
226             }
227             return;
228         }
229         if (action.equals("Set Data Value")) {
230             
231             IStructuredSelection sel = (IStructuredSelection)attrValsTable.getSelection();
232             Parameter selectedParam = (Parameter)sel.getFirstElement();
233             DataValue value = DataValueEditor.createEditor(
234                     getSite().getShell(),
235                     Utils.getTypesAsString(selectedParam.listTypes()));
236             if (value == null) {
237                 return;
238             }
239             doSetAttrValue(selectedParam, value);
240             return;
241         }
242         if (action.equals("Set Instance Value")) {
243             
244             IStructuredSelection sel = (IStructuredSelection)attrValsTable.getSelection();
245             Parameter selectedParam = (Parameter)sel.getFirstElement();
246             WSMOChooser chooser = WSMOChooser.createInstanceChooser(
247                     getSite().getShell(),
248                     WSMORuntime.getRuntime());
249             Instance newConceptType = (Instance)chooser.open();
250             if (newConceptType == null) {
251                 return;
252             }
253             doSetAttrValue(selectedParam, newConceptType);
254             return;
255         }
256     }
257     public void widgetDefaultSelected(SelectionEvent event) {
258     }
259     
260     private void doSetAttrValue(Parameter selectedParam, Value value) {
261         int pos = model.getInstance().getRelation().listParameters().indexOf(selectedParam);
262         try {
263             model.setParameterValue((byte)pos, value);
264         }
265         catch(InvalidModelException ime) {
266             LogManager.logError(ime);
267         }
268     }
269     
270     private void initDNDTargetValues() {
271         DropTarget target = new DropTarget(attrValsTable.getTree(), 
272                 DND.DROP_LINK | DND.DROP_DEFAULT); 
273           
274         final WSMOTransfer wsmoTransfer = WSMOTransfer.getInstance(); 
275         target.setTransfer(new Transfer[] {wsmoTransfer}); 
276           
277         target.addDropListener(new DropTargetAdapter() { 
278             public void dragEnter(DropTargetEvent event) { 
279                 event.detail = DND.DROP_NONE;
280             } 
281             public void dragOver(DropTargetEvent event) { 
282                 event.feedback = DND.FEEDBACK_SELECT | DND.FEEDBACK_SCROLL | DND.FEEDBACK_EXPAND; 
283                 if (event.item != null) {
284                     Object selected = event.item.getData();
285                     if (selected instanceof Parameter
286                             && Clipboard.getInstance().ensureContentType(Value.class)
287                             && Clipboard.getInstance().getSize() == 1) {
288                         event.detail = DND.DROP_LINK;
289                     }
290                     else {
291                         event.detail = DND.DROP_NONE;
292                     }
293                 }
294             } 
295             public void drop(DropTargetEvent event) { 
296                 if (wsmoTransfer.isSupportedType(event.currentDataType)) { 
297                     if (event.item == null) {
298                         return;
299                     }
300                     Object selected = event.item.getData();
301                     if (false == selected instanceof Parameter) {
302                         return;
303                     }
304                     
305                     byte pos = (byte)model.getInstance().getRelation()
306                             .listParameters().indexOf((Parameter)selected);
307                     try {
308                         model.setParameterValue(pos, 
309                                 (Value)Clipboard.getInstance().getContent().get(0));
310                     }
311                     catch(InvalidModelException ime) {
312                         MessageDialog.openError(getSite().getShell(), 
313                                 "Error", 
314                                 ime.getMessage());
315                         LogManager.logError(ime);
316                     }
317                 } 
318             } 
319         }); 
320     }
321 
322 }
323 
324 class ParamValueProvider implements ITreeContentProvider {
325 
326     private RelationInstance relInstance;
327     
328     ParamValueProvider(RelationInstance relInst) {
329         this.relInstance = relInst;
330     }
331     
332     public Object[] getChildren(Object parentElement) {
333         if (parentElement == relInstance) {
334             if (relInstance.getRelation() == null) {
335                 return null;
336             }
337             return relInstance.getRelation().listParameters().toArray();
338         }
339         if (parentElement instanceof Parameter) {
340             List vals = relInstance.getRelation().listParameters();
341             int paramPos = indexOf(vals, parentElement);
342             if (paramPos != -1) {
343                 try {
344                     Object value = relInstance.getParameterValue((byte)paramPos);
345                     if (value != null) {
346                         return new Object[] { value };
347                     }
348                 }
349                 catch(Exception e) { 
350                     LogManager.logError(e);
351                 }
352             }
353         }
354         return null;
355     }
356 
357     public Object getParent(Object element) {
358         return null;
359     }
360 
361     public boolean hasChildren(Object element) {
362         Object[] children = getChildren(element);
363         return children != null 
364                 && children.length > 0;
365     }
366     
367     public Object[] getElements(Object inputElement) {
368         if (inputElement == relInstance) {
369             return getChildren(inputElement);
370         }
371         return null;
372     }
373 
374     public void dispose() {
375     }
376 
377     public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
378     }
379     
380     private int indexOf(List params, Object param) {
381         for (int i = 0; i < params.size(); i++) {
382             // here instance identity is important as Parameter.equals() 
383             // is not precise enough
384             if (params.get(i) == param) { 
385                 return i;
386             }
387         }
388         return -1;
389     }
390 }
391 
392 /*
393  * $Log: RelationInstanceEditor.java,v $
394  * Revision 1.21  2006/01/09 12:51:13  alex_simov
395  * Copyright message in header updated
396  *
397  * Revision 1.20  2005/12/06 09:42:04  alex_simov
398  * DataValue editor made aware of type restriction depending on usage
399  *
400  * Revision 1.19  2005/12/01 13:51:07  alex_simov
401  * UI fix
402  *
403  * Revision 1.18  2005/12/01 10:20:18  alex_simov
404  * UI bugfix: Parameter.equals() not working properly. Object identity used instead
405  *
406  * Revision 1.17  2005/11/15 15:54:17  alex_simov
407  * Drag-and-drop support added from WSMO Navigator to related (ontology) editors
408  *
409  * Revision 1.16  2005/11/09 16:17:39  alex_simov
410  * UIModels notification improved
411  *
412  * Revision 1.15  2005/11/02 14:50:57  alex_simov
413  * moving to MVC architecture cont'd
414  *
415  * Revision 1.14  2005/09/21 15:09:11  alex_simov
416  * minor fixes
417  *
418  * Revision 1.13  2005/09/16 14:25:11  alex_simov
419  * Identifier.asString() removed, use Object.toString() instead
420  *
421  * Revision 1.12  2005/09/10 09:17:21  alex_simov
422  * bugfix
423  *
424  * Revision 1.11  2005/08/08 12:19:48  alex_simov
425  * Utils.createTableItem() used where appropriate
426  *
427  * Revision 1.10  2005/08/02 10:33:20  alex_simov
428  * minor code and/or javadoc fixes
429  *
430  * Revision 1.9  2005/07/29 15:08:02  alex_simov
431  * added javadoc: class description, footer
432  *
433  *
434  */