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