KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > versioning > system > cvss > ui > actions > 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.versioning.system.cvss.ui.actions.commit;
21
22 import org.openide.util.NbBundle;
23 import org.netbeans.modules.versioning.system.cvss.CvsFileNode;
24 import org.netbeans.modules.versioning.system.cvss.FileInformation;
25 import org.netbeans.modules.versioning.system.cvss.CvsVersioningSystem;
26 import org.netbeans.modules.versioning.system.cvss.util.Utils;
27 import org.netbeans.modules.versioning.system.cvss.CvsModuleConfig;
28 import org.netbeans.lib.cvsclient.command.KeywordSubstitutionOptions;
29
30 import javax.swing.table.AbstractTableModel JavaDoc;
31 import java.util.*;
32 import java.io.File JavaDoc;
33
34 /**
35  * Table model for the Commit dialog table.
36  *
37  * @author Maros Sandor
38  */

39 class CommitTableModel extends AbstractTableModel JavaDoc {
40
41     /**
42      * Defines labels for Versioning view table columns.
43      */

44     private final Map<String JavaDoc, String JavaDoc[]> columnLabels = new HashMap<String JavaDoc, String JavaDoc[]>(5);
45     {
46         ResourceBundle loc = NbBundle.getBundle(CommitTableModel.class);
47         columnLabels.put(CommitSettings.COLUMN_NAME_NAME, new String JavaDoc [] {
48                                           loc.getString("CTL_CommitTable_Column_File"),
49                                           loc.getString("CTL_CommitTable_Column_File")});
50         columnLabels.put(CommitSettings.COLUMN_NAME_STICKY, new String JavaDoc [] {
51                                           loc.getString("CTL_CommitTable_Column_Sticky"),
52                                           loc.getString("CTL_CommitTable_Column_Sticky")});
53         columnLabels.put(CommitSettings.COLUMN_NAME_STATUS, new String JavaDoc [] {
54                                           loc.getString("CTL_CommitTable_Column_Status"),
55                                           loc.getString("CTL_CommitTable_Column_Status")});
56         columnLabels.put(CommitSettings.COLUMN_NAME_ACTION, new String JavaDoc [] {
57                                           loc.getString("CTL_CommitTable_Column_Action"),
58                                           loc.getString("CTL_CommitTable_Column_Action")});
59         columnLabels.put(CommitSettings.COLUMN_NAME_PATH, new String JavaDoc [] {
60                                           loc.getString("CTL_CommitTable_Column_Folder"),
61                                           loc.getString("CTL_CommitTable_Column_Folder")});
62     }
63     
64     private CommitOptions [] commitOptions;
65     private CvsFileNode [] nodes;
66     
67     private String JavaDoc [] columns;
68
69     public CommitTableModel() {
70         setColumns(new String JavaDoc [0]);
71         setNodes(new CvsFileNode[0]);
72     }
73
74     void setNodes(CvsFileNode [] nodes) {
75         this.nodes = nodes;
76         createCommitOptions();
77         fireTableDataChanged();
78     }
79     
80     void setColumns(String JavaDoc [] cols) {
81         if (Arrays.equals(cols, columns)) return;
82         columns = cols;
83         fireTableStructureChanged();
84     }
85
86     public CommitSettings.CommitFile[] getCommitFiles() {
87         CommitSettings.CommitFile [] files = new CommitSettings.CommitFile[nodes.length];
88         for (int i = 0; i < nodes.length; i++) {
89             files[i] = new CommitSettings.CommitFile(nodes[i], commitOptions[i]);
90         }
91         return files;
92     }
93     
94     public String JavaDoc getColumnName(int column) {
95         return ((String JavaDoc []) columnLabels.get(columns[column]))[0];
96     }
97
98     public int getColumnCount() {
99         return columns.length;
100     }
101
102     public int getRowCount() {
103         return nodes.length;
104     }
105
106     public Class JavaDoc getColumnClass(int columnIndex) {
107         String JavaDoc col = columns[columnIndex];
108         if (col.equals(CommitSettings.COLUMN_NAME_ACTION)) {
109             return CommitOptions.class;
110         }
111         return String JavaDoc.class;
112     }
113
114     public boolean isCellEditable(int rowIndex, int columnIndex) {
115         String JavaDoc col = columns[columnIndex];
116         return col.equals(CommitSettings.COLUMN_NAME_ACTION);
117     }
118
119     public Object JavaDoc getValueAt(int rowIndex, int columnIndex) {
120         String JavaDoc col = columns[columnIndex];
121         if (col.equals(CommitSettings.COLUMN_NAME_NAME)) {
122             return nodes[rowIndex].getName();
123         } else if (col.equals(CommitSettings.COLUMN_NAME_STICKY)) {
124             String JavaDoc sticky = Utils.getSticky(nodes[rowIndex].getFile());
125             return sticky == null ? "" : sticky.substring(1); // NOI18N
126
} else if (col.equals(CommitSettings.COLUMN_NAME_STATUS)) {
127             return nodes[rowIndex].getInformation().getStatusText();
128         } else if (col.equals(CommitSettings.COLUMN_NAME_ACTION)) {
129             return commitOptions[rowIndex];
130         } else if (col.equals(CommitSettings.COLUMN_NAME_PATH)) {
131             return Utils.getRelativePath(nodes[rowIndex].getFile());
132         }
133         throw new IllegalArgumentException JavaDoc("Column index out of range: " + columnIndex); // NOI18N
134
}
135
136     public void setValueAt(Object JavaDoc aValue, int rowIndex, int columnIndex) {
137         String JavaDoc col = columns[columnIndex];
138         if (col.equals(CommitSettings.COLUMN_NAME_ACTION)) {
139             commitOptions[rowIndex] = (CommitOptions) aValue;
140             fireTableCellUpdated(rowIndex, columnIndex);
141         } else {
142             throw new IllegalArgumentException JavaDoc("Column index out of range: " + columnIndex); // NOI18N
143
}
144     }
145
146     private void createCommitOptions() {
147         boolean excludeNew = CvsModuleConfig.getDefault().getPreferences().getBoolean(CvsModuleConfig.PROP_EXCLUDE_NEW_FILES, false);
148         commitOptions = new CommitOptions[nodes.length];
149         for (int i = 0; i < nodes.length; i++) {
150             CvsFileNode node = nodes[i];
151             if (CvsModuleConfig.getDefault().isExcludedFromCommit(node.getFile())) {
152                 commitOptions[i] = CommitOptions.EXCLUDE;
153             } else {
154                 switch (node.getInformation().getStatus()) {
155                 case FileInformation.STATUS_NOTVERSIONED_NEWLOCALLY:
156                     commitOptions[i] = excludeNew ? CommitOptions.EXCLUDE : getDefaultCommitOptions(node.getFile());
157                     break;
158                 case FileInformation.STATUS_VERSIONED_DELETEDLOCALLY:
159                 case FileInformation.STATUS_VERSIONED_REMOVEDLOCALLY:
160                     commitOptions[i] = CommitOptions.COMMIT_REMOVE;
161                     break;
162                 default:
163                     commitOptions[i] = CommitOptions.COMMIT;
164                 }
165             }
166         }
167     }
168
169     CommitSettings.CommitFile getCommitFile(int row) {
170         return new CommitSettings.CommitFile(nodes[row], commitOptions[row]);
171     }
172     
173     private CommitOptions getDefaultCommitOptions(File JavaDoc file) {
174         KeywordSubstitutionOptions options = CvsVersioningSystem.getInstance().getDefaultKeywordSubstitution(file);
175         return options == KeywordSubstitutionOptions.BINARY ? CommitOptions.ADD_BINARY : CommitOptions.ADD_TEXT;
176     }
177 }
178
Popular Tags