KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > ca > directory > jxplorer > editor > booleaneditor


1 package com.ca.directory.jxplorer.editor;
2
3 import com.ca.commons.cbutil.*;
4 import com.ca.directory.jxplorer.viewer.tableviewer.*;
5 import com.ca.directory.jxplorer.HelpIDs;
6
7 import javax.swing.*;
8 import java.awt.*;
9 import java.awt.event.*;
10
11 /**
12  * Creates a Boolean Editor.
13  * @author Trudi.
14  */

15 public class booleaneditor extends JDialog
16     implements abstractstringeditor
17 {
18     public static final String JavaDoc TRUE = "TRUE";
19     public static final String JavaDoc FALSE = "FALSE";
20
21     protected CBButton btnOk = null, btnCancel = null, btnHelp = null;
22     protected JComboBox combo = null;
23     protected CBPanel panel = null;
24     protected editablestring newEditableString = null;
25
26    /**
27     * Creates a panel and adds 'Ok', 'Reset', and 'Cancel' buttons to it.
28     * Checks for attribute type and calls the appropriate method.
29     * @param owner the parent frame, usually JXplorer.
30     * @param att the value of the postalAddress attribute.
31     */

32     public booleaneditor(Frame owner, AttributeValue att)
33     {
34         super(owner);
35         setModal(true);
36
37         String JavaDoc attID = att.getID();
38
39         setTitle(CBIntText.get(attID));
40
41         panel = new CBPanel();
42
43         btnOk = new CBButton(CBIntText.get("Ok"), CBIntText.get("Click here to make the changes (remember to click Submit in the table editor)."));
44         btnOk.addActionListener(new ActionListener() {
45                     public void actionPerformed(ActionEvent e) {
46                        save();
47            }});
48
49         btnCancel = new CBButton(CBIntText.get("Cancel"), CBIntText.get("Click here to exit."));
50         btnCancel.addActionListener(new ActionListener() {
51                     public void actionPerformed(ActionEvent e) {
52                        quit();
53            }});
54
55         btnHelp = new CBButton(CBIntText.get("Help"), CBIntText.get("Click here for Help.")); //TE: creates a new help button with a listener that will open JX help at appropriate location.
56
CBHelpSystem.useDefaultHelp(btnHelp, HelpIDs.ATTR_BOOLEAN);
57
58         combo = new JComboBox();
59         combo.addItem(TRUE);
60         combo.addItem(FALSE);
61         combo.setSize(20, 40);
62
63         panel.makeLight();
64         panel.add(combo);
65         panel.addln(new JLabel(" "));
66
67         JPanel buttonPanel = new JPanel();
68
69         buttonPanel.add(btnOk);
70         buttonPanel.add(btnCancel);
71         buttonPanel.add(btnHelp); //TODO add help when and ID is supplied.
72
panel.add(buttonPanel);
73
74         //TE: better way to implement keystroke listening...
75
panel.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke("ENTER"), "enter");
76         panel.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke("ESCAPE"), "escape");
77         panel.getActionMap().put("enter", new MyAction(CBAction.ENTER));
78         panel.getActionMap().put("escape", new MyAction(CBAction.ESCAPE));
79
80         getContentPane().add(panel);
81         setSize(210,100);
82     }
83
84     /**
85      * Apparently it is better to use key bindings rather than adding a KeyListener...
86      * "for reacting in a special way to particular keys, you usually should use key
87      * bindings instead of a key listener".
88      * This class lets the user set the key as an int. If a key is pressed and it
89      * matches the assigned int, a check is done for if it is an escape or enter key.
90      * (27 or 10). If escape, the quit method is called. If enter, the save
91      * method is called.
92      * Bug 4646.
93      * @author Trudi.
94      */

95     private class MyAction extends CBAction
96     {
97         /**
98          * Calls super constructor.
99          * @param key
100          */

101         public MyAction(int key)
102         {
103             super(key);
104         }
105
106         /**
107          * quit is called if the Esc key pressed,
108          * save is called if Enter key is pressed.
109          * @param e never used.
110          */

111         public void actionPerformed(ActionEvent e)
112         {
113             if (getKey() == ESCAPE)
114                 quit();
115             else if (getKey() == ENTER)
116                 save();
117         }
118     }
119
120    /**
121     * Sets the attribute value in the attribute editor.
122     * @param oldEditableString the string value.
123     */

124     public void setStringValue(editablestring oldEditableString)
125     {
126         String JavaDoc attString = oldEditableString.getStringValue();
127
128         if(attString == null || attString.equalsIgnoreCase(FALSE))
129             combo.setSelectedItem(FALSE);
130         else
131             combo.setSelectedItem(TRUE);
132
133         newEditableString = oldEditableString;
134     }
135
136    /**
137     * Sets the changed attribute value from the attribute editor in the table.
138     */

139     public void save()
140     {
141         Object JavaDoc obj = combo.getSelectedItem();
142
143         if(obj != null && ((String JavaDoc)obj).equalsIgnoreCase(TRUE))
144             newEditableString.setStringValue(TRUE);
145         else if(obj != null && ((String JavaDoc)obj).equalsIgnoreCase(FALSE))
146             newEditableString.setStringValue(FALSE);
147
148         quit();
149     }
150
151    /**
152     * Shuts the panel.
153     */

154     public void quit()
155     {
156         setVisible(false);
157         dispose();
158     }
159 }
Popular Tags