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-2006 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 Ana von Klopp
044: */package org.netbeans.modules.web.wizards;
045:
046: import java.awt.Color;
047: import java.awt.Dialog;
048: import java.awt.Dimension;
049: import java.awt.Insets;
050: import java.awt.GridBagConstraints;
051: import java.awt.GridBagLayout;
052: import java.awt.event.ActionEvent;
053: import java.awt.event.ActionListener;
054: import java.awt.event.FocusEvent;
055: import java.awt.event.FocusListener;
056:
057: import javax.swing.BorderFactory;
058: import javax.swing.JLabel;
059: import javax.swing.JScrollPane;
060: import javax.swing.JTextArea;
061: import javax.swing.JTextField;
062:
063: import org.openide.DialogDisplayer;
064: import org.openide.DialogDescriptor;
065: import org.openide.NotifyDescriptor;
066: import org.openide.util.NbBundle;
067:
068: public class TableRowDialog extends javax.swing.JPanel {
069:
070: private final static boolean debug = false;
071:
072: // Do we need this to close it?
073: private Dialog dialog = null;
074: private DialogDescriptor editDialog = null;
075: private String errorMessage = null;
076: private boolean dialogOK = false;
077:
078: // Hold the name and value until the widgets are created.
079: private String name = ""; //NOI18N
080: private String value = ""; //NOI18N
081: private Editable editable;
082: private Condition condition;
083: private String title = ""; //NOI18N
084:
085: //private static boolean repainting = false;
086: private boolean repainting = false;
087:
088: private static final long serialVersionUID = -855447534116444417L;
089:
090: public TableRowDialog(String name, String value, Editable e,
091: Condition c, String title) {
092: if (debug)
093: log("::CONSTRUCTOR");
094: this .name = name;
095: this .value = value;
096: this .editable = e;
097: if (debug)
098: log("\tEditable: " + e.toString());
099: this .condition = c;
100: if (debug)
101: log("\tCondition: " + c.toString());
102: this .title = title;
103: initialize();
104: }
105:
106: public boolean getDialogOK() {
107: if (debug) {
108: log("::getDialogOK()"); //NOI18N
109: log("\tdialogOK = " + String.valueOf(dialogOK)); //NOI18N
110: }
111: return dialogOK;
112: }
113:
114: public String getName() {
115: return name;
116: }
117:
118: public String getValue() {
119: return value;
120: }
121:
122: public void initialize() {
123:
124: if (debug)
125: System.out.println("in (new) TableRowDialog.initialize()"); //NOI18N
126: this .setLayout(new GridBagLayout());
127:
128: // Entity covers entire row
129: GridBagConstraints labelC = new GridBagConstraints();
130: labelC.gridx = 0;
131: labelC.gridy = GridBagConstraints.RELATIVE;
132: labelC.anchor = GridBagConstraints.WEST;
133: labelC.fill = GridBagConstraints.HORIZONTAL;
134: labelC.insets = new Insets(4, 15, 4, 15);
135:
136: // Initial label
137: GridBagConstraints firstC = new GridBagConstraints();
138: firstC.gridx = 0;
139: firstC.gridy = GridBagConstraints.RELATIVE;
140: firstC.gridwidth = 1;
141: firstC.anchor = GridBagConstraints.WEST;
142: firstC.insets = new Insets(4, 15, 4, 0);
143:
144: // Text field
145: GridBagConstraints tfC = new GridBagConstraints();
146: tfC.gridx = GridBagConstraints.RELATIVE;
147: tfC.gridy = 0;
148: tfC.gridwidth = 7;
149: tfC.fill = GridBagConstraints.HORIZONTAL;
150: tfC.insets = new Insets(4, 0, 4, 15);
151:
152: // Text area
153: GridBagConstraints taC = new GridBagConstraints();
154: taC.gridx = 0;
155: taC.gridy = GridBagConstraints.RELATIVE;
156: taC.gridheight = 3;
157: taC.gridwidth = 8;
158: taC.fill = GridBagConstraints.BOTH;
159: taC.anchor = GridBagConstraints.WEST;
160: taC.insets = new Insets(0, 15, 4, 15);
161:
162: JLabel nameLabel = new JLabel();
163: nameLabel.setDisplayedMnemonic(NbBundle.getMessage(
164: TableRowDialog.class, "LBL_paramname_mnemonic").charAt(
165: 0));
166: nameLabel.getAccessibleContext().setAccessibleDescription(
167: NbBundle.getMessage(TableRowDialog.class,
168: "ACSD_paramname"));
169:
170: String text = NbBundle.getMessage(TableRowDialog.class,
171: "LBL_paramname");
172: text = text.concat(": "); //NOI18N
173:
174: if (editable == Editable.BOTH) {
175: final JTextField nameText = new JTextField(25);
176: nameText.addFocusListener(new FocusListener() {
177: public void focusGained(FocusEvent evt) {
178: }
179:
180: public void focusLost(FocusEvent evt) {
181: name = nameText.getText();
182: }
183: });
184: nameLabel.setLabelFor(nameText);
185: nameText.getAccessibleContext().setAccessibleName(
186: NbBundle.getMessage(TableRowDialog.class,
187: "ACSD_paramname"));
188: nameText.setText(name);
189: nameText.setBackground(java.awt.Color.white);
190: nameText.setEditable(editable == Editable.BOTH);
191:
192: this .add(nameLabel, firstC);
193: this .add(nameText, tfC);
194: } else {
195: this .add(nameLabel, labelC);
196: text = text.concat(name);
197: }
198: nameLabel.setText(text);
199:
200: JLabel valueLabel = new JLabel();
201: valueLabel.setText(NbBundle.getMessage(TableRowDialog.class,
202: "LBL_paramvalue").concat(":"));
203: valueLabel.setDisplayedMnemonic(NbBundle.getMessage(
204: TableRowDialog.class, "LBL_paramvalue_mnemonic")
205: .charAt(0));
206: valueLabel.getAccessibleContext().setAccessibleDescription(
207: NbBundle.getMessage(TableRowDialog.class,
208: "ACSD_paramvalue"));
209: firstC.gridy++;
210: this .add(valueLabel, labelC);
211:
212: final JTextArea valueText = new JTextArea();
213: valueText.addFocusListener(new FocusListener() {
214: public void focusGained(FocusEvent evt) {
215: }
216:
217: public void focusLost(FocusEvent evt) {
218: value = valueText.getText();
219: }
220: });
221: valueLabel.setLabelFor(valueText);
222: valueText.getAccessibleContext().setAccessibleName(
223: NbBundle.getMessage(TableRowDialog.class,
224: "ACSD_paramvalue"));
225:
226: if (editable == Editable.NEITHER) {
227: valueText.setEditable(false);
228: valueText.setBackground(this .getBackground().darker());
229: valueText.setForeground(Color.BLACK);
230: valueText.setBorder(BorderFactory
231: .createLoweredBevelBorder());
232: }
233: valueText.setText(value);
234: valueText.setLineWrap(true);
235: valueText.setWrapStyleWord(false);
236:
237: JScrollPane scrollpane = new JScrollPane(valueText);
238: //scrollpane.setViewportBorder(BorderFactory.createLoweredBevelBorder());
239: this .add(scrollpane, taC);
240:
241: // Housekeeping
242: // this.setMaximumSize(this.getPreferredSize());
243: this .repaint();
244: }
245:
246: public void showDialog() {
247:
248: if (editable == Editable.NEITHER) {
249: if (debug)
250: log("Non-modal dialog, OK option only");
251: NotifyDescriptor d = new NotifyDescriptor(this , title,
252: NotifyDescriptor.DEFAULT_OPTION,
253: NotifyDescriptor.PLAIN_MESSAGE,
254: new Object[] { NotifyDescriptor.OK_OPTION },
255: NotifyDescriptor.OK_OPTION);
256: DialogDisplayer.getDefault().notify(d);
257: } else {
258: editDialog = new DialogDescriptor(this , title, true,
259: DialogDescriptor.OK_CANCEL_OPTION,
260: DialogDescriptor.CANCEL_OPTION,
261: new ActionListener() {
262: public void actionPerformed(ActionEvent e) {
263: evaluateInput();
264: }
265: });
266:
267: dialog = DialogDisplayer.getDefault().createDialog(
268: editDialog);
269: dialog.getAccessibleContext().setAccessibleDescription(
270: NbBundle.getMessage(TableRowDialog.class,
271: "ACSD_initparam_edit")); // NOI18N
272:
273: dialog.show();
274: this .repaint();
275: }
276: }
277:
278: /**
279: * Handle user input...
280: */
281:
282: public void evaluateInput() {
283:
284: if (debug) {
285: log("::evaluateInput()");//NOI18N
286: }
287:
288: if (editDialog.getValue()
289: .equals(NotifyDescriptor.CANCEL_OPTION)) {
290: if (debug)
291: log("\tGot cancel"); //NOI18N
292: dialog.dispose();
293: dialogOK = false;
294: return;
295: }
296:
297: if (debug)
298: log("\tGot OK"); //NOI18N
299: if (editable == Editable.NEITHER) {
300: if (debug)
301: log("\tNot editable"); //NOI18N
302: dialog.dispose();
303: dialogOK = false;
304: return;
305: }
306: if (debug)
307: log("Name is " + name);
308: if (debug)
309: log("Value is " + value);
310:
311: errorMessage = null;
312:
313: if (name.equals(""))
314: errorMessage = NbBundle.getMessage(TableRowDialog.class,
315: "MSG_no_name");
316:
317: else if (condition == Condition.VALUE && value.equals(""))
318: errorMessage = NbBundle.getMessage(TableRowDialog.class,
319: "MSG_no_value");
320:
321: if (debug)
322: log("ErrorMessage: " + errorMessage);
323:
324: if (errorMessage == null) {
325: dialog.dispose();
326: dialogOK = true;
327: } else {
328: editDialog.setValue(NotifyDescriptor.CLOSED_OPTION);
329: NotifyDescriptor nd = new NotifyDescriptor.Message(
330: errorMessage, NotifyDescriptor.ERROR_MESSAGE);
331: DialogDisplayer.getDefault().notify(nd);
332: }
333: }
334:
335: // Do we need this?
336: public void repaint() {
337: super .repaint();
338: if (dialog != null && !repainting) {
339: repainting = true;
340: dialog.repaint();
341: repainting = false;
342: }
343: }
344:
345: private void log(String s) {
346: System.out.println("TableRowDialog" + s);
347: }
348:
349: static class Condition {
350: private String condition;
351:
352: private Condition(String condition) {
353: this .condition = condition;
354: }
355:
356: public String toString() {
357: return condition;
358: }
359:
360: public static final Condition NONE = new Condition("none");
361: public static final Condition VALUE = new Condition("value");
362: }
363:
364: } // TableRowDialog
|