Adding Git source, NetBeans project files, GPL v2 LICENSE, ant build file.
[nbgit.git] / src / org / netbeans / modules / git / ui / commit / CommitTable.java
blob6aaefc944021b86dd8086491136965bb02fa2a1d
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.netbeans.modules.git.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.netbeans.modules.git.FileInformation;
63 import org.netbeans.modules.git.Git;
64 import org.netbeans.modules.git.GitFileNode;
65 import org.netbeans.modules.git.util.GitUtils;
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
84 public static String [] IMPORT_COLUMNS = new String [] {
85 CommitTableModel.COLUMN_NAME_NAME,
86 CommitTableModel.COLUMN_NAME_ACTION,
87 CommitTableModel.COLUMN_NAME_PATH
90 private CommitTableModel tableModel;
91 private JTable table;
92 private JComponent component;
94 private TableSorter sorter;
95 private String[] columns;
96 private String[] sortByColumns;
99 public CommitTable(JLabel label, String[] columns, String[] sortByColumns) {
100 init(label, columns, null);
101 this.sortByColumns = sortByColumns;
102 setSortingStatus();
105 public CommitTable(JLabel label, String[] columns, TableSorter sorter) {
106 init(label, columns, sorter);
109 private void init(JLabel label, String[] columns, TableSorter sorter) {
110 tableModel = new CommitTableModel(columns);
111 tableModel.addTableModelListener(this);
112 if(sorter == null) {
113 sorter = new TableSorter(tableModel);
115 this.sorter = sorter;
116 table = new JTable(this.sorter);
117 table.getTableHeader().setReorderingAllowed(false);
118 table.setDefaultRenderer(String.class, new CommitStringsCellRenderer());
119 table.setDefaultEditor(CommitOptions.class, new CommitOptionsCellEditor());
120 table.getTableHeader().setReorderingAllowed(true);
121 this.sorter.setTableHeader(table.getTableHeader());
122 table.setRowHeight(table.getRowHeight() * 6 / 5);
123 table.addAncestorListener(this);
124 component = new JScrollPane(table, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
125 label.setLabelFor(table);
126 table.getAccessibleContext().setAccessibleDescription(NbBundle.getMessage(CommitTable.class, "ACSD_CommitTable")); // NOI18N
127 setColumns(columns);
130 public void ancestorAdded(AncestorEvent event) {
131 setDefaultColumnSizes();
135 * Sets sizes of Commit table columns, kind of hardcoded.
137 private void setDefaultColumnSizes() {
138 int width = table.getWidth();
139 TableColumnModel columnModel = table.getColumnModel();
140 if (columns == null || columnModel == null) return; // unsure when this methed will be called (component realization)
141 if (columnModel.getColumnCount() != columns.length) 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)) return;
216 columns = cols;
217 tableModel.setColumns(cols);
218 setDefaultColumnSizes();
221 public void setNodes(GitFileNode[] nodes) {
222 tableModel.setNodes(nodes);
226 * @return Map&lt;HgFileNode, CommitOptions>
228 public Map<GitFileNode, CommitOptions> getCommitFiles() {
229 return tableModel.getCommitFiles();
233 * @return table in a scrollpane
235 public JComponent getComponent() {
236 return component;
239 void dataChanged() {
240 int idx = table.getSelectedRow();
241 tableModel.fireTableDataChanged();
242 if (idx != -1) table.getSelectionModel().addSelectionInterval(idx, idx);
245 TableModel getTableModel() {
246 return tableModel;
249 public void tableChanged(TableModelEvent e) {
250 // change in commit options may alter name rendering (strikethrough)
251 table.repaint();
254 public void setRootFile(String repositoryPath, String rootLocalPath) {
255 tableModel.setRootFile(repositoryPath, rootLocalPath);
258 private class CommitOptionsCellEditor extends DefaultCellEditor {
260 private final Object[] dirAddOptions = new Object [] {
261 CommitOptions.COMMIT,
262 CommitOptions.EXCLUDE
265 private final Object[] addOptions = new Object [] {
266 CommitOptions.COMMIT,
267 CommitOptions.EXCLUDE
269 private final Object[] commitOptions = new Object [] {
270 CommitOptions.COMMIT,
271 CommitOptions.EXCLUDE
274 private final Object[] removeOptions = new Object [] {
275 CommitOptions.COMMIT_REMOVE,
276 CommitOptions.EXCLUDE
279 public CommitOptionsCellEditor() {
280 super(new JComboBox());
283 @Override
284 public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) {
285 FileInformation info = tableModel.getNode(sorter.modelIndex(row)).getInformation();
286 int fileStatus = info.getStatus();
287 JComboBox combo = (JComboBox) editorComponent;
288 if (fileStatus == FileInformation.STATUS_VERSIONED_DELETEDLOCALLY || fileStatus == FileInformation.STATUS_VERSIONED_REMOVEDLOCALLY) {
289 combo.setModel(new DefaultComboBoxModel(removeOptions));
290 } else if ((fileStatus & FileInformation.STATUS_IN_REPOSITORY) == 0) {
291 if (info.isDirectory()) {
292 combo.setModel(new DefaultComboBoxModel(dirAddOptions));
293 } else {
294 combo.setModel(new DefaultComboBoxModel(addOptions));
296 } else {
297 combo.setModel(new DefaultComboBoxModel(commitOptions));
299 return super.getTableCellEditorComponent(table, value, isSelected, row, column);
303 private class CommitStringsCellRenderer extends DefaultTableCellRenderer {
305 private FilePathCellRenderer pathRenderer = new FilePathCellRenderer();
307 @Override
308 public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
309 int col = table.convertColumnIndexToModel(column);
310 if (columns[col] == CommitTableModel.COLUMN_NAME_NAME) {
311 TableSorter sorter = (TableSorter) table.getModel();
312 CommitTableModel model = (CommitTableModel) sorter.getTableModel();
313 GitFileNode node = model.getNode(sorter.modelIndex(row));
314 CommitOptions options = model.getOptions(sorter.modelIndex(row));
315 if (!isSelected) {
316 value = "<html>" + Git.getInstance().getGitAnnotator().annotateNameHtml( // NOI18N
317 node.getFile().getName(), node.getInformation(), null);
319 if (options == CommitOptions.EXCLUDE) {
320 value = "<html><s>" + value + "</s></html>"; // NOI18N
322 return super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
323 } else if (columns[col] == CommitTableModel.COLUMN_NAME_PATH) {
324 return pathRenderer.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
325 } else {
326 return super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
331 private class StatusComparator extends GitUtils.ByImportanceComparator {
332 public int compare(Object o1, Object o2) {
333 Integer row1 = (Integer) o1;
334 Integer row2 = (Integer) o2;
335 return super.compare(tableModel.getNode(row1.intValue()).getInformation(),
336 tableModel.getNode(row2.intValue()).getInformation());
340 private class FileNameComparator implements Comparator {
341 public int compare(Object o1, Object o2) {
342 Integer row1 = (Integer) o1;
343 Integer row2 = (Integer) o2;
344 return tableModel.getNode(row1.intValue()).getName().compareToIgnoreCase(
345 tableModel.getNode(row2.intValue()).getName());