Cleaned up whitespace, fix DOS line endings
[nbgit.git] / src / org / nbgit / ui / commit / CommitTable.java
blobc0bda963a5079475a3c960a2b48ce62587ad5612
1 /*
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]"
24 * Contributor(s):
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.
29 * Portions Copyright 2008 Alexander Coles (Ikonoklastik Productions).
31 * If you wish your version of this file to be governed by only the CDDL
32 * or only the GPL Version 2, indicate your decision by adding
33 * "[Contributor] elects to include this software in this distribution
34 * under the [CDDL or GPL Version 2] license." If you do not indicate a
35 * single choice of license, a recipient has the option to distribute
36 * your version of this file under either the CDDL, the GPL Version 2 or
37 * to extend the choice of license to its licensees as provided above.
38 * However, if you add GPL Version 2 code and therefore, elected the GPL
39 * Version 2 license, then the option applies only if the new code is
40 * made subject to such option by the copyright holder.
42 package org.nbgit.ui.commit;
44 import java.awt.Component;
45 import java.util.Arrays;
46 import java.util.Comparator;
47 import java.util.Map;
48 import javax.swing.DefaultCellEditor;
49 import javax.swing.DefaultComboBoxModel;
50 import javax.swing.JComboBox;
51 import javax.swing.JComponent;
52 import javax.swing.JLabel;
53 import javax.swing.JScrollPane;
54 import javax.swing.JTable;
55 import javax.swing.event.AncestorEvent;
56 import javax.swing.event.AncestorListener;
57 import javax.swing.event.TableModelEvent;
58 import javax.swing.event.TableModelListener;
59 import javax.swing.table.DefaultTableCellRenderer;
60 import javax.swing.table.TableColumnModel;
61 import javax.swing.table.TableModel;
62 import org.nbgit.StatusInfo;
63 import org.nbgit.ui.GitFileNode;
64 import org.nbgit.util.GitUtils;
65 import org.nbgit.util.HtmlFormatter;
66 import org.netbeans.modules.versioning.util.FilePathCellRenderer;
67 import org.netbeans.modules.versioning.util.TableSorter;
68 import org.openide.util.NbBundle;
70 /**
71 * {@link #getComponent Table} that displays nodes in the commit dialog.
73 * @author Maros Sandor
75 public class CommitTable implements AncestorListener, TableModelListener {
77 public static String[] COMMIT_COLUMNS = new String[]{
78 CommitTableModel.COLUMN_NAME_NAME,
79 CommitTableModel.COLUMN_NAME_STATUS,
80 CommitTableModel.COLUMN_NAME_ACTION,
81 CommitTableModel.COLUMN_NAME_PATH
83 public static String[] IMPORT_COLUMNS = new String[]{
84 CommitTableModel.COLUMN_NAME_NAME,
85 CommitTableModel.COLUMN_NAME_ACTION,
86 CommitTableModel.COLUMN_NAME_PATH
88 private CommitTableModel tableModel;
89 private JTable table;
90 private JComponent component;
91 private TableSorter sorter;
92 private String[] columns;
93 private String[] sortByColumns;
95 public CommitTable(JLabel label, String[] columns, String[] sortByColumns) {
96 init(label, columns, null);
97 this.sortByColumns = sortByColumns;
98 setSortingStatus();
101 public CommitTable(JLabel label, String[] columns, TableSorter sorter) {
102 init(label, columns, sorter);
105 private void init(JLabel label, String[] columns, TableSorter sorter) {
106 tableModel = new CommitTableModel(columns);
107 tableModel.addTableModelListener(this);
108 if (sorter == null) {
109 sorter = new TableSorter(tableModel);
111 this.sorter = sorter;
112 table = new JTable(this.sorter);
113 table.getTableHeader().setReorderingAllowed(false);
114 table.setDefaultRenderer(String.class, new CommitStringsCellRenderer());
115 table.setDefaultEditor(CommitOptions.class, new CommitOptionsCellEditor());
116 table.getTableHeader().setReorderingAllowed(true);
117 this.sorter.setTableHeader(table.getTableHeader());
118 table.setRowHeight(table.getRowHeight() * 6 / 5);
119 table.addAncestorListener(this);
120 component = new JScrollPane(table, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
121 label.setLabelFor(table);
122 table.getAccessibleContext().setAccessibleDescription(NbBundle.getMessage(CommitTable.class, "ACSD_CommitTable")); // NOI18N
123 setColumns(columns);
126 public void ancestorAdded(AncestorEvent event) {
127 setDefaultColumnSizes();
131 * Sets sizes of Commit table columns, kind of hardcoded.
133 private void setDefaultColumnSizes() {
134 int width = table.getWidth();
135 TableColumnModel columnModel = table.getColumnModel();
136 if (columns == null || columnModel == null) {
137 return; // unsure when this methed will be called (component realization)
139 if (columnModel.getColumnCount() != columns.length) {
140 return;
142 if (columns.length == 3) {
143 for (int i = 0; i < columns.length; i++) {
144 String col = columns[i];
145 sorter.setColumnComparator(i, null);
146 if (col.equals(CommitTableModel.COLUMN_NAME_NAME)) {
147 sorter.setColumnComparator(i, new FileNameComparator());
148 columnModel.getColumn(i).setPreferredWidth(width * 30 / 100);
149 } else if (col.equals(CommitTableModel.COLUMN_NAME_ACTION)) {
150 columnModel.getColumn(i).setPreferredWidth(width * 15 / 100);
151 } else {
152 columnModel.getColumn(i).setPreferredWidth(width * 40 / 100);
155 } else if (columns.length == 4) {
156 for (int i = 0; i < columns.length; i++) {
157 String col = columns[i];
158 sorter.setColumnComparator(i, null);
159 if (col.equals(CommitTableModel.COLUMN_NAME_NAME)) {
160 sorter.setColumnComparator(i, new FileNameComparator());
161 columnModel.getColumn(i).setPreferredWidth(width * 25 / 100);
162 } else if (col.equals(CommitTableModel.COLUMN_NAME_STATUS)) {
163 sorter.setColumnComparator(i, new StatusComparator());
164 columnModel.getColumn(i).setPreferredWidth(width * 15 / 100);
165 } else if (col.equals(CommitTableModel.COLUMN_NAME_ACTION)) {
166 columnModel.getColumn(i).setPreferredWidth(width * 20 / 100);
167 } else {
168 columnModel.getColumn(i).setPreferredWidth(width * 40 / 100);
171 } else if (columns.length == 5) {
172 for (int i = 0; i < columns.length; i++) {
173 String col = columns[i];
174 sorter.setColumnComparator(i, null);
175 if (col.equals(CommitTableModel.COLUMN_NAME_NAME)) {
176 sorter.setColumnComparator(i, new FileNameComparator());
177 columnModel.getColumn(i).setPreferredWidth(width * 25 / 100);
178 } else if (col.equals(CommitTableModel.COLUMN_NAME_STATUS)) {
179 sorter.setColumnComparator(i, new StatusComparator());
180 sorter.setSortingStatus(i, TableSorter.ASCENDING);
181 columnModel.getColumn(i).setPreferredWidth(width * 15 / 100);
182 } else if (col.equals(CommitTableModel.COLUMN_NAME_ACTION)) {
183 columnModel.getColumn(i).setPreferredWidth(width * 15 / 100);
184 } else {
185 columnModel.getColumn(i).setPreferredWidth(width * 30 / 100);
191 private void setSortingStatus() {
192 for (int i = 0; i < sortByColumns.length; i++) {
193 String sortByColumn = sortByColumns[i];
194 for (int j = 0; j < columns.length; j++) {
195 String column = columns[j];
196 if (column.equals(sortByColumn)) {
197 sorter.setSortingStatus(j, column.equals(sortByColumn) ? TableSorter.ASCENDING : TableSorter.NOT_SORTED);
198 break;
204 public TableSorter getSorter() {
205 return sorter;
208 public void ancestorMoved(AncestorEvent event) {
211 public void ancestorRemoved(AncestorEvent event) {
214 void setColumns(String[] cols) {
215 if (Arrays.equals(columns, cols)) {
216 return;
218 columns = cols;
219 tableModel.setColumns(cols);
220 setDefaultColumnSizes();
223 public void setNodes(GitFileNode[] nodes) {
224 tableModel.setNodes(nodes);
229 * @return Map&lt;GitFileNode, CommitOptions&gt;
231 public Map<GitFileNode, CommitOptions> getCommitFiles() {
232 return tableModel.getCommitFiles();
236 * @return table in a scrollpane
238 public JComponent getComponent() {
239 return component;
242 void dataChanged() {
243 int idx = table.getSelectedRow();
244 tableModel.fireTableDataChanged();
245 if (idx != -1) {
246 table.getSelectionModel().addSelectionInterval(idx, idx);
250 TableModel getTableModel() {
251 return tableModel;
254 public void tableChanged(TableModelEvent e) {
255 // change in commit options may alter name rendering (strikethrough)
256 table.repaint();
259 public void setRootFile(String repositoryPath, String rootLocalPath) {
260 tableModel.setRootFile(repositoryPath, rootLocalPath);
263 private class CommitOptionsCellEditor extends DefaultCellEditor {
265 private final Object[] dirAddOptions = new Object[]{
266 CommitOptions.COMMIT,
267 CommitOptions.EXCLUDE
269 private final Object[] addOptions = new Object[]{
270 CommitOptions.COMMIT,
271 CommitOptions.EXCLUDE
273 private final Object[] commitOptions = new Object[]{
274 CommitOptions.COMMIT,
275 CommitOptions.EXCLUDE
277 private final Object[] removeOptions = new Object[]{
278 CommitOptions.COMMIT_REMOVE,
279 CommitOptions.EXCLUDE
282 public CommitOptionsCellEditor() {
283 super(new JComboBox());
286 @Override
287 public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) {
288 StatusInfo info = tableModel.getNode(sorter.modelIndex(row)).getInformation();
289 int fileStatus = info.getStatus();
290 JComboBox combo = (JComboBox) editorComponent;
291 if (fileStatus == StatusInfo.STATUS_VERSIONED_DELETEDLOCALLY || fileStatus == StatusInfo.STATUS_VERSIONED_REMOVEDLOCALLY) {
292 combo.setModel(new DefaultComboBoxModel(removeOptions));
293 } else if ((fileStatus & StatusInfo.STATUS_IN_REPOSITORY) == 0) {
294 if (info.isDirectory()) {
295 combo.setModel(new DefaultComboBoxModel(dirAddOptions));
296 } else {
297 combo.setModel(new DefaultComboBoxModel(addOptions));
299 } else {
300 combo.setModel(new DefaultComboBoxModel(commitOptions));
302 return super.getTableCellEditorComponent(table, value, isSelected, row, column);
306 private class CommitStringsCellRenderer extends DefaultTableCellRenderer {
308 private FilePathCellRenderer pathRenderer = new FilePathCellRenderer();
310 @Override
311 public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
312 int col = table.convertColumnIndexToModel(column);
313 if (columns[col].equals(CommitTableModel.COLUMN_NAME_NAME)) {
314 TableSorter sorter = (TableSorter) table.getModel();
315 CommitTableModel model = (CommitTableModel) sorter.getTableModel();
316 GitFileNode node = model.getNode(sorter.modelIndex(row));
317 CommitOptions options = model.getOptions(sorter.modelIndex(row));
318 if (!isSelected) {
319 value = "<html>" + HtmlFormatter.getInstance().annotateNameHtml( // NOI18N
320 node.getFile().getName(), node.getInformation(), null);
322 if (options == CommitOptions.EXCLUDE) {
323 value = "<html><s>" + value + "</s></html>";
325 return super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
326 } else if (columns[col].equals(CommitTableModel.COLUMN_NAME_PATH)) {
327 return pathRenderer.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
328 } else {
329 return super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
334 private class StatusComparator extends GitUtils.ByImportanceComparator {
336 public int compare(Object o1, Object o2) {
337 Integer row1 = (Integer) o1;
338 Integer row2 = (Integer) o2;
339 return super.compare(tableModel.getNode(row1.intValue()).getInformation(),
340 tableModel.getNode(row2.intValue()).getInformation());
344 private class FileNameComparator implements Comparator {
346 public int compare(Object o1, Object o2) {
347 Integer row1 = (Integer) o1;
348 Integer row2 = (Integer) o2;
349 return tableModel.getNode(row1.intValue()).getName().compareToIgnoreCase(
350 tableModel.getNode(row2.intValue()).getName());