1 | package genius.gui.boaframework;
|
---|
2 |
|
---|
3 | import java.awt.Frame;
|
---|
4 | import java.awt.event.ActionEvent;
|
---|
5 | import java.awt.event.ActionListener;
|
---|
6 | import java.util.ArrayList;
|
---|
7 | import java.util.HashSet;
|
---|
8 | import java.util.Set;
|
---|
9 |
|
---|
10 | import javax.swing.JButton;
|
---|
11 | import javax.swing.JDialog;
|
---|
12 | import javax.swing.JLabel;
|
---|
13 | import javax.swing.JOptionPane;
|
---|
14 | import javax.swing.JTextField;
|
---|
15 |
|
---|
16 | import genius.core.boaframework.BOAparameter;
|
---|
17 |
|
---|
18 | public class ParameterFrame extends JDialog {
|
---|
19 |
|
---|
20 | private static final long serialVersionUID = -8510562424350715921L;
|
---|
21 | private int higherBorder = 10;
|
---|
22 | private int lowerBorder = 20;
|
---|
23 | private int elementHeight = 50;
|
---|
24 | private int nameWidth = 100;
|
---|
25 | private int descriptionWidth = 400;
|
---|
26 | private int lowerBoundWidth = 100;
|
---|
27 | private int stepSizeWidth = 100;
|
---|
28 | private int higherBoundWidth = 100;
|
---|
29 | private int leftSideBorder = 15;
|
---|
30 | private int spacing = 50;
|
---|
31 | private int labelHeight = 30;
|
---|
32 | private int buttonHeight = 30;
|
---|
33 | private int buttonWidth = 80;
|
---|
34 | private JTextField[] descriptions;
|
---|
35 | private JTextField[] lowerbounds;
|
---|
36 | private JTextField[] stepsizes;
|
---|
37 | private JTextField[] upperbounds;
|
---|
38 |
|
---|
39 | private ArrayList<BOAparameter> result;
|
---|
40 |
|
---|
41 | public ParameterFrame(Frame frame) {
|
---|
42 | super(frame, "Edit parameters", true);
|
---|
43 | this.setLocation(frame.getLocation().x + frame.getWidth() / 4, frame.getLocation().y + frame.getHeight() / 4);
|
---|
44 | }
|
---|
45 |
|
---|
46 | public Set<BOAparameter> getResult(ArrayList<BOAparameter> input) {
|
---|
47 | this.result = input;
|
---|
48 | generateFrame();
|
---|
49 | generateLabels();
|
---|
50 | generateInput();
|
---|
51 | generateButtons();
|
---|
52 | pack();
|
---|
53 | setVisible(true);
|
---|
54 | return new HashSet<BOAparameter>(result);
|
---|
55 | }
|
---|
56 |
|
---|
57 | private void generateButtons() {
|
---|
58 | JButton okButton = new JButton("Ok");
|
---|
59 | okButton.setFont(new java.awt.Font("Tahoma", 1, 13));
|
---|
60 | getContentPane().add(okButton, new org.netbeans.lib.awtextra.AbsoluteConstraints(leftSideBorder,
|
---|
61 | (result.size() * elementHeight) + lowerBorder, buttonWidth, buttonHeight));
|
---|
62 | okButton.addActionListener(new ActionListener() {
|
---|
63 | public void actionPerformed(ActionEvent e) {
|
---|
64 | ArrayList<BOAparameter> temp = prepareResults();
|
---|
65 | if (temp != null && temp.size() > 0) {
|
---|
66 | result = temp;
|
---|
67 | dispose();
|
---|
68 | }
|
---|
69 | }
|
---|
70 | });
|
---|
71 |
|
---|
72 | JButton cancelButton = new JButton("Cancel");
|
---|
73 | getContentPane().add(cancelButton,
|
---|
74 | new org.netbeans.lib.awtextra.AbsoluteConstraints(leftSideBorder + buttonWidth + 20,
|
---|
75 | (result.size() * elementHeight) + lowerBorder, buttonWidth, buttonHeight));
|
---|
76 | cancelButton.addActionListener(new ActionListener() {
|
---|
77 | public void actionPerformed(ActionEvent e) {
|
---|
78 | dispose();
|
---|
79 | }
|
---|
80 | });
|
---|
81 | }
|
---|
82 |
|
---|
83 | private ArrayList<BOAparameter> prepareResults() {
|
---|
84 | ArrayList<BOAparameter> parameters = new ArrayList<BOAparameter>();
|
---|
85 | for (int i = 0; i < result.size(); i++) {
|
---|
86 | try {
|
---|
87 | Double lowerBound = Double.valueOf(lowerbounds[i].getText());
|
---|
88 |
|
---|
89 | if (!stepsizes[i].getText().equals("") && !upperbounds[i].getText().equals("")) {
|
---|
90 | Double stepSize = Double.valueOf(stepsizes[i].getText());
|
---|
91 | Double upperBound = Double.valueOf(upperbounds[i].getText());
|
---|
92 |
|
---|
93 | if (lowerBound.compareTo(upperBound) > 0) {
|
---|
94 | JOptionPane.showMessageDialog(null,
|
---|
95 | "Each upper bound must be higher or equal to the lower bound.", "Parameter error", 0);
|
---|
96 | break;
|
---|
97 | }
|
---|
98 |
|
---|
99 | if (stepSize <= 0) {
|
---|
100 | JOptionPane.showMessageDialog(null, "Each step size must be positive.", "Parameter error", 0);
|
---|
101 | break;
|
---|
102 | }
|
---|
103 | parameters.add(new BOAparameter(result.get(i).getName(), lowerBound, upperBound, stepSize,
|
---|
104 | descriptions[i].getText()));
|
---|
105 | } else {
|
---|
106 | parameters.add(new BOAparameter(result.get(i).getName(), lowerBound, lowerBound, 1.,
|
---|
107 | descriptions[i].getText()));
|
---|
108 | }
|
---|
109 | } catch (Exception e) {
|
---|
110 | JOptionPane.showMessageDialog(null, "All values should be numeric.", "Parameter error", 0);
|
---|
111 | break;
|
---|
112 | }
|
---|
113 | }
|
---|
114 | return parameters;
|
---|
115 | }
|
---|
116 |
|
---|
117 | private void generateLabels() {
|
---|
118 | JLabel description = new JLabel("Description");
|
---|
119 | description.setFont(new java.awt.Font("Tahoma", 1, 13));
|
---|
120 | getContentPane().add(description, new org.netbeans.lib.awtextra.AbsoluteConstraints(leftSideBorder + nameWidth,
|
---|
121 | higherBorder, descriptionWidth, labelHeight));
|
---|
122 |
|
---|
123 | JLabel lowerBound = new JLabel("Lower bound");
|
---|
124 | lowerBound.setFont(new java.awt.Font("Tahoma", 1, 13));
|
---|
125 | getContentPane().add(lowerBound, new org.netbeans.lib.awtextra.AbsoluteConstraints(
|
---|
126 | leftSideBorder + nameWidth + descriptionWidth, higherBorder, lowerBoundWidth, labelHeight));
|
---|
127 |
|
---|
128 | JLabel stepSize = new JLabel("Step size");
|
---|
129 | stepSize.setFont(new java.awt.Font("Tahoma", 1, 13));
|
---|
130 | getContentPane().add(stepSize,
|
---|
131 | new org.netbeans.lib.awtextra.AbsoluteConstraints(
|
---|
132 | leftSideBorder + nameWidth + descriptionWidth + lowerBoundWidth, higherBorder, stepSizeWidth,
|
---|
133 | labelHeight));
|
---|
134 |
|
---|
135 | JLabel upperBound = new JLabel("Upper bound");
|
---|
136 | upperBound.setFont(new java.awt.Font("Tahoma", 1, 13));
|
---|
137 | getContentPane().add(upperBound,
|
---|
138 | new org.netbeans.lib.awtextra.AbsoluteConstraints(
|
---|
139 | leftSideBorder + nameWidth + descriptionWidth + lowerBoundWidth + stepSizeWidth, higherBorder,
|
---|
140 | higherBoundWidth, labelHeight));
|
---|
141 | }
|
---|
142 |
|
---|
143 | private void generateInput() {
|
---|
144 | descriptions = new JTextField[result.size()];
|
---|
145 | lowerbounds = new JTextField[result.size()];
|
---|
146 | stepsizes = new JTextField[result.size()];
|
---|
147 | upperbounds = new JTextField[result.size()];
|
---|
148 |
|
---|
149 | for (int i = 0; i < result.size(); i++) {
|
---|
150 | JLabel label = new JLabel(result.get(i).getName());
|
---|
151 | label.setFont(new java.awt.Font("Tahoma", 1, 13));
|
---|
152 | getContentPane().add(label, new org.netbeans.lib.awtextra.AbsoluteConstraints(leftSideBorder,
|
---|
153 | labelHeight + higherBorder + i * elementHeight, nameWidth - spacing, -1));
|
---|
154 |
|
---|
155 | JTextField descriptionTF = new JTextField(result.get(i).getDescription() + "");
|
---|
156 | descriptionTF.setFont(new java.awt.Font("Tahoma", 1, 13));
|
---|
157 | descriptionTF.setEditable(false);
|
---|
158 | getContentPane().add(descriptionTF,
|
---|
159 | new org.netbeans.lib.awtextra.AbsoluteConstraints(leftSideBorder + nameWidth,
|
---|
160 | labelHeight + higherBorder + i * elementHeight, descriptionWidth - spacing, -1));
|
---|
161 | descriptions[i] = descriptionTF;
|
---|
162 |
|
---|
163 | JTextField lowerBoundTF = new JTextField(result.get(i).getLow() + "");
|
---|
164 | lowerBoundTF.setFont(new java.awt.Font("Tahoma", 1, 13));
|
---|
165 | getContentPane().add(lowerBoundTF,
|
---|
166 | new org.netbeans.lib.awtextra.AbsoluteConstraints(leftSideBorder + nameWidth + descriptionWidth,
|
---|
167 | labelHeight + higherBorder + i * elementHeight, lowerBoundWidth - spacing, -1));
|
---|
168 | lowerbounds[i] = lowerBoundTF;
|
---|
169 |
|
---|
170 | JTextField stepSizeTF = new JTextField();
|
---|
171 | if (!result.get(i).getLow().equals(result.get(i).getHigh())) {
|
---|
172 | stepSizeTF.setText(result.get(i).getStep() + "");
|
---|
173 | }
|
---|
174 | stepSizeTF.setFont(new java.awt.Font("Tahoma", 1, 13));
|
---|
175 | getContentPane().add(stepSizeTF,
|
---|
176 | new org.netbeans.lib.awtextra.AbsoluteConstraints(
|
---|
177 | leftSideBorder + nameWidth + descriptionWidth + lowerBoundWidth,
|
---|
178 | labelHeight + higherBorder + i * elementHeight, stepSizeWidth - spacing, -1));
|
---|
179 | stepsizes[i] = stepSizeTF;
|
---|
180 |
|
---|
181 | JTextField upperBoundTF = new JTextField();
|
---|
182 | if (!result.get(i).getLow().equals(result.get(i).getHigh())) {
|
---|
183 | upperBoundTF.setText(result.get(i).getHigh() + "");
|
---|
184 | }
|
---|
185 | upperBoundTF.setFont(new java.awt.Font("Tahoma", 1, 13));
|
---|
186 | getContentPane().add(upperBoundTF,
|
---|
187 | new org.netbeans.lib.awtextra.AbsoluteConstraints(
|
---|
188 | leftSideBorder + nameWidth + descriptionWidth + lowerBoundWidth + stepSizeWidth,
|
---|
189 | labelHeight + higherBorder + i * elementHeight, higherBoundWidth - spacing, -1));
|
---|
190 | upperbounds[i] = upperBoundTF;
|
---|
191 | }
|
---|
192 | }
|
---|
193 |
|
---|
194 | private void generateFrame() {
|
---|
195 | setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
|
---|
196 | int height = (result.size() * elementHeight) + higherBorder + lowerBorder + labelHeight + buttonHeight;
|
---|
197 | int width = leftSideBorder + nameWidth + descriptionWidth + lowerBoundWidth + stepSizeWidth + higherBoundWidth
|
---|
198 | + leftSideBorder;
|
---|
199 | setMaximumSize(new java.awt.Dimension(width, height));
|
---|
200 | setMinimumSize(new java.awt.Dimension(width, height));
|
---|
201 | setPreferredSize(new java.awt.Dimension(width, height));
|
---|
202 | setResizable(false);
|
---|
203 | getContentPane().setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());
|
---|
204 | }
|
---|
205 | } |
---|