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  /** Implementation of an ontology editor */
22  public class OntologyEditorImpl
23    extends AbstractVisualResource
24    implements OntologyEditor, CreoleListener, ObjectModificationListener
25  {
26    /**the size of the editor (x)  */
27    public final static int SIZE_X = 500;
28    /**the size of the editor (y)  */
29    public final static int SIZE_Y = 400;
30    /**the position of the editor (x)  */
31    public final static int POSITION_X = 300;
32    /**the position of the editor (y)  */
33    public final static int POSITION_Y = 200;
34  
35    /** the main panel */
36    private OEMainPanel panel = new OEMainPanel();
37  
38    /** flag indicating the mode of the editor. If with onto list then in an extended mode.*/
39    boolean withOntoList = true;
40  
41    /** the current ontology path */
42    private String path;
43  
44    /** same as ontoList */
45    private Object target;
46  
47    private Handle handle;
48  
49    /** The name of the ontology */
50    private String name;
51  
52    /** The ontology currently displayed */
53    private Taxonomy ontology;
54  
55    /** The list of ontologies */
56    private Vector ontoList;
57  
58    /** Ontology vs Tree map */
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    }//constructor
68  
69    /**Is called when an ontology has been selected from the ontology list
70     * @param o the selected ontology */
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     }//else : ontology is null
102 
103   }  // ontologySelected(Ontology)
104 
105 
106 /**Invokes an add sub class dialog in position x,y
107  * @param x the x coordinate of the dialog
108  * @param y the y coordinate of the dialog*/
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 } // addSubClass
121 
122 /**addSubClass given a ClassNode and the resulting info from the dialog
123  * @param root the node which is root to the sub class being added
124  * @param className the name from the dialog
125  * @param classComment the comment from the dialog */
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   } // if ontology
141   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     } // neither class neither ontology
160   } // else
161 
162 } // addSubClass
163 
164 
165 /**Removes the node/class
166  * @param node the node to be removed*/
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         /* the default behaviour is to make all sub nodes
191         top nodes.this could be optional on request */
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     } // if oclas
201 
202   } // else
203 
204 } // removeClass
205 
206 /**Renames a class
207  * @param c the class to be renamed
208  * @param n the class node associated with the class
209  * @param x coords
210  * @param y coords */
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 /** Visualizes the editor */
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 } // visualize()
247 
248 /**Creates a new ontology
249  * @param name the name of the ontology
250  * @param sourceURI
251  * @param theURL
252  * @param comment */
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     } // catch
267 
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 } // createOntology()
294 
295 /** Sets ontology to be loaded in the editor
296  * @param o the ontology to be loaded */
297 public void setOntology(Taxonomy o) {
298   ontology = o;
299   ontologySelected(o);
300   if(ontology instanceof TaxonomyImpl){
301     ((TaxonomyImpl)ontology).addObjectModificationListener(this);
302   }
303 } // setOntology();
304 
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     } // else
343   } // else
344 
345 }// setTarget(Object)
346 
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 /**Get Modified Ontologies
374  * @return list of the modified ontologies */
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   } // for ontologies
382   return modified;
383 } // getModifiedOntologies()
384 
385 
386 /**Save a list of ontologies.
387  * @param list a list of ontologies to be saved*/
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       } // for list
394     } // not null list
395   } catch (Exception x) {
396     x.printStackTrace(Err.getPrintWriter());
397   }
398 
399 } // saveOntologies
400 
401 /**close list of ontologies
402  * @param list a list of ontologies to be saved*/
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     }// for ontologies
415 
416     for ( int i = 0 ; i < list.size() ; i++ ) {
417       Taxonomy o = (Taxonomy ) list.get(i);
418       /** set modified to false and handle saves explicitly */
419       o.setModified(false);
420       closeOntology(o,0,0);
421 
422     }
423 
424     fileSave(0,0,modified);
425   } // if not null list
426 
427 } // closeOntologies
428 
429 /*----------ontologies list popup menu item listeners------------*/
430 
431 /**save this ontology
432  * @param o the ontology to be saved */
433 public void saveOntology(Taxonomy o) throws ResourceInstantiationException {
434   o.store();
435 }
436 
437 /** invoke a saveas dialog for this ontology and save it
438  *  to the location specified
439  *  @param o the ontology to be saved
440  *  @param x the x coordinate of the save as dialog
441  *  @param y the y coordinate of the save as dialog*/
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     } // approve
454   } catch (Exception e) {
455     throw new ResourceInstantiationException(e);
456   }
457 } // saveAsOntology
458 
459 /**rename an ontology. if the x and y coordinates are not set,
460  * then the default position is LocationRelativeTo
461  * the ontologies list.
462  * @param o the ontology to be renamed
463  * @param x the x coordinate of the rename dialog
464  * @param y the y coordinate of the rename dialog*/
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 }// renameOntology
478 
479 /**delete an ontology. invoke  if the
480  * ontology has been changed. currently deleteOntology
481  * works as closeOntology. does not delete the file.
482  * even asks for saving it if modified.
483  * @param o the ontology to be deleted
484  * @param x x coordinate of the option pane to be invoked
485  * @param y y coordinate of the option pane to be invoked*/
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     } // cancel
505 
506     if ( JOptionPane.YES_OPTION == option ) {
507       o.store();
508     } // yes
509 
510     Factory.deleteResource(o);
511 
512     Gate.getCreoleRegister().resourceUnloaded(
513       new CreoleEvent(o,CreoleEvent.RESOURCE_UNLOADED));
514   } // if ontology is in the list
515 } // deleteOntology
516 
517 /** edit the URI of an ontology
518  * @param o the ontology to be edited
519  * @param x  coords of the dialog
520  * @param y  coords of the dialog */
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 } // editURI()
528 
529 /** edit the URI of an ontology class
530  * @param c class to be edited
531  * @param x  coords of the dialog
532  * @param y  coords of the dialog */
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 } // editClassURI()
540 
541 
542 
543 /**
544  * @return all the uris that are available in the editor
545  */
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 } // getAllURIs()
555 
556 /**retrieve a set of all the URIs in an ontology
557  * @param o the ontology
558  * @return set of all the URIs in the ontology
559  */
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 } // getAllURIs(Ontology)
569 
570 
571 /**close an ontology. invoke AreYouSureDialog if the
572  * ontology has been changed.
573  * @param o the ontology to be closed
574  * @param x x coordinate of the option pane to be invoked
575  * @param y y coordinate of the option pane to be invoked*/
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     } // cancel
597 
598     if ( JOptionPane.YES_OPTION == option ) {
599       o.store();
600     } // yes
601 
602     Factory.deleteResource(o);
603 
604     Gate.getCreoleRegister().resourceUnloaded(
605       new CreoleEvent(o,CreoleEvent.RESOURCE_UNLOADED));
606   } // if ontology is in the list
607 
608 } // closeOntology
609 
610 
611 /*End-------ontologies list popup menu item listeners------------*/
612 
613 /*------------- menu bar methods --------------*/
614 
615 /**checks for unsaved ontologies and disposes the main panel*/
616 public void fileExit() {
617   fileSave(0,0,this.getModifiedOntologies());
618 
619 } // fileExit()
620 
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     } // approve
652   } catch (IOException ioe) {
653     throw new ResourceInstantiationException(ioe);
654   }
655 } // fileOpen()
656 
657 /**
658  * invoke a mutiple selection save dialog with a list of ontologies.
659  * @param x  coords of the dialog
660  * @param y  coords of the dialog
661  * @param ontologies the list of ontologies to be optionally saved
662  */
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       //filter ontologies that are outside of the jar
671       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     } // if not zero
690     else { // check for one ontology
691       if(ontology != null) {
692         Vector theOne = new Vector(1);
693         theOne.add(ontology);
694         saveOntologies(theOne);
695       } // if
696     }
697   } else {
698     // without ontology list
699     if (ontology != null) {
700       if (ontology.isModified()) {
701         AskWannaSave(ontology,0,0);
702       }
703     } // o not null
704   } //else
705 
706 } //fileSave();
707 
708 /**
709  * invoke a mutiple selection close dialog with a list of ontologies.
710  * @param x  coords of the dialog
711  * @param y  coords of the dialog
712  * @param ontologies the list of ontologies to be optionally closed
713  */
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   } // if not zero
733 } // fileClose()
734 
735 /**inovke a 'new ontology dialog'
736  * @param x  coords of the dialog
737  * @param y  coords of the dialog */
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 } // fileNew()
744 
745 
746 /**Wanna Save Dialog invocation. currently the x and y parameters
747  * are not used since the option pane is by default initialized
748  * with position   setLocationRelativeTo(parentComponent)
749  * @param o the ontology to be saved or not
750  * @param x x coordinate of the WannaSaveDialog to be invoked
751  * @param y y coordinate of the WannaSaveDialog to be invoked
752  * @return the result of the option pane execution*/
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 } // AskWannaSave
766 
767 
768 /*---------implementation of CreoleListener interface--------------*/
769   /**Called when a new {@link gate.Resource} has been loaded into the system*/
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       } // if panel
780     } // if ontology
781   } // resource loaded
782 
783   /**Called when a {@link gate.Resource} has been removed from the system*/
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         } // yes
798       } 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         } // equals ontology
820 
821       } // if
822     } // if
823   } // resourceUnloaded()
824 
825   /**Called when a {@link gate.DataStore} has been opened*/
826   public void datastoreOpened(CreoleEvent e){
827   }
828 
829   /**Called when a {@link gate.DataStore} has been created*/
830   public void datastoreCreated(CreoleEvent e){
831   }
832 
833   /**Called when a {@link gate.DataStore} has been closed*/
834   public void datastoreClosed(CreoleEvent e){
835   }
836 
837   /**
838    * Called when the creole register has renamed a resource.1
839    */
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 /*---------implementation of CreoleListener interface--------------*/
854 
855 /*->->->---implementation of ObjectModificationListener interface--------------*/
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           /* synchronize the expansion of the old and new trees */
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           /* synchronize the expansion of the old and new trees */
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       } //without ontoList
912     }
913   }
914 
915 /*-<-<-<---implementation of ObjectModificationListener interface--------------*/
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 } // class OntologyEditorImpl