KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > web > wizards > TableRowDialog


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-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 /**
21  * @author Ana von Klopp
22  */

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

65     //private static boolean repainting = false;
66
private boolean repainting = false;
67
68     private static final long serialVersionUID = -855447534116444417L;
69     
70     public TableRowDialog(String JavaDoc name, String JavaDoc value, Editable e, Condition c,
71                String JavaDoc title) {
72     if(debug) log("::CONSTRUCTOR");
73     this.name = name;
74     this.value = value;
75     this.editable = e;
76     if(debug) log("\tEditable: " + e.toString());
77     this.condition = c;
78     if(debug) log("\tCondition: " + c.toString());
79     this.title = title;
80     initialize();
81     }
82
83     public boolean getDialogOK() {
84     if(debug) {
85         log("::getDialogOK()"); //NOI18N
86
log("\tdialogOK = " + String.valueOf(dialogOK)); //NOI18N
87
}
88     return dialogOK;
89     }
90
91     public String JavaDoc getName() {
92     return name;
93     }
94
95     public String JavaDoc getValue() {
96     return value;
97     }
98     
99     public void initialize() {
100
101     if(debug) System.out.println("in (new) TableRowDialog.initialize()"); //NOI18N
102
this.setLayout(new GridBagLayout JavaDoc());
103
104     // Entity covers entire row
105
GridBagConstraints JavaDoc labelC = new GridBagConstraints JavaDoc();
106     labelC.gridx = 0;
107     labelC.gridy = GridBagConstraints.RELATIVE;
108     labelC.anchor = GridBagConstraints.WEST;
109     labelC.fill = GridBagConstraints.HORIZONTAL;
110     labelC.insets = new Insets JavaDoc(4, 15, 4, 15);
111
112     // Initial label
113
GridBagConstraints JavaDoc firstC = new GridBagConstraints JavaDoc();
114     firstC.gridx = 0;
115     firstC.gridy = GridBagConstraints.RELATIVE;
116     firstC.gridwidth = 1;
117     firstC.anchor = GridBagConstraints.WEST;
118     firstC.insets = new Insets JavaDoc(4, 15, 4, 0);
119
120     // Text field
121
GridBagConstraints JavaDoc tfC = new GridBagConstraints JavaDoc();
122     tfC.gridx = GridBagConstraints.RELATIVE;
123     tfC.gridy = 0;
124     tfC.gridwidth = 7;
125     tfC.fill = GridBagConstraints.HORIZONTAL;
126     tfC.insets = new Insets JavaDoc(4, 0, 4, 15);
127
128     // Text area
129
GridBagConstraints JavaDoc taC = new GridBagConstraints JavaDoc();
130     taC.gridx = 0;
131     taC.gridy = GridBagConstraints.RELATIVE;
132     taC.gridheight = 3;
133     taC.gridwidth = 8;
134     taC.fill = GridBagConstraints.BOTH;
135     taC.anchor = GridBagConstraints.WEST;
136     taC.insets = new Insets JavaDoc(0, 15, 4, 15);
137
138         JLabel JavaDoc nameLabel = new JLabel JavaDoc();
139         nameLabel.setDisplayedMnemonic(NbBundle.getMessage(TableRowDialog.class, "LBL_paramname_mnemonic").charAt(0));
140         nameLabel.getAccessibleContext().setAccessibleDescription(NbBundle.getMessage(TableRowDialog.class, "ACSD_paramname"));
141
142     String JavaDoc text = NbBundle.getMessage(TableRowDialog.class,
143                       "LBL_paramname");
144     text = text.concat(": "); //NOI18N
145

146     if(editable == Editable.BOTH) {
147         final JTextField JavaDoc nameText = new JTextField JavaDoc(25);
148         nameText.addFocusListener(new FocusListener JavaDoc() {
149         public void focusGained(FocusEvent JavaDoc evt) {
150         }
151         public void focusLost(FocusEvent JavaDoc evt) {
152             name = nameText.getText();
153         }
154         });
155         nameLabel.setLabelFor(nameText);
156         nameText.getAccessibleContext().setAccessibleName(NbBundle.getMessage(TableRowDialog.class, "ACSD_paramname"));
157         nameText.setText(name);
158         nameText.setBackground(java.awt.Color.white);
159         nameText.setEditable(editable == Editable.BOTH);
160
161         this.add(nameLabel, firstC);
162         this.add(nameText, tfC);
163     }
164     else {
165         this.add(nameLabel, labelC);
166         text = text.concat(name);
167     }
168     nameLabel.setText(text);
169         
170     JLabel JavaDoc valueLabel = new JLabel JavaDoc();
171     valueLabel.setText(NbBundle.getMessage(TableRowDialog.class, "LBL_paramvalue").concat(":"));
172         valueLabel.setDisplayedMnemonic(NbBundle.getMessage(TableRowDialog.class, "LBL_paramvalue_mnemonic").charAt(0));
173         valueLabel.getAccessibleContext().setAccessibleDescription(NbBundle.getMessage(TableRowDialog.class, "ACSD_paramvalue"));
174     firstC.gridy++;
175     this.add(valueLabel, labelC);
176
177     final JTextArea JavaDoc valueText = new JTextArea JavaDoc();
178     valueText.addFocusListener(new FocusListener JavaDoc() {
179         public void focusGained(FocusEvent JavaDoc evt) {
180         }
181         public void focusLost(FocusEvent JavaDoc evt) {
182             value = valueText.getText();
183         }
184         });
185     valueLabel.setLabelFor(valueText);
186         valueText.getAccessibleContext().setAccessibleName(NbBundle.getMessage(TableRowDialog.class, "ACSD_paramvalue"));
187
188     if(editable == Editable.NEITHER) {
189         valueText.setEditable(false);
190         valueText.setBackground(this.getBackground().darker());
191         valueText.setForeground(Color.BLACK);
192         valueText.setBorder(BorderFactory.createLoweredBevelBorder());
193     }
194     valueText.setText(value);
195     valueText.setLineWrap(true);
196     valueText.setWrapStyleWord(false);
197     
198     JScrollPane JavaDoc scrollpane = new JScrollPane JavaDoc(valueText);
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         if(debug) log("Non-modal dialog, OK option only");
211         NotifyDescriptor d =
212         new NotifyDescriptor(this, title,
213                      NotifyDescriptor.DEFAULT_OPTION,
214                      NotifyDescriptor.PLAIN_MESSAGE,
215                      new Object JavaDoc[] { NotifyDescriptor.OK_OPTION },
216                      NotifyDescriptor.OK_OPTION);
217         DialogDisplayer.getDefault().notify(d);
218     }
219     else {
220         editDialog = new DialogDescriptor
221         (this, title, true, DialogDescriptor.OK_CANCEL_OPTION,
222          DialogDescriptor.CANCEL_OPTION,
223          new ActionListener JavaDoc() {
224              public void actionPerformed(ActionEvent JavaDoc e) {
225              evaluateInput();
226              }
227          });
228
229         dialog = DialogDisplayer.getDefault().createDialog(editDialog);
230         dialog.getAccessibleContext().setAccessibleDescription(NbBundle.getMessage(TableRowDialog.class, "ACSD_initparam_edit")); // NOI18N
231

232         dialog.show();
233         this.repaint();
234     }
235     }
236
237     /**
238      * Handle user input...
239      */

