1 19 20 package org.netbeans.modules.j2ee.persistence.wizard.fromdb; 21 22 import java.awt.Color ; 23 import java.awt.Component ; 24 import java.util.ArrayList ; 25 import java.util.Collections ; 26 import java.util.HashSet ; 27 import java.util.Iterator ; 28 import java.util.List ; 29 import java.util.Set ; 30 import javax.swing.AbstractListModel ; 31 import javax.swing.DefaultListCellRenderer ; 32 import javax.swing.JComponent ; 33 import javax.swing.JLabel ; 34 import javax.swing.JList ; 35 import javax.swing.JTable ; 36 import javax.swing.UIManager ; 37 import javax.swing.event.ChangeEvent ; 38 import javax.swing.event.ChangeListener ; 39 import javax.swing.table.AbstractTableModel ; 40 import javax.swing.table.DefaultTableCellRenderer ; 41 import javax.swing.table.TableColumn ; 42 import org.netbeans.modules.j2ee.persistence.wizard.fromdb.Table.DisabledReason; 43 import org.openide.util.NbBundle; 44 45 49 public class TableUISupport { 50 51 private TableUISupport() { 52 } 53 54 public static void connectAvailable(JList availableTablesList, TableClosure tableClosure) { 55 availableTablesList.setModel(new AvailableTablesModel(tableClosure)); 56 57 if (!(availableTablesList.getCellRenderer() instanceof AvailableTableRenderer)) { 58 availableTablesList.setCellRenderer(new AvailableTableRenderer()); 59 } 60 } 61 62 public static void connectSelected(JList selectedTablesList, TableClosure tableClosure) { 63 selectedTablesList.setModel(new SelectedTablesModel(tableClosure)); 64 65 if (!(selectedTablesList.getCellRenderer() instanceof SelectedTableRenderer)) { 66 selectedTablesList.setCellRenderer(new SelectedTableRenderer()); 67 } 68 } 69 70 public static Set <Table> getSelectedTables(JList list) { 71 Set <Table> result = new HashSet <Table>(); 72 73 Object [] selectedValues = list.getSelectedValues(); 74 for (int i = 0; i < selectedValues.length; i++) { 75 result.add((Table)selectedValues[i]); 76 } 77 78 return result; 79 } 80 81 public static void connectClassNames(JTable table, SelectedTables selectedTables) { 82 table.setModel(new TableClassNamesModel(selectedTables)); 83 setRenderer(table.getColumnModel().getColumn(0)); 84 setRenderer(table.getColumnModel().getColumn(1)); 85 } 86 87 private static void setRenderer(TableColumn column) { 88 if (!(column.getCellRenderer() instanceof TableClassNameRenderer)) { 89 column.setCellRenderer(new TableClassNameRenderer()); 90 } 91 } 92 93 private static final class AvailableTablesModel extends AbstractListModel implements ChangeListener { 94 95 private final TableClosure tableClosure; 96 97 private List <Table> displayTables; 98 99 public AvailableTablesModel(TableClosure tableClosure) { 100 this.tableClosure = tableClosure; 101 tableClosure.addChangeListener(this); 102 refresh(); 103 } 104 105 public Object getElementAt(int index) { 106 return displayTables.get(index); 107 } 108 109 public int getSize() { 110 return displayTables != null ? displayTables.size() : 0; 111 } 112 113 public void stateChanged(ChangeEvent event) { 114 refresh(); 115 } 116 117 private void refresh() { 118 int oldSize = getSize(); 119 displayTables = new ArrayList <Table>(tableClosure.getAvailableTables()); 120 Collections.sort(displayTables); 121 fireContentsChanged(this, 0, Math.max(oldSize, getSize())); 122 } 123 } 124 125 private static final class SelectedTablesModel extends AbstractListModel implements ChangeListener { 126 127 private final TableClosure tableClosure; 128 129 private List <Table> displayTables; 130 131 public SelectedTablesModel(TableClosure tableClosure) { 132 this.tableClosure = tableClosure; 133 tableClosure.addChangeListener(this); 134 refresh(); 135 } 136 137 public Object getElementAt(int index) { 138 return displayTables.get(index); 139 } 140 141 public int getSize() { 142 return displayTables != null ? displayTables.size() : 0; 143 } 144 145 public void stateChanged(ChangeEvent event) { 146 refresh(); 147 } 148 149 private void refresh() { 150 int oldSize = getSize(); 151 displayTables = new ArrayList <Table>(tableClosure.getSelectedTables()); 152 Collections.sort(displayTables); 153 fireContentsChanged(this, 0, Math.max(oldSize, getSize())); 154 } 155 156 public TableClosure getTableClosure() { 157 return tableClosure; 158 } 159 } 160 161 private static final class AvailableTableRenderer extends DefaultListCellRenderer { 162 163 public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) { 164 DisabledReason disabledReason = null; 165 Object displayName = null; 166 167 if (value instanceof Table) { 168 Table tableItem = (Table)value; 169 disabledReason = tableItem.getDisabledReason(); 170 if (disabledReason!= null) { 171 displayName = NbBundle.getMessage(TableUISupport.class, "LBL_TableNameWithDisabledReason", tableItem.getName(), disabledReason.getDisplayName()); 172 } else { 173 displayName = tableItem.getName(); 174 } 175 } 176 177 JLabel component = (JLabel )super.getListCellRendererComponent(list, displayName, index, isSelected, cellHasFocus); 178 component.setEnabled(disabledReason == null); 179 component.setToolTipText(disabledReason != null ? disabledReason.getDescription() : null); 180 181 return component; 182 } 183 184 } 185 186 private static final class SelectedTableRenderer extends DefaultListCellRenderer { 187 188 public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) { 189 Table table = null; 190 Object displayName = null; 191 boolean referenced = false; 192 TableClosure tableClosure = null; 193 194 if (value instanceof Table) { 195 table = (Table)value; 196 displayName = table.getName(); 197 198 if (list.getModel() instanceof SelectedTablesModel) { 199 SelectedTablesModel model = (SelectedTablesModel)list.getModel(); 200 tableClosure = model.getTableClosure(); 201 referenced = tableClosure.getReferencedTables().contains(table); 202 } 203 } else { 204 displayName = value; 205 } 206 207 JLabel component = (JLabel )super.getListCellRendererComponent(list, displayName, index, isSelected, cellHasFocus); 208 component.setEnabled(!referenced); 209 component.setToolTipText(referenced ? getTableTooltip(table, tableClosure) : null); 211 return component; 212 } 213 214 private static String getTableTooltip(Table table, TableClosure tableClosure) { 215 List <Table> tables = new ArrayList <Table>(); 216 Set <Table> relatedTables; 217 String bundleKey; 218 219 if (table.isJoin()) { 220 relatedTables = table.getReferencedTables(); 221 bundleKey = "LBL_RelatedTableJoin"; } else { 223 relatedTables = table.getReferencedByTables(); 224 bundleKey = "LBL_RelatedTableRefBy"; } 226 for (Iterator <Table> i = relatedTables.iterator(); i.hasNext();) { 227 Table refTable = i.next(); 228 if (tableClosure.getSelectedTables().contains(refTable)) { 229 tables.add(refTable); 230 } 231 } 232 return NbBundle.getMessage(TableUISupport.class, bundleKey, createTableList(tables)); 233 } 234 235 private static String createTableList(List <Table> tables) { 236 assert tables.size() > 0; 237 238 if (tables.size() == 1) { 239 return tables.iterator().next().getName(); 240 } 241 242 Collections.sort(tables); 243 244 String separator = NbBundle.getMessage(TableUISupport.class, "LBL_TableListSep"); 245 Iterator <Table> i = tables.iterator(); 246 StringBuilder builder = new StringBuilder (i.next().getName()); 247 String lastTable = i.next().getName(); 248 while (i.hasNext()) { 249 builder.append(separator); 250 builder.append(lastTable); 251 lastTable = i.next().getName(); 252 } 253 return NbBundle.getMessage(TableUISupport.class, "LBL_TableList", builder.toString(), lastTable); 254 } 255 } 256 257 private static final class TableClassNamesModel extends AbstractTableModel { 258 259 private SelectedTables selectedTables; 260 private final List <Table> tables; 261 262 public TableClassNamesModel(SelectedTables selectedTables) { 263 this.selectedTables = selectedTables; 264 this.tables = selectedTables.getTables(); 265 } 266 267 public Table getTableAt(int rowIndex) { 268 return tables.get(rowIndex); 269 } 270 271 public boolean isValidClass(Table table) { 272 return !selectedTables.hasProblem(table); 273 } 274 275 public String getProblemDisplayName(Table table) { 276 return selectedTables.getProblemDisplayNameForTable(table); 277 } 278 279 public int getRowCount() { 280 return tables.size(); 281 } 282 283 public int getColumnCount() { 284 return 2; 285 } 286 287 public Object getValueAt(int rowIndex, int columnIndex) { 288 switch (columnIndex) { 289 case 0: 290 return tables.get(rowIndex).getName(); 291 292 case 1: 293 Table table = tables.get(rowIndex); 294 return selectedTables.getClassName(table); 295 296 default: 297 assert false; 298 } 299 300 return null; 301 } 302 303 public void setValueAt(Object value, int rowIndex, int columnIndex) { 304 if (columnIndex != 1) { 305 return; 306 } 307 308 Table table = tables.get(rowIndex); 309 selectedTables.setClassName(table, (String )value); 310 } 311 312 public boolean isCellEditable(int rowIndex, int columnIndex) { 313 Table table = tables.get(rowIndex); 314 return !table.isJoin() && columnIndex == 1; 315 } 316 317 public String getColumnName(int column) { 318 switch (column) { 319 case 0: 320 return NbBundle.getMessage(TableUISupport.class, "LBL_DatabaseTable"); 321 322 case 1: 323 return NbBundle.getMessage(TableUISupport.class, "LBL_ClassName"); 324 325 default: 326 assert false; 327 } 328 329 return null; 330 } 331 } 332 333 private static final class TableClassNameRenderer extends DefaultTableCellRenderer { 334 private static Color errorForeground; 335 private static Color nonErrorForeground; 336 337 static { 338 errorForeground = UIManager.getColor("nb.errorForeground"); if (errorForeground == null) { 340 errorForeground = Color.RED; 341 } 342 nonErrorForeground = UIManager.getColor("Label.foreground"); } 344 345 public Component getTableCellRendererComponent(JTable jTable, Object value, boolean isSelected, boolean hasFocus, int row, int column) { 346 boolean joinTable = false; 347 boolean validClass = true; 348 String problemDisplayName = null; 349 350 if (jTable.getModel() instanceof TableClassNamesModel) { 351 TableClassNamesModel model = (TableClassNamesModel)jTable.getModel(); 352 Table table = model.getTableAt(row); 353 joinTable = table.isJoin(); 354 if (column == 1) { 355 validClass = model.isValidClass(table); 356 if (!validClass) { 357 problemDisplayName = model.getProblemDisplayName(table); 358 } 359 } 360 } 361 362 Object realValue = null; 363 if (joinTable && column == 1) { 364 realValue = NbBundle.getMessage(TableUISupport.class, "LBL_JoinTable"); 365 } else { 366 realValue = value; 367 } 368 JComponent component = (JComponent )super.getTableCellRendererComponent(jTable, realValue, isSelected, hasFocus, row, column); 369 component.setEnabled(!joinTable); 370 component.setToolTipText(joinTable ? NbBundle.getMessage(TableUISupport.class, "LBL_JoinTableDescription") : problemDisplayName); 371 component.setForeground((validClass) ? nonErrorForeground : errorForeground); 372 373 return component; 374 } 375 } 376 } 377 | Popular Tags |