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  /**the dialog for renaming an ontology*/
15  public class RenameClassDialog 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    /** the main panel of the editor */
29    private OEMainPanel panel;
30    /**the class node associated with the class */
31    private ClassNode node;
32    /** the class being renamed */
33    private TClass clas;
34  
35    /**construct the dialog
36     * @param e the editor
37     * @param oef the main panel
38     * @param n the class node which should also be renamed
39     * @param o the ontology being renamed   */
40    public RenameClassDialog(OntologyEditor e,OEMainPanel oef,
41                            ClassNode n,TClass c) {
42      if ( null == e )
43        throw new GateRuntimeException(
44        "editor is null, on constructing RenameClassDialog");
45      if ( null == n )
46        throw new GateRuntimeException(
47        "class node is null, on constructing RenameClassDialog");
48      if ( null == oef )
49        throw new GateRuntimeException(
50        "editor's main panel is null, on constructing RenameClassDialog");
51      if ( null == c )
52        throw new GateRuntimeException(
53        "ontology class is null, on constructing RenameClassDialog");
54  
55      ontology = c.getOntology();
56  
57      if ( null == ontology )
58        throw new GateRuntimeException("ontology is null, on constructing RenameClassDialog");
59  
60      clas = c;
61      editor = e;
62      panel = oef;
63      node = n;
64  
65      try {
66        jbInit();
67      }
68      catch(Exception ex) {
69        ex.printStackTrace();
70      }
71  
72    }// constructor(editor,ontology)
73  
74    private void jbInit() throws Exception {
75      jLabel1.setText("Name");
76      gridLayout1.setColumns(2);
77      gridLayout1.setRows(3);
78      this.getContentPane().setLayout(gridLayout1);
79      jLabel2.setText("Comment");
80      btnOk.setMaximumSize(new Dimension(30, 20));
81      btnOk.setMinimumSize(new Dimension(30, 20));
82      btnOk.setPreferredSize(new Dimension(30, 20));
83      btnOk.setMnemonic('0');
84      btnOk.setText("OK");
85      btnCancel.setText("CANCEL");
86      this.setTitle("");
87      this.getContentPane().add(jLabel1, null);
88      this.getContentPane().add(nameField, null);
89      this.getContentPane().add(jLabel2, null);
90      this.getContentPane().add(commentField, null);
91      this.getContentPane().add(btnOk, null);
92      this.getContentPane().add(btnCancel, null);
93  
94      this.setSize(new Dimension(300, 91));
95  
96      this.btnOk.addActionListener(new BtnListener());
97      this.btnCancel.addActionListener(new BtnListener());
98      this.addKeyListener(new EnterEscListener());
99    }
100 
101   /**a button listener */
102   private class BtnListener implements ActionListener {
103     public void actionPerformed(ActionEvent e) {
104       if (btnCancel == e.getSource()) {
105         RenameClassDialog.this.setVisible(false);
106       } // if cancel
107 
108       if (btnOk == e.getSource()) {
109 
110         if (null == editor)
111             throw new GateRuntimeException("reference to the editor not set \n"
112             +"in RenameClassDialog");
113 
114         if (null == ontology)
115             throw new GateRuntimeException("reference to the ontology not set \n"
116             +"in RenameClassDialog");
117 
118         clas.setName(RenameClassDialog.this.nameField.getText());
119         clas.setComment(RenameClassDialog.this.commentField.getText());
120         clas.getOntology().setModified(true);
121 
122         RenameClassDialog.this.setVisible(false);
123 
124         node.rename(clas.getName());
125         panel.oTree.updateUI();
126 
127       } // if ok
128     } // actionPerformed();
129   }  // class btnListener
130 
131 
132   private class EnterEscListener implements KeyListener {
133     public void keyTyped(KeyEvent kev){};
134     public void keyReleased(KeyEvent kev) {};
135     public void keyPressed(KeyEvent kev) {
136       if (kev.VK_ENTER == kev.getKeyCode()) {
137         if (null == editor)
138           throw new GateRuntimeException("reference to the editor not set \n"
139           +"in RenameClassDialog");
140 
141         if (null == ontology )
142           throw new GateRuntimeException(
143           "reference to the ontolgy to be renamed is null \n"
144           +"in RenameClassDialog");
145 
146           clas.setName(RenameClassDialog.this.nameField.getText());
147           clas.setComment(RenameClassDialog.this.commentField.getText());
148           clas.getOntology().setModified(true);
149           RenameClassDialog.this.setVisible(false);
150 
151           node.rename(clas.getName());
152           panel.oTree.updateUI();
153 
154       } // if enter
155       else {
156         if (kev.VK_ESCAPE == kev.getKeyCode()) {
157           RenameClassDialog.this.setVisible(false);
158         } // if escape
159       } // else
160     } // keyReleased
161   }  // class enterEscListener
162 
163 
164 
165 }//class RenameClassDialog