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  /** Implementation of an ontology editor */
21  public class OntologyEditorImpl
22    extends AbstractVisualResource
23    implements OntologyEditor, CreoleListener, ObjectModificationListener
24  {
25    /**the size of the editor (x)  */
26    public final static int SIZE_X = 500;
27    /**the size of the editor (y)  */
28    public final static int SIZE_Y = 400;
29    /**the position of the editor (x)  */
30    public final static int POSITION_X = 300;
31    /**the position of the editor (y)  */
32    public final static int POSITION_Y = 200;
33  
34    /** the main panel */
35    private OEMainPanel panel = new OEMainPanel();
36  
37    /** flag indicating the mode of the editor. If with onto list then in an extended mode.*/
38    boolean withOntoList = true;
39  
40    /** the current ontology path */
41    private String path;
42  
43    /** same as ontoList */
44    private Object target;
45  
46    private Handle handle;
47  
48    /** The name of the ontology */
49    private String name;
50  
51    /** The ontology currently displayed */
52    private Taxonomy ontology;
53  
54    /** The list of ontologies */
55    private Vector ontoList;
56  
57    /** Ontology vs Tree map */
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    }//constructor
67  
68    /**Is called when an ontology has been selected from the ontology list
69     * @param o the selected ontology */
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     }//else : ontology is null
101 
102   }  // ontologySelected(Ontology)
103 
104 
105 /**Invokes an add sub class dialog in position x,y
106  * @param x the x coordinate of the dialog
107  * @param y the y coordinate of the dialog*/
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 } // addSubClass
120 
121 /**addSubClass given a ClassNode and the resulting info from the dialog
122  * @param root the node which is root to the sub class being added
123  * @param className the name from the dialog
124  * @param classComment the comment from the dialog */
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   } // if ontology
140   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     } // neither class neither ontology
159   } // else
160 
161 } // addSubClass
162 
163 
164 /**Removes the node/class
165  * @param node the node to be removed*/
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         /* the default behaviour is to make all sub nodes
190         top nodes.this could be optional on request */
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     } // if oclas
200 
201   } // else
202 
203 } // removeClass
204 
205 /**Renames a class
206  * @param c the class to be renamed
207  * @param n the class node associated with the class
208  * @param x coords
209  * @param y coords */
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 /** Visualizes the editor */
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 } // visualize()
246 
247 /**Creates a new ontology
248  * @param name the name of the ontology
249  * @param sourceURI
250  * @param theURL
251  * @param comment */
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     } // catch
266 
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 } // createOntology()
293 
294 /** Sets ontology to be loaded in the editor
295  * @param o the ontology to be loaded */
296 public void setOntology(Taxonomy o) {
297   ontology = o;
298   ontologySelected(o);
299 } // setOntology();
300 
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     } // else
339   } // else
340 
341 }// setTarget(Object)
342 
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 /**Get Modified Ontologies
370  * @return list of the modified ontologies */
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   } // for ontologies
378   return modified;
379 } // getModifiedOntologies()
380 
381 
382 /**Save a list of ontologies.
383  * @param list a list of ontologies to be saved*/
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       } // for list
390     } // not null list
391   } catch (Exception x) {
392     x.printStackTrace(Err.getPrintWriter());
393   }
394 
395 } // saveOntologies
396 
397 /**close list of ontologies
398  * @param list a list of ontologies to be saved*/
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     }// for ontologies
411 
412     for ( int i = 0 ; i < list.size() ; i++ ) {
413       Taxonomy o = (Taxonomy ) list.get(i);
414       /** set modified to false and handle saves explicitly */
415       o.setModified(false);
416       closeOntology(o,0,0);
417 
418     }
419 
420     fileSave(0,0,modified);
421   } // if not null list
422 
423 } // closeOntologies
424 
425 /*----------ontologies list popup menu item listeners------------*/
426 
427 /**save this ontology
428  * @param o the ontology to be saved */
429 public void saveOntology(Taxonomy o) throws ResourceInstantiationException {
430   o.store();
431 }
432 
433 /** invoke a saveas dialog for this ontology and save it
434  *  to the location specified
435  *  @param o the ontology to be saved
436  *  @param x the x coordinate of the save as dialog
437  *  @param y the y coordinate of the save as dialog*/
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     } // approve
450   } catch (Exception e) {
451     throw new ResourceInstantiationException(e);
452   }
453 } // saveAsOntology
454 
455 /**rename an ontology. if the x and y coordinates are not set,
456  * then the default position is LocationRelativeTo
457  * the ontologies list.
458  * @param o the ontology to be renamed
459  * @param x the x coordinate of the rename dialog
460  * @param y the y coordinate of the rename dialog*/
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 }// renameOntology
474 
475 /**delete an ontology. invoke  if the
476  * ontology has been changed. currently deleteOntology
477  * works as closeOntology. does not delete the file.
478  * even asks for saving it if modified.
479  * @param o the ontology to be deleted
480  * @param x x coordinate of the option pane to be invoked
481  * @param y y coordinate of the option pane to be invoked*/
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     } // cancel
501 
502     if ( JOptionPane.YES_OPTION == option ) {
503       o.store();
504     } // yes
505 
506     Factory.deleteResource(o);
507 
508     Gate.getCreoleRegister().resourceUnloaded(
509       new CreoleEvent(o,CreoleEvent.RESOURCE_UNLOADED));
510   } // if ontology is in the list
511 } // deleteOntology
512 
513 /** edit the URI of an ontology
514  * @param o the ontology to be edited
515  * @param x  coords of the dialog
516  * @param y  coords of the dialog */
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 } // editURI()
524 
525 /** edit the URI of an ontology class
526  * @param c class to be edited
527  * @param x  coords of the dialog
528  * @param y  coords of the dialog */
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 } // editClassURI()
536 
537 
538 
539 /**
540  * @return all the uris that are available in the editor
541  */
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 } // getAllURIs()
551 
552 /**retrieve a set of all the URIs in an ontology
553  * @param o the ontology
554  * @return set of all the URIs in the ontology
555  */
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 } // getAllURIs(Ontology)
565 
566 
567 /**close an ontology. invoke AreYouSureDialog if the
568  * ontology has been changed.
569  * @param o the ontology to be closed
570  * @param x x coordinate of the option pane to be invoked
571  * @param y y coordinate of the option pane to be invoked*/
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     } // cancel
593 
594     if ( JOptionPane.YES_OPTION == option ) {
595       o.store();
596     } // yes
597 
598     Factory.deleteResource(o);
599 
600     Gate.getCreoleRegister().resourceUnloaded(
601       new CreoleEvent(o,CreoleEvent.RESOURCE_UNLOADED));
602   } // if ontology is in the list
603 
604 } // closeOntology
605 
606 
607 /*End-------ontologies list popup menu item listeners------------*/
608 
609 /*------------- menu bar methods --------------*/
610 
611 /**checks for unsaved ontologies and disposes the main panel*/
612 public void fileExit() {
613   fileSave(0,0,this.getModifiedOntologies());
614 
615 } // fileExit()
616 
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     } // approve
648   } catch (IOException ioe) {
649     throw new ResourceInstantiationException(ioe);
650   }
651 } // fileOpen()
652 
653 /**
654  * invoke a mutiple selection save dialog with a list of ontologies.
655  * @param x  coords of the dialog
656  * @param y  coords of the dialog
657  * @param ontologies the list of ontologies to be optionally saved
658  */
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       //filter ontologies that are outside of the jar
667       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     } // if not zero
686     else { // check for one ontology
687       if(ontology != null) {
688         Vector theOne = new Vector(1);
689         theOne.add(ontology);
690         saveOntologies(theOne);
691       } // if
692     }
693   } else {
694     // without ontology list
695     if (ontology != null) {
696       if (ontology.isModified()) {
697         AskWannaSave(ontology,0,0);
698       }
699     } // o not null
700   } //else
701 
702 } //fileSave();
703 
704 /**
705  * invoke a mutiple selection close dialog with a list of ontologies.
706  * @param x  coords of the dialog
707  * @param y  coords of the dialog
708  * @param ontologies the list of ontologies to be optionally closed
709  */
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   } // if not zero
729 } // fileClose()
730 
731 /**inovke a 'new ontology dialog'
732  * @param x  coords of the dialog
733  * @param y  coords of the dialog */
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 } // fileNew()
740 
741 
742 /**Wanna Save Dialog invocation. currently the x and y parameters
743  * are not used since the option pane is by default initialized
744  * with position   setLocationRelativeTo(parentComponent)
745  * @param o the ontology to be saved or not
746  * @param x x coordinate of the WannaSaveDialog to be invoked
747  * @param y y coordinate of the WannaSaveDialog to be invoked
748  * @return the result of the option pane execution*/
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 } // AskWannaSave
762 
763 
764 /*---------implementation of CreoleListener interface--------------*/
765   /**Called when a new {@link gate.Resource} has been loaded into the system*/
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       } // if panel
776     } // if ontology
777   } // resource loaded
778 
779   /**Called when a {@link gate.Resource} has been removed from the system*/
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         } // yes
794       } 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         } // equals ontology
816 
817       } // if
818     } // if
819   } // resourceUnloaded()
820 
821   /**Called when a {@link gate.DataStore} has been opened*/
822   public void datastoreOpened(CreoleEvent e){
823   }
824 
825   /**Called when a {@link gate.DataStore} has been created*/
826   public void datastoreCreated(CreoleEvent e){
827   }
828 
829   /**Called when a {@link gate.DataStore} has been closed*/
830   public void datastoreClosed(CreoleEvent e){
831   }
832 
833   /**
834    * Called when the creole register has renamed a resource.1
835    */
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 /*---------implementation of CreoleListener interface--------------*/
850 
851 /*->->->---implementation of ObjectModificationListener interface--------------*/
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           /* synchronize the expansion of the old and new trees */
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           /* synchronize the expansion of the old and new trees */
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       } //without ontoList
908     }
909   }
910 
911 /*-<-<-<---implementation of ObjectModificationListener interface--------------*/
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 } // class OntologyEditorImpl