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.jface.dialogs.MessageDialog;
58  
59  import org.eclipse.jface.resource.JFaceResources;
60  
61  import org.eclipse.jface.viewers.IStructuredSelection;
62  
63  import org.eclipse.jface.viewers.ITreeContentProvider;
64  
65  import org.eclipse.jface.viewers.StructuredSelection;
66  
67  import org.eclipse.jface.viewers.TreeViewer;
68  
69  import org.eclipse.jface.viewers.Viewer;
70  
71  import org.eclipse.swt.widgets.Composite;
72  
73  import org.eclipse.swt.widgets.Menu;
74  
75  import org.eclipse.swt.widgets.MenuItem;
76  
77  import org.eclipse.swt.widgets.TabFolder;
78  
79  import org.eclipse.swt.widgets.TabItem;
80  
81  import org.eclipse.swt.widgets.Table;
82  
83  import org.eclipse.swt.widgets.TableItem;
84  
85  import org.eclipse.swt.widgets.TreeItem;
86  
87  import org.eclipse.swt.SWT;
88  
89  import org.eclipse.swt.custom.SashForm;
90  
91  import org.eclipse.swt.dnd.*;
92  
93  import org.eclipse.swt.events.MouseAdapter;
94  
95  import org.eclipse.swt.events.MouseEvent;
96  
97  import org.eclipse.swt.events.SelectionEvent;
98  
99  import org.eclipse.swt.events.SelectionListener;
100 
101 import org.eclipse.swt.graphics.Image;
102 
103 
104 
105 import org.eclipse.swt.layout.*;
106 
107 import org.eclipse.ui.IEditorInput;
108 
109 import org.eclipse.ui.IEditorSite;
110 
111 import org.eclipse.ui.PartInitException;
112 
113 import org.wsmo.common.*;
114 
115 import org.wsmo.common.exception.InvalidModelException;
116 
117 import org.omwg.ontology.*;
118 
119 import org.wsmostudio.runtime.LogManager;
120 
121 import org.wsmostudio.runtime.WSMORuntime;
122 
123 import org.wsmostudio.runtime.WsmoImageRegistry;
124 
125 import org.wsmostudio.ui.Utils;
126 
127 import org.wsmostudio.ui.dnd.*;
128 
129 import org.wsmostudio.ui.dnd.Clipboard;
130 
131 import org.wsmostudio.ui.editors.common.*;
132 
133 import org.wsmostudio.ui.editors.model.InstanceModel;
134 
135 import org.wsmostudio.ui.views.navigator.WSMOLabelProvider;
136 
137 
138 
139 /***
140 
141  * An editor component, subclass of WSMOEditor, capable to handle with WSMO-API 
142 
143  * (Concept) Instance objects.
144 
145  * This editor appears as a part of the WSMO Studio workbench. It is the default
146 
147  * editor implementation for this kind of objects and it can be replaced by 
148 
149  * extending extension points <code>org.wsmostudio.ui.editors</code> and 
150 
151  * <code>org.eclipse.ui.editors</code>.
152 
153  *
154 
155  * @author not attributable
156 
157  * @version $Revision: 1.24 $ $Date: 2006/04/10 14:00:34 $
158 
159  */
160 
161 
162 
163 public class InstanceEditor extends WSMOEditor 
164 
165         implements SelectionListener {
166 
167 
168 
169     private NFPPanel nfpPanel;
170 
171     private InstanceModel model;
172 
173     private TabFolder conceptsFolder, attrValsFolder;
174 
175     
176 
177     private Table conceptsTable;
178 
179     private TreeViewer attrValuesList;
180 
181     
182 
183     public void createPartControl(Composite parent) {
184 
185         parent.setLayout(new GridLayout(1, false));
186 
187         
188 
189         nfpPanel = new NFPPanel(parent,model);
190 
191         
192 
193         SashForm splitter = new SashForm(parent, SWT.VERTICAL );
194 
195         splitter.setLayoutData(new GridData(GridData.FILL_BOTH));
196 
197 
198 
199         conceptsFolder = new TabFolder(splitter, SWT.NONE);
200 
201         createSuperConceptsView(conceptsFolder);
202 
203         initDNDTargetConcepts();
204 
205 
206 
207         attrValsFolder = new TabFolder(splitter, SWT.NONE);
208 
209         createAttrValsView(attrValsFolder);
210 
211         initDNDTargetValues();
212 
213 
214 
215         splitter.setWeights(new int[] {1, 2});
216 
217     }
218 
219     
220 
221     public void init(IEditorSite site, IEditorInput input) throws PartInitException {
222 
223         super.init(site, input);
224 
225         this.model = (InstanceModel)input.getAdapter(InstanceModel.class);
226 
227     }
228 
229 
230 
231     public void dispose() {
232 
233         nfpPanel.dispose();
234 
235         super.dispose();
236 
237     }
238 
239     
240 
241     public void updateFromModel() {
242 
243         refreshAttrValuesView();
244 
245         updateConceptsTable();
246 
247     }
248 
249 
250 
251     @SuppressWarnings("unchecked")
252 
253     private void updateConceptsTable() {
254 
255         Set<Concept> concepts = new HashSet<Concept>(
256 
257                 model.getInstance().listConcepts());
258 
259         TableItem[] items = conceptsTable.getItems();
260 
261         for(int i = 0; i < items.length; i++) {
262 
263             if (false == concepts.contains(items[i].getData())) {
264 
265                 items[i].dispose();
266 
267             }
268 
269             else {
270 
271                 concepts.remove(items[i].getData());
272 
273             }
274 
275         }
276 
277         for(Concept concept : concepts) {
278 
279             Utils.createTableItem(conceptsTable, 
280 
281                     concept, 
282 
283                     WsmoImageRegistry.CONCEPT_ICON);
284 
285         }
286 
287     }
288 
289 
290 
291     protected void doTryToSave() throws Exception {
292 
293         WSMORuntime.getRuntime().doUpdateEntity(model.getInstance().getOntology());
294 
295     }
296 
297     
298 
299     
300 
301     private void createSuperConceptsView(TabFolder parent) {
302 
303         TabItem tabItem = new TabItem(parent, SWT.NONE);
304 
305         tabItem.setText("Concepts");
306 
307         
308 
309         conceptsTable = new Table(parent, SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL);
310 
311         conceptsTable.setLinesVisible(false);
312 
313         Set superConcepts = model.getInstance().listConcepts();
314 
315         for(Iterator it = superConcepts.iterator(); it.hasNext();) {
316 
317             Utils.createTableItem(conceptsTable, 
318 
319                                   (Concept)it.next(), 
320 
321                                   WsmoImageRegistry.CONCEPT_ICON);
322 
323         }
324 
325         conceptsTable.addMouseListener(new MouseAdapter() {
326 
327             public void mouseUp(MouseEvent e) {
328 
329                 if (e.button != 3) {
330 
331                     return;
332 
333                 }
334 
335                 showContextMenu(e);
336 
337             }
338 
339             
340 
341         });
342 
343         tabItem.setControl(conceptsTable);
344 
345     }
346 
347     private void showContextMenu(MouseEvent e) {
348 
349         Menu contextMenu = new Menu(conceptsTable.getShell(), SWT.POP_UP);
350 
351 
352 
353         MenuItem item = new MenuItem(contextMenu, SWT.PUSH);
354 
355         item.setText("Add Concept");
356 
357         item.addSelectionListener(this);
358 
359 
360 
361         TableItem[] sel = conceptsTable.getSelection();
362 
363         if (sel != null 
364 
365                 && sel.length > 0 
366 
367                 && sel[0].getBounds(0).contains(e.x, e.y)) {
368 
369             new MenuItem(contextMenu, SWT.SEPARATOR);
370 
371             
372 
373             item = new MenuItem(contextMenu, SWT.PUSH);
374 
375             item.setText("Remove Concept");
376 
377             item.addSelectionListener(this);
378 
379         }
380 
381         
382 
383         contextMenu.setLocation(conceptsTable.toDisplay(e.x, e.y));
384 
385         contextMenu.setVisible(true);
386 
387     }
388 
389 
390 
391     private void showAttrContextMenu(MouseEvent e) {
392 
393         Menu contextMenu = new Menu(conceptsTable.getShell(), SWT.POP_UP);
394 
395 
396 
397         TreeItem[] sel = attrValuesList.getTree().getSelection();
398 
399         String[] actions = null;
400 
401         Object selectedEntry = null;
402 
403         if (sel != null 
404 
405                 && sel.length > 0 
406 
407                 && sel[0].getBounds().contains(e.x, e.y)) {
408 
409             selectedEntry = sel[0].getData();
410 
411         }
412 
413         if (selectedEntry == null) { // no selection or click outside selection
414 
415             actions = new String[] { "New Attribute Data Value",
416 
417                                      "New Attribute Instance Value" };
418 
419         }
420 
421         else if (selectedEntry instanceof Identifier) {
422 
423             actions = new String[] { "Add Data Value", 
424 
425                                      "Add Instance Value", 
426 
427                                      null, 
428 
429                                      "Remove Attribute" };
430 
431         }
432 
433         else if (selectedEntry instanceof DataValue) {
434 
435             actions = new String[] { "Edit Value", 
436 
437                                      null, 
438 
439                                      "Remove Value" };
440 
441         }
442 
443         else if (selectedEntry instanceof Entity) {
444 
445             actions = new String[] { "Remove Value" };
446 
447         }
448 
449         
450 
451         MenuItem item;
452 
453         for (int i = 0; i < actions.length; i++) {
454 
455             if (actions[i] == null) { // put menu separator 
456 
457                 new MenuItem(contextMenu, SWT.SEPARATOR);
458 
459             }
460 
461             else {
462 
463                 item = new MenuItem(contextMenu, SWT.PUSH);
464 
465                 item.setText(actions[i]);
466 
467                 item.addSelectionListener(this);
468 
469             }
470 
471         }
472 
473         contextMenu.setLocation(attrValuesList.getTree().toDisplay(e.x, e.y));
474 
475         contextMenu.setVisible(true);
476 
477     }
478 
479 
480 
481     private void createAttrValsView(TabFolder parent) {
482 
483         TabItem tabItem = new TabItem(parent, SWT.NONE);
484 
485         tabItem.setText("Attribute Values");
486 
487         
488 
489         attrValuesList = new TreeViewer(parent, SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL);
490 
491 
492 
493         attrValuesList.setContentProvider(
494 
495                 new AttributeContentProvider(model.getInstance()));
496 
497         attrValuesList.setLabelProvider(new WSMOLabelProvider() {
498 
499             public String getText(Object element) {
500 
501                 if (element instanceof DataValue) {
502 
503                     return ((DataValue)element).toString();
504 
505                 }
506 
507                 if (element instanceof Identifier) {
508 
509                     return Utils.getEntityLocalName(element.toString());
510 
511                 }
512 
513                 return super.getText(element);
514 
515             }
516 
517             public Image getImage(Object element) {
518 
519                 if (element instanceof Identifier) {
520 
521                     return JFaceResources.getImageRegistry().get(
522 
523                             WsmoImageRegistry.ATTRIBUTE_ICON);
524 
525                 }
526 
527                 return super.getImage(element);
528 
529             }
530 
531         });
532 
533         attrValuesList.setInput(model.getInstance());
534 
535         attrValuesList.getTree().addMouseListener(new MouseAdapter() {
536 
537             public void mouseDoubleClick(MouseEvent e) {
538 
539                 TreeItem[] sel = attrValuesList.getTree().getSelection();
540 
541                 if (sel != null 
542 
543                         && sel.length > 0) {
544 
545                     if (sel[0].getData() instanceof Identifier) {
546 
547                         if (sel[0].getExpanded()) {
548 
549                             sel[0].setExpanded(false);
550 
551                         }
552 
553                         else {
554 
555                             attrValuesList.expandToLevel(
556 
557                                                  sel[0].getData(), 
558 
559                                                  TreeViewer.ALL_LEVELS);
560 
561                         }
562 
563                     }
564 
565                     else if (sel[0].getData() instanceof DataValue) {
566 
567                         doEditValue((DataValue)sel[0].getData());
568 
569                     }
570 
571                 }
572 
573             }
574 
575             public void mouseUp(MouseEvent e) {
576 
577                 if (e.button != 3) {
578 
579                     return;
580 
581                 }
582 
583                 showAttrContextMenu(e);
584 
585             }
586 
587 
588 
589         });
590 
591         
592 
593         tabItem.setControl(attrValuesList.getControl());
594 
595     }
596 
597 
598 
599     public void widgetSelected(SelectionEvent event) {
600 
601         String action = ((MenuItem)event.widget).getText();
602 
603         if (action.equals("Add Concept")) {
604 
605             WSMOChooser chooser = WSMOChooser.createConceptChooser(
606 
607                                                    getSite().getShell(),
608 
609                                                    WSMORuntime.getRuntime());
610 
611             Concept newSuperConcept = (Concept)chooser.open();
612 
613             if (newSuperConcept != null) {
614 
615                 try {
616 
617                     model.addConcept(newSuperConcept);
618 
619                 }
620 
621                 catch(InvalidModelException ime) {
622 
623                     LogManager.logError(ime);
624 
625                 }
626 
627             }
628 
629             return;
630 
631         }
632 
633         if (action.equals("Remove Concept")) {
634 
635             TableItem[] sel = conceptsTable.getSelection();
636 
637             if (sel == null || sel.length != 1) {
638 
639                 return;
640 
641             }
642 
643 
644 
645             try {
646 
647                 model.removeConcept((Concept)sel[0].getData());
648 
649             }
650 
651             catch(InvalidModelException ime) {
652 
653                 LogManager.logError(ime);
654 
655             }
656 
657         }
658 
659         // attribute actions support
660 
661         if (action.equals("Add Data Value")) {
662 
663             Identifier selAttr = (Identifier)
664 
665                 ((IStructuredSelection)attrValuesList.getSelection()).getFirstElement();
666 
667             DataValue newVal = DataValueEditor.createEditor(
668 
669                                                getSite().getShell(),
670 
671                                                Utils.collectAllTypesAsString(
672 
673                                                        model.getInstance().findAttributeDefinitions(selAttr)));
674 
675             if (newVal != null) {
676 
677                 try {
678 
679                     model.addAttributeValue(selAttr, newVal);
680 
681                 }
682 
683                 catch(InvalidModelException ime) {
684 
685                     LogManager.logError(ime);
686 
687                 }
688 
689             }
690 
691             return;
692 
693         }
694 
695         if (action.equals("Add Instance Value")) {
696 
697             WSMOChooser chooser = WSMOChooser.createInstanceChooser(
698 
699                                                    getSite().getShell(),
700 
701                                                    WSMORuntime.getRuntime());
702 
703             Instance newConceptType = (Instance)chooser.open();
704 
705             if (newConceptType != null) {
706 
707                 IStructuredSelection sel = (IStructuredSelection)
708 
709                                                attrValuesList.getSelection();
710 
711                 Identifier targetAttr = ((Identifier)sel.getFirstElement());
712 
713                 try {
714 
715                     model.addAttributeValue(targetAttr, newConceptType);
716 
717                 }
718 
719                 catch(InvalidModelException ime) {
720 
721                     LogManager.logError(ime);
722 
723                 }
724 
725                 attrValuesList.setSelection(new StructuredSelection(newConceptType));
726 
727             }
728 
729             return;
730 
731         }
732 
733         if (action.equals("Edit Value")) {
734 
735             DataValue val = (DataValue)((IStructuredSelection)attrValuesList.getSelection()).getFirstElement();
736 
737             doEditValue(val);
738 
739             return;
740 
741         }
742 
743         if (action.equals("Remove Value")) {
744 
745             Value val = (Value)((IStructuredSelection)attrValuesList.getSelection()).getFirstElement();
746 
747             TreeItem[] swtSelection = attrValuesList.getTree().getSelection();
748 
749             Identifier ownerAttr = (Identifier)swtSelection[0].getParentItem().getData();
750 
751             try {
752 
753                 model.removeAttributeValue(ownerAttr, val);
754 
755             }
756 
757             catch(InvalidModelException ime) {
758 
759                 LogManager.logError(ime);
760 
761             }
762 
763             attrValuesList.refresh(ownerAttr, true);
764 
765             return;
766 
767         }
768 
769 
770 
771         if (action.startsWith("New Attribute ")) {
772 
773             
774 
775             WSMOChooser chooser = new WSMOChooser(getSite().getShell(),
776 
777                     SWT.SINGLE,
778 
779                     WSMORuntime.getRuntime(),
780 
781                     new ContextAwareAttributeProvider(
782 
783                             model.getInstance().listConcepts()));
784 
785             
786 
787             chooser.setDialogTitle("Select Attribute");
788 
789             chooser.setFilter(new IWSMOSelectionValidator() {
790 
791                 public String isValid(Object selection) {
792 
793                     if (false == selection instanceof Attribute) {
794 
795                         return "Please select an Attribute";
796 
797                     }
798 
799                     return null; // i.e. no error
800 
801                 }
802 
803             });
804 
805             chooser.expandTree();
806 
807             
808 
809             Attribute attr = (Attribute)chooser.open();
810 
811             if (attr == null) { // operation has been cancelled
812 
813                 return;
814 
815             }
816 
817             Value attrValue = null;
818 
819             if (action.endsWith("Instance Value")) {
820 
821                 chooser = WSMOChooser.createInstanceChooser(
822 
823                         getSite().getShell(),
824 
825                         WSMORuntime.getRuntime());
826 
827                 chooser.setDialogTitle("Select Attribute Instance Value");
828 
829                 attrValue = (Value)chooser.open();
830 
831             }
832 
833             else {
834 
835                 attrValue = DataValueEditor.createEditor(
836 
837                         getSite().getShell(),
838 
839                         Utils.getTypesAsString(
840 
841                                 attr.listTypes()));
842 
843             }
844 
845 
846 
847             if (attrValue == null) { // operation has been cancelled
848 
849                 return;
850 
851             }
852 
853             try {
854 
855                 model.addAttributeValue(attr.getIdentifier(), attrValue);
856 
857             }
858 
859             catch(InvalidModelException ime) {
860 
861                 LogManager.logError(ime);
862 
863             }
864 
865             refreshAttrValuesView();
866 
867             return;
868 
869         }
870 
871 
872 
873         if (action.equals("Remove Attribute")) {
874 
875             Identifier attr = (Identifier)((IStructuredSelection)
876 
877                                              attrValuesList
878 
879                                                  .getSelection())
880 
881                                                      .getFirstElement();
882 
883             try {
884 
885                 model.removeAttributeValues(attr);
886 
887             }
888 
889             catch(InvalidModelException ime) {
890 
891                 LogManager.logError(ime);
892 
893             }
894 
895             refreshAttrValuesView();
896 
897             return;
898 
899         }
900 
901     }
902 
903     public void widgetDefaultSelected(SelectionEvent event) {
904 
905     }
906 
907     
908 
909     private void doEditValue(DataValue value) {
910 
911         TreeItem[] swtSelection = attrValuesList.getTree().getSelection();
912 
913         Identifier ownerAttr = (Identifier)swtSelection[0].getParentItem().getData();
914 
915 
916 
917         DataValue newValue = DataValueEditor.createEditor(
918 
919                 getSite().getShell(),
920 
921                 value,
922 
923                 Utils.collectAllTypesAsString(
924 
925                         model.getInstance().findAttributeDefinitions(ownerAttr)));
926 
927 
928 
929         if (newValue == null 
930 
931                 || newValue.equals(value)) {
932 
933             return;
934 
935         }
936 
937         
938 
939         try {
940 
941             model.removeAttributeValue(ownerAttr, value);
942 
943             model.addAttributeValue(ownerAttr, newValue);
944 
945         }
946 
947         catch(InvalidModelException ime) {
948 
949             LogManager.logError(ime);
950 
951         }
952 
953         attrValuesList.refresh(ownerAttr, true);
954 
955     }
956 
957     
958 
959     /*
960 
961      * Helper method which refreshes the attributes view. The call to prepareToRefresh()
962 
963      * raises a flag that the whole view will be refreshed and thus allowing the root to 
964 
965      * be visualized (otherwise it is locked in order to prevent recursive tree visualization).
966 
967      * 
968 
969      */
970 
971     
972 
973     private void refreshAttrValuesView() {
974 
975         ((AttributeContentProvider)attrValuesList.getContentProvider()).prepareToRefresh();
976 
977         attrValuesList.refresh(true);
978 
979     }
980 
981     
982 
983     private void initDNDTargetConcepts() {
984 
985         DropTarget target = new DropTarget(conceptsTable, 
986 
987                 DND.DROP_LINK | DND.DROP_DEFAULT); 
988 
989           
990 
991         final WSMOTransfer wsmoTransfer = WSMOTransfer.getInstance(); 
992 
993         target.setTransfer(new Transfer[] {wsmoTransfer}); 
994 
995           
996 
997         target.addDropListener(new DropTargetAdapter() { 
998 
999             public void dragEnter(DropTargetEvent event) { 
1000 
1001                 if (true == Clipboard.getInstance().ensureContentType(Concept.class)) {
1002 
1003                     event.detail = DND.DROP_LINK;
1004 
1005                 }
1006 
1007                 else {
1008 
1009                     event.detail = DND.DROP_NONE;
1010 
1011                 }
1012 
1013             } 
1014 
1015             public void dragOver(DropTargetEvent event) { 
1016 
1017                 event.feedback = DND.FEEDBACK_SELECT | DND.FEEDBACK_SCROLL; 
1018 
1019             } 
1020 
1021             public void drop(DropTargetEvent event) { 
1022 
1023                 if (wsmoTransfer.isSupportedType(event.currentDataType)) { 
1024 
1025                     List<Entity> concepts = Clipboard.getInstance().getContent();
1026 
1027                     for(Entity entity : concepts) {
1028 
1029                         if (entity instanceof Concept) {
1030 
1031                             try {
1032 
1033                                 if (false == model.getInstance().listConcepts().contains(entity)) {
1034 
1035                                     model.addConcept((Concept)entity);
1036 
1037                                 }
1038 
1039                             }
1040 
1041                             catch(InvalidModelException ime) {
1042 
1043                                 MessageDialog.openError(getSite().getShell(), 
1044 
1045                                         "Error", 
1046 
1047                                         ime.getMessage());
1048 
1049                                 LogManager.logError(ime);
1050 
1051                             }
1052 
1053                         }
1054 
1055                     }
1056 
1057                 } 
1058 
1059             } 
1060 
1061         }); 
1062 
1063     }
1064 
1065     private void initDNDTargetValues() {
1066 
1067         DropTarget target = new DropTarget(attrValuesList.getTree(), 
1068 
1069                 DND.DROP_LINK | DND.DROP_DEFAULT); 
1070 
1071           
1072 
1073         final WSMOTransfer wsmoTransfer = WSMOTransfer.getInstance(); 
1074 
1075         target.setTransfer(new Transfer[] {wsmoTransfer}); 
1076 
1077           
1078 
1079         target.addDropListener(new DropTargetAdapter() { 
1080 
1081             public void dragEnter(DropTargetEvent event) { 
1082 
1083                 event.detail = DND.DROP_NONE;
1084 
1085             } 
1086 
1087             public void dragOver(DropTargetEvent event) { 
1088 
1089                 event.feedback = DND.FEEDBACK_SELECT | DND.FEEDBACK_SCROLL | DND.FEEDBACK_EXPAND; 
1090 
1091                 if (event.item != null) {
1092 
1093                     Object selected = event.item.getData();
1094 
1095                     if (selected instanceof Identifier
1096 
1097                             && Clipboard.getInstance().ensureContentType(Value.class)) {
1098 
1099                         event.detail = DND.DROP_LINK;
1100 
1101                     }
1102 
1103                     else {
1104 
1105                         event.detail = DND.DROP_NONE;
1106 
1107                     }
1108 
1109                 }
1110 
1111             } 
1112 
1113             public void drop(DropTargetEvent event) { 
1114 
1115                 if (wsmoTransfer.isSupportedType(event.currentDataType)) { 
1116 
1117                     if (event.item == null) {
1118 
1119                         return;
1120 
1121                     }
1122 
1123                     Object selected = event.item.getData();
1124 
1125                     if (false == selected instanceof Identifier) {
1126 
1127                         return;
1128 
1129                     }
1130 
1131                     
1132 
1133                     List<Entity> instances = Clipboard.getInstance().getContent();
1134 
1135                     Set values = model.getInstance().listAttributeValues((Identifier)selected);
1136 
1137                     for(Entity entity : instances) {
1138 
1139                         if (entity instanceof Value) {
1140 
1141                             try {
1142 
1143                                 if (false == values.contains(entity)) {
1144 
1145                                     model.addAttributeValue((Identifier)selected, (Value)entity);
1146 
1147                                 }
1148 
1149                             }
1150 
1151                             catch(InvalidModelException ime) {
1152 
1153                                 MessageDialog.openError(getSite().getShell(), 
1154 
1155                                         "Error", 
1156 
1157                                         ime.getMessage());
1158 
1159                                 LogManager.logError(ime);
1160 
1161                             }
1162 
1163                         }
1164 
1165                     }
1166 
1167                 } 
1168 
1169             } 
1170 
1171         }); 
1172 
1173     }
1174 
1175 }
1176 
1177 
1178 
1179 class AttributeContentProvider implements ITreeContentProvider {
1180 
1181     
1182 
1183     private Instance targetInst;
1184 
1185     private boolean rootIsShown = false;
1186 
1187     
1188 
1189     public AttributeContentProvider(Instance targetInst) {
1190 
1191         this.targetInst = targetInst;
1192 
1193     }
1194 
1195 
1196 
1197     /*
1198 
1199      * this method is invoked just before the viewer is updated
1200 
1201      */
1202 
1203     public void prepareToRefresh() {
1204 
1205         rootIsShown = false;
1206 
1207     }
1208 
1209 
1210 
1211     public Object[] getChildren(Object parentElement) {
1212 
1213         if (parentElement instanceof Instance 
1214 
1215                 && parentElement == targetInst) {
1216 
1217             return targetInst.listAttributeValues().keySet().toArray();
1218 
1219         }
1220 
1221         if (parentElement instanceof Identifier) {
1222 
1223             return targetInst.listAttributeValues((Identifier)parentElement).toArray();
1224 
1225         }
1226 
1227         return null;
1228 
1229     }
1230 
1231 
1232 
1233     public Object getParent(Object element) {
1234 
1235         return null;
1236 
1237     }
1238 
1239 
1240 
1241     public boolean hasChildren(Object element) {
1242 
1243         if (element instanceof Instance 
1244 
1245                 && element == targetInst) {
1246 
1247             if (rootIsShown) {
1248 
1249                 return false; // cut recursion
1250 
1251             }
1252 
1253             return ((Instance)element).listAttributeValues().size() > 0;
1254 
1255         }
1256 
1257         if (element instanceof Identifier) {
1258 
1259             return targetInst.listAttributeValues(((Identifier)element)).size() > 0;
1260 
1261         }
1262 
1263         return false;
1264 
1265     }
1266 
1267 
1268 
1269     public Object[] getElements(Object inputElement) {
1270 
1271 
1272 
1273         if (inputElement instanceof Instance) {
1274 
1275             rootIsShown = true;
1276 
1277             return getChildren(inputElement);
1278 
1279         }
1280 
1281         return null;
1282 
1283     }
1284 
1285 
1286 
1287     public void dispose() {
1288 
1289     }
1290 
1291 
1292 
1293     public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
1294 
1295     }
1296 
1297 }
1298 
1299 
1300 
1301 /*
1302 
1303  * $Log: InstanceEditor.java,v $
1304  * Revision 1.24  2006/04/10 14:00:34  alex_simov
1305  * rfe[1465742]: Attribute chooser offers only relevant for the context attributes
1306  *
1307  * Revision 1.23  2006/02/20 12:44:49  alex_simov
1308  * wsmo4j api change: attributes handled by IRI instead of object refs
1309  *
1310  * Revision 1.22  2006/01/09 12:51:13  alex_simov
1311  * Copyright message in header updated
1312  *
1313  * Revision 1.21  2005/12/06 09:42:04  alex_simov
1314  * DataValue editor made aware of type restriction depending on usage
1315  *
1316  * Revision 1.20  2005/11/15 15:54:17  alex_simov
1317  * Drag-and-drop support added from WSMO Navigator to related (ontology) editors
1318  *
1319  * Revision 1.19  2005/11/09 16:17:39  alex_simov
1320  * UIModels notification improved
1321  *
1322  * Revision 1.18  2005/11/02 14:50:57  alex_simov
1323  * moving to MVC architecture cont'd
1324  *
1325  * Revision 1.17  2005/10/07 08:58:47  alex_simov
1326  * 1) bugfix (1314010) - recursive visualization in attribute values panel removed
1327  * 2) 'Add Attribute' split into: 'New Attr. Instance Val' and 'New Attr. DataVal'
1328  * 3) bugfix: 'dirty' notification problem fixed
1329  *
1330  * Revision 1.16  2005/09/30 12:13:53  alex_simov
1331  * unused variable 'item' assignment
1332  *
1333  * Revision 1.15  2005/09/21 15:09:11  alex_simov
1334  * minor fixes
1335  *
1336  * Revision 1.14  2005/09/20 14:40:23  alex_simov
1337  * small updates
1338  *
1339  * Revision 1.13  2005/09/10 09:17:21  alex_simov
1340  * bugfix
1341  *
1342  * Revision 1.12  2005/09/08 16:46:25  alex_simov
1343  * Migrating to Java 1.5
1344  *
1345  * Revision 1.11  2005/08/08 12:19:48  alex_simov
1346  * Utils.createTableItem() used where appropriate
1347  *
1348  * Revision 1.10  2005/08/02 10:33:20  alex_simov
1349  * minor code and/or javadoc fixes
1350  *
1351  * Revision 1.9  2005/07/29 15:08:02  alex_simov
1352  * added javadoc: class description, footer
1353  *
1354 
1355  *
1356 
1357  */
1358 
1359 
1360