1 19 package org.netbeans.modules.subversion.ui.properties; 20 21 import java.awt.Component ; 22 import java.awt.Dimension ; 23 import java.util.Arrays ; 24 import javax.swing.JComponent ; 25 import javax.swing.JScrollPane ; 26 import javax.swing.JTable ; 27 import javax.swing.event.AncestorEvent ; 28 import javax.swing.event.AncestorListener ; 29 import javax.swing.event.TableModelEvent ; 30 import javax.swing.event.TableModelListener ; 31 import javax.swing.table.DefaultTableCellRenderer ; 32 import javax.swing.table.TableColumnModel ; 33 import javax.swing.table.TableModel ; 34 import org.netbeans.modules.versioning.util.TableSorter; 35 import org.openide.util.NbBundle; 36 37 41 public class PropertiesTable implements AncestorListener , TableModelListener { 42 43 static final String [] PROPERTIES_COLUMNS = new String [] {PropertiesTableModel.COLUMN_NAME_NAME, PropertiesTableModel.COLUMN_NAME_VALUE}; 44 45 private PropertiesTableModel tableModel; 46 private JTable table; 47 private TableSorter sorter; 48 private JComponent component; 49 private String [] columns; 50 private String [] sortByColumns; 51 52 53 public PropertiesTable(String [] columns, String [] sortByColumns) { 54 init(columns, null); 55 this.sortByColumns = sortByColumns; 56 setSortingStatus(); 57 } 58 59 public PropertiesTable(String [] columns, TableSorter sorter) { 60 init(columns, sorter); 61 } 62 63 private void init(String [] columns, TableSorter sorter) { 64 tableModel = new PropertiesTableModel(columns); 65 tableModel.addTableModelListener(this); 66 if(sorter == null) { 67 sorter = new TableSorter(tableModel); 68 } 69 this.sorter = sorter; 70 table = new JTable (this.sorter); 71 table.getTableHeader().setReorderingAllowed(false); 72 table.setDefaultRenderer(String .class, new PropertiesTableCellRenderer()); 73 table.getTableHeader().setReorderingAllowed(true); 75 this.sorter.setTableHeader(table.getTableHeader()); 76 table.setRowHeight(table.getRowHeight()); 77 table.addAncestorListener(this); 78 component = new JScrollPane (table, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED); 79 component.setPreferredSize(new Dimension (340, 150)); 80 table.getAccessibleContext().setAccessibleDescription(NbBundle.getMessage(PropertiesTable.class, "ACSD_PropertiesTable")); setColumns(columns); 82 } 83 84 public void setColumns(String [] clmns) { 85 if (Arrays.equals(columns, clmns)) 86 return; 87 columns = clmns; 88 tableModel.setColumns(clmns); 89 setDefaultColumnSize(); 90 } 91 92 public JTable getTable() { 93 return table; 94 } 95 96 private void setDefaultColumnSize() { 97 int width = table.getWidth(); 98 TableColumnModel columnModel = table.getColumnModel(); 99 if (columns == null || columnModel == null) 100 return; 101 if (columnModel.getColumnCount() != columns.length) 102 return; 103 for (int i = 0; i < columns.length; i++) { 104 String col = columns[i]; 105 sorter.setColumnComparator(i, null); 106 if (col.equals(PropertiesTableModel.COLUMN_NAME_NAME)) { 107 columnModel.getColumn(i).setPreferredWidth(width * 20 / 100); 108 } else if (col.equals(PropertiesTableModel.COLUMN_NAME_VALUE)) { 109 columnModel.getColumn(i).setPreferredWidth(width * 40 / 100); 110 } 111 } 112 } 113 114 private void setSortingStatus() { 115 for (int i = 0; i < sortByColumns.length; i++) { 116 String sortByColumn = sortByColumns[i]; 117 for (int j = 0; j < columns.length; j++) { 118 String column = columns[j]; 119 if(column.equals(sortByColumn)) { 120 sorter.setSortingStatus(j, column.equals(sortByColumn) ? TableSorter.ASCENDING : TableSorter.NOT_SORTED); 121 break; 122 } 123 } 124 } 125 } 126 127 TableModel getTableModel() { 128 return tableModel; 129 } 130 131 void dataChanged() { 132 int idx = table.getSelectedRow(); 133 tableModel.fireTableDataChanged(); 134 if (idx != -1) { 135 table.getSelectionModel().addSelectionInterval(idx, idx); 136 } 137 } 138 139 public int getModelIndex(int viewIndex) { 140 return sorter.modelIndex(viewIndex); 141 } 142 143 public int[] getSelectedItems() { 144 return table.getSelectedRows(); 145 } 146 147 public SvnPropertiesNode[] getNodes() { 148 return tableModel.getNodes(); 149 } 150 151 public void setNodes(SvnPropertiesNode[] nodes) { 152 tableModel.setNodes(nodes); 153 } 154 155 JComponent getComponent() { 156 return component; 157 } 158 159 public void ancestorAdded(AncestorEvent arg0) { 160 setDefaultColumnSize(); 161 } 162 163 public void ancestorRemoved(AncestorEvent arg0) { 164 } 165 166 public void ancestorMoved(AncestorEvent arg0) { 167 } 168 169 public void tableChanged(TableModelEvent event) { 170 table.repaint(); 171 } 172 173 public class PropertiesTableCellRenderer extends DefaultTableCellRenderer { 174 175 public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int rowIndex, int columnIndex) { 176 Component renderer = super.getTableCellRendererComponent(table, value, hasFocus, hasFocus, rowIndex, columnIndex); 177 if (renderer instanceof JComponent ) { 178 String strValue = tableModel.getNode(sorter.modelIndex(rowIndex)).getValue(); 179 ((JComponent ) renderer).setToolTipText(strValue); 180 } 181 setToolTipText(value.toString()); 182 return renderer; 183 } 184 } 185 186 187 } 188 | Popular Tags |