KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > versioning > system > cvss > ui > syncview > SyncTable


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.syncview;
21
22 import org.openide.explorer.view.NodeTableModel;
23 import org.openide.nodes.*;
24 import org.openide.nodes.PropertySupport.ReadOnly;
25 import org.openide.windows.TopComponent;
26 import org.openide.util.NbBundle;
27 import org.openide.util.actions.SystemAction;
28 import org.openide.ErrorManager;
29 import org.openide.awt.MouseUtils;
30 import org.openide.awt.Mnemonics;
31 import org.netbeans.modules.versioning.system.cvss.Annotator;
32 import org.netbeans.modules.versioning.system.cvss.FileStatusCache;
33 import org.netbeans.modules.versioning.system.cvss.CvsVersioningSystem;
34 import org.netbeans.modules.versioning.system.cvss.FileInformation;
35 import org.netbeans.modules.versioning.system.cvss.ui.actions.DeleteLocalAction;
36 import org.netbeans.modules.versioning.system.cvss.ui.actions.ignore.IgnoreAction;
37 import org.netbeans.modules.versioning.system.cvss.ui.actions.log.AnnotationsAction;
38 import org.netbeans.modules.versioning.system.cvss.ui.actions.log.SearchHistoryAction;
39 import org.netbeans.modules.versioning.system.cvss.ui.actions.tag.TagAction;
40 import org.netbeans.modules.versioning.system.cvss.ui.actions.tag.BranchAction;
41 import org.netbeans.modules.versioning.system.cvss.ui.actions.commit.CommitAction;
42 import org.netbeans.modules.versioning.system.cvss.ui.actions.commit.ExcludeFromCommitAction;
43 import org.netbeans.modules.versioning.system.cvss.ui.actions.update.UpdateAction;
44 import org.netbeans.modules.versioning.system.cvss.ui.actions.update.GetCleanAction;
45 import org.netbeans.modules.versioning.system.cvss.ui.actions.diff.DiffAction;
46 import org.netbeans.modules.versioning.system.cvss.ui.actions.diff.ResolveConflictsAction;
47 import org.netbeans.modules.versioning.system.cvss.CvsModuleConfig;
48 import org.netbeans.modules.versioning.system.cvss.util.Utils;
49 import org.netbeans.modules.versioning.util.FilePathCellRenderer;
50 import org.netbeans.modules.versioning.util.TableSorter;
51
52 import javax.swing.*;
53 import javax.swing.table.DefaultTableCellRenderer JavaDoc;
54 import javax.swing.event.ListSelectionListener JavaDoc;
55 import javax.swing.event.ListSelectionEvent JavaDoc;
56 import javax.swing.event.AncestorListener JavaDoc;
57 import javax.swing.event.AncestorEvent JavaDoc;
58 import java.lang.reflect.InvocationTargetException JavaDoc;
59 import java.awt.event.MouseListener JavaDoc;
60 import java.awt.event.MouseEvent JavaDoc;
61 import java.awt.event.ActionEvent JavaDoc;
62 import java.awt.event.KeyEvent JavaDoc;
63 import java.awt.Component JavaDoc;
64 import java.awt.Color JavaDoc;
65 import java.awt.Point JavaDoc;
66 import java.util.*;
67 import java.io.File JavaDoc;
68 import org.netbeans.modules.versioning.util.SystemActionBridge;
69 import org.netbeans.modules.versioning.spi.VCSContext;
70
71 /**
72  * Table that displays nodes in the Versioning view.
73  *
74  * @author Maros Sandor
75  */

