private void fillColumnCombo(ItemListener listener) { // sort column names alphabetically final int columnCount = this.list.getTableModel().getColumnCount(); final String[][] names = new String[columnCount][]; final int[] indexes = new int[columnCount]; for (int i = 0; i < columnCount; i++) { names[i] = this.list.getColumnNames(i); indexes[i] = 0; } // use column index as columns names are not unique final SortedMap<String, Integer> map = solve(names, indexes); final List<Column> cols = new ArrayList<Column>(columnCount); cols.add(TOUT); for (final Entry<String, Integer> e : map.entrySet()) { final int colIndex = e.getValue().intValue(); final String[] colNames = names[colIndex]; cols.add(new Column(e.getKey(), colNames[colNames.length - 1], colIndex)); } // don't fire when filling, we will fire when selecting this.comboColonnePourRecherche.removeItemListener(listener); final ListComboBoxModel comboModel = (ListComboBoxModel) this.comboColonnePourRecherche.getModel(); assert !comboModel.isSelectOnAdd() : "Otherwise our following select might not fire"; comboModel.removeAllElements(); comboModel.addAll(cols); this.comboColonnePourRecherche.addItemListener(listener); }
private void columnsChanged(final ItemListener listener) { final String currentID = ((Column) this.comboColonnePourRecherche.getSelectedItem()).getID(); fillColumnCombo(listener); final ListComboBoxModel comboModel = (ListComboBoxModel) this.comboColonnePourRecherche.getModel(); // no selection since the model was just emptied assert this.comboColonnePourRecherche.getSelectedIndex() == -1 && this.comboColonnePourRecherche.getSelectedItem() == null; // try to reselect the same column if it's still there for (final Object o : comboModel.getList()) { final Column col = (Column) o; if (CompareUtils.equals(col.getID(), currentID)) this.comboColonnePourRecherche.setSelectedItem(o); } if (comboModel.getSelectedItem() == null) selectAllColumnsItem(); }
public SearchItemComponent(final SearchListComponent list) { super(); this.list = list; this.setOpaque(false); // Initialisation de l'interface graphique this.searchMode = new JComboBox( new String[] { TM.tr("contains"), TM.tr("contains.exactly"), TM.tr("isLessThan"), TM.tr("isEqualTo"), TM.tr("isExactlyEqualTo"), TM.tr("isGreaterThan"), TM.tr("isEmpty") }); final ListComboBoxModel comboModel = new ListComboBoxModel(Arrays.asList(TOUT)); comboModel.setSelectOnAdd(false); // allow getColIndex() and thus getSearchItem() to work from now on assert comboModel.getSelectedItem() != null; this.comboColonnePourRecherche = new JComboBox(comboModel); uiInit(); }