KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > subversion > ui > properties > PropertiesTable


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19 package org.netbeans.modules.subversion.ui.properties;
20
21 import java.awt.Component JavaDoc;
22 import java.awt.Dimension JavaDoc;
23 import java.util.Arrays JavaDoc;
24 import javax.swing.JComponent JavaDoc;
25 import javax.swing.JScrollPane JavaDoc;
26 import javax.swing.JTable JavaDoc;
27 import javax.swing.event.AncestorEvent JavaDoc;
28 import javax.swing.event.AncestorListener JavaDoc;
29 import javax.swing.event.TableModelEvent JavaDoc;
30 import javax.swing.event.TableModelListener JavaDoc;
31 import javax.swing.table.DefaultTableCellRenderer JavaDoc;
32 import javax.swing.table.TableColumnModel JavaDoc;
33 import javax.swing.table.TableModel JavaDoc;
34 import org.netbeans.modules.versioning.util.TableSorter;
35 import org.openide.util.NbBundle;
36
37 /**
38  *
39  * @author Peter Pis
40  */

41 public class PropertiesTable implements AncestorListener JavaDoc, TableModelListener JavaDoc {
42     
43     static final String JavaDoc[] PROPERTIES_COLUMNS = new String JavaDoc[] {PropertiesTableModel.COLUMN_NAME_NAME, PropertiesTableModel.COLUMN_NAME_VALUE};
44             
45     private PropertiesTableModel tableModel;
46     private JTable JavaDoc table;
47     private TableSorter sorter;
48     private JComponent JavaDoc component;
49     private String JavaDoc[] columns;
50     private String JavaDoc[] sortByColumns;
51     
52     /** Creates a new instance of PropertiesTable */
53     public PropertiesTable(String JavaDoc[] columns, String JavaDoc[] sortByColumns) {
54         init(columns, null);
55         this.sortByColumns = sortByColumns;
56         setSortingStatus();
57     }
58     
59     public PropertiesTable(String JavaDoc[] columns, TableSorter sorter) {
60         init(columns, sorter);
61     }
62     
63     private void init(String JavaDoc[] columns, TableSorter sorter) {
64         tableModel = new PropertiesTableModel(columns);
65         tableModel.addTableModelListener(this);
66         if(sorter == null) {
67             sorter = new TableSorter(tableModel);
68         }
69         this.sorter = sorter;
70         table = new JTable JavaDoc(this.sorter);
71         table.getTableHeader().setReorderingAllowed(false);
72         table.setDefaultRenderer(String JavaDoc.class, new PropertiesTableCellRenderer());
73         //table.setDefaultEditor(CommitOptions.class, new CommitOptionsCellEditor());
74
table.getTableHeader().setReorderingAllowed(true);
75         this.sorter.setTableHeader(table.getTableHeader());
76         table.setRowHeight(table.getRowHeight());
77         table.addAncestorListener(this);
78         component = new JScrollPane JavaDoc(table, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
79         component.setPreferredSize(new Dimension JavaDoc(340, 150));
80         table.getAccessibleContext().setAccessibleDescription(NbBundle.getMessage(PropertiesTable.class, "ACSD_PropertiesTable")); // NOI18N
81
setColumns(columns);
82     }
83     
84     public void setColumns(String JavaDoc[] clmns) {
85         if (Arrays.equals(columns, clmns))
86             return;
87         columns = clmns;
88         tableModel.setColumns(clmns);
89         setDefaultColumnSize();
90     }
91     
92     public JTable JavaDoc getTable() {
93         return table;
94     }
95     
96     private void setDefaultColumnSize() {
97         int width = table.getWidth();
98         TableColumnModel JavaDoc columnModel = table.getColumnModel();
99         if (columns == null || columnModel == null)
100             return;
101         if (columnModel.getColumnCount() != columns.length)
102             return;
103         for (int i = 0; i < columns.length; i++) {
104             String JavaDoc col = columns[i];
105             sorter.setColumnComparator(i, null);
106             if (col.equals(PropertiesTableModel.COLUMN_NAME_NAME)) {
107                 columnModel.getColumn(i).setPreferredWidth(width * 20 / 100);
108             } else if (col.equals(PropertiesTableModel.COLUMN_NAME_VALUE)) {
109                 columnModel.getColumn(i).setPreferredWidth(width * 40 / 100);
110             }
111         }
112     }
113     
114     private void setSortingStatus() {
115         for (int i = 0; i < sortByColumns.length; i++) {
116             String JavaDoc sortByColumn = sortByColumns[i];
117             for (int j = 0; j < columns.length; j++) {
118                 String JavaDoc column = columns[j];
119                 if(column.equals(sortByColumn)) {
120                     sorter.setSortingStatus(j, column.equals(sortByColumn) ? TableSorter.ASCENDING : TableSorter.NOT_SORTED);
121                     break;
122                 }
123             }
124         }
125     }
126     
127     TableModel JavaDoc getTableModel() {
128         return tableModel;
129     }
130     
131     void dataChanged() {
132         int idx = table.getSelectedRow();
133         tableModel.fireTableDataChanged();
134         if (idx != -1) {
135             table.getSelectionModel().addSelectionInterval(idx, idx);
136         }
137     }
138     
139     public int getModelIndex(int viewIndex) {
140         return sorter.modelIndex(viewIndex);
141     }
142     
143     public int[] getSelectedItems() {
144         return table.getSelectedRows();
145     }
146      
147     public SvnPropertiesNode[] getNodes() {
148         return tableModel.getNodes();
149     }
150     
151     public void setNodes(SvnPropertiesNode[] nodes) {
152         tableModel.setNodes(nodes);
153     }
154     
155     JComponent JavaDoc getComponent() {
156         return component;
157     }
158     
159     public void ancestorAdded(AncestorEvent JavaDoc arg0) {
160         setDefaultColumnSize();
161     }
162
163     public void ancestorRemoved(AncestorEvent JavaDoc arg0) {
164     }
165
166     public void ancestorMoved(AncestorEvent JavaDoc arg0) {
167     }
168
169     public void tableChanged(TableModelEvent JavaDoc event) {
170         table.repaint();
171     }
172
173     public class PropertiesTableCellRenderer extends DefaultTableCellRenderer JavaDoc {
174            
175         public Component JavaDoc getTableCellRendererComponent(JTable JavaDoc table, Object JavaDoc value, boolean isSelected, boolean hasFocus, int rowIndex, int columnIndex) {
176             Component JavaDoc renderer = super.getTableCellRendererComponent(table, value, hasFocus, hasFocus, rowIndex, columnIndex);
177             if (renderer instanceof JComponent JavaDoc) {
178                 String JavaDoc strValue = tableModel.getNode(sorter.modelIndex(rowIndex)).getValue();
179                 ((JComponent JavaDoc) renderer).setToolTipText(strValue);
180             }
181             setToolTipText(value.toString());
182             return renderer;
183         }
184     }
185     
186     
187 }
188
Popular Tags