KickJava   Java API By Example, From Geeks To Geeks.

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


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 package org.netbeans.modules.web.wizards;
20
21 import java.awt.Color JavaDoc;
22 import java.awt.Dialog JavaDoc;
23 import java.awt.Dimension JavaDoc;
24 import java.awt.FlowLayout JavaDoc;
25 import java.awt.GridBagConstraints JavaDoc;
26 import java.awt.GridBagLayout JavaDoc;
27 import java.awt.Insets JavaDoc;
28 import java.awt.event.ActionListener JavaDoc;
29 import java.awt.event.ActionEvent JavaDoc;
30
31 import javax.swing.ButtonGroup JavaDoc;
32 import javax.swing.JCheckBox JavaDoc;
33 import javax.swing.JLabel JavaDoc;
34 import javax.swing.JPanel JavaDoc;
35 import javax.swing.JRadioButton JavaDoc;
36 import javax.swing.JTextField JavaDoc;
37
38 import javax.swing.event.ChangeListener JavaDoc;
39
40 import org.openide.DialogDisplayer;
41 import org.openide.NotifyDescriptor;
42 import org.openide.DialogDescriptor;
43 import org.openide.util.HelpCtx;
44 import org.openide.util.NbBundle;
45
46
47 /**
48  * Panel where the user enters the settings to create a new Object.
49 *
50 * @author Ana von Klopp
51 */

