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