001: package org.acm.seguin.pmd.swingui;
002:
003: import org.acm.seguin.pmd.swingui.event.RulesEditingEvent;
004:
005: import javax.swing.JPanel;
006: import javax.swing.JTabbedPane;
007: import javax.swing.SwingUtilities;
008: import javax.swing.UIManager;
009: import javax.swing.event.ChangeEvent;
010: import javax.swing.event.ChangeListener;
011: import javax.swing.event.TreeSelectionEvent;
012: import javax.swing.event.TreeSelectionListener;
013: import javax.swing.tree.TreePath;
014:
015: /**
016: *
017: * @author Donald A. Leckie
018: * @since August 29, 2002
019: * @version $Revision: 1.1 $, $Date: 2003/07/29 20:51:59 $
020: */
021: class RuleEditingTabbedPane extends JTabbedPane {
022:
023: private RulesTree m_rulesTree;
024: private RuleAllEditingPanel m_ruleAllPanel;
025: private RuleSetEditingPanel m_ruleSetPanel;
026: private RuleEditingPanel m_rulePanel;
027: private RulePropertyEditingPanel m_rulePropertyPanel;
028: private RulesTreeNode m_currentTreeNode;
029:
030: /**
031: *******************************************************************************
032: *
033: * @return
034: */
035: protected RuleEditingTabbedPane(RulesTree rulesTree) {
036: super (JTabbedPane.TOP);
037:
038: m_rulesTree = rulesTree;
039: m_ruleAllPanel = new RuleAllEditingPanel();
040: m_ruleSetPanel = new RuleSetEditingPanel();
041: m_rulePanel = new RuleEditingPanel();
042: m_rulePropertyPanel = new RulePropertyEditingPanel();
043:
044: addTab("All", m_ruleAllPanel);
045: addTab("Rule Set", m_ruleSetPanel);
046: addTab("Rule", m_rulePanel);
047: addTab("Property", m_rulePropertyPanel);
048: setFont(UIManager.getFont("tabFont"));
049:
050: m_ruleAllPanel.setIsEditing(true);
051:
052: rulesTree
053: .addTreeSelectionListener(new RulesTreeSelectionListener());
054: addChangeListener(new TabChangeListener());
055: }
056:
057: /**
058: *******************************************************************************
059: *
060: * @return
061: */
062: protected RulesTreeNode getSelectedTreeNode() {
063: return m_rulesTree.getSelectedNode();
064: }
065:
066: /**
067: *******************************************************************************
068: *
069: */
070: protected void saveData() {
071: if (m_currentTreeNode != null) {
072: RulesEditingEvent.notifySaveData(this , m_currentTreeNode);
073: }
074: }
075:
076: /**
077: *******************************************************************************
078: *
079: */
080: private void loadData(RulesTreeNode treeNode) {
081: JPanel editingTab = (JPanel) getSelectedComponent();
082:
083: if (editingTab == m_ruleAllPanel) {
084: m_ruleAllPanel.setIsEditing(true);
085: m_rulePanel.setIsEditing(false);
086: m_rulePropertyPanel.setIsEditing(false);
087: } else if (editingTab == m_rulePanel) {
088: m_ruleAllPanel.setIsEditing(false);
089: m_rulePanel.setIsEditing(true);
090: m_rulePropertyPanel.setIsEditing(false);
091: } else {
092: m_ruleAllPanel.setIsEditing(false);
093: m_rulePanel.setIsEditing(false);
094: m_rulePropertyPanel.setIsEditing(true);
095: }
096:
097: m_currentTreeNode = treeNode;
098: RulesEditingEvent.notifyLoadData(this , treeNode);
099: }
100:
101: /**
102: *******************************************************************************
103: *******************************************************************************
104: *******************************************************************************
105: */
106: private class TabChangeListener implements ChangeListener {
107:
108: /**
109: *******************************************************************************
110: *
111: * @param event
112: */
113: public void stateChanged(ChangeEvent event) {
114: RulesTreeNode selectedTreeNode = getSelectedTreeNode();
115: saveData();
116: loadData(selectedTreeNode);
117: SwingUtilities.invokeLater(new SortChildren(
118: selectedTreeNode));
119: }
120: }
121:
122: /**
123: *******************************************************************************
124: *******************************************************************************
125: *******************************************************************************
126: */
127: private class RulesTreeSelectionListener implements
128: TreeSelectionListener {
129:
130: /**
131: *******************************************************************************
132: *
133: * @param event
134: */
135: public void valueChanged(TreeSelectionEvent event) {
136: TreePath treePath = event.getPath();
137: Object component = treePath.getLastPathComponent();
138:
139: if (component instanceof RulesTreeNode) {
140: saveData();
141:
142: RulesTreeNode treeNode = (RulesTreeNode) component;
143:
144: if (getSelectedComponent() != m_ruleAllPanel) {
145: if (treeNode.isRuleSet()) {
146: setSelectedComponent(m_ruleSetPanel);
147: } else if (treeNode.isRule()) {
148: setSelectedComponent(m_rulePanel);
149: } else if (treeNode.isProperty()) {
150: setSelectedComponent(m_rulePropertyPanel);
151: }
152: }
153:
154: if (treeNode.getParent() == null) {
155: // Get next to last tree node in tree path.
156: int pathIndex = treePath.getPathCount() - 2;
157:
158: if (pathIndex >= 0) {
159: treeNode = (RulesTreeNode) treePath
160: .getPathComponent(pathIndex);
161: }
162: }
163:
164: loadData(treeNode);
165: SwingUtilities.invokeLater(new SortChildren(treeNode));
166: }
167: }
168: }
169:
170: /**
171: *******************************************************************************
172: *******************************************************************************
173: *******************************************************************************
174: */
175: private class SortChildren implements Runnable {
176:
177: private RulesTreeNode m_parentNode;
178:
179: /**
180: ***************************************************************************
181: *
182: * @param parentNode
183: */
184: private SortChildren(RulesTreeNode parentNode) {
185: m_parentNode = parentNode;
186: }
187:
188: /**
189: ***************************************************************************
190: *
191: */
192: public void run() {
193: if (m_parentNode != null) {
194: m_parentNode.sortChildren();
195: }
196: }
197: }
198: }
|