1 package com.ontotext.gate.vr;
2
3 import gate.gui.*;
4 import gate.creole.ontology.*;
5 import gate.creole.*;
6 import gate.event.*;
7 import gate.*;
8 import gate.util.*;
9
10 import com.ontotext.gate.vr.dialog.*;
11 import com.ontotext.gate.ontology.*;
12
13 import java.util.*;
14 import java.io.*;
15 import java.net.*;
16 import javax.swing.*;
17 import javax.swing.tree.*;
18 import java.awt.Component;
19
20
21 public class OntologyEditorImpl
22 extends AbstractVisualResource
23 implements OntologyEditor, CreoleListener, ObjectModificationListener
24 {
25
26 public final static int SIZE_X = 500;
27
28 public final static int SIZE_Y = 400;
29
30 public final static int POSITION_X = 300;
31
32 public final static int POSITION_Y = 200;
33
34
35 private OEMainPanel panel = new OEMainPanel();
36
37
38 boolean withOntoList = true;
39
40
41 private String path;
42
43
44 private Object target;
45
46 private Handle handle;
47
48
49 private String name;
50
51
52 private Taxonomy ontology;
53
54
55 private Vector ontoList;
56
57
58 private Map OvsT = new HashMap();
59
60 public OntologyEditorImpl() {
61 panel.setOntologyEditor(this);
62 this.setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
63 this.add(panel);
64 gate.Gate.addCreoleListener(OntologyEditorImpl.this);
65 OntologyImpl.addObjectModificationListener(this);
66 }
68
70 public void ontologySelected(Taxonomy o) {
71 if ( null != o ) {
72 ontology = o;
73
74 if (null == o.getURL()) {
75 throw new GateRuntimeException("null URL for ontology "+o.getName());
76 }
77
78 boolean saveEnabled = (-1 == o.getURL().getProtocol().indexOf("jar"));
79 panel.fileSave.setEnabled(saveEnabled);
80 panel.saveItem.setEnabled(saveEnabled);
81
82 String curPath = o.getURL().getPath();
83 int index = curPath.lastIndexOf("/");
84 if (curPath.length()-1==index)
85 curPath = curPath.substring(0,index);
86 if ( -1 != index ) {
87 path = curPath.substring(0,index);
88 }
89 panel.oTree = (JTree)OvsT.get(o);
90 if ( null == panel.oTree ) {
91 panel.buildOntoTree(o);
92 OvsT.put(o,panel.oTree);
93 } else {
94 panel.setOntoTree(panel.oTree);
95 }
96 panel.oTree.setVisible(true);
97
98 } else {
99 panel.oTree.setVisible(false);
100 }
102 }
104
105
108 public void addSubClass(int x, int y) {
109 AddSubClassDialog dialog = new AddSubClassDialog();
110 ClassNode node = (ClassNode)panel.oTree.getLastSelectedPathComponent();
111 dialog.setTitle("add a sub class of "+node.toString());
112 dialog.setInvokers(this,node);
113 if ( 0 == x && 0 == y ) {
114 dialog.setLocationRelativeTo(panel.oTree);
115 } else {
116 dialog.setLocation(x,y);
117 }
118 dialog.setVisible(true);
119 }
121
125 public void addSubClass(ClassNode root, String className, String classComment) {
126 Object o = root.getSource();
127 if ( o instanceof Taxonomy) {
128 Taxonomy onto = (Taxonomy) o;
129 TClass clas = onto.createClass(className,classComment);
130 clas.setURI(onto.getSourceURI().substring(0,
131 onto.getSourceURI().lastIndexOf("#")+1)+className);
132 ClassNode subNode = new ClassNode(clas);
133 Vector kids = root.children();
134 kids.add(subNode);
135 root.setChildren(kids);
136
137 panel.oTree.updateUI();
138
139 } else {
141 if ( o instanceof TClass) {
142 TClass clas = (TClass) o;
143 TClass subClass = clas.getOntology().createClass(className,classComment);
144 subClass.setURI(clas.getURI().substring(0,clas.getURI().lastIndexOf("#")+1)
145 + className);
146 ClassNode subNode = new ClassNode(subClass);
147
148 Vector rChildren = root.children();
149 rChildren.add(subNode);
150 root.setChildren(rChildren);
151
152 clas.addSubClass(subClass);
153
154 panel.oTree.updateUI();
155 } else {
156 throw new GateRuntimeException(
157 "class node's source is neither TClass, neither Ontology");
158 } }
161 }
163
164
166 public void removeClass(ClassNode node) {
167 Object source = node.getSource();
168
169 if (source instanceof Taxonomy) {
170
171 } else {
172
173 if (source instanceof TClass) {
174
175 TClass clas = (TClass) source;
176 clas.getOntology().removeClass(clas);
177
178 if ( panel.oTree.getAnchorSelectionPath() != null ) {
179 TreePath path = panel.oTree.getAnchorSelectionPath().getParentPath();
180 if (null == path)
181 throw new GateRuntimeException("selection path is null (on removing class)");
182
183 ClassNode parentNode = (ClassNode)path.getLastPathComponent();
184
185 Vector kids = parentNode.children();
186 kids.remove(node);
187 parentNode.setChildren(kids);
188
189
191 kids = node.children();
192 ClassNode rootNode =
193 (ClassNode)panel.oTree.getPathForRow(0).getLastPathComponent();
194 kids.addAll(rootNode.children());
195 rootNode.setChildren(kids);
196 }
197
198 panel.oTree.updateUI();
199 }
201 }
203 }
205
210 public void renameClass(TClass c,ClassNode n, int x, int y) {
211 if ( null == c )
212 throw new GateRuntimeException(
213 "ontology class parameter is null while renaming ");
214 if ( null == n )
215 throw new GateRuntimeException(
216 "class node parameter is null while renaming ");
217
218 RenameClassDialog dialog = new RenameClassDialog(this,panel,n,c);
219
220 dialog.nameField.setText(c.getName());
221 dialog.commentField.setText(c.getComment());
222
223 if ( 0 == x && 0 == y) {
224 dialog.setLocationRelativeTo(panel.oList);
225 } else {
226 dialog.setLocation(x,y);
227 }
228
229 dialog.setTitle("Rename Class "+c);
230 dialog.setVisible(true);
231 }
232
233
234
235
236 public void visualize() {
237 ontologySelected(ontology);
238
239 setOntologyList( new Vector (
240 Gate.getCreoleRegister().getLrInstances(
241 "com.ontotext.gate.ontology.DAMLOntology")));
242
243 panel.setVisible(true);
244
245 }
247
252 public void createOntology (
253 String name, String sourceURI, String theURL, String comment)
254 throws ResourceInstantiationException {
255 try {
256 Taxonomy o = new DAMLOntology();
257 o.setComment(comment);
258
259 URL localurl=null;
260 try {
261 localurl = new URL(theURL);
262 o.setURL(localurl);
263 } catch (MalformedURLException urle) {
264 throw new ResourceInstantiationException(urle);
265 }
267 o.setName(name);
268 o.setSourceURI(sourceURI);
269
270 try {
271 Main.getMainFrame().resourceLoaded(
272 new gate.event.CreoleEvent(o,gate.event.CreoleEvent.RESOURCE_LOADED));
273 } catch (gate.util.GateException ge) {
274 throw new GeneralEditorException(
275 "\ncannot create new ontology because of:"+
276 "\ngate.util.GateException:\n"+
277 ge.getMessage()+"\n");
278 }
279
280 ontoList.add(o);
281 setOntologyList(ontoList);
282 setOntology(o);
283
284 panel.oList.setSelectedIndex(ontoList.size()-1);
285
286 panel.oList.updateUI();
287 panel.oTree.updateUI();
288 }catch (Exception x) {
289 if (!(x instanceof ResourceInstantiationException))
290 throw new ResourceInstantiationException(x);
291 }
292 }
294
296 public void setOntology(Taxonomy o) {
297 ontology = o;
298 ontologySelected(o);
299 }
301 public Taxonomy getOntology() {
302 return ontology;
303 }
304
305 public void setOntologyList(Vector list) {
306 ontoList = list;
307 target = list;
308 panel.setOntologyList(list);
309 }
310
311 public Vector getOntologyList() {
312 return ontoList;
313 }
314
315 public void setTarget(Object target) {
316
317 this.target = target;
318
319 if ( target instanceof Vector ) {
320 ontoList = (Vector)target;
321 } else {
322 if (target instanceof Taxonomy) {
323 ontology = (Taxonomy) target;
324
325 Vector olist = new Vector(Gate.getCreoleRegister().getLrInstances("com.ontotext.gate.ontology.DAMLOntology"));
326 setOntologyList( olist );
327
328 setOntology(ontology);
329 panel.setVisible(true);
330 panel.listPanel.setVisible(false);
331 withOntoList = false;
332 panel.withOntoList=false;
333 panel.fileNew.setEnabled(false);
334 panel.fileOpen.setEnabled(false);
335 } else {
336 throw new GateRuntimeException("setTarget should be called with a \n"+
337 "java.util.Vector or gate.creole.ontology.Ontology");
338 } }
341 }
343 public void setHandle(Handle handle) {
344 this.handle = handle;
345 }
346
347 public Resource init() throws ResourceInstantiationException {
348 panel.setOntologyEditor(this);
349 return this;
350 }
351
352
353 public void cleanup() {
354 handle = null;
355 panel = null;
356 target = null;
357 ontoList = null;
358 ontology = null;
359 }
360
361 public void setName(String name) {
362 this.name = name;
363 }
364
365 public String getName() {
366 return name;
367 }
368
369
371 public Vector getModifiedOntologies() {
372 Vector modified = new Vector();
373 for ( int i = 0 ; i<ontoList.size(); i++) {
374 Taxonomy o = (Taxonomy)ontoList.get(i);
375 if (o.isModified())
376 modified.add(o);
377 } return modified;
379 }
381
382
384 public void saveOntologies(Vector list) {
385 try {
386 if (null != list) {
387 for ( int i = 0 ; i < list.size() ; i++) {
388 this.saveOntology((Taxonomy) list.get(i));
389 } } } catch (Exception x) {
392 x.printStackTrace(Err.getPrintWriter());
393 }
394
395 }
397
399 public void closeOntologies(Vector list)throws ResourceInstantiationException{
400 if ( null != list ) {
401 Vector modified = new Vector();
402 Vector unmodified = new Vector();
403 for ( int i = 0 ; i < list.size(); i++) {
404 Taxonomy o = (Taxonomy)list.get(i);
405 if ( o.isModified()) {
406 modified.add(o);
407 } else {
408 unmodified.add(o);
409 }
410 }
412 for ( int i = 0 ; i < list.size() ; i++ ) {
413 Taxonomy o = (Taxonomy ) list.get(i);
414
415 o.setModified(false);
416 closeOntology(o,0,0);
417
418 }
419
420 fileSave(0,0,modified);
421 }
423 }
425
426
427
429 public void saveOntology(Taxonomy o) throws ResourceInstantiationException {
430 o.store();
431 }
432
433
438 public void saveAsOntology(Taxonomy o, int x, int y) throws ResourceInstantiationException {
439 try {
440 JFileChooser chooser = MainFrame.getFileChooser();
441 chooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
442
443 int result = chooser.showSaveDialog(panel.oList);
444 if ( result == JFileChooser.APPROVE_OPTION ) {
445 File selected = chooser.getSelectedFile();
446 URL url = new URL("file:///"+selected.getAbsolutePath());
447 o.setURL(url);
448 o.store();
449 } } catch (Exception e) {
451 throw new ResourceInstantiationException(e);
452 }
453 }
455
461 public void renameOntology(Taxonomy o, int x, int y) {
462 if ( null == o )
463 throw new GateRuntimeException(" ontology parameter is null while renaming ");
464 RenameOntologyDialog dialog = new RenameOntologyDialog(this,o);
465
466 dialog.nameField.setText(o.getName());
467 dialog.commentField.setText(o.getComment());
468
469 dialog.setLocationRelativeTo(panel.oList);
470
471 dialog.setTitle("Rename Ontology "+o);
472 dialog.setVisible(true);
473 }
475
482 public void deleteOntology(Taxonomy o, int x, int y)
483 throws ResourceInstantiationException {
484 int index = ontoList.indexOf(o);
485 if ( -1 != index ) {
486 int option = JOptionPane.NO_OPTION;
487
488 if (o.isModified()) {
489 option = AskWannaSave(o,x,y);
490 }
491
492 if ( JOptionPane.CANCEL_OPTION != option ) {
493 ontoList.remove(o);
494 OvsT.remove(o);
495 if (index > 0 ) {
496 index--;
497 }
498 panel.setOntologyList(ontoList);
499 panel.oTree.setVisible(false);
500 }
502 if ( JOptionPane.YES_OPTION == option ) {
503 o.store();
504 }
506 Factory.deleteResource(o);
507
508 Gate.getCreoleRegister().resourceUnloaded(
509 new CreoleEvent(o,CreoleEvent.RESOURCE_UNLOADED));
510 } }
513
517 public void editURI(Taxonomy o, int x, int y) {
518 EditURIDialog dialog = new EditURIDialog(this,o);
519 dialog.setLocationRelativeTo(panel.oList);
520
521 dialog.setTitle("Edit URI of Ontology : "+o);
522 dialog.setVisible(true);
523 }
525
529 public void editClassURI(TClass c, int x, int y){
530 EditClassURIDialog dialog = new EditClassURIDialog(this,c);
531 dialog.setLocationRelativeTo(panel.oTree);
532
533 dialog.setTitle("Edit URI of class : "+c);
534 dialog.setVisible(true);
535 }
537
538
539
542 public Set getAllURIs() {
543 Set result = new HashSet();
544 for ( int i = 0 ; i < ontoList.size(); i++ ) {
545 String u = ((Taxonomy)ontoList.get(i)).getSourceURI();
546
547 result.add(u);
548 }
549 return result;
550 }
552
556 public Set getAllURIs(Taxonomy o) {
557 Set result = new HashSet();
558 Iterator ci = o.getClasses().iterator();
559 while(ci.hasNext()) {
560 TClass c = (TClass) ci.next();
561 result.add(c.getURI());
562 }
563 return result;
564 }
566
567
572 public void closeOntology(Taxonomy o, int x, int y)
573 throws ResourceInstantiationException{
574 int index = ontoList.indexOf(o);
575 if ( -1 != index ) {
576
577 int option = JOptionPane.NO_OPTION;
578
579 if (o.isModified()) {
580 option = AskWannaSave(o,x,y);
581 }
582
583 if ( JOptionPane.CANCEL_OPTION != option ) {
584 ontoList.remove(o);
585 OvsT.remove(o);
586 if (index > 0 ) {
587 index--;
588 }
589
590 panel.setOntologyList(ontoList);
591 panel.oTree.setVisible(false);
592 }
594 if ( JOptionPane.YES_OPTION == option ) {
595 o.store();
596 }
598 Factory.deleteResource(o);
599
600 Gate.getCreoleRegister().resourceUnloaded(
601 new CreoleEvent(o,CreoleEvent.RESOURCE_UNLOADED));
602 }
604 }
606
607
608
609
610
611
612 public void fileExit() {
613 fileSave(0,0,this.getModifiedOntologies());
614
615 }
617
618 public void fileOpen(int x,int y) throws ResourceInstantiationException {
619 try {
620 JFileChooser chooser = MainFrame.getFileChooser();
621 chooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
622 int result = chooser.showOpenDialog(panel.oList);
623 if ( result == JFileChooser.APPROVE_OPTION ) {
624 File selected = chooser.getSelectedFile();
625 URL url = new URL("file:///"+selected.getAbsolutePath());
626
627 FeatureMap fm = Factory.newFeatureMap();
628 fm.put("URL",url);
629 Taxonomy damlo;
630
631 damlo = (Taxonomy)Factory.createResource(
632 "com.ontotext.gate.ontology.DAMLOntology",
633 fm
634 );
635
636 if ( this.ontoList.contains(damlo) ) {
637 JOptionPane.showMessageDialog(panel,
638 "The newly loaded ontology "+damlo+
639 "\nis already in the editor's list of ontologies.\n"+
640 ontology.getURL(),
641 "Ontology Load",JOptionPane.WARNING_MESSAGE);
642 }
643
644 Vector ol = this.getOntologyList();
645 ol.add(damlo);
646 this.setOntologyList(ol);
647 } } catch (IOException ioe) {
649 throw new ResourceInstantiationException(ioe);
650 }
651 }
653
659 public void fileSave(int x, int y, Vector ontologies) {
660 if ( null == ontologies)
661 throw new GateRuntimeException("null ontologies parameter");
662
663 if (withOntoList) {
664 if ( 0 != ontologies.size() ) {
665 Vector ontolos = new Vector();
666 for ( int i = 0; i < ontologies.size() ; i++) {
668 Taxonomy o = (Taxonomy)ontologies.get(i);
669 if (-1 == o.getURL().getProtocol().indexOf("jar"))
670 ontolos.add(o);
671 }
672 MultipleSelectionDialog dialog = new MultipleSelectionDialog(
673 this,
674 ontolos,
675 "Please select the ontologies to be saved.",
676 "Multiple Selection Save Dialog"
677 );
678 if ( 0 == x && 0 == y ) {
679 dialog.setLocationRelativeTo(panel);
680 } else {
681 dialog.setLocation(x,y);
682 }
683 dialog.okBtn.addActionListener(new SaveOKListener(dialog));
684 dialog.setVisible(true);
685 } else { if(ontology != null) {
688 Vector theOne = new Vector(1);
689 theOne.add(ontology);
690 saveOntologies(theOne);
691 } }
693 } else {
694 if (ontology != null) {
696 if (ontology.isModified()) {
697 AskWannaSave(ontology,0,0);
698 }
699 } }
702 }
704
710 public void fileClose(int x, int y,Vector ontologies) {
711 if ( null == ontologies)
712 throw new GateRuntimeException("null ontologies parameter");
713
714 if ( 0 != ontologies.size() ) {
715 MultipleSelectionDialog dialog = new MultipleSelectionDialog(
716 this,
717 ontologies,
718 "Please select the ontologies to be closed.",
719 "Multiple Selection Close Dialog"
720 );
721 if ( 0 == x && 0 == y ) {
722 dialog.setLocationRelativeTo(panel);
723 } else {
724 dialog.setLocation(x,y);
725 }
726 dialog.okBtn.addActionListener(new CloseOKListener(dialog));
727 dialog.setVisible(true);
728 } }
731
734 public void fileNew(int x, int y) {
735 NewOntologyDialog dialog = new NewOntologyDialog(this);
736 dialog.setLocationRelativeTo(panel);
737 dialog.setTitle("New Ontology Dialog");
738 dialog.setVisible(true);
739 }
741
742
749 public int AskWannaSave(Taxonomy o, int x, int y) {
750 JOptionPane opane = new JOptionPane();
751
752 int option = opane.showConfirmDialog(panel,"The ontology "+o+
753 " has been modified\n"+
754 "Save the ontology?"
755 ,"Wanna Save Option Pane"
756 ,JOptionPane.YES_NO_CANCEL_OPTION
757 ,JOptionPane.QUESTION_MESSAGE
758 );
759
760 return option;
761 }
763
764
765
766 public void resourceLoaded(CreoleEvent e) {
767 Resource r;
768
769 if ( (r = e.getResource() )instanceof Ontology ) {
770 if (null != panel ) {
771
772 Vector olist = new Vector(Gate.getCreoleRegister().getLrInstances(
773 "com.ontotext.gate.ontology.DAMLOntology"));
774 setOntologyList(olist);
775 } } }
779
780 public void resourceUnloaded(CreoleEvent e) {
781 Resource r;
782 if ( (r = e.getResource() )instanceof Taxonomy ) {
783 try {
784 Taxonomy o = (Taxonomy)r;
785 int option = JOptionPane.NO_OPTION;
786
787 if (o.isModified()) {
788 option = AskWannaSave(o,0,0);
789 }
790
791 if ( JOptionPane.YES_OPTION == option ) {
792 o.store();
793 } } catch (ResourceInstantiationException ex) {
795 JOptionPane.showMessageDialog(panel,
796 "Close ontology failed.\n"+
797
798 ((Taxonomy)r).getURL()+"\n"+
799 "Due to :"+ex.getClass()+":\nMessage:"+ex.getMessage(),
800 "Ontology Close Failure",JOptionPane.ERROR_MESSAGE);
801 }
802
803 if (null != panel && null != panel.oTree) {
804 Vector olist = new Vector(Gate.getCreoleRegister().getLrInstances(
805 "com.ontotext.gate.ontology.DAMLOntology"));
806 olist.remove(r);
807 setOntologyList(olist);
808
809 if (null != ontology && ontology.equals(r)) {
810 if ( olist.size() > 0 ) {
811 setOntology((Taxonomy)olist.get(0));
812 } else {
813 setOntology(null);
814 }
815 }
817 } } }
821
822 public void datastoreOpened(CreoleEvent e){
823 }
824
825
826 public void datastoreCreated(CreoleEvent e){
827 }
828
829
830 public void datastoreClosed(CreoleEvent e){
831 }
832
833
836 public void resourceRenamed(Resource resource, String oldName,
837 String newName){
838 if ( resource instanceof Taxonomy ) {
839 if (ontology.equals(resource)) {
840 ontology.setName(newName);
841 Object daRoot = panel.oTree.getModel().getRoot();
842 if (daRoot instanceof ClassNode) {
843 ((ClassNode)daRoot).rename(newName);
844 }
845 }
846 }
847
848 }
849
850
851
852 public void processGateEvent(GateEvent e) {
853 }
854
855 public void objectCreated(ObjectModificationEvent e) {
856 }
857
858 public void objectDeleted(ObjectModificationEvent e) {
859 }
860
861 public void objectModified(ObjectModificationEvent e) {
862 Object source = e.getSource();
863 EditableTreeView view = null;
864 if ( source instanceof Taxonomy ) {
865 if (withOntoList) {
866 JTree tree = (JTree)OvsT.get((Taxonomy)source);
867 if ( null!= tree ) {
868 OvsT.remove(source);
869 boolean includeInstances = source instanceof Ontology;
870 ClassNode root = ClassNode.createRootNode((Taxonomy)source,
871 includeInstances);
872 OntoTreeModel model = new OntoTreeModel(root);
873 view = new EditableTreeView(model);
874
875 KnowledgeBaseTreeCellRenderer kbTreeCellRenderer =
876 new KnowledgeBaseTreeCellRenderer();
877 view.setCellRenderer(kbTreeCellRenderer);
878
879
880 EditableTreeView.synchronizeTreeExpansion(tree,view);
881
882 OvsT.put((Taxonomy)source,view);
883 if (ontology.equals((Taxonomy)source)) {
884 view.setMainPanel(panel);
885 panel.setOntoTree(view);
886 }
887 }
888 } else {
889 if (ontology != null && ontology.equals((Taxonomy)source)) {
890 boolean includeInstances = source instanceof Ontology;
891 ClassNode root = ClassNode.createRootNode((Taxonomy)source,
892 includeInstances);
893 OntoTreeModel model = new OntoTreeModel(root);
894 view = new EditableTreeView(model);
895 KnowledgeBaseTreeCellRenderer kbTreeCellRenderer =
896 new KnowledgeBaseTreeCellRenderer();
897 view.setCellRenderer(kbTreeCellRenderer);
898
899
900 if (panel.oTree != null )
901 EditableTreeView.synchronizeTreeExpansion(panel.oTree,view);
902
903 OvsT.put((Taxonomy)source,view);
904 view.setMainPanel(panel);
905 panel.setOntoTree(view);
906 }
907 } }
909 }
910
911
912
913 protected class KnowledgeBaseTreeCellRenderer extends DefaultTreeCellRenderer {
914 public KnowledgeBaseTreeCellRenderer() {
915 }
916 public Component getTreeCellRendererComponent(JTree tree,
917 Object value,
918 boolean sel,
919 boolean expanded,
920 boolean leaf,
921 int row,
922 boolean hasFocus){
923 super.getTreeCellRendererComponent(tree, value, sel, expanded,
924 leaf, row, hasFocus);
925 if (! (value instanceof ClassNode))
926 return this;
927 ClassNode theNode = (ClassNode) value;
928 if(theNode.getSource() instanceof TClass) {
929 setIcon(MainFrame.getIcon("Class.gif"));
930 } else if(theNode.getSource() instanceof OInstance) {
931 setIcon(MainFrame.getIcon("Instance.gif"));
932 }
933 return this;
934 }
935 }
936
937
938 }