source: java/main/src/main/java/com/framsticks/gui/controls/SliderControl.java @ 97

Last change on this file since 97 was 97, checked in by psniegowski, 11 years ago

HIGHLIGHTS:

  • add proper exception passing between communication sides:

if exception occur during handling client request, it is
automatically passed as comment to error response.

it may be used to snoop communication between peers

  • fix algorithm choosing text controls in GUI
  • allow GUI testing in virtual frame buffer (xvfb)

FEST had some problem with xvfb but workaround was found

supports tab-completion based on requests history

CHANGELOG:
Further improve handling of exceptions in GUI.

Add StatusBar? implementing ExceptionResultHandler?.

Make completion processing asynchronous.

Minor changes.

Improve completion in console.

Improve history in InteractiveConsole?.

First working version of DirectConsole?.

Minor changes.

Make Connection.address non final.

It is more suitable to use in configuration.

Improvement of consoles.

Improve PopupMenu? and closing of FrameJoinable?.

Fix BrowserTest?.

Found bug with FEST running under xvfb.

JButtonFixture.click() is not working under xvfb.
GuiTest? has wrapper which uses JButton.doClick() directly.

Store CompositeParam? param in TreeNode?.

Simplify ClientSideManagedConnection? connecting.

There is now connectedFunctor needed, ApplicationRequests? can be
send right after creation. They are buffered until the version
and features are negotiated.

Narow down interface of ClientSideManagedConnection?.

Allow that connection specialization send only
ApplicationRequests?.

Improve policy of text control choosing.

Change name of Genotype in BrowserTest?.

Make BrowserTest? change name of Genotype.

Minor change.

First working draft of TrackConsole?.

Simplify Consoles.

More improvements with gui joinables.

Unify initialization on gui joinables.

More rework of Frame based entities.

Refactorize structure of JFrames based entities.

Extract GuiTest? from BrowserBaseTest?.

Reorganize Console classes structure.

Add Collection view to JoinableCollection?.

Configure timeout in testing.

Minor changes.

Rework connections hierarchy.

Add Mode to the get operation.

Make get and set in Tree take PrimitiveParam?.

Unify naming of operations.

Make RunAt? use the given ExceptionHandler?.

It wraps the virtual runAt() method call with
try-catch passing exception to handler.

Force RunAt? to include ExceptionHandler?.

Improve ClientAtServer?.

Minor change.

Another sweep with FindBugs?.

Rename Instance to Tree.

Minor changes.

Minor changes.

Further clarify semantics of Futures.

Add FutureHandler?.

FutureHandler? is refinement of Future, that proxifies
exception handling to ExceptionResultHandler? given
at construction time.

Remove StateFunctor? (use Future<Void> instead).

Make Connection use Future<Void>.

Unparametrize *ResponseFuture?.

Remove StateCallback? not needed anymore.

Distinguish between sides of ResponseFuture?.

Base ResponseCallback? on Future (now ResponseFuture?).

Make asynchronous store taking Future for flags.

Implement storeValue in ObjectInstance?.

