~ubuntu-branches/ubuntu/quantal/netbeans/quantal

« back to all changes in this revision

Viewing changes to subversion/main/src/org/netbeans/modules/subversion/ui/commit/CommitTable.java

  • Committer: Bazaar Package Importer
  • Author(s): Marek Slama
  • Date: 2008-01-29 14:11:22 UTC
  • Revision ID: james.westby@ubuntu.com-20080129141122-fnzjbo11ntghxfu7
Tags: upstream-6.0.1
ImportĀ upstreamĀ versionĀ 6.0.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 
3
 *
 
4
 * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
 
5
 *
 
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]"
 
23
 *
 
24
 * Contributor(s):
 
25
 *
 
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
 *
 
30
 * If you wish your version of this file to be governed by only the CDDL
 
31
 * or only the GPL Version 2, indicate your decision by adding
 
32
 * "[Contributor] elects to include this software in this distribution
 
33
 * under the [CDDL or GPL Version 2] license." If you do not indicate a
 
34
 * single choice of license, a recipient has the option to distribute
 
35
 * your version of this file under either the CDDL, the GPL Version 2 or
 
36
 * to extend the choice of license to its licensees as provided above.
 
37
 * However, if you add GPL Version 2 code and therefore, elected the GPL
 
38
 * Version 2 license, then the option applies only if the new code is
 
39
 * made subject to such option by the copyright holder.
 
40
 */
 
41
 
 
42
package org.netbeans.modules.subversion.ui.commit;
 
43
 
 
44
import org.netbeans.modules.versioning.util.FilePathCellRenderer;
 
45
import org.netbeans.modules.versioning.util.TableSorter;
 
46
import org.netbeans.modules.subversion.util.SvnUtils;
 
47
import org.netbeans.modules.subversion.SvnFileNode;
 
48
import org.netbeans.modules.subversion.FileInformation;
 
49
import org.netbeans.modules.subversion.Subversion;
 
50
import org.openide.util.NbBundle;
 
51
 
 
52
import javax.swing.*;
 
53
import javax.swing.event.AncestorEvent;
 
54
import javax.swing.event.AncestorListener;
 
55
import javax.swing.event.TableModelListener;
 
56
import javax.swing.event.TableModelEvent;
 
57
import javax.swing.table.DefaultTableCellRenderer;
 
58
import javax.swing.table.TableModel;
 
59
import javax.swing.table.TableColumnModel;
 
60
import java.awt.Component;
 
61
import java.lang.String;
 
62
import java.util.*;
 
63
 
 
64
/**
 
65
 * {@link #getComponent Table} that displays nodes in the commit dialog.
 
66
 * 
 
67
 * @author Maros Sandor
 
68
 */
 
