1 19 package org.netbeans.modules.web.wizards; 20 21 import java.awt.Color ; 22 import java.awt.Dialog ; 23 import java.awt.Dimension ; 24 import java.awt.FlowLayout ; 25 import java.awt.GridBagConstraints ; 26 import java.awt.GridBagLayout ; 27 import java.awt.Insets ; 28 import java.awt.event.ActionListener ; 29 import java.awt.event.ActionEvent ; 30 31 import javax.swing.ButtonGroup ; 32 import javax.swing.JCheckBox ; 33 import javax.swing.JLabel ; 34 import javax.swing.JPanel ; 35 import javax.swing.JRadioButton ; 36 import javax.swing.JTextField ; 37 38 import javax.swing.event.ChangeListener ; 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 52 53 public class MappingEditor extends JPanel implements ActionListener { 54 55 private JRadioButton urlRadio; 57 private JRadioButton servletRadio; 58 private JTextField mappingField; 59 private ToolTipCombo servletCombo; 60 private JCheckBox [] cb; 61 62 private final static boolean debug = false; 63 64 private Dialog dialog; 65 private DialogDescriptor editDialog; 66 67 private final static String URL = "URL"; 68 private final static String SERVLET = "SERVLET"; 69 private final static String 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 79 public MappingEditor(FilterMappingData fmd, String [] 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 96 private void initComponents(String [] names) { 97 98 if(debug) log("::initComponents()"); 99 100 Insets insets = new Insets (4, 20, 4, 0); 101 Insets endInsets = new Insets (4, 20, 4, 60); 102 103 GridBagConstraints fullRowC = new GridBagConstraints (); 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 GridBagConstraints firstC = new GridBagConstraints (); 114 firstC.gridx = 0; 115 firstC.gridy = GridBagConstraints.RELATIVE; 116 firstC.anchor = GridBagConstraints.WEST; 118 firstC.insets = insets; 119 120 GridBagConstraints tflC = new GridBagConstraints (); 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 ()); 129 130 JLabel jLname = new JLabel (); 133 jLname.setText(NbBundle.getMessage(MappingEditor.class, 134 "LBL_name_filter")); 136 JTextField jTFname = new JTextField (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 urlRadio = new JRadioButton 152 (NbBundle.getMessage(MappingEditor.class, "LBL_url")); urlRadio.setMnemonic(NbBundle.getMessage(MappingEditor.class, "LBL_url_mnemonic").charAt(0)); urlRadio.addActionListener(this); 155 urlRadio.setActionCommand(URL); 156 urlRadio.getAccessibleContext().setAccessibleDescription(NbBundle.getMessage(MappingEditor.class, "ACSD_pattern_mapping")); 157 158 servletRadio = new JRadioButton (NbBundle.getMessage(MappingEditor.class,"LBL_servlet")); servletRadio.setMnemonic(NbBundle.getMessage(MappingEditor.class,"LBL_servlet_mnemonic").charAt(0)); servletRadio.addActionListener(this); 162 servletRadio.setActionCommand(SERVLET); servletRadio.getAccessibleContext().setAccessibleDescription(NbBundle.getMessage(MappingEditor.class, "ACSD_servlet_mapping")); 164 165 ButtonGroup group = new ButtonGroup (); 167 group.add(urlRadio); 168 group.add(servletRadio); 169 170 this.add(urlRadio, firstC); 172 mappingField = new JTextField (); 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 this.add(servletRadio, firstC); 180 if(names == null || names.length == 0) { 181 names = new String [1]; 182 names[0] = NbBundle.getMessage(MappingEditor.class, 183 "LBL_no_servlets"); 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 JLabel conditions = new JLabel (); 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 JPanel p0 = new JPanel (); 223 p0.setLayout(new FlowLayout (FlowLayout.LEFT)); 224 cb = new JCheckBox [4]; 225 String dispatcher; 226 for(int i=0; i<FilterMappingData.Dispatcher.getAll().length; ++i) { 227 dispatcher = FilterMappingData.Dispatcher.getAll()[i].toString(); 228 cb[i] = new JCheckBox (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 JPanel p1 = new JPanel (); 257 GridBagConstraints filler = new java.awt.GridBagConstraints (); 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 title = NbBundle.getMessage(MappingEditor.class, 268 "TITLE_filter_mapping"); 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 evt) { 282 283 if(debug) log("\tReceived action " + evt.getActionCommand()); 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 retValue = editDialog.getValue(); 309 if(DialogDescriptor.CANCEL_OPTION.equals(retValue) || DialogDescriptor.CLOSED_OPTION.equals(retValue)) { 310 if(debug) log("\tCancel"); 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 msg) { 356 357 Object [] 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 s) { 370 System.out.println("MappingEditor" + s); 371 } 372 } 373 374 | Popular Tags |