Adding Git source, NetBeans project files, GPL v2 LICENSE, ant build file.
[nbgit.git] / src / org / netbeans / modules / git / ui / log / DiffTreeTable.java
blobf4edace274b10997aa0a53baf98c34998b68fa58
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.netbeans.modules.git.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;
64 /**
65 * Treetable to show results of Search History action.
67 * @author Maros Sandor
69 class DiffTreeTable extends TreeTableView {
71 private RevisionsRootNode rootNode;
72 private List results;
73 private final SearchHistoryPanel master;
75 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() {
93 Node.Property [] columns = new Node.Property[4];
94 ResourceBundle loc = NbBundle.getBundle(DiffTreeTable.class);
95 columns[0] = new ColumnDescriptor(RevisionNode.COLUMN_NAME_NAME, String.class, "", ""); // NOI18N
96 columns[0].setValue("TreeColumnTTV", Boolean.TRUE); // NOI18N
97 columns[1] = new ColumnDescriptor(RevisionNode.COLUMN_NAME_DATE, String.class, loc.getString("LBL_DiffTree_Column_Time"), loc.getString("LBL_DiffTree_Column_Time_Desc"));
98 columns[2] = new ColumnDescriptor(RevisionNode.COLUMN_NAME_USERNAME, String.class, loc.getString("LBL_DiffTree_Column_Username"), loc.getString("LBL_DiffTree_Column_Username_Desc"));
99 columns[3] = new ColumnDescriptor(RevisionNode.COLUMN_NAME_MESSAGE, String.class, loc.getString("LBL_DiffTree_Column_Message"), loc.getString("LBL_DiffTree_Column_Message_Desc"));
100 setProperties(columns);
103 private void setDefaultColumnSizes() {
104 SwingUtilities.invokeLater(new Runnable() {
105 public void run() {
106 int width = getWidth();
107 treeTable.getColumnModel().getColumn(0).setPreferredWidth(width * 25 / 100);
108 treeTable.getColumnModel().getColumn(1).setPreferredWidth(width * 15 / 100);
109 treeTable.getColumnModel().getColumn(2).setPreferredWidth(width * 10 / 100);
110 treeTable.getColumnModel().getColumn(3).setPreferredWidth(width * 50 / 100);
115 void setSelection(int idx) {
116 treeTable.getSelectionModel().setValueIsAdjusting(false);
117 treeTable.scrollRectToVisible(treeTable.getCellRect(idx, 1, true));
118 treeTable.getSelectionModel().setSelectionInterval(idx, idx);
121 void setSelection(RepositoryRevision container) {
122 RevisionNode node = (RevisionNode) getNode(rootNode, container);
123 if (node == null) return;
124 ExplorerManager em = ExplorerManager.find(this);
125 try {
126 em.setSelectedNodes(new Node [] { node });
127 } catch (PropertyVetoException e) {
128 ErrorManager.getDefault().notify(e);
132 void setSelection(RepositoryRevision.Event revision) {
133 RevisionNode node = (RevisionNode) getNode(rootNode, revision);
134 if (node == null) return;
135 ExplorerManager em = ExplorerManager.find(this);
136 try {
137 em.setSelectedNodes(new Node [] { node });
138 } catch (PropertyVetoException e) {
139 ErrorManager.getDefault().notify(e);
143 private Node getNode(Node node, Object obj) {
144 Object object = node.getLookup().lookup(obj.getClass());
145 if (obj.equals(object)) return node;
146 Enumeration children = node.getChildren().nodes();
147 while (children.hasMoreElements()) {
148 Node child = (Node) children.nextElement();
149 Node result = getNode(child, obj);
150 if (result != null) return result;
152 return null;
155 public int [] getSelection() {
156 return treeTable.getSelectedRows();
159 public int getRowCount() {
160 return treeTable.getRowCount();
163 private static class ColumnDescriptor<T> extends PropertySupport.ReadOnly<T> {
165 public ColumnDescriptor(String name, Class<T> type, String displayName, String shortDescription) {
166 super(name, type, displayName, shortDescription);
169 public T getValue() throws IllegalAccessException, InvocationTargetException {
170 return null;
174 public void addNotify() {
175 super.addNotify();
176 ExplorerManager em = ExplorerManager.find(this);
177 em.setRootContext(rootNode);
178 setDefaultColumnSizes();
181 public void setResults(List results) {
182 this.results = results;
183 rootNode = new RevisionsRootNode();
184 ExplorerManager em = ExplorerManager.find(this);
185 if (em != null) {
186 em.setRootContext(rootNode);
190 private class RevisionsRootNode extends AbstractNode {
192 public RevisionsRootNode() {
193 super(new RevisionsRootNodeChildren(), Lookups.singleton(results));
196 public String getName() {
197 return "revision"; // NOI18N
200 public String getDisplayName() {
201 return NbBundle.getMessage(DiffTreeTable.class, "LBL_DiffTree_Column_Name"); // NOI18N
204 public String getShortDescription() {
205 return NbBundle.getMessage(DiffTreeTable.class, "LBL_DiffTree_Column_Name_Desc"); // NOI18N
209 private class RevisionsRootNodeChildren extends Children.Keys {
211 public RevisionsRootNodeChildren() {
214 protected void addNotify() {
215 refreshKeys();
218 @SuppressWarnings("unchecked")
219 protected void removeNotify() {
220 setKeys(Collections.EMPTY_SET);
223 @SuppressWarnings("unchecked")
224 private void refreshKeys() {
225 setKeys(results);
228 protected Node[] createNodes(Object key) {
229 RevisionNode node;
230 if (key instanceof RepositoryRevision) {
231 node = new RevisionNode((RepositoryRevision) key, master);
232 } else { // key instanceof RepositoryRevision.Event
233 node = new RevisionNode(((RepositoryRevision.Event) key), master);
235 return new Node[] { node };