1   package com.ontotext.gate.vr.dialog;
2   
3   import javax.swing.*;
4   import java.awt.*;
5   import java.awt.event.*;
6   
7   import gate.creole.ontology.*;
8   import gate.util.*;
9   
10  import com.ontotext.gate.vr.*;
11  
12  
13  
14  /**Rename Ontology Dialog*/
15  public class RenameOntologyDialog extends JDialog {
16    protected GridLayout gridLayout1 = new GridLayout();
17    protected JLabel jLabel1 = new JLabel();
18    public JTextField nameField = new JTextField();
19    protected JLabel jLabel2 = new JLabel();
20    public JTextField commentField = new JTextField();
21    protected JButton btnOk = new JButton();
22    protected JButton btnCancel = new JButton();
23  
24    /**reference to the ontology editor that invoked this*/
25    private OntologyEditor editor;
26    /** reference to the ontology being renamed */
27    private Taxonomy ontology;
28  
29    /**construct the dialog
30     * @param e the editor
31     * @param o the ontology being renamed   */
32    public RenameOntologyDialog(OntologyEditor e, Taxonomy o) {
33      if ( null == e )
34        throw new GateRuntimeException("editor is null, on constructing RenameOntologyDialog");
35  
36      if ( null == o )
37        throw new GateRuntimeException("ontology is null, on constructing RenameOntologyDialog");
38  
39      ontology = o;
40      editor = e;
41  
42      try {
43        jbInit();
44      }
45      catch(Exception ex) {
46        ex.printStackTrace();
47      }
48  
49    }// constructor(editor,ontology)
50  
51    private void jbInit() throws Exception {
52      jLabel1.setText("Name");
53      gridLayout1.setColumns(2);
54      gridLayout1.setRows(3);
55      this.getContentPane().setLayout(gridLayout1);
56      jLabel2.setText("Comment");
57      btnOk.setMaximumSize(new Dimension(30, 20));
58      btnOk.setMinimumSize(new Dimension(30, 20));
59      btnOk.setPreferredSize(new Dimension(30, 20));
60      btnOk.setMnemonic('0');
61      btnOk.setText("OK");
62      btnCancel.setText("CANCEL");
63      this.setTitle("");
64      this.getContentPane().add(jLabel1, null);
65      this.getContentPane().add(nameField, null);
66      this.getContentPane().add(jLabel2, null);
67      this.getContentPane().add(commentField, null);
68      this.getContentPane().add(btnOk, null);
69      this.getContentPane().add(btnCancel, null);
70  
71      this.setSize(new Dimension(300, 91));
72  
73      this.btnOk.addActionListener(new BtnListener());
74      this.btnCancel.addActionListener(new BtnListener());
75      this.addKeyListener(new EnterEscListener());
76    }
77  
78    /**a button listener */
79    private class BtnListener implements ActionListener {
80      public void actionPerformed(ActionEvent e) {
81        if (btnCancel == e.getSource()) {
82          RenameOntologyDialog.this.setVisible(false);
83        } // if cancel
84  
85        if (btnOk == e.getSource()) {
86  
87          if (null == editor)
88              throw new GateRuntimeException("reference to the editor not set \n"
89              +"in RenameOntologyDialog");
90  
91          if (null == ontology)
92              throw new GateRuntimeException("reference to the ontology not set \n"
93              +"in RenameOntologyDialog");
94  
95          ontology.setName(RenameOntologyDialog.this.nameField.getText());
96          ontology.setComment(RenameOntologyDialog.this.commentField.getText());
97  
98          gate.CreoleRegister reg = gate.Gate.getCreoleRegister();
99          reg.setResourceName(ontology,RenameOntologyDialog.this.nameField.getText());
100 
101         RenameOntologyDialog.this.setVisible(false);
102       } // if ok
103     } // actionPerformed();
104   }  // class btnListener
105 
106 
107   /** an Enter and Esc KeyListener */
108   private class EnterEscListener implements KeyListener {
109     public void keyTyped(KeyEvent kev){};
110     public void keyReleased(KeyEvent kev) {};
111     public void keyPressed(KeyEvent kev) {
112       if (kev.VK_ENTER == kev.getKeyCode()) {
113         if (null == editor)
114           throw new GateRuntimeException("reference to the editor not set \n"
115           +"in RenameOntologyDialog");
116 
117         if (null == ontology )
118           throw new GateRuntimeException(
119           "reference to the ontolgy to be renamed is null \n"
120           +"in RenameOntologyDialog");
121 
122           ontology.setName(RenameOntologyDialog.this.nameField.getText());
123 
124           ontology.setComment(RenameOntologyDialog.this.commentField.getText());
125 
126           gate.CreoleRegister reg = gate.Gate.getCreoleRegister();
127           reg.setResourceName(ontology,RenameOntologyDialog.this.nameField.getText());
128 
129 
130           RenameOntologyDialog.this.setVisible(false);
131 
132       } // if enter
133       else {
134         if (kev.VK_ESCAPE == kev.getKeyCode()) {
135           RenameOntologyDialog.this.setVisible(false);
136         } // if escape
137       } // else
138     } // keyReleased
139   }  // class enterEscListener
140 
141 
142 
143 }//class RenameOntologyDialog