Renamed constants, local variables and member variables using "hg" to "git".
[nbgit.git] / src / org / netbeans / modules / git / ui / commit / CommitTableModel.java
blob82c7f545b716a4846df67b4cd6f21295f0e97920
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.io.File;
45 import java.util.Arrays;
46 import java.util.HashMap;
47 import java.util.Map;
48 import java.util.ResourceBundle;
49 import javax.swing.table.AbstractTableModel;
50 import org.netbeans.modules.git.FileInformation;
51 import org.netbeans.modules.git.GitFileNode;
52 import org.netbeans.modules.git.GitModuleConfig;
53 import org.netbeans.modules.git.util.GitUtils;
54 import org.openide.util.NbBundle;
56 /**
57 * Table model for the Commit dialog table.
59 * @author Maros Sandor
61 public class CommitTableModel extends AbstractTableModel {
63 public static final String COLUMN_NAME_NAME = "name"; // NOI18N
64 public static final String COLUMN_NAME_STATUS = "status"; // NOI18N
65 public static final String COLUMN_NAME_ACTION = "action"; // NOI18N
66 public static final String COLUMN_NAME_PATH = "path"; // NOI18N
67 public static final String COLUMN_NAME_BRANCH = "branch"; // NOI18N
69 private class RootFile {
70 String repositoryPath;
71 String rootLocalPath;
73 //private Set<SVNUrl> repositoryRoots;
74 private RootFile rootFile;
76 /**
77 * Defines labels for Versioning view table columns.
78 */
79 private static final Map<String, String[]> columnLabels = new HashMap<String, String[]>(4);
82 ResourceBundle loc = NbBundle.getBundle(CommitTableModel.class);
83 columnLabels.put(COLUMN_NAME_NAME, new String [] {
84 loc.getString("CTL_CommitTable_Column_File"), // NOI18N
85 loc.getString("CTL_CommitTable_Column_File")}); // NOI18N
86 columnLabels.put(COLUMN_NAME_BRANCH, new String [] {
87 loc.getString("CTL_CommitTable_Column_Branch"), // NOI18N
88 loc.getString("CTL_CommitTable_Column_Branch")}); // NOI18N
89 columnLabels.put(COLUMN_NAME_STATUS, new String [] {
90 loc.getString("CTL_CommitTable_Column_Status"), // NOI18N
91 loc.getString("CTL_CommitTable_Column_Status")}); // NOI18N
92 columnLabels.put(COLUMN_NAME_ACTION, new String [] {
93 loc.getString("CTL_CommitTable_Column_Action"), // NOI18N
94 loc.getString("CTL_CommitTable_Column_Action")}); // NOI18N
95 columnLabels.put(COLUMN_NAME_PATH, new String [] {
96 loc.getString("CTL_CommitTable_Column_Folder"), // NOI18N
97 loc.getString("CTL_CommitTable_Column_Folder")}); // NOI18N
100 private CommitOptions [] commitOptions;
101 private GitFileNode [] nodes;
103 private String [] columns;
106 * Create stable with name, status, action and path columns
107 * and empty nodes {@link #setNodes model}.
109 public CommitTableModel(String[] columns) {
110 setColumns(columns);
111 setNodes(new GitFileNode[0]);
114 void setNodes(GitFileNode [] nodes) {
115 this.nodes = nodes;
116 defaultCommitOptions();
117 fireTableDataChanged();
120 void setColumns(String [] cols) {
121 if (Arrays.equals(cols, columns)) return;
122 columns = cols;
123 fireTableStructureChanged();
127 * @return Map&lt;GitFileNode, CommitOptions>
129 public Map<GitFileNode, CommitOptions> getCommitFiles() {
130 Map<GitFileNode, CommitOptions> ret = new HashMap<GitFileNode, CommitOptions>(nodes.length);
131 for (int i = 0; i < nodes.length; i++) {
132 ret.put(nodes[i], commitOptions[i]);
134 return ret;
137 @Override
138 public String getColumnName(int column) {
139 return columnLabels.get(columns[column])[0];
142 public int getColumnCount() {
143 return columns.length;
146 public int getRowCount() {
147 return nodes.length;
150 @Override
151 public Class getColumnClass(int columnIndex) {
152 String col = columns[columnIndex];
153 if (col.equals(COLUMN_NAME_ACTION)) {
154 return CommitOptions.class;
156 return String.class;
159 @Override
160 public boolean isCellEditable(int rowIndex, int columnIndex) {
161 String col = columns[columnIndex];
162 return col.equals(COLUMN_NAME_ACTION);
165 public Object getValueAt(int rowIndex, int columnIndex) {
166 GitFileNode node;
167 String col = columns[columnIndex];
168 if (col.equals(COLUMN_NAME_NAME)) {
169 return nodes[rowIndex].getName();
170 // TODO deal with branch?
171 //} else if (col.equals(COLUMN_NAME_BRANCH)) {
172 // String branch = GitUtils.getCopy(nodes[rowIndex].getFile());
173 // return branch == null ? "" : branch; // NOI18N
174 } else if (col.equals(COLUMN_NAME_STATUS)) {
175 node = nodes[rowIndex];
176 FileInformation finfo = node.getInformation();
177 //TODO what should we do with this?
178 //finfo.getEntry(node.getFile()); // HACK returned value is not interesting, point is side effect, it loads ISVNStatus structure
179 return finfo.getStatusText();
180 } else if (col.equals(COLUMN_NAME_ACTION)) {
181 return commitOptions[rowIndex];
182 } else if (col.equals(COLUMN_NAME_PATH)) {
183 String shortPath = null;
184 // XXX this is a mess
185 if(rootFile != null) {
186 // must convert from native separators to slashes
187 String relativePath = nodes[rowIndex].getFile().getAbsolutePath().substring(rootFile.rootLocalPath.length());
188 shortPath = rootFile.repositoryPath + relativePath.replace(File.separatorChar, '/');
189 } else {
190 shortPath = GitUtils.getRelativePath(nodes[rowIndex].getFile());
191 if (shortPath == null) {
192 shortPath = org.openide.util.NbBundle.getMessage(CommitTableModel.class, "CTL_CommitForm_NotInRepository"); // NOI18N
195 return shortPath;
197 throw new IllegalArgumentException("Column index out of range: " + columnIndex); // NOI18N
200 @Override
201 public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
202 String col = columns[columnIndex];
203 if (col.equals(COLUMN_NAME_ACTION)) {
204 commitOptions[rowIndex] = (CommitOptions) aValue;
205 fireTableCellUpdated(rowIndex, columnIndex);
206 } else {
207 throw new IllegalArgumentException("Column index out of range: " + columnIndex); // NOI18N
211 private void defaultCommitOptions() {
212 boolean excludeNew = System.getProperty("netbeans.git.excludeNewFiles") != null; // NOI18N
213 commitOptions = new CommitOptions[nodes.length];
214 for (int i = 0; i < nodes.length; i++) {
215 GitFileNode node = nodes[i];
216 File file = node.getFile();
217 if (GitModuleConfig.getDefault().isExcludedFromCommit(file.getAbsolutePath())) {
218 commitOptions[i] = CommitOptions.EXCLUDE;
219 } else {
220 switch (node.getInformation().getStatus()) {
221 case FileInformation.STATUS_VERSIONED_DELETEDLOCALLY:
222 case FileInformation.STATUS_VERSIONED_REMOVEDLOCALLY:
223 commitOptions[i] = CommitOptions.COMMIT_REMOVE;
224 break;
225 default:
226 commitOptions[i] = CommitOptions.COMMIT;
232 public GitFileNode getNode(int row) {
233 return nodes[row];
236 public CommitOptions getOptions(int row) {
237 return commitOptions[row];
240 void setRootFile(String repositoryPath, String rootLocalPath) {
241 rootFile = new RootFile();
242 rootFile.repositoryPath = repositoryPath;
243 rootFile.rootLocalPath = rootLocalPath;