KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > subversion > ui > commit > CommitTableModel


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.subversion.ui.commit;
21
22 import org.openide.util.NbBundle;
23 import org.netbeans.modules.subversion.SvnFileNode;
24 import org.netbeans.modules.subversion.FileInformation;
25 import org.netbeans.modules.subversion.util.SvnUtils;
26 import org.netbeans.modules.subversion.SvnModuleConfig;
27
28 import javax.swing.table.AbstractTableModel JavaDoc;
29 import java.util.*;
30 import java.io.File JavaDoc;
31 import org.tigris.subversion.svnclientadapter.SVNUrl;
32
33 /**
34  * Table model for the Commit dialog table.
35  *
36  * @author Maros Sandor
37  */

38 public class CommitTableModel extends AbstractTableModel JavaDoc {
39
40     public static final String JavaDoc COLUMN_NAME_NAME = "name"; // NOI18N
41
public static final String JavaDoc COLUMN_NAME_STATUS = "status"; // NOI18N
42
public static final String JavaDoc COLUMN_NAME_ACTION = "action"; // NOI18N
43
public static final String JavaDoc COLUMN_NAME_PATH = "path"; // NOI18N
44
public static final String JavaDoc COLUMN_NAME_BRANCH = "branch"; // NOI18N
45

46     private class RootFile {
47         String JavaDoc repositoryPath;
48         String JavaDoc rootLocalPath;
49     }
50     private Set<SVNUrl> repositoryRoots;
51     private RootFile rootFile;
52
53     /**
54      * Defines labels for Versioning view table columns.
55      */

56     private static final Map<String JavaDoc, String JavaDoc[]> columnLabels = new HashMap<String JavaDoc, String JavaDoc[]>(4);
57
58     {
59         ResourceBundle loc = NbBundle.getBundle(CommitTableModel.class);
60         columnLabels.put(COLUMN_NAME_NAME, new String JavaDoc [] {
61                                           loc.getString("CTL_CommitTable_Column_File"),
62                                           loc.getString("CTL_CommitTable_Column_File")});
63         columnLabels.put(COLUMN_NAME_BRANCH, new String JavaDoc [] {
64                                           loc.getString("CTL_CommitTable_Column_Branch"),
65                                           loc.getString("CTL_CommitTable_Column_Branch")});
66         columnLabels.put(COLUMN_NAME_STATUS, new String JavaDoc [] {
67                                           loc.getString("CTL_CommitTable_Column_Status"),
68                                           loc.getString("CTL_CommitTable_Column_Status")});
69         columnLabels.put(COLUMN_NAME_ACTION, new String JavaDoc [] {
70                                           loc.getString("CTL_CommitTable_Column_Action"),
71                                           loc.getString("CTL_CommitTable_Column_Action")});
72         columnLabels.put(COLUMN_NAME_PATH, new String JavaDoc [] {
73                                           loc.getString("CTL_CommitTable_Column_Folder"),
74                                           loc.getString("CTL_CommitTable_Column_Folder")});
75     }
76     
77     private CommitOptions [] commitOptions;
78     private SvnFileNode [] nodes;
79     
80     private String JavaDoc [] columns;
81
82     /**
83      * Create stable with name, status, action and path columns
84      * and empty nodes {@link #setNodes model}.
85      */

86     public CommitTableModel(String JavaDoc[] columns) {
87         setColumns(columns);
88         setNodes(new SvnFileNode[0]);
89     }
90
91     void setNodes(SvnFileNode [] nodes) {
92         this.nodes = nodes;
93         defaultCommitOptions();
94         fireTableDataChanged();
95     }
96     
97     void setColumns(String JavaDoc [] cols) {
98         if (Arrays.equals(cols, columns)) return;
99         columns = cols;
100         fireTableStructureChanged();
101     }
102
103     /**
104      * @return Map&lt;SvnFileNode, CommitOptions>
105      */

106     public Map<SvnFileNode, CommitOptions> getCommitFiles() {
107         Map<SvnFileNode, CommitOptions> ret = new HashMap<SvnFileNode, CommitOptions>(nodes.length);
108         for (int i = 0; i < nodes.length; i++) {
109             ret.put(nodes[i], commitOptions[i]);
110         }
111         return ret;
112     }
113     
114     public String JavaDoc getColumnName(int column) {
115         return columnLabels.get(columns[column])[0];
116     }
117
118     public int getColumnCount() {
119         return columns.length;
120     }
121
122     public int getRowCount() {
123         return nodes.length;
124     }
125
126     public Class JavaDoc getColumnClass(int columnIndex) {
127         String JavaDoc col = columns[columnIndex];
128         if (col.equals(COLUMN_NAME_ACTION)) {
129             return CommitOptions.class;
130         }
131         return String JavaDoc.class;
132     }
133
134     public boolean isCellEditable(int rowIndex, int columnIndex) {
135         String JavaDoc col = columns[columnIndex];
136         return col.equals(COLUMN_NAME_ACTION);
137     }
138
139     public Object JavaDoc getValueAt(int rowIndex, int columnIndex) {
140         SvnFileNode node;
141         String JavaDoc col = columns[columnIndex];
142         if (col.equals(COLUMN_NAME_NAME)) {
143             return nodes[rowIndex].getName();
144         } else if (col.equals(COLUMN_NAME_BRANCH)) {
145             String JavaDoc branch = SvnUtils.getCopy(nodes[rowIndex].getFile());
146             return branch == null ? "" : branch; // NOI18N
147
} else if (col.equals(COLUMN_NAME_STATUS)) {
148             node = nodes[rowIndex];
149             FileInformation finfo = node.getInformation();
150             finfo.getEntry(node.getFile()); // HACK returned value is not interesting, point is side effect, it loads ISVNStatus structure
151
return finfo.getStatusText();
152         } else if (col.equals(COLUMN_NAME_ACTION)) {
153             return commitOptions[rowIndex];
154         } else if (col.equals(COLUMN_NAME_PATH)) {
155             String JavaDoc shortPath = null;
156             // XXX this is a mess
157
if(rootFile != null) {
158                 // must convert from native separators to slashes
159
String JavaDoc relativePath = nodes[rowIndex].getFile().getAbsolutePath().substring(rootFile.rootLocalPath.length());
160                 shortPath = rootFile.repositoryPath + relativePath.replace(File.separatorChar, '/');
161             } else {
162                 Set<SVNUrl> url = getRepositoryRoots();
163                 for (Iterator<SVNUrl> it = url.iterator(); it.hasNext();) {
164                     SVNUrl nextUrl = it.next();
165                     shortPath = SvnUtils.getRelativePath(nextUrl, nodes[rowIndex].getFile());
166                 }
167                 if (shortPath == null) {
168                     SVNUrl newUrl = SvnUtils.getRepositoryRootUrl(nodes[rowIndex].getFile());
169                     shortPath = SvnUtils.getRelativePath(newUrl, nodes[rowIndex].getFile());
170                     url.add(newUrl);
171                 }
172                 //shortPath = SvnUtils.getRelativePath(nodes[rowIndex].getFile());
173
if (shortPath == null) {
174                     shortPath = org.openide.util.NbBundle.getMessage(CommitTableModel.class, "CTL_CommitForm_NotInRepository"); // NOI18N
175
}
176             }
177             return shortPath;
178         }
179         throw new IllegalArgumentException JavaDoc("Column index out of range: " + columnIndex); // NOI18N
180
}
181
182     public void setValueAt(Object JavaDoc aValue, int rowIndex, int columnIndex) {
183         String JavaDoc col = columns[columnIndex];
184         if (col.equals(COLUMN_NAME_ACTION)) {
185             commitOptions[rowIndex] = (CommitOptions) aValue;
186             fireTableCellUpdated(rowIndex, columnIndex);
187         } else {
188             throw new IllegalArgumentException JavaDoc("Column index out of range: " + columnIndex); // NOI18N
189
}
190     }
191
192     private void defaultCommitOptions() {
193         boolean excludeNew = System.getProperty("netbeans.subversion.excludeNewFiles") != null; // NOI18N
194
commitOptions = new CommitOptions[nodes.length];
195         for (int i = 0; i < nodes.length; i++) {
196             SvnFileNode node = nodes[i];
197             File JavaDoc file = node.getFile();
198             if (SvnModuleConfig.getDefault().isExcludedFromCommit(file.getAbsolutePath())) {
199                 commitOptions[i] = CommitOptions.EXCLUDE;
200             } else {
201                 switch (node.getInformation().getStatus()) {
202                 case FileInformation.STATUS_NOTVERSIONED_NEWLOCALLY:
203                     commitOptions[i] = excludeNew ? CommitOptions.EXCLUDE : getDefaultCommitOptions(node.getFile());
204                     break;
205                 case FileInformation.STATUS_VERSIONED_DELETEDLOCALLY:
206                 case FileInformation.STATUS_VERSIONED_REMOVEDLOCALLY:
207                     commitOptions[i] = CommitOptions.COMMIT_REMOVE;
208                     break;
209                 default:
210                     commitOptions[i] = CommitOptions.COMMIT;
211                 }
212             }
213         }
214     }
215
216     public SvnFileNode getNode(int row) {
217         return nodes[row];
218     }
219
220     public CommitOptions getOptions(int row) {
221         return commitOptions[row];
222     }
223
224     private CommitOptions getDefaultCommitOptions(File JavaDoc file) {
225         if (file.isFile()) {
226             if (SvnUtils.isFileContentBinary(file)) {
227                 return CommitOptions.ADD_BINARY;
228             } else {
229                 return CommitOptions.ADD_TEXT;
230             }
231         } else {
232             return CommitOptions.ADD_DIRECTORY;
233         }
234     }
235
236     private Set<SVNUrl> getRepositoryRoots() {
237         if(repositoryRoots == null) {
238             repositoryRoots = new HashSet<SVNUrl>();
239         }
240         return repositoryRoots;
241     }
242
243     void setRootFile(String JavaDoc repositoryPath, String JavaDoc rootLocalPath) {
244         rootFile = new RootFile();
245         rootFile.repositoryPath = repositoryPath;
246         rootFile.rootLocalPath = rootLocalPath;
247     }
248
249 }
250
Popular Tags