1 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 ; 33 import javax.swing.event.AncestorListener ; 34 import javax.swing.event.AncestorEvent ; 35 import javax.swing.event.ListSelectionEvent ; 36 import javax.swing.*; 37 import javax.swing.table.DefaultTableCellRenderer ; 38 import java.awt.event.MouseListener ; 39 import java.awt.event.ActionEvent ; 40 import java.awt.event.MouseEvent ; 41 import java.awt.Color ; 42 import java.awt.Component ; 43 import java.util.*; 44 import java.lang.reflect.InvocationTargetException ; 45 46 51 class UpdateResultsTable implements MouseListener , ListSelectionListener , AncestorListener { 52 53 private NodeTableModel tableModel; 54 private JTable table; 55 private JScrollPane component; 56 private UpdateResultNode[] nodes = new UpdateResultNode[0]; 57 58 private String [] tableColumns; 59 private TableSorter sorter; 60 61 64 private final Map<String , String []> columnLabels = new HashMap<String , String []>(4); 65 { 66 ResourceBundle loc = NbBundle.getBundle(UpdateResultsTable.class); 67 columnLabels.put(UpdateResultNode.COLUMN_NAME_NAME, new String [] { 68 loc.getString("CTL_UpdateResults_Column_File_Title"), loc.getString("CTL_UpdateResults_Column_File_Desc")}); columnLabels.put(UpdateResultNode.COLUMN_NAME_STATUS, new String [] { 71 loc.getString("CTL_UpdateResults_Column_Status_Title"), loc.getString("CTL_UpdateResults_Column_Status_Desc")}); columnLabels.put(UpdateResultNode.COLUMN_NAME_PATH, new String [] { 74 loc.getString("CTL_UpdateResults_Column_Path_Title"), loc.getString("CTL_UpdateResults_Column_Path_Desc")}); } 77 78 private static final Comparator NodeComparator = new Comparator() { 79 public int compare(Object o1, Object o2) { 80 Node.Property p1 = (Node.Property) o1; 81 Node.Property p2 = (Node.Property) o2; 82 String sk1 = (String ) p1.getValue("sortkey"); if (sk1 != null) { 84 String sk2 = (String ) p2.getValue("sortkey"); return sk1.compareToIgnoreCase(sk2); 86 } else { 87 try { 88 String s1 = (String ) p1.getValue(); 89 String s2 = (String ) p2.getValue(); 90 return s1.compareToIgnoreCase(s2); 91 } catch (Exception 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; table.setRowHeight(height); 107 component = new JScrollPane(table, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER); 108 component.getViewport().setBackground(table.getBackground()); 109 Color borderColor = UIManager.getColor("scrollpane_border"); if (borderColor == null) borderColor = UIManager.getColor("controlShadow"); 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")); table.getAccessibleContext().setAccessibleDescription(NbBundle.getMessage(UpdateResultsTable.class, "ACSD_UpdateResults")); setColumns(new String [] { UpdateResultNode.COLUMN_NAME_NAME, UpdateResultNode.COLUMN_NAME_STATUS, UpdateResultNode.COLUMN_NAME_PATH }); 119 } 120 121 void setDefaultColumnSizes() { 122 SwingUtilities.invokeLater(new Runnable () { 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 event) { 137 setDefaultColumnSizes(); 138 } 139 140 public void ancestorMoved(AncestorEvent event) { 141 } 142 143 public void ancestorRemoved(AncestorEvent 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 164 final void setColumns(String [] 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 [] columns) { 180 Node.Property [] properties = new Node.Property[columns.length]; 181 for (int i = 0; i < columns.length; i++) { 182 String column = columns[i]; 183 String [] labels = (String []) columnLabels.get(column); 184 properties[i] = new UpdateResultsTable.ColumnDescriptor(column, String .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 name, Class type, String displayName, String shortDescription) { 201 super(name, type, displayName, shortDescription); 202 } 203 204 public Object getValue() throws IllegalAccessException , InvocationTargetException { 205 return null; 206 } 207 } 208 209 public void mouseEntered(MouseEvent e) { 210 } 211 212 public void mouseExited(MouseEvent e) { 213 } 214 215 public void mousePressed(MouseEvent e) { 216 } 217 218 public void mouseReleased(MouseEvent e) { 219 } 220 221 public void mouseClicked(MouseEvent 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 (this, 0, "")); } 231 } 232 } 233 234 public void valueChanged(ListSelectionEvent 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; 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 { 255 256 private FilePathCellRenderer pathRenderer = new FilePathCellRenderer(); 257 258 public Component getTableCellRendererComponent(JTable table, Object 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(); } 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 path = nodes[sorter.modelIndex(row)].getInfo().getFile().getAbsolutePath(); 274 ((JComponent) renderer).setToolTipText(path); 275 } 276 return renderer; 277 } 278 } 279 } 280 | Popular Tags |