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.
30
* If you wish your version of this file to be governed by only the CDDL
31
* or only the GPL Version 2, indicate your decision by adding
32
* "[Contributor] elects to include this software in this distribution
33
* under the [CDDL or GPL Version 2] license." If you do not indicate a
34
* single choice of license, a recipient has the option to distribute
35
* your version of this file under either the CDDL, the GPL Version 2 or
36
* to extend the choice of license to its licensees as provided above.
37
* However, if you add GPL Version 2 code and therefore, elected the GPL
38
* Version 2 license, then the option applies only if the new code is
39
* made subject to such option by the copyright holder.
42
package org.netbeans.modules.versioning.system.cvss.ui.actions.commit;
44
import org.netbeans.modules.versioning.system.cvss.*;
45
import org.netbeans.modules.versioning.system.cvss.util.Utils;
46
import org.netbeans.modules.versioning.util.FilePathCellRenderer;
47
import org.netbeans.modules.versioning.util.TableSorter;
48
import org.openide.util.NbBundle;
51
import javax.swing.event.AncestorEvent;
52
import javax.swing.event.AncestorListener;
53
import javax.swing.event.TableModelListener;
54
import javax.swing.event.TableModelEvent;
55
import javax.swing.table.DefaultTableCellRenderer;
56
import javax.swing.table.TableModel;
57
import javax.swing.table.TableColumnModel;
58
import java.awt.Component;
62
* View that displays nodes in the Synchronize view.
64
* @author Maros Sandor
66
class CommitTable implements AncestorListener, TableModelListener {
68
private CommitTableModel tableModel;
70
private JComponent component;
72
private TableSorter sorter;
73
private String[] columns;
75
public CommitTable(JLabel label) {
76
tableModel = new CommitTableModel();
77
tableModel.addTableModelListener(this);
78
sorter = new TableSorter(tableModel);
79
table = new JTable(sorter);
80
table.getTableHeader().setReorderingAllowed(false);
81
table.setDefaultRenderer(String.class, new CommitStringsCellRenderer());
82
table.setDefaultEditor(CommitOptions.class, new CommitOptionsCellEditor());
83
table.getTableHeader().setReorderingAllowed(true);
84
sorter.setTableHeader(table.getTableHeader());
85
int height = new JLabel("FONTSIZE").getPreferredSize().height * 6 / 5; // NOI18N
86
table.setRowHeight(height);
87
table.addAncestorListener(this);
88
component = new JScrollPane(table, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
89
label.setLabelFor(table);
90
table.getAccessibleContext().setAccessibleDescription(NbBundle.getMessage(CommitTable.class, "ACSD_CommitTable")); // NOI18N
93
public void ancestorAdded(AncestorEvent event) {
94
setDefaultColumnSizes();
98
* Sets sizes of Commit table columns, kind of hardcoded.
100
private void setDefaultColumnSizes() {
101
int width = table.getWidth();
102
TableColumnModel columnModel = table.getColumnModel();
103
if (columns == null || columnModel == null) return; // unsure when this methed will be called (component realization)
104
if (columnModel.getColumnCount() != columns.length) return;
105
if (columns.length == 4) {
106
for (int i = 0; i < columns.length; i++) {
107
String col = columns[i];
108
sorter.setColumnComparator(i, null);
109
sorter.setSortingStatus(i, TableSorter.NOT_SORTED);
110
if (col.equals(CommitSettings.COLUMN_NAME_NAME)) {
111
sorter.setColumnComparator(i, new FileNameComparator());
112
columnModel.getColumn(i).setPreferredWidth(width * 30 / 100);
113
} else if (col.equals(CommitSettings.COLUMN_NAME_STATUS)) {
114
sorter.setColumnComparator(i, new StatusComparator());
115
sorter.setSortingStatus(i, TableSorter.ASCENDING);
116
columnModel.getColumn(i).setPreferredWidth(width * 15 / 100);
117
} else if (col.equals(CommitSettings.COLUMN_NAME_ACTION)) {
118
columnModel.getColumn(i).setPreferredWidth(width * 15 / 100);
120
columnModel.getColumn(i).setPreferredWidth(width * 40 / 100);
123
} else if (columns.length == 5) {
124
for (int i = 0; i < columns.length; i++) {
125
String col = columns[i];
126
sorter.setColumnComparator(i, null);
127
sorter.setSortingStatus(i, TableSorter.NOT_SORTED);
128
if (col.equals(CommitSettings.COLUMN_NAME_NAME)) {
129
sorter.setColumnComparator(i, new FileNameComparator());
130
columnModel.getColumn(i).setPreferredWidth(width * 25 / 100);
131
} else if (col.equals(CommitSettings.COLUMN_NAME_STICKY)) {
132
columnModel.getColumn(i).setPreferredWidth(width * 15 / 100);
133
} else if (col.equals(CommitSettings.COLUMN_NAME_STATUS)) {
134
sorter.setColumnComparator(i, new StatusComparator());
135
sorter.setSortingStatus(i, TableSorter.ASCENDING);
136
columnModel.getColumn(i).setPreferredWidth(width * 15 / 100);
137
} else if (col.equals(CommitSettings.COLUMN_NAME_ACTION)) {
138
columnModel.getColumn(i).setPreferredWidth(width * 15 / 100);
140
columnModel.getColumn(i).setPreferredWidth(width * 30 / 100);
146
public void ancestorMoved(AncestorEvent event) {
149
public void ancestorRemoved(AncestorEvent event) {
152
void setColumns(String[] cols) {
153
if (Arrays.equals(columns, cols)) return;
155
tableModel.setColumns(cols);
156
setDefaultColumnSizes();
159
void setNodes(CvsFileNode[] nodes) {
160
tableModel.setNodes(nodes);
163
public CommitSettings.CommitFile[] getCommitFiles() {
164
return tableModel.getCommitFiles();
167
public JComponent getComponent() {
172
int idx = table.getSelectedRow();
173
tableModel.fireTableDataChanged();
174
if (idx != -1) table.getSelectionModel().addSelectionInterval(idx, idx);
177
TableModel getTableModel() {
181
public void tableChanged(TableModelEvent e) {
182
// change in commit options may alter name rendering (strikethrough)
186
private class CommitOptionsCellEditor extends DefaultCellEditor {
188
private final Object[] addOptions = new Object [] {
189
CommitOptions.ADD_TEXT,
190
CommitOptions.ADD_BINARY,
191
CommitOptions.EXCLUDE
193
private final Object[] commitOptions = new Object [] {
194
CommitOptions.COMMIT,
195
CommitOptions.EXCLUDE
198
private final Object[] removeOptions = new Object [] {
199
CommitOptions.COMMIT_REMOVE,
200
CommitOptions.EXCLUDE
203
public CommitOptionsCellEditor() {
204
super(new JComboBox());
207
public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) {
208
int fileStatus = tableModel.getCommitFile(sorter.modelIndex(row)).getNode().getInformation().getStatus();
209
JComboBox combo = (JComboBox) editorComponent;
210
if (fileStatus == FileInformation.STATUS_VERSIONED_DELETEDLOCALLY || fileStatus == FileInformation.STATUS_VERSIONED_REMOVEDLOCALLY) {
211
combo.setModel(new DefaultComboBoxModel(removeOptions));
212
} else if ((fileStatus & FileInformation.STATUS_IN_REPOSITORY) == 0 && fileStatus != FileInformation.STATUS_VERSIONED_ADDEDLOCALLY) {
213
combo.setModel(new DefaultComboBoxModel(addOptions));
215
combo.setModel(new DefaultComboBoxModel(commitOptions));
217
return super.getTableCellEditorComponent(table, value, isSelected, row, column);
221
private static class CommitStringsCellRenderer extends DefaultTableCellRenderer {
223
private FilePathCellRenderer pathRenderer = new FilePathCellRenderer();
225
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
226
int col = table.convertColumnIndexToModel(column);
228
TableSorter sorter = (TableSorter) table.getModel();
229
CommitTableModel model = (CommitTableModel) sorter.getTableModel();
230
CommitSettings.CommitFile commitFile = model.getCommitFile(sorter.modelIndex(row));
232
value = "<html>" + CvsVersioningSystem.getInstance().getAnnotator().annotateNameHtml( // NOI18N
233
commitFile.getNode().getFile().getName(), commitFile.getNode().getInformation(), null);
235
if (commitFile.getOptions() == CommitOptions.EXCLUDE) {
236
value = "<html><s>" + value + "</s></html>"; // NOI18N
238
return super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
239
} else if (col == 3) {
240
return pathRenderer.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
242
return super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
247
private class StatusComparator extends Utils.ByImportanceComparator {
248
public int compare(Object o1, Object o2) {
249
Integer row1 = (Integer) o1;
250
Integer row2 = (Integer) o2;
251
return super.compare(tableModel.getCommitFile(row1.intValue()).getNode().getInformation(),
252
tableModel.getCommitFile(row2.intValue()).getNode().getInformation());
256
private class FileNameComparator implements Comparator {
257
public int compare(Object o1, Object o2) {
258
Integer row1 = (Integer) o1;
259
Integer row2 = (Integer) o2;
260
return tableModel.getCommitFile(row1.intValue()).getNode().getName().compareToIgnoreCase(
261
tableModel.getCommitFile(row2.intValue()).getNode().getName());