001: package org.acm.seguin.pmd.swingui;
002:
003: import org.acm.seguin.pmd.swingui.event.ListenerList;
004: import org.acm.seguin.pmd.swingui.event.RulesEditingEvent;
005: import org.acm.seguin.pmd.swingui.event.RulesEditingEventListener;
006:
007: import javax.swing.JLabel;
008: import javax.swing.JPanel;
009: import javax.swing.JScrollPane;
010: import javax.swing.JTextArea;
011: import javax.swing.JTextField;
012: import javax.swing.UIManager;
013: import javax.swing.border.EmptyBorder;
014: import javax.swing.border.TitledBorder;
015: import java.awt.BorderLayout;
016: import java.awt.Color;
017: import java.awt.Component;
018: import java.awt.GridBagConstraints;
019: import java.awt.GridBagLayout;
020: import java.awt.Insets;
021: import java.awt.Window;
022: import java.awt.event.FocusEvent;
023: import java.awt.event.FocusListener;
024: import java.text.MessageFormat;
025:
026: /**
027: *
028: * @author Donald A. Leckie
029: * @since August 29, 2002
030: * @version $Revision: 1.1 $, $Date: 2003/07/29 20:51:59 $
031: */
032: class RuleSetEditingPanel extends JPanel {
033: private JLabel m_nameLabel;
034: private JTextField m_name;
035: private JLabel m_descriptionLabel;
036: private JTextArea m_description;
037: private JScrollPane m_descriptionScrollPane;
038: private boolean m_enabled;
039: private RulesTreeNode m_currentDataNode;
040: private boolean m_isEditing;
041: private String m_originalName;
042: private FocusListener m_focusListener = new RuleSetNameFocusListener();
043:
044: /**
045: *******************************************************************************
046: *
047: */
048: protected RuleSetEditingPanel() {
049: super (new BorderLayout());
050:
051: EmptyBorder emptyBorder = new EmptyBorder(5, 5, 5, 5);
052:
053: setBorder(emptyBorder);
054:
055: GridBagLayout layout;
056: GridBagConstraints constraints;
057: JPanel panel;
058: TitledBorder titledBorder;
059:
060: int[] columnWidths = { 25, 100, 100, 100, 100, 100 };
061: layout = new GridBagLayout();
062: layout.columnWidths = columnWidths;
063: panel = new JPanel(layout);
064: titledBorder = ComponentFactory
065: .createTitledBorder(" Rule Set ");
066:
067: panel.setBorder(titledBorder);
068: add(panel, BorderLayout.CENTER);
069:
070: // Rule Set Name Label
071: m_nameLabel = new JLabel("Name");
072: m_nameLabel.setFont(UIManager.getFont("labelFont"));
073: m_nameLabel.setHorizontalAlignment(JLabel.RIGHT);
074: constraints = layout.getConstraints(m_nameLabel);
075: constraints.gridx = 0;
076: constraints.gridy = 0;
077: constraints.gridwidth = 1;
078: constraints.gridheight = 1;
079: constraints.anchor = GridBagConstraints.NORTHEAST;
080: constraints.fill = GridBagConstraints.NONE;
081: constraints.insets = new Insets(4, 2, 4, 2);
082: panel.add(m_nameLabel, constraints);
083:
084: // Rule Set Name Text
085: m_name = new JTextField();
086: m_name.setFont(UIManager.getFont("dataFont"));
087: m_name.addFocusListener(m_focusListener);
088: m_name.setRequestFocusEnabled(true);
089: constraints = layout.getConstraints(m_name);
090: constraints.gridx = 1;
091: constraints.gridy = 0;
092: constraints.gridwidth = 2;
093: constraints.gridheight = 1;
094: constraints.anchor = GridBagConstraints.NORTHWEST;
095: constraints.fill = GridBagConstraints.BOTH;
096: constraints.insets = new Insets(4, 2, 4, 2);
097: panel.add(m_name, constraints);
098:
099: // Rule Set Description Label
100: m_descriptionLabel = new JLabel("Description");
101: m_descriptionLabel.setFont(UIManager.getFont("labelFont"));
102: m_descriptionLabel.setHorizontalAlignment(JLabel.RIGHT);
103: constraints = layout.getConstraints(m_nameLabel);
104: constraints.gridx = 0;
105: constraints.gridy = 1;
106: constraints.gridwidth = 1;
107: constraints.gridheight = 1;
108: constraints.anchor = GridBagConstraints.NORTHEAST;
109: constraints.fill = GridBagConstraints.NONE;
110: constraints.insets = new Insets(4, 2, 4, 2);
111: panel.add(m_descriptionLabel, constraints);
112:
113: // Rule Set Description Text
114: m_description = ComponentFactory.createTextArea("");
115:
116: // Rule Set Description Scroll Pane;
117: m_descriptionScrollPane = ComponentFactory
118: .createScrollPane(m_description);
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: constraints.ipady = 4 * 20; // 4 lines * 20 pixels/line
128: panel.add(m_descriptionScrollPane, constraints);
129:
130: enableData(false);
131:
132: ListenerList.addListener(new RulesEditingEventHandler());
133: }
134:
135: /**
136: *******************************************************************************
137: *
138: * @param dataNode
139: */
140: private void saveData(RulesTreeNode dataNode) {
141: if ((dataNode != null) && m_isEditing) {
142: if (dataNode.isRuleSet() || dataNode.isRule()
143: || dataNode.isProperty()) {
144: String ruleSetName = m_name.getText();
145:
146: if (ruleSetName.length() == 0) {
147: String message = "The rule set name is missing. The change will not be applied.";
148: boolean hasFocus = m_name.hasFocus();
149:
150: m_name.removeFocusListener(m_focusListener);
151: MessageDialog.show(getParentWindow(), message);
152: m_name.addFocusListener(m_focusListener);
153:
154: if (hasFocus) {
155: m_name.requestFocus();
156: }
157:
158: ruleSetName = m_originalName;
159: } else if (ruleSetName.equalsIgnoreCase(m_originalName) == false) {
160: if (dataNode.getSibling(ruleSetName) != null) {
161: String template = "Another rule set already has the name \"{0}\". The change will not be applied.";
162: String[] args = { ruleSetName };
163: String message = MessageFormat.format(template,
164: args);
165: boolean hasFocus = m_name.hasFocus();
166:
167: m_name.removeFocusListener(m_focusListener);
168: MessageDialog.show(getParentWindow(), message);
169: m_name.addFocusListener(m_focusListener);
170:
171: if (hasFocus) {
172: m_name.requestFocus();
173: }
174:
175: ruleSetName = m_originalName;
176: }
177: }
178:
179: dataNode.setName(ruleSetName);
180: dataNode.setDescription(m_description.getText());
181: }
182: }
183: }
184:
185: /**
186: *******************************************************************************
187: *
188: * @param isEditing
189: */
190: protected void setIsEditing(boolean isEditing) {
191: m_isEditing = isEditing;
192: }
193:
194: /**
195: *******************************************************************************
196: *
197: * @param dataNode
198: */
199: private void loadData(RulesTreeNode dataNode) {
200: if (dataNode == null) {
201: enableData(false);
202: } else if (dataNode.isRuleSet()) {
203: loadData_(dataNode);
204: } else if (dataNode.isRule()) {
205: loadData_(dataNode.getParentRuleSetData());
206: } else if (dataNode.isProperty()) {
207: loadData_(dataNode.getParentRuleSetData());
208: } else {
209: enableData(false);
210: }
211: }
212:
213: /**
214: *******************************************************************************
215: *
216: * @param dataNode
217: */
218: private void loadData_(RulesTreeNode dataNode) {
219: if (m_enabled == false) {
220: enableData(true);
221: }
222:
223: m_name.setText(dataNode.getName());
224: m_description.setText(dataNode.getDescription());
225: m_originalName = dataNode.getName();
226: m_currentDataNode = dataNode;
227: }
228:
229: /**
230: *******************************************************************************
231: *
232: */
233: private void enableData(boolean enable) {
234: if (enable) {
235: // Just to be sure the focus listener isn't set.
236: m_name.removeFocusListener(m_focusListener);
237: m_name.addFocusListener(m_focusListener);
238:
239: m_nameLabel.setEnabled(true);
240:
241: m_name.setEnabled(true);
242: m_name.setBackground(Color.white);
243:
244: m_descriptionLabel.setEnabled(true);
245:
246: m_description.setEnabled(true);
247: m_description.setBackground(Color.white);
248: } else {
249: m_name.removeFocusListener(m_focusListener);
250:
251: Color background = UIManager
252: .getColor("disabledTextBackground");
253:
254: m_nameLabel.setEnabled(false);
255:
256: m_name.setText("");
257: m_name.setEnabled(false);
258: m_name.setBackground(background);
259:
260: m_descriptionLabel.setEnabled(false);
261:
262: m_description.setText("");
263: m_description.setEnabled(false);
264: m_description.setBackground(background);
265:
266: m_currentDataNode = null;
267: }
268:
269: m_enabled = enable;
270: }
271:
272: /**
273: *******************************************************************************
274: *
275: * @return
276: */
277: private Window getParentWindow() {
278: Component component = getParent();
279:
280: while ((component != null)
281: && ((component instanceof Window) == false)) {
282: component = component.getParent();
283: }
284:
285: return (Window) component;
286: }
287:
288: /**
289: ************************************************************************************
290: ************************************************************************************
291: ************************************************************************************
292: */
293: private class RuleSetNameFocusListener implements FocusListener {
294:
295: /**
296: **************************************************************************
297: *
298: * @param event
299: */
300: public void focusGained(FocusEvent event) {
301: }
302:
303: /**
304: **************************************************************************
305: *
306: * @param event
307: */
308: public void focusLost(FocusEvent event) {
309: String ruleSetName = m_name.getText().trim();
310:
311: if (ruleSetName.length() == 0) {
312: String message = "The rule set name is missing.";
313: m_name.removeFocusListener(this );
314: MessageDialog.show(getParentWindow(), message);
315: m_name.addFocusListener(this );
316: m_name.requestFocus();
317: } else if (ruleSetName.equalsIgnoreCase(m_originalName) == false) {
318: if (m_currentDataNode.getSibling(ruleSetName) != null) {
319: String template = "Another rule set already has the name \"{0}\".";
320: String[] args = { ruleSetName };
321: String message = MessageFormat.format(template,
322: args);
323: m_name.removeFocusListener(this );
324: MessageDialog.show(getParentWindow(), message);
325: m_name.addFocusListener(this );
326: m_name.requestFocus();
327: }
328: }
329: }
330: }
331:
332: /**
333: ************************************************************************************
334: ************************************************************************************
335: ************************************************************************************
336: */
337: private class RulesEditingEventHandler implements
338: RulesEditingEventListener {
339:
340: /**
341: *************************************************************************
342: *
343: * @param event
344: */
345: public void loadData(RulesEditingEvent event) {
346: RuleSetEditingPanel.this .loadData(event.getDataNode());
347: }
348:
349: /**
350: *************************************************************************
351: *
352: * @param event
353: */
354: public void saveData(RulesEditingEvent event) {
355: RuleSetEditingPanel.this.saveData(event.getDataNode());
356: }
357: }
358: }
|