KickJava   Java API By Example, From Geeks To Geeks.

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


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 an attribute editor depending on which attribute is to be edited.
13  * Currently only creates a 'postalAddress' editor.
14  * @author Trudi.
15  */

16 public class postaladdresseditor extends JDialog
17     implements abstractstringeditor
18 {
19     protected CBButton btnOk, btnReset, btnCancel, btnHelp;
20     protected JTextArea area;
21     protected CBPanel display;
22     protected editablestring edStringNew;
23
24     /**
25      * Backup of attribute value before editing.
26      */

27     private final AttributeValue attBackup;
28
29    /**
30     * Creates a panel and adds 'Ok', 'Reset', and 'Cancel' buttons to it.
31     * Checks for attribute type and calls the appropriate method.
32     * @param owner the parent frame, usually JXplorer.
33     * @param att the value of the postalAddress attribute.
34     */

35     public postaladdresseditor(Frame owner, AttributeValue att)
36     {
37         super(owner);
38         setModal(true);
39
40         attBackup = att;
41
42         String JavaDoc attID = att.getID();
43
44         setTitle(CBIntText.get(attID));
45
46         display = new CBPanel();
47
48         btnOk = new CBButton(CBIntText.get("Ok"), CBIntText.get("Click here to make the changes (remember to click Submit in the table editor)."));
49         btnOk.addActionListener(new ActionListener() {
50                     public void actionPerformed(ActionEvent e) {
51                        save();
52            }});
53
54         btnReset = new CBButton(CBIntText.get("Reset"), CBIntText.get("Click here to reset your changes."));
55         btnReset.addActionListener(new ActionListener() {
56                     public void actionPerformed(ActionEvent e) {
57                        setStringValue(attBackup);
58            }});
59
60         btnCancel = new CBButton(CBIntText.get("Cancel"), CBIntText.get("Click here to exit."));
61         btnCancel.addActionListener(new ActionListener() {
62                     public void actionPerformed(ActionEvent e) {
63                        quit();
64            }});
65
66         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.
67
CBHelpSystem.useDefaultHelp(btnHelp, HelpIDs.ATTR_POSTAL);
68
69         area = new JTextArea();
70
71         JScrollPane scrollPane = new JScrollPane(area);
72         scrollPane.setPreferredSize(new Dimension(310,60));
73
74         display.makeHeavy();
75         display.addln(scrollPane);
76         display.makeLight();
77
78         JPanel buttonPanel = new JPanel();
79
80         buttonPanel.add(btnOk);
81         buttonPanel.add(btnReset);
82         buttonPanel.add(btnCancel);
83         buttonPanel.add(btnHelp);
84         display.add(buttonPanel);
85
86         //TE: better way to implement keystroke listening...
87
display.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke("ENTER"), "enter");
88         display.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke("ESCAPE"), "escape");
89         display.getActionMap().put("enter", new MyAction(CBAction.ENTER));
90         display.getActionMap().put("escape", new MyAction(CBAction.ESCAPE));
91
92         getContentPane().add(display);
93         setSize(300,175);
94     }
95
96     /**
97      * Apparently it is better to use key bindings rather than adding a KeyListener...
98      * "for reacting in a special way to particular keys, you usually should use key
99      * bindings instead of a key listener".
100      * This class lets the user set the key as an int. If a key is pressed and it
101      * matches the assigned int, a check is done for if it is an escape or enter key.
102      * (27 or 10). If escape, the quit method is called. If enter, the save
103      * method is called.
104      * Bug 4646.
105      * @author Trudi.
106      */

107     private class MyAction extends CBAction
108     {
109         /**
110          * Calls super constructor.
111          * @param key
112          */

113         public MyAction(int key)
114         {
115             super(key);
116         }
117
118         /**
119          * quit is called if the Esc key pressed,
120          * save is called if Enter key is pressed.
121          * @param e never used.
122          */

123         public void actionPerformed(ActionEvent e)
124         {
125             if (getKey() == ESCAPE)
126                 quit();
127             else if (getKey() == ENTER)
128                 save();
129         }
130     }
131
132    /**
133     * Sets the attribute value in the text area of the attribute editor.
134     * Replaces the '$' with '\n'.
135     * @param edStringOld the string that needs the '$' replaced with the '\n'.
136     */

137     public void setStringValue(editablestring edStringOld)
138     {
139         String JavaDoc attString = edStringOld.getStringValue();
140         String JavaDoc attRemoveDollar = attString.replace('$', '\n');
141         attRemoveDollar = CBParse.replaceAllString(new StringBuffer JavaDoc(attRemoveDollar), "\\24", "$"); //TE: replaces '\24' with '$'.
142
//attRemoveDollar = CBUtility.replaceAllString(new StringBuffer(attRemoveDollar), "\\", "");
143
area.setText(attRemoveDollar);
144         edStringNew = edStringOld;
145     }
146
147    /**
148     * Sets the changed attribute value from the attribute editor in the table.
149     */

150     public void save()
151     {
152         String JavaDoc newAttribute = area.getText();
153
154         // Replaces '$' with '\24'...
155
String JavaDoc temp = CBParse.replaceAllString(new StringBuffer JavaDoc(newAttribute), "$", "\\24");
156
157         // Places the '$' back...
158
String JavaDoc editedAttribute = temp.replace('\n', '$');
159
160         boolean check = true;
161
162         // Un-comment this to do the 6 line/30character check!
163
//check = addressChecker(editedAttribute);
164

165         if(check)
166         {
167             // Sets the attribute value to reflect the changes made in the attribute editor...
168
edStringNew.setStringValue(editedAttribute);
169             quit();
170         }
171     }
172
173    /**
174     * Shuts the panel.
175     */

176     public void quit()
177     {
178         setVisible(false);
179         dispose();
180     }
181 }
Popular Tags