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.JLabel; 8 import javax.swing.JPanel; 9 import javax.swing.JScrollPane; 10 import javax.swing.JTextArea; 11 import javax.swing.JTextField; 12 import javax.swing.UIManager; 13 import javax.swing.border.EmptyBorder; 14 import javax.swing.border.TitledBorder; 15 import java.awt.BorderLayout; 16 import java.awt.Color; 17 import java.awt.Component; 18 import java.awt.GridBagConstraints; 19 import java.awt.GridBagLayout; 20 import java.awt.Insets; 21 import java.awt.Window; 22 import java.awt.event.FocusEvent; 23 import java.awt.event.FocusListener; 24 import java.text.MessageFormat; 25 26 /*** 27 * 28 * @author Donald A. Leckie 29 * @since August 29, 2002 30 * @version $Revision: 1.21 $, $Date: 2003/05/28 18:08:35 $ 31 */ 32 class RuleSetEditingPanel extends JPanel { 33 private JLabel m_nameLabel; 34 private JTextField m_name; 35 private JLabel m_descriptionLabel; 36 private JTextArea m_description; 37 private JScrollPane m_descriptionScrollPane; 38 private boolean m_enabled; 39 private RulesTreeNode m_currentDataNode; 40 private boolean m_isEditing; 41 private String m_originalName; 42 private FocusListener m_focusListener = new RuleSetNameFocusListener(); 43 44 /*** 45 ******************************************************************************* 46 * 47 */ 48 protected RuleSetEditingPanel() { 49 super(new BorderLayout()); 50 51 EmptyBorder emptyBorder = new EmptyBorder(5, 5, 5, 5); 52 53 setBorder(emptyBorder); 54 55 GridBagLayout layout; 56 GridBagConstraints constraints; 57 JPanel panel; 58 TitledBorder titledBorder; 59 60 int[] columnWidths = {25, 100, 100, 100, 100, 100}; 61 layout = new GridBagLayout(); 62 layout.columnWidths = columnWidths; 63 panel = new JPanel(layout); 64 titledBorder = ComponentFactory.createTitledBorder(" Rule Set "); 65 66 panel.setBorder(titledBorder); 67 add(panel, BorderLayout.CENTER); 68 69 // Rule Set Name Label 70 m_nameLabel = new JLabel("Name"); 71 m_nameLabel.setFont(UIManager.getFont("labelFont")); 72 m_nameLabel.setHorizontalAlignment(JLabel.RIGHT); 73 constraints = layout.getConstraints(m_nameLabel); 74 constraints.gridx = 0; 75 constraints.gridy = 0; 76 constraints.gridwidth = 1; 77 constraints.gridheight = 1; 78 constraints.anchor = GridBagConstraints.NORTHEAST; 79 constraints.fill = GridBagConstraints.NONE; 80 constraints.insets = new Insets(4, 2, 4, 2); 81 panel.add(m_nameLabel, constraints); 82 83 // Rule Set Name Text 84 m_name = new JTextField(); 85 m_name.setFont(UIManager.getFont("dataFont")); 86 m_name.addFocusListener(m_focusListener); 87 m_name.setRequestFocusEnabled(true); 88 constraints = layout.getConstraints(m_name); 89 constraints.gridx = 1; 90 constraints.gridy = 0; 91 constraints.gridwidth = 2; 92 constraints.gridheight = 1; 93 constraints.anchor = GridBagConstraints.NORTHWEST; 94 constraints.fill = GridBagConstraints.BOTH; 95 constraints.insets = new Insets(4, 2, 4, 2); 96 panel.add(m_name, constraints); 97 98 // Rule Set Description Label 99 m_descriptionLabel = new JLabel("Description"); 100 m_descriptionLabel.setFont(UIManager.getFont("labelFont")); 101 m_descriptionLabel.setHorizontalAlignment(JLabel.RIGHT); 102 constraints = layout.getConstraints(m_nameLabel); 103 constraints.gridx = 0; 104 constraints.gridy = 1; 105 constraints.gridwidth = 1; 106 constraints.gridheight = 1; 107 constraints.anchor = GridBagConstraints.NORTHEAST; 108 constraints.fill = GridBagConstraints.NONE; 109 constraints.insets = new Insets(4, 2, 4, 2); 110 panel.add(m_descriptionLabel, constraints); 111 112 // Rule Set Description Text 113 m_description = ComponentFactory.createTextArea(""); 114 115 // Rule Set Description Scroll Pane; 116 m_descriptionScrollPane = ComponentFactory.createScrollPane(m_description); 117 constraints = layout.getConstraints(m_name); 118 constraints.gridx = 1; 119 constraints.gridy = 1; 120 constraints.gridwidth = GridBagConstraints.REMAINDER; 121 constraints.gridheight = 1; 122 constraints.anchor = GridBagConstraints.NORTHWEST; 123 constraints.fill = GridBagConstraints.BOTH; 124 constraints.insets = new Insets(4, 2, 4, 2); 125 constraints.ipady = 4 * 20; // 4 lines * 20 pixels/line 126 panel.add(m_descriptionScrollPane, constraints); 127 128 enableData(false); 129 130 ListenerList.addListener(new RulesEditingEventHandler()); 131 } 132 133 /*** 134 ******************************************************************************* 135 * 136 * @param dataNode 137 */ 138 private void saveData(RulesTreeNode dataNode) { 139 if ((dataNode != null) && m_isEditing) { 140 if (dataNode.isRuleSet() || dataNode.isRule() || dataNode.isProperty()) { 141 String ruleSetName = m_name.getText(); 142 143 if (ruleSetName.length() == 0) { 144 String message = "The rule set name is missing. The change will not be applied."; 145 boolean hasFocus = m_name.hasFocus(); 146 147 m_name.removeFocusListener(m_focusListener); 148 MessageDialog.show(getParentWindow(), message); 149 m_name.addFocusListener(m_focusListener); 150 151 if (hasFocus) { 152 m_name.requestFocus(); 153 } 154 155 ruleSetName = m_originalName; 156 } else if (ruleSetName.equalsIgnoreCase(m_originalName) == false) { 157 if (dataNode.getSibling(ruleSetName) != null) { 158 String template = "Another rule set already has the name \"{0}\". The change will not be applied."; 159 String[] args = {ruleSetName}; 160 String message = MessageFormat.format(template, args); 161 boolean hasFocus = m_name.hasFocus(); 162 163 m_name.removeFocusListener(m_focusListener); 164 MessageDialog.show(getParentWindow(), message); 165 m_name.addFocusListener(m_focusListener); 166 167 if (hasFocus) { 168 m_name.requestFocus(); 169 } 170 171 ruleSetName = m_originalName; 172 } 173 } 174 175 dataNode.setName(ruleSetName); 176 dataNode.setDescription(m_description.getText()); 177 } 178 } 179 } 180 181 /*** 182 ******************************************************************************* 183 * 184 * @param isEditing 185 */ 186 protected void setIsEditing(boolean isEditing) { 187 m_isEditing = isEditing; 188 } 189 190 /*** 191 ******************************************************************************* 192 * 193 * @param dataNode 194 */ 195 private void loadData(RulesTreeNode dataNode) { 196 if (dataNode == null) { 197 enableData(false); 198 } else if (dataNode.isRuleSet()) { 199 loadData_(dataNode); 200 } else if (dataNode.isRule()) { 201 loadData_(dataNode.getParentRuleSetData()); 202 } else if (dataNode.isProperty()) { 203 loadData_(dataNode.getParentRuleSetData()); 204 } else { 205 enableData(false); 206 } 207 } 208 209 /*** 210 ******************************************************************************* 211 * 212 * @param dataNode 213 */ 214 private void loadData_(RulesTreeNode dataNode) { 215 if (m_enabled == false) { 216 enableData(true); 217 } 218 219 m_name.setText(dataNode.getName()); 220 m_description.setText(dataNode.getDescription()); 221 m_originalName = dataNode.getName(); 222 m_currentDataNode = dataNode; 223 } 224 225 /*** 226 ******************************************************************************* 227 * 228 */ 229 private void enableData(boolean enable) { 230 if (enable) { 231 // Just to be sure the focus listener isn't set. 232 m_name.removeFocusListener(m_focusListener); 233 m_name.addFocusListener(m_focusListener); 234 235 m_nameLabel.setEnabled(true); 236 237 m_name.setEnabled(true); 238 m_name.setBackground(Color.white); 239 240 m_descriptionLabel.setEnabled(true); 241 242 m_description.setEnabled(true); 243 m_description.setBackground(Color.white); 244 } else { 245 m_name.removeFocusListener(m_focusListener); 246 247 Color background = UIManager.getColor("disabledTextBackground"); 248 249 m_nameLabel.setEnabled(false); 250 251 m_name.setText(""); 252 m_name.setEnabled(false); 253 m_name.setBackground(background); 254 255 m_descriptionLabel.setEnabled(false); 256 257 m_description.setText(""); 258 m_description.setEnabled(false); 259 m_description.setBackground(background); 260 261 m_currentDataNode = null; 262 } 263 264 m_enabled = enable; 265 } 266 267 /*** 268 ******************************************************************************* 269 * 270 * @return 271 */ 272 private Window getParentWindow() { 273 Component component = getParent(); 274 275 while ((component != null) && ((component instanceof Window) == false)) { 276 component = component.getParent(); 277 } 278 279 return (Window) component; 280 } 281 282 /*** 283 ************************************************************************************ 284 ************************************************************************************ 285 ************************************************************************************ 286 */ 287 private class RuleSetNameFocusListener implements FocusListener { 288 289 /*** 290 ************************************************************************** 291 * 292 * @param event 293 */ 294 public void focusGained(FocusEvent event) { 295 } 296 297 /*** 298 ************************************************************************** 299 * 300 * @param event 301 */ 302 public void focusLost(FocusEvent event) { 303 String ruleSetName = m_name.getText().trim(); 304 305 if (ruleSetName.length() == 0) { 306 String message = "The rule set name is missing."; 307 m_name.removeFocusListener(this); 308 MessageDialog.show(getParentWindow(), message); 309 m_name.addFocusListener(this); 310 m_name.requestFocus(); 311 } else if (ruleSetName.equalsIgnoreCase(m_originalName) == false) { 312 if (m_currentDataNode.getSibling(ruleSetName) != null) { 313 String template = "Another rule set already has the name \"{0}\"."; 314 String[] args = {ruleSetName}; 315 String message = MessageFormat.format(template, args); 316 m_name.removeFocusListener(this); 317 MessageDialog.show(getParentWindow(), message); 318 m_name.addFocusListener(this); 319 m_name.requestFocus(); 320 } 321 } 322 } 323 } 324 325 /*** 326 ************************************************************************************ 327 ************************************************************************************ 328 ************************************************************************************ 329 */ 330 private class RulesEditingEventHandler implements RulesEditingEventListener { 331 332 /*** 333 ************************************************************************* 334 * 335 * @param event 336 */ 337 public void loadData(RulesEditingEvent event) { 338 RuleSetEditingPanel.this.loadData(event.getDataNode()); 339 } 340 341 /*** 342 ************************************************************************* 343 * 344 * @param event 345 */ 346 public void saveData(RulesEditingEvent event) { 347 RuleSetEditingPanel.this.saveData(event.getDataNode()); 348 } 349 } 350 }

This page was automatically generated by Maven