Reenable the "Diff" link in the log summary view
[nbgit.git] / src / org / nbgit / ui / properties / PropertiesTable.java
blob22dcb648cf676f4102c592d82ac62bc21e764517
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.properties;
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.openide.util.NbBundle;
60 /**
62 * @author Padraig O'Briain
64 public class PropertiesTable implements AncestorListener, TableModelListener {
66 public static final String[] PROPERTIES_COLUMNS = new String[]{PropertiesTableModel.COLUMN_NAME_NAME, PropertiesTableModel.COLUMN_NAME_VALUE};
68 private PropertiesTableModel tableModel;
69 private JTable table;
70 private JComponent component;
71 private String[] columns;
73 /** Creates a new instance of PropertiesTable */
74 public PropertiesTable(JLabel label, String[] columns) {
75 init(label, columns);
78 private void init(JLabel label, String[] columns) {
79 tableModel = new PropertiesTableModel(columns);
80 tableModel.addTableModelListener(this);
81 table = new JTable(tableModel);
82 table.getTableHeader().setReorderingAllowed(false);
83 table.setDefaultRenderer(String.class, new PropertiesTableCellRenderer());
84 table.setRowHeight(table.getRowHeight());
85 table.addAncestorListener(this);
86 component = new JScrollPane(table, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
87 component.setPreferredSize(new Dimension(340, 150));
88 table.getAccessibleContext().setAccessibleDescription(NbBundle.getMessage(PropertiesTable.class, "ACSD_PropertiesTable")); // NOI18N
89 table.getAccessibleContext().setAccessibleName(NbBundle.getMessage(PropertiesTable.class, "ACSN_PropertiesTable")); // NOI18N
90 label.setLabelFor(table);
91 setColumns(columns);
94 public void setColumns(String[] clmns) {
95 if (Arrays.equals(columns, clmns))
96 return;
97 columns = clmns;
98 tableModel.setColumns(clmns);
99 setDefaultColumnSize();
102 public JTable getTable() {
103 return table;
106 private void setDefaultColumnSize() {
107 int width = table.getWidth();
108 TableColumnModel columnModel = table.getColumnModel();
109 if (columns == null || columnModel == null)
110 return;
111 if (columnModel.getColumnCount() != columns.length)
112 return;
113 for (int i = 0; i < columns.length; i++) {
114 String col = columns[i];
115 if (col.equals(PropertiesTableModel.COLUMN_NAME_NAME)) {
116 columnModel.getColumn(i).setPreferredWidth(width * 20 / 100);
117 } else if (col.equals(PropertiesTableModel.COLUMN_NAME_VALUE)) {
118 columnModel.getColumn(i).setPreferredWidth(width * 40 / 100);
123 public TableModel getTableModel() {
124 return tableModel;
127 public void dataChanged() {
128 int idx = table.getSelectedRow();
129 tableModel.fireTableDataChanged();
130 if (idx != -1) {
131 table.getSelectionModel().addSelectionInterval(idx, idx);
135 public int[] getSelectedItems() {
136 return table.getSelectedRows();
139 public GitPropertiesNode[] getNodes() {
140 return tableModel.getNodes();
143 public void setNodes(GitPropertiesNode[] nodes) {
144 tableModel.setNodes(nodes);
147 public JComponent getComponent() {
148 return component;
151 public void ancestorAdded(AncestorEvent arg0) {
152 setDefaultColumnSize();
155 public void ancestorRemoved(AncestorEvent arg0) {
158 public void ancestorMoved(AncestorEvent arg0) {
161 public void tableChanged(TableModelEvent event) {
162 table.repaint();
165 public class PropertiesTableCellRenderer extends DefaultTableCellRenderer {
167 @Override
168 public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int rowIndex, int columnIndex) {
169 Component renderer = super.getTableCellRendererComponent(table, value, hasFocus, hasFocus, rowIndex, columnIndex);
170 if (renderer instanceof JComponent) {
171 String strValue = tableModel.getNode(rowIndex).getValue();
172 ((JComponent) renderer).setToolTipText(strValue);
174 setToolTipText(value.toString());
175 return renderer;