69
public class CommitTable implements AncestorListener, TableModelListener {
 
70
 
 
71
    public static String [] COMMIT_COLUMNS = new String [] {
 
72
                                            CommitTableModel.COLUMN_NAME_NAME,
 
73
                                            CommitTableModel.COLUMN_NAME_STATUS,
 
74
                                            CommitTableModel.COLUMN_NAME_ACTION,
 
75
                                            CommitTableModel.COLUMN_NAME_PATH
 
76
                                        };
 
77
 
 
78
    public static String [] IMPORT_COLUMNS = new String [] {
 
79
                                            CommitTableModel.COLUMN_NAME_NAME,                                            
 
80
                                            CommitTableModel.COLUMN_NAME_ACTION,
 
81
                                            CommitTableModel.COLUMN_NAME_PATH
 
82
                                        };
 
83
    
 
84
    private CommitTableModel    tableModel;
 
85
    private JTable              table;
 
86
    private JComponent          component;
 
87
    
 
88
    private TableSorter         sorter;
 
89
    private String[]            columns;
 
90
    private String[]            sortByColumns;
 
91
    
 
92
    
 
93
    public CommitTable(JLabel label, String[] columns, String[] sortByColumns) {
 
94
        init(label, columns, null);
 
95
        this.sortByColumns = sortByColumns;        
 
96
        setSortingStatus();            
 
97
    }
 
98
 
 
99
    public CommitTable(JLabel label, String[] columns, TableSorter sorter) {
 
100
        init(label, columns, sorter);        
 
101
    }
 
102
    
 
103
    private void init(JLabel label, String[] columns, TableSorter sorter) {
 
104
        tableModel = new CommitTableModel(columns);
 
105
        tableModel.addTableModelListener(this);
 
106
        if(sorter == null) {
 
107
            sorter = new TableSorter(tableModel);
 
108
        } 
 
109
        this.sorter = sorter;   
 
110
        table = new JTable(this.sorter);
 
111
        table.getTableHeader().setReorderingAllowed(false);
 
112
        table.setDefaultRenderer(String.class, new CommitStringsCellRenderer());
 
113
        table.setDefaultEditor(CommitOptions.class, new CommitOptionsCellEditor());
 
114
        table.getTableHeader().setReorderingAllowed(true);
 
115
        this.sorter.setTableHeader(table.getTableHeader());
 
116
        table.setRowHeight(table.getRowHeight() * 6 / 5);
 
117
        table.addAncestorListener(this);
 
118
        component = new JScrollPane(table, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
 
119
        label.setLabelFor(table);
 
120
        table.getAccessibleContext().setAccessibleDescription(NbBundle.getMessage(CommitTable.class, "ACSD_CommitTable")); // NOI18N        
 
121
        setColumns(columns);
 
122
    }
 
123
 
 
124
    public void ancestorAdded(AncestorEvent event) {
 
125
        setDefaultColumnSizes();
 
126
    }
 
127
 
 
128
    /**
 
129
     * Sets sizes of Commit table columns, kind of hardcoded.
 
130
     */ 
 
131
    private void setDefaultColumnSizes() {
 
132
        int width = table.getWidth();
 
133
        TableColumnModel columnModel = table.getColumnModel();
 
134
        if (columns == null || columnModel == null) return; // unsure when this methed will be called (component realization) 
 
135
        if (columnModel.getColumnCount() != columns.length) return; 
 
136
        if (columns.length == 3) {
 
137
            for (int i = 0; i < columns.length; i++) {
 
138
                String col = columns[i];                                
 
139
                sorter.setColumnComparator(i, null);                    
 
140
                if (col.equals(CommitTableModel.COLUMN_NAME_NAME)) {
 
141
                    sorter.setColumnComparator(i, new FileNameComparator());
 
142
                    columnModel.getColumn(i).setPreferredWidth(width * 30 / 100);
 
143
                } else if (col.equals(CommitTableModel.COLUMN_NAME_ACTION)) {
 
144
                    columnModel.getColumn(i).setPreferredWidth(width * 15 / 100);
 
145
                } else {
 
146
                    columnModel.getColumn(i).setPreferredWidth(width * 40 / 100);
 
147
                }                
 
148
            }
 
149
        } else if (columns.length == 4) {
 
150
            for (int i = 0; i < columns.length; i++) {
 
151
                String col = columns[i];                                
 
152
                sorter.setColumnComparator(i, null);                    
 
153
                if (col.equals(CommitTableModel.COLUMN_NAME_NAME)) {
 
154
                    sorter.setColumnComparator(i, new FileNameComparator());
 
155
                    columnModel.getColumn(i).setPreferredWidth(width * 30 / 100);
 
156
                } else if (col.equals(CommitTableModel.COLUMN_NAME_STATUS)) {
 
157
                    sorter.setColumnComparator(i, new StatusComparator());                    
 
158
                    columnModel.getColumn(i).setPreferredWidth(width * 15 / 100);
 
159
                } else if (col.equals(CommitTableModel.COLUMN_NAME_ACTION)) {
 
160
                    columnModel.getColumn(i).setPreferredWidth(width * 15 / 100);
 
161
                } else {
 
162
                    columnModel.getColumn(i).setPreferredWidth(width * 40 / 100);
 
163
                }                
 
164
            }
 
165
        } else if (columns.length == 5) {
 
166
            for (int i = 0; i < columns.length; i++) {
 
167
                String col = columns[i];
 
168
                sorter.setColumnComparator(i, null);                
 
169
                if (col.equals(CommitTableModel.COLUMN_NAME_NAME)) {
 
170
                    sorter.setColumnComparator(i, new FileNameComparator());
 
171
                    columnModel.getColumn(i).setPreferredWidth(width * 25 / 100);
 
172
                } else if (col.equals(CommitTableModel.COLUMN_NAME_STATUS)) {
 
173
                    sorter.setColumnComparator(i, new StatusComparator());
 
174
                    sorter.setSortingStatus(i, TableSorter.ASCENDING);
 
175
                    columnModel.getColumn(i).setPreferredWidth(width * 15 / 100);
 
176
                } else if (col.equals(CommitTableModel.COLUMN_NAME_ACTION)) {
 
177
                    columnModel.getColumn(i).setPreferredWidth(width * 15 / 100);
 
178
                } else {
 
179
                    columnModel.getColumn(i).setPreferredWidth(width * 30 / 100);
 
180
                }
 
181
            }
 
182
        }
 
183
    }
 
184
 
 
185
    private void setSortingStatus() {
 
186
        for (int i = 0; i < sortByColumns.length; i++) {
 
187
            String sortByColumn = sortByColumns[i];        
 
188
            for (int j = 0; j < columns.length; j++) {
 
189
                String column = columns[j];
 
190
                if(column.equals(sortByColumn)) {
 
191
                    sorter.setSortingStatus(j, column.equals(sortByColumn) ? TableSorter.ASCENDING : TableSorter.NOT_SORTED);                       
 
192
                    break;
 
193
                }                    
 
194
            }                        
 
195
        }        
 
196
    }
 
197
    
 
198
    public TableSorter getSorter() {
 
199
        return sorter;
 
200
    }
 
201
    
 
202
    public void ancestorMoved(AncestorEvent event) {
 
203
    }
 
204
 
 
205
    public void ancestorRemoved(AncestorEvent event) {
 
206
    }
 
207
    
 
208
    void setColumns(String[] cols) {
 
209
        if (Arrays.equals(columns, cols)) return;
 
210
        columns = cols;
 
211
        tableModel.setColumns(cols);
 
212
        setDefaultColumnSizes();
 
213
    }
 
214
 
 
215
    public void setNodes(SvnFileNode[] nodes) {
 
216
        tableModel.setNodes(nodes);
 
217
    }
 
218
 
 
219
    /**
 
220
     * @return Map&lt;SvnFileNode, CommitOptions>
 
221
     */
 
222
    public Map<SvnFileNode, CommitOptions> getCommitFiles() {
 
223
        return tableModel.getCommitFiles();
 
224
    }
 
225
 
 
226
    /**
 
227
     * @return table in a scrollpane 
 
228
     */
 
229
    public JComponent getComponent() {
 
230
        return component;
 
231
    }
 
232
 
 
233
    void dataChanged() {
 
234
        int idx = table.getSelectedRow();
 
235
        tableModel.fireTableDataChanged();
 
236
        if (idx != -1) table.getSelectionModel().addSelectionInterval(idx, idx);
 
237
    }
 
238
 
 
239
    TableModel getTableModel() {
 
240
        return tableModel;
 
241
    }
 
242
 
 
243
    public void tableChanged(TableModelEvent e) {
 
244
        // change in commit options may alter name rendering (strikethrough)
 
245
        table.repaint();
 
246
    }
 
247
 
 
248
    public void setRootFile(String repositoryPath, String rootLocalPath) {
 
249
        tableModel.setRootFile(repositoryPath, rootLocalPath);
 
250
    }
 
251
    
 
252
    private class CommitOptionsCellEditor extends DefaultCellEditor {
 
253
 
 
254
        private final Object[] dirAddOptions = new Object [] {
 
255
                CommitOptions.ADD_DIRECTORY,
 
256
                CommitOptions.EXCLUDE
 
257
            };
 
258
        
 
259
        private final Object[] addOptions = new Object [] {
 
260
                CommitOptions.ADD_TEXT,
 
261
                CommitOptions.ADD_BINARY,
 
262
                CommitOptions.EXCLUDE
 
263
            };
 
264
        private final Object[] commitOptions = new Object [] {
 
265
                CommitOptions.COMMIT,
 
266
                CommitOptions.EXCLUDE
 
267
            };
 
268
 
 
269
        private final Object[] removeOptions = new Object [] {
 
270
                CommitOptions.COMMIT_REMOVE,
 
271
                CommitOptions.EXCLUDE
 
272
            };
 
273
 
 
274
        public CommitOptionsCellEditor() {
 
275
            super(new JComboBox());
 
276
        }
 
277
 
 
278
        public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) {
 
279
            FileInformation info = tableModel.getNode(sorter.modelIndex(row)).getInformation();
 
280
            int fileStatus = info.getStatus();
 
281
            JComboBox combo = (JComboBox) editorComponent;
 
282
            if (fileStatus == FileInformation.STATUS_VERSIONED_DELETEDLOCALLY || fileStatus == FileInformation.STATUS_VERSIONED_REMOVEDLOCALLY) {
 
283
                combo.setModel(new DefaultComboBoxModel(removeOptions));
 
284
            } else if ((fileStatus & FileInformation.STATUS_IN_REPOSITORY) == 0) {
 
285
                if (info.isDirectory()) {
 
286
                    combo.setModel(new DefaultComboBoxModel(dirAddOptions));
 
287
                } else {
 
288
                    combo.setModel(new DefaultComboBoxModel(addOptions));
 
289
                }
 
290
            } else {
 
291
                combo.setModel(new DefaultComboBoxModel(commitOptions));
 
292
            }
 
293
            return super.getTableCellEditorComponent(table, value, isSelected, row, column);
 
294
        }
 
295
    }
 
296
 
 
297
    private class CommitStringsCellRenderer extends DefaultTableCellRenderer {
 
298
 
 
299
        private FilePathCellRenderer pathRenderer = new FilePathCellRenderer();
 
300
 
 
301
        public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
 
302
            int col = table.convertColumnIndexToModel(column);
 
303
            if (columns[col] == CommitTableModel.COLUMN_NAME_NAME) {
 
304
                TableSorter sorter = (TableSorter) table.getModel();
 
305
                CommitTableModel model = (CommitTableModel) sorter.getTableModel();
 
306
                SvnFileNode node = model.getNode(sorter.modelIndex(row));
 
307
                CommitOptions options = model.getOptions(sorter.modelIndex(row));
 
308
                if (!isSelected) {
 
309
                    value = "<html>" + Subversion.getInstance().getAnnotator().annotateNameHtml(  // NOI18N
 
310
                            node.getFile().getName(), node.getInformation(), null);
 
311
                }
 
312
                if (options == CommitOptions.EXCLUDE) {
 
313
                    value = "<html><s>" + value + "</s></html>"; // NOI18N
 
314
                }
 
315
                return super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
 
316
            } else if (columns[col] == CommitTableModel.COLUMN_NAME_PATH) {
 
317
                return pathRenderer.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
 
318
            } else {
 
319
                return super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
 
320
            }
 
321
        }
 
322
    }
 
323
    
 
324
    private class StatusComparator extends SvnUtils.ByImportanceComparator {
 
325
        public int compare(Object o1, Object o2) {
 
326
            Integer row1 = (Integer) o1;
 
327
            Integer row2 = (Integer) o2;
 
328
            return super.compare(tableModel.getNode(row1.intValue()).getInformation(),
 
329
                                 tableModel.getNode(row2.intValue()).getInformation());
 
330
        }
 
331
    }
 
332
    
 
333
    private class FileNameComparator implements Comparator {
 
334
        public int compare(Object o1, Object o2) {
 
335
            Integer row1 = (Integer) o1;
 
336
            Integer row2 = (Integer) o2;
 
337
            return tableModel.getNode(row1.intValue()).getName().compareToIgnoreCase(
 
338
                    tableModel.getNode(row2.intValue()).getName());
 
339
        }
 
340
    }
 
341
 
 
342
 
 
343
}