240
241     public void evaluateInput() {
242
243     if (debug) {
244         log("::evaluateInput()");//NOI18N
245
}
246
247     if (editDialog.getValue().equals(NotifyDescriptor.CANCEL_OPTION)) {
248         if(debug) log("\tGot cancel"); //NOI18N
249
dialog.dispose();
250         dialogOK = false;
251         return;
252     }
253
254     if(debug) log("\tGot OK"); //NOI18N
255
if(editable == Editable.NEITHER) {
256         if(debug) log("\tNot editable"); //NOI18N
257
dialog.dispose();
258         dialogOK = false;
259         return;
260     }
261     if(debug) log("Name is " + name);
262     if(debug) log("Value is " + value);
263
264     errorMessage = null;
265
266     if(name.equals(""))
267         errorMessage = NbBundle.getMessage(TableRowDialog.class,
268                            "MSG_no_name");
269
270     else if(condition == Condition.VALUE && value.equals(""))
271         errorMessage = NbBundle.getMessage(TableRowDialog.class,
272                            "MSG_no_value");
273
274     if(debug) log("ErrorMessage: " + errorMessage);
275
276     if(errorMessage == null) {
277         dialog.dispose();
278         dialogOK = true;
279     }
280     else {
281        editDialog.setValue(NotifyDescriptor.CLOSED_OPTION);
282        NotifyDescriptor nd = new NotifyDescriptor.Message
283            (errorMessage, NotifyDescriptor.ERROR_MESSAGE);
284         DialogDisplayer.getDefault().notify(nd);
285     }
286     }
287
288
289     // Do we need this?
290
public void repaint() {
291     super.repaint();
292     if (dialog != null && !repainting) {
293         repainting = true;
294         dialog.repaint();
295         repainting = false;
296     }
297     }
298
299     private void log(String JavaDoc s) {
300     System.out.println("TableRowDialog" + s);
301     }
302
303     static class Condition {
304     private String JavaDoc condition;
305
306     private Condition(String JavaDoc condition) {
307         this.condition = condition;
308     }
309     
310     public String JavaDoc toString() { return condition; }
311
312     public static final Condition NONE = new Condition("none");
313     public static final Condition VALUE = new Condition("value");
314     }
315
316 } // TableRowDialog
317
Popular Tags