View Javadoc

1   /*
2    * $Id: OverviewPanel.java,v 1.9 2006/09/06 17:48:19 edankert Exp $
3    *
4    * The contents of this file are subject to the Mozilla Public License 
5    * Version 1.1 (the "License"); you may not use this file except in 
6    * compliance with the License. You may obtain a copy of the License at 
7    * http://www.mozilla.org/MPL/ 
8    *
9    * Software distributed under the License is distributed on an "AS IS" basis, 
10   * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License 
11   * for the specific language governing rights and limitations under the License.
12   *
13   * The Original Code is XML Hammer code. (org.xmlhammer.*)
14   *
15   * The Initial Developer of the Original Code is Edwin Dankert. Portions created 
16   * by the Initial Developer are Copyright (C) 2005 - 2006 the Initial Developer. 
17   * All Rights Reserved.
18   *
19   * Contributor(s): Edwin Dankert <edankert@gmail.com>
20   */
21  
22  package org.xmlhammer.gui.overview;
23  
24  import java.awt.BorderLayout;
25  import java.awt.Dimension;
26  import java.util.ArrayList;
27  
28  import javax.swing.JPanel;
29  import javax.swing.JScrollPane;
30  import javax.swing.JTree;
31  import javax.swing.border.EmptyBorder;
32  import javax.swing.event.TreeExpansionEvent;
33  import javax.swing.event.TreeSelectionEvent;
34  import javax.swing.event.TreeSelectionListener;
35  import javax.swing.event.TreeWillExpandListener;
36  import javax.swing.plaf.TreeUI;
37  import javax.swing.plaf.basic.BasicTreeUI;
38  import javax.swing.tree.ExpandVetoException;
39  import javax.swing.tree.TreePath;
40  
41  import org.xmlhammer.gui.Page;
42  import org.xmlhammer.gui.ProjectView;
43  
44  /***s
45   * The UI representation of a Project.
46   * 
47   * @version $Revision: 1.9 $, $Date: 2006/09/06 17:48:19 $
48   * @author Edwin Dankert <edankert@gmail.com>
49   */
50  public class OverviewPanel extends JPanel implements TreeSelectionListener {
51  	private static final long serialVersionUID = 4121138017445755189L;
52  	
53  	private ProjectView view = null;
54  	private JTree tree = null;
55  	private OverviewTreeModel model = null;
56  	
57  
58  	/***
59  	 * @param view the project view.
60  	 */
61  	public OverviewPanel( ProjectView view) {
62  		super( new BorderLayout());
63  		
64  		this.view = view;
65  		
66  		model = new OverviewTreeModel();
67  
68  		tree = new JTree( model);
69  		tree.setBorder( new EmptyBorder( 3, 3, 2, 2));
70  		tree.setRootVisible( false);
71  		tree.setShowsRootHandles( false);
72          tree.setCellRenderer( new OverviewNodeRenderer());
73          tree.setFont( tree.getFont().deriveFont( (float)11));
74          tree.addTreeSelectionListener( this);
75          tree.addTreeWillExpandListener(new TreeWillExpandListener() {
76              public void treeWillExpand(TreeExpansionEvent event) {
77                  // can always expand.
78              }
79  
80              public void treeWillCollapse(TreeExpansionEvent event) throws ExpandVetoException {
81                  throw new ExpandVetoException(event); // can never collapse
82              }
83          });
84          
85          TreeUI ui = tree.getUI();
86          
87          if (ui instanceof BasicTreeUI) {
88              ((BasicTreeUI)ui).setExpandedIcon(null);
89              ((BasicTreeUI)ui).setCollapsedIcon(null);
90              ((BasicTreeUI)ui).setLeftChildIndent(7);
91              ((BasicTreeUI)ui).setRightChildIndent(7);
92          }
93  		
94          JScrollPane scrollPane = new JScrollPane(	tree,
95  										JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
96  										JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
97  //
98  //		/***
99  //		 * Work around to make sure the scroll pane shows the vertical 
100 //		 * scrollbar for the first time when resized to a size small enough.
101 //		 * JDK 1.3.0-C 
102 //		 *
103 //		 * Got work around from Bug ID: 4243631 (It should be fixed...)
104 //		 *
105 //		 * ED: Check with JDK1.4
106 //		 */
107 //		ComponentAdapter resizeListener = new ComponentAdapter() {
108 //			public void componentResized( ComponentEvent e) {
109 //				scrollPane.doLayout();
110 //			}
111 //		};
112 //
113 //		scrollPane.getViewport().addComponentListener( resizeListener);
114 		scrollPane.setPreferredSize( new Dimension( 160, 100));
115 
116 		this.add( scrollPane, BorderLayout.CENTER);
117 	}
118 		
119 	public void setChildren( ArrayList<OverviewNode> children) {		
120 		model.setChildren( children);
121 	}
122 	
123 	public void valueChanged( TreeSelectionEvent event) {
124         view.show( (OverviewNode)tree.getLastSelectedPathComponent());
125 	}
126 
127     public OverviewTreeModel getModel() {
128         return model;
129     }
130     
131     public void addPage( Page page) {
132         model.addChild( page);
133     }
134     
135     public void expandAll() {
136     	expandNode( model.getRoot());
137     }
138 
139     public void selectPreviousNode( OverviewNode node) {
140         OverviewNode parent = node.getParentNode();
141         int index = parent.getChildNodes().indexOf( node);
142 
143         OverviewNode next = parent.getChildNodes().get( index-1);
144         tree.setSelectionPath( new TreePath( model.getPathToRoot( next, 0)));
145     }
146 
147     public void selectNextNode( OverviewNode node) {
148         OverviewNode parent = node.getParentNode();
149         int index = parent.getChildNodes().indexOf( node);
150 
151         OverviewNode next = parent.getChildNodes().get( index+1);
152         tree.setSelectionPath( new TreePath( model.getPathToRoot( next, 0)));
153     }
154 
155     public void selectNode( OverviewNode node) {
156         tree.setSelectionPath( new TreePath( model.getPathToRoot( node, 0)));
157 	}
158 
159 	public void expandNode( OverviewNode node) {
160         tree.expandPath( new TreePath( model.getPathToRoot( node, 0)));
161         
162         for( OverviewNode n:  node.getChildNodes()) {
163             expandNode( n);
164         }
165     }
166 }