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
.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
.netbeans
.modules
.git
.ui
.properties
.GitPropertiesNode
;
59 import org
.netbeans
.modules
.versioning
.util
.TableSorter
;
60 import org
.openide
.util
.NbBundle
;
66 public class PropertiesTable
implements AncestorListener
, TableModelListener
{
68 static public final String
[] PROPERTIES_COLUMNS
= new String
[] {PropertiesTableModel
.COLUMN_NAME_NAME
, PropertiesTableModel
.COLUMN_NAME_VALUE
};
70 private PropertiesTableModel tableModel
;
72 private TableSorter sorter
;
73 private JComponent component
;
74 private String
[] columns
;
75 private String
[] sortByColumns
;
77 /** Creates a new instance of PropertiesTable */
78 public PropertiesTable(JLabel label
, String
[] columns
, String
[] sortByColumns
) {
79 init(label
, columns
, null);
80 this.sortByColumns
= sortByColumns
;
84 public PropertiesTable(JLabel label
, String
[] columns
, TableSorter sorter
) {
85 init(label
, columns
, sorter
);
88 private void init(JLabel label
, String
[] columns
, TableSorter sorter
) {
89 tableModel
= new PropertiesTableModel(columns
);
90 tableModel
.addTableModelListener(this);
92 sorter
= new TableSorter(tableModel
);
95 table
= new JTable(this.sorter
);
96 table
.getTableHeader().setReorderingAllowed(false);
97 table
.setDefaultRenderer(String
.class, new PropertiesTableCellRenderer());
98 //table.setDefaultEditor(CommitOptions.class, new CommitOptionsCellEditor());
99 table
.getTableHeader().setReorderingAllowed(true);
100 this.sorter
.setTableHeader(table
.getTableHeader());
101 table
.setRowHeight(table
.getRowHeight());
102 table
.addAncestorListener(this);
103 component
= new JScrollPane(table
, JScrollPane
.VERTICAL_SCROLLBAR_ALWAYS
, JScrollPane
.HORIZONTAL_SCROLLBAR_AS_NEEDED
);
104 component
.setPreferredSize(new Dimension(340, 150));
105 table
.getAccessibleContext().setAccessibleDescription(NbBundle
.getMessage(PropertiesTable
.class, "ACSD_PropertiesTable")); // NOI18N
106 label
.setLabelFor(table
);
110 public void setColumns(String
[] clmns
) {
111 if (Arrays
.equals(columns
, clmns
))
114 tableModel
.setColumns(clmns
);
115 setDefaultColumnSize();
118 public JTable
getTable() {
122 private void setDefaultColumnSize() {
123 int width
= table
.getWidth();
124 TableColumnModel columnModel
= table
.getColumnModel();
125 if (columns
== null || columnModel
== null)
127 if (columnModel
.getColumnCount() != columns
.length
)
129 for (int i
= 0; i
< columns
.length
; i
++) {
130 String col
= columns
[i
];
131 sorter
.setColumnComparator(i
, null);
132 if (col
.equals(PropertiesTableModel
.COLUMN_NAME_NAME
)) {
133 columnModel
.getColumn(i
).setPreferredWidth(width
* 20 / 100);
134 } else if (col
.equals(PropertiesTableModel
.COLUMN_NAME_VALUE
)) {
135 columnModel
.getColumn(i
).setPreferredWidth(width
* 40 / 100);
140 private void setSortingStatus() {
141 for (int i
= 0; i
< sortByColumns
.length
; i
++) {
142 String sortByColumn
= sortByColumns
[i
];
143 for (int j
= 0; j
< columns
.length
; j
++) {
144 String column
= columns
[j
];
145 if(column
.equals(sortByColumn
)) {
146 sorter
.setSortingStatus(j
, column
.equals(sortByColumn
) ? TableSorter
.ASCENDING
: TableSorter
.NOT_SORTED
);
153 TableModel
getTableModel() {
158 int idx
= table
.getSelectedRow();
159 tableModel
.fireTableDataChanged();
161 table
.getSelectionModel().addSelectionInterval(idx
, idx
);
165 public int getModelIndex(int viewIndex
) {
166 return sorter
.modelIndex(viewIndex
);
169 public int[] getSelectedItems() {
170 return table
.getSelectedRows();
173 public GitPropertiesNode
[] getNodes() {
174 return tableModel
.getNodes();
177 public void setNodes(GitPropertiesNode
[] nodes
) {
178 tableModel
.setNodes(nodes
);
181 public JComponent
getComponent() {
185 public void ancestorAdded(AncestorEvent arg0
) {
186 setDefaultColumnSize();
189 public void ancestorRemoved(AncestorEvent arg0
) {
192 public void ancestorMoved(AncestorEvent arg0
) {
195 public void tableChanged(TableModelEvent event
) {
199 public class PropertiesTableCellRenderer
extends DefaultTableCellRenderer
{
202 public Component
getTableCellRendererComponent(JTable table
, Object value
, boolean isSelected
, boolean hasFocus
, int rowIndex
, int columnIndex
) {
203 Component renderer
= super.getTableCellRendererComponent(table
, value
, hasFocus
, hasFocus
, rowIndex
, columnIndex
);
204 if (renderer
instanceof JComponent
) {
205 String strValue
= tableModel
.getNode(sorter
.modelIndex(rowIndex
)).getValue();
206 ((JComponent
) renderer
).setToolTipText(strValue
);
208 setToolTipText(value
.toString());