KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > versioning > system > cvss > ui > actions > update > UpdateResultsTable


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-2007 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19 package org.netbeans.modules.versioning.system.cvss.ui.actions.update;
20
21 import org.openide.explorer.view.NodeTableModel;
22 import org.openide.util.NbBundle;
23 import org.openide.nodes.Node;
24 import org.openide.nodes.PropertySupport;
25 import org.openide.ErrorManager;
26 import org.openide.windows.TopComponent;
27 import org.openide.awt.MouseUtils;
28 import org.netbeans.modules.versioning.system.cvss.ui.syncview.OpenInEditorAction;
29 import org.netbeans.modules.versioning.util.TableSorter;
30 import org.netbeans.modules.versioning.util.FilePathCellRenderer;
31
32 import javax.swing.event.ListSelectionListener JavaDoc;
33 import javax.swing.event.AncestorListener JavaDoc;
34 import javax.swing.event.AncestorEvent JavaDoc;
35 import javax.swing.event.ListSelectionEvent JavaDoc;
36 import javax.swing.*;
37 import javax.swing.table.DefaultTableCellRenderer JavaDoc;
38 import java.awt.event.MouseListener JavaDoc;
39 import java.awt.event.ActionEvent JavaDoc;
40 import java.awt.event.MouseEvent JavaDoc;
41 import java.awt.Color JavaDoc;
42 import java.awt.Component JavaDoc;
43 import java.util.*;
44 import java.lang.reflect.InvocationTargetException JavaDoc;
45
46 /**
47  * Table that displays nodes in the Update Results view.
48  *
49  * @author Maros Sandor
50  */