76 class SyncTable implements MouseListener JavaDoc, ListSelectionListener JavaDoc, AncestorListener JavaDoc {
77
78     private NodeTableModel tableModel;
79     private JTable table;
80     private JScrollPane component;
81     private SyncFileNode [] nodes = new SyncFileNode[0];
82     
83     private String JavaDoc [] tableColumns;
84     private TableSorter sorter;
85
86     /**
87      * Defines labels for Versioning view table columns.
88      */

89     private static final Map columnLabels = new HashMap(4);
90     {
91         ResourceBundle loc = NbBundle.getBundle(SyncTable.class);
92         columnLabels.put(SyncFileNode.COLUMN_NAME_STICKY, new String JavaDoc [] {
93                                           loc.getString("CTL_VersioningView_Column_Sticky_Title"),
94                                           loc.getString("CTL_VersioningView_Column_Sticky_Desc")});
95         columnLabels.put(SyncFileNode.COLUMN_NAME_NAME, new String JavaDoc [] {
96                                           loc.getString("CTL_VersioningView_Column_File_Title"),
97                                           loc.getString("CTL_VersioningView_Column_File_Desc")});
98         columnLabels.put(SyncFileNode.COLUMN_NAME_STATUS, new String JavaDoc [] {
99                                           loc.getString("CTL_VersioningView_Column_Status_Title"),
100                                           loc.getString("CTL_VersioningView_Column_Status_Desc")});
101         columnLabels.put(SyncFileNode.COLUMN_NAME_PATH, new String JavaDoc [] {
102                                           loc.getString("CTL_VersioningView_Column_Path_Title"),
103                                           loc.getString("CTL_VersioningView_Column_Path_Desc")});
104     }
105
106     private static final Comparator NodeComparator = new Comparator() {
107         public int compare(Object JavaDoc o1, Object JavaDoc o2) {
108             Node.Property p1 = (Node.Property) o1;
109             Node.Property p2 = (Node.Property) o2;
110             String JavaDoc sk1 = (String JavaDoc) p1.getValue("sortkey"); // NOI18N
111
if (sk1 != null) {
112                 String JavaDoc sk2 = (String JavaDoc) p2.getValue("sortkey"); // NOI18N
113
return sk1.compareToIgnoreCase(sk2);
114             } else {
115                 try {
116                     String JavaDoc s1 = (String JavaDoc) p1.getValue();
117                     String JavaDoc s2 = (String JavaDoc) p2.getValue();
118                     return s1.compareToIgnoreCase(s2);
119                 } catch (Exception JavaDoc e) {
120                     ErrorManager.getDefault().notify(e);
121                     return 0;
122                 }
123             }
124         }
125     };
126     
127     public SyncTable() {
128         tableModel = new NodeTableModel();
129         sorter = new TableSorter(tableModel);
130         sorter.setColumnComparator(Node.Property.class, NodeComparator);
131         table = new JTable(sorter);
132         sorter.setTableHeader(table.getTableHeader());
133         int height = new JLabel("FONTSIZE").getPreferredSize().height * 6 / 5; // NOI18N
134
table.setRowHeight(height);
135         component = new JScrollPane(table, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
136         component.getViewport().setBackground(table.getBackground());
137         Color JavaDoc borderColor = UIManager.getColor("scrollpane_border"); // NOI18N
138
if (borderColor == null) borderColor = UIManager.getColor("controlShadow"); // NOI18N
139
component.setBorder(BorderFactory.createMatteBorder(1, 0, 0, 0, borderColor));
140         table.addMouseListener(this);
141         table.setDefaultRenderer(Node.Property.class, new SyncTableCellRenderer());
142         table.getSelectionModel().addListSelectionListener(this);
143         table.addAncestorListener(this);
144         table.getAccessibleContext().setAccessibleName(NbBundle.getMessage(SyncTable.class, "ACSN_VersioningTable"));
145         table.getAccessibleContext().setAccessibleDescription(NbBundle.getMessage(SyncTable.class, "ACSD_VersioningTable"));
146         setColumns(new String JavaDoc [] { SyncFileNode.COLUMN_NAME_NAME, SyncFileNode.COLUMN_NAME_STATUS, SyncFileNode.COLUMN_NAME_PATH });
147         table.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT ).put(
148                 KeyStroke.getKeyStroke(KeyEvent.VK_F10, KeyEvent.SHIFT_DOWN_MASK ), "org.openide.actions.PopupAction");
149         table.getActionMap().put("org.openide.actions.PopupAction", new AbstractAction() {
150             public void actionPerformed(ActionEvent JavaDoc e) {
151                 showPopup(org.netbeans.modules.versioning.util.Utils.getPositionForPopup(table));
152             }
153         });
154     }
155
156     void setDefaultColumnSizes() {
157         SwingUtilities.invokeLater(new Runnable JavaDoc() {
158             public void run() {
159                 int width = table.getWidth();
160                 if (tableColumns.length == 3) {
161                     for (int i = 0; i < tableColumns.length; i++) {
162                         if (SyncFileNode.COLUMN_NAME_PATH.equals(tableColumns[i])) {
163                             table.getColumnModel().getColumn(i).setPreferredWidth(width * 60 / 100);
164                         } else {
165                             table.getColumnModel().getColumn(i).setPreferredWidth(width * 20 / 100);
166                         }
167                     }
168                 } else if (tableColumns.length == 4) {
169                     for (int i = 0; i < tableColumns.length; i++) {
170                         if (SyncFileNode.COLUMN_NAME_PATH.equals(tableColumns[i])) {
171                             table.getColumnModel().getColumn(i).setPreferredWidth(width * 55 / 100);
172                         } else {
173                             table.getColumnModel().getColumn(i).setPreferredWidth(width * 15 / 100);
174                         }
175                     }
176                 }
177             }
178         });
179     }
180
181     public void ancestorAdded(AncestorEvent JavaDoc event) {
182         setDefaultColumnSizes();
183     }
184
185     public void ancestorMoved(AncestorEvent JavaDoc event) {
186     }
187
188     public void ancestorRemoved(AncestorEvent JavaDoc event) {
189     }
190
191     public SyncFileNode [] getDisplayedNodes() {
192         int n = sorter.getRowCount();
193         SyncFileNode [] ret = new SyncFileNode[n];
194         for (int i = 0; i < n; i++) {
195             ret[i] = nodes[sorter.modelIndex(i)];
196         }
197         return ret;
198     }
199
200     public JComponent getComponent() {
201         return component;
202     }
203     
204     /**
205      * Sets visible columns in the Versioning table.
206      *
207      * @param columns array of column names, they must be one of SyncFileNode.COLUMN_NAME_XXXXX constants.
208      */

209     final void setColumns(String JavaDoc [] columns) {
210         if (Arrays.equals(columns, tableColumns)) return;
211         setDefaultColumnSizes();
212         setModelProperties(columns);
213         tableColumns = columns;
214         for (int i = 0; i < tableColumns.length; i++) {
215             sorter.setColumnComparator(i, null);
216             sorter.setSortingStatus(i, TableSorter.NOT_SORTED);
217             if (SyncFileNode.COLUMN_NAME_STATUS.equals(tableColumns[i])) {
218                 sorter.setSortingStatus(i, TableSorter.ASCENDING);
219                 break;
220             }
221         }
222     }
223         
224     private void setModelProperties(String JavaDoc [] columns) {
225         Node.Property [] properties = new Node.Property[columns.length];
226         for (int i = 0; i < columns.length; i++) {
227             String JavaDoc column = columns[i];
228             String JavaDoc [] labels = (String JavaDoc[]) columnLabels.get(column);
229             properties[i] = new ColumnDescriptor(column, String JavaDoc.class, labels[0], labels[1]);
230         }
231         tableModel.setProperties(properties);
232     }
233
234     void setTableModel(SyncFileNode [] nodes) {
235         this.nodes = nodes;
236         tableModel.setNodes(nodes);
237     }
238
239     void focus() {
240         table.requestFocus();
241     }
242
243     private static class ColumnDescriptor extends ReadOnly {
244         
245         public ColumnDescriptor(String JavaDoc name, Class JavaDoc type, String JavaDoc displayName, String JavaDoc shortDescription) {
246             super(name, type, displayName, shortDescription);
247         }
248
249         public Object JavaDoc getValue() throws IllegalAccessException JavaDoc, InvocationTargetException JavaDoc {
250             return null;
251         }
252     }
253
254     private void showPopup(MouseEvent JavaDoc e) {
255         int row = table.rowAtPoint(e.getPoint());
256         if (row != -1) {
257             boolean makeRowSelected = true;
258             int [] selectedrows = table.getSelectedRows();
259             for (int i = 0; i < selectedrows.length; i++) {
260                 if (row == selectedrows[i]) {
261                     makeRowSelected = false;
262                     break;
263                 }
264             }
265             if (makeRowSelected) {
266                 table.getSelectionModel().setSelectionInterval(row, row);
267             }
268         }
269         showPopup(e.getPoint());
270     }
271     
272     private void showPopup(Point JavaDoc p) {
273         JPopupMenu menu = getPopup();
274         menu.show(table, p.x, p.y);
275     }
276
277     /**
278      * Constructs contextual Menu: File Node
279         <pre>
280         Open
281         -------------------
282         Diff (default action)
283         Update
284         Commit...
285         --------------------
286         Tag...
287         Branch...
288         --------------------
289         Show Annotations...
290         Show History...
291         --------------------
292         Exclude from Commit (Include in Commit)
293         Revert Modifications (Revert Delete)(Delete)
294         Ignore (Unignore)
295         </pre>
296      */

297     private JPopupMenu getPopup() {
298
299         File JavaDoc [] files = Utils.getCurrentContext(null).getFiles();
300         JPopupMenu menu = new JPopupMenu();
301         JMenuItem item;
302         
303         item = menu.add(new OpenInEditorAction());
304         Mnemonics.setLocalizedText(item, item.getText());
305         menu.add(new JSeparator());
306         item = menu.add(new SystemActionBridge(SystemAction.get(DiffAction.class), actionString("CTL_PopupMenuItem_Diff"))); // NOI18N
307
Mnemonics.setLocalizedText(item, item.getText());
308         item = menu.add(new SystemActionBridge(SystemAction.get(UpdateAction.class), actionString("CTL_PopupMenuItem_Update"))); // NOI18N
309
Mnemonics.setLocalizedText(item, item.getText());
310         item = menu.add(new SystemActionBridge(SystemAction.get(CommitAction.class), actionString("CTL_PopupMenuItem_Commit"))); // NOI18N
311
Mnemonics.setLocalizedText(item, item.getText());
312         menu.add(new JSeparator());
313         item = menu.add(new SystemActionBridge(SystemAction.get(TagAction.class), actionString("CTL_PopupMenuItem_Tag"))); // NOI18N
314
Mnemonics.setLocalizedText(item, item.getText());
315         item = menu.add(new SystemActionBridge(SystemAction.get(BranchAction.class), actionString("CTL_PopupMenuItem_Branch"))); // NOI18N
316
Mnemonics.setLocalizedText(item, item.getText());
317         menu.add(new JSeparator());
318         item = menu.add(new SystemActionBridge(SystemAction.get(AnnotationsAction.class),
319                                                ((AnnotationsAction)SystemAction.get(AnnotationsAction.class)).visible(null) ?
320                                                actionString("CTL_PopupMenuItem_HideAnnotations") : // NOI18N
321
actionString("CTL_PopupMenuItem_ShowAnnotations"))); // NOI18N
322
Mnemonics.setLocalizedText(item, item.getText());
323         item = menu.add(new SystemActionBridge(SystemAction.get(SearchHistoryAction.class), actionString("CTL_PopupMenuItem_SearchHistory"))); // NOI18N
324
Mnemonics.setLocalizedText(item, item.getText());
325         menu.add(new JSeparator());
326         item = org.netbeans.modules.versioning.util.Utils.toMenuItem(new ExcludeFromCommitAction(VCSContext.forFiles(new HashSet(Arrays.asList(files)))));
327         menu.add(item);
328
329         Action revertAction;
330         boolean allLocallyNew = true;
331         boolean allLocallyDeleted = true;
332         FileStatusCache cache = CvsVersioningSystem.getInstance().getStatusCache();
333         for (int i = 0; i < files.length; i++) {
334             File JavaDoc file = files[i];
335             FileInformation info = cache.getStatus(file);
336             if ((info.getStatus() & DeleteLocalAction.LOCALLY_DELETABLE_MASK) == 0 ) {
337                 allLocallyNew = false;
338             }
339             if (info.getStatus() != FileInformation.STATUS_VERSIONED_DELETEDLOCALLY && info.getStatus() != FileInformation.STATUS_VERSIONED_REMOVEDLOCALLY) {
340                 allLocallyDeleted = false;
341             }
342         }
343         if (allLocallyNew) {
344             SystemAction systemAction = SystemAction.get(DeleteLocalAction.class);
345             revertAction = new SystemActionBridge(systemAction, actionString("CTL_PopupMenuItem_Delete")); // NOI18N
346
} else if (allLocallyDeleted) {
347             revertAction = new SystemActionBridge(SystemAction.get(GetCleanAction.class), actionString("CTL_PopupMenuItem_RevertDelete")); // NOI18N
348
} else {
349             revertAction = new SystemActionBridge(SystemAction.get(GetCleanAction.class), actionString("CTL_PopupMenuItem_GetClean")); // NOI18N
350
}
351         item = menu.add(revertAction);
352         Mnemonics.setLocalizedText(item, item.getText());
353
354         item = menu.add(new SystemActionBridge(SystemAction.get(ResolveConflictsAction.class), actionString("CTL_PopupMenuItem_ResolveConflicts"))); // NOI18N
355
Mnemonics.setLocalizedText(item, item.getText());
356         
357         Action ignoreAction = new SystemActionBridge(SystemAction.get(IgnoreAction.class),
358            ((IgnoreAction)SystemAction.get(IgnoreAction.class)).getActionStatus(files) == IgnoreAction.UNIGNORING ?
359            actionString("CTL_PopupMenuItem_Unignore") : // NOI18N
360
actionString("CTL_PopupMenuItem_Ignore")); // NOI18N
361
item = menu.add(ignoreAction);
362         Mnemonics.setLocalizedText(item, item.getText());
363
364         return menu;
365     }
366
367     /**
368      * Workaround.
369      * I18N Test Wizard searches for keys in syncview package Bundle.properties
370      */

371     private String JavaDoc actionString(String JavaDoc key) {
372         ResourceBundle actionsLoc = NbBundle.getBundle(Annotator.class);
373         return actionsLoc.getString(key);
374     }
375     
376     public void mouseEntered(MouseEvent JavaDoc e) {
377     }
378
379     public void mouseExited(MouseEvent JavaDoc e) {
380     }
381
382     public void mousePressed(MouseEvent JavaDoc e) {
383         if (e.isPopupTrigger()) {
384             showPopup(e);
385         }
386     }
387
388     public void mouseReleased(MouseEvent JavaDoc e) {
389         if (e.isPopupTrigger()) {
390             showPopup(e);
391         }
392     }
393
394     public void mouseClicked(MouseEvent JavaDoc e) {
395         if (SwingUtilities.isLeftMouseButton(e) && MouseUtils.isDoubleClick(e)) {
396             int row = table.rowAtPoint(e.getPoint());
397             if (row == -1) return;
398             row = sorter.modelIndex(row);
399             Action action = nodes[row].getPreferredAction();
400             if (action == null || !action.isEnabled()) action = new OpenInEditorAction();
401             if (action.isEnabled()) {
402                 action.actionPerformed(new ActionEvent JavaDoc(this, 0, "")); // NOI18N
403
}
404         }
405     }
406
407     public void valueChanged(ListSelectionEvent JavaDoc e) {
408         List selectedNodes = new ArrayList();
409         ListSelectionModel selectionModel = table.getSelectionModel();
410         TopComponent tc = (TopComponent) SwingUtilities.getAncestorOfClass(TopComponent.class, table);
411         if (tc == null) return; // table is no longer in component hierarchy
412

413         int min = selectionModel.getMinSelectionIndex();
414         if (min == -1) {
415             tc.setActivatedNodes(new Node[0]);
416         }
417         int max = selectionModel.getMaxSelectionIndex();
418         for (int i = min; i <= max; i++) {
419             if (selectionModel.isSelectedIndex(i)) {
420                 int idx = sorter.modelIndex(i);
421                 selectedNodes.add(nodes[idx]);
422             }
423         }
424         tc.setActivatedNodes((Node[]) selectedNodes.toArray(new Node[selectedNodes.size()]));
425     }
426     
427     private class SyncTableCellRenderer extends DefaultTableCellRenderer JavaDoc {
428         
429         private FilePathCellRenderer pathRenderer = new FilePathCellRenderer();
430         
431         public Component getTableCellRendererComponent(JTable table, Object JavaDoc value, boolean isSelected, boolean hasFocus, int row, int column) {
432             Component renderer;
433             int modelColumnIndex = table.convertColumnIndexToModel(column);
434             if (modelColumnIndex == 0) {
435                 SyncFileNode node = nodes[sorter.modelIndex(row)];
436                 if (!isSelected) {
437                     value = "<html>" + node.getHtmlDisplayName(); // NOI18N
438
}
439                 if (CvsModuleConfig.getDefault().isExcludedFromCommit(node.getFile())) {
440                     String JavaDoc nodeName = node.getDisplayName();
441                     if (isSelected) {
442                         value = "<html><s>" + nodeName + "</s></html>"; // NOI18N
443
} else {
444                         value = "<html><s>" + CvsVersioningSystem.getInstance().getAnnotator().annotateNameHtml(nodeName, node.getFileInformation(), null) + "</s>"; // NOI18N
445
}
446                 }
447             }
448             if (modelColumnIndex == 2) {
449                 renderer = pathRenderer.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
450             } else {
451                 renderer = super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
452             }
453             if (renderer instanceof JComponent) {
454                 String JavaDoc path = nodes[sorter.modelIndex(row)].getFile().getAbsolutePath();
455                 ((JComponent) renderer).setToolTipText(path);
456             }
457             return renderer;
458         }
459     }
460 }
461
Popular Tags