KickJava   Java API By Example, From Geeks To Geeks.

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


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 import org.openide.util.NbBundle;
39
40 /*
41  * Wizard panel that collects deployment data for Servlets and Filters
42  * @author Ana von Klopp
43  */

44
45 class MappingPanel extends JPanel JavaDoc implements ActionListener JavaDoc,
46                          TableModelListener 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 final static String JavaDoc UP = "up";
53     private final static String JavaDoc DOWN = "down";
54
55     // UI Variables
56
private JLabel JavaDoc jLtableheader;
57     private MappingTable table;
58     private JButton JavaDoc jBnew, jBedit, jBdelete, jBdown, jBup;
59     private JScrollPane JavaDoc scrollP;
60     private ServletData deployData;
61     private BaseWizardPanel parent;
62
63     private boolean edited = false;
64
65     private static final boolean debug = false;
66
67     private static final long serialVersionUID = 6540270797782597645L;
68     
69     public MappingPanel(ServletData deployData, BaseWizardPanel parent) {
70     this.deployData = deployData;
71     this.parent = parent;
72     initComponents ();
73     }
74
75     private void initComponents () {
76     // Layout description
77
setLayout(new java.awt.GridBagLayout JavaDoc());
78
79     // Entity covers entire row
80
GridBagConstraints JavaDoc fullRowC = new GridBagConstraints JavaDoc();
81     fullRowC.gridx = 0;
82     fullRowC.gridy = 0;
83     fullRowC.gridwidth = 2;
84     fullRowC.anchor = GridBagConstraints.WEST;
85     fullRowC.fill = GridBagConstraints.HORIZONTAL;
86     fullRowC.insets = new Insets JavaDoc(4, 0, 4, 60);
87
88     // Button
89
GridBagConstraints JavaDoc bC = new GridBagConstraints JavaDoc();
90     bC.gridx = 1;
91     bC.gridy = 1;
92     bC.weightx = 0.1;
93     bC.fill = GridBagConstraints.HORIZONTAL;
94     bC.insets = new Insets JavaDoc(4, 20, 4, 60);
95
96     // Table panel
97
GridBagConstraints JavaDoc tablePanelC = new GridBagConstraints JavaDoc();
98     tablePanelC.gridx = 0;
99     tablePanelC.gridy = 1;
100     tablePanelC.gridheight = 6;
101     tablePanelC.fill = GridBagConstraints.BOTH;
102     tablePanelC.weightx = 0.9;
103     tablePanelC.weighty = 1.0;
104     tablePanelC.anchor = GridBagConstraints.WEST;
105     tablePanelC.insets = new Insets JavaDoc(4, 0, 4, 0);
106         
107     // Filler panel
108
GridBagConstraints JavaDoc fillerC = new GridBagConstraints JavaDoc();
109     fillerC.gridx = 1;
110         fillerC.gridy = GridBagConstraints.RELATIVE;
111     fillerC.fill = GridBagConstraints.BOTH;
112     fillerC.weighty = 1.0;
113     fillerC.insets = new Insets JavaDoc(4, 0, 4, 0);
114
115     // 2. Table header
116
jLtableheader = new JLabel JavaDoc(NbBundle.getMessage(MappingPanel.class, "LBL_filter_mappings"));
117     jLtableheader.setDisplayedMnemonic(NbBundle.getMessage (MappingPanel.class, "LBL_filter_mappings_mnemonic").charAt(0));
118     jLtableheader.setLabelFor(table);
119     this.add(jLtableheader, fullRowC);
120
121     // 3. Table row
122
table = new MappingTable(deployData.getName(),
123                  deployData.getFilterMappings());
124     jLtableheader.setLabelFor(table);
125     table.getAccessibleContext().setAccessibleName(NbBundle.getMessage(MappingPanel.class, "ACSD_filter_mappings")); // NOI18N
126
table.getAccessibleContext().setAccessibleDescription(NbBundle.getMessage(MappingPanel.class, "ACSD_filter_mappings_desc")); // NOI18N
127

128     table.getModel().addTableModelListener(this);
129     table.getSelectionModel().addListSelectionListener(this);
130     scrollP = new JScrollPane JavaDoc(table);
131     table.setPreferredScrollableViewportSize(new Dimension JavaDoc(300, 200));
132     this.add(scrollP, tablePanelC);
133
134     jBnew = new JButton JavaDoc();
135     jBnew.setText(NbBundle.getMessage(MappingPanel.class,
136                       "LBL_newdots")); //NOI18N
137
jBnew.setMnemonic(NbBundle.getMessage(MappingPanel.class, "LBL_new_mnemonic").charAt(0));
138     jBnew.addActionListener(this);
139     jBnew.setActionCommand(ADD);
140     jBnew.getAccessibleContext().setAccessibleDescription(NbBundle.getMessage(MappingPanel.class, "ACSD_filter_mappings_new"));
141     this.add(jBnew, bC);
142
143     bC.gridy++;
144     jBedit = new JButton JavaDoc();
145     jBedit.setText(NbBundle.getMessage(MappingPanel.class,
146                       "LBL_edit")); //NOI18N
147
jBedit.setMnemonic(NbBundle.getMessage(MappingPanel.class, "LBL_edit_mnemonic").charAt(0));
148     jBedit.addActionListener(this);
149     jBedit.setActionCommand(EDIT);
150     jBedit.getAccessibleContext().setAccessibleDescription(NbBundle.getMessage(MappingPanel.class, "ACSD_filter_mappings_edit"));
151     this.add(jBedit, bC);
152
153     bC.gridy++;
154     jBdelete = new JButton JavaDoc();
155     jBdelete.setText(NbBundle.getMessage(MappingPanel.class,
156                          "LBL_delete")); //NOI18N
157
jBdelete.setMnemonic(NbBundle.getMessage(MappingPanel.class, "LBL_delete_mnemonic").charAt(0));
158     jBdelete.addActionListener(this);
159     jBdelete.setActionCommand(REMOVE);
160     jBdelete.getAccessibleContext().setAccessibleDescription(NbBundle.getMessage(MappingPanel.class, "ACSD_filter_mappings_delete"));
161     this.add(jBdelete, bC);
162
163     bC.gridy++;
164     jBup = new JButton JavaDoc();
165     jBup.setText(NbBundle.getMessage(MappingPanel.class,
166                      "LBL_move_up")); //NOI18N
167
jBup.setMnemonic(NbBundle.getMessage(MappingPanel.class, "LBL_move_up_mnemonic").charAt(0));
168     jBup.addActionListener(this);
169     jBup.setActionCommand(UP);
170     jBup.getAccessibleContext().setAccessibleDescription(NbBundle.getMessage(MappingPanel.class, "ACSD_filter_mappings_up"));
171     this.add(jBup, bC);
172
173     bC.gridy++;
174     jBdown = new JButton JavaDoc();
175     jBdown.setText(NbBundle.getMessage(MappingPanel.class,
176                        "LBL_move_down")); //NOI18N
177
jBdown.setMnemonic(NbBundle.getMessage(MappingPanel.class, "LBL_move_down_mnemonic").charAt(0));
178     jBdown.addActionListener(this);
179     jBdown.setActionCommand(DOWN);
180     jBdown.getAccessibleContext().setAccessibleDescription(NbBundle.getMessage(MappingPanel.class, "ACSD_filter_mappings_down"));
181     this.add(jBdown, bC);
182
183     bC.gridy++;
184     bC.fill=GridBagConstraints.BOTH;
185     JPanel JavaDoc filler = new JPanel JavaDoc();
186     this.add(filler, bC);
187         
188         this.add(new javax.swing.JPanel JavaDoc(),fillerC);
189     }
190
191     void setData() {
192     if(debug) log("::setData()"); //NOi18N
193

194     // Check if the name has changed - if it has, then we
195
// change all the entries for this filter.
196

197     table.setFilterName(deployData.getName());
198     
199     if(!edited) {
200
201         if(!deployData.makeEntry()) {
202         this.setEnabled(false);
203         return;
204         }
205
206         table.setRowSelectionInterval(0,0);
207         edited = true;
208     }
209
210     //scrollP.repaint();
211
}
212
213     public void actionPerformed(ActionEvent JavaDoc evt) {
214
215     if(debug) log("::actionPerformed()"); //NOI18n
216
if(evt.getSource() instanceof JButton JavaDoc) {
217         if(evt.getActionCommand() == ADD) {
218         FilterMappingData fmd = new FilterMappingData(deployData.getName());
219                     
220         MappingEditor editor =
221             new MappingEditor(fmd, deployData.getServletNames());
222         editor.showEditor();
223         if(editor.isOK()) {
224             table.addRow(0, fmd);
225             if(debug) log(fmd.toString());
226         }
227         }
228
229         else if (evt.getActionCommand() == EDIT) {
230         int index = table.getSelectedRow();
231         FilterMappingData fmd, fmd2;
232         fmd = table.getRow(index);
233         fmd2 = (FilterMappingData)(fmd.clone());
234         MappingEditor editor =
235             new MappingEditor(fmd2, deployData.getServletNames());
236         editor.showEditor();
237         if(editor.isOK()) {
238             table.setRow(index, fmd2);
239             if(debug) log(fmd2.toString());
240         }
241         }
242         else if (evt.getActionCommand() == REMOVE) {
243         int index = table.getSelectedRow();
244         table.removeRow(index);
245         table.clearSelection();
246         }
247         else if (evt.getActionCommand() == UP) {
248         if(debug) log("\tMove up");//NOI18N
249
int index = table.getSelectedRow();
250         table.moveRowUp(index);
251         table.setRowSelectionInterval(index-1, index-1);
252
253         }
254         else if (evt.getActionCommand() == DOWN) {
255         int index = table.getSelectedRow();
256         table.moveRowDown(index);
257         table.setRowSelectionInterval(index+1, index+1);
258         }
259     }
260     edited = true;
261     deployData.setFilterMappings(table.getFilterMappings());
262     scrollP.revalidate();
263     parent.fireChangeEvent();
264     }
265
266     public void tableChanged(TableModelEvent JavaDoc e) {
267     if(debug) log("::tableChanged()"); //NOI18N
268
edited = true;
269     deployData.setFilterMappings(table.getFilterMappings());
270     parent.fireChangeEvent();
271     }
272     
273     public void valueChanged(ListSelectionEvent JavaDoc e) {
274     //Ignore extra messages.
275
if (e.getValueIsAdjusting()) return;
276     this.setEnabled(deployData.makeEntry());
277     }
278
279     public void setEnabled(boolean enable) {
280     if(debug) log("::setEnabled()"); //NOI18N
281

282     jLtableheader.setEnabled(enable);
283     jBnew.setEnabled(enable);
284
285     if(!enable) {
286         jBedit.setEnabled(false);
287         jBdelete.setEnabled(false);
288         jBup.setEnabled(false);
289         jBdown.setEnabled(false);
290         return;
291     }
292
293     ListSelectionModel JavaDoc lsm = table.getSelectionModel();
294     if (lsm.isSelectionEmpty()) {
295         // disable the relevant buttons
296
jBdelete.setEnabled(false);
297         jBedit.setEnabled(false);
298         jBdown.setEnabled(false);
299         jBup.setEnabled(false);
300     }
301     else {
302         // We only allow single selections
303
int selectedRow = lsm.getMinSelectionIndex();
304         String JavaDoc str = (String JavaDoc)(table.getValueAt(selectedRow, 0));
305         boolean canEdit = str.equals(deployData.getName());
306         jBdelete.setEnabled(canEdit);
307         jBedit.setEnabled(canEdit);
308         int numRows = table.getRowCount();
309         if(selectedRow > 0)
310         jBup.setEnabled(true);
311         else
312         jBup.setEnabled(false);
313         if(selectedRow < numRows-1)
314         jBdown.setEnabled(true);
315         else
316         jBdown.setEnabled(false);
317     }
318     }
319
320
321     private void log(String JavaDoc s) {
322     System.out.println("MappingPanel" + s);
323     }
324 }
Popular Tags