View Javadoc

1   /*
2    * JDiskCatalog
3    *
4    * Copyright 2007 Przemek Więch
5    *
6    * This file is part of JDiskCatalog.
7    *
8    * JDiskCatalog is free software; you can redistribute it and/or modify
9    * it under the terms of the GNU General Public License as published by
10   * the Free Software Foundation; either version 2 of the License, or
11   * (at your option) any later version.
12   *
13   * This program is distributed in the hope that it will be useful,
14   * but WITHOUT ANY WARRANTY; without even the implied warranty of
15   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16   * GNU General Public License for more details.
17  
18   * You should have received a copy of the GNU General Public License
19   * along with this program; if not, write to the Free Software
20   * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
21   */
22  
23  package net.sf.jdiskcatalog.view;
24  
25  import java.awt.Dimension;
26  
27  import javax.swing.JComponent;
28  import javax.swing.JScrollPane;
29  import javax.swing.JTree;
30  import javax.swing.event.TreeSelectionEvent;
31  import javax.swing.event.TreeSelectionListener;
32  import javax.swing.tree.DefaultMutableTreeNode;
33  import javax.swing.tree.DefaultTreeModel;
34  import javax.swing.tree.MutableTreeNode;
35  import javax.swing.tree.TreeNode;
36  import javax.swing.tree.TreePath;
37  
38  import net.sf.jdiskcatalog.api.Document;
39  import net.sf.jdiskcatalog.api.Node;
40  import net.sf.jdiskcatalog.api.NodeInfo;
41  import net.sf.jdiskcatalog.api.NodeSelectionListener;
42  import net.sf.jdiskcatalog.api.View;
43  
44  /**
45   * Displays a tree view of all collections, disks and disk content.
46   *
47   * @author Przemek Więch <pwiech@losthive.org>
48   * @version $Id$
49   */
50  public class TreeView implements View, TreeSelectionListener
51  {
52  	private JTree tree;
53  	private JScrollPane panel;
54  
55  	private NodeSelectionListener nodeSelectionListener;
56  
57  	private Document document;
58  
59  	public TreeView()
60  	{
61  		tree = new JTree();
62  		tree.setRootVisible(false);
63          tree.addTreeSelectionListener(this);
64  		panel = new JScrollPane(tree);
65  		panel.setPreferredSize(new Dimension(180, 400));
66  	}
67  
68  	public JComponent getComponent()
69  	{
70  		return panel;
71  	}
72  
73  	public void nodeSelected(Node node)
74  	{
75  		if (node == null)
76  			return;
77  		if (!displayNode(node))
78  			node = node.getParentNode();
79  		TreePath path = getTreePath(node);
80  		tree.setSelectionPath(path);
81  		tree.scrollPathToVisible(path);
82  	}
83  
84  	private TreePath getTreePath(Node node)
85  	{
86  		TreeNode[] pathNodes = ((DefaultTreeModel)tree.getModel()).getPathToRoot((TreeNode)node);
87  		return new TreePath(pathNodes);
88  	}
89  
90  	public void setDocument(Document document)
91  	{
92  		if (document != null)
93  			tree.setModel(new DirectoryTreeModel(document));
94  		this.document = document;
95  	}
96  
97  	public void refresh()
98  	{
99  		setDocument(document);
100 	}
101 
102 	private static boolean displayNode(Node node)
103 	{
104 		return (node.getNodeInfo().getType() != NodeInfo.Type.FILE) || node.hasChildren();
105 	}
106 
107 	private static class DirectoryTreeModel extends DefaultTreeModel
108 	{
109 		public DirectoryTreeModel(Document document)
110 		{
111 			super(null);
112 			DefaultMutableTreeNode root = new DefaultMutableTreeNode();
113 			for (Node node : document.getNodes())
114 				root.add((MutableTreeNode)node);
115 			setRoot(root);
116 		}
117 
118 		@Override
119 		public Object getChild(Object parent, int index)
120 		{
121 			if (parent instanceof TreeNode)
122 			{
123 				TreeNode p = (TreeNode) parent;
124 				int j = -1;
125 				for (int i = 0; i < p.getChildCount(); i++)
126 				{
127 					Node pc = (Node) p.getChildAt(i);
128 					if (displayNode(pc))
129 						j++;
130 					if (j == index)
131 						return pc;
132 				}
133 			}
134 			return null;
135 		}
136 
137 		@Override
138 		public int getChildCount(Object parent)
139 		{
140 			int count = 0;
141 			if (parent instanceof TreeNode)
142 			{
143 				TreeNode p = (TreeNode) parent;
144 				for (int i = 0; i < p.getChildCount(); i++)
145 				{
146 					Node pc = (Node) p.getChildAt(i);
147 					if (displayNode(pc))
148 						count++;
149 				}
150 			}
151 			return count;
152 		}
153 
154 		@Override
155 		public int getIndexOfChild(Object parent, Object child)
156 		{
157 			int index = -1;
158 			if (parent instanceof TreeNode && child instanceof TreeNode)
159 			{
160 				TreeNode p = (TreeNode) parent;
161 				Node c = (Node) child;
162 				if (displayNode(c))
163 				{
164 					index = 0;
165 					for (int i = 0; i < p.getChildCount(); i++)
166 					{
167 						Node pc = (Node) p.getChildAt(i);
168 						if (pc.equals(c))
169 							return index;
170 						if (displayNode(pc))
171 							index++;
172 					}
173 				}
174 			}
175 			return index;
176 		}
177 	}
178 
179 	public void setNodeSelectionListener(NodeSelectionListener listener)
180 	{
181 		this.nodeSelectionListener = listener;
182 	}
183 
184 	public void valueChanged(TreeSelectionEvent e)
185 	{
186 		if (nodeSelectionListener == null)
187 			return;
188         TreePath path = tree.getSelectionPath();
189         if (path != null) {
190             Node node = ((Node)path.getLastPathComponent());
191             nodeSelectionListener.nodeSelected(node);
192         }
193 	}
194 }