KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > web > monitor > client > ParamEditor


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 /**
21  * @author Simran Gleason
22  * @author Ana von Klopp
23  */

24
25
26 package org.netbeans.modules.web.monitor.client;
27
28 import java.awt.Color JavaDoc;
29 import java.awt.Dialog JavaDoc;
30 import java.awt.Dimension JavaDoc;
31 import java.awt.Insets JavaDoc;
32 import java.awt.GridBagConstraints JavaDoc;
33 import java.awt.GridBagLayout JavaDoc;
34 import java.awt.event.ActionEvent JavaDoc;
35 import java.awt.event.ActionListener JavaDoc;
36 import java.awt.event.FocusEvent JavaDoc;
37 import java.awt.event.FocusListener JavaDoc;
38
39 import javax.swing.BorderFactory JavaDoc;
40 import javax.swing.JLabel JavaDoc;
41 import javax.swing.JScrollPane JavaDoc;
42 import javax.swing.JTextArea JavaDoc;
43 import javax.swing.JTextField JavaDoc;
44
45 import org.openide.DialogDisplayer;
46 import org.openide.DialogDescriptor;
47 import org.openide.NotifyDescriptor;
48 import org.openide.util.NbBundle;
49
50 public class ParamEditor extends javax.swing.JPanel JavaDoc {
51
52     private static final Dimension JavaDoc valueSize = new Dimension JavaDoc(400, 150);
53
54     // Do we need this to close it?
55
private Dialog JavaDoc dialog = null;
56     private Dialog JavaDoc d2 = null;
57     private DialogDescriptor editDialog = null;
58     private String JavaDoc errorMessage = null;
59     private boolean dialogOK = false;
60
61     // Hold the name and value until the widgets are created.
62
private String JavaDoc name = ""; //NOI18N
63
private String JavaDoc value = ""; //NOI18N
64
private Editable editable;
65     private Condition condition;
66     private String JavaDoc title = ""; //NOI18N
67

68     //private static boolean repainting = false;
69
private boolean repainting = false;
70
71     public ParamEditor(String JavaDoc name, String JavaDoc value, Editable e, Condition c,
72                String JavaDoc title) {
73     this.name = name;
74     this.value = value;
75     this.editable = e;
76     this.condition = c;
77     this.title = title;
78     initialize();
79     }
80
81     public boolean getDialogOK() {
82     return dialogOK;
83     }
84
85     public String JavaDoc getName() {
86     return name;
87     }
88
89     public String JavaDoc getValue() {
90     return value;
91     }
92     
93     public void initialize() {
94
95     this.setLayout(new GridBagLayout JavaDoc());
96
97     // Entity covers entire row
98
GridBagConstraints JavaDoc labelC = new GridBagConstraints JavaDoc();
99     labelC.gridx = 0;
100     labelC.gridy = GridBagConstraints.RELATIVE;
101     labelC.anchor = GridBagConstraints.WEST;
102     labelC.fill = GridBagConstraints.HORIZONTAL;
103     labelC.insets = new Insets JavaDoc(4, 15, 4, 15);
104
105     // Initial label
106
GridBagConstraints JavaDoc firstC = new GridBagConstraints JavaDoc();
107     firstC.gridx = 0;
108     firstC.gridy = GridBagConstraints.RELATIVE;
109     firstC.gridwidth = 1;
110     firstC.anchor = GridBagConstraints.WEST;
111     firstC.insets = new Insets JavaDoc(4, 15, 4, 0);
112
113     // Text field
114
GridBagConstraints JavaDoc tfC = new GridBagConstraints JavaDoc();
115     tfC.gridx = GridBagConstraints.RELATIVE;
116     tfC.gridy = 0;
117     tfC.gridwidth = 7;
118     tfC.fill = GridBagConstraints.HORIZONTAL;
119     tfC.insets = new Insets JavaDoc(4, 0, 4, 15);
120
121     // Text area
122
GridBagConstraints JavaDoc taC = new GridBagConstraints JavaDoc();
123     taC.gridx = 0;
124     taC.gridy = GridBagConstraints.RELATIVE;
125     taC.gridheight = 3;
126     taC.gridwidth = 8;
127         taC.weightx = 1.0;
128         taC.weighty = 1.0;
129     taC.fill = GridBagConstraints.BOTH;
130     taC.anchor = GridBagConstraints.WEST;
131     taC.insets = new Insets JavaDoc(0, 15, 4, 15);
132
133         JLabel JavaDoc nameLabel = new JLabel JavaDoc();
134         nameLabel.setDisplayedMnemonic(NbBundle.getMessage(ParamEditor.class, "MON_Name_Mnemonic").charAt(0));
135         nameLabel.getAccessibleContext().setAccessibleDescription(NbBundle.getMessage(ParamEditor.class, "ACS_MON_NameA11yDesc"));
136         if (name == null || name.length()==0)
137             getAccessibleContext().setAccessibleDescription(title);
138         else
139              getAccessibleContext().setAccessibleDescription(NbBundle.getMessage(ParamEditor.class,
140                 "ACS_ParamEditorA11yDesc",title, name)); //NOI18N
141
String JavaDoc text = NbBundle.getMessage(ParamEditor.class, "MON_Name");
142     text = text.concat(": "); //NOI18N
143

144     if(editable == Editable.BOTH) {
145         final JTextField JavaDoc nameText = new JTextField JavaDoc(25);
146         nameText.addFocusListener(new FocusListener JavaDoc() {
147         public void focusGained(FocusEvent JavaDoc evt) {
148         }
149         public void focusLost(FocusEvent JavaDoc evt) {
150             name = nameText.getText();
151         }
152         });
153         nameLabel.setLabelFor(nameText);
154         nameText.getAccessibleContext().setAccessibleName(NbBundle.getMessage(ParamEditor.class, "ACS_MON_NameTextFieldA11yName"));
155         nameText.setToolTipText(NbBundle.getMessage(ParamEditor.class, "ACS_MON_NameTextFieldA11yDesc"));
156         nameText.setText(name);
157         nameText.setBackground(java.awt.Color.white);
158         nameText.setEditable(editable == Editable.BOTH);
159
160         this.add(nameLabel, firstC);
161         this.add(nameText, tfC);
162     }
163     else {
164         this.add(nameLabel, labelC);
165         text = text.concat(name);
166     }
167     nameLabel.setText(text);
168         
169     JLabel JavaDoc valueLabel = new JLabel JavaDoc();
170     valueLabel.setText(NbBundle.getMessage(ParamEditor.class, "MON_Value").concat(":"));
171         valueLabel.setDisplayedMnemonic(NbBundle.getMessage(ParamEditor.class, "MON_Value_Mnemonic").charAt(0));
172         valueLabel.getAccessibleContext().setAccessibleDescription(NbBundle.getMessage(ParamEditor.class, "ACS_MON_ValueA11yDesc"));
173     firstC.gridy++;
174     this.add(valueLabel, labelC);
175
176     final JTextArea JavaDoc valueText = new JTextArea JavaDoc();
177     valueText.addFocusListener(new FocusListener JavaDoc() {
178         public void focusGained(FocusEvent JavaDoc evt) {
179         }
180         public void focusLost(FocusEvent JavaDoc evt) {
181             value = valueText.getText();
182         }
183         });
184     valueLabel.setLabelFor(valueText);
185         valueText.getAccessibleContext().setAccessibleName(NbBundle.getMessage(ParamEditor.class, "ACS_MON_ValueTextAreaA11yName"));
186         valueText.setToolTipText(NbBundle.getMessage(ParamEditor.class, "ACS_MON_ValueTextAreaA11yDesc"));
187     if(editable == Editable.NEITHER) {
188         valueText.setEditable(false);
189         valueText.setBackground(this.getBackground());
190         valueText.setForeground(Color.BLACK);
191         valueText.setBorder(BorderFactory.createLoweredBevelBorder());
192     }
193     valueText.setText(value);
194     valueText.setLineWrap(true);
195     valueText.setWrapStyleWord(false);
196     
197     JScrollPane JavaDoc scrollpane = new JScrollPane JavaDoc(valueText);
198     scrollpane.setPreferredSize(valueSize);
199     //scrollpane.setViewportBorder(BorderFactory.createLoweredBevelBorder());
200
this.add(scrollpane, taC);
201
202     // Housekeeping
203
// this.setMaximumSize(this.getPreferredSize());
204
this.repaint();
205     }
206
207     public void showDialog() {
208
209     if(editable == Editable.NEITHER) {
210         NotifyDescriptor d =
211         new NotifyDescriptor(this, title,
212                      NotifyDescriptor.DEFAULT_OPTION,
213                      NotifyDescriptor.PLAIN_MESSAGE,
214                      new Object JavaDoc[] { NotifyDescriptor.OK_OPTION },
215                      NotifyDescriptor.OK_OPTION);
216         DialogDisplayer.getDefault().notify(d);
217     }
218     else {
219         editDialog = new DialogDescriptor
220         (this, title, true, DialogDescriptor.OK_CANCEL_OPTION,
221          DialogDescriptor.CANCEL_OPTION,
222          new ActionListener JavaDoc() {
223              public void actionPerformed(ActionEvent JavaDoc e) {
224              evaluateInput();
225              }
226          });
227
228         dialog = DialogDisplayer.getDefault().createDialog(editDialog);
229         dialog.setVisible(true);
230         this.repaint();
231     }
232     }
233
234     /**
235      * Handle user input...
236      */

237
238     public void evaluateInput() {
239
240     if (editDialog.getValue().equals(NotifyDescriptor.CANCEL_OPTION)) {
241         dialog.dispose();
242         dialogOK = false;
243         return;
244     }
245
246     if(editable == Editable.NEITHER) {
247         dialog.dispose();
248         dialogOK = false;
249         return;
250     }
251
252     errorMessage = null;
253
254     if(name.equals(""))
255         errorMessage = NbBundle.getMessage(ParamEditor.class,
256                            "MSG_no_name");
257     else if(condition == Condition.COOKIE && name.equalsIgnoreCase("jsessionid"))
258         errorMessage = NbBundle.getMessage(ParamEditor.class,
259                            "MSG_no_jsession");
260     else if(condition == Condition.VALUE && value.equals(""))
261         errorMessage = NbBundle.getMessage(ParamEditor.class,
262                            "MSG_no_value");
263     else if(condition == Condition.HEADER &&
264         name.equalsIgnoreCase("cookie") &&
265         (value.indexOf("jsessionid") > -1 ||
266          value.indexOf("JSESSIONID") > -1))
267         errorMessage = NbBundle.getMessage(ParamEditor.class,
268                            "MSG_no_jsession");
269
270     if(errorMessage == null) {
271         dialog.dispose();
272         dialogOK = true;
273     }
274     else {
275        editDialog.setValue(NotifyDescriptor.CLOSED_OPTION);
276        NotifyDescriptor nd = new NotifyDescriptor.Message
277            (errorMessage, NotifyDescriptor.ERROR_MESSAGE);
278         DialogDisplayer.getDefault().notify(nd);
279     }
280     }
281
282
283     // Do we need this?
284
public void repaint() {
285     super.repaint();
286     if (dialog != null && !repainting) {
287         repainting = true;
288         dialog.repaint();
289         repainting = false;
290     }
291     }
292
293     static class Editable {
294     private String JavaDoc editable;
295
296     private Editable(String JavaDoc editable) {
297         this.editable = editable;
298     }
299     
300     public String JavaDoc toString() { return editable; }
301
302     public static final Editable BOTH = new Editable("both");
303     public static final Editable VALUE = new Editable("value");
304     public static final Editable NEITHER = new Editable("neither");
305     }
306
307     static class Condition {
308     private String JavaDoc condition;
309
310     private Condition(String JavaDoc condition) {
311         this.condition = condition;
312     }
313     
314     public String JavaDoc toString() { return condition; }
315
316     public static final Condition NONE = new Condition("none");
317     public static final Condition VALUE = new Condition("value");
318     public static final Condition COOKIE = new Condition("cookie");
319     public static final Condition HEADER = new Condition("header");
320     }
321
322 } // ParamEditor
323
Popular Tags