51 class UpdateResultsTable implements MouseListener JavaDoc, ListSelectionListener JavaDoc, AncestorListener JavaDoc {
52
53     private NodeTableModel tableModel;
54     private JTable table;
55     private JScrollPane component;
56     private UpdateResultNode[] nodes = new UpdateResultNode[0];
57     
58     private String JavaDoc [] tableColumns;
59     private TableSorter sorter;
60
61     /**
62      * Defines labels for Versioning view table columns.
63      */

64     private final Map<String JavaDoc, String JavaDoc[]> columnLabels = new HashMap<String JavaDoc, String JavaDoc[]>(4);
65     {
66         ResourceBundle loc = NbBundle.getBundle(UpdateResultsTable.class);
67         columnLabels.put(UpdateResultNode.COLUMN_NAME_NAME, new String JavaDoc [] {
68                                           loc.getString("CTL_UpdateResults_Column_File_Title"), // NOI18N
69
loc.getString("CTL_UpdateResults_Column_File_Desc")}); // NOI18N
70
columnLabels.put(UpdateResultNode.COLUMN_NAME_STATUS, new String JavaDoc [] {
71                                           loc.getString("CTL_UpdateResults_Column_Status_Title"), // NOI18N
72
loc.getString("CTL_UpdateResults_Column_Status_Desc")}); // NOI18N
73
columnLabels.put(UpdateResultNode.COLUMN_NAME_PATH, new String JavaDoc [] {
74                                           loc.getString("CTL_UpdateResults_Column_Path_Title"), // NOI18N
75
loc.getString("CTL_UpdateResults_Column_Path_Desc")}); // NOI18N
76
}
77
78     private static final Comparator NodeComparator = new Comparator() {
79         public int compare(Object JavaDoc o1, Object JavaDoc o2) {
80             Node.Property p1 = (Node.Property) o1;
81             Node.Property p2 = (Node.Property) o2;
82             String JavaDoc sk1 = (String JavaDoc) p1.getValue("sortkey"); // NOI18N
83
if (sk1 != null) {
84                 String JavaDoc sk2 = (String JavaDoc) p2.getValue("sortkey"); // NOI18N
85
return sk1.compareToIgnoreCase(sk2);
86             } else {
87                 try {
88                     String JavaDoc s1 = (String JavaDoc) p1.getValue();
89                     String JavaDoc s2 = (String JavaDoc) p2.getValue();
90                     return s1.compareToIgnoreCase(s2);
91                 } catch (Exception JavaDoc e) {
92                     ErrorManager.getDefault().notify(e);
93                     return 0;
94                 }
95             }
96         }
97     };
98     
99     public UpdateResultsTable() {
100         tableModel = new NodeTableModel();
101         sorter = new TableSorter(tableModel);
102         sorter.setColumnComparator(Node.Property.class, NodeComparator);
103         table = new JTable(sorter);
104         sorter.setTableHeader(table.getTableHeader());
105         int height = new JLabel("FONTSIZE").getPreferredSize().height * 6 / 5; // NOI18N
106
table.setRowHeight(height);
107         component = new JScrollPane(table, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
108         component.getViewport().setBackground(table.getBackground());
109         Color JavaDoc borderColor = UIManager.getColor("scrollpane_border"); // NOI18N
110
if (borderColor == null) borderColor = UIManager.getColor("controlShadow"); // NOI18N
111
component.setBorder(BorderFactory.createMatteBorder(1, 0, 0, 0, borderColor));
112         table.addMouseListener(this);
113         table.setDefaultRenderer(Node.Property.class, new UpdateResultsTable.SyncTableCellRenderer());
114         table.getSelectionModel().addListSelectionListener(this);
115         table.addAncestorListener(this);
116         table.getAccessibleContext().setAccessibleName(NbBundle.getMessage(UpdateResultsTable.class, "ACSN_UpdateResults")); // NOI18N
117
table.getAccessibleContext().setAccessibleDescription(NbBundle.getMessage(UpdateResultsTable.class, "ACSD_UpdateResults")); // NOI18N
118
setColumns(new String JavaDoc [] { UpdateResultNode.COLUMN_NAME_NAME, UpdateResultNode.COLUMN_NAME_STATUS, UpdateResultNode.COLUMN_NAME_PATH });
119     }
120
121     void setDefaultColumnSizes() {
122         SwingUtilities.invokeLater(new Runnable JavaDoc() {
123             public void run() {
124                 int width = table.getWidth();
125                 for (int i = 0; i < tableColumns.length; i++) {
126                     if (UpdateResultNode.COLUMN_NAME_PATH.equals(tableColumns[i])) {
127                         table.getColumnModel().getColumn(i).setPreferredWidth(width * 60 / 100);
128                     } else {
129                         table.getColumnModel().getColumn(i).setPreferredWidth(width * 20 / 100);
130                     }
131                 }
132             }
133         });
134     }
135
136     public void ancestorAdded(AncestorEvent JavaDoc event) {
137         setDefaultColumnSizes();
138     }
139
140     public void ancestorMoved(AncestorEvent JavaDoc event) {
141     }
142
143     public void ancestorRemoved(AncestorEvent JavaDoc event) {
144     }
145
146     public UpdateResultNode [] getDisplayedNodes() {
147         int n = sorter.getRowCount();
148         UpdateResultNode [] ret = new UpdateResultNode[n];
149         for (int i = 0; i < n; i++) {
150             ret[i] = nodes[sorter.modelIndex(i)];
151         }
152         return ret;
153     }
154
155     public JComponent getComponent() {
156         return component;
157     }
158     
159     /**
160      * Sets visible columns in the Versioning table.
161      *
162      * @param columns array of column names, they must be one of SyncFileNode.COLUMN_NAME_XXXXX constants.
163      */

164     final void setColumns(String JavaDoc [] columns) {
165         if (Arrays.equals(columns, tableColumns)) return;
166         setDefaultColumnSizes();
167         setModelProperties(columns);
168         tableColumns = columns;
169         for (int i = 0; i < tableColumns.length; i++) {
170             sorter.setColumnComparator(i, null);
171             sorter.setSortingStatus(i, TableSorter.NOT_SORTED);
172             if (UpdateResultNode.COLUMN_NAME_STATUS.equals(tableColumns[i])) {
173                 sorter.setSortingStatus(i, TableSorter.ASCENDING);
174                 break;
175             }
176         }
177     }
178         
179     private void setModelProperties(String JavaDoc [] columns) {
180         Node.Property [] properties = new Node.Property[columns.length];
181         for (int i = 0; i < columns.length; i++) {
182             String JavaDoc column = columns[i];
183             String JavaDoc [] labels = (String JavaDoc[]) columnLabels.get(column);
184             properties[i] = new UpdateResultsTable.ColumnDescriptor(column, String JavaDoc.class, labels[0], labels[1]);
185         }
186         tableModel.setProperties(properties);
187     }
188
189     void setTableModel(UpdateResultNode [] nodes) {
190         this.nodes = nodes;
191         tableModel.setNodes(nodes);
192     }
193
194     void focus() {
195         table.requestFocus();
196     }
197
198     private static class ColumnDescriptor extends PropertySupport.ReadOnly {
199         
200         public ColumnDescriptor(String JavaDoc name, Class JavaDoc type, String JavaDoc displayName, String JavaDoc shortDescription) {
201             super(name, type, displayName, shortDescription);
202         }
203
204         public Object JavaDoc getValue() throws IllegalAccessException JavaDoc, InvocationTargetException JavaDoc {
205             return null;
206         }
207     }
208
209     public void mouseEntered(MouseEvent JavaDoc e) {
210     }
211
212     public void mouseExited(MouseEvent JavaDoc e) {
213     }
214
215     public void mousePressed(MouseEvent JavaDoc e) {
216     }
217
218     public void mouseReleased(MouseEvent JavaDoc e) {
219     }
220
221     public void mouseClicked(MouseEvent JavaDoc e) {
222         if (SwingUtilities.isLeftMouseButton(e) && MouseUtils.isDoubleClick(e)) {
223             int row = table.rowAtPoint(e.getPoint());
224             if (row == -1) return;
225             row = sorter.modelIndex(row);
226             Action action = nodes[row].getPreferredAction();
227             if (action == null || !action.isEnabled()) action = new OpenInEditorAction();
228             if (action.isEnabled()) {
229                 action.actionPerformed(new ActionEvent JavaDoc(this, 0, "")); // NOI18N
230
}
231         }
232     }
233
234     public void valueChanged(ListSelectionEvent JavaDoc e) {
235         List<Node> selectedNodes = new ArrayList<Node>();
236         ListSelectionModel selectionModel = table.getSelectionModel();
237         TopComponent tc = (TopComponent) SwingUtilities.getAncestorOfClass(TopComponent.class, table);
238         if (tc == null) return; // table is no longer in component hierarchy
239

240         int min = selectionModel.getMinSelectionIndex();
241         if (min == -1) {
242             tc.setActivatedNodes(new Node[0]);
243         }
244         int max = selectionModel.getMaxSelectionIndex();
245         for (int i = min; i <= max; i++) {
246             if (selectionModel.isSelectedIndex(i)) {
247                 int idx = sorter.modelIndex(i);
248                 selectedNodes.add(nodes[idx]);
249             }
250         }
251         tc.setActivatedNodes((Node[]) selectedNodes.toArray(new Node[selectedNodes.size()]));
252     }
253     
254     private class SyncTableCellRenderer extends DefaultTableCellRenderer JavaDoc {
255         
256         private FilePathCellRenderer pathRenderer = new FilePathCellRenderer();
257         
258         public Component getTableCellRendererComponent(JTable table, Object JavaDoc value, boolean isSelected, boolean hasFocus, int row, int column) {
259             Component renderer;
260             int modelColumnIndex = table.convertColumnIndexToModel(column);
261             if (modelColumnIndex == 0) {
262                 UpdateResultNode node = nodes[sorter.modelIndex(row)];
263                 if (!isSelected) {
264                     value = "<html>" + node.getHtmlDisplayName(); // NOI18N
265
}
266             }
267             if (modelColumnIndex == 2) {
268                 renderer = pathRenderer.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
269             } else {
270                 renderer = super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
271             }
272             if (renderer instanceof JComponent) {
273                 String JavaDoc path = nodes[sorter.modelIndex(row)].getInfo().getFile().getAbsolutePath();
274                 ((JComponent) renderer).setToolTipText(path);
275             }
276             return renderer;
277         }
278     }
279 }
280
Popular Tags