1 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 ; 54 import javax.swing.event.ListSelectionListener ; 55 import javax.swing.event.ListSelectionEvent ; 56 import javax.swing.event.AncestorListener ; 57 import javax.swing.event.AncestorEvent ; 58 import java.lang.reflect.InvocationTargetException ; 59 import java.awt.event.MouseListener ; 60 import java.awt.event.MouseEvent ; 61 import java.awt.event.ActionEvent ; 62 import java.awt.event.KeyEvent ; 63 import java.awt.Component ; 64 import java.awt.Color ; 65 import java.awt.Point ; 66 import java.util.*; 67 import java.io.File ; 68 import org.netbeans.modules.versioning.util.SystemActionBridge; 69 import org.netbeans.modules.versioning.spi.VCSContext; 70 71 76 class SyncTable implements MouseListener , ListSelectionListener , AncestorListener { 77 78 private NodeTableModel tableModel; 79 private JTable table; 80 private JScrollPane component; 81 private SyncFileNode [] nodes = new SyncFileNode[0]; 82 83 private String [] tableColumns; 84 private TableSorter sorter; 85 86 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 [] { 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 [] { 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 [] { 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 [] { 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 o1, Object o2) { 108 Node.Property p1 = (Node.Property) o1; 109 Node.Property p2 = (Node.Property) o2; 110 String sk1 = (String ) p1.getValue("sortkey"); if (sk1 != null) { 112 String sk2 = (String ) p2.getValue("sortkey"); return sk1.compareToIgnoreCase(sk2); 114 } else { 115 try { 116 String s1 = (String ) p1.getValue(); 117 String s2 = (String ) p2.getValue(); 118 return s1.compareToIgnoreCase(s2); 119 } catch (Exception 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; table.setRowHeight(height); 135 component = new JScrollPane(table, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER); 136 component.getViewport().setBackground(table.getBackground()); 137 Color borderColor = UIManager.getColor("scrollpane_border"); if (borderColor == null) borderColor = UIManager.getColor("controlShadow"); 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 [] { 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 e) { 151 showPopup(org.netbeans.modules.versioning.util.Utils.getPositionForPopup(table)); 152 } 153 }); 154 } 155 156 void setDefaultColumnSizes() { 157 SwingUtilities.invokeLater(new Runnable () { 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 event) { 182 setDefaultColumnSizes(); 183 } 184 185 public void ancestorMoved(AncestorEvent event) { 186 } 187 188 public void ancestorRemoved(AncestorEvent 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 209 final void setColumns(String [] 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 [] columns) { 225 Node.Property [] properties = new Node.Property[columns.length]; 226 for (int i = 0; i < columns.length; i++) { 227 String column = columns[i]; 228 String [] labels = (String []) columnLabels.get(column); 229 properties[i] = new ColumnDescriptor(column, String .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 name, Class type, String displayName, String shortDescription) { 246 super(name, type, displayName, shortDescription); 247 } 248 249 public Object getValue() throws IllegalAccessException , InvocationTargetException { 250 return null; 251 } 252 } 253 254 private void showPopup(MouseEvent 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 p) { 273 JPopupMenu menu = getPopup(); 274 menu.show(table, p.x, p.y); 275 } 276 277 297 private JPopupMenu getPopup() { 298 299 File [] 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"))); Mnemonics.setLocalizedText(item, item.getText()); 308 item = menu.add(new SystemActionBridge(SystemAction.get(UpdateAction.class), actionString("CTL_PopupMenuItem_Update"))); Mnemonics.setLocalizedText(item, item.getText()); 310 item = menu.add(new SystemActionBridge(SystemAction.get(CommitAction.class), actionString("CTL_PopupMenuItem_Commit"))); Mnemonics.setLocalizedText(item, item.getText()); 312 menu.add(new JSeparator()); 313 item = menu.add(new SystemActionBridge(SystemAction.get(TagAction.class), actionString("CTL_PopupMenuItem_Tag"))); Mnemonics.setLocalizedText(item, item.getText()); 315 item = menu.add(new SystemActionBridge(SystemAction.get(BranchAction.class), actionString("CTL_PopupMenuItem_Branch"))); 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") : actionString("CTL_PopupMenuItem_ShowAnnotations"))); Mnemonics.setLocalizedText(item, item.getText()); 323 item = menu.add(new SystemActionBridge(SystemAction.get(SearchHistoryAction.class), actionString("CTL_PopupMenuItem_SearchHistory"))); 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 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")); } else if (allLocallyDeleted) { 347 revertAction = new SystemActionBridge(SystemAction.get(GetCleanAction.class), actionString("CTL_PopupMenuItem_RevertDelete")); } else { 349 revertAction = new SystemActionBridge(SystemAction.get(GetCleanAction.class), actionString("CTL_PopupMenuItem_GetClean")); } 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"))); 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") : actionString("CTL_PopupMenuItem_Ignore")); item = menu.add(ignoreAction); 362 Mnemonics.setLocalizedText(item, item.getText()); 363 364 return menu; 365 } 366 367 371 private String actionString(String key) { 372 ResourceBundle actionsLoc = NbBundle.getBundle(Annotator.class); 373 return actionsLoc.getString(key); 374 } 375 376 public void mouseEntered(MouseEvent e) { 377 } 378 379 public void mouseExited(MouseEvent e) { 380 } 381 382 public void mousePressed(MouseEvent e) { 383 if (e.isPopupTrigger()) { 384 showPopup(e); 385 } 386 } 387 388 public void mouseReleased(MouseEvent e) { 389 if (e.isPopupTrigger()) { 390 showPopup(e); 391 } 392 } 393 394 public void mouseClicked(MouseEvent 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 (this, 0, "")); } 404 } 405 } 406 407 public void valueChanged(ListSelectionEvent 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; 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 { 428 429 private FilePathCellRenderer pathRenderer = new FilePathCellRenderer(); 430 431 public Component getTableCellRendererComponent(JTable table, Object 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(); } 439 if (CvsModuleConfig.getDefault().isExcludedFromCommit(node.getFile())) { 440 String nodeName = node.getDisplayName(); 441 if (isSelected) { 442 value = "<html><s>" + nodeName + "</s></html>"; } else { 444 value = "<html><s>" + CvsVersioningSystem.getInstance().getAnnotator().annotateNameHtml(nodeName, node.getFileInformation(), null) + "</s>"; } 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 path = nodes[sorter.modelIndex(row)].getFile().getAbsolutePath(); 455 ((JComponent) renderer).setToolTipText(path); 456 } 457 return renderer; 458 } 459 } 460 } 461 | Popular Tags |