001: package org.acm.seguin.pmd.swingui;
002:
003: import org.acm.seguin.pmd.Rule;
004: import org.acm.seguin.pmd.swingui.event.ListenerList;
005: import org.acm.seguin.pmd.swingui.event.RulesEditingEvent;
006: import org.acm.seguin.pmd.swingui.event.RulesEditingEventListener;
007:
008: import javax.swing.JComboBox;
009: import javax.swing.JLabel;
010: import javax.swing.JPanel;
011: import javax.swing.JScrollPane;
012: import javax.swing.JTextArea;
013: import javax.swing.JTextField;
014: import javax.swing.UIManager;
015: import javax.swing.border.EmptyBorder;
016: import javax.swing.border.TitledBorder;
017: import java.awt.BorderLayout;
018: import java.awt.Color;
019: import java.awt.Component;
020: import java.awt.Font;
021: import java.awt.GridBagConstraints;
022: import java.awt.GridBagLayout;
023: import java.awt.Insets;
024: import java.awt.Window;
025: import java.awt.event.FocusEvent;
026: import java.awt.event.FocusListener;
027: import java.text.MessageFormat;
028:
029: /**
030: *
031: * @author Donald A. Leckie
032: * @since August 29, 2002
033: * @version $Revision: 1.1 $, $Date: 2003/07/29 20:51:59 $
034: */
035: class RuleEditingPanel extends JPanel {
036:
037: private JLabel m_nameLabel;
038: private JTextField m_name;
039: private JLabel m_classNameLabel;
040: private JTextField m_className;
041: private JLabel m_messageLabel;
042: private JTextArea m_message;
043: private JScrollPane m_messageScrollPane;
044: private JLabel m_descriptionLabel;
045: private JTextArea m_description;
046: private JScrollPane m_descriptionScrollPane;
047: private JLabel m_exampleLabel;
048: private JTextArea m_example;
049: private JScrollPane m_exampleScrollPane;
050: private JLabel m_priorityLabel;
051: private JComboBox m_priority;
052: private boolean m_enabled;
053: private RulesTreeNode m_currentDataNode;
054: private boolean m_isEditing;
055: private String m_originalName;
056: private FocusListener m_focusListener = new RuleNameFocusListener();
057:
058: /**
059: *******************************************************************************
060: *
061: * @return
062: */
063: protected RuleEditingPanel() {
064: super (new BorderLayout());
065:
066: EmptyBorder emptyBorder = new EmptyBorder(5, 5, 5, 5);
067:
068: setBorder(emptyBorder);
069:
070: JPanel panel;
071: TitledBorder titledBorder;
072: GridBagLayout layout;
073: GridBagConstraints constraints;
074:
075: int[] columnWidths = { 50, 100, 100, 100, 100, 100 };
076:
077: layout = new GridBagLayout();
078: layout.columnWidths = columnWidths;
079: panel = new JPanel(layout);
080: titledBorder = ComponentFactory.createTitledBorder(" Rule ");
081:
082: panel.setBorder(titledBorder);
083: add(panel, BorderLayout.CENTER);
084:
085: Font labelFont = UIManager.getFont("labelFont");
086:
087: // Rule Name Label
088: m_nameLabel = new JLabel("Name");
089: m_nameLabel.setFont(labelFont);
090: m_nameLabel.setHorizontalAlignment(JLabel.RIGHT);
091: constraints = layout.getConstraints(m_nameLabel);
092: constraints.gridx = 0;
093: constraints.gridy = 0;
094: constraints.gridwidth = 1;
095: constraints.gridheight = 1;
096: constraints.anchor = GridBagConstraints.NORTHEAST;
097: constraints.fill = GridBagConstraints.NONE;
098: constraints.insets = new Insets(4, 2, 4, 2);
099: panel.add(m_nameLabel, constraints);
100:
101: // Rule Name Text
102: m_name = new JTextField();
103: m_name.setFont(UIManager.getFont("dataFont"));
104: m_name.addFocusListener(m_focusListener);
105: m_name.setRequestFocusEnabled(true);
106: m_name.setOpaque(true);
107: constraints = layout.getConstraints(m_name);
108: constraints.gridx = 1;
109: constraints.gridy = 0;
110: constraints.gridwidth = 2;
111: constraints.gridheight = 1;
112: constraints.anchor = GridBagConstraints.NORTHWEST;
113: constraints.fill = GridBagConstraints.BOTH;
114: constraints.insets = new Insets(4, 2, 4, 2);
115: panel.add(m_name, constraints);
116:
117: // Rule Class Name Label
118: m_classNameLabel = new JLabel("Class Name");
119: m_classNameLabel.setFont(labelFont);
120: m_classNameLabel.setHorizontalAlignment(JLabel.RIGHT);
121: constraints = layout.getConstraints(m_name);
122: constraints.gridx = 0;
123: constraints.gridy = 1;
124: constraints.gridwidth = 1;
125: constraints.gridheight = 1;
126: constraints.anchor = GridBagConstraints.NORTHEAST;
127: constraints.fill = GridBagConstraints.NONE;
128: constraints.insets = new Insets(4, 2, 4, 2);
129: panel.add(m_classNameLabel, constraints);
130:
131: // Rule Class Name Text
132: m_className = new JTextField();
133: m_className.setFont(UIManager.getFont("dataFont"));
134: m_className.setBackground(UIManager
135: .getColor("disabledTextBackground"));
136: m_className.setForeground(Color.black);
137: m_className.setSelectedTextColor(Color.black);
138: m_className.setDisabledTextColor(Color.black);
139: m_className.setEditable(false);
140: m_className.setEnabled(false);
141: m_className.setOpaque(true);
142: constraints = layout.getConstraints(m_name);
143: constraints.gridx = 1;
144: constraints.gridy = 1;
145: constraints.gridwidth = GridBagConstraints.REMAINDER;
146: constraints.gridheight = 1;
147: constraints.anchor = GridBagConstraints.NORTHWEST;
148: constraints.fill = GridBagConstraints.BOTH;
149: constraints.insets = new Insets(4, 2, 4, 2);
150: panel.add(m_className, constraints);
151:
152: // Rule Message Label
153: m_messageLabel = new JLabel("Message");
154: m_messageLabel.setFont(labelFont);
155: m_messageLabel.setHorizontalAlignment(JLabel.RIGHT);
156: constraints = layout.getConstraints(m_name);
157: constraints.gridx = 0;
158: constraints.gridy = 2;
159: constraints.gridwidth = 1;
160: constraints.gridheight = 1;
161: constraints.anchor = GridBagConstraints.NORTHEAST;
162: constraints.fill = GridBagConstraints.NONE;
163: constraints.insets = new Insets(4, 2, 4, 2);
164: panel.add(m_messageLabel, constraints);
165:
166: // Rule Message Text
167: m_message = ComponentFactory.createTextArea("");
168:
169: // Rule Message Scroll Pane;
170: m_messageScrollPane = ComponentFactory
171: .createScrollPane(m_message);
172: constraints = layout.getConstraints(m_name);
173: constraints.gridx = 1;
174: constraints.gridy = 2;
175: constraints.gridwidth = GridBagConstraints.REMAINDER;
176: constraints.gridheight = 1;
177: constraints.ipady = 4 * 20; // 4 lines * 20 pixels/line
178: constraints.anchor = GridBagConstraints.NORTHWEST;
179: constraints.fill = GridBagConstraints.BOTH;
180: constraints.insets = new Insets(4, 2, 4, 2);
181: panel.add(m_messageScrollPane, constraints);
182:
183: // Rule Description Label
184: m_descriptionLabel = new JLabel("Description");
185: m_descriptionLabel.setFont(labelFont);
186: m_descriptionLabel.setHorizontalAlignment(JLabel.RIGHT);
187: constraints = layout.getConstraints(m_name);
188: constraints.gridx = 0;
189: constraints.gridy = 3;
190: constraints.gridwidth = 1;
191: constraints.gridheight = 1;
192: constraints.anchor = GridBagConstraints.NORTHEAST;
193: constraints.fill = GridBagConstraints.NONE;
194: constraints.insets = new Insets(4, 2, 4, 2);
195: panel.add(m_descriptionLabel, constraints);
196:
197: // Rule Description Text
198: m_description = ComponentFactory.createTextArea("");
199:
200: // Rule Description Scroll Pane;
201: m_descriptionScrollPane = ComponentFactory
202: .createScrollPane(m_description);
203: constraints = layout.getConstraints(m_name);
204: constraints.gridx = 1;
205: constraints.gridy = 3;
206: constraints.gridwidth = GridBagConstraints.REMAINDER;
207: constraints.gridheight = 1;
208: constraints.ipady = 4 * 20; // 4 lines * 20 pixels/line
209: constraints.anchor = GridBagConstraints.NORTHWEST;
210: constraints.fill = GridBagConstraints.BOTH;
211: constraints.insets = new Insets(4, 2, 4, 2);
212: panel.add(m_descriptionScrollPane, constraints);
213:
214: // Rule Example Label
215: m_exampleLabel = new JLabel("Example");
216: m_exampleLabel.setFont(labelFont);
217: m_exampleLabel.setHorizontalAlignment(JLabel.RIGHT);
218: constraints = layout.getConstraints(m_name);
219: constraints.gridx = 0;
220: constraints.gridy = 4;
221: constraints.gridwidth = 1;
222: constraints.gridheight = 1;
223: constraints.anchor = GridBagConstraints.NORTHEAST;
224: constraints.fill = GridBagConstraints.NONE;
225: constraints.insets = new Insets(4, 2, 4, 2);
226: panel.add(m_exampleLabel, constraints);
227:
228: // Rule Example Text
229: m_example = ComponentFactory.createTextArea("");
230: m_example.setFont(UIManager.getFont("codeFont"));
231:
232: // Rule Example Scroll Pane;
233: m_exampleScrollPane = ComponentFactory
234: .createScrollPane(m_example);
235: constraints = layout.getConstraints(m_name);
236: constraints.gridx = 1;
237: constraints.gridy = 4;
238: constraints.gridwidth = GridBagConstraints.REMAINDER;
239: constraints.gridheight = 1;
240: constraints.ipady = 6 * 20; // 6 lines * 20 pixels/line
241: constraints.anchor = GridBagConstraints.NORTHWEST;
242: constraints.fill = GridBagConstraints.BOTH;
243: constraints.insets = new Insets(4, 2, 4, 2);
244: panel.add(m_exampleScrollPane, constraints);
245:
246: // Rule Priority Label
247: m_priorityLabel = new JLabel("Priority");
248: m_priorityLabel.setFont(labelFont);
249: m_priorityLabel.setHorizontalAlignment(JLabel.RIGHT);
250: constraints = layout.getConstraints(m_name);
251: constraints.gridx = 0;
252: constraints.gridy = 5;
253: constraints.gridwidth = 1;
254: constraints.gridheight = 1;
255: constraints.anchor = GridBagConstraints.NORTHEAST;
256: constraints.fill = GridBagConstraints.NONE;
257: constraints.insets = new Insets(4, 2, 4, 2);
258: panel.add(m_priorityLabel, constraints);
259:
260: // Rule Priority
261: m_priority = new JComboBox(Rule.PRIORITIES);
262: constraints = layout.getConstraints(m_name);
263: constraints.gridx = 1;
264: constraints.gridy = 5;
265: constraints.gridwidth = 1;
266: constraints.gridheight = 1;
267: constraints.anchor = GridBagConstraints.WEST;
268: constraints.fill = GridBagConstraints.NONE;
269: constraints.insets = new Insets(4, 2, 4, 2);
270: panel.add(m_priority, constraints);
271:
272: enableData(false);
273:
274: ListenerList.addListener(new RulesEditingEventHandler());
275: }
276:
277: /**
278: *******************************************************************************
279: *
280: * @param dataNode
281: */
282: private void saveData(RulesTreeNode dataNode) {
283: if ((dataNode != null) && m_isEditing) {
284: if (dataNode.isRule() || dataNode.isProperty()) {
285: String ruleName = m_name.getText().trim();
286:
287: if (ruleName.length() == 0) {
288: String message = "The rule name is missing. The change will not be applied.";
289:
290: m_name.removeFocusListener(m_focusListener);
291: MessageDialog.show(getParentWindow(), message);
292: m_name.addFocusListener(m_focusListener);
293:
294: if (m_name.hasFocus()) {
295: m_name.requestFocus();
296: }
297:
298: ruleName = m_originalName;
299: } else if (ruleName.equalsIgnoreCase(m_originalName) == false) {
300: if (dataNode.getSibling(ruleName) != null) {
301: String template = "Another rule already has the name \"{0}\". The change will not be applied.";
302: String[] args = { ruleName };
303: String message = MessageFormat.format(template,
304: args);
305:
306: m_name.removeFocusListener(m_focusListener);
307: MessageDialog.show(getParentWindow(), message);
308: m_name.addFocusListener(m_focusListener);
309:
310: if (m_name.hasFocus()) {
311: m_name.requestFocus();
312: }
313:
314: ruleName = m_originalName;
315: }
316: }
317:
318: dataNode.setName(ruleName);
319: dataNode.setClassName(m_className.getText());
320: dataNode.setMessage(m_message.getText());
321: dataNode.setDescription(m_description.getText());
322: dataNode.setExample(m_example.getText());
323: dataNode.setPriority(m_priority.getSelectedIndex() + 1);
324: }
325: }
326: }
327:
328: /**
329: *******************************************************************************
330: *
331: * @param isEditing
332: */
333: protected void setIsEditing(boolean isEditing) {
334: m_isEditing = isEditing;
335: }
336:
337: /**
338: *******************************************************************************
339: *
340: * @param dataNode
341: */
342: private void loadData(RulesTreeNode dataNode) {
343: if (dataNode == null) {
344: enableData(false);
345: } else if (dataNode.isRuleSet()) {
346: enableData(false);
347: } else if (dataNode.isRule()) {
348: loadData_(dataNode);
349: } else if (dataNode.isProperty()) {
350: loadData_(dataNode.getParentRuleData());
351: } else {
352: enableData(false);
353: }
354: }
355:
356: /**
357: *******************************************************************************
358: *
359: * @param dataNode
360: */
361: private void loadData_(RulesTreeNode dataNode) {
362: if (m_enabled == false) {
363: enableData(true);
364: }
365:
366: m_name.setText(dataNode.getName());
367: m_className.setText(dataNode.getClassName());
368: m_message.setText(dataNode.getMessage());
369: m_description.setText(dataNode.getDescription());
370: m_example.setText(dataNode.getExample());
371: m_priority.setSelectedIndex(dataNode.getPriority() - 1);
372: m_originalName = dataNode.getName();
373: m_currentDataNode = dataNode;
374: }
375:
376: /**
377: *******************************************************************************
378: *
379: */
380: private void enableData(boolean enable) {
381: if (enable) {
382: // Just to be sure the focus listener isn't already set.
383: m_name.removeFocusListener(m_focusListener);
384: m_name.addFocusListener(m_focusListener);
385:
386: m_nameLabel.setEnabled(true);
387:
388: m_name.setEnabled(true);
389: m_name.setBackground(Color.white);
390:
391: m_messageLabel.setEnabled(true);
392:
393: m_message.setEnabled(true);
394: m_message.setBackground(Color.white);
395:
396: m_classNameLabel.setEnabled(true);
397:
398: m_descriptionLabel.setEnabled(true);
399:
400: m_description.setEnabled(true);
401: m_description.setBackground(Color.white);
402:
403: m_exampleLabel.setEnabled(true);
404:
405: m_example.setEnabled(true);
406: m_example.setBackground(Color.white);
407:
408: m_priorityLabel.setEnabled(true);
409:
410: m_priority.setEnabled(true);
411: m_priority.setBackground(Color.white);
412: } else {
413: m_name.removeFocusListener(m_focusListener);
414:
415: Color background = UIManager
416: .getColor("disabledTextBackground");
417:
418: m_nameLabel.setEnabled(false);
419:
420: m_name.setText("");
421: m_name.setEnabled(false);
422: m_name.setBackground(background);
423:
424: m_messageLabel.setEnabled(false);
425:
426: m_message.setText("");
427: m_message.setEnabled(false);
428: m_message.setBackground(background);
429:
430: m_classNameLabel.setEnabled(false);
431:
432: m_className.setText("");
433:
434: m_descriptionLabel.setEnabled(false);
435:
436: m_description.setText("");
437: m_description.setEnabled(false);
438: m_description.setBackground(background);
439:
440: m_exampleLabel.setEnabled(false);
441:
442: m_example.setText("");
443: m_example.setEnabled(false);
444: m_example.setBackground(background);
445:
446: m_priorityLabel.setEnabled(false);
447:
448: m_priority.setSelectedIndex(0);
449: m_priority.setEnabled(false);
450: m_priority.setBackground(background);
451:
452: m_currentDataNode = null;
453: }
454:
455: m_enabled = enable;
456: }
457:
458: /**
459: *******************************************************************************
460: *
461: * @return
462: */
463: private Window getParentWindow() {
464: Component component = getParent();
465:
466: while ((component != null)
467: && ((component instanceof Window) == false)) {
468: component = component.getParent();
469: }
470:
471: return (Window) component;
472: }
473:
474: /**
475: ************************************************************************************
476: ************************************************************************************
477: ************************************************************************************
478: */
479:
480: private class RuleNameFocusListener implements FocusListener {
481:
482: /**
483: **************************************************************************
484: *
485: * @param event
486: */
487: public void focusGained(FocusEvent event) {
488: }
489:
490: /**
491: **************************************************************************
492: *
493: * @param event
494: */
495: public void focusLost(FocusEvent event) {
496: String ruleName = m_name.getText().trim();
497:
498: if (ruleName.length() == 0) {
499: String message = "The rule name is missing.";
500: m_name.removeFocusListener(this );
501: MessageDialog.show(getParentWindow(), message);
502: m_name.addFocusListener(this );
503: m_name.requestFocus();
504: } else if (ruleName.equalsIgnoreCase(m_originalName) == false) {
505: if (m_currentDataNode.getSibling(ruleName) != null) {
506: String template = "Another rule already has the name \"{0}\".";
507: String[] args = { ruleName };
508: String message = MessageFormat.format(template,
509: args);
510: m_name.removeFocusListener(this );
511: MessageDialog.show(getParentWindow(), message);
512: m_name.addFocusListener(this );
513: m_name.requestFocus();
514: }
515: }
516: }
517: }
518:
519: /**
520: ************************************************************************************
521: ************************************************************************************
522: ************************************************************************************
523: */
524: private class RulesEditingEventHandler implements
525: RulesEditingEventListener {
526:
527: /**
528: *************************************************************************
529: *
530: * @param event
531: */
532: public void loadData(RulesEditingEvent event) {
533: RuleEditingPanel.this .loadData(event.getDataNode());
534: }
535:
536: /**
537: *************************************************************************
538: *
539: * @param event
540: */
541: public void saveData(RulesEditingEvent event) {
542: RuleEditingPanel.this.saveData(event.getDataNode());
543: }
544: }
545: }
|