View Javadoc

1   /*
2   * The contents of this file are subject to the BT "ZEUS" Open Source 
3   * Licence (L77741), Version 1.0 (the "Licence"); you may not use this file 
4   * except in compliance with the Licence. You may obtain a copy of the Licence
5   * from $ZEUS_INSTALL/licence.html or alternatively from
6   * http://www.labs.bt.com/projects/agents/zeus/licence.htm
7   * 
8   * Except as stated in Clause 7 of the Licence, software distributed under the
9   * Licence is distributed WITHOUT WARRANTY OF ANY KIND, either express or 
10  * implied. See the Licence for the specific language governing rights and 
11  * limitations under the Licence.
12  * 
13  * The Original Code is within the package zeus.*.
14  * The Initial Developer of the Original Code is British Telecommunications
15  * public limited company, whose registered office is at 81 Newgate Street, 
16  * London, EC1A 7AJ, England. Portions created by British Telecommunications 
17  * public limited company are Copyright 1996-9. All Rights Reserved.
18  * 
19  * THIS NOTICE MUST BE INCLUDED ON ANY COPY OF THIS FILE
20  */
21  
22  
23  
24  /******************************************************************************
25  * OntologyEditor.java
26  *
27  * October 2000 - Updated by Jaron to import schemas via JDBC
28  *
29  * Main frame of the Ontology Editor
30  *****************************************************************************/
31  
32  package zeus.ontology;
33  
34  import java.awt.*;
35  import java.awt.event.*;
36  import javax.swing.*;
37  import javax.swing.event.*;
38  import java.io.*;
39  
40  import zeus.util.*;
41  import zeus.gui.*;
42  import zeus.concepts.*;
43  import zeus.ontology.attributes.*;
44  import zeus.ontology.database.*;
45  import zeus.ontology.facts.*;
46  import zeus.ontology.restrictions.*;
47  import zeus.generator.GeneratorModel;
48  
49  public class OntologyEditor 
50  {
51     static final String[] MESSAGE = {  /* sequence of (question,title) pairs */
52        /* 0 */ "Save current ontology?",
53        /* 1 */ "Ontology save needed",
54        /* 2 */ "File already exists.\nOverwrite saved file?",
55        /* 3 */ "Save Ontology as DAML",
56        /* 4 */ "Error",            // singleton
57        /* 5 */ "Warning",           // singleton
58        /* 6 */ "About ZEUS Ontology Editor",
59        /* 7 */ "About",
60        /* 8 */ "Using ontology: ",
61        /* 9 */ "No ontology loaded",
62        /* 10 */ "Clear current ontology?",
63        /* 11 */ "Not in the open source version of Zeus."
64     };
65  
66     static final String ONTOLOGY_FILE_EXT = "*.*";
67  
68     static final String FILE           = "File";
69     static final String NEW            = "New";
70     static final String LOAD           = "Load...";
71     static final String SAVE           = "Save...";
72     static final String SAVE_AS        = "Save as...";
73     static final String SAVE_AS_XML    = "Save as DAML...";
74     static final String EXIT           = "Exit";
75     static final String ABOUT          = "About";
76     static final String ABOUT_ZEUS     = "About ZEUS...";
77  
78     public static final String ONTOLOGY_EDITOR = "Ontology Editor";
79  
80     protected JFrame          frame = null;
81     protected JInternalFrame  iframe = null;
82     protected JTabbedPane     tabbedPane;
83     protected JLabel          infoLabel= null;
84     protected Component       root = null;
85     protected boolean         AS_XML = false;
86  
87     protected OntologyDb     ontologyDb = null;
88     protected GeneratorModel genmodel = null;
89  
90     protected JMenuItem miNew;
91     protected JMenuItem miOpen;
92     protected JMenuItem miSave;
93     protected JMenuItem miSaveAs;
94     protected JMenuItem miSaveAsXML;
95     protected JMenuItem miExit;
96     protected JMenuItem miAbout;
97  
98  
99     private static int NUMBER_DISPLAYED = 0;
100 
101    protected void initMenus() 
102    {
103      JMenuBar mainMenuBar = new JMenuBar();
104      JMenu menu1 = new JMenu(FILE);
105      miNew = new JMenuItem(NEW);
106      miOpen = new JMenuItem(LOAD);
107      miSave = new JMenuItem(SAVE);
108      miSaveAs = new JMenuItem(SAVE_AS);
109      miSaveAsXML = new JMenuItem(SAVE_AS_XML);
110      miExit = new JMenuItem(EXIT);
111      menu1.add(miNew);
112      menu1.add(miOpen);
113      menu1.add(miSave);
114      menu1.add(miSaveAs);
115      menu1.add(miSaveAsXML);
116      menu1.addSeparator();
117      menu1.add(miExit);
118      mainMenuBar.add(menu1);
119 
120      JMenu menu2 = new JMenu(ABOUT);
121      miAbout = new JMenuItem(ABOUT_ZEUS);
122      menu2.add(miAbout);
123 
124      if ( frame != null )
125         frame.setJMenuBar(mainMenuBar);
126      else if ( iframe != null )
127         iframe.setJMenuBar(mainMenuBar);
128    }
129 
130    /***
131     * Constructor invoked from the Agent Generator tool
132     */
133    public OntologyEditor(OntologyDb ontologyDb, Component root, GeneratorModel genmodel, JLabel infoLabel)
134    {
135      this.ontologyDb = ontologyDb;
136      this.genmodel = genmodel;
137      this.infoLabel = infoLabel;
138      this.root = root;
139      setTitle("");
140    }
141 
142    /***
143     * Constructor to build standalone ontology editor
144     */
145    public OntologyEditor(OntologyDb ontologyDb, boolean isFrame) 
146    {
147      this.ontologyDb = ontologyDb;
148      if ( isFrame )
149        createFrame();
150    }
151 
152 
153 //---------------------------------------------------------------------------
154    JTabbedPane getTabbedPane() {
155       JTabbedPane tabbedPane = new JTabbedPane();
156       Component treepanel = new FactTreePane(this,ontologyDb);
157       tabbedPane.addTab("Known Facts", null, treepanel,
158                          "Shows the hierarchy of known facts");
159 
160       Component rpanel = new RestrictionTableUI(ontologyDb);
161       tabbedPane.addTab("Restriction Definitions", null, rpanel,
162                          "Shows a table containing known restrictions");
163 
164       Component dbpanel = new DatabasePane(ontologyDb, genmodel);
165       tabbedPane.addTab("Database Import", null, dbpanel,
166                         "Allows facts to be imported from relational database schemas");
167 
168       tabbedPane.setSelectedIndex(0);
169       tabbedPane.setTabPlacement(JTabbedPane.BOTTOM);
170 
171       return tabbedPane;
172    }
173 
174    ImageIcon getIcon() {
175       String sep = System.getProperty("file.separator");
176       String gifpath = SystemProps.getProperty("gif.dir") + "ontology" + sep;
177       ImageIcon icon = new ImageIcon(gifpath + "cloudicon.gif");
178       return icon;
179    }
180 
181    void registerListeners() {
182       SymAction lSymAction = new SymAction();
183       miNew.addActionListener(lSymAction);
184       miOpen.addActionListener(lSymAction);
185       miSave.addActionListener(lSymAction);
186       miSaveAs.addActionListener(lSymAction);
187       miSaveAsXML.addActionListener(lSymAction);
188 
189       miExit.addActionListener(lSymAction);
190       miAbout.addActionListener(lSymAction);
191    }
192 
193    public JInternalFrame createInternalFrame() {
194       iframe = new JInternalFrame("Ontology Database",true,true,true,true);
195       iframe.setTitle("Ontology Database:" + (++NUMBER_DISPLAYED));
196       iframe.setFrameIcon(getIcon());
197 
198       iframe.getContentPane().setBackground(Color.gray);
199       iframe.getContentPane().setLayout(new BorderLayout());
200       iframe.getContentPane().add(getTabbedPane(),BorderLayout.CENTER);
201 
202       iframe.setBackground(Color.lightGray);
203       iframe.pack();
204       return iframe;
205    }
206 
207    public JFrame createFrame() 
208    {
209      frame = new JFrame();
210      frame.setIconImage(getIcon().getImage());
211 
212      frame.getContentPane().setBackground(Color.gray);
213      frame.getContentPane().setLayout(new BorderLayout());
214      frame.getContentPane().add(getTabbedPane(),BorderLayout.CENTER);
215 
216      initMenus();
217 
218      // REGISTER_LISTENERS
219      frame.addWindowListener(new SymWindow());
220      registerListeners();
221 
222      frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
223      frame.setBackground(Color.lightGray);
224      frame.setSize(540,540);
225      String title = ontologyDb.getFilename();
226      if ( title == null ) title = "";
227      setTitle(title);
228      frame.pack();
229      frame.setVisible(true);
230      return frame;
231    }
232 
233    protected void setTitle(String title) {
234      if ( frame != null ) {
235          if ( title.equals("") )
236             frame.setTitle(ONTOLOGY_EDITOR);
237          else
238             frame.setTitle(ONTOLOGY_EDITOR + ": " + title);
239      }
240      else if ( iframe != null ) {
241          if ( title.equals("") )
242             iframe.setTitle(ONTOLOGY_EDITOR);
243          else
244             iframe.setTitle(ONTOLOGY_EDITOR + ": " + title);
245      }
246      if ( infoLabel != null ) {
247          if ( title.equals("") )
248             infoLabel.setText(MESSAGE[9]);
249          else
250             infoLabel.setText(MESSAGE[8] + " " + Misc.relativePath(title));
251      }
252    }
253 
254    protected Component getRoot() {
255      return frame != null ? (Component)frame : (Component)root;
256    }
257 
258    public void showFrame() {
259      if ( frame == null )
260         createFrame();
261      else
262         frame.setVisible(true);
263    }
264 
265    class SymWindow extends WindowAdapter {
266       public void windowClosing(WindowEvent event) {
267          Exit();
268       }
269    }
270 
271    class SymAction implements ActionListener {
272       public void actionPerformed(ActionEvent event) {
273          Object object = event.getSource();
274          if (object == miNew)
275     	    Close();
276          else if (object == miOpen)
277     	    Open();
278          else if (object == miSave)
279     	    Save();
280          else if (object == miSaveAs)
281     	    SaveAs();
282          else if (object == miSaveAsXML)
283     	    SaveAsXML();
284          else if (object == miExit)
285     	    Exit();
286          else if (object == miAbout)
287     	    About();
288       }
289    }
290 
291    public boolean Close() {
292      if ( ontologyDb.isSaveNeeded() ) {
293         int answer = JOptionPane.showConfirmDialog(getRoot(),MESSAGE[0],
294                      MESSAGE[1],JOptionPane.YES_NO_CANCEL_OPTION);
295         if ( answer == JOptionPane.YES_OPTION && !Save() )
296            return false;
297         if ( answer == JOptionPane.CANCEL_OPTION )
298            return false;
299      }
300      return closeFile();
301    }
302 
303    public void Open() {
304       File f = getFile(FileDialog.LOAD);
305       String fname;
306 
307 /*
308       if (f != null) {
309          fname = f.getName();
310          if (fname.endsWith(".xml") || fname.endsWith(".dtd")) {
311             Xml2Zeus xml_zeus = new Xml2Zeus(f);
312 	    f = xml_zeus.getOntFile();
313          }
314       }
315 */
316       if ( f != null && Close() )
317          openFile(f);
318    }
319 
320    public boolean Save() {
321       if ( ontologyDb.getFilename() == null )
322          return SaveAs();
323       return saveFile(new File(ontologyDb.getFilename()));
324    }
325 
326    boolean SaveAs() {
327       File f = getFile(FileDialog.SAVE);
328       if ( f != null ) {
329          File f1 = null;
330          String filename = ontologyDb.getFilename();
331          if ( filename != null ) f1 = new File(filename);
332          if ( f.exists() && f1 != null && !f.equals(f1) ) {
333             int answer = JOptionPane.showConfirmDialog(getRoot(),MESSAGE[2],
334                          MESSAGE[3],JOptionPane.YES_NO_OPTION);
335             if ( answer == JOptionPane.NO_OPTION )
336                return false;
337         }
338         return saveFile(f);
339       }
340       if (AS_XML)
341         AS_XML = false;
342       return false;
343    }
344 
345    boolean SaveAsXML() {
346       JOptionPane.showMessageDialog(getRoot(),MESSAGE[11],
347                          MESSAGE[3],JOptionPane.OK_OPTION);
348       return false;
349    }
350 
351    void Exit() {
352      if ( root == null && ontologyDb.isSaveNeeded() ) {
353         int answer = JOptionPane.showConfirmDialog(getRoot(),MESSAGE[0],
354                      MESSAGE[1],JOptionPane.YES_NO_CANCEL_OPTION);
355         if ( answer == JOptionPane.YES_OPTION && !Save() )
356            return;
357         if ( answer == JOptionPane.CANCEL_OPTION )
358            return;
359      }
360      if ( root != null )
361         frame.setVisible(false);
362      else if ( frame != null ) {
363         frame.dispose();
364        // System.exit(0);
365      }
366      else if ( iframe != null ) {
367         frame.dispose();
368      }
369    }
370 
371    void About() {
372       JOptionPane.showMessageDialog(getRoot(),MESSAGE[6],MESSAGE[7],
373                                     JOptionPane.INFORMATION_MESSAGE);
374    }
375 
376    boolean saveFile( File f ) {
377      Assert.notNull(f);
378      int status;
379      Frame frame = (Frame)SwingUtilities.getRoot(getRoot());
380      Cursor lastCursor = frame.getCursor();
381      frame.setCursor(new Cursor(Cursor.WAIT_CURSOR));
382 
383      if(AS_XML)
384        status = ontologyDb.saveDAML(f);
385      else
386        status = ontologyDb.saveFile(f);
387 
388      frame.setCursor(lastCursor);
389      if ( (status & OntologyDb.ERROR_MASK) != 0 ) {
390         JOptionPane.showMessageDialog(getRoot(),ontologyDb.getError(),
391            MESSAGE[4],JOptionPane.ERROR_MESSAGE);
392         return false;
393      }
394      else if ( (status & OntologyDb.WARNING_MASK) != 0 ) {
395         JOptionPane.showMessageDialog(getRoot(),ontologyDb.getWarning(),
396            MESSAGE[5],JOptionPane.WARNING_MESSAGE);
397      }
398      if (!AS_XML)
399       setTitle(f.getName());
400      else
401         AS_XML = false;
402      return true;
403    }
404 
405 
406    boolean openFile(File f) {
407      Assert.notNull(f);
408      Frame frame = (Frame)SwingUtilities.getRoot(getRoot());
409      Cursor lastCursor = frame.getCursor();
410      frame.setCursor(new Cursor(Cursor.WAIT_CURSOR));
411      int status = ontologyDb.openFile(f);
412      frame.setCursor(lastCursor);
413      if ( (status & OntologyDb.ERROR_MASK) != 0 ) {
414         JOptionPane.showMessageDialog(getRoot(),ontologyDb.getError(),
415            MESSAGE[4],JOptionPane.ERROR_MESSAGE);
416        closeFile();
417        return false;
418      }
419      else if ( (status & OntologyDb.WARNING_MASK) != 0 ) {
420        JOptionPane.showMessageDialog(getRoot(),ontologyDb.getWarning(),
421           MESSAGE[5],JOptionPane.WARNING_MESSAGE);
422      }
423      setTitle(f.getName());
424      return true;
425    }
426 
427    public boolean closeFile() {
428      ontologyDb.clear();
429      setTitle("");
430      return true;
431    }
432 
433 
434    protected File getFile(int type) {
435      FileDialog f = new FileDialog((Frame)SwingUtilities.getRoot(getRoot()),
436                                    "Select File", type);
437 
438      File f1 = null;
439      String filename = ontologyDb.getFilename();
440      if ( filename != null ) f1 = new File(filename);
441      if ( f1 != null ) {
442         f.setFile(f1.getName());
443         f.setDirectory(f1.getParent());
444      }
445      else
446         f.setFile(ONTOLOGY_FILE_EXT);
447 
448      f.pack();
449      f.setVisible(true);
450 
451      return f.getFile() == null ? null : new File(f.getDirectory(),f.getFile());
452    }
453 
454    protected static void version() {
455      System.err.println("OntologyEditor version: " +
456                         SystemProps.getProperty("version.id"));
457     // System.exit(0);
458    }
459 
460    protected static void usage() {
461      System.err.println("Usage: java OntologyEditor [-f <file>] [-h] [-v]");
462      System.exit(0);
463    }
464 
465    public static void main( String[] arg ) {
466      String file = null;
467 
468      for( int i = 0; i < arg.length; i++ ) {
469        if ( arg[i].equals("-f") && ++i < arg.length )
470          file = arg[i];
471        else if ( arg[i].equals("-h") )
472          usage();
473        else if ( arg[i].equals("-v") )
474          version();
475        else
476          usage();
477      }
478 
479      OntologyEditor z;
480      z = new OntologyEditor(new OntologyDb(new GenSym("name")),true);
481 
482      if ( file != null ) {
483         String dir = System.getProperty("user.dir") +
484                      System.getProperty("file.separator");
485         z.openFile( new File(dir + file) );
486      }
487    }
488 }