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: package org.netbeans.modules.web.wizards;
043:
044: import java.awt.Dimension;
045: import java.awt.GridBagConstraints;
046: import java.awt.Insets;
047: import java.awt.event.ActionEvent;
048: import java.awt.event.ActionListener;
049:
050: import javax.swing.JButton;
051: import javax.swing.JLabel;
052: import javax.swing.JPanel;
053: import javax.swing.JScrollPane;
054: import javax.swing.ListSelectionModel;
055: import javax.swing.event.ListSelectionEvent;
056: import javax.swing.event.ListSelectionListener;
057: import javax.swing.event.TableModelEvent;
058: import javax.swing.event.TableModelListener;
059:
060: import org.openide.util.NbBundle;
061:
062: /*
063: * Wizard panel that collects deployment data for Servlets and Filters
064: * @author Ana von Klopp
065: */
066:
067: class InitParamPanel extends JPanel implements ActionListener,
068: ListSelectionListener {
069:
070: private final static String ADD = "add";
071: private final static String EDIT = "edit";
072: private final static String REMOVE = "remove";
073: private ServletData deployData;
074: private BaseWizardPanel parent;
075: private boolean edited = false;
076:
077: private JLabel jLinitparams;
078: private DDTable table;
079: private JButton jBnew;
080: private JButton jBedit;
081: private JButton jBdelete;
082: private JScrollPane scrollP;
083:
084: private static final boolean debug = false;
085:
086: private static final long serialVersionUID = -5803905591685582710L;
087:
088: public InitParamPanel(ServletData deployData, BaseWizardPanel parent) {
089: this .deployData = deployData;
090: this .parent = parent;
091: initComponents();
092: }
093:
094: private void initComponents() {
095:
096: // Layout description
097: setLayout(new java.awt.GridBagLayout());
098:
099: // Entity covers entire row
100: GridBagConstraints fullRowC = new GridBagConstraints();
101: fullRowC.gridx = 0;
102: fullRowC.gridy = 0;
103: fullRowC.gridwidth = 2;
104: fullRowC.anchor = GridBagConstraints.WEST;
105: fullRowC.fill = GridBagConstraints.HORIZONTAL;
106: fullRowC.insets = new Insets(4, 0, 4, 60);
107:
108: // Button
109: GridBagConstraints bC = new GridBagConstraints();
110: bC.gridx = 1;
111: bC.gridy = 1;
112: bC.weightx = 0.1;
113: bC.fill = GridBagConstraints.HORIZONTAL;
114: bC.insets = new Insets(4, 20, 4, 60);
115:
116: // Table panel
117: GridBagConstraints tableC = new GridBagConstraints();
118: tableC.gridx = 0;
119: tableC.gridy = 1;
120: tableC.gridheight = 4;
121: tableC.fill = GridBagConstraints.BOTH;
122: tableC.weightx = 0.9;
123: tableC.weighty = 1.0;
124: tableC.anchor = GridBagConstraints.WEST;
125: tableC.insets = new Insets(4, 0, 4, 0);
126:
127: // Filler panel
128: GridBagConstraints fillerC = new GridBagConstraints();
129: fillerC.gridx = 1;
130: fillerC.gridy = GridBagConstraints.RELATIVE;
131: fillerC.fill = GridBagConstraints.BOTH;
132: fillerC.weighty = 1.0;
133: fillerC.insets = new Insets(4, 0, 4, 0);
134:
135: // Component Initialization by row
136: // 1. Init parameter
137: jLinitparams = new JLabel(NbBundle.getMessage(
138: InitParamPanel.class, "LBL_initparamsL"));
139: jLinitparams.setDisplayedMnemonic(NbBundle.getMessage(
140: InitParamPanel.class, "LBL_initparams_mnemonic")
141: .charAt(0));
142: // PENDING
143: this .add(jLinitparams, fullRowC);
144:
145: // 2. Table row
146:
147: String[] headers = { "paramname", "paramvalue" };
148: table = new DDTable(headers, "LBL_initparams", Editable.BOTH);
149:
150: jLinitparams.setLabelFor(table);
151:
152: // Enable the buttons according to the row selected
153: table.getSelectionModel().addListSelectionListener(this );
154: table.getAccessibleContext().setAccessibleDescription(
155: NbBundle.getMessage(InitParamPanel.class,
156: "ACSD_initparams_desc")); // NOI18N
157: table.getAccessibleContext().setAccessibleName(
158: NbBundle.getMessage(InitParamPanel.class,
159: "ACSD_initparams")); // NOI18N
160:
161: table.getModel().addTableModelListener(
162: new TableModelListener() {
163: public void tableChanged(TableModelEvent evt) {
164: if (debug)
165: log("\ttable model changed"); //NOI18N
166: updateInitParams();
167: //table.revalidate();
168: //scrollP.revalidate();
169: //scrollP.repaint();
170: }
171: });
172:
173: scrollP = new JScrollPane(table);
174: this .add(scrollP, tableC);
175:
176: jBnew = new JButton();
177: jBnew.setText(NbBundle.getMessage(InitParamPanel.class,
178: "LBL_new"));
179: jBnew.setMnemonic(NbBundle.getMessage(InitParamPanel.class,
180: "LBL_new_mnemonic").charAt(0));
181: jBnew.addActionListener(this );
182: jBnew.setActionCommand(ADD);
183: jBnew.getAccessibleContext().setAccessibleDescription(
184: NbBundle.getMessage(InitParamPanel.class,
185: "ACSD_initparam_new")); // NOI18N
186: this .add(jBnew, bC);
187:
188: bC.gridy++;
189: jBedit = new JButton();
190: jBedit.setText(NbBundle.getMessage(InitParamPanel.class,
191: "LBL_edit"));
192: jBedit.setMnemonic(NbBundle.getMessage(InitParamPanel.class,
193: "LBL_edit_mnemonic").charAt(0));
194: jBedit.addActionListener(this );
195: jBedit.setActionCommand(EDIT);
196: jBedit.getAccessibleContext().setAccessibleDescription(
197: NbBundle.getMessage(InitParamPanel.class,
198: "ACSD_initparam_edit")); // NOI18N
199: jBedit.setEnabled(false);
200: this .add(jBedit, bC);
201:
202: bC.gridy++;
203: jBdelete = new JButton();
204: jBdelete.setText(NbBundle.getMessage(InitParamPanel.class,
205: "LBL_delete"));
206: jBdelete.setMnemonic(NbBundle.getMessage(InitParamPanel.class,
207: "LBL_delete_mnemonic").charAt(0));
208: jBdelete.addActionListener(this );
209: jBdelete.setActionCommand(REMOVE);
210: jBdelete.getAccessibleContext().setAccessibleDescription(
211: NbBundle.getMessage(InitParamPanel.class,
212: "ACSD_initparam_delete")); // NOI18N
213: jBdelete.setEnabled(false);
214: this .add(jBdelete, bC);
215:
216: this .add(new javax.swing.JPanel(), fillerC);
217:
218: }
219:
220: public void setEnabled() {
221:
222: boolean enable = deployData.makeEntry();
223:
224: jLinitparams.setEnabled(enable);
225: jBnew.setEnabled(enable);
226: if (enable) {
227: if (debug) {
228: log("\tnumRows: " + table.getRowCount()); //NOI18N
229: log("\tselected row: " + table.getSelectedRow()); //NOI18N
230: }
231: ListSelectionModel lsm = table.getSelectionModel();
232: if (lsm.isSelectionEmpty()) {
233: jBdelete.setEnabled(false);
234: jBedit.setEnabled(false);
235: } else {
236: jBdelete.setEnabled(true);
237: jBedit.setEnabled(true);
238: }
239: } else {
240: jBdelete.setEnabled(false);
241: jBedit.setEnabled(false);
242: }
243: table.setEditable(enable ? Editable.BOTH : Editable.NEITHER);
244: }
245:
246: public void actionPerformed(ActionEvent evt) {
247:
248: if (debug)
249: log("::actionPerformed()"); //NOI18n
250: int row = -1;
251: if (evt.getSource() instanceof JButton) {
252: if (evt.getActionCommand() == ADD) {
253: if (debug)
254: log("\tAdding row"); //NOI18N
255: String[] values = {
256: NbBundle.getMessage(InitParamPanel.class,
257: "LBL_paramname"),
258: NbBundle.getMessage(InitParamPanel.class,
259: "LBL_paramvalue"), };
260: row = table.addRow(values);
261: table.setRowSelectionInterval(row, row);
262: if (debug)
263: log("\tAdded row " + row);
264: } else if (evt.getActionCommand() == REMOVE) {
265: if (debug)
266: log("\tDeleting row"); //NOI18N
267: row = table.getSelectedRow();
268: table.removeRow(row);
269: setEnabled();
270: } else if (evt.getActionCommand() == EDIT) {
271: if (debug)
272: log("\tEditing row"); //NOI18N
273: //table.stopEditing();
274: row = table.getSelectedRow();
275: String name = (String) (table.getValueAt(row, 0));
276: String value = (String) (table.getValueAt(row, 1));
277: String title = NbBundle.getMessage(DDTable.class,
278: "LBL_initparams_edit"); //NOI18N
279: TableRowDialog d = new TableRowDialog(name, value,
280: Editable.BOTH, TableRowDialog.Condition.NONE,
281: title);
282: d.showDialog();
283: if (d.getDialogOK()) {
284: if (debug)
285: log("Dialog is OK");
286: table.setData(d.getName(), d.getValue(), row);
287: } else
288: table.setData(name, value, row);
289: }
290: scrollP.revalidate();
291: }
292: }
293:
294: public void valueChanged(ListSelectionEvent e) {
295: if (debug)
296: log("::valueChanged()"); //NOI18N
297: //Ignore extra messages.
298: if (e.getValueIsAdjusting())
299: return;
300: setEnabled();
301: updateInitParams();
302: }
303:
304: private void updateInitParams() {
305:
306: if (debug)
307: log("::updateInitParams()");
308: edited = true;
309:
310: if (deployData.makeEntry()) {
311:
312: if (debug) {
313: log("\tnumRows: " + table.getRowCount()); //NOI18N
314: log("\tselected row: " + table.getSelectedRow()); //NOI18N
315: }
316:
317: int numInitParams = table.getRowCount();
318: String[][] param = new String[numInitParams][2];
319:
320: if (debug)
321: log("\tnum params " + //NOI18N
322: String.valueOf(numInitParams));
323:
324: boolean isOK = true;
325: for (int i = 0; i < numInitParams; ++i) {
326: param[i][0] = (String) (table.getModel().getValueAt(i,
327: 0));
328: if (debug)
329: log("\tname is " + param[i][0]); //NOI18N
330: if (param[i][0].length() == 0)
331: isOK = false;
332: param[i][1] = (String) (table.getModel().getValueAt(i,
333: 1));
334: if (debug)
335: log("\tname is " + param[i][1]); //NOI18N
336: }
337: deployData.setInitParams(param, isOK);
338: parent.fireChangeEvent();
339: }
340: }
341:
342: private void log(String s) {
343: System.out.println("InitParamPanel" + s);
344: }
345: }
|