001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: /**
043: * @author Simran Gleason
044: * @author Ana von Klopp
045: */package org.netbeans.modules.web.monitor.client;
046:
047: import java.awt.Color;
048: import java.awt.Dialog;
049: import java.awt.Dimension;
050: import java.awt.Insets;
051: import java.awt.GridBagConstraints;
052: import java.awt.GridBagLayout;
053: import java.awt.event.ActionEvent;
054: import java.awt.event.ActionListener;
055: import java.awt.event.FocusEvent;
056: import java.awt.event.FocusListener;
057:
058: import javax.swing.BorderFactory;
059: import javax.swing.JLabel;
060: import javax.swing.JScrollPane;
061: import javax.swing.JTextArea;
062: import javax.swing.JTextField;
063:
064: import org.openide.DialogDisplayer;
065: import org.openide.DialogDescriptor;
066: import org.openide.NotifyDescriptor;
067: import org.openide.util.NbBundle;
068:
069: public class ParamEditor extends javax.swing.JPanel {
070:
071: private static final Dimension valueSize = new Dimension(400, 150);
072:
073: // Do we need this to close it?
074: private Dialog dialog = null;
075: private Dialog d2 = null;
076: private DialogDescriptor editDialog = null;
077: private String errorMessage = null;
078: private boolean dialogOK = false;
079:
080: // Hold the name and value until the widgets are created.
081: private String name = ""; //NOI18N
082: private String value = ""; //NOI18N
083: private Editable editable;
084: private Condition condition;
085: private String title = ""; //NOI18N
086:
087: //private static boolean repainting = false;
088: private boolean repainting = false;
089:
090: public ParamEditor(String name, String value, Editable e,
091: Condition c, String title) {
092: this .name = name;
093: this .value = value;
094: this .editable = e;
095: this .condition = c;
096: this .title = title;
097: initialize();
098: }
099:
100: public boolean getDialogOK() {
101: return dialogOK;
102: }
103:
104: public String getName() {
105: return name;
106: }
107:
108: public String getValue() {
109: return value;
110: }
111:
112: public void initialize() {
113:
114: this .setLayout(new GridBagLayout());
115:
116: // Entity covers entire row
117: GridBagConstraints labelC = new GridBagConstraints();
118: labelC.gridx = 0;
119: labelC.gridy = GridBagConstraints.RELATIVE;
120: labelC.anchor = GridBagConstraints.WEST;
121: labelC.fill = GridBagConstraints.HORIZONTAL;
122: labelC.insets = new Insets(4, 15, 4, 15);
123:
124: // Initial label
125: GridBagConstraints firstC = new GridBagConstraints();
126: firstC.gridx = 0;
127: firstC.gridy = GridBagConstraints.RELATIVE;
128: firstC.gridwidth = 1;
129: firstC.anchor = GridBagConstraints.WEST;
130: firstC.insets = new Insets(4, 15, 4, 0);
131:
132: // Text field
133: GridBagConstraints tfC = new GridBagConstraints();
134: tfC.gridx = GridBagConstraints.RELATIVE;
135: tfC.gridy = 0;
136: tfC.gridwidth = 7;
137: tfC.fill = GridBagConstraints.HORIZONTAL;
138: tfC.insets = new Insets(4, 0, 4, 15);
139:
140: // Text area
141: GridBagConstraints taC = new GridBagConstraints();
142: taC.gridx = 0;
143: taC.gridy = GridBagConstraints.RELATIVE;
144: taC.gridheight = 3;
145: taC.gridwidth = 8;
146: taC.weightx = 1.0;
147: taC.weighty = 1.0;
148: taC.fill = GridBagConstraints.BOTH;
149: taC.anchor = GridBagConstraints.WEST;
150: taC.insets = new Insets(0, 15, 4, 15);
151:
152: JLabel nameLabel = new JLabel();
153: nameLabel.setDisplayedMnemonic(NbBundle.getMessage(
154: ParamEditor.class, "MON_Name_Mnemonic").charAt(0));
155: nameLabel.getAccessibleContext().setAccessibleDescription(
156: NbBundle.getMessage(ParamEditor.class,
157: "ACS_MON_NameA11yDesc"));
158: if (name == null || name.length() == 0)
159: getAccessibleContext().setAccessibleDescription(title);
160: else
161: getAccessibleContext().setAccessibleDescription(
162: NbBundle.getMessage(ParamEditor.class,
163: "ACS_ParamEditorA11yDesc", title, name)); //NOI18N
164: String text = NbBundle
165: .getMessage(ParamEditor.class, "MON_Name");
166: text = text.concat(": "); //NOI18N
167:
168: if (editable == Editable.BOTH) {
169: final JTextField nameText = new JTextField(25);
170: nameText.addFocusListener(new FocusListener() {
171: public void focusGained(FocusEvent evt) {
172: }
173:
174: public void focusLost(FocusEvent evt) {
175: name = nameText.getText();
176: }
177: });
178: nameLabel.setLabelFor(nameText);
179: nameText.getAccessibleContext().setAccessibleName(
180: NbBundle.getMessage(ParamEditor.class,
181: "ACS_MON_NameTextFieldA11yName"));
182: nameText
183: .setToolTipText(NbBundle.getMessage(
184: ParamEditor.class,
185: "ACS_MON_NameTextFieldA11yDesc"));
186: nameText.setText(name);
187: nameText.setBackground(java.awt.Color.white);
188: nameText.setEditable(editable == Editable.BOTH);
189:
190: this .add(nameLabel, firstC);
191: this .add(nameText, tfC);
192: } else {
193: this .add(nameLabel, labelC);
194: text = text.concat(name);
195: }
196: nameLabel.setText(text);
197:
198: JLabel valueLabel = new JLabel();
199: valueLabel.setText(NbBundle.getMessage(ParamEditor.class,
200: "MON_Value").concat(":"));
201: valueLabel.setDisplayedMnemonic(NbBundle.getMessage(
202: ParamEditor.class, "MON_Value_Mnemonic").charAt(0));
203: valueLabel.getAccessibleContext().setAccessibleDescription(
204: NbBundle.getMessage(ParamEditor.class,
205: "ACS_MON_ValueA11yDesc"));
206: firstC.gridy++;
207: this .add(valueLabel, labelC);
208:
209: final JTextArea valueText = new JTextArea();
210: valueText.addFocusListener(new FocusListener() {
211: public void focusGained(FocusEvent evt) {
212: }
213:
214: public void focusLost(FocusEvent evt) {
215: value = valueText.getText();
216: }
217: });
218: valueLabel.setLabelFor(valueText);
219: valueText.getAccessibleContext().setAccessibleName(
220: NbBundle.getMessage(ParamEditor.class,
221: "ACS_MON_ValueTextAreaA11yName"));
222: valueText.setToolTipText(NbBundle.getMessage(ParamEditor.class,
223: "ACS_MON_ValueTextAreaA11yDesc"));
224: if (editable == Editable.NEITHER) {
225: valueText.setEditable(false);
226: valueText.setBackground(this .getBackground());
227: valueText.setForeground(Color.BLACK);
228: valueText.setBorder(BorderFactory
229: .createLoweredBevelBorder());
230: }
231: valueText.setText(value);
232: valueText.setLineWrap(true);
233: valueText.setWrapStyleWord(false);
234:
235: JScrollPane scrollpane = new JScrollPane(valueText);
236: scrollpane.setPreferredSize(valueSize);
237: //scrollpane.setViewportBorder(BorderFactory.createLoweredBevelBorder());
238: this .add(scrollpane, taC);
239:
240: // Housekeeping
241: // this.setMaximumSize(this.getPreferredSize());
242: this .repaint();
243: }
244:
245: public void showDialog() {
246:
247: if (editable == Editable.NEITHER) {
248: NotifyDescriptor d = new NotifyDescriptor(this , title,
249: NotifyDescriptor.DEFAULT_OPTION,
250: NotifyDescriptor.PLAIN_MESSAGE,
251: new Object[] { NotifyDescriptor.OK_OPTION },
252: NotifyDescriptor.OK_OPTION);
253: DialogDisplayer.getDefault().notify(d);
254: } else {
255: editDialog = new DialogDescriptor(this , title, true,
256: DialogDescriptor.OK_CANCEL_OPTION,
257: DialogDescriptor.CANCEL_OPTION,
258: new ActionListener() {
259: public void actionPerformed(ActionEvent e) {
260: evaluateInput();
261: }
262: });
263:
264: dialog = DialogDisplayer.getDefault().createDialog(
265: editDialog);
266: dialog.setVisible(true);
267: this .repaint();
268: }
269: }
270:
271: /**
272: * Handle user input...
273: */
274:
275: public void evaluateInput() {
276:
277: if (editDialog.getValue()
278: .equals(NotifyDescriptor.CANCEL_OPTION)) {
279: dialog.dispose();
280: dialogOK = false;
281: return;
282: }
283:
284: if (editable == Editable.NEITHER) {
285: dialog.dispose();
286: dialogOK = false;
287: return;
288: }
289:
290: errorMessage = null;
291:
292: if (name.equals(""))
293: errorMessage = NbBundle.getMessage(ParamEditor.class,
294: "MSG_no_name");
295: else if (condition == Condition.COOKIE
296: && name.equalsIgnoreCase("jsessionid"))
297: errorMessage = NbBundle.getMessage(ParamEditor.class,
298: "MSG_no_jsession");
299: else if (condition == Condition.VALUE && value.equals(""))
300: errorMessage = NbBundle.getMessage(ParamEditor.class,
301: "MSG_no_value");
302: else if (condition == Condition.HEADER
303: && name.equalsIgnoreCase("cookie")
304: && (value.indexOf("jsessionid") > -1 || value
305: .indexOf("JSESSIONID") > -1))
306: errorMessage = NbBundle.getMessage(ParamEditor.class,
307: "MSG_no_jsession");
308:
309: if (errorMessage == null) {
310: dialog.dispose();
311: dialogOK = true;
312: } else {
313: editDialog.setValue(NotifyDescriptor.CLOSED_OPTION);
314: NotifyDescriptor nd = new NotifyDescriptor.Message(
315: errorMessage, NotifyDescriptor.ERROR_MESSAGE);
316: DialogDisplayer.getDefault().notify(nd);
317: }
318: }
319:
320: // Do we need this?
321: public void repaint() {
322: super .repaint();
323: if (dialog != null && !repainting) {
324: repainting = true;
325: dialog.repaint();
326: repainting = false;
327: }
328: }
329:
330: static class Editable {
331: private String editable;
332:
333: private Editable(String editable) {
334: this .editable = editable;
335: }
336:
337: public String toString() {
338: return editable;
339: }
340:
341: public static final Editable BOTH = new Editable("both");
342: public static final Editable VALUE = new Editable("value");
343: public static final Editable NEITHER = new Editable("neither");
344: }
345:
346: static class Condition {
347: private String condition;
348:
349: private Condition(String condition) {
350: this .condition = condition;
351: }
352:
353: public String toString() {
354: return condition;
355: }
356:
357: public static final Condition NONE = new Condition("none");
358: public static final Condition VALUE = new Condition("value");
359: public static final Condition COOKIE = new Condition("cookie");
360: public static final Condition HEADER = new Condition("header");
361: }
362:
363: } // ParamEditor
|