KickJava   Java API By Example, From Geeks To Geeks.

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


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 package org.netbeans.modules.web.wizards;
21
22 import java.awt.Dimension JavaDoc;
23 import java.awt.GridBagConstraints JavaDoc;
24 import java.awt.Insets JavaDoc;
25 import java.awt.event.ActionEvent JavaDoc;
26 import java.awt.event.ActionListener JavaDoc;
27
28 import javax.swing.JButton JavaDoc;
29 import javax.swing.JLabel JavaDoc;
30 import javax.swing.JPanel JavaDoc;
31 import javax.swing.JScrollPane JavaDoc;
32 import javax.swing.ListSelectionModel JavaDoc;
33 import javax.swing.event.ListSelectionEvent JavaDoc;
34 import javax.swing.event.ListSelectionListener JavaDoc;
35 import javax.swing.event.TableModelEvent JavaDoc;
36 import javax.swing.event.TableModelListener JavaDoc;
37
38
39 import org.openide.util.NbBundle;
40
41 /*
42  * Wizard panel that collects deployment data for Servlets and Filters
43  * @author Ana von Klopp
44  */

45
46 class InitParamPanel extends JPanel JavaDoc implements ActionListener JavaDoc,
47                         ListSelectionListener JavaDoc {
48
49     private final static String JavaDoc ADD = "add";
50     private final static String JavaDoc EDIT = "edit";
51     private final static String JavaDoc REMOVE = "remove";
52     private ServletData deployData;
53     private BaseWizardPanel parent;
54     private boolean edited = false;
55
56     private JLabel JavaDoc jLinitparams;
57     private DDTable table;
58     private JButton JavaDoc jBnew;
59     private JButton JavaDoc jBedit;
60     private JButton JavaDoc jBdelete;
61     private JScrollPane JavaDoc scrollP;
62
63     private static final boolean debug = false;
64
65     private static final long serialVersionUID = -5803905591685582710L;
66     
67     public InitParamPanel(ServletData deployData, BaseWizardPanel parent) {
68     this.deployData = deployData;
69     this.parent = parent;
70     initComponents ();
71     }
72
73     private void initComponents () {
74
75     // Layout description
76
setLayout(new java.awt.GridBagLayout JavaDoc());
77
78     // Entity covers entire row
79
GridBagConstraints JavaDoc fullRowC = new GridBagConstraints JavaDoc();
80     fullRowC.gridx = 0;
81         fullRowC.gridy = 0;
82     fullRowC.gridwidth = 2;
83     fullRowC.anchor = GridBagConstraints.WEST;
84     fullRowC.fill = GridBagConstraints.HORIZONTAL;
85     fullRowC.insets = new Insets JavaDoc(4, 0, 4, 60);
86
87     // Button
88
GridBagConstraints JavaDoc bC = new GridBagConstraints JavaDoc();
89     bC.gridx = 1;
90         bC.gridy = 1;
91     bC.weightx = 0.1;
92     bC.fill = GridBagConstraints.HORIZONTAL;
93     bC.insets = new Insets JavaDoc(4, 20, 4, 60);
94
95     // Table panel
96
GridBagConstraints JavaDoc tableC = new GridBagConstraints JavaDoc();
97     tableC.gridx = 0;
98         tableC.gridy = 1;
99     tableC.gridheight = 4;
100     tableC.fill = GridBagConstraints.BOTH;
101     tableC.weightx = 0.9;
102     tableC.weighty = 1.0;
103     tableC.anchor = GridBagConstraints.WEST;
104     tableC.insets = new Insets JavaDoc(4, 0, 4, 0);
105
106     // Filler panel
107
GridBagConstraints JavaDoc fillerC = new GridBagConstraints JavaDoc();
108     fillerC.gridx = 1;
109         fillerC.gridy = GridBagConstraints.RELATIVE;
110     fillerC.fill = GridBagConstraints.BOTH;
111     fillerC.weighty = 1.0;
112     fillerC.insets = new Insets JavaDoc(4, 0, 4, 0);
113         
114     // Component Initialization by row
115
// 1. Init parameter
116
jLinitparams = new JLabel JavaDoc(NbBundle.getMessage(InitParamPanel.class, "LBL_initparamsL"));
117     jLinitparams.setDisplayedMnemonic(NbBundle.getMessage (InitParamPanel.class, "LBL_initparams_mnemonic").charAt(0));
118     // PENDING
119
this.add(jLinitparams, fullRowC);
120
121     // 2. Table row
122

123     String JavaDoc[] headers = { "paramname", "paramvalue" };
124     table = new DDTable(headers,
125                 "LBL_initparams",
126                 Editable.BOTH);
127
128     jLinitparams.setLabelFor(table);
129
130     // Enable the buttons according to the row selected
131
table.getSelectionModel().addListSelectionListener(this);
132     table.getAccessibleContext().setAccessibleDescription(NbBundle.getMessage(InitParamPanel.class, "ACSD_initparams_desc")); // NOI18N
133
table.getAccessibleContext().setAccessibleName(NbBundle.getMessage(InitParamPanel.class, "ACSD_initparams")); // NOI18N
134

135
136     table.getModel().addTableModelListener(new TableModelListener JavaDoc() {
137         public void tableChanged(TableModelEvent JavaDoc evt) {
138         if(debug) log("\ttable model changed"); //NOI18N
139
updateInitParams();
140         //table.revalidate();
141
//scrollP.revalidate();
142
//scrollP.repaint();
143
}});
144
145     scrollP = new JScrollPane JavaDoc(table);
146     this.add(scrollP, tableC);
147
148     jBnew = new JButton JavaDoc();
149     jBnew.setText(NbBundle.getMessage(InitParamPanel.class,
150                       "LBL_new"));
151     jBnew.setMnemonic(NbBundle.getMessage(InitParamPanel.class, "LBL_new_mnemonic").charAt(0));
152     jBnew.addActionListener(this);
153     jBnew.setActionCommand(ADD);
154     jBnew.getAccessibleContext().setAccessibleDescription(NbBundle.getMessage(InitParamPanel.class, "ACSD_initparam_new")); // NOI18N
155
this.add(jBnew, bC);
156
157     bC.gridy++;
158     jBedit = new JButton JavaDoc();
159     jBedit.setText(NbBundle.getMessage(InitParamPanel.class,
160                          "LBL_edit"));
161     jBedit.setMnemonic(NbBundle.getMessage(InitParamPanel.class, "LBL_edit_mnemonic").charAt(0));
162     jBedit.addActionListener(this);
163     jBedit.setActionCommand(EDIT);
164     jBedit.getAccessibleContext().setAccessibleDescription(NbBundle.getMessage(InitParamPanel.class, "ACSD_initparam_edit")); // NOI18N
165
jBedit.setEnabled(false);
166     this.add(jBedit, bC);
167
168     bC.gridy++;
169     jBdelete = new JButton JavaDoc();
170     jBdelete.setText(NbBundle.getMessage(InitParamPanel.class,
171                          "LBL_delete"));
172     jBdelete.setMnemonic(NbBundle.getMessage(InitParamPanel.class, "LBL_delete_mnemonic").charAt(0));
173     jBdelete.addActionListener(this);
174     jBdelete.setActionCommand(REMOVE);
175     jBdelete.getAccessibleContext().setAccessibleDescription(NbBundle.getMessage(InitParamPanel.class, "ACSD_initparam_delete")); // NOI18N
176
jBdelete.setEnabled(false);
177     this.add(jBdelete, bC);
178         
179         this.add(new javax.swing.JPanel JavaDoc(),fillerC);
180
181     }
182
183     public void setEnabled() {
184     
185     boolean enable = deployData.makeEntry();
186     
187     jLinitparams.setEnabled(enable);
188     jBnew.setEnabled(enable);
189     if(enable) {
190         if(debug) {
191         log("\tnumRows: " + table.getRowCount()); //NOI18N
192
log("\tselected row: " + table.getSelectedRow()); //NOI18N
193
}
194         ListSelectionModel JavaDoc lsm = table.getSelectionModel();
195         if (lsm.isSelectionEmpty()) {
196         jBdelete.setEnabled(false);
197         jBedit.setEnabled(false);
198         }
199         else {
200         jBdelete.setEnabled(true);
201         jBedit.setEnabled(true);
202         }
203     }
204     else {
205         jBdelete.setEnabled(false);
206         jBedit.setEnabled(false);
207     }
208     table.setEditable(enable ? Editable.BOTH : Editable.NEITHER);
209     }
210
211     public void actionPerformed(ActionEvent JavaDoc evt) {
212
213     if(debug) log("::actionPerformed()"); //NOI18n
214
int row = -1;
215     if(evt.getSource() instanceof JButton JavaDoc) {
216         if(evt.getActionCommand() == ADD) {
217         if(debug) log("\tAdding row"); //NOI18N
218
String JavaDoc[] values= {
219             NbBundle.getMessage(InitParamPanel.class, "LBL_paramname"),
220             NbBundle.getMessage(InitParamPanel.class, "LBL_paramvalue"),
221         };
222         row = table.addRow(values);
223         table.setRowSelectionInterval(row, row);
224         if(debug) log("\tAdded row " + row);
225         }
226         else if (evt.getActionCommand() == REMOVE) {
227         if(debug) log("\tDeleting row"); //NOI18N
228
row = table.getSelectedRow();
229         table.removeRow(row);
230                 setEnabled();
231         }
232         else if (evt.getActionCommand() == EDIT) {
233         if(debug) log("\tEditing row"); //NOI18N
234
//table.stopEditing();
235
row = table.getSelectedRow();
236         String JavaDoc name = (String JavaDoc)(table.getValueAt(row, 0));
237         String JavaDoc value = (String JavaDoc)(table.getValueAt(row, 1));
238         String JavaDoc title = NbBundle.getMessage(DDTable.class,
239                             "LBL_initparams_edit"); //NOI18N
240
TableRowDialog d =
241             new TableRowDialog(name, value, Editable.BOTH,
242                        TableRowDialog.Condition.NONE,
243                        title);
244         d.showDialog();
245         if(d.getDialogOK()) {
246             if(debug) log("Dialog is OK");
247             table.setData(d.getName(), d.getValue(), row);
248         }
249         else
250             table.setData(name, value, row);
251         }
252         scrollP.revalidate();
253     }
254     }
255
256     public void valueChanged(ListSelectionEvent JavaDoc e) {
257     if(debug) log("::valueChanged()"); //NOI18N
258
//Ignore extra messages.
259
if (e.getValueIsAdjusting()) return;
260     setEnabled();
261     updateInitParams();
262     }
263
264     private void updateInitParams() {
265
266     if(debug) log("::updateInitParams()");
267     edited = true;
268
269     if(deployData.makeEntry()) {
270
271         if(debug) {
272         log("\tnumRows: " + table.getRowCount()); //NOI18N
273
log("\tselected row: " + table.getSelectedRow()); //NOI18N
274
}
275
276         int numInitParams = table.getRowCount();
277         String JavaDoc[][] param = new String JavaDoc[numInitParams][2];
278
279         if(debug) log("\tnum params " + //NOI18N
280
String.valueOf(numInitParams));
281
282         boolean isOK = true;
283         for(int i=0; i<numInitParams; ++i) {
284         param[i][0] = (String JavaDoc)(table.getModel().getValueAt(i,0));
285         if(debug) log("\tname is " + param[i][0]); //NOI18N
286
if(param[i][0].length() == 0) isOK = false;
287         param[i][1] = (String JavaDoc)(table.getModel().getValueAt(i,1));
288         if(debug) log("\tname is " + param[i][1]); //NOI18N
289
}
290         deployData.setInitParams(param, isOK);
291         parent.fireChangeEvent();
292     }
293     }
294     
295     private void log(String JavaDoc s) {
296     System.out.println("InitParamPanel" + s);
297     }
298 }
Popular Tags