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.subversion.ui.commit;
44
import org.netbeans.modules.versioning.util.FilePathCellRenderer;
45
import org.netbeans.modules.versioning.util.TableSorter;
46
import org.netbeans.modules.subversion.util.SvnUtils;
47
import org.netbeans.modules.subversion.SvnFileNode;
48
import org.netbeans.modules.subversion.FileInformation;
49
import org.netbeans.modules.subversion.Subversion;
50
import org.openide.util.NbBundle;
53
import javax.swing.event.AncestorEvent;
54
import javax.swing.event.AncestorListener;
55
import javax.swing.event.TableModelListener;
56
import javax.swing.event.TableModelEvent;
57
import javax.swing.table.DefaultTableCellRenderer;
58
import javax.swing.table.TableModel;
59
import javax.swing.table.TableColumnModel;
60
import java.awt.Component;
61
import java.lang.String;
65
* {@link #getComponent Table} that displays nodes in the commit dialog.
67
* @author Maros Sandor
69
public class CommitTable implements AncestorListener, TableModelListener {
71
public static String [] COMMIT_COLUMNS = new String [] {
72
CommitTableModel.COLUMN_NAME_NAME,
73
CommitTableModel.COLUMN_NAME_STATUS,
74
CommitTableModel.COLUMN_NAME_ACTION,
75
CommitTableModel.COLUMN_NAME_PATH
78
public static String [] IMPORT_COLUMNS = new String [] {
79
CommitTableModel.COLUMN_NAME_NAME,
80
CommitTableModel.COLUMN_NAME_ACTION,
81
CommitTableModel.COLUMN_NAME_PATH
84
private CommitTableModel tableModel;
86
private JComponent component;
88
private TableSorter sorter;
89
private String[] columns;
90
private String[] sortByColumns;
93
public CommitTable(JLabel label, String[] columns, String[] sortByColumns) {
94
init(label, columns, null);
95
this.sortByColumns = sortByColumns;
99
public CommitTable(JLabel label, String[] columns, TableSorter sorter) {
100
init(label, columns, sorter);
103
private void init(JLabel label, String[] columns, TableSorter sorter) {
104
tableModel = new CommitTableModel(columns);
105
tableModel.addTableModelListener(this);
107
sorter = new TableSorter(tableModel);
109
this.sorter = sorter;
110
table = new JTable(this.sorter);
111
table.getTableHeader().setReorderingAllowed(false);
112
table.setDefaultRenderer(String.class, new CommitStringsCellRenderer());
113
table.setDefaultEditor(CommitOptions.class, new CommitOptionsCellEditor());
114
table.getTableHeader().setReorderingAllowed(true);
115
this.sorter.setTableHeader(table.getTableHeader());
116
table.setRowHeight(table.getRowHeight() * 6 / 5);
117
table.addAncestorListener(this);
118
component = new JScrollPane(table, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
119
label.setLabelFor(table);
120
table.getAccessibleContext().setAccessibleDescription(NbBundle.getMessage(CommitTable.class, "ACSD_CommitTable")); // NOI18N
124
public void ancestorAdded(AncestorEvent event) {
125
setDefaultColumnSizes();
129
* Sets sizes of Commit table columns, kind of hardcoded.
131
private void setDefaultColumnSizes() {
132
int width = table.getWidth();
133
TableColumnModel columnModel = table.getColumnModel();
134
if (columns == null || columnModel == null) return; // unsure when this methed will be called (component realization)
135
if (columnModel.getColumnCount() != columns.length) return;
136
if (columns.length == 3) {
137
for (int i = 0; i < columns.length; i++) {
138
String col = columns[i];
139
sorter.setColumnComparator(i, null);
140
if (col.equals(CommitTableModel.COLUMN_NAME_NAME)) {
141
sorter.setColumnComparator(i, new FileNameComparator());
142
columnModel.getColumn(i).setPreferredWidth(width * 30 / 100);
143
} else if (col.equals(CommitTableModel.COLUMN_NAME_ACTION)) {
144
columnModel.getColumn(i).setPreferredWidth(width * 15 / 100);
146
columnModel.getColumn(i).setPreferredWidth(width * 40 / 100);
149
} else if (columns.length == 4) {
150
for (int i = 0; i < columns.length; i++) {
151
String col = columns[i];
152
sorter.setColumnComparator(i, null);
153
if (col.equals(CommitTableModel.COLUMN_NAME_NAME)) {
154
sorter.setColumnComparator(i, new FileNameComparator());
155
columnModel.getColumn(i).setPreferredWidth(width * 30 / 100);
156
} else if (col.equals(CommitTableModel.COLUMN_NAME_STATUS)) {
157
sorter.setColumnComparator(i, new StatusComparator());
158
columnModel.getColumn(i).setPreferredWidth(width * 15 / 100);
159
} else if (col.equals(CommitTableModel.COLUMN_NAME_ACTION)) {
160
columnModel.getColumn(i).setPreferredWidth(width * 15 / 100);
162
columnModel.getColumn(i).setPreferredWidth(width * 40 / 100);
165
} else if (columns.length == 5) {
166
for (int i = 0; i < columns.length; i++) {
167
String col = columns[i];
168
sorter.setColumnComparator(i, null);
169
if (col.equals(CommitTableModel.COLUMN_NAME_NAME)) {
170
sorter.setColumnComparator(i, new FileNameComparator());
171
columnModel.getColumn(i).setPreferredWidth(width * 25 / 100);
172
} else if (col.equals(CommitTableModel.COLUMN_NAME_STATUS)) {
173
sorter.setColumnComparator(i, new StatusComparator());
174
sorter.setSortingStatus(i, TableSorter.ASCENDING);
175
columnModel.getColumn(i).setPreferredWidth(width * 15 / 100);
176
} else if (col.equals(CommitTableModel.COLUMN_NAME_ACTION)) {
177
columnModel.getColumn(i).setPreferredWidth(width * 15 / 100);
179
columnModel.getColumn(i).setPreferredWidth(width * 30 / 100);
185
private void setSortingStatus() {
186
for (int i = 0; i < sortByColumns.length; i++) {
187
String sortByColumn = sortByColumns[i];
188
for (int j = 0; j < columns.length; j++) {
189
String column = columns[j];
190
if(column.equals(sortByColumn)) {
191
sorter.setSortingStatus(j, column.equals(sortByColumn) ? TableSorter.ASCENDING : TableSorter.NOT_SORTED);
198
public TableSorter getSorter() {
202
public void ancestorMoved(AncestorEvent event) {
205
public void ancestorRemoved(AncestorEvent event) {
208
void setColumns(String[] cols) {
209
if (Arrays.equals(columns, cols)) return;
211
tableModel.setColumns(cols);
212
setDefaultColumnSizes();
215
public void setNodes(SvnFileNode[] nodes) {
216
tableModel.setNodes(nodes);
220
* @return Map<SvnFileNode, CommitOptions>
222
public Map<SvnFileNode, CommitOptions> getCommitFiles() {
223
return tableModel.getCommitFiles();
227
* @return table in a scrollpane
229
public JComponent getComponent() {
234
int idx = table.getSelectedRow();
235
tableModel.fireTableDataChanged();
236
if (idx != -1) table.getSelectionModel().addSelectionInterval(idx, idx);
239
TableModel getTableModel() {
243
public void tableChanged(TableModelEvent e) {
244
// change in commit options may alter name rendering (strikethrough)
248
public void setRootFile(String repositoryPath, String rootLocalPath) {
249
tableModel.setRootFile(repositoryPath, rootLocalPath);
252
private class CommitOptionsCellEditor extends DefaultCellEditor {
254
private final Object[] dirAddOptions = new Object [] {
255
CommitOptions.ADD_DIRECTORY,
256
CommitOptions.EXCLUDE
259
private final Object[] addOptions = new Object [] {
260
CommitOptions.ADD_TEXT,
261
CommitOptions.ADD_BINARY,
262
CommitOptions.EXCLUDE
264
private final Object[] commitOptions = new Object [] {
265
CommitOptions.COMMIT,
266
CommitOptions.EXCLUDE
269
private final Object[] removeOptions = new Object [] {
270
CommitOptions.COMMIT_REMOVE,
271
CommitOptions.EXCLUDE
274
public CommitOptionsCellEditor() {
275
super(new JComboBox());
278
public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) {
279
FileInformation info = tableModel.getNode(sorter.modelIndex(row)).getInformation();
280
int fileStatus = info.getStatus();
281
JComboBox combo = (JComboBox) editorComponent;
282
if (fileStatus == FileInformation.STATUS_VERSIONED_DELETEDLOCALLY || fileStatus == FileInformation.STATUS_VERSIONED_REMOVEDLOCALLY) {
283
combo.setModel(new DefaultComboBoxModel(removeOptions));
284
} else if ((fileStatus & FileInformation.STATUS_IN_REPOSITORY) == 0) {
285
if (info.isDirectory()) {
286
combo.setModel(new DefaultComboBoxModel(dirAddOptions));
288
combo.setModel(new DefaultComboBoxModel(addOptions));
291
combo.setModel(new DefaultComboBoxModel(commitOptions));
293
return super.getTableCellEditorComponent(table, value, isSelected, row, column);
297
private class CommitStringsCellRenderer extends DefaultTableCellRenderer {
299
private FilePathCellRenderer pathRenderer = new FilePathCellRenderer();
301
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
302
int col = table.convertColumnIndexToModel(column);
303
if (columns[col] == CommitTableModel.COLUMN_NAME_NAME) {
304
TableSorter sorter = (TableSorter) table.getModel();
305
CommitTableModel model = (CommitTableModel) sorter.getTableModel();
306
SvnFileNode node = model.getNode(sorter.modelIndex(row));
307
CommitOptions options = model.getOptions(sorter.modelIndex(row));
309
value = "<html>" + Subversion.getInstance().getAnnotator().annotateNameHtml( // NOI18N
310
node.getFile().getName(), node.getInformation(), null);
312
if (options == CommitOptions.EXCLUDE) {
313
value = "<html><s>" + value + "</s></html>"; // NOI18N
315
return super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
316
} else if (columns[col] == CommitTableModel.COLUMN_NAME_PATH) {
317
return pathRenderer.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
319
return super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
324
private class StatusComparator extends SvnUtils.ByImportanceComparator {
325
public int compare(Object o1, Object o2) {
326
Integer row1 = (Integer) o1;
327
Integer row2 = (Integer) o2;
328
return super.compare(tableModel.getNode(row1.intValue()).getInformation(),
329
tableModel.getNode(row2.intValue()).getInformation());
333
private class FileNameComparator implements Comparator {
334
public int compare(Object o1, Object o2) {
335
Integer row1 = (Integer) o1;
336
Integer row2 = (Integer) o2;
337
return tableModel.getNode(row1.intValue()).getName().compareToIgnoreCase(
338
tableModel.getNode(row2.intValue()).getName());