KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > versioning > system > cvss > 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
20 package org.netbeans.modules.versioning.system.cvss.ui.history;
21
22 import org.openide.explorer.view.TreeTableView;
23 import org.openide.explorer.ExplorerManager;
24 import org.openide.nodes.Node;
25 import org.openide.nodes.PropertySupport;
26 import org.openide.nodes.AbstractNode;
27 import org.openide.nodes.Children;
28 import org.openide.util.lookup.Lookups;
29 import org.openide.util.NbBundle;
30 import org.openide.ErrorManager;
31 import org.netbeans.modules.versioning.system.cvss.CvsModuleConfig;
32
33 import javax.swing.*;
34 import javax.swing.tree.DefaultTreeCellRenderer JavaDoc;
35 import java.lang.reflect.InvocationTargetException JavaDoc;
36 import java.util.*;
37 import java.beans.PropertyVetoException JavaDoc;
38 import java.awt.Window JavaDoc;
39 import java.awt.Point JavaDoc;
40 import java.awt.Cursor JavaDoc;
41 import java.awt.event.MouseListener JavaDoc;
42 import java.awt.event.MouseEvent JavaDoc;
43 import java.awt.event.MouseMotionListener JavaDoc;
44
45 /**
46  * Treetable to show results of Search History action.
47  *
48  * @author Maros Sandor
49  */

