1 19 20 package org.netbeans.modules.subversion.ui.commit; 21 22 import org.netbeans.modules.versioning.util.FilePathCellRenderer; 23 import org.netbeans.modules.versioning.util.TableSorter; 24 import org.netbeans.modules.subversion.util.SvnUtils; 25 import org.netbeans.modules.subversion.SvnFileNode; 26 import org.netbeans.modules.subversion.FileInformation; 27 import org.netbeans.modules.subversion.Subversion; 28 import org.openide.util.NbBundle; 29 30 import javax.swing.*; 31 import javax.swing.event.AncestorEvent ; 32 import javax.swing.event.AncestorListener ; 33 import javax.swing.event.TableModelListener ; 34 import javax.swing.event.TableModelEvent ; 35 import javax.swing.table.DefaultTableCellRenderer ; 36 import javax.swing.table.TableModel ; 37 import javax.swing.table.TableColumnModel ; 38 import java.awt.Component ; 39 import java.lang.String ; 40 import java.util.*; 41 42 47 public class CommitTable implements AncestorListener , TableModelListener { 48 49 public static String [] COMMIT_COLUMNS = new String [] { 50 CommitTableModel.COLUMN_NAME_NAME, 51 CommitTableModel.COLUMN_NAME_STATUS, 52 CommitTableModel.COLUMN_NAME_ACTION, 53 CommitTableModel.COLUMN_NAME_PATH 54 }; 55 56 public static String [] IMPORT_COLUMNS = new String [] { 57 CommitTableModel.COLUMN_NAME_NAME, 58 CommitTableModel.COLUMN_NAME_ACTION, 59 CommitTableModel.COLUMN_NAME_PATH 60 }; 61 62 private CommitTableModel tableModel; 63 private JTable table; 64 private JComponent component; 65 66 private TableSorter sorter; 67 private String [] columns; 68 private String [] sortByColumns; 69 70 71 public CommitTable(JLabel label, String [] columns, String [] sortByColumns) { 72 init(label, columns, null); 73 this.sortByColumns = sortByColumns; 74 setSortingStatus(); 75 } 76 77 public CommitTable(JLabel label, String [] columns, TableSorter sorter) { 78 init(label, columns, sorter); 79 } 80 81 private void init(JLabel label, String [] columns, TableSorter sorter) { 82 tableModel = new CommitTableModel(columns); 83 tableModel.addTableModelListener(this); 84 if(sorter == null) { 85 sorter = new TableSorter(tableModel); 86 } 87 this.sorter = sorter; 88 table = new JTable(this.sorter); 89 table.getTableHeader().setReorderingAllowed(false); 90 table.setDefaultRenderer(String .class, new CommitStringsCellRenderer()); 91 table.setDefaultEditor(CommitOptions.class, new CommitOptionsCellEditor()); 92 table.getTableHeader().setReorderingAllowed(true); 93 this.sorter.setTableHeader(table.getTableHeader()); 94 table.setRowHeight(table.getRowHeight() * 6 / 5); 95 table.addAncestorListener(this); 96 component = new JScrollPane(table, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER); 97 label.setLabelFor(table); 98 table.getAccessibleContext().setAccessibleDescription(NbBundle.getMessage(CommitTable.class, "ACSD_CommitTable")); setColumns(columns); 100 } 101 102 public void ancestorAdded(AncestorEvent event) { 103 setDefaultColumnSizes(); 104 } 105 106 109 private void setDefaultColumnSizes() { 110 int width = table.getWidth(); 111 TableColumnModel columnModel = table.getColumnModel(); 112 if (columns == null || columnModel == null) return; if (columnModel.getColumnCount() != columns.length) return; 114 if (columns.length == 3) { 115 for (int i = 0; i < columns.length; i++) { 116 String col = columns[i]; 117 sorter.setColumnComparator(i, null); 118 if (col.equals(CommitTableModel.COLUMN_NAME_NAME)) { 119 sorter.setColumnComparator(i, new FileNameComparator()); 120 columnModel.getColumn(i).setPreferredWidth(width * 30 / 100); 121 } else if (col.equals(CommitTableModel.COLUMN_NAME_ACTION)) { 122 columnModel.getColumn(i).setPreferredWidth(width * 15 / 100); 123 } else { 124 columnModel.getColumn(i).setPreferredWidth(width * 40 / 100); 125 } 126 } 127 } else if (columns.length == 4) { 128 for (int i = 0; i < columns.length; i++) { 129 String col = columns[i]; 130 sorter.setColumnComparator(i, null); 131 if (col.equals(CommitTableModel.COLUMN_NAME_NAME)) { 132 sorter.setColumnComparator(i, new FileNameComparator()); 133 columnModel.getColumn(i).setPreferredWidth(width * 30 / 100); 134 } else if (col.equals(CommitTableModel.COLUMN_NAME_STATUS)) { 135 sorter.setColumnComparator(i, new StatusComparator()); 136 columnModel.getColumn(i).setPreferredWidth(width * 15 / 100); 137 } else if (col.equals(CommitTableModel.COLUMN_NAME_ACTION)) { 138 columnModel.getColumn(i).setPreferredWidth(width * 15 / 100); 139 } else { 140 columnModel.getColumn(i).setPreferredWidth(width * 40 / 100); 141 } 142 } 143 } else if (columns.length == 5) { 144 for (int i = 0; i < columns.length; i++) { 145 String col = columns[i]; 146 sorter.setColumnComparator(i, null); 147 if (col.equals(CommitTableModel.COLUMN_NAME_NAME)) { 148 sorter.setColumnComparator(i, new FileNameComparator()); 149 columnModel.getColumn(i).setPreferredWidth(width * 25 / 100); 150 } else if (col.equals(CommitTableModel.COLUMN_NAME_STATUS)) { 151 sorter.setColumnComparator(i, new StatusComparator()); 152 sorter.setSortingStatus(i, TableSorter.ASCENDING); 153 columnModel.getColumn(i).setPreferredWidth(width * 15 / 100); 154 } else if (col.equals(CommitTableModel.COLUMN_NAME_ACTION)) { 155 columnModel.getColumn(i).setPreferredWidth(width * 15 / 100); 156 } else { 157 columnModel.getColumn(i).setPreferredWidth(width * 30 / 100); 158 } 159 } 160 } 161 } 162 163 private void setSortingStatus() { 164 for (int i = 0; i < sortByColumns.length; i++) { 165 String sortByColumn = sortByColumns[i]; 166 for (int j = 0; j < columns.length; j++) { 167 String column = columns[j]; 168 if(column.equals(sortByColumn)) { 169 sorter.setSortingStatus(j, column.equals(sortByColumn) ? TableSorter.ASCENDING : TableSorter.NOT_SORTED); 170 break; 171 } 172 } 173 } 174 } 175 176 public TableSorter getSorter() { 177 return sorter; 178 } 179 180 public void ancestorMoved(AncestorEvent event) { 181 } 182 183 public void ancestorRemoved(AncestorEvent event) { 184 } 185 186 void setColumns(String [] cols) { 187 if (Arrays.equals(columns, cols)) return; 188 columns = cols; 189 tableModel.setColumns(cols); 190 setDefaultColumnSizes(); 191 } 192 193 public void setNodes(SvnFileNode[] nodes) { 194 tableModel.setNodes(nodes); 195 } 196 197 200 public Map<SvnFileNode, CommitOptions> getCommitFiles() { 201 return tableModel.getCommitFiles(); 202 } 203 204 207 public JComponent getComponent() { 208 return component; 209 } 210 211 void dataChanged() { 212 int idx = table.getSelectedRow(); 213 tableModel.fireTableDataChanged(); 214 if (idx != -1) table.getSelectionModel().addSelectionInterval(idx, idx); 215 } 216 217 TableModel getTableModel() { 218 return tableModel; 219 } 220 221 public void tableChanged(TableModelEvent e) { 222 table.repaint(); 224 } 225 226 public void setRootFile(String repositoryPath, String rootLocalPath) { 227 tableModel.setRootFile(repositoryPath, rootLocalPath); 228 } 229 230 private class CommitOptionsCellEditor extends DefaultCellEditor { 231 232 private final Object [] dirAddOptions = new Object [] { 233 CommitOptions.ADD_DIRECTORY, 234 CommitOptions.EXCLUDE 235 }; 236 237 private final Object [] addOptions = new Object [] { 238 CommitOptions.ADD_TEXT, 239 CommitOptions.ADD_BINARY, 240 CommitOptions.EXCLUDE 241 }; 242 private final Object [] commitOptions = new Object [] { 243 CommitOptions.COMMIT, 244 CommitOptions.EXCLUDE 245 }; 246 247 private final Object [] removeOptions = new Object [] { 248 CommitOptions.COMMIT_REMOVE, 249 CommitOptions.EXCLUDE 250 }; 251 252 public CommitOptionsCellEditor() { 253 super(new JComboBox()); 254 } 255 256 public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) { 257 FileInformation info = tableModel.getNode(sorter.modelIndex(row)).getInformation(); 258 int fileStatus = info.getStatus(); 259 JComboBox combo = (JComboBox) editorComponent; 260 if (fileStatus == FileInformation.STATUS_VERSIONED_DELETEDLOCALLY || fileStatus == FileInformation.STATUS_VERSIONED_REMOVEDLOCALLY) { 261 combo.setModel(new DefaultComboBoxModel(removeOptions)); 262 } else if ((fileStatus & FileInformation.STATUS_IN_REPOSITORY) == 0) { 263 if (info.isDirectory()) { 264 combo.setModel(new DefaultComboBoxModel(dirAddOptions)); 265 } else { 266 combo.setModel(new DefaultComboBoxModel(addOptions)); 267 } 268 } else { 269 combo.setModel(new DefaultComboBoxModel(commitOptions)); 270 } 271 return super.getTableCellEditorComponent(table, value, isSelected, row, column); 272 } 273 } 274 275 private class CommitStringsCellRenderer extends DefaultTableCellRenderer { 276 277 private FilePathCellRenderer pathRenderer = new FilePathCellRenderer(); 278 279 public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) { 280 int col = table.convertColumnIndexToModel(column); 281 if (columns[col] == CommitTableModel.COLUMN_NAME_NAME) { 282 TableSorter sorter = (TableSorter) table.getModel(); 283 CommitTableModel model = (CommitTableModel) sorter.getTableModel(); 284 SvnFileNode node = model.getNode(sorter.modelIndex(row)); 285 CommitOptions options = model.getOptions(sorter.modelIndex(row)); 286 if (!isSelected) { 287 value = "<html>" + Subversion.getInstance().getAnnotator().annotateNameHtml( node.getFile().getName(), node.getInformation(), null); 289 } 290 if (options == CommitOptions.EXCLUDE) { 291 value = "<html><s>" + value + "</s></html>"; } 293 return super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column); 294 } else if (columns[col] == CommitTableModel.COLUMN_NAME_PATH) { 295 return pathRenderer.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column); 296 } else { 297 return super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column); 298 } 299 } 300 } 301 302 private class StatusComparator extends SvnUtils.ByImportanceComparator { 303 public int compare(Object o1, Object o2) { 304 Integer row1 = (Integer ) o1; 305 Integer row2 = (Integer ) o2; 306 return super.compare(tableModel.getNode(row1.intValue()).getInformation(), 307 tableModel.getNode(row2.intValue()).getInformation()); 308 } 309 } 310 311 private class FileNameComparator implements Comparator { 312 public int compare(Object o1, Object o2) { 313 Integer row1 = (Integer ) o1; 314 Integer row2 = (Integer ) o2; 315 return tableModel.getNode(row1.intValue()).getName().compareToIgnoreCase( 316 tableModel.getNode(row2.intValue()).getName()); 317 } 318 } 319 320 321 } 322 | Popular Tags |