001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.modules.subversion.ui.commit;
043:
044: import org.openide.util.NbBundle;
045: import org.netbeans.modules.subversion.SvnFileNode;
046: import org.netbeans.modules.subversion.FileInformation;
047: import org.netbeans.modules.subversion.util.SvnUtils;
048: import org.netbeans.modules.subversion.SvnModuleConfig;
049: import javax.swing.table.AbstractTableModel;
050: import java.util.*;
051: import java.io.File;
052: import org.netbeans.modules.subversion.Subversion;
053: import org.netbeans.modules.subversion.client.SvnClientExceptionHandler;
054: import org.netbeans.modules.versioning.util.Utils;
055: import org.tigris.subversion.svnclientadapter.SVNClientException;
056: import org.tigris.subversion.svnclientadapter.SVNUrl;
057:
058: /**
059: * Table model for the Commit dialog table.
060: *
061: * @author Maros Sandor
062: */
063: public class CommitTableModel extends AbstractTableModel {
064:
065: public static final String COLUMN_NAME_NAME = "name"; // NOI18N
066: public static final String COLUMN_NAME_STATUS = "status"; // NOI18N
067: public static final String COLUMN_NAME_ACTION = "action"; // NOI18N
068: public static final String COLUMN_NAME_PATH = "path"; // NOI18N
069: public static final String COLUMN_NAME_BRANCH = "branch"; // NOI18N
070:
071: private class RootFile {
072: String repositoryPath;
073: String rootLocalPath;
074: }
075:
076: private Set<SVNUrl> repositoryRoots;
077: private RootFile rootFile;
078:
079: /**
080: * Defines labels for Versioning view table columns.
081: */
082: private static final Map<String, String[]> columnLabels = new HashMap<String, String[]>(
083: 4);
084:
085: {
086: ResourceBundle loc = NbBundle.getBundle(CommitTableModel.class);
087: columnLabels.put(COLUMN_NAME_NAME, new String[] {
088: loc.getString("CTL_CommitTable_Column_File"),
089: loc.getString("CTL_CommitTable_Column_File") });
090: columnLabels.put(COLUMN_NAME_BRANCH, new String[] {
091: loc.getString("CTL_CommitTable_Column_Branch"),
092: loc.getString("CTL_CommitTable_Column_Branch") });
093: columnLabels.put(COLUMN_NAME_STATUS, new String[] {
094: loc.getString("CTL_CommitTable_Column_Status"),
095: loc.getString("CTL_CommitTable_Column_Status") });
096: columnLabels.put(COLUMN_NAME_ACTION, new String[] {
097: loc.getString("CTL_CommitTable_Column_Action"),
098: loc.getString("CTL_CommitTable_Column_Action") });
099: columnLabels.put(COLUMN_NAME_PATH, new String[] {
100: loc.getString("CTL_CommitTable_Column_Folder"),
101: loc.getString("CTL_CommitTable_Column_Folder") });
102: }
103:
104: private CommitOptions[] commitOptions;
105: private SvnFileNode[] nodes;
106:
107: private String[] columns;
108:
109: /**
110: * Create stable with name, status, action and path columns
111: * and empty nodes {@link #setNodes model}.
112: */
113: public CommitTableModel(String[] columns) {
114: setColumns(columns);
115: setNodes(new SvnFileNode[0]);
116: }
117:
118: void setNodes(SvnFileNode[] nodes) {
119: this .nodes = nodes;
120: defaultCommitOptions();
121: fireTableDataChanged();
122: }
123:
124: void setColumns(String[] cols) {
125: if (Arrays.equals(cols, columns))
126: return;
127: columns = cols;
128: fireTableStructureChanged();
129: }
130:
131: /**
132: * @return Map<SvnFileNode, CommitOptions>
133: */
134: public Map<SvnFileNode, CommitOptions> getCommitFiles() {
135: Map<SvnFileNode, CommitOptions> ret = new HashMap<SvnFileNode, CommitOptions>(
136: nodes.length);
137: for (int i = 0; i < nodes.length; i++) {
138: ret.put(nodes[i], commitOptions[i]);
139: }
140: return ret;
141: }
142:
143: public String getColumnName(int column) {
144: return columnLabels.get(columns[column])[0];
145: }
146:
147: public int getColumnCount() {
148: return columns.length;
149: }
150:
151: public int getRowCount() {
152: return nodes.length;
153: }
154:
155: public Class getColumnClass(int columnIndex) {
156: String col = columns[columnIndex];
157: if (col.equals(COLUMN_NAME_ACTION)) {
158: return CommitOptions.class;
159: }
160: return String.class;
161: }
162:
163: public boolean isCellEditable(int rowIndex, int columnIndex) {
164: String col = columns[columnIndex];
165: return col.equals(COLUMN_NAME_ACTION);
166: }
167:
168: public Object getValueAt(int rowIndex, int columnIndex) {
169: SvnFileNode node;
170: String col = columns[columnIndex];
171: if (col.equals(COLUMN_NAME_NAME)) {
172: return nodes[rowIndex].getName();
173: } else if (col.equals(COLUMN_NAME_BRANCH)) {
174: String branch = SvnUtils.getCopy(nodes[rowIndex].getFile());
175: return branch == null ? "" : branch; // NOI18N
176: } else if (col.equals(COLUMN_NAME_STATUS)) {
177: node = nodes[rowIndex];
178: FileInformation finfo = node.getInformation();
179: finfo.getEntry(node.getFile()); // HACK returned value is not interesting, point is side effect, it loads ISVNStatus structure
180: return finfo.getStatusText();
181: } else if (col.equals(COLUMN_NAME_ACTION)) {
182: return commitOptions[rowIndex];
183: } else if (col.equals(COLUMN_NAME_PATH)) {
184: String shortPath = null;
185: // XXX this is a mess
186: if (rootFile != null) {
187: // must convert from native separators to slashes
188: String relativePath = nodes[rowIndex].getFile()
189: .getAbsolutePath().substring(
190: rootFile.rootLocalPath.length());
191: shortPath = rootFile.repositoryPath
192: + relativePath.replace(File.separatorChar, '/');
193: } else {
194: Set<SVNUrl> url = getRepositoryRoots();
195: try {
196: for (Iterator<SVNUrl> it = url.iterator(); it
197: .hasNext();) {
198: SVNUrl nextUrl = it.next();
199: shortPath = SvnUtils.getRelativePath(nextUrl,
200: nodes[rowIndex].getFile());
201: }
202: if (shortPath == null) {
203:
204: SVNUrl newUrl = SvnUtils
205: .getRepositoryRootUrl(nodes[rowIndex]
206: .getFile());
207: shortPath = SvnUtils.getRelativePath(newUrl,
208: nodes[rowIndex].getFile());
209: url.add(newUrl);
210:
211: }
212: } catch (SVNClientException ex) {
213: SvnClientExceptionHandler.notifyException(ex,
214: false, false);
215: }
216: if (shortPath == null) {
217: shortPath = org.openide.util.NbBundle.getMessage(
218: CommitTableModel.class,
219: "CTL_CommitForm_NotInRepository"); // NOI18N
220: }
221: }
222: return shortPath;
223: }
224: throw new IllegalArgumentException(
225: "Column index out of range: " + columnIndex); // NOI18N
226: }
227:
228: public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
229: String col = columns[columnIndex];
230: if (col.equals(COLUMN_NAME_ACTION)) {
231: commitOptions[rowIndex] = (CommitOptions) aValue;
232: fireTableCellUpdated(rowIndex, columnIndex);
233: } else {
234: throw new IllegalArgumentException(
235: "Column index out of range: " + columnIndex); // NOI18N
236: }
237: }
238:
239: private void defaultCommitOptions() {
240: boolean excludeNew = System
241: .getProperty("netbeans.subversion.excludeNewFiles") != null; // NOI18N
242: commitOptions = new CommitOptions[nodes.length];
243: for (int i = 0; i < nodes.length; i++) {
244: SvnFileNode node = nodes[i];
245: File file = node.getFile();
246: if (SvnModuleConfig.getDefault().isExcludedFromCommit(
247: file.getAbsolutePath())) {
248: commitOptions[i] = CommitOptions.EXCLUDE;
249: } else {
250: switch (node.getInformation().getStatus()) {
251: case FileInformation.STATUS_NOTVERSIONED_NEWLOCALLY:
252: commitOptions[i] = excludeNew ? CommitOptions.EXCLUDE
253: : getDefaultCommitOptions(node.getFile());
254: break;
255: case FileInformation.STATUS_VERSIONED_DELETEDLOCALLY:
256: case FileInformation.STATUS_VERSIONED_REMOVEDLOCALLY:
257: commitOptions[i] = CommitOptions.COMMIT_REMOVE;
258: break;
259: default:
260: commitOptions[i] = CommitOptions.COMMIT;
261: }
262: }
263: }
264: }
265:
266: public SvnFileNode getNode(int row) {
267: return nodes[row];
268: }
269:
270: public CommitOptions getOptions(int row) {
271: return commitOptions[row];
272: }
273:
274: private CommitOptions getDefaultCommitOptions(File file) {
275: if (file.isFile()) {
276: if (SvnUtils.getMimeType(file).startsWith("text")) {
277: return CommitOptions.ADD_TEXT;
278: } else {
279: return CommitOptions.ADD_BINARY;
280: }
281: } else {
282: return CommitOptions.ADD_DIRECTORY;
283: }
284: }
285:
286: private Set<SVNUrl> getRepositoryRoots() {
287: if (repositoryRoots == null) {
288: repositoryRoots = new HashSet<SVNUrl>();
289: }
290: return repositoryRoots;
291: }
292:
293: void setRootFile(String repositoryPath, String rootLocalPath) {
294: rootFile = new RootFile();
295: rootFile.repositoryPath = repositoryPath;
296: rootFile.rootLocalPath = rootLocalPath;
297: }
298:
299: }
|