50 class DiffTreeTable extends TreeTableView implements MouseListener JavaDoc, MouseMotionListener JavaDoc {
51     
52     private RevisionsRootNode rootNode;
53     private List results;
54
55     public DiffTreeTable() {
56         treeTable.setShowHorizontalLines(true);
57         treeTable.setShowVerticalLines(false);
58         setRootVisible(false);
59         setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
60         setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
61         setupColumns();
62
63         DefaultTreeCellRenderer JavaDoc renderer = new DefaultTreeCellRenderer JavaDoc();
64         renderer.setOpenIcon(null);
65         renderer.setClosedIcon(null);
66         renderer.setLeafIcon(null);
67         tree.setCellRenderer(renderer);
68         
69         treeTable.addMouseListener(this);
70         treeTable.addMouseMotionListener(this);
71     }
72
73     private SearchHistoryPanel.DispRevision getRevisionWithTagsAt(Point JavaDoc p) {
74         int row = treeTable.rowAtPoint(p);
75         int column = treeTable.columnAtPoint(p);
76         if (row == -1 || column == -1) return null;
77         Object JavaDoc o = treeTable.getValueAt(row, column);
78         if (o instanceof Node.Property) {
79             Node.Property tags = (Node.Property) o;
80             SearchHistoryPanel.DispRevision drev = (SearchHistoryPanel.DispRevision) tags.getValue("dispRevision"); // NOI18N
81
if (drev != null && drev.getBranches() != null && drev.getBranches().size() + drev.getTags().size() > 1) {
82                 return drev;
83             }
84         }
85         return null;
86     }
87     
88     public void mouseClicked(MouseEvent JavaDoc e) {
89         Point JavaDoc p = e.getPoint();
90         SearchHistoryPanel.DispRevision drev = getRevisionWithTagsAt(p);
91         if (drev != null) {
92             Window JavaDoc w = SwingUtilities.windowForComponent(treeTable);
93             SwingUtilities.convertPointToScreen(p, treeTable);
94             p.x += 10;
95             p.y += 10;
96             SummaryView.showAllTags(w, p, drev);
97         }
98     }
99
100     public void mouseDragged(MouseEvent JavaDoc e) {
101     }
102
103     public void mouseMoved(MouseEvent JavaDoc e) {
104         if (getRevisionWithTagsAt(e.getPoint()) != null) {
105             treeTable.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
106         } else {
107             treeTable.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
108         }
109     }
110
111     public void mousePressed(MouseEvent JavaDoc e) {
112     }
113
114     public void mouseReleased(MouseEvent JavaDoc e) {
115     }
116
117     public void mouseEntered(MouseEvent JavaDoc e) {
118     }
119
120     public void mouseExited(MouseEvent JavaDoc e) {
121     }
122
123     private void setupColumns() {
124         ResourceBundle loc = NbBundle.getBundle(DiffTreeTable.class);
125         Node.Property [] columns;
126         if (CvsModuleConfig.getDefault().getPreferences().getBoolean(CvsModuleConfig.PROP_SEARCHHISTORY_FETCHTAGS, true)) {
127             columns = new Node.Property[6];
128             columns[4] = new ColumnDescriptor(RevisionNode.COLUMN_NAME_TAGS, List.class, loc.getString("LBL_DiffTree_Column_Tags"), loc.getString("LBL_DiffTree_Column_Tags_Desc"));
129             columns[5] = new ColumnDescriptor(RevisionNode.COLUMN_NAME_MESSAGE, String JavaDoc.class, loc.getString("LBL_DiffTree_Column_Message"), loc.getString("LBL_DiffTree_Column_Message_Desc"));
130         } else {
131             columns = new Node.Property[5];
132             columns[4] = new ColumnDescriptor(RevisionNode.COLUMN_NAME_MESSAGE, String JavaDoc.class, loc.getString("LBL_DiffTree_Column_Message"), loc.getString("LBL_DiffTree_Column_Message_Desc"));
133         }
134         columns[0] = new ColumnDescriptor(RevisionNode.COLUMN_NAME_NAME, String JavaDoc.class, "", ""); // NOI18N
135
columns[0].setValue("TreeColumnTTV", Boolean.TRUE); // NOI18N
136
columns[1] = new ColumnDescriptor(RevisionNode.COLUMN_NAME_LOCATION, String JavaDoc.class, loc.getString("LBL_DiffTree_Column_Location"), loc.getString("LBL_DiffTree_Column_Location_Desc"));
137         columns[2] = new ColumnDescriptor(RevisionNode.COLUMN_NAME_DATE, String JavaDoc.class, loc.getString("LBL_DiffTree_Column_Time"), loc.getString("LBL_DiffTree_Column_Time_Desc"));
138         columns[3] = new ColumnDescriptor(RevisionNode.COLUMN_NAME_USERNAME, String JavaDoc.class, loc.getString("LBL_DiffTree_Column_Username"), loc.getString("LBL_DiffTree_Column_Username_Desc"));
139         setProperties(columns);
140     }
141     
142     private void setDefaultColumnSizes() {
143         SwingUtilities.invokeLater(new Runnable JavaDoc() {
144             public void run() {
145                 int width = getWidth();
146                 if (CvsModuleConfig.getDefault().getPreferences().getBoolean(CvsModuleConfig.PROP_SEARCHHISTORY_FETCHTAGS, true)) {
147                     if (treeTable.getColumnModel().getColumnCount() != 6) return;
148                     treeTable.getColumnModel().getColumn(0).setPreferredWidth(width * 15 / 100);
149                     treeTable.getColumnModel().getColumn(1).setPreferredWidth(width * 15 / 100);
150                     treeTable.getColumnModel().getColumn(2).setPreferredWidth(width * 10 / 100);
151                     treeTable.getColumnModel().getColumn(3).setPreferredWidth(width * 10 / 100);
152                     treeTable.getColumnModel().getColumn(4).setPreferredWidth(width * 10 / 100);
153                     treeTable.getColumnModel().getColumn(5).setPreferredWidth(width * 40 / 100);
154                 } else {
155                     if (treeTable.getColumnModel().getColumnCount() != 5) return;
156                     treeTable.getColumnModel().getColumn(0).setPreferredWidth(width * 20 / 100);
157                     treeTable.getColumnModel().getColumn(1).setPreferredWidth(width * 20 / 100);
158                     treeTable.getColumnModel().getColumn(2).setPreferredWidth(width * 10 / 100);
159                     treeTable.getColumnModel().getColumn(3).setPreferredWidth(width * 10 / 100);
160                     treeTable.getColumnModel().getColumn(4).setPreferredWidth(width * 40 / 100);
161                 }
162             }
163         });
164     }
165
166     void setSelection(int idx) {
167         treeTable.getSelectionModel().setValueIsAdjusting(false);
168         treeTable.scrollRectToVisible(treeTable.getCellRect(idx, 1, true));
169         treeTable.getSelectionModel().setSelectionInterval(idx, idx);
170     }
171
172     void setSelection(SearchHistoryPanel.ResultsContainer container) {
173         RevisionNode node = (RevisionNode) getNode(rootNode, container);
174         if (node == null) return;
175         ExplorerManager em = ExplorerManager.find(this);
176         try {
177             em.setSelectedNodes(new Node [] { node });
178         } catch (PropertyVetoException JavaDoc e) {
179             ErrorManager.getDefault().notify(e);
180         }
181     }
182
183     void setSelection(SearchHistoryPanel.DispRevision revision) {
184         RevisionNode node = (RevisionNode) getNode(rootNode, revision);
185         if (node == null) return;
186         ExplorerManager em = ExplorerManager.find(this);
187         try {
188             em.setSelectedNodes(new Node [] { node });
189         } catch (PropertyVetoException JavaDoc e) {
190             ErrorManager.getDefault().notify(e);
191         }
192     }
193
194     private Node getNode(Node node, Object JavaDoc obj) {
195         Object JavaDoc object = node.getLookup().lookup(obj.getClass());
196         if (obj.equals(object)) return node;
197         Enumeration children = node.getChildren().nodes();
198         while (children.hasMoreElements()) {
199             Node child = (Node) children.nextElement();
200             Node result = getNode(child, obj);
201             if (result != null) return result;
202         }
203         return null;
204     }
205
206     public int [] getSelection() {
207         return treeTable.getSelectedRows();
208     }
209
210     public int getRowCount() {
211         return treeTable.getRowCount();
212     }
213
214     private static class ColumnDescriptor extends PropertySupport.ReadOnly {
215         
216         public ColumnDescriptor(String JavaDoc name, Class JavaDoc type, String JavaDoc displayName, String JavaDoc shortDescription) {
217             super(name, type, displayName, shortDescription);
218         }
219
220         public Object JavaDoc getValue() throws IllegalAccessException JavaDoc, InvocationTargetException JavaDoc {
221             return null;
222         }
223     }
224
225     public void addNotify() {
226         super.addNotify();
227         ExplorerManager em = ExplorerManager.find(this);
228         em.setRootContext(rootNode);
229         setDefaultColumnSizes();
230     }
231
232     public void setResults(List results) {
233         this.results = results;
234         rootNode = new RevisionsRootNode();
235         ExplorerManager em = ExplorerManager.find(this);
236         if (em != null) {
237             em.setRootContext(rootNode);
238         }
239     }
240     
241     private class RevisionsRootNode extends AbstractNode {
242     
243         public RevisionsRootNode() {
244             super(new RevisionsRootNodeChildren(), Lookups.singleton(results));
245         }
246
247         public String JavaDoc getName() {
248             return "revision"; // NOI18N
249
}
250
251         public String JavaDoc getDisplayName() {
252             return NbBundle.getMessage(DiffTreeTable.class, "LBL_DiffTree_Column_Name"); // NOI18N
253
}
254
255         public String JavaDoc getShortDescription() {
256             return NbBundle.getMessage(DiffTreeTable.class, "LBL_DiffTree_Column_Name_Desc"); // NOI18N
257
}
258     }
259
260     private class RevisionsRootNodeChildren extends Children.Keys {
261     
262         public RevisionsRootNodeChildren() {
263         }
264
265         protected void addNotify() {
266             refreshKeys();
267         }
268
269         protected void removeNotify() {
270             setKeys(Collections.EMPTY_SET);
271         }
272     
273         private void refreshKeys() {
274             setKeys(results);
275         }
276     
277         protected Node[] createNodes(Object JavaDoc key) {
278             RevisionNode node;
279             if (key instanceof SearchHistoryPanel.ResultsContainer) {
280                 node = new RevisionNode((SearchHistoryPanel.ResultsContainer) key);
281             } else { // key instanceof SearchHistoryPanel.DispRevision
282
node = new RevisionNode(((SearchHistoryPanel.DispRevision) key));
283             }
284             return new Node[] { node };
285         }
286     }
287 }
288
Popular Tags