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 MappingPanel extends JPanel implements ActionListener,
068: TableModelListener, 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 final static String UP = "up";
074: private final static String DOWN = "down";
075:
076: // UI Variables
077: private JLabel jLtableheader;
078: private MappingTable table;
079: private JButton jBnew, jBedit, jBdelete, jBdown, jBup;
080: private JScrollPane scrollP;
081: private ServletData deployData;
082: private BaseWizardPanel parent;
083:
084: private boolean edited = false;
085:
086: private static final boolean debug = false;
087:
088: private static final long serialVersionUID = 6540270797782597645L;
089:
090: public MappingPanel(ServletData deployData, BaseWizardPanel parent) {
091: this .deployData = deployData;
092: this .parent = parent;
093: initComponents();
094: }
095:
096: private void initComponents() {
097: // Layout description
098: setLayout(new java.awt.GridBagLayout());
099:
100: // Entity covers entire row
101: GridBagConstraints fullRowC = new GridBagConstraints();
102: fullRowC.gridx = 0;
103: fullRowC.gridy = 0;
104: fullRowC.gridwidth = 2;
105: fullRowC.anchor = GridBagConstraints.WEST;
106: fullRowC.fill = GridBagConstraints.HORIZONTAL;
107: fullRowC.insets = new Insets(4, 0, 4, 60);
108:
109: // Button
110: GridBagConstraints bC = new GridBagConstraints();
111: bC.gridx = 1;
112: bC.gridy = 1;
113: bC.weightx = 0.1;
114: bC.fill = GridBagConstraints.HORIZONTAL;
115: bC.insets = new Insets(4, 20, 4, 60);
116:
117: // Table panel
118: GridBagConstraints tablePanelC = new GridBagConstraints();
119: tablePanelC.gridx = 0;
120: tablePanelC.gridy = 1;
121: tablePanelC.gridheight = 6;
122: tablePanelC.fill = GridBagConstraints.BOTH;
123: tablePanelC.weightx = 0.9;
124: tablePanelC.weighty = 1.0;
125: tablePanelC.anchor = GridBagConstraints.WEST;
126: tablePanelC.insets = new Insets(4, 0, 4, 0);
127:
128: // Filler panel
129: GridBagConstraints fillerC = new GridBagConstraints();
130: fillerC.gridx = 1;
131: fillerC.gridy = GridBagConstraints.RELATIVE;
132: fillerC.fill = GridBagConstraints.BOTH;
133: fillerC.weighty = 1.0;
134: fillerC.insets = new Insets(4, 0, 4, 0);
135:
136: // 2. Table header
137: jLtableheader = new JLabel(NbBundle.getMessage(
138: MappingPanel.class, "LBL_filter_mappings"));
139: jLtableheader.setDisplayedMnemonic(NbBundle.getMessage(
140: MappingPanel.class, "LBL_filter_mappings_mnemonic")
141: .charAt(0));
142: jLtableheader.setLabelFor(table);
143: this .add(jLtableheader, fullRowC);
144:
145: // 3. Table row
146: table = new MappingTable(deployData.getName(), deployData
147: .getFilterMappings());
148: jLtableheader.setLabelFor(table);
149: table.getAccessibleContext().setAccessibleName(
150: NbBundle.getMessage(MappingPanel.class,
151: "ACSD_filter_mappings")); // NOI18N
152: table.getAccessibleContext().setAccessibleDescription(
153: NbBundle.getMessage(MappingPanel.class,
154: "ACSD_filter_mappings_desc")); // NOI18N
155:
156: table.getModel().addTableModelListener(this );
157: table.getSelectionModel().addListSelectionListener(this );
158: scrollP = new JScrollPane(table);
159: table
160: .setPreferredScrollableViewportSize(new Dimension(300,
161: 200));
162: this .add(scrollP, tablePanelC);
163:
164: jBnew = new JButton();
165: jBnew.setText(NbBundle.getMessage(MappingPanel.class,
166: "LBL_newdots")); //NOI18N
167: jBnew.setMnemonic(NbBundle.getMessage(MappingPanel.class,
168: "LBL_new_mnemonic").charAt(0));
169: jBnew.addActionListener(this );
170: jBnew.setActionCommand(ADD);
171: jBnew.getAccessibleContext().setAccessibleDescription(
172: NbBundle.getMessage(MappingPanel.class,
173: "ACSD_filter_mappings_new"));
174: this .add(jBnew, bC);
175:
176: bC.gridy++;
177: jBedit = new JButton();
178: jBedit.setText(NbBundle.getMessage(MappingPanel.class,
179: "LBL_edit")); //NOI18N
180: jBedit.setMnemonic(NbBundle.getMessage(MappingPanel.class,
181: "LBL_edit_mnemonic").charAt(0));
182: jBedit.addActionListener(this );
183: jBedit.setActionCommand(EDIT);
184: jBedit.getAccessibleContext().setAccessibleDescription(
185: NbBundle.getMessage(MappingPanel.class,
186: "ACSD_filter_mappings_edit"));
187: this .add(jBedit, bC);
188:
189: bC.gridy++;
190: jBdelete = new JButton();
191: jBdelete.setText(NbBundle.getMessage(MappingPanel.class,
192: "LBL_delete")); //NOI18N
193: jBdelete.setMnemonic(NbBundle.getMessage(MappingPanel.class,
194: "LBL_delete_mnemonic").charAt(0));
195: jBdelete.addActionListener(this );
196: jBdelete.setActionCommand(REMOVE);
197: jBdelete.getAccessibleContext().setAccessibleDescription(
198: NbBundle.getMessage(MappingPanel.class,
199: "ACSD_filter_mappings_delete"));
200: this .add(jBdelete, bC);
201:
202: bC.gridy++;
203: jBup = new JButton();
204: jBup.setText(NbBundle.getMessage(MappingPanel.class,
205: "LBL_move_up")); //NOI18N
206: jBup.setMnemonic(NbBundle.getMessage(MappingPanel.class,
207: "LBL_move_up_mnemonic").charAt(0));
208: jBup.addActionListener(this );
209: jBup.setActionCommand(UP);
210: jBup.getAccessibleContext().setAccessibleDescription(
211: NbBundle.getMessage(MappingPanel.class,
212: "ACSD_filter_mappings_up"));
213: this .add(jBup, bC);
214:
215: bC.gridy++;
216: jBdown = new JButton();
217: jBdown.setText(NbBundle.getMessage(MappingPanel.class,
218: "LBL_move_down")); //NOI18N
219: jBdown.setMnemonic(NbBundle.getMessage(MappingPanel.class,
220: "LBL_move_down_mnemonic").charAt(0));
221: jBdown.addActionListener(this );
222: jBdown.setActionCommand(DOWN);
223: jBdown.getAccessibleContext().setAccessibleDescription(
224: NbBundle.getMessage(MappingPanel.class,
225: "ACSD_filter_mappings_down"));
226: this .add(jBdown, bC);
227:
228: bC.gridy++;
229: bC.fill = GridBagConstraints.BOTH;
230: JPanel filler = new JPanel();
231: this .add(filler, bC);
232:
233: this .add(new javax.swing.JPanel(), fillerC);
234: }
235:
236: void setData() {
237: if (debug)
238: log("::setData()"); //NOi18N
239:
240: // Check if the name has changed - if it has, then we
241: // change all the entries for this filter.
242:
243: table.setFilterName(deployData.getName());
244:
245: if (!edited) {
246:
247: if (!deployData.makeEntry()) {
248: this .setEnabled(false);
249: return;
250: }
251:
252: table.setRowSelectionInterval(0, 0);
253: edited = true;
254: }
255:
256: //scrollP.repaint();
257: }
258:
259: public void actionPerformed(ActionEvent evt) {
260:
261: if (debug)
262: log("::actionPerformed()"); //NOI18n
263: if (evt.getSource() instanceof JButton) {
264: if (evt.getActionCommand() == ADD) {
265: FilterMappingData fmd = new FilterMappingData(
266: deployData.getName());
267:
268: MappingEditor editor = new MappingEditor(fmd,
269: deployData.getServletNames());
270: editor.showEditor();
271: if (editor.isOK()) {
272: table.addRow(0, fmd);
273: if (debug)
274: log(fmd.toString());
275: }
276: }
277:
278: else if (evt.getActionCommand() == EDIT) {
279: int index = table.getSelectedRow();
280: FilterMappingData fmd, fmd2;
281: fmd = table.getRow(index);
282: fmd2 = (FilterMappingData) (fmd.clone());
283: MappingEditor editor = new MappingEditor(fmd2,
284: deployData.getServletNames());
285: editor.showEditor();
286: if (editor.isOK()) {
287: table.setRow(index, fmd2);
288: if (debug)
289: log(fmd2.toString());
290: }
291: } else if (evt.getActionCommand() == REMOVE) {
292: int index = table.getSelectedRow();
293: table.removeRow(index);
294: table.clearSelection();
295: } else if (evt.getActionCommand() == UP) {
296: if (debug)
297: log("\tMove up");//NOI18N
298: int index = table.getSelectedRow();
299: table.moveRowUp(index);
300: table.setRowSelectionInterval(index - 1, index - 1);
301:
302: } else if (evt.getActionCommand() == DOWN) {
303: int index = table.getSelectedRow();
304: table.moveRowDown(index);
305: table.setRowSelectionInterval(index + 1, index + 1);
306: }
307: }
308: edited = true;
309: deployData.setFilterMappings(table.getFilterMappings());
310: scrollP.revalidate();
311: parent.fireChangeEvent();
312: }
313:
314: public void tableChanged(TableModelEvent e) {
315: if (debug)
316: log("::tableChanged()"); //NOI18N
317: edited = true;
318: deployData.setFilterMappings(table.getFilterMappings());
319: parent.fireChangeEvent();
320: }
321:
322: public void valueChanged(ListSelectionEvent e) {
323: //Ignore extra messages.
324: if (e.getValueIsAdjusting())
325: return;
326: this .setEnabled(deployData.makeEntry());
327: }
328:
329: public void setEnabled(boolean enable) {
330: if (debug)
331: log("::setEnabled()"); //NOI18N
332:
333: jLtableheader.setEnabled(enable);
334: jBnew.setEnabled(enable);
335:
336: if (!enable) {
337: jBedit.setEnabled(false);
338: jBdelete.setEnabled(false);
339: jBup.setEnabled(false);
340: jBdown.setEnabled(false);
341: return;
342: }
343:
344: ListSelectionModel lsm = table.getSelectionModel();
345: if (lsm.isSelectionEmpty()) {
346: // disable the relevant buttons
347: jBdelete.setEnabled(false);
348: jBedit.setEnabled(false);
349: jBdown.setEnabled(false);
350: jBup.setEnabled(false);
351: } else {
352: // We only allow single selections
353: int selectedRow = lsm.getMinSelectionIndex();
354: String str = (String) (table.getValueAt(selectedRow, 0));
355: boolean canEdit = str.equals(deployData.getName());
356: jBdelete.setEnabled(canEdit);
357: jBedit.setEnabled(canEdit);
358: int numRows = table.getRowCount();
359: if (selectedRow > 0)
360: jBup.setEnabled(true);
361: else
362: jBup.setEnabled(false);
363: if (selectedRow < numRows - 1)
364: jBdown.setEnabled(true);
365: else
366: jBdown.setEnabled(false);
367: }
368: }
369:
370: private void log(String s) {
371: System.out.println("MappingPanel" + s);
372: }
373: }
|