001: package vicazh.hyperpool.stream;
002:
003: import java.awt.event.*;
004: import java.util.*;
005: import javax.swing.*;
006: import javax.swing.event.*;
007:
008: /**
009: * The graphic control list selector
010: *
011: * @author Victor Zhigunov
012: * @version 0.4.0
013: */
014: public class IControlListSelector extends IDefSelector implements
015: ControlListSelectorMBean, ActionListener, ItemListener,
016: ListSelectionListener {
017:
018: private JTable table;
019:
020: private List<Object> data;
021:
022: private JButton buttonRemove;
023:
024: /**
025: * @param name
026: * selector name
027: * @param valModel
028: * combo box model for values
029: * @param defBox
030: * combo box for default value
031: * @param pmodel
032: * priorities table model
033: * @param table
034: * selector table
035: * @param buttonRemove
036: * remove client button
037: * @param data
038: * data list
039: */
040: public IControlListSelector(String name,
041: ISelectorListModel valModel, JComboBox defBox,
042: IPrioritiesModel pmodel, JTable table,
043: JButton buttonRemove, List<Object> data) {
044: super (name, valModel, (ISelectorModel) table.getModel(),
045: defBox, pmodel);
046: this .table = table;
047: table.getSelectionModel().addListSelectionListener(this );
048: this .data = data;
049: this .buttonRemove = buttonRemove;
050: buttonRemove.setEnabled(false);
051: buttonRemove.addActionListener(this );
052: }
053:
054: private boolean auto;
055:
056: public void itemStateChanged(ItemEvent e) {
057: AbstractButton source = (AbstractButton) e.getItemSelectable();
058: if (source.getActionCommand().equals("auto"))
059: auto = source.isSelected();
060: }
061:
062: public void actionPerformed(ActionEvent e) {
063: if (e.getSource() == buttonRemove) {
064: if (table.isEditing())
065: return;
066: List<List<Object>> list = (List<List<Object>>) getList();
067: while (table.getSelectedRows().length > 0) {
068: int i = table.getSelectedRow();
069: Object o = data.get(i);
070: list.get(getIndex(o)).remove(o);
071: data.remove(i);
072: model.fireTableRowsDeleted(i, i);
073: }
074: setList(list);
075: } else {
076: if (!data.contains(null) && !data.contains("")) {
077: data.add(null);
078: model.fireTableRowsInserted(data.size() - 1, data
079: .size() - 1);
080: }
081: table.editCellAt(data.indexOf(data.contains(null) ? null
082: : ""), 0);
083: table.repaint();
084: }
085: }
086:
087: public void valueChanged(ListSelectionEvent e) {
088: if (!e.getValueIsAdjusting())
089: buttonRemove.setEnabled(table.getSelectedRow() != -1);
090: }
091:
092: protected void otherNotification(String type, Object value) {
093: if (type.equals(ControlListSelectorMBean.SEEK)) {
094: if (!auto || data.contains(value))
095: return;
096: data.add(value);
097: model.fireTableRowsInserted(data.size() - 1,
098: data.size() - 1);
099: } else if (type.equals(ListSelectorMBean.LIST)) {
100: for (Object l : (List<?>) value)
101: for (Object o : (List<?>) l)
102: if (!data.contains(o)) {
103: data.add(o);
104: model.fireTableRowsInserted(data.size() - 1,
105: data.size() - 1);
106: }
107: }
108: super.otherNotification(type, value);
109: }
110: }
|