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]"
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
.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
;
62 * @author Padraig O'Briain
64 public class PropertiesTable
implements AncestorListener
, TableModelListener
{
66 static public final String
[] PROPERTIES_COLUMNS
= new String
[] {PropertiesTableModel
.COLUMN_NAME_NAME
, PropertiesTableModel
.COLUMN_NAME_VALUE
};
68 private PropertiesTableModel tableModel
;
70 private JComponent component
;
71 private String
[] columns
;
73 /** Creates a new instance of PropertiesTable */
74 public PropertiesTable(JLabel label
, String
[] 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.setDefaultEditor(CommitOptions.class, new CommitOptionsCellEditor());
85 table
.setRowHeight(table
.getRowHeight());
86 table
.addAncestorListener(this);
87 component
= new JScrollPane(table
, JScrollPane
.VERTICAL_SCROLLBAR_ALWAYS
, JScrollPane
.HORIZONTAL_SCROLLBAR_AS_NEEDED
);
88 component
.setPreferredSize(new Dimension(340, 150));
89 table
.getAccessibleContext().setAccessibleDescription(NbBundle
.getMessage(PropertiesTable
.class, "ACSD_PropertiesTable")); // NOI18N
90 table
.getAccessibleContext().setAccessibleName(NbBundle
.getMessage(PropertiesTable
.class, "ACSN_PropertiesTable")); // NOI18N
91 label
.setLabelFor(table
);
95 public void setColumns(String
[] clmns
) {
96 if (Arrays
.equals(columns
, clmns
))
99 tableModel
.setColumns(clmns
);
100 setDefaultColumnSize();
103 public JTable
getTable() {
107 private void setDefaultColumnSize() {
108 int width
= table
.getWidth();
109 TableColumnModel columnModel
= table
.getColumnModel();
110 if (columns
== null || columnModel
== null)
112 if (columnModel
.getColumnCount() != columns
.length
)
114 for (int i
= 0; i
< columns
.length
; i
++) {
115 String col
= columns
[i
];
116 if (col
.equals(PropertiesTableModel
.COLUMN_NAME_NAME
)) {
117 columnModel
.getColumn(i
).setPreferredWidth(width
* 20 / 100);
118 } else if (col
.equals(PropertiesTableModel
.COLUMN_NAME_VALUE
)) {
119 columnModel
.getColumn(i
).setPreferredWidth(width
* 40 / 100);
124 public TableModel
getTableModel() {
128 public void dataChanged() {
129 int idx
= table
.getSelectedRow();
130 tableModel
.fireTableDataChanged();
132 table
.getSelectionModel().addSelectionInterval(idx
, idx
);
136 public int[] getSelectedItems() {
137 return table
.getSelectedRows();
140 public GitPropertiesNode
[] getNodes() {
141 return tableModel
.getNodes();
144 public void setNodes(GitPropertiesNode
[] nodes
) {
145 tableModel
.setNodes(nodes
);
148 public JComponent
getComponent() {
152 public void ancestorAdded(AncestorEvent arg0
) {
153 setDefaultColumnSize();
156 public void ancestorRemoved(AncestorEvent arg0
) {
159 public void ancestorMoved(AncestorEvent arg0
) {
162 public void tableChanged(TableModelEvent event
) {
167 public class PropertiesTableCellRenderer
extends DefaultTableCellRenderer
{
170 public Component
getTableCellRendererComponent(JTable table
, Object value
, boolean isSelected
, boolean hasFocus
, int rowIndex
, int columnIndex
) {
171 Component renderer
= super.getTableCellRendererComponent(table
, value
, hasFocus
, hasFocus
, rowIndex
, columnIndex
);
172 if (renderer
instanceof JComponent
) {
173 String strValue
= tableModel
.getNode(rowIndex
).getValue();
174 ((JComponent
) renderer
).setToolTipText(strValue
);
176 setToolTipText(value
.toString());