File size: 5.6 KB
Line 
1package com.framsticks.gui.controls;
2
3import java.awt.BorderLayout;
4import java.awt.Dimension;
5import java.util.Hashtable;
6
7import javax.swing.Box;
8import javax.swing.BoxLayout;
9import javax.swing.JComponent;
10import javax.swing.JLabel;
11import javax.swing.JPanel;
12import javax.swing.JSlider;
13import javax.swing.JTextField;
14import javax.swing.event.ChangeEvent;
15import javax.swing.event.ChangeListener;
16
17import org.apache.log4j.Logger;
18
19import com.framsticks.params.Flags;
20import com.framsticks.params.types.DecimalParam;
21import com.framsticks.params.types.FloatParam;
22import com.framsticks.params.types.NumberParam;
23import com.framsticks.util.lang.Numbers;
24import com.framsticks.util.swing.Layout;
25
26@SuppressWarnings("serial")
27public class SliderControl extends TextControl {
28
29        private static final Logger log = Logger.getLogger(SliderControl.class.getName());
30
31        protected final JSlider slider;
32
33        protected final JTextField text;
34
35        /**
36         * Division factor used to implement float value slider.
37         */
38        private final int div = 10;
39        private JComponent changing = null;
40
41        private Class<? extends Number> valueType;
42
43        public SliderControl(NumberParam<?> param) {
44                super(param);
45                text = new JTextField();
46
47                //TODO: that factor should be done as a constant
48                this.setMaximumSize(new Dimension(Integer.MAX_VALUE, (int)(LINE_HEIGHT * 1.2)));
49
50                // ComponentUI ui = UIManager.getUI(slider);
51                // assert ui instanceof SliderUI;
52                // SliderUI sui = (SliderUI) ui;
53                // slider.setUI(sui);
54
55                slider = new JSlider();
56
57                slider.setEnabled(!isReadOnly());
58                slider.setPaintLabels(false);
59                if (param instanceof DecimalParam) {
60                        valueType = Integer.class;
61
62                        int min = param.getMin(Integer.class);
63                        int max = param.getMax(Integer.class);
64                        slider.setMinimum(min);
65                        slider.setMaximum(max);
66                        if (param.getDef(Integer.class) != null) {
67                                slider.setValue(param.getDef(Integer.class));
68                        } else {
69                                slider.setValue(min);
70                        }
71                        slider.setMajorTickSpacing((int) ((max - min) / 5));
72                        slider.setMinorTickSpacing((int) ((max - min) / 10));
73                } else if (param instanceof FloatParam) {
74                        valueType = Double.class;
75
76                        double min = param.getMin(Double.class) * div;
77                        slider.setMinimum((int) min);
78                        double max = param.getMax(Double.class) * div;
79                        double diff = max - min;
80                        slider.setMaximum((int) max);
81
82                        Hashtable<Integer, java.awt.Component> labels = new Hashtable<Integer, java.awt.Component>();
83                        int ticks = 6;
84                        for (int i = 0; i <= ticks; i++) {
85                                double val = (diff / ticks) * i + min;
86                                String label = String.format("%1$.1f", (val / 10)).replace(",",
87                                                ".");
88                                labels.put((int) val, new JLabel(label, JLabel.CENTER));
89                        }
90                        slider.setLabelTable(labels);
91                        slider.setMajorTickSpacing((int) (diff / 5));
92                        slider.setMinorTickSpacing((int) (diff / 10));
93                        if (param.getDef(Double.class) != null) {
94                                double defaultValue = param.getDef(Double.class) * div;
95                                slider.setValue((int) defaultValue);
96                        }
97                }
98                slider.setPaintLabels(true);
99                slider.setPaintTicks(true);
100
101                this.setLayout(new BoxLayout(this, BoxLayout.LINE_AXIS));
102                this.setAlignmentX(Box.CENTER_ALIGNMENT);
103                this.setAlignmentY(Box.CENTER_ALIGNMENT);
104
105                JPanel sliderPanel = new JPanel();
106                // sliderPanel.setLayout(new BoxLayout(sliderPanel, BoxLayout.LINE_AXIS));
107
108
109                Layout.fixComponentSize(text, new Dimension(90, Control.LINE_HEIGHT));
110                text.setHorizontalAlignment(JSlider.CENTER);
111
112                text.setEnabled(!param.hasFlag(Flags.READONLY));
113
114                slider.addChangeListener(new ChangeListener() {
115                        @Override
116                        public void stateChanged(ChangeEvent e) {
117                                if (changing != null) {
118                                        return;
119                                }
120                                log.trace("changing " + getParam() + " with slider: " + slider.getValue());
121                                changing = slider;
122                                text.setText(convertFromSliderDomain(slider.getValue()).toString());
123                                changing = null;
124                        }
125                });
126
127                text.getDocument().addDocumentListener(createDocumentListener(new Runnable() {
128                        @Override
129                        public void run() {
130                                if (changing != null) {
131                                        return;
132                                }
133                                log.trace("changing " + getParam() + " with text: " + text.getText());
134                                changing = text;
135                                Integer i = convertToSliderDomain(convertTextToNumber());
136                                if (i != null) {
137                                        slider.setValue(i);
138                                }
139                                changing = null;
140                                notifyOfChange();
141                        }
142                }));
143
144                JPanel sVPanel = new JPanel();
145                sVPanel.setLayout(new BoxLayout(sVPanel, BoxLayout.LINE_AXIS));
146                sVPanel.add(text);
147                Layout.copyComponentDimensions(sVPanel, text);
148
149                JPanel sPanel = new JPanel();
150                sPanel.setLayout(new BoxLayout(sPanel, BoxLayout.LINE_AXIS));
151
152                sliderPanel.setLayout(new BorderLayout());
153                sliderPanel.add(slider, BorderLayout.CENTER);
154                sliderPanel.setMaximumSize(new Dimension(Integer.MAX_VALUE, Integer.MAX_VALUE));
155                sliderPanel.setMinimumSize(new Dimension(0, 60));
156
157                sPanel.add(sVPanel);
158                sPanel.add(sliderPanel);
159
160                this.add(sPanel);
161        }
162
163        @Override
164        public NumberParam<?> getParam() {
165                return (NumberParam<?>) param;
166        }
167
168        private Number convertFromSliderDomain(int value) {
169                if (param instanceof DecimalParam) {
170                        return value;
171                }
172                if (param instanceof FloatParam) {
173                        return (double) value / (double) div;
174                }
175                return null;
176        }
177
178        private Integer convertToSliderDomain(Number value) {
179                if (value == null) {
180                        return null;
181                }
182                if (param instanceof DecimalParam) {
183                        return (Integer) value;
184                }
185                if (param instanceof FloatParam) {
186                        return (int) ((Double) value * div);
187                }
188                return null;
189        }
190
191        private Number convertTextToNumber() {
192                return Numbers.cast(text.getText(), valueType);
193        }
194
195        @Override
196        public void pushValueToUserInterfaceImpl(Object value) {
197                if (value == null) {
198                        return;
199                }
200                text.setText(value.toString());
201        }
202
203        @Override
204        public Number pullValueFromUserInterface() {
205                return convertTextToNumber();
206        }
207
208}
Note: See TracBrowser for help on using the repository browser.