1   package com.ontotext.gate.vr;
2   
3   import javax.swing.*;
4   import javax.swing.event.*;
5   
6   import java.awt.*;
7   import java.awt.event.*;
8   
9   import java.util.*;
10  import java.net.*;
11  import java.io.*;
12  
13  import gate.creole.ontology.*;
14  import gate.creole.ResourceInstantiationException;
15  import gate.util.Err;
16  import gate.gui.MainFrame;
17  import javax.swing.tree.DefaultTreeCellRenderer;
18  
19  import gate.util.Out;
20  
21  /** Main frame class of the ontology editor */
22  public class OEMainPanel extends JPanel  {
23  
24    /** flag indicating whether the ontology editor is in extended [ with onto list ] or
25     *  simple/linear mode.
26     */
27    boolean withOntoList = true;
28  
29    protected JSplitPane splitPane = new JSplitPane();
30    protected JPanel oPanel = new JPanel();
31    protected BorderLayout borderLayout2 = new BorderLayout();
32    protected JLabel oLabel = new JLabel();
33    public    JScrollPane oScrollPane = new JScrollPane();
34    public    JTree oTree = new JTree();
35    protected JPanel listPanel = new JPanel();
36    protected BorderLayout borderLayout1 = new BorderLayout();
37    protected BorderLayout borderLayout3 = new BorderLayout();
38    protected JLabel jLabel1 = new JLabel();
39    protected JList oList = new JList();
40    protected JMenuBar menuBar = new JMenuBar();
41    protected JMenu fileMenu = new JMenu();
42    protected JMenuItem fileOpen = new JMenuItem();
43    protected JMenuItem fileSave = new JMenuItem();
44    protected JMenuItem fileExit = new JMenuItem();
45    /** a popup menu over the ontologies list */
46    protected JPopupMenu listPopup = new JPopupMenu();
47    protected JMenuItem saveItem;
48    protected JMenuItem saveAsItem;
49    protected JMenuItem renameItem;
50    protected JMenuItem deleteItem;
51    protected JMenuItem editURIItem;
52    protected JMenuItem closeItem;
53  
54    /**reference to the ontology editor agregating this main frame*/
55    private OntologyEditor editor;
56    protected JMenu View = new JMenu();
57    protected JMenuItem viewRefresh = new JMenuItem();
58    protected JMenu helpMenu = new JMenu();
59    protected JMenuItem helpHelp = new JMenuItem();
60    protected JMenuItem helpAbout = new JMenuItem();
61    protected JMenuItem fileClose = new JMenuItem();
62    protected JMenuItem fileNew = new JMenuItem();
63    protected JMenuItem fileSaveAs = new JMenuItem();
64  
65    public OEMainPanel() {
66      try {
67        jbInit();
68      }
69      catch(Exception e) {
70        e.printStackTrace();
71      }
72    }
73    private void jbInit() throws Exception {
74      this.setLayout(borderLayout3);
75      oPanel.setLayout(borderLayout2);
76      oLabel.setText("Ontology");
77      listPanel.setLayout(borderLayout1);
78      jLabel1.setText("Ontologies List");
79      splitPane.setMaximumSize(new Dimension(400, 400));
80      splitPane.setMinimumSize(new Dimension(400, 400));
81      splitPane.setPreferredSize(new Dimension(400,400));
82      splitPane.setDividerSize(3);
83      splitPane.setResizeWeight(0.0);
84      oList.setBorder(BorderFactory.createEtchedBorder());
85      oList.setToolTipText("");
86      fileMenu.setText("File");
87      fileOpen.setText("Open");
88      fileSave.setText("Save");
89      fileExit.setText("Exit");
90      View.setText("View");
91      viewRefresh.setText("Refresh");
92      helpMenu.setText("Help");
93      helpHelp.setText("Help");
94      helpAbout.setText("About");
95      fileClose.setText("Close");
96      fileNew.setText("New");
97      fileSaveAs.setText("Save As");
98      this.add(splitPane, BorderLayout.CENTER);
99      splitPane.add(oPanel, JSplitPane.RIGHT);
100     oPanel.add(oLabel, BorderLayout.NORTH);
101     oPanel.add(oScrollPane, BorderLayout.CENTER);
102     splitPane.add(listPanel, JSplitPane.LEFT);
103     listPanel.add(jLabel1, BorderLayout.NORTH);
104     listPanel.add(oList, BorderLayout.CENTER);
105     oScrollPane.getViewport().add(oTree, null);
106     menuBar.add(fileMenu);
107     menuBar.add(View);
108     menuBar.add(helpMenu);
109     fileMenu.add(fileNew);
110     fileMenu.add(fileOpen);
111     fileMenu.add(fileSave);
112     fileMenu.add(fileSaveAs);
113     fileMenu.add(fileClose);
114     fileMenu.addSeparator();
115     fileMenu.add(fileExit);
116     View.add(viewRefresh);
117     splitPane.setDividerLocation(160);
118 
119     /* setting the ontologies list popup menu */
120     saveItem = new JMenuItem("Save");
121     saveAsItem = new JMenuItem("Save as ...");
122     renameItem = new JMenuItem("Rename");
123 //    deleteItem = new JMenuItem("delete");
124     editURIItem = new JMenuItem("Edit URI");
125     closeItem = new JMenuItem("Close");
126 
127     listPopup.add(saveItem);
128     listPopup.add(saveAsItem);
129     listPopup.addSeparator();
130     listPopup.add(renameItem);
131     listPopup.add(editURIItem);
132     listPopup.addSeparator();
133     listPopup.add(closeItem);
134 
135     /* this line should be commented if working with the jbuilder designer */
136     this.add(menuBar,borderLayout1.BEFORE_FIRST_LINE);
137     helpMenu.add(helpHelp);
138     helpMenu.add(helpAbout);
139 
140     /*init listeners*/
141     AssociateListeners();
142 
143   }// jbInit();
144 
145   /**Sets the ontology editor which agregates this main frame
146    * @param oe the ontology editor be set   */
147   public void setOntologyEditor(OntologyEditor oe) {
148     editor = oe;
149   }
150 
151   /**Gets the Ontology Editor.
152    * @return the ontology editor   */
153   public OntologyEditor getOntologyEditor() {
154     return editor;
155   }
156 
157   /**Sets list of ontologies to be displayed
158    * @param list */
159   public void setOntologyList(Vector list) {
160       int index = oList.getAnchorSelectionIndex();
161       index--;
162       if ( index < 0 ) index = 0;
163 
164       oList.setListData(list);
165       oList.validate();
166   }
167 
168   /**Sets the tree to be displayed.see also setOntology.
169    * @param tree  */
170   public void setOntoTree(JTree tree) {
171     if (null != oTree) {
172       oScrollPane.getViewport().remove(oTree);
173     }
174     oTree = tree;
175     oTree.validate();
176     oScrollPane.getViewport().add(oTree,null);
177   }// setOntoTree();
178 
179   /**Same as setOntoTree but builds the tree from an ontology
180    * @param o an ontology    */
181   public void buildOntoTree(Taxonomy o) {
182     boolean includeInstances = o instanceof Ontology;
183     ClassNode root = ClassNode.createRootNode(o, includeInstances);
184     OntoTreeModel model = new OntoTreeModel(root);
185     EditableTreeView view = new EditableTreeView(model);
186     KnowledgeBaseTreeCellRenderer kbTreeCellRenderer =
187                               new KnowledgeBaseTreeCellRenderer();
188     view.setCellRenderer(kbTreeCellRenderer);
189     view.setMainPanel(this);
190     this.setOntoTree(view);
191   }//buildOntoTree();
192 
193   /**Associates listeners with components   */
194   private void AssociateListeners() {
195     oList.addListSelectionListener(new LSListener());
196 
197     saveItem.addActionListener(new SaveListener());
198     saveAsItem.addActionListener(new SaveAsListener());
199     renameItem.addActionListener(new RenameListener());
200 //    deleteItem.addActionListener(new DeleteListener());
201     editURIItem.addActionListener(new EditURIListener() );
202 
203     closeItem.addActionListener(new CloseListener());
204 
205     oList.addMouseListener(new ListPopupListener());
206 
207     fileExit.addActionListener(new FileExitListener());
208     fileOpen.addActionListener(new FileOpenListener());
209     fileSave.addActionListener(new FileSaveListener());
210     fileSaveAs.addActionListener(new FileSaveAsListener());
211     fileClose.addActionListener(new FileCloseListener());
212     fileNew.addActionListener(new FileNewListener());
213 
214 
215     viewRefresh.addActionListener(new ViewRefreshListener());
216 
217   } // AssociateListeners()
218 
219 
220   /* private classes */
221   /**LSListener - a list selection listener.*/
222   private class LSListener implements ListSelectionListener {
223     public void valueChanged(ListSelectionEvent e) {
224       if (0 < oList.getModel().getSize()) {
225 
226         Object obj = oList.getModel().getElementAt(oList.getAnchorSelectionIndex());
227         if ( obj instanceof Taxonomy ) {
228           OEMainPanel.this.editor.ontologySelected((Taxonomy) obj);
229         } // only if ontology
230       } // size > 0
231     } // valueChanged();
232   } // class LSListner
233 
234 /** Listener for right click on the ontologies list */
235 private class ListPopupListener extends MouseAdapter {
236   public void mouseClicked(MouseEvent e) {
237     if(SwingUtilities.isRightMouseButton(e)){
238       OEMainPanel.this.listPopup.show(OEMainPanel.this.oList,e.getX(),e.getY());
239     } // if right button
240   } // mouse clicked
241 } // class listPopupListener
242 
243 
244 
245 /**Listener for choosing [save] from the popup menu */
246 private class SaveListener implements ActionListener{
247   public void actionPerformed(ActionEvent e) {
248       try {
249         Set selset = new HashSet();
250 
251         selset.add(
252           oList.getModel().getElementAt(
253             oList.getAnchorSelectionIndex()
254           )
255         );
256 
257 
258         Object[] selecto = oList.getSelectedValues();
259         for ( int i = 0 ; i < selecto.length ; i++) {
260           selset.add(selecto[i]);
261         } // for i
262 
263         Iterator seti = selset.iterator();
264 
265         while (seti.hasNext()) {
266           OEMainPanel.this.editor.saveOntology((Taxonomy) seti.next());
267         } // while set iter
268 
269       } catch (ResourceInstantiationException x) {
270         x.printStackTrace(Err.getPrintWriter());
271       }
272 
273   } // actionPerformed
274 } //class SaveListener
275 
276 /**Listener for choosing [save As] from the popup menu */
277 private class SaveAsListener implements ActionListener{
278   public void actionPerformed(ActionEvent e) {
279     try {
280       Object o = oList.getModel().getElementAt(
281             oList.getAnchorSelectionIndex());
282       if ( o instanceof Taxonomy) {
283         OEMainPanel.this.editor.saveAsOntology((Taxonomy)o,
284         (int)OEMainPanel.this.oList.getLocation().getX(),
285         (int)OEMainPanel.this.oList.getLocation().getY());
286       }
287     } catch (ResourceInstantiationException x) {
288       x.printStackTrace(Err.getPrintWriter());
289     }
290   } // actionPerformed
291 } //class SaveAsListener
292 
293 /**Listener for choosing [rename] from the popup menu */
294 private class RenameListener implements ActionListener{
295   public void actionPerformed(ActionEvent e) {
296     Object o = oList.getModel().getElementAt(
297           oList.getAnchorSelectionIndex());
298     if ( o instanceof Taxonomy) {
299       OEMainPanel.this.editor.renameOntology((Taxonomy)o,
300       (int)OEMainPanel.this.oList.getLocation().getX(),
301       (int)OEMainPanel.this.oList.getLocation().getY());
302     }
303   } // actionPerformed
304 } //class RenameListener
305 
306 /**Listener for choosing [delete] from the popup menu */
307 private class DeleteListener implements ActionListener{
308   public void actionPerformed(ActionEvent e) {
309     try  {
310       Object o = oList.getModel().getElementAt(
311             oList.getAnchorSelectionIndex());
312       if ( o instanceof Taxonomy) {
313         OEMainPanel.this.editor.deleteOntology((Taxonomy)o,
314         (int)OEMainPanel.this.oList.getLocation().getX(),
315         (int)OEMainPanel.this.oList.getLocation().getY());
316       }
317     } catch (ResourceInstantiationException x) {
318       x.printStackTrace(Err.getPrintWriter());
319     }
320   } // actionPerformed
321 } //class DeleteListener
322 
323 /**Listener for choosing [edit URI] from the popup menu */
324 private class EditURIListener implements ActionListener{
325   public void actionPerformed(ActionEvent e) {
326     Object o = oList.getModel().getElementAt(
327           oList.getAnchorSelectionIndex());
328     if ( o instanceof Taxonomy) {
329       OEMainPanel.this.editor.editURI((Taxonomy)o,
330       (int)OEMainPanel.this.oList.getLocation().getX(),
331       (int)OEMainPanel.this.oList.getLocation().getY());
332     }
333   } // actionPerformed
334 } //class DeleteListener
335 
336 
337 /**Listener for choosing [close] from the popup menu */
338 private class CloseListener implements ActionListener{
339   public void actionPerformed(ActionEvent e) {
340     try {
341       Object o = oList.getModel().getElementAt(
342             oList.getAnchorSelectionIndex());
343       if ( o instanceof Taxonomy) {
344         OEMainPanel.this.editor.closeOntology((Taxonomy)o,
345         (int)OEMainPanel.this.oList.getLocation().getX(),
346         (int)OEMainPanel.this.oList.getLocation().getY());
347       }
348     } catch (ResourceInstantiationException x) {
349       x.printStackTrace(Err.getPrintWriter());
350     }
351 
352   } // actionPerformed
353 } //class CloseListener
354 
355 /*------- menu bar items listeners ---------------*/
356 
357 /** File Exit Listener */
358 private class FileExitListener implements ActionListener {
359   public void actionPerformed(ActionEvent e) {
360     OEMainPanel.this.editor.fileExit();
361   } // actionPerformed
362 } // class FileExitListener
363 
364 /** File Open Listener */
365 private class FileOpenListener implements ActionListener {
366   public void actionPerformed(ActionEvent e) {
367     try {
368       OEMainPanel.this.editor.fileOpen(
369         (int)OEMainPanel.this.oList.getLocation().getX(),
370         (int)OEMainPanel.this.oList.getLocation().getY());
371     } catch (ResourceInstantiationException x) {
372       x.printStackTrace(Err.getPrintWriter());
373     }
374   } // actionPerformed
375 } // class FileOpenListener
376 
377 /** File Save Listener */
378 private class FileSaveListener implements ActionListener {
379   public void actionPerformed(ActionEvent e) {
380 
381     OEMainPanel.this.editor.fileSave(
382       0,0,
383       OEMainPanel.this.editor.getModifiedOntologies());
384 
385   } // actionPerformed
386 } // class FileSaveListener
387 
388 /** File Save As Listener */
389 private class FileSaveAsListener implements ActionListener {
390   public void actionPerformed(ActionEvent e) {
391     try {
392       JFileChooser chooser = gate.gui.MainFrame.getFileChooser();
393       chooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
394       int result = chooser.showSaveDialog(OEMainPanel.this);
395       if ( result == JFileChooser.APPROVE_OPTION ) {
396         File selected = chooser.getSelectedFile();
397         URL lurl;
398           lurl = new URL("file:///"+selected.getAbsolutePath());
399           editor.getOntology().setURL(lurl);
400           editor.getOntology().store();
401           JOptionPane.showMessageDialog(
402             OEMainPanel.this,
403             "Ontology saved sucessfuly.\n"
404             +lurl,
405             "Ontology Save As",
406             JOptionPane.PLAIN_MESSAGE);
407 
408       } // approve
409     } catch (Exception xx) {
410       JOptionPane.showMessageDialog(OEMainPanel.this,
411       "The Ontology Save As operation failed.\n"+
412       "Due to: "+xx.getClass() + xx.getMessage(),
413       "Ontology Save As Failure",JOptionPane.ERROR_MESSAGE);
414     }
415   } // actionPerformed
416 } // class FileSaveListener
417 
418 
419 /** File Close Listener */
420 private class FileCloseListener implements ActionListener {
421   public void actionPerformed(ActionEvent e) {
422 
423     OEMainPanel.this.editor.fileClose(
424       0,0,
425       OEMainPanel.this.editor.getOntologyList());
426 
427   } // actionPerformed
428 } // class FileSaveListener
429 
430 /** File New Listener */
431 private class FileNewListener implements ActionListener {
432   public void actionPerformed(ActionEvent e) {
433 
434     OEMainPanel.this.editor.fileNew(
435       0,0);
436   } // actionPerformed
437 } // class FileSaveListener
438 
439 
440 
441 /** View Refresh Listener */
442 private class ViewRefreshListener implements ActionListener {
443   public void actionPerformed(ActionEvent e) {
444     OEMainPanel.this.update(OEMainPanel.this.getGraphics());
445   } // actionPerformed
446 } // class ViewRefreshListener
447 
448 protected class KnowledgeBaseTreeCellRenderer extends DefaultTreeCellRenderer {
449   public KnowledgeBaseTreeCellRenderer() {
450   }
451   public Component getTreeCellRendererComponent(JTree tree,
452                                             Object value,
453                                             boolean sel,
454                                             boolean expanded,
455                                             boolean leaf,
456                                             int row,
457                                             boolean hasFocus){
458     super.getTreeCellRendererComponent(tree, value, sel, expanded,
459                                        leaf, row, hasFocus);
460     if (! (value instanceof ClassNode))
461       return this;
462     ClassNode theNode = (ClassNode) value;
463     if(theNode.getSource() instanceof OClass) {
464       setIcon(MainFrame.getIcon("Class.gif"));
465     } else if(theNode.getSource() instanceof OInstance) {
466       setIcon(MainFrame.getIcon("Instance.gif"));
467     }
468     return this;
469   }
470 }
471 
472 
473 } //class OEMainPanel
474 
475 
476 
477 
478 
479 
480 
481 
482 
483 
484 
485 
486 
487