View Javadoc
1 package net.sourceforge.pmd.swingui; 2 3 import net.sourceforge.pmd.swingui.event.ListenerList; 4 import net.sourceforge.pmd.swingui.event.RulesEditingEvent; 5 import net.sourceforge.pmd.swingui.event.RulesEditingEventListener; 6 7 import javax.swing.JComboBox; 8 import javax.swing.JLabel; 9 import javax.swing.JPanel; 10 import javax.swing.JTextField; 11 import javax.swing.UIManager; 12 import javax.swing.border.EmptyBorder; 13 import javax.swing.border.TitledBorder; 14 import java.awt.BorderLayout; 15 import java.awt.Color; 16 import java.awt.Component; 17 import java.awt.GridBagConstraints; 18 import java.awt.GridBagLayout; 19 import java.awt.Insets; 20 import java.awt.Window; 21 import java.awt.event.FocusEvent; 22 import java.awt.event.FocusListener; 23 import java.text.MessageFormat; 24 25 /*** 26 * 27 * @author Donald A. Leckie 28 * @since August 29, 2002 29 * @version $Revision: 1.21 $, $Date: 2003/05/28 18:08:35 $ 30 */ 31 class RulePropertyEditingPanel extends JPanel implements Constants { 32 33 private JLabel m_nameLabel; 34 private JTextField m_name; 35 private JLabel m_valueLabel; 36 private JTextField m_value; 37 private JLabel m_valueTypeLabel; 38 private JComboBox m_valueType; 39 private boolean m_enabled; 40 private RulesTreeNode m_currentDataNode; 41 private boolean m_isEditing; 42 private String m_originalName; 43 private String m_originalValue; 44 private FocusListener m_focusListener = new PropertyNameFocusListener(); 45 46 /*** 47 ******************************************************************************* 48 * 49 * @return 50 */ 51 protected RulePropertyEditingPanel() { 52 super(new BorderLayout()); 53 54 EmptyBorder emptyBorder = new EmptyBorder(5, 5, 5, 5); 55 56 setBorder(emptyBorder); 57 58 JPanel panel; 59 TitledBorder titledBorder; 60 GridBagLayout layout; 61 GridBagConstraints constraints; 62 63 int[] columnWidths = {50, 100, 100}; 64 layout = new GridBagLayout(); 65 layout.columnWidths = columnWidths; 66 panel = new JPanel(layout); 67 titledBorder = ComponentFactory.createTitledBorder(" Property "); 68 69 panel.setBorder(titledBorder); 70 add(panel, BorderLayout.CENTER); 71 72 // Property Name Label 73 m_nameLabel = new JLabel("Name"); 74 m_nameLabel.setFont(UIManager.getFont("labelFont")); 75 m_nameLabel.setHorizontalAlignment(JLabel.RIGHT); 76 constraints = layout.getConstraints(m_nameLabel); 77 constraints.gridx = 0; 78 constraints.gridy = 0; 79 constraints.gridwidth = 1; 80 constraints.gridheight = 1; 81 constraints.anchor = GridBagConstraints.NORTHEAST; 82 constraints.fill = GridBagConstraints.NONE; 83 constraints.insets = new Insets(4, 2, 4, 2); 84 panel.add(m_nameLabel, constraints); 85 86 // Property Name Text 87 m_name = new JTextField(); 88 m_name.setFont(UIManager.getFont("dataFont")); 89 m_name.addFocusListener(m_focusListener); 90 m_name.setRequestFocusEnabled(true); 91 constraints = layout.getConstraints(m_name); 92 constraints.gridx = 1; 93 constraints.gridy = 0; 94 constraints.gridwidth = GridBagConstraints.REMAINDER; 95 constraints.gridheight = 1; 96 constraints.anchor = GridBagConstraints.NORTHWEST; 97 constraints.fill = GridBagConstraints.BOTH; 98 constraints.insets = new Insets(4, 2, 4, 2); 99 panel.add(m_name, constraints); 100 101 // Property Value Label 102 m_valueLabel = new JLabel("Value"); 103 m_valueLabel.setFont(UIManager.getFont("labelFont")); 104 m_valueLabel.setHorizontalAlignment(JLabel.RIGHT); 105 constraints = layout.getConstraints(m_nameLabel); 106 constraints.gridx = 0; 107 constraints.gridy = 1; 108 constraints.gridwidth = 1; 109 constraints.gridheight = 1; 110 constraints.anchor = GridBagConstraints.NORTHEAST; 111 constraints.fill = GridBagConstraints.NONE; 112 constraints.insets = new Insets(4, 2, 4, 2); 113 panel.add(m_valueLabel, constraints); 114 115 // Property Value Text 116 m_value = new JTextField(); 117 m_value.setFont(UIManager.getFont("dataFont")); 118 m_value.setOpaque(true); 119 constraints = layout.getConstraints(m_name); 120 constraints.gridx = 1; 121 constraints.gridy = 1; 122 constraints.gridwidth = GridBagConstraints.REMAINDER; 123 constraints.gridheight = 1; 124 constraints.anchor = GridBagConstraints.NORTHWEST; 125 constraints.fill = GridBagConstraints.BOTH; 126 constraints.insets = new Insets(4, 2, 4, 2); 127 panel.add(m_value, constraints); 128 129 // Property Value Type Label 130 m_valueTypeLabel = new JLabel("Type"); 131 m_valueTypeLabel.setFont(UIManager.getFont("labelFont")); 132 m_valueTypeLabel.setHorizontalAlignment(JLabel.RIGHT); 133 constraints.gridx = 0; 134 constraints.gridy = 2; 135 constraints.gridwidth = 1; 136 constraints.gridheight = 1; 137 constraints.anchor = GridBagConstraints.NORTHEAST; 138 constraints.fill = GridBagConstraints.NONE; 139 constraints.insets = new Insets(4, 2, 4, 2); 140 panel.add(m_valueTypeLabel, constraints); 141 142 // Property Value Type 143 String[] items = {STRING, BOOLEAN, DECIMAL_NUMBER, INTEGER}; 144 m_valueType = new JComboBox(items); 145 m_valueType.setEditable(false); 146 m_valueType.setOpaque(true); 147 constraints = layout.getConstraints(m_name); 148 constraints.gridx = 1; 149 constraints.gridy = 2; 150 constraints.gridwidth = 1; 151 constraints.gridheight = 1; 152 constraints.anchor = GridBagConstraints.NORTHWEST; 153 constraints.fill = GridBagConstraints.BOTH; 154 constraints.insets = new Insets(4, 2, 4, 2); 155 panel.add(m_valueType, constraints); 156 157 enableData(false); 158 159 ListenerList.addListener(new RulesEditingEventHandler()); 160 } 161 162 /*** 163 ******************************************************************************* 164 * 165 * @param dataNode 166 */ 167 private void saveData(RulesTreeNode dataNode) { 168 if ((dataNode != null) && m_isEditing) { 169 if (dataNode.isProperty()) { 170 // Test for valid property name. 171 String propertyName = m_name.getText(); 172 173 if (propertyName.equalsIgnoreCase(m_originalName) == false) { 174 if (dataNode.getSibling(propertyName) != null) { 175 String template = "Another property already has the name \"{0}\". The change will not be applied."; 176 String[] args = {propertyName}; 177 String message = MessageFormat.format(template, args); 178 boolean hasFocus = m_name.hasFocus(); 179 180 m_name.removeFocusListener(m_focusListener); 181 MessageDialog.show(getParentWindow(), message); 182 m_name.addFocusListener(m_focusListener); 183 184 if (hasFocus) { 185 m_name.requestFocus(); 186 } 187 188 propertyName = m_originalName; 189 } 190 } 191 192 // Test for valid value. 193 String valueText = m_value.getText(); 194 String selectedItem = (String) m_valueType.getSelectedItem(); 195 196 if (selectedItem.equalsIgnoreCase(BOOLEAN)) { 197 valueText = saveBoolean(valueText); 198 } else if (selectedItem.equalsIgnoreCase(DECIMAL_NUMBER)) { 199 valueText = saveDecimalNumber(valueText); 200 } else if (selectedItem.equalsIgnoreCase(INTEGER)) { 201 valueText = saveInteger(valueText); 202 } 203 204 dataNode.setName(propertyName); 205 dataNode.setPropertyValue(valueText); 206 dataNode.setPropertyValueType(selectedItem); 207 } 208 } 209 } 210 211 /*** 212 ******************************************************************************* 213 * 214 * @param valueText 215 */ 216 private String saveBoolean(String valueText) { 217 boolean originalValue; 218 boolean newValue; 219 220 try { 221 originalValue = Boolean.getBoolean(m_originalValue); 222 } catch (NumberFormatException exception) { 223 originalValue = true; 224 } 225 226 try { 227 newValue = Boolean.getBoolean(valueText); 228 valueText = String.valueOf(newValue); 229 } catch (NumberFormatException exception) { 230 String template = "New property of \"{0}\" is not a boolean. The change will not be applied."; 231 String[] args = {valueText}; 232 String message = MessageFormat.format(template, args); 233 234 m_name.removeFocusListener(m_focusListener); 235 MessageDialog.show(getParentWindow(), message); 236 m_name.addFocusListener(m_focusListener); 237 238 newValue = originalValue; 239 valueText = m_originalValue; 240 } 241 242 return valueText; 243 } 244 245 /*** 246 ******************************************************************************* 247 * 248 * @param valueText 249 */ 250 private String saveDecimalNumber(String valueText) { 251 double originalValue; 252 double newValue; 253 254 try { 255 originalValue = Double.parseDouble(m_originalValue); 256 } catch (NumberFormatException exception) { 257 originalValue = 0.0; 258 } 259 260 try { 261 newValue = Double.parseDouble(valueText); 262 valueText = String.valueOf(newValue); 263 } catch (NumberFormatException exception) { 264 String template = "New property of \"{0}\" is not a decimal number. The change will not be applied."; 265 String[] args = {valueText}; 266 String message = MessageFormat.format(template, args); 267 268 m_name.removeFocusListener(m_focusListener); 269 MessageDialog.show(getParentWindow(), message); 270 m_name.addFocusListener(m_focusListener); 271 272 newValue = originalValue; 273 valueText = m_originalValue; 274 } 275 276 return valueText; 277 } 278 279 /*** 280 ******************************************************************************* 281 * 282 * @param valueText 283 */ 284 private String saveInteger(String valueText) { 285 int originalValue; 286 int newValue; 287 288 try { 289 originalValue = Integer.parseInt(m_originalValue); 290 } catch (NumberFormatException exception) { 291 originalValue = 0; 292 } 293 294 try { 295 newValue = Integer.parseInt(valueText); 296 valueText = String.valueOf(newValue); 297 } catch (NumberFormatException exception) { 298 String template = "New property of \"{0}\" is not an integer. The change will not be applied."; 299 String[] args = {valueText}; 300 String message = MessageFormat.format(template, args); 301 302 m_name.removeFocusListener(m_focusListener); 303 MessageDialog.show(getParentWindow(), message); 304 m_name.addFocusListener(m_focusListener); 305 306 newValue = originalValue; 307 valueText = m_originalValue; 308 } 309 310 return valueText; 311 } 312 313 /*** 314 ******************************************************************************* 315 * 316 * @param isEditing 317 */ 318 protected void setIsEditing(boolean isEditing) { 319 m_isEditing = isEditing; 320 } 321 322 /*** 323 ******************************************************************************* 324 * 325 * @param dataNode 326 */ 327 private void loadData(RulesTreeNode dataNode) { 328 if (dataNode == null) { 329 enableData(false); 330 } else if (dataNode.isRuleSet()) { 331 enableData(false); 332 } else if (dataNode.isRule()) { 333 enableData(false); 334 } else if (dataNode.isProperty()) { 335 loadData_(dataNode); 336 } else { 337 enableData(false); 338 } 339 } 340 341 /*** 342 ******************************************************************************* 343 * 344 * @param data 345 */ 346 private void loadData_(RulesTreeNode dataNode) { 347 if (m_enabled == false) { 348 enableData(true); 349 } 350 351 String name = dataNode.getName(); 352 String valueType = dataNode.getPropertyValueType(); 353 354 m_name.setText(name); 355 m_value.setText(dataNode.getPropertyValue()); 356 m_valueType.setSelectedItem(valueType); 357 358 m_originalName = name; 359 m_originalValue = valueType; 360 m_currentDataNode = dataNode; 361 } 362 363 /*** 364 ******************************************************************************* 365 * 366 */ 367 private void enableData(boolean enable) { 368 if (enable) { 369 // Just to be sure the focus listener isn't set. 370 m_name.removeFocusListener(m_focusListener); 371 m_name.addFocusListener(m_focusListener); 372 373 m_nameLabel.setEnabled(true); 374 375 m_name.setEnabled(true); 376 m_name.setBackground(Color.white); 377 378 m_valueLabel.setEnabled(true); 379 380 m_value.setEnabled(true); 381 m_value.setBackground(Color.white); 382 383 m_valueTypeLabel.setEnabled(true); 384 385 m_valueType.setEnabled(true); 386 m_valueType.setBackground(Color.white); 387 } else { 388 m_name.removeFocusListener(m_focusListener); 389 390 Color background = UIManager.getColor("disabledTextBackground"); 391 392 m_nameLabel.setEnabled(false); 393 394 m_name.setText(""); 395 m_name.setEnabled(false); 396 m_name.setBackground(background); 397 398 m_valueLabel.setEnabled(false); 399 400 m_value.setText(""); 401 m_value.setEnabled(false); 402 m_value.setBackground(background); 403 404 m_valueTypeLabel.setEnabled(false); 405 406 m_valueType.setSelectedIndex(0); 407 m_valueType.setEnabled(false); 408 m_valueType.setBackground(background); 409 410 m_currentDataNode = null; 411 } 412 413 m_enabled = enable; 414 } 415 416 /*** 417 ******************************************************************************* 418 * 419 * @return 420 */ 421 private Window getParentWindow() { 422 Component component = getParent(); 423 424 while ((component != null) && ((component instanceof Window) == false)) { 425 component = component.getParent(); 426 } 427 428 return (Window) component; 429 } 430 431 /*** 432 ************************************************************************************ 433 ************************************************************************************ 434 ************************************************************************************ 435 */ 436 private class PropertyNameFocusListener implements FocusListener { 437 438 /*** 439 ************************************************************************** 440 * 441 * @param event 442 */ 443 public void focusGained(FocusEvent event) { 444 } 445 446 /*** 447 ************************************************************************** 448 * 449 * @param event 450 */ 451 public void focusLost(FocusEvent event) { 452 String propertyName = m_name.getText().trim(); 453 454 if (propertyName.length() == 0) { 455 String message = "The property name is missing."; 456 m_name.removeFocusListener(this); 457 MessageDialog.show(getParentWindow(), message); 458 m_name.addFocusListener(this); 459 m_name.requestFocus(); 460 } else if (propertyName.equalsIgnoreCase(m_originalName) == false) { 461 if (m_currentDataNode.getSibling(propertyName) != null) { 462 String template = "Another property already has the name \"{0}\"."; 463 String[] args = {propertyName}; 464 String message = MessageFormat.format(template, args); 465 m_name.removeFocusListener(this); 466 MessageDialog.show(getParentWindow(), message); 467 m_name.addFocusListener(this); 468 m_name.requestFocus(); 469 } 470 } 471 } 472 } 473 474 /*** 475 ************************************************************************************ 476 ************************************************************************************ 477 ************************************************************************************ 478 */ 479 private class RulesEditingEventHandler implements RulesEditingEventListener { 480 481 /*** 482 ************************************************************************* 483 * 484 * @param event 485 */ 486 public void loadData(RulesEditingEvent event) { 487 RulePropertyEditingPanel.this.loadData(event.getDataNode()); 488 } 489 490 /*** 491 ************************************************************************* 492 * 493 * @param event 494 */ 495 public void saveData(RulesEditingEvent event) { 496 RulePropertyEditingPanel.this.saveData(event.getDataNode()); 497 } 498 } 499 }

This page was automatically generated by Maven