Removed unused ServeAction.
[nbgit.git] / src / org / netbeans / modules / git / ui / commit / CommitTable.java
blobf0e1da1f9aa3e69c090b7bdd40a33595810ec5d2
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);
227 * @return Map&lt;GitFileNode, CommitOptions&gt;
229 public Map<GitFileNode, CommitOptions> getCommitFiles() {
230 return tableModel.getCommitFiles();
234 * @return table in a scrollpane
236 public JComponent getComponent() {
237 return component;
240 void dataChanged() {
241 int idx = table.getSelectedRow();
242 tableModel.fireTableDataChanged();
243 if (idx != -1) table.getSelectionModel().addSelectionInterval(idx, idx);
246 TableModel getTableModel() {
247 return tableModel;
250 public void tableChanged(TableModelEvent e) {
251 // change in commit options may alter name rendering (strikethrough)
252 table.repaint();
255 public void setRootFile(String repositoryPath, String rootLocalPath) {
256 tableModel.setRootFile(repositoryPath, rootLocalPath);
259 private class CommitOptionsCellEditor extends DefaultCellEditor {
261 private final Object[] dirAddOptions = new Object [] {
262 CommitOptions.COMMIT,
263 CommitOptions.EXCLUDE
266 private final Object[] addOptions = new Object [] {
267 CommitOptions.COMMIT,
268 CommitOptions.EXCLUDE
270 private final Object[] commitOptions = new Object [] {
271 CommitOptions.COMMIT,
272 CommitOptions.EXCLUDE
275 private final Object[] removeOptions = new Object [] {
276 CommitOptions.COMMIT_REMOVE,
277 CommitOptions.EXCLUDE
280 public CommitOptionsCellEditor() {
281 super(new JComboBox());
284 @Override
285 public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) {
286 FileInformation info = tableModel.getNode(sorter.modelIndex(row)).getInformation();
287 int fileStatus = info.getStatus();
288 JComboBox combo = (JComboBox) editorComponent;
289 if (fileStatus == FileInformation.STATUS_VERSIONED_DELETEDLOCALLY || fileStatus == FileInformation.STATUS_VERSIONED_REMOVEDLOCALLY) {
290 combo.setModel(new DefaultComboBoxModel(removeOptions));
291 } else if ((fileStatus & FileInformation.STATUS_IN_REPOSITORY) == 0) {
292 if (info.isDirectory()) {
293 combo.setModel(new DefaultComboBoxModel(dirAddOptions));
294 } else {
295 combo.setModel(new DefaultComboBoxModel(addOptions));
297 } else {
298 combo.setModel(new DefaultComboBoxModel(commitOptions));
300 return super.getTableCellEditorComponent(table, value, isSelected, row, column);
304 private class CommitStringsCellRenderer extends DefaultTableCellRenderer {
306 private FilePathCellRenderer pathRenderer = new FilePathCellRenderer();
308 @Override
309 public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
310 int col = table.convertColumnIndexToModel(column);
311 if (columns[col] == CommitTableModel.COLUMN_NAME_NAME) {
312 TableSorter sorter = (TableSorter) table.getModel();
313 CommitTableModel model = (CommitTableModel) sorter.getTableModel();
314 GitFileNode node = model.getNode(sorter.modelIndex(row));
315 CommitOptions options = model.getOptions(sorter.modelIndex(row));
316 if (!isSelected) {
317 value = "<html>" + Git.getInstance().getGitAnnotator().annotateNameHtml( // NOI18N
318 node.getFile().getName(), node.getInformation(), null);
320 if (options == CommitOptions.EXCLUDE) {
321 value = "<html><s>" + value + "</s></html>"; // NOI18N
323 return super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
324 } else if (columns[col] == CommitTableModel.COLUMN_NAME_PATH) {
325 return pathRenderer.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
326 } else {
327 return super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
332 private class StatusComparator extends GitUtils.ByImportanceComparator {
333 public int compare(Object o1, Object o2) {
334 Integer row1 = (Integer) o1;
335 Integer row2 = (Integer) o2;
336 return super.compare(tableModel.getNode(row1.intValue()).getInformation(),
337 tableModel.getNode(row2.intValue()).getInformation());
341 private class FileNameComparator implements Comparator {
342 public int compare(Object o1, Object o2) {
343 Integer row1 = (Integer) o1;
344 Integer row2 = (Integer) o2;
345 return tableModel.getNode(row1.intValue()).getName().compareToIgnoreCase(
346 tableModel.getNode(row2.intValue()).getName());