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.ui.properties;
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.JScrollPane;
048: import javax.swing.JTable;
049: import javax.swing.JLabel;
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.openide.util.NbBundle;
059:
060: /**
061: *
062: * @author Padraig O'Briain
063: */
064: public class PropertiesTable implements AncestorListener,
065: TableModelListener {
066:
067: static public final String[] PROPERTIES_COLUMNS = new String[] {
068: PropertiesTableModel.COLUMN_NAME_NAME,
069: PropertiesTableModel.COLUMN_NAME_VALUE };
070:
071: private PropertiesTableModel tableModel;
072: private JTable table;
073: private JComponent component;
074: private String[] columns;
075:
076: /** Creates a new instance of PropertiesTable */
077: public PropertiesTable(JLabel label, String[] columns) {
078: init(label, columns);
079: }
080:
081: private void init(JLabel label, String[] columns) {
082: tableModel = new PropertiesTableModel(columns);
083: tableModel.addTableModelListener(this );
084: table = new JTable(tableModel);
085: table.getTableHeader().setReorderingAllowed(false);
086: table.setDefaultRenderer(String.class,
087: new PropertiesTableCellRenderer());
088: //table.setDefaultEditor(CommitOptions.class, new CommitOptionsCellEditor());
089: table.setRowHeight(table.getRowHeight());
090: table.addAncestorListener(this );
091: component = new JScrollPane(table,
092: JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
093: JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
094: component.setPreferredSize(new Dimension(340, 150));
095: table.getAccessibleContext().setAccessibleDescription(
096: NbBundle.getMessage(PropertiesTable.class,
097: "ACSD_PropertiesTable")); // NOI18N
098: table.getAccessibleContext().setAccessibleName(
099: NbBundle.getMessage(PropertiesTable.class,
100: "ACSN_PropertiesTable")); // NOI18N
101: label.setLabelFor(table);
102: setColumns(columns);
103: }
104:
105: public void setColumns(String[] clmns) {
106: if (Arrays.equals(columns, clmns))
107: return;
108: columns = clmns;
109: tableModel.setColumns(clmns);
110: setDefaultColumnSize();
111: }
112:
113: public JTable getTable() {
114: return table;
115: }
116:
117: private void setDefaultColumnSize() {
118: int width = table.getWidth();
119: TableColumnModel columnModel = table.getColumnModel();
120: if (columns == null || columnModel == null)
121: return;
122: if (columnModel.getColumnCount() != columns.length)
123: return;
124: for (int i = 0; i < columns.length; i++) {
125: String col = columns[i];
126: if (col.equals(PropertiesTableModel.COLUMN_NAME_NAME)) {
127: columnModel.getColumn(i).setPreferredWidth(
128: width * 20 / 100);
129: } else if (col
130: .equals(PropertiesTableModel.COLUMN_NAME_VALUE)) {
131: columnModel.getColumn(i).setPreferredWidth(
132: width * 40 / 100);
133: }
134: }
135: }
136:
137: public TableModel getTableModel() {
138: return tableModel;
139: }
140:
141: public void dataChanged() {
142: int idx = table.getSelectedRow();
143: tableModel.fireTableDataChanged();
144: if (idx != -1) {
145: table.getSelectionModel().addSelectionInterval(idx, idx);
146: }
147: }
148:
149: public int[] getSelectedItems() {
150: return table.getSelectedRows();
151: }
152:
153: public HgPropertiesNode[] getNodes() {
154: return tableModel.getNodes();
155: }
156:
157: public void setNodes(HgPropertiesNode[] nodes) {
158: tableModel.setNodes(nodes);
159: }
160:
161: public JComponent getComponent() {
162: return component;
163: }
164:
165: public void ancestorAdded(AncestorEvent arg0) {
166: setDefaultColumnSize();
167: }
168:
169: public void ancestorRemoved(AncestorEvent arg0) {
170: }
171:
172: public void ancestorMoved(AncestorEvent arg0) {
173: }
174:
175: public void tableChanged(TableModelEvent event) {
176: table.repaint();
177: }
178:
179: public class PropertiesTableCellRenderer extends
180: DefaultTableCellRenderer {
181:
182: public Component getTableCellRendererComponent(JTable table,
183: Object value, boolean isSelected, boolean hasFocus,
184: int rowIndex, int columnIndex) {
185: Component renderer = super .getTableCellRendererComponent(
186: table, value, hasFocus, hasFocus, rowIndex,
187: columnIndex);
188: if (renderer instanceof JComponent) {
189: String strValue = tableModel.getNode(rowIndex)
190: .getValue();
191: ((JComponent) renderer).setToolTipText(strValue);
192: }
193: setToolTipText(value.toString());
194: return renderer;
195: }
196: }
197:
198: }
|