52
53 public class MappingEditor extends JPanel JavaDoc implements ActionListener JavaDoc {
54
55     // UI Input Components
56
private JRadioButton JavaDoc urlRadio;
57     private JRadioButton JavaDoc servletRadio;
58     private JTextField JavaDoc mappingField;
59     private ToolTipCombo servletCombo;
60     private JCheckBox JavaDoc[] cb;
61
62     private final static boolean debug = false;
63
64     private Dialog JavaDoc dialog;
65     private DialogDescriptor editDialog;
66
67     private final static String JavaDoc URL = "URL";
68     private final static String JavaDoc SERVLET = "SERVLET";
69     private final static String JavaDoc SELECT_SERVLET = "SELECT";
70
71     private static FilterMappingData fmd;
72     private static boolean haveNames = true;
73     
74     private static boolean OK = false;
75
76     private static final long serialVersionUID = 4947167720581796971L;
77     
78     /** Creates new form MappingEditor */
79     public MappingEditor(FilterMappingData fmd, String JavaDoc[] servletNames) {
80     this.fmd = fmd;
81         this.getAccessibleContext().setAccessibleDescription(NbBundle.getMessage(MappingEditor.class, "ACSD_filter_mappings_edit"));
82     initComponents(servletNames);
83     }
84
85     boolean isOK() {
86     return OK;
87     }
88
89     public HelpCtx getHelp () {
90         return new HelpCtx("org.netbeans.modules.web.wizards.MappingEditor");
91     }
92     
93     /** This method is called from within the constructor to
94      * initialize the form.
95      */

96     private void initComponents(String JavaDoc[] names) {
97
98     if(debug) log("::initComponents()");
99
100     Insets JavaDoc insets = new Insets JavaDoc(4, 20, 4, 0);
101     Insets JavaDoc endInsets = new Insets JavaDoc(4, 20, 4, 60);
102
103     // Entity covers entire row
104
GridBagConstraints JavaDoc fullRowC = new GridBagConstraints JavaDoc();
105         fullRowC.gridx = 0;
106         fullRowC.gridy = GridBagConstraints.RELATIVE;
107         fullRowC.gridwidth = GridBagConstraints.REMAINDER;
108         fullRowC.anchor = GridBagConstraints.WEST;
109         fullRowC.fill = GridBagConstraints.HORIZONTAL;
110         fullRowC.insets = endInsets;
111
112     // Initial label
113
GridBagConstraints JavaDoc firstC = new GridBagConstraints JavaDoc();
114         firstC.gridx = 0;
115         firstC.gridy = GridBagConstraints.RELATIVE;
116         //firstC.weightx = 0.2;
117
firstC.anchor = GridBagConstraints.WEST;
118         firstC.insets = insets;
119
120     // Textfield covers end of row
121
GridBagConstraints JavaDoc tflC = new GridBagConstraints JavaDoc();
122         tflC.gridx = GridBagConstraints.RELATIVE;
123         tflC.weightx = 0.8;
124         tflC.gridwidth = GridBagConstraints.REMAINDER;
125         tflC.fill = GridBagConstraints.HORIZONTAL;
126         tflC.insets = endInsets;
127
128         this.setLayout(new GridBagLayout JavaDoc());
129     
130     // Add the component rows
131
// 1. Filter name
132
JLabel JavaDoc jLname = new JLabel JavaDoc();
133     jLname.setText(NbBundle.getMessage(MappingEditor.class,
134                        "LBL_name_filter")); //NOI18N
135

136     JTextField JavaDoc jTFname = new JTextField JavaDoc(25);
137     jTFname.setText(fmd.getName());
138     jTFname.setEnabled(false);
139     jTFname.setBackground(this.getBackground());
140     jTFname.setDisabledTextColor(Color.black);
141
142
143     jLname.setLabelFor(jTFname);
144     jLname.setDisplayedMnemonic(NbBundle.getMessage(MappingEditor.class, "LBL_name_filter_mnem").charAt(0));
145     jTFname.getAccessibleContext().setAccessibleDescription(NbBundle.getMessage(MappingEditor.class,"ACSD_name_filter"));
146         
147     this.add(jLname, firstC);
148     this.add(jTFname, tflC);
149     
150     // Create the radio buttons: web module button
151
urlRadio = new JRadioButton JavaDoc
152         (NbBundle.getMessage(MappingEditor.class, "LBL_url")); // NOI18N
153
urlRadio.setMnemonic(NbBundle.getMessage(MappingEditor.class, "LBL_url_mnemonic").charAt(0)); // NOI18N
154
urlRadio.addActionListener(this);
155     urlRadio.setActionCommand(URL);
156     urlRadio.getAccessibleContext().setAccessibleDescription(NbBundle.getMessage(MappingEditor.class, "ACSD_pattern_mapping"));
157     
158     // Create the radio buttons: directory button
159
servletRadio = new JRadioButton JavaDoc(NbBundle.getMessage(MappingEditor.class,"LBL_servlet")); // NOI18N
160
servletRadio.setMnemonic(NbBundle.getMessage(MappingEditor.class,"LBL_servlet_mnemonic").charAt(0)); // NOI18N
161
servletRadio.addActionListener(this);
162     servletRadio.setActionCommand(SERVLET); // NOI18N
163
servletRadio.getAccessibleContext().setAccessibleDescription(NbBundle.getMessage(MappingEditor.class, "ACSD_servlet_mapping"));
164     
165     // Create the radio button group
166
ButtonGroup JavaDoc group = new ButtonGroup JavaDoc();
167     group.add(urlRadio);
168     group.add(servletRadio);
169
170     // 2. URL row
171
this.add(urlRadio, firstC);
172     mappingField = new JTextField JavaDoc();
173         
174         mappingField.getAccessibleContext().setAccessibleName(NbBundle.getMessage(MappingEditor.class, "LBL_url"));
175     mappingField.getAccessibleContext().setAccessibleDescription(NbBundle.getMessage(MappingEditor.class,"ACSD_pattern_mapping_desc"));
176     this.add(mappingField, tflC);
177
178     // 3. Servlet row
179
this.add(servletRadio, firstC);
180     if(names == null || names.length == 0) {
181         names = new String JavaDoc[1];
182         names[0] = NbBundle.getMessage(MappingEditor.class,
183                        "LBL_no_servlets"); //
184
// NOI18N
185
haveNames = false;
186     }
187     servletCombo = new ToolTipCombo(names);
188     servletCombo.setBackground(this.getBackground());
189     servletCombo.setActionCommand(SELECT_SERVLET);
190     servletCombo.addActionListener(this);
191     servletCombo.setEnabled(haveNames);
192     servletCombo.getAccessibleContext().setAccessibleName(NbBundle.getMessage(MappingEditor.class, "ACSD_select_servlet"));
193     servletCombo.getAccessibleContext().setAccessibleDescription(NbBundle.getMessage(MappingEditor.class, "ACSD_select_servlet_desc"));
194
195     servletRadio.setEnabled(haveNames);
196     this.add(servletCombo, tflC);
197
198
199     if(fmd.getType() == FilterMappingData.Type.URL) {
200         urlRadio.setSelected(true);
201         mappingField.setText(fmd.getPattern());
202     }
203     else {
204         servletRadio.setSelected(true);
205         int size = servletCombo.getModel().getSize();
206         for(int i=0; i<size; ++i) {
207         if(servletCombo.getModel().getElementAt(i).toString().equals(fmd.getPattern())){
208             servletCombo.setSelectedIndex(i);
209             break;
210         }
211         }
212     }
213
214     // 4. Conditions label
215
JLabel JavaDoc conditions = new JLabel JavaDoc();
216     conditions.setText(NbBundle.getMessage(MappingEditor.class,
217                            "LBL_conditions"));
218     conditions.setDisplayedMnemonic(NbBundle.getMessage(MappingEditor.class,"LBL_conditions_mnemonic").charAt(0));
219     this.add(conditions, fullRowC);
220
221     // 5. Checkboxes for the conditions
222
JPanel JavaDoc p0 = new JPanel JavaDoc();
223     p0.setLayout(new FlowLayout JavaDoc(FlowLayout.LEFT));
224     cb = new JCheckBox JavaDoc[4];
225     String JavaDoc dispatcher;
226     for(int i=0; i<FilterMappingData.Dispatcher.getAll().length; ++i) {
227         dispatcher = FilterMappingData.Dispatcher.getAll()[i].toString();
228         cb[i] = new JCheckBox JavaDoc(dispatcher);
229         cb[i].setMnemonic(dispatcher.charAt(0));
230         cb[i].getAccessibleContext().setAccessibleDescription(NbBundle.getMessage(MappingEditor.class, "ACSD_dispatcher_".concat(dispatcher)));
231         p0.add(cb[i]);
232     }
233     
234     FilterMappingData.Dispatcher[] config = fmd.getDispatcher();
235     for(int i=0; i<config.length; ++i) {
236         if(config[i] == FilterMappingData.Dispatcher.REQUEST) {
237         cb[0].setSelected(true);
238         continue;
239         }
240         if(config[i] == FilterMappingData.Dispatcher.FORWARD) {
241         cb[1].setSelected(true);
242         continue;
243         }
244         if(config[i] == FilterMappingData.Dispatcher.INCLUDE) {
245         cb[2].setSelected(true);
246         continue;
247         }
248         if(config[i] == FilterMappingData.Dispatcher.ERROR) {
249         cb[3].setSelected(true);
250         continue;
251         }
252     }
253     this.add(p0, fullRowC);
254
255     // 6. Add filler panel at the bottom
256
JPanel JavaDoc p1 = new JPanel JavaDoc ();
257     GridBagConstraints JavaDoc filler = new java.awt.GridBagConstraints JavaDoc();
258     filler.gridx = 0;
259     filler.gridy = GridBagConstraints.RELATIVE;
260     filler.fill = java.awt.GridBagConstraints.HORIZONTAL;
261     filler.weighty = 1.0;
262     this.add(p1, filler);
263     }
264
265     public void showEditor() {
266         
267     String JavaDoc title = NbBundle.getMessage(MappingEditor.class,
268                        "TITLE_filter_mapping"); //NOI18N
269
editDialog = new DialogDescriptor(this, title, true,
270                       DialogDescriptor.OK_CANCEL_OPTION,
271                       DialogDescriptor.CANCEL_OPTION,
272                       DialogDescriptor.BOTTOM_ALIGN,
273                       null,
274                       this);
275
276     dialog = DialogDisplayer.getDefault().createDialog(editDialog);
277     dialog.pack();
278     dialog.show();
279     }
280
281     public void actionPerformed(ActionEvent JavaDoc evt) {
282
283     if(debug) log("\tReceived action " + evt.getActionCommand()); //NOI18N
284

285     if(evt.getActionCommand() == URL) {
286         fmd.setType(FilterMappingData.Type.URL);
287         fmd.setPattern(mappingField.getText().trim());
288             mappingField.requestFocus();
289         return;
290     }
291
292     if(evt.getActionCommand() == SERVLET) {
293         if(!haveNames) return;
294         fmd.setType(FilterMappingData.Type.SERVLET);
295         fmd.setPattern(servletCombo.getSelectedItem().toString());
296             servletCombo.requestFocus();
297         return;
298     }
299
300     if(evt.getActionCommand() == SELECT_SERVLET) {
301         if(!haveNames) return;
302         fmd.setType(FilterMappingData.Type.SERVLET);
303         servletRadio.setSelected(true);
304         fmd.setPattern(servletCombo.getSelectedItem().toString());
305         return;
306     }
307         
308     Object JavaDoc retValue = editDialog.getValue();
309     if(DialogDescriptor.CANCEL_OPTION.equals(retValue) || DialogDescriptor.CLOSED_OPTION.equals(retValue)) {
310         if(debug) log("\tCancel"); //NOI18N
311
OK = false;
312         dialog.dispose();
313         return;
314     }
315
316     if(fmd.getType() == FilterMappingData.Type.URL) {
317         fmd.setPattern(mappingField.getText().trim());
318         if(fmd.getPattern().length() == 0) {
319         notifyBadInput(NbBundle.getMessage(TableRowDialog.class,
320                            "MSG_no_pattern"));
321         return;
322         }
323     }
324
325     int i=0;
326     int num = 0;
327     for(i=0; i<4; ++i)
328         if(cb[i].isSelected()) num++;
329     
330     FilterMappingData.Dispatcher[] d =
331         new FilterMappingData.Dispatcher[num];
332     
333     num = 0;
334     if(cb[0].isSelected()) {
335         d[num] = FilterMappingData.Dispatcher.REQUEST;
336         ++num;
337     }
338     if(cb[1].isSelected()) {
339         d[num] = FilterMappingData.Dispatcher.FORWARD;
340         ++num;
341     }
342     if(cb[2].isSelected()) {
343         d[num] = FilterMappingData.Dispatcher.INCLUDE;
344         ++num;
345     }
346     if(cb[3].isSelected())
347         d[num] = FilterMappingData.Dispatcher.ERROR;
348
349     fmd.setDispatcher(d);
350     OK = true;
351     dialog.dispose();
352     return;
353     }
354
355     private void notifyBadInput(String JavaDoc msg) {
356
357     Object JavaDoc[] options = { NotifyDescriptor.OK_OPTION };
358     NotifyDescriptor badInputDialog =
359         new NotifyDescriptor(msg,
360                  NbBundle.getMessage(TableRowDialog.class,
361                              "MSG_invalid_input"),
362                  NotifyDescriptor.DEFAULT_OPTION,
363                  NotifyDescriptor.ERROR_MESSAGE,
364                  options,
365                  options[0]);
366     DialogDisplayer.getDefault().notify(badInputDialog);
367     }
368
369     private void log(String JavaDoc s) {
370     System.out.println("MappingEditor" + s);
371     }
372 }
373
374
Popular Tags