001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041: package org.netbeans.modules.mercurial.options;
042:
043: import java.awt.Component;
044: import java.awt.Dimension;
045: import java.util.Arrays;
046: import javax.swing.JComponent;
047: import javax.swing.JLabel;
048: import javax.swing.JScrollPane;
049: import javax.swing.JTable;
050: import javax.swing.event.AncestorEvent;
051: import javax.swing.event.AncestorListener;
052: import javax.swing.event.TableModelEvent;
053: import javax.swing.event.TableModelListener;
054: import javax.swing.table.DefaultTableCellRenderer;
055: import javax.swing.table.TableColumnModel;
056: import javax.swing.table.TableModel;
057: import org.netbeans.modules.versioning.util.TableSorter;
058: import org.netbeans.modules.mercurial.ui.properties.HgPropertiesNode;
059: import org.netbeans.modules.mercurial.options.PropertiesTableModel;
060: import org.openide.util.NbBundle;
061:
062: /**
063: *
064: * @author Peter Pis
065: */
066: public class PropertiesTable implements AncestorListener,
067: TableModelListener {
068:
069: static public final String[] PROPERTIES_COLUMNS = new String[] {
070: PropertiesTableModel.COLUMN_NAME_NAME,
071: PropertiesTableModel.COLUMN_NAME_VALUE };
072:
073: private PropertiesTableModel tableModel;
074: private JTable table;
075: private TableSorter sorter;
076: private JComponent component;
077: private String[] columns;
078: private String[] sortByColumns;
079:
080: /** Creates a new instance of PropertiesTable */
081: public PropertiesTable(JLabel label, String[] columns,
082: String[] sortByColumns) {
083: init(label, columns, null);
084: this .sortByColumns = sortByColumns;
085: setSortingStatus();
086: }
087:
088: public PropertiesTable(JLabel label, String[] columns,
089: TableSorter sorter) {
090: init(label, columns, sorter);
091: }
092:
093: private void init(JLabel label, String[] columns, TableSorter sorter) {
094: tableModel = new PropertiesTableModel(columns);
095: tableModel.addTableModelListener(this );
096: if (sorter == null) {
097: sorter = new TableSorter(tableModel);
098: }
099: this .sorter = sorter;
100: table = new JTable(this .sorter);
101: table.getTableHeader().setReorderingAllowed(false);
102: table.setDefaultRenderer(String.class,
103: new PropertiesTableCellRenderer());
104: //table.setDefaultEditor(CommitOptions.class, new CommitOptionsCellEditor());
105: table.getTableHeader().setReorderingAllowed(true);
106: this .sorter.setTableHeader(table.getTableHeader());
107: table.setRowHeight(table.getRowHeight());
108: table.addAncestorListener(this );
109: component = new JScrollPane(table,
110: JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
111: JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
112: component.setPreferredSize(new Dimension(340, 150));
113: table.getAccessibleContext().setAccessibleDescription(
114: NbBundle.getMessage(PropertiesTable.class,
115: "ACSD_PropertiesTable")); // NOI18N
116: label.setLabelFor(table);
117: setColumns(columns);
118: }
119:
120: public void setColumns(String[] clmns) {
121: if (Arrays.equals(columns, clmns))
122: return;
123: columns = clmns;
124: tableModel.setColumns(clmns);
125: setDefaultColumnSize();
126: }
127:
128: public JTable getTable() {
129: return table;
130: }
131:
132: private void setDefaultColumnSize() {
133: int width = table.getWidth();
134: TableColumnModel columnModel = table.getColumnModel();
135: if (columns == null || columnModel == null)
136: return;
137: if (columnModel.getColumnCount() != columns.length)
138: return;
139: for (int i = 0; i < columns.length; i++) {
140: String col = columns[i];
141: sorter.setColumnComparator(i, null);
142: if (col.equals(PropertiesTableModel.COLUMN_NAME_NAME)) {
143: columnModel.getColumn(i).setPreferredWidth(
144: width * 20 / 100);
145: } else if (col
146: .equals(PropertiesTableModel.COLUMN_NAME_VALUE)) {
147: columnModel.getColumn(i).setPreferredWidth(
148: width * 40 / 100);
149: }
150: }
151: }
152:
153: private void setSortingStatus() {
154: for (int i = 0; i < sortByColumns.length; i++) {
155: String sortByColumn = sortByColumns[i];
156: for (int j = 0; j < columns.length; j++) {
157: String column = columns[j];
158: if (column.equals(sortByColumn)) {
159: sorter
160: .setSortingStatus(
161: j,
162: column.equals(sortByColumn) ? TableSorter.ASCENDING
163: : TableSorter.NOT_SORTED);
164: break;
165: }
166: }
167: }
168: }
169:
170: TableModel getTableModel() {
171: return tableModel;
172: }
173:
174: void dataChanged() {
175: int idx = table.getSelectedRow();
176: tableModel.fireTableDataChanged();
177: if (idx != -1) {
178: table.getSelectionModel().addSelectionInterval(idx, idx);
179: }
180: }
181:
182: public int getModelIndex(int viewIndex) {
183: return sorter.modelIndex(viewIndex);
184: }
185:
186: public int[] getSelectedItems() {
187: return table.getSelectedRows();
188: }
189:
190: public HgPropertiesNode[] getNodes() {
191: return tableModel.getNodes();
192: }
193:
194: public void setNodes(HgPropertiesNode[] nodes) {
195: tableModel.setNodes(nodes);
196: }
197:
198: public JComponent getComponent() {
199: return component;
200: }
201:
202: public void ancestorAdded(AncestorEvent arg0) {
203: setDefaultColumnSize();
204: }
205:
206: public void ancestorRemoved(AncestorEvent arg0) {
207: }
208:
209: public void ancestorMoved(AncestorEvent arg0) {
210: }
211:
212: public void tableChanged(TableModelEvent event) {
213: table.repaint();
214: }
215:
216: public class PropertiesTableCellRenderer extends
217: DefaultTableCellRenderer {
218:
219: public Component getTableCellRendererComponent(JTable table,
220: Object value, boolean isSelected, boolean hasFocus,
221: int rowIndex, int columnIndex) {
222: Component renderer = super .getTableCellRendererComponent(
223: table, value, hasFocus, hasFocus, rowIndex,
224: columnIndex);
225: if (renderer instanceof JComponent) {
226: String strValue = tableModel.getNode(
227: sorter.modelIndex(rowIndex)).getValue();
228: ((JComponent) renderer).setToolTipText(strValue);
229: }
230: setToolTipText(value.toString());
231: return renderer;
232: }
233: }
234:
235: }
|