source: src/main/java/genius/gui/uncertainty/PairwiseComparisonPanel.java@ 346

Last change on this file since 346 was 70, checked in by Tim Baarslag, 6 years ago

Completely new GUI for uncertainty

File size: 4.9 KB
Line 
1package genius.gui.uncertainty;
2
3import java.util.Hashtable;
4
5import java.awt.Dimension;
6import java.awt.GridBagConstraints;
7import java.awt.GridBagLayout;
8import java.awt.Insets;
9import java.awt.event.KeyAdapter;
10import java.awt.event.KeyEvent;
11
12import javax.swing.JFrame;
13import javax.swing.JLabel;
14import javax.swing.JPanel;
15import javax.swing.JSlider;
16import javax.swing.JTextField;
17import javax.swing.event.ChangeEvent;
18import javax.swing.event.ChangeListener;
19
20import genius.gui.panels.ButtonPanel;
21import genius.gui.panels.ExtendedCheckboxPanel;
22
23public class PairwiseComparisonPanel extends JPanel
24{
25 private static final long serialVersionUID = 7745677414933157895L;
26
27 private PairwiseComparisonModel model;
28
29 private final JLabel label;
30 private final JLabel numberOfCompsLabel;
31 private final JLabel rankingsLabel;
32
33 private JPanel uncertaintyLevelPanel;
34 private SteppingSlider uncertaintySlider;
35
36 final JTextField text = new JTextField(4);
37
38 private JSlider errorSlider;
39 private JLabel errorLabel;
40
41 private ExtendedCheckboxPanel experimentalBox;
42 private final ButtonPanel okButton;
43
44
45 public PairwiseComparisonPanel(PairwiseComparisonModel model)
46 {
47 JFrame frame = new JFrame("Uncertainty options");
48 this.model = model;
49
50 this.label = new JLabel("Set user model parameters");
51 this.numberOfCompsLabel = new JLabel("(Maximum number of rankings = " + this.model.getMaxNumberInComps() +")");
52
53 this.uncertaintyLevelPanel = new JPanel(new GridBagLayout());
54
55 GridBagConstraints c = new GridBagConstraints();
56 c.insets = new Insets(10,10,10,10);
57
58
59 this.uncertaintySlider = createUncertaintySlider();
60 this.rankingsLabel = new JLabel("Amount of rankings");
61
62 uncertaintySlider.addChangeListener(new ChangeListener(){
63 @Override
64 public void stateChanged(ChangeEvent e)
65 {
66
67 // Don't set the text field if that was the one triggering this event
68 if (uncertaintySlider.isFocusOwner())
69 text.setText(String.valueOf(uncertaintySlider.getDomainValue()));
70
71 model.setNumberOfComparisons(uncertaintySlider.getDomainValue());
72 }
73 });
74
75 text.addKeyListener(new KeyAdapter(){
76 @Override
77 public void keyReleased(KeyEvent ke) {
78 String typed = text.getText();
79 if(!typed.matches("\\d+") || typed.length() > 3) {
80 uncertaintySlider.setDomainValue(0);
81 return;
82 }
83 int value = Integer.parseInt(typed);
84 uncertaintySlider.setDomainValue(value);
85 model.setNumberOfComparisons(value);
86 }
87 });
88
89 text.setMaximumSize(new Dimension(20, 30));
90
91 // add components to the panel
92 c.gridx = 0;
93 c.gridy = 0;
94 uncertaintyLevelPanel.add(rankingsLabel, c);
95
96 c.gridx = 1;
97 uncertaintyLevelPanel.add(uncertaintySlider, c);
98
99 c.gridx = 2;
100 uncertaintyLevelPanel.add(text, c);
101
102 c.gridx = 0;
103 c.gridy = 1;
104 c.weighty = 1.0; //request any extra vertical space
105 this.errorLabel = new JLabel("Error rate");
106 uncertaintyLevelPanel.add(errorLabel, c);
107
108 c.gridx = 1;
109 c.gridy = 1;
110 this.errorSlider = createErrorSlider();
111 uncertaintyLevelPanel.add(errorSlider, c);
112
113 c.gridx = 0;
114 c.gridy = 2;
115 c.gridwidth = 3;
116
117 experimentalBox = new ExtendedCheckboxPanel("Grant agent access to real utility function (experimental setup)",
118 model.getExperimentalModel());
119 experimentalBox.getCheckBox().setSelected(true);
120 uncertaintyLevelPanel.add(experimentalBox, c);
121
122 c.gridx = 1;
123 c.gridy = 3;
124 c.gridwidth = 1;
125
126 okButton = new ButtonPanel("OK", model.getConfirmationModel());
127 uncertaintyLevelPanel.add(okButton, c);
128
129// setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
130
131 add(numberOfCompsLabel);
132 add(uncertaintyLevelPanel);
133
134 frame.add(this);
135 frame.setSize(500,300);
136 frame.setLocationRelativeTo(null);
137 frame.setVisible(true);
138 }
139
140
141 private SteppingSlider createUncertaintySlider()
142 {
143 final SteppingSlider steppingSlider = new SteppingSlider(model.getPossibleValues());
144 return steppingSlider;
145 }
146
147 private JSlider createErrorSlider() {
148 JSlider slider = new JSlider(JSlider.HORIZONTAL,0,100,0);
149 slider.setMajorTickSpacing( 1 );
150 slider.setMinorTickSpacing( 2 );
151 Hashtable<Integer, JLabel> sliderLabelsTable = new Hashtable<Integer, JLabel>();
152 sliderLabelsTable.put( 0 , new JLabel("0%") );
153 sliderLabelsTable.put( 100 , new JLabel("100%") );
154 slider.setLabelTable(sliderLabelsTable);
155 slider.setPaintLabels(true);
156// slider.setMaximumSize(new Dimension(50,50));
157 slider.addChangeListener(new ChangeListener() {
158 @Override
159 public void stateChanged(ChangeEvent e) {
160 model.getErrorModel().setText(String.valueOf((double) slider.getValue() / 100));
161 }
162 });
163 return slider;
164 }
165
166
167}
Note: See TracBrowser for help on using the repository browser.