KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > subversion > ui > history > DiffTreeTable


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19 package org.netbeans.modules.subversion.ui.history;
20
21 import org.openide.explorer.view.TreeTableView;
22 import org.openide.explorer.ExplorerManager;
23 import org.openide.nodes.Node;
24 import org.openide.nodes.PropertySupport;
25 import org.openide.nodes.AbstractNode;
26 import org.openide.nodes.Children;
27 import org.openide.util.NbBundle;
28 import org.openide.util.lookup.Lookups;
29 import org.openide.ErrorManager;
30
31 import javax.swing.*;
32 import javax.swing.tree.DefaultTreeCellRenderer JavaDoc;
33 import java.util.*;
34 import java.beans.PropertyVetoException JavaDoc;
35 import java.lang.reflect.InvocationTargetException JavaDoc;
36
37 /**
38  * Treetable to show results of Search History action.
39  *
40  * @author Maros Sandor
41  */

42 class DiffTreeTable extends TreeTableView {
43     
44     private RevisionsRootNode rootNode;
45     private List results;
46     private final SearchHistoryPanel master;
47
48     public DiffTreeTable(SearchHistoryPanel master) {
49         this.master = master;
50         treeTable.setShowHorizontalLines(true);
51         treeTable.setShowVerticalLines(false);
52         setRootVisible(false);
53         setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
54         setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
55         setupColumns();
56
57         DefaultTreeCellRenderer JavaDoc renderer = new DefaultTreeCellRenderer JavaDoc();
58         renderer.setOpenIcon(null);
59         renderer.setClosedIcon(null);
60         renderer.setLeafIcon(null);
61         tree.setCellRenderer(renderer);
62     }
63     
64     private void setupColumns() {
65         Node.Property [] columns = new Node.Property[4];
66         ResourceBundle loc = NbBundle.getBundle(DiffTreeTable.class);
67         columns[0] = new ColumnDescriptor(RevisionNode.COLUMN_NAME_NAME, String JavaDoc.class, "", ""); // NOI18N
68
columns[0].setValue("TreeColumnTTV", Boolean.TRUE); // NOI18N
69
columns[1] = new ColumnDescriptor(RevisionNode.COLUMN_NAME_DATE, String JavaDoc.class, loc.getString("LBL_DiffTree_Column_Time"), loc.getString("LBL_DiffTree_Column_Time_Desc"));
70         columns[2] = new ColumnDescriptor(RevisionNode.COLUMN_NAME_USERNAME, String JavaDoc.class, loc.getString("LBL_DiffTree_Column_Username"), loc.getString("LBL_DiffTree_Column_Username_Desc"));
71         columns[3] = new ColumnDescriptor(RevisionNode.COLUMN_NAME_MESSAGE, String JavaDoc.class, loc.getString("LBL_DiffTree_Column_Message"), loc.getString("LBL_DiffTree_Column_Message_Desc"));
72         setProperties(columns);
73     }
74     
75     private void setDefaultColumnSizes() {
76         SwingUtilities.invokeLater(new Runnable JavaDoc() {
77             public void run() {
78                 int width = getWidth();
79                 treeTable.getColumnModel().getColumn(0).setPreferredWidth(width * 25 / 100);
80                 treeTable.getColumnModel().getColumn(1).setPreferredWidth(width * 15 / 100);
81                 treeTable.getColumnModel().getColumn(2).setPreferredWidth(width * 10 / 100);
82                 treeTable.getColumnModel().getColumn(3).setPreferredWidth(width * 50 / 100);
83             }
84         });
85     }
86
87     void setSelection(int idx) {
88         treeTable.getSelectionModel().setValueIsAdjusting(false);
89         treeTable.scrollRectToVisible(treeTable.getCellRect(idx, 1, true));
90         treeTable.getSelectionModel().setSelectionInterval(idx, idx);
91     }
92
93     void setSelection(RepositoryRevision container) {
94         RevisionNode node = (RevisionNode) getNode(rootNode, container);
95         if (node == null) return;
96         ExplorerManager em = ExplorerManager.find(this);
97         try {
98             em.setSelectedNodes(new Node [] { node });
99         } catch (PropertyVetoException JavaDoc e) {
100             ErrorManager.getDefault().notify(e);
101         }
102     }
103
104     void setSelection(RepositoryRevision.Event revision) {
105         RevisionNode node = (RevisionNode) getNode(rootNode, revision);
106         if (node == null) return;
107         ExplorerManager em = ExplorerManager.find(this);
108         try {
109             em.setSelectedNodes(new Node [] { node });
110         } catch (PropertyVetoException JavaDoc e) {
111             ErrorManager.getDefault().notify(e);
112         }
113     }
114
115     private Node getNode(Node node, Object JavaDoc obj) {
116         Object JavaDoc object = node.getLookup().lookup(obj.getClass());
117         if (obj.equals(object)) return node;
118         Enumeration children = node.getChildren().nodes();
119         while (children.hasMoreElements()) {
120             Node child = (Node) children.nextElement();
121             Node result = getNode(child, obj);
122             if (result != null) return result;
123         }
124         return null;
125     }
126
127     public int [] getSelection() {
128         return treeTable.getSelectedRows();
129     }
130
131     public int getRowCount() {
132         return treeTable.getRowCount();
133     }
134
135     private static class ColumnDescriptor<T> extends PropertySupport.ReadOnly<T> {
136         
137         public ColumnDescriptor(String JavaDoc name, Class JavaDoc<T> type, String JavaDoc displayName, String JavaDoc shortDescription) {
138             super(name, type, displayName, shortDescription);
139         }
140
141         public T getValue() throws IllegalAccessException JavaDoc, InvocationTargetException JavaDoc {
142             return null;
143         }
144     }
145
146     public void addNotify() {
147         super.addNotify();
148         ExplorerManager em = ExplorerManager.find(this);
149         em.setRootContext(rootNode);
150         setDefaultColumnSizes();
151     }
152
153     public void setResults(List results) {
154         this.results = results;
155         rootNode = new RevisionsRootNode();
156         ExplorerManager em = ExplorerManager.find(this);
157         if (em != null) {
158             em.setRootContext(rootNode);
159         }
160     }
161     
162     private class RevisionsRootNode extends AbstractNode {
163     
164         public RevisionsRootNode() {
165             super(new RevisionsRootNodeChildren(), Lookups.singleton(results));
166         }
167
168         public String JavaDoc getName() {
169             return "revision"; // NOI18N
170
}
171
172         public String JavaDoc getDisplayName() {
173             return NbBundle.getMessage(DiffTreeTable.class, "LBL_DiffTree_Column_Name"); // NOI18N
174
}
175
176         public String JavaDoc getShortDescription() {
177             return NbBundle.getMessage(DiffTreeTable.class, "LBL_DiffTree_Column_Name_Desc"); // NOI18N
178
}
179     }
180
181     private class RevisionsRootNodeChildren extends Children.Keys {
182     
183         public RevisionsRootNodeChildren() {
184         }
185
186         protected void addNotify() {
187             refreshKeys();
188         }
189
190         protected void removeNotify() {
191             setKeys(Collections.EMPTY_SET);
192         }
193     
194         private void refreshKeys() {
195             setKeys(results);
196         }
197     
198         protected Node[] createNodes(Object JavaDoc key) {
199             RevisionNode node;
200             if (key instanceof RepositoryRevision) {
201                 node = new RevisionNode((RepositoryRevision) key, master);
202             } else { // key instanceof RepositoryRevision.Event
203
node = new RevisionNode(((RepositoryRevision.Event) key), master);
204             }
205             return new Node[] { node };
206         }
207     }
208 }
209
Popular Tags