Cleanup code whitespace
[nbgit.git] / src / org / nbgit / ui / log / DiffTreeTable.java
blob664cca58f84bc90d23d851dea63fdfa5307feb08
1 /*
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
4 * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
6 * The contents of this file are subject to the terms of either the GNU
7 * General Public License Version 2 only ("GPL") or the Common
8 * Development and Distribution License("CDDL") (collectively, the
9 * "License"). You may not use this file except in compliance with the
10 * License. You can obtain a copy of the License at
11 * http://www.netbeans.org/cddl-gplv2.html
12 * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
13 * specific language governing permissions and limitations under the
14 * License. When distributing the software, include this License Header
15 * Notice in each file and include the License file at
16 * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
17 * particular file as subject to the "Classpath" exception as provided
18 * by Sun in the GPL Version 2 section of the License file that
19 * accompanied this code. If applicable, add the following below the
20 * License Header, with the fields enclosed by brackets [] replaced by
21 * your own identifying information:
22 * "Portions Copyrighted [year] [name of copyright owner]"
24 * Contributor(s):
26 * The Original Software is NetBeans. The Initial Developer of the Original
27 * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
28 * Microsystems, Inc. All Rights Reserved.
29 * Portions Copyright 2008 Alexander Coles (Ikonoklastik Productions).
31 * If you wish your version of this file to be governed by only the CDDL
32 * or only the GPL Version 2, indicate your decision by adding
33 * "[Contributor] elects to include this software in this distribution
34 * under the [CDDL or GPL Version 2] license." If you do not indicate a
35 * single choice of license, a recipient has the option to distribute
36 * your version of this file under either the CDDL, the GPL Version 2 or
37 * to extend the choice of license to its licensees as provided above.
38 * However, if you add GPL Version 2 code and therefore, elected the GPL
39 * Version 2 license, then the option applies only if the new code is
40 * made subject to such option by the copyright holder.
42 package org.nbgit.ui.log;
44 import java.beans.PropertyVetoException;
45 import java.lang.reflect.InvocationTargetException;
46 import java.util.Collections;
47 import java.util.Enumeration;
48 import java.util.List;
49 import java.util.ResourceBundle;
50 import javax.swing.JScrollPane;
51 import javax.swing.SwingUtilities;
52 import javax.swing.tree.DefaultTreeCellRenderer;
53 import org.openide.ErrorManager;
54 import org.openide.explorer.ExplorerManager;
55 import org.openide.explorer.view.TreeTableView;
56 import org.openide.nodes.AbstractNode;
57 import org.openide.nodes.Children;
58 import org.openide.nodes.Node;
59 import org.openide.nodes.PropertySupport;
60 import org.openide.util.NbBundle;
61 import org.openide.util.lookup.Lookups;
63 /**
64 * Treetable to show results of Search History action.
66 * @author Maros Sandor
68 class DiffTreeTable extends TreeTableView {
70 private RevisionsRootNode rootNode;
71 private List results;
72 private final SearchHistoryPanel master;
74 public DiffTreeTable(SearchHistoryPanel master)
76 this.master = master;
77 treeTable.setShowHorizontalLines(true);
78 treeTable.setShowVerticalLines(false);
79 setRootVisible(false);
80 setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
81 setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
82 setupColumns();
84 DefaultTreeCellRenderer renderer = new DefaultTreeCellRenderer();
85 renderer.setOpenIcon(null);
86 renderer.setClosedIcon(null);
87 renderer.setLeafIcon(null);
88 tree.setCellRenderer(renderer);
91 @SuppressWarnings("unchecked")
92 private void setupColumns()
94 Node.Property[] columns = new Node.Property[4];
95 ResourceBundle loc = NbBundle.getBundle(DiffTreeTable.class);
96 columns[0] = new ColumnDescriptor(RevisionNode.COLUMN_NAME_NAME, String.class, "", ""); // NOI18N
97 columns[0].setValue("TreeColumnTTV", Boolean.TRUE); // NOI18N
98 columns[1] = new ColumnDescriptor(RevisionNode.COLUMN_NAME_DATE, String.class, loc.getString("LBL_DiffTree_Column_Time"), loc.getString("LBL_DiffTree_Column_Time_Desc"));
99 columns[2] = new ColumnDescriptor(RevisionNode.COLUMN_NAME_USERNAME, String.class, loc.getString("LBL_DiffTree_Column_Username"), loc.getString("LBL_DiffTree_Column_Username_Desc"));
100 columns[3] = new ColumnDescriptor(RevisionNode.COLUMN_NAME_MESSAGE, String.class, loc.getString("LBL_DiffTree_Column_Message"), loc.getString("LBL_DiffTree_Column_Message_Desc"));
101 setProperties(columns);
104 private void setDefaultColumnSizes()
106 SwingUtilities.invokeLater(new Runnable() {
108 public void run()
110 int width = getWidth();
111 treeTable.getColumnModel().getColumn(0).setPreferredWidth(width * 25 / 100);
112 treeTable.getColumnModel().getColumn(1).setPreferredWidth(width * 15 / 100);
113 treeTable.getColumnModel().getColumn(2).setPreferredWidth(width * 10 / 100);
114 treeTable.getColumnModel().getColumn(3).setPreferredWidth(width * 50 / 100);
120 void setSelection(int idx)
122 treeTable.getSelectionModel().setValueIsAdjusting(false);
123 treeTable.scrollRectToVisible(treeTable.getCellRect(idx, 1, true));
124 treeTable.getSelectionModel().setSelectionInterval(idx, idx);
127 void setSelection(RepositoryRevision container)
129 RevisionNode node = (RevisionNode) getNode(rootNode, container);
130 if (node == null)
131 return;
132 ExplorerManager em = ExplorerManager.find(this);
133 try {
134 em.setSelectedNodes(new Node[]{node});
135 } catch (PropertyVetoException e) {
136 ErrorManager.getDefault().notify(e);
140 void setSelection(RepositoryRevision.Event revision)
142 RevisionNode node = (RevisionNode) getNode(rootNode, revision);
143 if (node == null)
144 return;
145 ExplorerManager em = ExplorerManager.find(this);
146 try {
147 em.setSelectedNodes(new Node[]{node});
148 } catch (PropertyVetoException e) {
149 ErrorManager.getDefault().notify(e);
153 private Node getNode(Node node, Object obj)
155 Object object = node.getLookup().lookup(obj.getClass());
156 if (obj.equals(object))
157 return node;
158 Enumeration children = node.getChildren().nodes();
159 while (children.hasMoreElements()) {
160 Node child = (Node) children.nextElement();
161 Node result = getNode(child, obj);
162 if (result != null)
163 return result;
165 return null;
168 public int[] getSelection()
170 return treeTable.getSelectedRows();
173 public int getRowCount()
175 return treeTable.getRowCount();
178 private static class ColumnDescriptor<T> extends PropertySupport.ReadOnly<T> {
180 public ColumnDescriptor(String name, Class<T> type, String displayName, String shortDescription)
182 super(name, type, displayName, shortDescription);
185 public T getValue() throws IllegalAccessException, InvocationTargetException
187 return null;
192 public void addNotify()
194 super.addNotify();
195 ExplorerManager em = ExplorerManager.find(this);
196 em.setRootContext(rootNode);
197 setDefaultColumnSizes();
200 public void setResults(List results)
202 this.results = results;
203 rootNode = new RevisionsRootNode();
204 ExplorerManager em = ExplorerManager.find(this);
205 if (em != null)
206 em.setRootContext(rootNode);
209 private class RevisionsRootNode extends AbstractNode {
211 public RevisionsRootNode()
213 super(new RevisionsRootNodeChildren(), Lookups.singleton(results));
216 public String getName()
218 return "revision"; // NOI18N
221 public String getDisplayName()
223 return NbBundle.getMessage(DiffTreeTable.class, "LBL_DiffTree_Column_Name"); // NOI18N
226 public String getShortDescription()
228 return NbBundle.getMessage(DiffTreeTable.class, "LBL_DiffTree_Column_Name_Desc"); // NOI18N
233 private class RevisionsRootNodeChildren extends Children.Keys {
235 public RevisionsRootNodeChildren()
239 protected void addNotify()
241 refreshKeys();
244 @SuppressWarnings("unchecked")
245 @Override
246 protected void removeNotify()
248 setKeys(Collections.EMPTY_SET);
251 @SuppressWarnings("unchecked")
252 private void refreshKeys()
254 setKeys(results);
257 protected Node[] createNodes(Object key)
259 RevisionNode node;
260 if (key instanceof RepositoryRevision)
261 node = new RevisionNode((RepositoryRevision) key, master);
262 else // key instanceof RepositoryRevision.Event
263 node = new RevisionNode(((RepositoryRevision.Event) key), master);
264 return new Node[]{node};