1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151
|
package test;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.table.*;
/**
* <p>Title: </p>
*
* <p>Description: </p>
*
* <p>Copyright: Copyright (c) 2006</p>
*
* <p>Company: </p>
*
* @author not attributable
* @version 1.0
*/
public class ComboTable {
/** Pseudo values.
*/
enum Val {
TOTO, TITI, TUTU, TATA, GAGA, MOMO, GUGU, BYE, AILLE, OUILLE, OUI, NON, YES, NO, JA, NEIN;
}
/** The pseudo database.
*/
private static final Map<Integer, java.util.List<Val>> DATA_BASE = createDataBase();
/** Self test main.
* @param args Arguments from the command-line.
*/
public static void main(String ...args) {
DefaultTableModel tableModel = new DefaultTableModel() {
/** {@inheritDoc}
*/
@Override public Class getColumnClass(int i) {
if (i == 1) {
return Val.class;
}
return String.class;
}
};
tableModel.setColumnCount(2);
tableModel.setColumnIdentifiers(new Object[] {"Name", "Choice"});
for (Val val : Val.values()) {
tableModel.addRow(new Object[] {val.toString(), val});
}
JTable table = new JTable(tableModel);
table.setDefaultRenderer(Val.class, new ComboTableCellRenderer());
table.setDefaultEditor(Val.class, new ComboTableCellEditor(new DataBaseQuerySystem()));
JFrame frame = new JFrame("Test");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.add(new JScrollPane(table), BorderLayout.CENTER);
frame.pack();
frame.setVisible(true);
}
/** Create the pseudo data base.
* @return The pseudo data base.
*/
private static Map<Integer, java.util.List<Val>> createDataBase() {
Random random = new Random();
Map<Integer, java.util.List<Val>> result = new HashMap<Integer, java.util.List<Val>>();
Val[] vals = Val.values();
int valueNumber = vals.length;
int i = 0;
for (Val val : vals) {
java.util.List<Val> list = new ArrayList<Val>(4);
list.add(val);
while (list.size() != 4) {
int index = random.nextInt(valueNumber);
Val v = vals[index];
if (!list.contains(v)) {
list.add(v);
}
}
result.put(i, Collections.unmodifiableList(list));
i++;
}
return Collections.unmodifiableMap(result);
}
/** The combo box table cell renderer.
*/
private static class ComboTableCellRenderer extends JComboBox implements TableCellRenderer {
/** {@inheritDoc}
*/
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
if (isSelected) {
setForeground(table.getSelectionForeground());
setBackground(table.getSelectionBackground());
} else {
setForeground(table.getForeground());
setBackground(table.getBackground());
}
removeAllItems();
if (value != null) {
addItem(value);
}
setSelectedItem(value);
return this;
}
}
/** The combo box table cell editor.
*/
private static class ComboTableCellEditor extends DefaultCellEditor {
private DataBaseQuerySystem dbConnect;
public ComboTableCellEditor(DataBaseQuerySystem dbConnect) {
super(new JComboBox());
this.dbConnect = dbConnect;
}
/** {@inheritDoc}
*/
@Override public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) {
Component editor = super.getTableCellEditorComponent(table, value, isSelected, row, column);
JComboBox combo = (JComboBox) getComponent();
combo.removeAllItems();
System.out.println("query for row" + row);
java.util.List<Val> authorizedValues = dbConnect.getAuthorizedValuesForRow(row);
for (Val authorizedValue : authorizedValues) {
combo.addItem(authorizedValue);
}
combo.setSelectedItem(value);
return editor;
}
}
/** Query data base.
*/
private static class DataBaseQuerySystem {
/** Query the data base.
* @param row Current row.
* @return A list of authorized values.
*/
public java.util.List<Val> getAuthorizedValuesForRow(int row) {
return DATA_BASE.get(row);
}
}
} |
Partager