1   package com.ontotext.gate.vr.dialog;
2   
3   import javax.swing.*;
4   import java.awt.*;
5   import java.awt.event.*;
6   import javax.swing.border.*;
7   
8   import java.net.*;
9   import java.io.*;
10  
11  
12  import com.ontotext.gate.vr.*;
13  
14  import gate.util.*;
15  import gate.creole.ontology.*;
16  
17  
18  /**New Ontology Dialog. Creates a new ontology.
19   */
20  public class NewOntologyDialog extends JDialog {
21    /**reference to the ontology editor which invokde this dialog */
22    protected OntologyEditor editor;
23  
24    protected GridBagLayout gridBagLayout1 = new GridBagLayout();
25    protected JTextField name = new JTextField();
26    protected JLabel jLabel1 = new JLabel();
27    protected JLabel jLabel2 = new JLabel();
28    protected JLabel jLabel3 = new JLabel();
29    protected JTextField sourceURI = new JTextField();
30    protected JTextField url = new JTextField();
31    protected JLabel jLabel4 = new JLabel();
32    protected JTextField comment = new JTextField();
33    protected JButton btnOk = new JButton();
34    protected JButton btnCancel = new JButton();
35    protected TitledBorder titledBorder1;
36  
37    public NewOntologyDialog(OntologyEditor e) {
38      if ( null == e ) {
39        throw new GateRuntimeException(
40        "null ontology editor passed to the constructor of NewOntologyDialog");
41      }
42      editor = e;
43      try {
44        jbInit();
45        initListeners();
46      }
47      catch(Exception ex) {
48        ex.printStackTrace();
49      }
50    }
51    private void jbInit() throws Exception {
52      titledBorder1 = new TitledBorder("");
53      jLabel1.setText("Name");
54      this.getContentPane().setLayout(gridBagLayout1);
55      jLabel2.setText("Source URI");
56      jLabel3.setText("URL");
57      jLabel4.setText("Comment");
58      btnOk.setBorder(BorderFactory.createEtchedBorder());
59      btnOk.setNextFocusableComponent(btnCancel);
60      btnOk.setText("OK");
61      btnCancel.setBorder(BorderFactory.createEtchedBorder());
62      btnCancel.setNextFocusableComponent(name);
63      btnCancel.setText("Cancel");
64      if (!name.requestDefaultFocus())
65        name.requestFocus();
66      name.setNextFocusableComponent(sourceURI);
67      sourceURI.setNextFocusableComponent(url);
68      url.setNextFocusableComponent(comment);
69      comment.setNextFocusableComponent(btnOk);
70      this.setResizable(false);
71      this.getContentPane().add(jLabel1,                       new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0
72              ,GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(13, 0, 0, 0), 33, 0));
73      this.getContentPane().add(jLabel2,                new GridBagConstraints(0, 1, 1, 1, 0.0, 0.0
74              ,GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 6, 0));
75      this.getContentPane().add(jLabel3,                new GridBagConstraints(0, 2, 1, 1, 0.0, 0.0
76              ,GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 41, 0));
77      this.getContentPane().add(sourceURI,                     new GridBagConstraints(1, 1, 4, 1, 0.0, 0.0
78              ,GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(10, 7, 7, 0), 186, 0));
79      this.getContentPane().add(jLabel4,             new GridBagConstraints(0, 3, 1, 1, 0.0, 0.0
80              ,GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 10, 0));
81      this.getContentPane().add(comment,                              new GridBagConstraints(1, 3, 6, 1, 0.0, 0.0
82              ,GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(7, 8, 8, 0), 186, 0));
83      this.getContentPane().add(name,                         new GridBagConstraints(1, 0, 3, 1, 0.0, 0.0
84              ,GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(20, 7, 8, 0), 130, 0));
85      this.getContentPane().add(url,                   new GridBagConstraints(1, 2, 5, 1, 0.0, 0.0
86              ,GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(9, 7, 8, 0), 186, 0));
87      this.getContentPane().add(btnOk,             new GridBagConstraints(1, 4, 1, 1, 0.0, 0.0
88              ,GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(5, 0, 8, 0), 29, 0));
89      this.getContentPane().add(btnCancel,     new GridBagConstraints(2, 4, 3, 1, 0.0, 0.0
90              ,GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(7, 0, 11, 7), 44, 0));
91  
92    }
93  
94    /**
95     * initializes the listeners of the dialog.
96     */
97    private void initListeners() {
98      this.btnOk.addActionListener(new BtnListener());
99      this.btnCancel.addActionListener(new BtnListener());
100     this.addKeyListener(new EnterEscListener());
101 
102     this.setSize(300,245);
103   } // init Listeners
104 
105   /**a button listener */
106   private class BtnListener implements ActionListener {
107     public void actionPerformed(ActionEvent e) {
108       try {
109         if (btnCancel == e.getSource()) {
110           NewOntologyDialog.this.setVisible(false);
111         } // if cancel
112 
113         if (btnOk == e.getSource()) {
114 
115           if (null == editor)
116             throw new GateRuntimeException("reference to the editor not set \n"
117             +"in addSubClassDialog");
118 
119           try {
120             java.net.URL u = new URL(url.getText());
121             String pth = u.getPath();
122             if (-1 != u.getProtocol().indexOf("gate")) {
123               u = gate.util.protocols.gate.Handler.class.getResource(
124                             Files.getResourcePath()
125                           );
126             } // if gate:path url
127 
128             File f;
129             if (u.getPath().equals(pth)) {
130               f = new File(pth);
131             } else {
132               f = new File(u.getPath()+pth);
133             }
134             if (!f.exists()) {
135               f.createNewFile();
136             } // if file doesn't exist
137 
138             editor.createOntology(
139               name.getText(),
140               sourceURI.getText(),
141               url.getText(),
142               comment.getText());
143 
144             NewOntologyDialog.this.setVisible(false);
145 
146           } catch (java.net.MalformedURLException ux) {
147             JOptionPane.showMessageDialog(null,
148               "This is not a valid URL:\n"+
149               url.getText(),"Create Ontology Failure",
150               JOptionPane.ERROR_MESSAGE);
151           }//catch
152            catch (IOException ioe) {
153             JOptionPane.showMessageDialog(null,
154               "Cannot Create New Ontology \n Due to :"+
155               ioe.getMessage(),"Create Ontology Failure",
156               JOptionPane.ERROR_MESSAGE);
157           }//catch
158         } // if ok
159       } catch (gate.creole.ResourceInstantiationException x) {
160         x.printStackTrace(gate.util.Err.getPrintWriter());
161       }
162     } // actionPerformed();
163   }  // class btnListener
164 
165   /**key listener class */
166   private class EnterEscListener implements KeyListener {
167     public void keyTyped(KeyEvent kev){};
168     public void keyReleased(KeyEvent kev) {};
169     public void keyPressed(KeyEvent kev) {
170       try {
171         if (kev.VK_ENTER == kev.getKeyCode()) {
172 
173           if (null == editor)
174             throw new GateRuntimeException("reference to the editor not set \n"
175             +"in addSubClassDialog");
176 
177           try {
178             java.net.URL u = new URL(url.getText());
179             String pth = u.getPath();
180             if (-1 != u.getProtocol().indexOf("gate")) {
181               u = gate.util.protocols.gate.Handler.class.getResource(
182                             Files.getResourcePath()
183                           );
184             } // if gate:path url
185 
186             File f;
187             if (u.getPath().equals(pth)) {
188               f = new File(pth);
189             } else {
190               f = new File(u.getPath()+pth);
191             }
192             if (!f.exists()) {
193               f.createNewFile();
194             } // if file doesn't exist
195 
196             editor.createOntology(
197               name.getText(),
198               sourceURI.getText(),
199               url.getText(),
200               comment.getText());
201 
202             NewOntologyDialog.this.setVisible(false);
203 
204           } catch (java.net.MalformedURLException ux) {
205             JOptionPane.showMessageDialog(null,
206               "This is not a valid URL:\n"+
207               url.getText(),"Create Ontology Failure",
208               JOptionPane.ERROR_MESSAGE);
209           }//catch
210            catch (IOException ioe) {
211             JOptionPane.showMessageDialog(null,
212               "Cannot Create New Ontology \n Due to :"+
213               ioe.getMessage(),"Create Ontology Failure",
214               JOptionPane.ERROR_MESSAGE);
215           }//catch
216         } // if enter
217         else {
218           if (kev.VK_ESCAPE == kev.getKeyCode()) {
219             NewOntologyDialog.this.setVisible(false);
220           } // if escape
221         } // else
222       } catch (gate.creole.ResourceInstantiationException x) {
223         x.printStackTrace(gate.util.Err.getPrintWriter());
224       }
225 
226     } // keyPressed();
227   }  // class enterEscListener
228 
229 
230 } // class NewOntologyDialog