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

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

HIGHLIGHTS:

  • add <include/> to configuration
  • add side notes to tree
    • used to store arbitrary information alongside the tree structure
  • migrate to log4j2
    • supports lazy string evaluation of passed arguments
  • improve GUI tree
    • it stays in synchronization with actual state (even in high load test scenario)
  • improve panel management in GUI
  • make loading objects in GUI more lazy
  • offload parsing to connection receiver thread
    • info parsing
    • first step of objects parsing
  • fix connection parsing bug (eof in long values)
  • support zero-arguments procedure in table view

CHANGELOG:
Implement procedure calls from table view.

Refactorization around procedures in tables.

Add table editor for buttons.

Render buttons in the the list view.

Further improve Columns.

Add Column class for TableModel?.

Accept also non-arguments ProcedureParams? in tableView.

Increase maximal TextAreaControl? size.

Add tooltip to ProcedureControl?.

Fix bug of interpreting eofs in long values by connection reader.

Further rework connection parsing.

Simplify client connection processing.

Test ListChange? modification.

Test ListChange? events with java server.

Add TestChild?.

Fix bug with fast deregistering when connecting to running server.

Another minor refactorization in TreeOperations?.

Fix bug in SimpleAbstractAccess? loading routine.

Another minor improvement.

Minor change.

Make reading of List objects two-phase.

Another minor change.

Dispatch parsing into receiver thread.

Another step.

Enclose passing value in ObjectParam? case in closure.

Minor step.

Minor change on way to offload parsing.

Temporarily comment out single ValueParam? get.

It will be generalized to multi ValueParam?.

Process info in receiver thread.

Add DispatchingExceptionHandler?.

Make waits in browser test longer.

Use FETCHED_MARK.

It is honored in GUI, where it used to decide whether to get values

after user action.

It is set in standard algorithm for processing fetched values.

Add remove operation to side notes.

Make loading more lazy.

Improve loading policy.

On node choose load itself, on node expansion, load children.

Minor improvement.

Fix bug with panel interleaving.

Minor improvements.

Improve panel management.

More cleaning around panels.

Reorganize panels.

Further improve tree.

Fix bug in TreeModel?.

Remove children from TreeNode?.

Implement TreeNode? hashCode and equals.

Make TreeNode? delegate equals and hashcode to internal reference.

Move listeners from TreeNode? to side notes.

Store path.textual as a side note.

Side note params instead of accesses for objects.

More refactorizations.

In TreeNode? bindAccess based on side notes.

Minor step.

Hide createAccess.

Rename AccessInterface? to Access.

Minor changes.

Several improvements in high load scenarios.

Change semantics of ArrayListAccess?.set(index, null);

It now removes the element, making list shorter
(it was set to null before).

Add path remove handler.

Handle exceptions in Connection.

Update .gitignore

Configure logging to file.

Move registration to TreeModel?.

Further refactorization.

Minor refactorization.

Minor improvements.

Use specialized event also for Modify action of ListChange?.

Use remove events.

Use the insertion events for tree.

Further improve tree refreshing.

Further improve reacting on events in GUI.

Fix problem with not adding objects on addition list change.

Migrate to log4j lazy String construction interface.

Migrate imports to log4j2.

Drop dependency on adapter to version 1.2.

Switch log4j implementation to log4j2.

Add dirty mark to the NodeAtFrame?.

Make selecting in AccessInterfaces? type safe.

Ignore containers size settings in Model and Genotype.

Use tree side notes to remember local changes and panels.

Add sideNotes to tree.

They will be used to store various accompanying information
right in the tree.

Use ReferenceIdentityMap? from apache in TreeNode?.

It suits the need perfectly (weak semantics on both key and value).

Make ArrayListParam? do not react size changes.

Guard in TableModel? before not yet loaded objects.

Add <include/> clause and AutoInjector?.

