1 19 20 24 25 26 package org.netbeans.modules.web.monitor.client; 27 28 import java.awt.Color ; 29 import java.awt.Dialog ; 30 import java.awt.Dimension ; 31 import java.awt.Insets ; 32 import java.awt.GridBagConstraints ; 33 import java.awt.GridBagLayout ; 34 import java.awt.event.ActionEvent ; 35 import java.awt.event.ActionListener ; 36 import java.awt.event.FocusEvent ; 37 import java.awt.event.FocusListener ; 38 39 import javax.swing.BorderFactory ; 40 import javax.swing.JLabel ; 41 import javax.swing.JScrollPane ; 42 import javax.swing.JTextArea ; 43 import javax.swing.JTextField ; 44 45 import org.openide.DialogDisplayer; 46 import org.openide.DialogDescriptor; 47 import org.openide.NotifyDescriptor; 48 import org.openide.util.NbBundle; 49 50 public class ParamEditor extends javax.swing.JPanel { 51 52 private static final Dimension valueSize = new Dimension (400, 150); 53 54 private Dialog dialog = null; 56 private Dialog d2 = null; 57 private DialogDescriptor editDialog = null; 58 private String errorMessage = null; 59 private boolean dialogOK = false; 60 61 private String name = ""; private String value = ""; private Editable editable; 65 private Condition condition; 66 private String title = ""; 68 private boolean repainting = false; 70 71 public ParamEditor(String name, String value, Editable e, Condition c, 72 String title) { 73 this.name = name; 74 this.value = value; 75 this.editable = e; 76 this.condition = c; 77 this.title = title; 78 initialize(); 79 } 80 81 public boolean getDialogOK() { 82 return dialogOK; 83 } 84 85 public String getName() { 86 return name; 87 } 88 89 public String getValue() { 90 return value; 91 } 92 93 public void initialize() { 94 95 this.setLayout(new GridBagLayout ()); 96 97 GridBagConstraints labelC = new GridBagConstraints (); 99 labelC.gridx = 0; 100 labelC.gridy = GridBagConstraints.RELATIVE; 101 labelC.anchor = GridBagConstraints.WEST; 102 labelC.fill = GridBagConstraints.HORIZONTAL; 103 labelC.insets = new Insets (4, 15, 4, 15); 104 105 GridBagConstraints firstC = new GridBagConstraints (); 107 firstC.gridx = 0; 108 firstC.gridy = GridBagConstraints.RELATIVE; 109 firstC.gridwidth = 1; 110 firstC.anchor = GridBagConstraints.WEST; 111 firstC.insets = new Insets (4, 15, 4, 0); 112 113 GridBagConstraints tfC = new GridBagConstraints (); 115 tfC.gridx = GridBagConstraints.RELATIVE; 116 tfC.gridy = 0; 117 tfC.gridwidth = 7; 118 tfC.fill = GridBagConstraints.HORIZONTAL; 119 tfC.insets = new Insets (4, 0, 4, 15); 120 121 GridBagConstraints taC = new GridBagConstraints (); 123 taC.gridx = 0; 124 taC.gridy = GridBagConstraints.RELATIVE; 125 taC.gridheight = 3; 126 taC.gridwidth = 8; 127 taC.weightx = 1.0; 128 taC.weighty = 1.0; 129 taC.fill = GridBagConstraints.BOTH; 130 taC.anchor = GridBagConstraints.WEST; 131 taC.insets = new Insets (0, 15, 4, 15); 132 133 JLabel nameLabel = new JLabel (); 134 nameLabel.setDisplayedMnemonic(NbBundle.getMessage(ParamEditor.class, "MON_Name_Mnemonic").charAt(0)); 135 nameLabel.getAccessibleContext().setAccessibleDescription(NbBundle.getMessage(ParamEditor.class, "ACS_MON_NameA11yDesc")); 136 if (name == null || name.length()==0) 137 getAccessibleContext().setAccessibleDescription(title); 138 else 139 getAccessibleContext().setAccessibleDescription(NbBundle.getMessage(ParamEditor.class, 140 "ACS_ParamEditorA11yDesc",title, name)); String text = NbBundle.getMessage(ParamEditor.class, "MON_Name"); 142 text = text.concat(": "); 144 if(editable == Editable.BOTH) { 145 final JTextField nameText = new JTextField (25); 146 nameText.addFocusListener(new FocusListener () { 147 public void focusGained(FocusEvent evt) { 148 } 149 public void focusLost(FocusEvent evt) { 150 name = nameText.getText(); 151 } 152 }); 153 nameLabel.setLabelFor(nameText); 154 nameText.getAccessibleContext().setAccessibleName(NbBundle.getMessage(ParamEditor.class, "ACS_MON_NameTextFieldA11yName")); 155 nameText.setToolTipText(NbBundle.getMessage(ParamEditor.class, "ACS_MON_NameTextFieldA11yDesc")); 156 nameText.setText(name); 157 nameText.setBackground(java.awt.Color.white); 158 nameText.setEditable(editable == Editable.BOTH); 159 160 this.add(nameLabel, firstC); 161 this.add(nameText, tfC); 162 } 163 else { 164 this.add(nameLabel, labelC); 165 text = text.concat(name); 166 } 167 nameLabel.setText(text); 168 169 JLabel valueLabel = new JLabel (); 170 valueLabel.setText(NbBundle.getMessage(ParamEditor.class, "MON_Value").concat(":")); 171 valueLabel.setDisplayedMnemonic(NbBundle.getMessage(ParamEditor.class, "MON_Value_Mnemonic").charAt(0)); 172 valueLabel.getAccessibleContext().setAccessibleDescription(NbBundle.getMessage(ParamEditor.class, "ACS_MON_ValueA11yDesc")); 173 firstC.gridy++; 174 this.add(valueLabel, labelC); 175 176 final JTextArea valueText = new JTextArea (); 177 valueText.addFocusListener(new FocusListener () { 178 public void focusGained(FocusEvent evt) { 179 } 180 public void focusLost(FocusEvent evt) { 181 value = valueText.getText(); 182 } 183 }); 184 valueLabel.setLabelFor(valueText); 185 valueText.getAccessibleContext().setAccessibleName(NbBundle.getMessage(ParamEditor.class, "ACS_MON_ValueTextAreaA11yName")); 186 valueText.setToolTipText(NbBundle.getMessage(ParamEditor.class, "ACS_MON_ValueTextAreaA11yDesc")); 187 if(editable == Editable.NEITHER) { 188 valueText.setEditable(false); 189 valueText.setBackground(this.getBackground()); 190 valueText.setForeground(Color.BLACK); 191 valueText.setBorder(BorderFactory.createLoweredBevelBorder()); 192 } 193 valueText.setText(value); 194 valueText.setLineWrap(true); 195 valueText.setWrapStyleWord(false); 196 197 JScrollPane scrollpane = new JScrollPane (valueText); 198 scrollpane.setPreferredSize(valueSize); 199 this.add(scrollpane, taC); 201 202 this.repaint(); 205 } 206 207 public void showDialog() { 208 209 if(editable == Editable.NEITHER) { 210 NotifyDescriptor d = 211 new NotifyDescriptor(this, title, 212 NotifyDescriptor.DEFAULT_OPTION, 213 NotifyDescriptor.PLAIN_MESSAGE, 214 new Object [] { NotifyDescriptor.OK_OPTION }, 215 NotifyDescriptor.OK_OPTION); 216 DialogDisplayer.getDefault().notify(d); 217 } 218 else { 219 editDialog = new DialogDescriptor 220 (this, title, true, DialogDescriptor.OK_CANCEL_OPTION, 221 DialogDescriptor.CANCEL_OPTION, 222 new ActionListener () { 223 public void actionPerformed(ActionEvent e) { 224 evaluateInput(); 225 } 226 }); 227 228 dialog = DialogDisplayer.getDefault().createDialog(editDialog); 229 dialog.setVisible(true); 230 this.repaint(); 231 } 232 } 233 234 237 238 public void evaluateInput() { 239 240 if (editDialog.getValue().equals(NotifyDescriptor.CANCEL_OPTION)) { 241 dialog.dispose(); 242 dialogOK = false; 243 return; 244 } 245 246 if(editable == Editable.NEITHER) { 247 dialog.dispose(); 248 dialogOK = false; 249 return; 250 } 251 252 errorMessage = null; 253 254 if(name.equals("")) 255 errorMessage = NbBundle.getMessage(ParamEditor.class, 256 "MSG_no_name"); 257 else if(condition == Condition.COOKIE && name.equalsIgnoreCase("jsessionid")) 258 errorMessage = NbBundle.getMessage(ParamEditor.class, 259 "MSG_no_jsession"); 260 else if(condition == Condition.VALUE && value.equals("")) 261 errorMessage = NbBundle.getMessage(ParamEditor.class, 262 "MSG_no_value"); 263 else if(condition == Condition.HEADER && 264 name.equalsIgnoreCase("cookie") && 265 (value.indexOf("jsessionid") > -1 || 266 value.indexOf("JSESSIONID") > -1)) 267 errorMessage = NbBundle.getMessage(ParamEditor.class, 268 "MSG_no_jsession"); 269 270 if(errorMessage == null) { 271 dialog.dispose(); 272 dialogOK = true; 273 } 274 else { 275 editDialog.setValue(NotifyDescriptor.CLOSED_OPTION); 276 NotifyDescriptor nd = new NotifyDescriptor.Message 277 (errorMessage, NotifyDescriptor.ERROR_MESSAGE); 278 DialogDisplayer.getDefault().notify(nd); 279 } 280 } 281 282 283 public void repaint() { 285 super.repaint(); 286 if (dialog != null && !repainting) { 287 repainting = true; 288 dialog.repaint(); 289 repainting = false; 290 } 291 } 292 293 static class Editable { 294 private String editable; 295 296 private Editable(String editable) { 297 this.editable = editable; 298 } 299 300 public String toString() { return editable; } 301 302 public static final Editable BOTH = new Editable("both"); 303 public static final Editable VALUE = new Editable("value"); 304 public static final Editable NEITHER = new Editable("neither"); 305 } 306 307 static class Condition { 308 private String condition; 309 310 private Condition(String condition) { 311 this.condition = condition; 312 } 313 314 public String toString() { return condition; } 315 316 public static final Condition NONE = new Condition("none"); 317 public static final Condition VALUE = new Condition("value"); 318 public static final Condition COOKIE = new Condition("cookie"); 319 public static final Condition HEADER = new Condition("header"); 320 } 321 322 } | Popular Tags |