KickJava   Java API By Example, From Geeks To Geeks.

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


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.netbeans.modules.versioning.util.FilePathCellRenderer;
23 import org.netbeans.modules.versioning.util.TableSorter;
24 import org.netbeans.modules.subversion.util.SvnUtils;
25 import org.netbeans.modules.subversion.SvnFileNode;
26 import org.netbeans.modules.subversion.FileInformation;
27 import org.netbeans.modules.subversion.Subversion;
28 import org.openide.util.NbBundle;
29
30 import javax.swing.*;
31 import javax.swing.event.AncestorEvent JavaDoc;
32 import javax.swing.event.AncestorListener JavaDoc;
33 import javax.swing.event.TableModelListener JavaDoc;
34 import javax.swing.event.TableModelEvent JavaDoc;
35 import javax.swing.table.DefaultTableCellRenderer JavaDoc;
36 import javax.swing.table.TableModel JavaDoc;
37 import javax.swing.table.TableColumnModel JavaDoc;
38 import java.awt.Component JavaDoc;
39 import java.lang.String JavaDoc;
40 import java.util.*;
41
42 /**
43  * {@link #getComponent Table} that displays nodes in the commit dialog.
44  *
45  * @author Maros Sandor
46  */

47 public class CommitTable implements AncestorListener JavaDoc, TableModelListener JavaDoc {
48
49     public static String JavaDoc [] COMMIT_COLUMNS = new String JavaDoc [] {
50                                             CommitTableModel.COLUMN_NAME_NAME,
51                                             CommitTableModel.COLUMN_NAME_STATUS,
52                                             CommitTableModel.COLUMN_NAME_ACTION,
53                                             CommitTableModel.COLUMN_NAME_PATH
54                                         };
55
56     public static String JavaDoc [] IMPORT_COLUMNS = new String JavaDoc [] {
57                                             CommitTableModel.COLUMN_NAME_NAME,
58                                             CommitTableModel.COLUMN_NAME_ACTION,
59                                             CommitTableModel.COLUMN_NAME_PATH
60                                         };
61     
62     private CommitTableModel tableModel;
63     private JTable table;
64     private JComponent component;
65     
66     private TableSorter sorter;
67     private String JavaDoc[] columns;
68     private String JavaDoc[] sortByColumns;
69     
70     
71     public CommitTable(JLabel label, String JavaDoc[] columns, String JavaDoc[] sortByColumns) {
72         init(label, columns, null);
73         this.sortByColumns = sortByColumns;
74         setSortingStatus();
75     }
76
77     public CommitTable(JLabel label, String JavaDoc[] columns, TableSorter sorter) {
78         init(label, columns, sorter);
79     }
80     
81     private void init(JLabel label, String JavaDoc[] columns, TableSorter sorter) {
82         tableModel = new CommitTableModel(columns);
83         tableModel.addTableModelListener(this);
84         if(sorter == null) {
85             sorter = new TableSorter(tableModel);
86         }
87         this.sorter = sorter;
88         table = new JTable(this.sorter);
89         table.getTableHeader().setReorderingAllowed(false);
90         table.setDefaultRenderer(String JavaDoc.class, new CommitStringsCellRenderer());
91         table.setDefaultEditor(CommitOptions.class, new CommitOptionsCellEditor());
92         table.getTableHeader().setReorderingAllowed(true);
93         this.sorter.setTableHeader(table.getTableHeader());
94         table.setRowHeight(table.getRowHeight() * 6 / 5);
95         table.addAncestorListener(this);
96         component = new JScrollPane(table, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
97         label.setLabelFor(table);
98         table.getAccessibleContext().setAccessibleDescription(NbBundle.getMessage(CommitTable.class, "ACSD_CommitTable")); // NOI18N
99
setColumns(columns);
100     }
101
102     public void ancestorAdded(AncestorEvent JavaDoc event) {
103         setDefaultColumnSizes();
104     }
105
106     /**
107      * Sets sizes of Commit table columns, kind of hardcoded.
108      */

109     private void setDefaultColumnSizes() {
110         int width = table.getWidth();
111         TableColumnModel JavaDoc columnModel = table.getColumnModel();
112         if (columns == null || columnModel == null) return; // unsure when this methed will be called (component realization)
113
if (columnModel.getColumnCount() != columns.length) return;
114         if (columns.length == 3) {
115             for (int i = 0; i < columns.length; i++) {
116                 String JavaDoc col = columns[i];
117                 sorter.setColumnComparator(i, null);
118                 if (col.equals(CommitTableModel.COLUMN_NAME_NAME)) {
119                     sorter.setColumnComparator(i, new FileNameComparator());
120                     columnModel.getColumn(i).setPreferredWidth(width * 30 / 100);
121                 } else if (col.equals(CommitTableModel.COLUMN_NAME_ACTION)) {
122                     columnModel.getColumn(i).setPreferredWidth(width * 15 / 100);
123                 } else {
124                     columnModel.getColumn(i).setPreferredWidth(width * 40 / 100);
125                 }
126             }
127         } else if (columns.length == 4) {
128             for (int i = 0; i < columns.length; i++) {
129                 String JavaDoc col = columns[i];
130                 sorter.setColumnComparator(i, null);
131                 if (col.equals(CommitTableModel.COLUMN_NAME_NAME)) {
132                     sorter.setColumnComparator(i, new FileNameComparator());
133                     columnModel.getColumn(i).setPreferredWidth(width * 30 / 100);
134                 } else if (col.equals(CommitTableModel.COLUMN_NAME_STATUS)) {
135                     sorter.setColumnComparator(i, new StatusComparator());
136                     columnModel.getColumn(i).setPreferredWidth(width * 15 / 100);
137                 } else if (col.equals(CommitTableModel.COLUMN_NAME_ACTION)) {
138                     columnModel.getColumn(i).setPreferredWidth(width * 15 / 100);
139                 } else {
140                     columnModel.getColumn(i).setPreferredWidth(width * 40 / 100);
141                 }
142             }
143         } else if (columns.length == 5) {
144             for (int i = 0; i < columns.length; i++) {
145                 String JavaDoc col = columns[i];
146                 sorter.setColumnComparator(i, null);
147                 if (col.equals(CommitTableModel.COLUMN_NAME_NAME)) {
148                     sorter.setColumnComparator(i, new FileNameComparator());
149                     columnModel.getColumn(i).setPreferredWidth(width * 25 / 100);
150                 } else if (col.equals(CommitTableModel.COLUMN_NAME_STATUS)) {
151                     sorter.setColumnComparator(i, new StatusComparator());
152                     sorter.setSortingStatus(i, TableSorter.ASCENDING);
153                     columnModel.getColumn(i).setPreferredWidth(width * 15 / 100);
154                 } else if (col.equals(CommitTableModel.COLUMN_NAME_ACTION)) {
155                     columnModel.getColumn(i).setPreferredWidth(width * 15 / 100);
156                 } else {
157                     columnModel.getColumn(i).setPreferredWidth(width * 30 / 100);
158                 }
159             }
160         }
161     }
162
163     private void setSortingStatus() {
164         for (int i = 0; i < sortByColumns.length; i++) {
165             String JavaDoc sortByColumn = sortByColumns[i];
166             for (int j = 0; j < columns.length; j++) {
167                 String JavaDoc column = columns[j];
168                 if(column.equals(sortByColumn)) {
169                     sorter.setSortingStatus(j, column.equals(sortByColumn) ? TableSorter.ASCENDING : TableSorter.NOT_SORTED);
170                     break;
171                 }
172             }
173         }
174     }
175     
176     public TableSorter getSorter() {
177         return sorter;
178     }
179     
180     public void ancestorMoved(AncestorEvent JavaDoc event) {
181     }
182
183     public void ancestorRemoved(AncestorEvent JavaDoc event) {
184     }
185     
186     void setColumns(String JavaDoc[] cols) {
187         if (Arrays.equals(columns, cols)) return;
188         columns = cols;
189         tableModel.setColumns(cols);
190         setDefaultColumnSizes();
191     }
192
193     public void setNodes(SvnFileNode[] nodes) {
194         tableModel.setNodes(nodes);
195     }
196
197     /**
198      * @return Map&lt;SvnFileNode, CommitOptions>
199      */

200     public Map<SvnFileNode, CommitOptions> getCommitFiles() {
201         return tableModel.getCommitFiles();
202     }
203
204     /**
205      * @return table in a scrollpane
206      */

207     public JComponent getComponent() {
208         return component;
209     }
210
211     void dataChanged() {
212         int idx = table.getSelectedRow();
213         tableModel.fireTableDataChanged();
214         if (idx != -1) table.getSelectionModel().addSelectionInterval(idx, idx);
215     }
216
217     TableModel JavaDoc getTableModel() {
218         return tableModel;
219     }
220
221     public void tableChanged(TableModelEvent JavaDoc e) {
222         // change in commit options may alter name rendering (strikethrough)
223
table.repaint();
224     }
225
226     public void setRootFile(String JavaDoc repositoryPath, String JavaDoc rootLocalPath) {
227         tableModel.setRootFile(repositoryPath, rootLocalPath);
228     }
229     
230     private class CommitOptionsCellEditor extends DefaultCellEditor {
231
232         private final Object JavaDoc[] dirAddOptions = new Object JavaDoc [] {
233                 CommitOptions.ADD_DIRECTORY,
234                 CommitOptions.EXCLUDE
235             };
236         
237         private final Object JavaDoc[] addOptions = new Object JavaDoc [] {
238                 CommitOptions.ADD_TEXT,
239                 CommitOptions.ADD_BINARY,
240                 CommitOptions.EXCLUDE
241             };
242         private final Object JavaDoc[] commitOptions = new Object JavaDoc [] {
243                 CommitOptions.COMMIT,
244                 CommitOptions.EXCLUDE
245             };
246
247         private final Object JavaDoc[] removeOptions = new Object JavaDoc [] {
248                 CommitOptions.COMMIT_REMOVE,
249                 CommitOptions.EXCLUDE
250             };
251
252         public CommitOptionsCellEditor() {
253             super(new JComboBox());
254         }
255
256         public Component JavaDoc getTableCellEditorComponent(JTable table, Object JavaDoc value, boolean isSelected, int row, int column) {
257             FileInformation info = tableModel.getNode(sorter.modelIndex(row)).getInformation();
258             int fileStatus = info.getStatus();
259             JComboBox combo = (JComboBox) editorComponent;
260             if (fileStatus == FileInformation.STATUS_VERSIONED_DELETEDLOCALLY || fileStatus == FileInformation.STATUS_VERSIONED_REMOVEDLOCALLY) {
261                 combo.setModel(new DefaultComboBoxModel(removeOptions));
262             } else if ((fileStatus & FileInformation.STATUS_IN_REPOSITORY) == 0) {
263                 if (info.isDirectory()) {
264                     combo.setModel(new DefaultComboBoxModel(dirAddOptions));
265                 } else {
266                     combo.setModel(new DefaultComboBoxModel(addOptions));
267                 }
268             } else {
269                 combo.setModel(new DefaultComboBoxModel(commitOptions));
270             }
271             return super.getTableCellEditorComponent(table, value, isSelected, row, column);
272         }
273     }
274
275     private class CommitStringsCellRenderer extends DefaultTableCellRenderer JavaDoc {
276
277         private FilePathCellRenderer pathRenderer = new FilePathCellRenderer();
278
279         public Component JavaDoc getTableCellRendererComponent(JTable table, Object JavaDoc value, boolean isSelected, boolean hasFocus, int row, int column) {
280             int col = table.convertColumnIndexToModel(column);
281             if (columns[col] == CommitTableModel.COLUMN_NAME_NAME) {
282                 TableSorter sorter = (TableSorter) table.getModel();
283                 CommitTableModel model = (CommitTableModel) sorter.getTableModel();
284                 SvnFileNode node = model.getNode(sorter.modelIndex(row));
285                 CommitOptions options = model.getOptions(sorter.modelIndex(row));
286                 if (!isSelected) {
287                     value = "<html>" + Subversion.getInstance().getAnnotator().annotateNameHtml( // NOI18N
288
node.getFile().getName(), node.getInformation(), null);
289                 }
290                 if (options == CommitOptions.EXCLUDE) {
291                     value = "<html><s>" + value + "</s></html>"; // NOI18N
292
}
293                 return super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
294             } else if (columns[col] == CommitTableModel.COLUMN_NAME_PATH) {
295                 return pathRenderer.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
296             } else {
297                 return super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
298             }
299         }
300     }
301     
302     private class StatusComparator extends SvnUtils.ByImportanceComparator {
303         public int compare(Object JavaDoc o1, Object JavaDoc o2) {
304             Integer JavaDoc row1 = (Integer JavaDoc) o1;
305             Integer JavaDoc row2 = (Integer JavaDoc) o2;
306             return super.compare(tableModel.getNode(row1.intValue()).getInformation(),
307                                  tableModel.getNode(row2.intValue()).getInformation());
308         }
309     }
310     
311     private class FileNameComparator implements Comparator {
312         public int compare(Object JavaDoc o1, Object JavaDoc o2) {
313             Integer JavaDoc row1 = (Integer JavaDoc) o1;
314             Integer JavaDoc row2 = (Integer JavaDoc) o2;
315             return tableModel.getNode(row1.intValue()).getName().compareToIgnoreCase(
316                     tableModel.getNode(row2.intValue()).getName());
317         }
318     }
319
320
321 }
322
Popular Tags