1 19 20 package org.netbeans.modules.versioning.system.cvss.ui.actions.commit; 21 22 import org.netbeans.modules.versioning.system.cvss.*; 23 import org.netbeans.modules.versioning.system.cvss.util.Utils; 24 import org.netbeans.modules.versioning.util.FilePathCellRenderer; 25 import org.netbeans.modules.versioning.util.TableSorter; 26 import org.openide.util.NbBundle; 27 28 import javax.swing.*; 29 import javax.swing.event.AncestorEvent ; 30 import javax.swing.event.AncestorListener ; 31 import javax.swing.event.TableModelListener ; 32 import javax.swing.event.TableModelEvent ; 33 import javax.swing.table.DefaultTableCellRenderer ; 34 import javax.swing.table.TableModel ; 35 import javax.swing.table.TableColumnModel ; 36 import java.awt.Component ; 37 import java.util.*; 38 39 44 class CommitTable implements AncestorListener , TableModelListener { 45 46 private CommitTableModel tableModel; 47 private JTable table; 48 private JComponent component; 49 50 private TableSorter sorter; 51 private String [] columns; 52 53 public CommitTable(JLabel label) { 54 tableModel = new CommitTableModel(); 55 tableModel.addTableModelListener(this); 56 sorter = new TableSorter(tableModel); 57 table = new JTable(sorter); 58 table.getTableHeader().setReorderingAllowed(false); 59 table.setDefaultRenderer(String .class, new CommitStringsCellRenderer()); 60 table.setDefaultEditor(CommitOptions.class, new CommitOptionsCellEditor()); 61 table.getTableHeader().setReorderingAllowed(true); 62 sorter.setTableHeader(table.getTableHeader()); 63 int height = new JLabel("FONTSIZE").getPreferredSize().height * 6 / 5; table.setRowHeight(height); 65 table.addAncestorListener(this); 66 component = new JScrollPane(table, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER); 67 label.setLabelFor(table); 68 table.getAccessibleContext().setAccessibleDescription(NbBundle.getMessage(CommitTable.class, "ACSD_CommitTable")); } 70 71 public void ancestorAdded(AncestorEvent event) { 72 setDefaultColumnSizes(); 73 } 74 75 78 private void setDefaultColumnSizes() { 79 int width = table.getWidth(); 80 TableColumnModel columnModel = table.getColumnModel(); 81 if (columns == null || columnModel == null) return; if (columnModel.getColumnCount() != columns.length) return; 83 if (columns.length == 4) { 84 for (int i = 0; i < columns.length; i++) { 85 String col = columns[i]; 86 sorter.setColumnComparator(i, null); 87 sorter.setSortingStatus(i, TableSorter.NOT_SORTED); 88 if (col.equals(CommitSettings.COLUMN_NAME_NAME)) { 89 sorter.setColumnComparator(i, new FileNameComparator()); 90 columnModel.getColumn(i).setPreferredWidth(width * 30 / 100); 91 } else if (col.equals(CommitSettings.COLUMN_NAME_STATUS)) { 92 sorter.setColumnComparator(i, new StatusComparator()); 93 sorter.setSortingStatus(i, TableSorter.ASCENDING); 94 columnModel.getColumn(i).setPreferredWidth(width * 15 / 100); 95 } else if (col.equals(CommitSettings.COLUMN_NAME_ACTION)) { 96 columnModel.getColumn(i).setPreferredWidth(width * 15 / 100); 97 } else { 98 columnModel.getColumn(i).setPreferredWidth(width * 40 / 100); 99 } 100 } 101 } else if (columns.length == 5) { 102 for (int i = 0; i < columns.length; i++) { 103 String col = columns[i]; 104 sorter.setColumnComparator(i, null); 105 sorter.setSortingStatus(i, TableSorter.NOT_SORTED); 106 if (col.equals(CommitSettings.COLUMN_NAME_NAME)) { 107 sorter.setColumnComparator(i, new FileNameComparator()); 108 columnModel.getColumn(i).setPreferredWidth(width * 25 / 100); 109 } else if (col.equals(CommitSettings.COLUMN_NAME_STICKY)) { 110 columnModel.getColumn(i).setPreferredWidth(width * 15 / 100); 111 } else if (col.equals(CommitSettings.COLUMN_NAME_STATUS)) { 112 sorter.setColumnComparator(i, new StatusComparator()); 113 sorter.setSortingStatus(i, TableSorter.ASCENDING); 114 columnModel.getColumn(i).setPreferredWidth(width * 15 / 100); 115 } else if (col.equals(CommitSettings.COLUMN_NAME_ACTION)) { 116 columnModel.getColumn(i).setPreferredWidth(width * 15 / 100); 117 } else { 118 columnModel.getColumn(i).setPreferredWidth(width * 30 / 100); 119 } 120 } 121 } 122 } 123 124 public void ancestorMoved(AncestorEvent event) { 125 } 126 127 public void ancestorRemoved(AncestorEvent event) { 128 } 129 130 void setColumns(String [] cols) { 131 if (Arrays.equals(columns, cols)) return; 132 columns = cols; 133 tableModel.setColumns(cols); 134 setDefaultColumnSizes(); 135 } 136 137 void setNodes(CvsFileNode[] nodes) { 138 tableModel.setNodes(nodes); 139 } 140 141 public CommitSettings.CommitFile[] getCommitFiles() { 142 return tableModel.getCommitFiles(); 143 } 144 145 public JComponent getComponent() { 146 return component; 147 } 148 149 void dataChanged() { 150 int idx = table.getSelectedRow(); 151 tableModel.fireTableDataChanged(); 152 if (idx != -1) table.getSelectionModel().addSelectionInterval(idx, idx); 153 } 154 155 TableModel getTableModel() { 156 return tableModel; 157 } 158 159 public void tableChanged(TableModelEvent e) { 160 table.repaint(); 162 } 163 164 private class CommitOptionsCellEditor extends DefaultCellEditor { 165 166 private final Object [] addOptions = new Object [] { 167 CommitOptions.ADD_TEXT, 168 CommitOptions.ADD_BINARY, 169 CommitOptions.EXCLUDE 170 }; 171 private final Object [] commitOptions = new Object [] { 172 CommitOptions.COMMIT, 173 CommitOptions.EXCLUDE 174 }; 175 176 private final Object [] removeOptions = new Object [] { 177 CommitOptions.COMMIT_REMOVE, 178 CommitOptions.EXCLUDE 179 }; 180 181 public CommitOptionsCellEditor() { 182 super(new JComboBox()); 183 } 184 185 public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) { 186 int fileStatus = tableModel.getCommitFile(sorter.modelIndex(row)).getNode().getInformation().getStatus(); 187 JComboBox combo = (JComboBox) editorComponent; 188 if (fileStatus == FileInformation.STATUS_VERSIONED_DELETEDLOCALLY || fileStatus == FileInformation.STATUS_VERSIONED_REMOVEDLOCALLY) { 189 combo.setModel(new DefaultComboBoxModel(removeOptions)); 190 } else if ((fileStatus & FileInformation.STATUS_IN_REPOSITORY) == 0) { 191 combo.setModel(new DefaultComboBoxModel(addOptions)); 192 } else { 193 combo.setModel(new DefaultComboBoxModel(commitOptions)); 194 } 195 return super.getTableCellEditorComponent(table, value, isSelected, row, column); 196 } 197 } 198 199 private static class CommitStringsCellRenderer extends DefaultTableCellRenderer { 200 201 private FilePathCellRenderer pathRenderer = new FilePathCellRenderer(); 202 203 public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) { 204 int col = table.convertColumnIndexToModel(column); 205 if (col == 0) { 206 TableSorter sorter = (TableSorter) table.getModel(); 207 CommitTableModel model = (CommitTableModel) sorter.getTableModel(); 208 CommitSettings.CommitFile commitFile = model.getCommitFile(sorter.modelIndex(row)); 209 if (!isSelected) { 210 value = "<html>" + CvsVersioningSystem.getInstance().getAnnotator().annotateNameHtml( commitFile.getNode().getFile().getName(), commitFile.getNode().getInformation(), null); 212 } 213 if (commitFile.getOptions() == CommitOptions.EXCLUDE) { 214 value = "<html><s>" + value + "</s></html>"; } 216 return super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column); 217 } else if (col == 3) { 218 return pathRenderer.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column); 219 } else { 220 return super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column); 221 } 222 } 223 } 224 225 private class StatusComparator extends Utils.ByImportanceComparator { 226 public int compare(Object o1, Object o2) { 227 Integer row1 = (Integer ) o1; 228 Integer row2 = (Integer ) o2; 229 return super.compare(tableModel.getCommitFile(row1.intValue()).getNode().getInformation(), 230 tableModel.getCommitFile(row2.intValue()).getNode().getInformation()); 231 } 232 } 233 234 private class FileNameComparator implements Comparator { 235 public int compare(Object o1, Object o2) { 236 Integer row1 = (Integer ) o1; 237 Integer row2 = (Integer ) o2; 238 return tableModel.getCommitFile(row1.intValue()).getNode().getName().compareToIgnoreCase( 239 tableModel.getCommitFile(row2.intValue()).getNode().getName()); 240 } 241 } 242 } 243 | Popular Tags |