Extract common columns configuration to separate xml,
that can be included by other configurations.

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.logging.log4j.Logger;
18import org.apache.logging.log4j.LogManager;
19
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 = LogManager.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.setPaintLabels(false);
58                if (param instanceof DecimalParam) {
59                        valueType = Integer.class;
60
61                        int min = param.getMin(Integer.class);
62                        int max = param.getMax(Integer.class);
63                        slider.setMinimum(min);
64                        slider.setMaximum(max);
65                        if (param.getDef(Integer.class) != null) {
66                                slider.setValue(param.getDef(Integer.class));
67                        } else {
68                                slider.setValue(min);
69                        }
70                        slider.setMajorTickSpacing((int) ((max - min) / 5));
71                        slider.setMinorTickSpacing((int) ((max - min) / 10));
72                } else if (param instanceof FloatParam) {
73                        valueType = Double.class;
74
75                        double min = param.getMin(Double.class) * div;
76                        slider.setMinimum((int) min);
77                        double max = param.getMax(Double.class) * div;
78                        double diff = max - min;
79                        slider.setMaximum((int) max);
80
81                        Hashtable<Integer, java.awt.Component> labels = new Hashtable<Integer, java.awt.Component>();
82                        int ticks = 6;
83                        for (int i = 0; i <= ticks; i++) {
84                                double val = (diff / ticks) * i + min;
85                                String label = String.format("%1$.1f", (val / 10)).replace(",",
86                                                ".");
87                                labels.put((int) val, new JLabel(label, JLabel.CENTER));
88                        }
89                        slider.setLabelTable(labels);
90                        slider.setMajorTickSpacing((int) (diff / 5));
91                        slider.setMinorTickSpacing((int) (diff / 10));
92                        if (param.getDef(Double.class) != null) {
93                                double defaultValue = param.getDef(Double.class) * div;
94                                slider.setValue((int) defaultValue);
95                        }
96                }
97                slider.setPaintLabels(true);
98                slider.setPaintTicks(true);
99
100                this.setLayout(new BoxLayout(this, BoxLayout.LINE_AXIS));
101                this.setAlignmentX(Box.CENTER_ALIGNMENT);
102                this.setAlignmentY(Box.CENTER_ALIGNMENT);
103
104                JPanel sliderPanel = new JPanel();
105                // sliderPanel.setLayout(new BoxLayout(sliderPanel, BoxLayout.LINE_AXIS));
106
107
108                Layout.fixComponentSize(text, new Dimension(90, Control.LINE_HEIGHT));
109                text.setHorizontalAlignment(JSlider.CENTER);
110
111
112                slider.addChangeListener(new ChangeListener() {
113                        @Override
114                        public void stateChanged(ChangeEvent e) {
115                                if (changing != null) {
116                                        return;
117                                }
118                                log.trace("changing {} with slider: {}", getParam(), slider.getValue());
119                                changing = slider;
120                                text.setText(convertFromSliderDomain(slider.getValue()).toString());
121                                changing = null;
122                        }
123                });
124
125                text.getDocument().addDocumentListener(createDocumentListener(new Runnable() {
126                        @Override
127                        public void run() {
128                                if (changing != null) {
129                                        return;
130                                }
131                                log.trace("changing {} with text: {}", getParam(), text.getText());
132                                changing = text;
133                                Integer i = convertToSliderDomain(convertTextToNumber());
134                                if (i != null) {
135                                        slider.setValue(i);
136                                }
137                                changing = null;
138                                notifyOfChange();
139                        }
140                }));
141
142                JPanel sVPanel = new JPanel();
143                sVPanel.setLayout(new BoxLayout(sVPanel, BoxLayout.LINE_AXIS));
144                sVPanel.add(text);
145                Layout.copyComponentDimensions(sVPanel, text);
146
147                JPanel sPanel = new JPanel();
148                sPanel.setLayout(new BoxLayout(sPanel, BoxLayout.LINE_AXIS));
149
150                sliderPanel.setLayout(new BorderLayout());
151                sliderPanel.add(slider, BorderLayout.CENTER);
152                sliderPanel.setMaximumSize(new Dimension(Integer.MAX_VALUE, Integer.MAX_VALUE));
153                sliderPanel.setMinimumSize(new Dimension(0, 60));
154
155                sPanel.add(sVPanel);
156                sPanel.add(sliderPanel);
157
158                this.add(sPanel);
159        }
160
161        @Override
162        public NumberParam<?> getParam() {
163                return (NumberParam<?>) param;
164        }
165
166        private Number convertFromSliderDomain(int value) {
167                if (param instanceof DecimalParam) {
168                        return value;
169                }
170                if (param instanceof FloatParam) {
171                        return (double) value / (double) div;
172                }
173                return null;
174        }
175
176        private Integer convertToSliderDomain(Number value) {
177                if (value == null) {
178                        return null;
179                }
180                if (param instanceof DecimalParam) {
181                        return (Integer) value;
182                }
183                if (param instanceof FloatParam) {
184                        return (int) ((Double) value * div);
185                }
186                return null;
187        }
188
189        private Number convertTextToNumber() {
190                return Numbers.cast(text.getText(), valueType);
191        }
192
193        @Override
194        public void pushValueToUserInterfaceImpl(Object value) {
195                if (value == null) {
196                        return;
197                }
198                text.setText(value.toString());
199        }
200
201        @Override
202        public Number pullValueFromUserInterface() {
203                return convertTextToNumber();
204        }
205
206        @Override
207        protected void updateEnabled(boolean enabled) {
208                slider.setEnabled(enabled);
209                text.setEnabled(enabled);
210        }
211
212}
Note: See TracBrowser for help on using the repository browser.