Reenable the "Diff" link in the log summary view
[nbgit.git] / src / org / nbgit / options / PropertiesTable.java
blobafebc3ee237bd1559618bc1529d7ccf4cf2ea76f
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.options;
44 import java.awt.Component;
45 import java.awt.Dimension;
46 import java.util.Arrays;
47 import javax.swing.JComponent;
48 import javax.swing.JLabel;
49 import javax.swing.JScrollPane;
50 import javax.swing.JTable;
51 import javax.swing.event.AncestorEvent;
52 import javax.swing.event.AncestorListener;
53 import javax.swing.event.TableModelEvent;
54 import javax.swing.event.TableModelListener;
55 import javax.swing.table.DefaultTableCellRenderer;
56 import javax.swing.table.TableColumnModel;
57 import javax.swing.table.TableModel;
58 import org.nbgit.ui.properties.GitPropertiesNode;
59 import org.netbeans.modules.versioning.util.TableSorter;
60 import org.openide.util.NbBundle;
62 /**
64 * @author Peter Pis
66 public class PropertiesTable implements AncestorListener, TableModelListener {
68 static public final String[] PROPERTIES_COLUMNS = new String[]{PropertiesTableModel.COLUMN_NAME_NAME, PropertiesTableModel.COLUMN_NAME_VALUE};
69 private PropertiesTableModel tableModel;
70 private JTable table;
71 private TableSorter sorter;
72 private JComponent component;
73 private String[] columns;
74 private String[] sortByColumns;
76 /** Creates a new instance of PropertiesTable */
77 public PropertiesTable(JLabel label, String[] columns, String[] sortByColumns)
79 init(label, columns, null);
80 this.sortByColumns = sortByColumns;
81 setSortingStatus();
84 public PropertiesTable(JLabel label, String[] columns, TableSorter sorter)
86 init(label, columns, sorter);
89 private void init(JLabel label, String[] columns, TableSorter sorter)
91 tableModel = new PropertiesTableModel(columns);
92 tableModel.addTableModelListener(this);
93 if (sorter == null)
94 sorter = new TableSorter(tableModel);
95 this.sorter = sorter;
96 table = new JTable(this.sorter);
97 table.getTableHeader().setReorderingAllowed(false);
98 table.setDefaultRenderer(String.class, new PropertiesTableCellRenderer());
99 //table.setDefaultEditor(CommitOptions.class, new CommitOptionsCellEditor());
100 table.getTableHeader().setReorderingAllowed(true);
101 this.sorter.setTableHeader(table.getTableHeader());
102 table.setRowHeight(table.getRowHeight());
103 table.addAncestorListener(this);
104 component = new JScrollPane(table, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
105 component.setPreferredSize(new Dimension(340, 150));
106 table.getAccessibleContext().setAccessibleDescription(NbBundle.getMessage(PropertiesTable.class, "ACSD_PropertiesTable")); // NOI18N
107 label.setLabelFor(table);
108 setColumns(columns);
111 public void setColumns(String[] clmns)
113 if (Arrays.equals(columns, clmns))
114 return;
115 columns = clmns;
116 tableModel.setColumns(clmns);
117 setDefaultColumnSize();
120 public JTable getTable()
122 return table;
125 private void setDefaultColumnSize()
127 int width = table.getWidth();
128 TableColumnModel columnModel = table.getColumnModel();
129 if (columns == null || columnModel == null)
130 return;
131 if (columnModel.getColumnCount() != columns.length)
132 return;
133 for (int i = 0; i < columns.length; i++) {
134 String col = columns[i];
135 sorter.setColumnComparator(i, null);
136 if (col.equals(PropertiesTableModel.COLUMN_NAME_NAME))
137 columnModel.getColumn(i).setPreferredWidth(width * 20 / 100);
138 else if (col.equals(PropertiesTableModel.COLUMN_NAME_VALUE))
139 columnModel.getColumn(i).setPreferredWidth(width * 40 / 100);
143 private void setSortingStatus()
145 for (int i = 0; i < sortByColumns.length; i++) {
146 String sortByColumn = sortByColumns[i];
147 for (int j = 0; j < columns.length; j++) {
148 String column = columns[j];
149 if (column.equals(sortByColumn)) {
150 sorter.setSortingStatus(j, column.equals(sortByColumn) ? TableSorter.ASCENDING : TableSorter.NOT_SORTED);
151 break;
157 TableModel getTableModel()
159 return tableModel;
162 void dataChanged()
164 int idx = table.getSelectedRow();
165 tableModel.fireTableDataChanged();
166 if (idx != -1)
167 table.getSelectionModel().addSelectionInterval(idx, idx);
170 public int getModelIndex(int viewIndex)
172 return sorter.modelIndex(viewIndex);
175 public int[] getSelectedItems()
177 return table.getSelectedRows();
180 public GitPropertiesNode[] getNodes()
182 return tableModel.getNodes();
185 public void setNodes(GitPropertiesNode[] nodes)
187 tableModel.setNodes(nodes);
190 public JComponent getComponent()
192 return component;
195 public void ancestorAdded(AncestorEvent arg0)
197 setDefaultColumnSize();
200 public void ancestorRemoved(AncestorEvent arg0)
204 public void ancestorMoved(AncestorEvent arg0)
208 public void tableChanged(TableModelEvent event)
210 table.repaint();
213 public class PropertiesTableCellRenderer extends DefaultTableCellRenderer {
215 @Override
216 public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int rowIndex, int columnIndex)
218 Component renderer = super.getTableCellRendererComponent(table, value, hasFocus, hasFocus, rowIndex, columnIndex);
219 if (renderer instanceof JComponent) {
220 String strValue = tableModel.getNode(sorter.modelIndex(rowIndex)).getValue();
221 ((JComponent) renderer).setToolTipText(strValue);
223 setToolTipText(value.toString());
224 return renderer;