source: src/main/java/genius/gui/panels/DoublePanel.java@ 346

Last change on this file since 346 was 262, checked in by Adel Magra, 5 years ago

Modified GUI for uncertainty settings. Elicitation cost can now take values up to a precision of 4 decimals.

File size: 3.3 KB
Line 
1package genius.gui.panels;
2
3import java.awt.BorderLayout;
4import java.awt.Dimension;
5
6import javax.swing.JComponent;
7import javax.swing.JFormattedTextField;
8import javax.swing.JLabel;
9import javax.swing.JOptionPane;
10import javax.swing.JPanel;
11import javax.swing.JSpinner;
12import javax.swing.JTextField;
13import javax.swing.SpinnerNumberModel;
14import javax.swing.SwingConstants;
15import javax.swing.SwingUtilities;
16import javax.swing.event.DocumentEvent;
17import javax.swing.event.DocumentListener;
18
19import genius.core.listener.Listener;
20
21/**
22 * Shows a single text line input area for a double value.
23 */
24@SuppressWarnings("serial")
25public class DoublePanel extends JPanel
26{
27 private final JLabel jlabel;
28 private final JTextField textfield;
29 DoubleModel model;
30
31 public DoublePanel(String label, final DoubleModel model)
32 {
33 setLayout(new BorderLayout());
34
35 this.model = model;
36 setLayout(new BorderLayout());
37
38 this.jlabel = new JLabel(label);
39 jlabel.setHorizontalAlignment(SwingConstants.RIGHT);
40 add(jlabel, BorderLayout.WEST);
41
42 JSpinner jSpinner = new JSpinner(new SpinnerNumberModel((double) model.getValue(), 0.0, 1.0, 0.001));
43 jSpinner.setMaximumSize(new Dimension(300, 30));
44 JSpinner.NumberEditor numberEditor = new JSpinner.NumberEditor(jSpinner,"0.0000");
45 jSpinner.setEditor(numberEditor);
46 this.textfield = getTextField(jSpinner);
47 this.textfield.setColumns(5);
48
49 add(jSpinner, BorderLayout.EAST);
50
51 // not working?
52 // setMaximumSize(new Dimension(99999999, 30));
53 textfield.getDocument().addDocumentListener(new DocumentListener() {
54
55 @Override
56 public void removeUpdate(DocumentEvent e) {
57 if (!textfield.getText().isEmpty())
58 {
59 model.setText(parseDouble());
60 }
61 // we ignore empty text to avoid reacting on replace procedure
62 // too early
63 }
64
65 @Override
66 public void insertUpdate(DocumentEvent e) {
67 model.setText(parseDouble());
68 }
69
70 @Override
71 public void changedUpdate(DocumentEvent e) {
72 model.setText(parseDouble());
73 }
74 });
75
76 model.addListener(new Listener<Double>() {
77 @Override
78 public void notifyChange(final Double data) {
79 SwingUtilities.invokeLater(new Runnable() {
80
81 @Override
82 public void run() {
83 textfield.setText("" + data);
84 updateEnabledness();
85 }
86 });
87 }
88 });
89
90 updateEnabledness();
91 }
92
93 private void updateEnabledness()
94 {
95 boolean enabled = !model.isLocked();
96 setEnabled(enabled);
97 textfield.setEnabled(enabled);
98 jlabel.setEnabled(enabled);
99 }
100
101 private double parseDouble()
102 {
103 double d = 0;
104 String text = textfield.getText();
105 String err = "The value \"" + text + "\" is not a valid number.";
106 try {
107 d = Double.parseDouble(text);
108 if (d < 0 || d > 1) {
109 showError(err);
110 return 0;
111 }
112 } catch (Exception e) {
113 showError(err);
114 return 0;
115 }
116 return d;
117 }
118
119 private void showError(String error)
120 {
121 JOptionPane.showMessageDialog(null, error, "Edit error", 0);
122 }
123
124 private JFormattedTextField getTextField(JSpinner spinner) {
125 JComponent editor = spinner.getEditor();
126 if (editor instanceof JSpinner.DefaultEditor) {
127 return ((JSpinner.DefaultEditor)editor).getTextField();
128 } else {
129 System.err.println("Unexpected editor type: "
130 + spinner.getEditor().getClass()
131 + " isn't a descendant of DefaultEditor");
132 return null;
133 }
134 }
135}
Note: See TracBrowser for help on using the repository browser.