1 | package genius.gui.boaparties;
|
---|
2 |
|
---|
3 | import java.awt.BorderLayout;
|
---|
4 | import java.awt.event.ActionEvent;
|
---|
5 | import java.awt.event.ActionListener;
|
---|
6 | import java.util.List;
|
---|
7 |
|
---|
8 | import javax.swing.JButton;
|
---|
9 | import javax.swing.JComboBox;
|
---|
10 | import javax.swing.JFrame;
|
---|
11 | import javax.swing.JLabel;
|
---|
12 | import javax.swing.JOptionPane;
|
---|
13 | import javax.swing.JPanel;
|
---|
14 | import javax.swing.JTextField;
|
---|
15 | import javax.swing.event.TableModelEvent;
|
---|
16 | import javax.swing.event.TableModelListener;
|
---|
17 |
|
---|
18 | import genius.core.boaframework.AcceptanceStrategy;
|
---|
19 | import genius.core.boaframework.BOA;
|
---|
20 | import genius.core.boaframework.BOAparameter;
|
---|
21 | import genius.core.boaframework.BoaType;
|
---|
22 | import genius.core.exceptions.InstantiateException;
|
---|
23 | import genius.core.repository.boa.BoaRepItem;
|
---|
24 | import genius.gui.renderer.RepItemListCellRenderer;
|
---|
25 |
|
---|
26 | /**
|
---|
27 | * Allows user to pick properties of a Boa Component. Contains a row with the
|
---|
28 | * component (combobox) and a row with the settings (shows values as text and a
|
---|
29 | * "Change" button)..
|
---|
30 | *
|
---|
31 | * @param T
|
---|
32 | * the type of the BOA components
|
---|
33 | */
|
---|
34 | @SuppressWarnings("serial")
|
---|
35 | public class BoaComponentPanel<T extends BOA> extends JPanel {
|
---|
36 |
|
---|
37 | public BoaComponentPanel(final BoaComponentModel<T> model, String title) {
|
---|
38 | setLayout(new BorderLayout());
|
---|
39 | add(new JLabel(title), BorderLayout.NORTH);
|
---|
40 | JComboBox<BoaRepItem<T>> combo = new JComboBox<BoaRepItem<T>>(model.getComponentsListModel());
|
---|
41 | combo.setRenderer(new RepItemListCellRenderer());
|
---|
42 | add(combo, BorderLayout.CENTER);
|
---|
43 | add(new BoaSettingsArea(model.getParameters()), BorderLayout.SOUTH);
|
---|
44 | }
|
---|
45 |
|
---|
46 | /**
|
---|
47 | * Simple test function
|
---|
48 | *
|
---|
49 | * @param args
|
---|
50 | * @throws InstantiateException
|
---|
51 | * @throws InterruptedException
|
---|
52 | */
|
---|
53 | public static void main(String[] args) throws InstantiateException, InterruptedException {
|
---|
54 | JFrame frame = new JFrame();
|
---|
55 | frame.setLayout(new BorderLayout());
|
---|
56 |
|
---|
57 | BoaComponentModel<AcceptanceStrategy> model = new BoaComponentModel<>(BoaType.ACCEPTANCESTRATEGY);
|
---|
58 |
|
---|
59 | frame.getContentPane().add(new BoaComponentPanel<AcceptanceStrategy>(model, "test boa component panel"),
|
---|
60 | BorderLayout.CENTER);
|
---|
61 | frame.pack();
|
---|
62 | frame.setVisible(true);
|
---|
63 | }
|
---|
64 |
|
---|
65 | }
|
---|
66 |
|
---|
67 | /**
|
---|
68 | * The lower part of the panel: the settings as text, plus a change button.
|
---|
69 | */
|
---|
70 | @SuppressWarnings("serial")
|
---|
71 | class BoaSettingsArea extends JPanel {
|
---|
72 | private JTextField text = new JTextField();
|
---|
73 | private JButton change = new JButton("Change");
|
---|
74 | private BoaParametersModel model;
|
---|
75 |
|
---|
76 | public BoaSettingsArea(final BoaParametersModel model) {
|
---|
77 | this.model = model;
|
---|
78 | setLayout(new BorderLayout());
|
---|
79 | text.setEditable(false);
|
---|
80 |
|
---|
81 | add(text, BorderLayout.CENTER);
|
---|
82 | add(change, BorderLayout.EAST);
|
---|
83 |
|
---|
84 | // handle change button click
|
---|
85 | change.addActionListener(new ActionListener() {
|
---|
86 | @Override
|
---|
87 | public void actionPerformed(ActionEvent e) {
|
---|
88 | BoaParametersPanel parampanel = new BoaParametersPanel(model);
|
---|
89 | int result = JOptionPane.showConfirmDialog(BoaSettingsArea.this, parampanel, "enter parameters",
|
---|
90 | JOptionPane.OK_CANCEL_OPTION);
|
---|
91 | if (result == JOptionPane.OK_OPTION) {
|
---|
92 | System.out.println("result=" + model);
|
---|
93 | }
|
---|
94 | }
|
---|
95 |
|
---|
96 | });
|
---|
97 |
|
---|
98 | // update when parameters change
|
---|
99 | model.addTableModelListener(new TableModelListener() {
|
---|
100 | @Override
|
---|
101 | public void tableChanged(TableModelEvent e) {
|
---|
102 | update();
|
---|
103 | }
|
---|
104 | });
|
---|
105 |
|
---|
106 | update();
|
---|
107 | }
|
---|
108 |
|
---|
109 | private void update() {
|
---|
110 | List<BOAparameter> setting = model.getSetting();
|
---|
111 | text.setText(setting.toString());
|
---|
112 | change.setEnabled(!setting.isEmpty());
|
---|
113 | }
|
---|
114 |
|
---|
115 | }
|
---|