KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > versioning > system > cvss > ui > actions > 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.versioning.system.cvss.ui.actions.commit;
21
22 import org.netbeans.modules.versioning.system.cvss.*;
23 import org.netbeans.modules.versioning.system.cvss.util.Utils;
24 import org.netbeans.modules.versioning.util.FilePathCellRenderer;
25 import org.netbeans.modules.versioning.util.TableSorter;
26 import org.openide.util.NbBundle;
27
28 import javax.swing.*;
29 import javax.swing.event.AncestorEvent JavaDoc;
30 import javax.swing.event.AncestorListener JavaDoc;
31 import javax.swing.event.TableModelListener JavaDoc;
32 import javax.swing.event.TableModelEvent JavaDoc;
33 import javax.swing.table.DefaultTableCellRenderer JavaDoc;
34 import javax.swing.table.TableModel JavaDoc;
35 import javax.swing.table.TableColumnModel JavaDoc;
36 import java.awt.Component JavaDoc;
37 import java.util.*;
38
39 /**
40  * View that displays nodes in the Synchronize view.
41  *
42  * @author Maros Sandor
43  */

44 class CommitTable implements AncestorListener JavaDoc, TableModelListener JavaDoc {
45
46     private CommitTableModel tableModel;
47     private JTable table;
48     private JComponent component;
49     
50     private TableSorter sorter;
51     private String JavaDoc[] columns;
52
53     public CommitTable(JLabel label) {
54         tableModel = new CommitTableModel();
55         tableModel.addTableModelListener(this);
56         sorter = new TableSorter(tableModel);
57         table = new JTable(sorter);
58         table.getTableHeader().setReorderingAllowed(false);
59         table.setDefaultRenderer(String JavaDoc.class, new CommitStringsCellRenderer());
60         table.setDefaultEditor(CommitOptions.class, new CommitOptionsCellEditor());
61         table.getTableHeader().setReorderingAllowed(true);
62         sorter.setTableHeader(table.getTableHeader());
63         int height = new JLabel("FONTSIZE").getPreferredSize().height * 6 / 5; // NOI18N
64
table.setRowHeight(height);
65         table.addAncestorListener(this);
66         component = new JScrollPane(table, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
67         label.setLabelFor(table);
68         table.getAccessibleContext().setAccessibleDescription(NbBundle.getMessage(CommitTable.class, "ACSD_CommitTable")); // NOI18N
69
}
70
71     public void ancestorAdded(AncestorEvent JavaDoc event) {
72         setDefaultColumnSizes();
73     }
74
75     /**
76      * Sets sizes of Commit table columns, kind of hardcoded.
77      */

78     private void setDefaultColumnSizes() {
79         int width = table.getWidth();
80         TableColumnModel JavaDoc columnModel = table.getColumnModel();
81         if (columns == null || columnModel == null) return; // unsure when this methed will be called (component realization)
82
if (columnModel.getColumnCount() != columns.length) return;
83         if (columns.length == 4) {
84             for (int i = 0; i < columns.length; i++) {
85                 String JavaDoc col = columns[i];
86                 sorter.setColumnComparator(i, null);
87                 sorter.setSortingStatus(i, TableSorter.NOT_SORTED);
88                 if (col.equals(CommitSettings.COLUMN_NAME_NAME)) {
89                     sorter.setColumnComparator(i, new FileNameComparator());
90                     columnModel.getColumn(i).setPreferredWidth(width * 30 / 100);
91                 } else if (col.equals(CommitSettings.COLUMN_NAME_STATUS)) {
92                     sorter.setColumnComparator(i, new StatusComparator());
93                     sorter.setSortingStatus(i, TableSorter.ASCENDING);
94                     columnModel.getColumn(i).setPreferredWidth(width * 15 / 100);
95                 } else if (col.equals(CommitSettings.COLUMN_NAME_ACTION)) {
96                     columnModel.getColumn(i).setPreferredWidth(width * 15 / 100);
97                 } else {
98                     columnModel.getColumn(i).setPreferredWidth(width * 40 / 100);
99                 }
100             }
101         } else if (columns.length == 5) {
102             for (int i = 0; i < columns.length; i++) {
103                 String JavaDoc col = columns[i];
104                 sorter.setColumnComparator(i, null);
105                 sorter.setSortingStatus(i, TableSorter.NOT_SORTED);
106                 if (col.equals(CommitSettings.COLUMN_NAME_NAME)) {
107                     sorter.setColumnComparator(i, new FileNameComparator());
108                     columnModel.getColumn(i).setPreferredWidth(width * 25 / 100);
109                 } else if (col.equals(CommitSettings.COLUMN_NAME_STICKY)) {
110                         columnModel.getColumn(i).setPreferredWidth(width * 15 / 100);
111                 } else if (col.equals(CommitSettings.COLUMN_NAME_STATUS)) {
112                     sorter.setColumnComparator(i, new StatusComparator());
113                     sorter.setSortingStatus(i, TableSorter.ASCENDING);
114                     columnModel.getColumn(i).setPreferredWidth(width * 15 / 100);
115                 } else if (col.equals(CommitSettings.COLUMN_NAME_ACTION)) {
116                     columnModel.getColumn(i).setPreferredWidth(width * 15 / 100);
117                 } else {
118                     columnModel.getColumn(i).setPreferredWidth(width * 30 / 100);
119                 }
120             }
121         }
122     }
123
124     public void ancestorMoved(AncestorEvent JavaDoc event) {
125     }
126
127     public void ancestorRemoved(AncestorEvent JavaDoc event) {
128     }
129     
130     void setColumns(String JavaDoc[] cols) {
131         if (Arrays.equals(columns, cols)) return;
132         columns = cols;
133         tableModel.setColumns(cols);
134         setDefaultColumnSizes();
135     }
136
137     void setNodes(CvsFileNode[] nodes) {
138         tableModel.setNodes(nodes);
139     }
140     
141     public CommitSettings.CommitFile[] getCommitFiles() {
142         return tableModel.getCommitFiles();
143     }
144     
145     public JComponent getComponent() {
146         return component;
147     }
148
149     void dataChanged() {
150         int idx = table.getSelectedRow();
151         tableModel.fireTableDataChanged();
152         if (idx != -1) table.getSelectionModel().addSelectionInterval(idx, idx);
153     }
154
155     TableModel JavaDoc getTableModel() {
156         return tableModel;
157     }
158
159     public void tableChanged(TableModelEvent JavaDoc e) {
160         // change in commit options may alter name rendering (strikethrough)
161
table.repaint();
162     }
163     
164     private class CommitOptionsCellEditor extends DefaultCellEditor {
165         
166         private final Object JavaDoc[] addOptions = new Object JavaDoc [] {
167                 CommitOptions.ADD_TEXT,
168                 CommitOptions.ADD_BINARY,
169                 CommitOptions.EXCLUDE
170             };
171         private final Object JavaDoc[] commitOptions = new Object JavaDoc [] {
172                 CommitOptions.COMMIT,
173                 CommitOptions.EXCLUDE
174             };
175
176         private final Object JavaDoc[] removeOptions = new Object JavaDoc [] {
177                 CommitOptions.COMMIT_REMOVE,
178                 CommitOptions.EXCLUDE
179             };
180
181         public CommitOptionsCellEditor() {
182             super(new JComboBox());
183         }
184
185         public Component JavaDoc getTableCellEditorComponent(JTable table, Object JavaDoc value, boolean isSelected, int row, int column) {
186             int fileStatus = tableModel.getCommitFile(sorter.modelIndex(row)).getNode().getInformation().getStatus();
187             JComboBox combo = (JComboBox) editorComponent;
188             if (fileStatus == FileInformation.STATUS_VERSIONED_DELETEDLOCALLY || fileStatus == FileInformation.STATUS_VERSIONED_REMOVEDLOCALLY) {
189                 combo.setModel(new DefaultComboBoxModel(removeOptions));
190             } else if ((fileStatus & FileInformation.STATUS_IN_REPOSITORY) == 0) {
191                 combo.setModel(new DefaultComboBoxModel(addOptions));
192             } else {
193                 combo.setModel(new DefaultComboBoxModel(commitOptions));
194             }
195             return super.getTableCellEditorComponent(table, value, isSelected, row, column);
196         }
197     }
198
199     private static class CommitStringsCellRenderer extends DefaultTableCellRenderer JavaDoc {
200
201         private FilePathCellRenderer pathRenderer = new FilePathCellRenderer();
202
203         public Component JavaDoc getTableCellRendererComponent(JTable table, Object JavaDoc value, boolean isSelected, boolean hasFocus, int row, int column) {
204             int col = table.convertColumnIndexToModel(column);
205             if (col == 0) {
206                 TableSorter sorter = (TableSorter) table.getModel();
207                 CommitTableModel model = (CommitTableModel) sorter.getTableModel();
208                 CommitSettings.CommitFile commitFile = model.getCommitFile(sorter.modelIndex(row));
209                 if (!isSelected) {
210                     value = "<html>" + CvsVersioningSystem.getInstance().getAnnotator().annotateNameHtml( // NOI18N
211
commitFile.getNode().getFile().getName(), commitFile.getNode().getInformation(), null);
212                 }
213                 if (commitFile.getOptions() == CommitOptions.EXCLUDE) {
214                     value = "<html><s>" + value + "</s></html>"; // NOI18N
215
}
216                 return super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
217             } else if (col == 3) {
218                 return pathRenderer.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
219             } else {
220                 return super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
221             }
222         }
223     }
224     
225     private class StatusComparator extends Utils.ByImportanceComparator {
226         public int compare(Object JavaDoc o1, Object JavaDoc o2) {
227             Integer JavaDoc row1 = (Integer JavaDoc) o1;
228             Integer JavaDoc row2 = (Integer JavaDoc) o2;
229             return super.compare(tableModel.getCommitFile(row1.intValue()).getNode().getInformation(),
230                                  tableModel.getCommitFile(row2.intValue()).getNode().getInformation());
231         }
232     }
233     
234     private class FileNameComparator implements Comparator {
235         public int compare(Object JavaDoc o1, Object JavaDoc o2) {
236             Integer JavaDoc row1 = (Integer JavaDoc) o1;
237             Integer JavaDoc row2 = (Integer JavaDoc) o2;
238             return tableModel.getCommitFile(row1.intValue()).getNode().getName().compareToIgnoreCase(
239                     tableModel.getCommitFile(row2.intValue()).getNode().getName());
240         }
241     }
242 }
243
Popular Tags