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.SetupFilesEvent;
005: import org.acm.seguin.pmd.swingui.event.SetupFilesEventListener;
006: import org.acm.seguin.pmd.swingui.event.StatusBarEvent;
007:
008: import javax.swing.BorderFactory;
009: import javax.swing.Icon;
010: import javax.swing.JTree;
011: import javax.swing.UIManager;
012: import javax.swing.border.EtchedBorder;
013: import javax.swing.tree.DefaultTreeCellRenderer;
014: import javax.swing.tree.TreePath;
015: import java.awt.Component;
016: import java.io.File;
017:
018: /**
019: *
020: * @author Donald A. Leckie
021: * @since August 17, 2002
022: * @version $Revision: 1.1 $, $Date: 2003/07/29 20:51:59 $
023: */
024: class DirectoryTree extends JTree {
025:
026: /**
027: *******************************************************************************
028: *
029: */
030: protected DirectoryTree(String rootName) {
031: super (new DirectoryTreeModel(rootName));
032:
033: setRootVisible(true);
034: setBorder(BorderFactory
035: .createEtchedBorder(EtchedBorder.LOWERED));
036: setCellRenderer(new DirectoryTreeNodeRenderer());
037: ((DirectoryTreeModel) getModel()).setDirectoryTree(this );
038: setBackground(UIManager.getColor("pmdTreeBackground"));
039: ListenerList
040: .addListener((SetupFilesEventListener) new SetupFilesEventHandler());
041: }
042:
043: /**
044: *******************************************************************************
045: *
046: */
047: protected void expandRootNode() {
048: DirectoryTreeModel treeModel = (DirectoryTreeModel) getModel();
049: DirectoryTreeNode treeNode = (DirectoryTreeNode) treeModel
050: .getRoot();
051: TreePath treePath = new TreePath(treeNode.getPath());
052: expandPath(treePath);
053: }
054:
055: /**
056: *******************************************************************************
057: *
058: */
059: protected DirectoryTreeNode getSelectedNode() {
060: TreePath path = getSelectionModel().getSelectionPath();
061:
062: return (DirectoryTreeNode) path.getLastPathComponent();
063: }
064:
065: /**
066: *********************************************************************************
067: *********************************************************************************
068: *********************************************************************************
069: */
070: private class SetupFilesEventHandler implements
071: SetupFilesEventListener {
072:
073: /**
074: ****************************************************************************
075: *
076: * @param event
077: */
078: public void startSetup(SetupFilesEvent event) {
079: }
080:
081: /**
082: ****************************************************************************
083: *
084: * @param event
085: */
086: public void stopSetup(SetupFilesEvent event) {
087: }
088:
089: /**
090: ****************************************************************************
091: *
092: * @param event
093: */
094: public void setFileList(SetupFilesEvent event) {
095: File[] directories = event.getFileList();
096: String name = "Locating root directories.";
097: SetupFilesThread setupFilesThread = new SetupFilesThread(
098: name, directories);
099: setupFilesThread.start();
100: }
101: }
102:
103: /**
104: ********************************************************************************
105: ********************************************************************************
106: ********************************************************************************
107: */
108: private class SetupFilesThread extends Thread {
109: private File[] m_rootDirectories;
110:
111: /**
112: ****************************************************************************
113: *
114: * @param name
115: */
116: private SetupFilesThread(String threadName,
117: File[] rootDirectories) {
118: super (threadName);
119:
120: m_rootDirectories = rootDirectories;
121: }
122:
123: /**
124: ***************************************************************************
125: *
126: */
127: public void run() {
128: setup();
129: process();
130: cleanup();
131: }
132:
133: /**
134: ***************************************************************************
135: *
136: */
137: protected void setup() {
138: SetupFilesEvent.notifyStartSetup(this );
139: StatusBarEvent.notifyStartAnimation(this );
140: }
141:
142: /**
143: ***************************************************************************
144: *
145: */
146: protected void process() {
147: StatusBarEvent.notifyShowMessage(this ,
148: "Locating root directories. Please wait...");
149: DirectoryTreeModel treeModel = (DirectoryTreeModel) getModel();
150: treeModel.setupFiles(m_rootDirectories);
151: expandRootNode();
152: }
153:
154: /**
155: ***************************************************************************
156: *
157: */
158: protected void cleanup() {
159: StatusBarEvent.notifyStopAnimation(this );
160: SetupFilesEvent.notifyStopSetup(this );
161: }
162: }
163:
164: /**
165: ********************************************************************************
166: ********************************************************************************
167: ********************************************************************************
168: */
169: private class DirectoryTreeNodeRenderer extends
170: DefaultTreeCellRenderer {
171:
172: private Icon m_defaultClosedIcon;
173: private Icon m_defaultLeafIcon;
174: private Icon m_defaultOpenIcon;
175:
176: /**
177: ***************************************************************************
178: *
179: */
180: protected DirectoryTreeNodeRenderer() {
181: super ();
182:
183: m_defaultClosedIcon = getDefaultClosedIcon();
184: m_defaultLeafIcon = getDefaultLeafIcon();
185: m_defaultOpenIcon = getDefaultOpenIcon();
186: setBackgroundNonSelectionColor(UIManager
187: .getColor("pmdTreeBackground"));
188: }
189:
190: /**
191: **************************************************************************
192: *
193: * @param tree
194: * @param object
195: * @param isSelected
196: * @param isExpanded
197: * @param isLeaf
198: * @param row
199: * @param hasFocus
200: *
201: * @return
202: */
203: public Component getTreeCellRendererComponent(JTree tree,
204: Object object, boolean isSelected, boolean isExpanded,
205: boolean isLeaf, int row, boolean hasFocus) {
206: DirectoryTreeNode treeNode = (DirectoryTreeNode) object;
207: Object userObject = treeNode.getUserObject();
208:
209: if (userObject instanceof String) {
210: // The root node will display either an open or closed icon.
211: setClosedIcon(m_defaultClosedIcon);
212: setLeafIcon(m_defaultClosedIcon);
213: setOpenIcon(m_defaultOpenIcon);
214: } else if (((File) userObject).isFile()) {
215: // A file cannot have any children; therefore, the default icon settings are used.
216: setClosedIcon(m_defaultClosedIcon);
217: setLeafIcon(m_defaultLeafIcon);
218: setOpenIcon(m_defaultOpenIcon);
219: } else {
220: // A directory may or may not have children. The following conditions are used:
221: //
222: // For a file
223: // files are not viewed in the tree
224: // For a directory
225: // has no children --- use closed folder icon
226: // has children
227: // is expanded --- use open folder icon
228: // is collapsed --- use closed folder icon
229: //
230: setClosedIcon(m_defaultClosedIcon);
231: setLeafIcon(m_defaultClosedIcon);
232: setOpenIcon(m_defaultOpenIcon);
233: }
234:
235: return super.getTreeCellRendererComponent(tree, object,
236: isSelected, isExpanded, isLeaf, row, hasFocus);
237: }
238: }
239: }
|