Cleanup code whitespace
[nbgit.git] / src / org / nbgit / ui / commit / CommitTable.java
blobf23bb04a67c1508d28f1227affd21e793fc5bd2a
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)
97 init(label, columns, null);
98 this.sortByColumns = sortByColumns;
99 setSortingStatus();
102 public CommitTable(JLabel label, String[] columns, TableSorter sorter)
104 init(label, columns, sorter);
107 private void init(JLabel label, String[] columns, TableSorter sorter)
109 tableModel = new CommitTableModel(columns);
110 tableModel.addTableModelListener(this);
111 if (sorter == null)
112 sorter = new TableSorter(tableModel);
113 this.sorter = sorter;
114 table = new JTable(this.sorter);
115 table.getTableHeader().setReorderingAllowed(false);
116 table.setDefaultRenderer(String.class, new CommitStringsCellRenderer());
117 table.setDefaultEditor(CommitOptions.class, new CommitOptionsCellEditor());
118 table.getTableHeader().setReorderingAllowed(true);
119 this.sorter.setTableHeader(table.getTableHeader());
120 table.setRowHeight(table.getRowHeight() * 6 / 5);
121 table.addAncestorListener(this);
122 component = new JScrollPane(table, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
123 label.setLabelFor(table);
124 table.getAccessibleContext().setAccessibleDescription(NbBundle.getMessage(CommitTable.class, "ACSD_CommitTable")); // NOI18N
125 setColumns(columns);
128 public void ancestorAdded(AncestorEvent event)
130 setDefaultColumnSizes();
134 * Sets sizes of Commit table columns, kind of hardcoded.
136 private void setDefaultColumnSizes()
138 int width = table.getWidth();
139 TableColumnModel columnModel = table.getColumnModel();
140 if (columns == null || columnModel == null)
141 return; // unsure when this methed will be called (component realization)
142 if (columnModel.getColumnCount() != columns.length)
143 return;
144 if (columns.length == 3)
145 for (int i = 0; i < columns.length; i++) {
146 String col = columns[i];
147 sorter.setColumnComparator(i, null);
148 if (col.equals(CommitTableModel.COLUMN_NAME_NAME)) {
149 sorter.setColumnComparator(i, new FileNameComparator());
150 columnModel.getColumn(i).setPreferredWidth(width * 30 / 100);
151 } else if (col.equals(CommitTableModel.COLUMN_NAME_ACTION))
152 columnModel.getColumn(i).setPreferredWidth(width * 15 / 100);
153 else
154 columnModel.getColumn(i).setPreferredWidth(width * 40 / 100);
156 else if (columns.length == 4)
157 for (int i = 0; i < columns.length; i++) {
158 String col = columns[i];
159 sorter.setColumnComparator(i, null);
160 if (col.equals(CommitTableModel.COLUMN_NAME_NAME)) {
161 sorter.setColumnComparator(i, new FileNameComparator());
162 columnModel.getColumn(i).setPreferredWidth(width * 25 / 100);
163 } else if (col.equals(CommitTableModel.COLUMN_NAME_STATUS)) {
164 sorter.setColumnComparator(i, new StatusComparator());
165 columnModel.getColumn(i).setPreferredWidth(width * 15 / 100);
166 } else if (col.equals(CommitTableModel.COLUMN_NAME_ACTION))
167 columnModel.getColumn(i).setPreferredWidth(width * 20 / 100);
168 else
169 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);
189 private void setSortingStatus()
191 for (int i = 0; i < sortByColumns.length; i++) {
192 String sortByColumn = sortByColumns[i];
193 for (int j = 0; j < columns.length; j++) {
194 String column = columns[j];
195 if (column.equals(sortByColumn)) {
196 sorter.setSortingStatus(j, column.equals(sortByColumn) ? TableSorter.ASCENDING : TableSorter.NOT_SORTED);
197 break;
203 public TableSorter getSorter()
205 return sorter;
208 public void ancestorMoved(AncestorEvent event)
212 public void ancestorRemoved(AncestorEvent event)
216 void setColumns(String[] cols)
218 if (Arrays.equals(columns, cols))
219 return;
220 columns = cols;
221 tableModel.setColumns(cols);
222 setDefaultColumnSizes();
225 public void setNodes(GitFileNode[] nodes)
227 tableModel.setNodes(nodes);
232 * @return Map&lt;GitFileNode, CommitOptions&gt;
234 public Map<GitFileNode, CommitOptions> getCommitFiles()
236 return tableModel.getCommitFiles();
240 * @return table in a scrollpane
242 public JComponent getComponent()
244 return component;
247 void dataChanged()
249 int idx = table.getSelectedRow();
250 tableModel.fireTableDataChanged();
251 if (idx != -1)
252 table.getSelectionModel().addSelectionInterval(idx, idx);
255 TableModel getTableModel()
257 return tableModel;
260 public void tableChanged(TableModelEvent e)
262 // change in commit options may alter name rendering (strikethrough)
263 table.repaint();
266 public void setRootFile(String repositoryPath, String rootLocalPath)
268 tableModel.setRootFile(repositoryPath, rootLocalPath);
271 private class CommitOptionsCellEditor extends DefaultCellEditor {
273 private final Object[] dirAddOptions = new Object[]{
274 CommitOptions.COMMIT,
275 CommitOptions.EXCLUDE
277 private final Object[] addOptions = new Object[]{
278 CommitOptions.COMMIT,
279 CommitOptions.EXCLUDE
281 private final Object[] commitOptions = new Object[]{
282 CommitOptions.COMMIT,
283 CommitOptions.EXCLUDE
285 private final Object[] removeOptions = new Object[]{
286 CommitOptions.COMMIT_REMOVE,
287 CommitOptions.EXCLUDE
290 public CommitOptionsCellEditor()
292 super(new JComboBox());
295 @Override
296 public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column)
298 StatusInfo info = tableModel.getNode(sorter.modelIndex(row)).getInformation();
299 int fileStatus = info.getStatus();
300 JComboBox combo = (JComboBox) editorComponent;
301 if (fileStatus == StatusInfo.STATUS_VERSIONED_DELETEDLOCALLY || fileStatus == StatusInfo.STATUS_VERSIONED_REMOVEDLOCALLY)
302 combo.setModel(new DefaultComboBoxModel(removeOptions));
303 else if ((fileStatus & StatusInfo.STATUS_IN_REPOSITORY) == 0)
304 if (info.isDirectory())
305 combo.setModel(new DefaultComboBoxModel(dirAddOptions));
306 else
307 combo.setModel(new DefaultComboBoxModel(addOptions));
308 else
309 combo.setModel(new DefaultComboBoxModel(commitOptions));
310 return super.getTableCellEditorComponent(table, value, isSelected, row, column);
315 private class CommitStringsCellRenderer extends DefaultTableCellRenderer {
317 private FilePathCellRenderer pathRenderer = new FilePathCellRenderer();
319 @Override
320 public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column)
322 int col = table.convertColumnIndexToModel(column);
323 if (columns[col] == CommitTableModel.COLUMN_NAME_NAME) {
324 TableSorter sorter = (TableSorter) table.getModel();
325 CommitTableModel model = (CommitTableModel) sorter.getTableModel();
326 GitFileNode node = model.getNode(sorter.modelIndex(row));
327 CommitOptions options = model.getOptions(sorter.modelIndex(row));
328 if (!isSelected)
329 value = "<html>" + HtmlFormatter.getInstance().annotateNameHtml( // NOI18N
330 node.getFile().getName(), node.getInformation(), null);
331 if (options == CommitOptions.EXCLUDE)
332 value = "<html><s>" + value + "</s></html>";
333 return super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
334 } else if (columns[col] == CommitTableModel.COLUMN_NAME_PATH)
335 return pathRenderer.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
336 else
337 return super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
342 private class StatusComparator extends GitUtils.ByImportanceComparator {
344 public int compare(Object o1, Object o2)
346 Integer row1 = (Integer) o1;
347 Integer row2 = (Integer) o2;
348 return super.compare(tableModel.getNode(row1.intValue()).getInformation(),
349 tableModel.getNode(row2.intValue()).getInformation());
354 private class FileNameComparator implements Comparator {
356 public int compare(Object o1, Object o2)
358 Integer row1 = (Integer) o1;
359 Integer row2 = (Integer) o2;
360 return tableModel.getNode(row1.intValue()).getName().compareToIgnoreCase(
361 tableModel.getNode(row2.intValue()).getName());