source: java/main/src/main/java/com/framsticks/params/SimpleAbstractAccess.java @ 84

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

HIGHLIGHTS:

  • simplification of entities management model
  • cleanup around params (improve hierarchy)
  • migrate from JUnit to TestNG
  • introduce FEST to automatically test GUI
  • improve slider control
  • loosen synchronization between gui tree and backend representation
  • and many other bug fixes

NOTICE:

  • a great many of lines is changed only because of substituting spaces with tabs

CHANGELOG (oldest changes at the bottom):

Some cleaning after fix found.

Fix bug with tree.

More changes with TreeNodes?.

Finally fix issue with tree.

Improve gui tree management.

Decouple update of values from fetch request in gui.

Minor changes.

Minor changes.

Minor change.

Change Path construction wording.

More fixes to SliderControl?.

Fix SliderControl?.

Fix SliderControl?.

Minor improvement.

Several changes.

Make NumberParam? a generic class.

Add robot to the gui test.

Setup common testing logging configuration.

Remove Parameters class.

Remove entityOwner from Parameters.

Move name out from Parameters class.

Move configuration to after the construction.

Simplify observers and endpoints.

Remove superfluous configureEntity overrides.

Add dependency on fest-swing-testng.

Use FEST for final print test.

Use FEST for more concise and readable assertions.

Divide test of F0Parser into multiple methods.

Migrate to TestNG

Minor change.

Change convention from LOGGER to log.

Fix reporting of errors during controls filling.

Bound maximal height of SliderControl?.

Minor improvements.

Improve tooltips for controls.

Also use Delimeted in more places.

Move static control utilities to Gui.

Rename package gui.components to controls.

Some cleaning in controls.

Improve Param classes placing.

Move ValueParam?, PrimitiveParam? and CompositeParam? one package up.

Improve ParamBuilder?.

Move getDef to ValueParam? and PrimitiveParam?.

Move getMax and getDef to ValueParam?.

Move getMin to ValueParam?.

Upgrade to laters apache commons versions.

Use filterInstanceof extensively.

Add instanceof filters.

Make ValueParam? in many places of Param.

Place assertions about ValueParam?.

Add ValueParam?

Rename ValueParam? to PrimitiveParam?

Minor changes.

Several improvements to params types.

Add NumberParam?.

Add TextControl? component.

Add .swp files to .gitignore

Greatly improved slider component.

Some improvements.

Make Param.reassign return also a state.

Add IterableIterator?.

Several changes.

  • Move util classes to better packages.
  • Remove warnings from eclim.

Several improvements.

Fix bug with BooleanParam?.

Some experiments with visualization.

Another fix to panel management.

Improve panel management.

Some refactorization around panels.

Add root class for panel.

File size: 7.1 KB
Line 
1package com.framsticks.params;
2
3import java.io.IOException;
4import java.util.Collection;
5
6import static com.framsticks.util.lang.Containers.filterInstanceof;
7
8import org.apache.log4j.Logger;
9
10/**
11 * The Class SimpleAbstractAccess implements all the methods of AccessInterface
12 * which actions can be implemented with usage of {@link AccessInterface} methods
13 * or concern schema, which is stored in {@link #framsClass}
14 *
15 * Based on c++ class SimpleAbstractParam located in: cpp/gdk/param.*
16 *
17 * @author Jarek Szymczak <name.surname@gmail.com>, Mateusz Jarus (please
18 *         replace name and surname with my personal data)
19 *
20 * @author Piotr Sniegowski
21 */
22public abstract class SimpleAbstractAccess implements AccessInterface {
23
24        private final static Logger log = Logger.getLogger(SimpleAbstractAccess.class.getName());
25
26        @Override
27        public final FramsClass getFramsClass() {
28                return framsClass;
29        }
30
31        public void setFramsClass(FramsClass framsClass) {
32                this.framsClass = framsClass;
33        }
34        /**
35         * Simple String key, value class.
36         */
37        public static class Entry {
38
39                public final String key;
40                public final String value;
41
42                public Entry(String key, String value) {
43                        this.key = key;
44                        this.value = value;
45                }
46
47                @Override
48                public String toString() {
49                        return key + " = " + value;
50                }
51        }
52
53        protected FramsClass framsClass;
54
55        @Override
56        public String getId() {
57                return framsClass.getId();
58        }
59
60        @Override
61        public int getParamCount() {
62                return framsClass.getParamCount();
63        }
64
65        @Override
66        public Param getParam(int i) {
67                return framsClass.getParamEntry(i, Param.class);
68        }
69
70        @Override
71        public Param getParam(String id) {
72                return framsClass.getParamEntry(id, Param.class);
73        }
74
75        @Override
76        public Param getGroupMember(int gi, int n) {
77                return framsClass.getGroupMember(gi, n);
78        }
79
80        @Override
81        public <T> T get(int i, Class<T> type) {
82                Param p = getParam(i);
83                assert p instanceof ValueParam;
84                return get((ValueParam) p, type);
85        }
86
87        @Override
88        public <T> T get(String id, Class<T> type) {
89                Param p = getParam(id);
90                assert p instanceof ValueParam;
91                return get((ValueParam) p, type);
92        }
93
94        @Override
95        public <T> int set(String id, T value) {
96                Param p = getParam(id);
97                assert p instanceof ValueParam;
98                return set((ValueParam) p, value);
99        }
100
101        @Override
102        public <T> int set(int i, T value) {
103                Param p = getParam(i);
104                assert p instanceof ValueParam;
105                return set((ValueParam) p, value);
106        }
107
108        @Override
109        public <T> int set(ValueParam param, T value) {
110                int flags = 0;
111
112                //String id = param.getEffectiveId();
113                try {
114                        Object oldValue = get(param, param.getStorageType());
115                        ReassignResult<?> result = param.reassign(value, oldValue);
116                        Object casted = result.getValue();
117                        if (casted != oldValue) {
118                                internalSet(param, casted);
119                        }
120                        flags = result.getFlags();
121                } catch (CastFailure e) {
122                        log.error("casting failure while set: ", e);
123                }
124                return flags;
125        }
126
127        @Override
128        public void setDefault(boolean numericOnly) {
129                for (int i = 0; i < framsClass.getParamCount(); i++) {
130                        setDefault(i, numericOnly);
131                }
132        }
133
134        @Override
135        public void setDefault(int i, boolean numericOnly) {
136                ValueParam entry = framsClass.getParamEntry(i, ValueParam.class);
137                if ((entry != null)     && (!numericOnly || entry.isNumeric())) {
138                        set(i, entry.getDef(entry.getStorageType()));
139                }
140        }
141
142        @Override
143        public void setMin() {
144                for (int i = 0; i < framsClass.getParamCount(); i++) {
145                        setMin(i);
146                }
147        }
148
149        @Override
150        public void setMin(int i) {
151                PrimitiveParam entry = framsClass.getParamEntry(i, PrimitiveParam.class);
152                if (entry == null) {
153                        return;
154                }
155                Object min = entry.getMin(entry.getStorageType());
156                if (min != null) {
157                        set(i, min);
158                }
159        }
160
161        @Override
162        public void setMax() {
163                for (int i = 0; i < framsClass.getParamCount(); i++) {
164                        setMax(i);
165                }
166        }
167
168        @Override
169        public void setMax(int i) {
170                PrimitiveParam entry = framsClass.getParamEntry(i, PrimitiveParam.class);
171                if (entry == null) {
172                        return;
173                }
174                Object max = entry.getMax(entry.getStorageType());
175                if (max != null) {
176                        set(i, max);
177                }
178        }
179
180        @Override
181        public void copyFrom(AccessInterface src) {
182                clearValues();
183                //TODO: iterate over self, and pull from src
184                /*
185                for (int i = 0; i < src.getFramsClass().size(); i++) {
186                        this.set(i, src.get(i, Object.class));
187                }
188                */
189        }
190
191
192
193
194        @Override
195        public void save(SinkInterface sink) {
196                assert framsClass != null;
197                sink.print(framsClass.getId()).print(":").breakLine();
198                for (PrimitiveParam p : filterInstanceof(framsClass.getParamEntries(), PrimitiveParam.class)) {
199                        Object value = get(p, Object.class);
200                        if (value == null) {
201                                continue;
202                        }
203                        sink.print(p.getId()).print(":");
204                        p.save(sink, value);
205                        sink.breakLine();
206                }
207                sink.breakLine();
208        }
209
210        private Entry readEntry(SourceInterface source)
211                        throws IOException {
212
213                String line;
214                String key = null;
215                StringBuilder value = null;
216                while ((line = source.readLine()) != null)
217                {
218                        if (key == null) {
219                                int colonIndex = line.indexOf(':');
220                                if (colonIndex == -1) {
221                                        return null;
222                                }
223                                key = line.substring(0, colonIndex);
224                                String inlineValue = line.substring(colonIndex + 1);
225
226
227                                if (!inlineValue.startsWith("~")) {
228                                        return new Entry(key, inlineValue);
229                                }
230                                value = new StringBuilder();
231                                value.append(inlineValue.substring(1));
232                                continue;
233                        }
234                        if (value.length() != 0) {
235                                value.append(System.getProperty("line.separator"));
236                        }
237                        if (line.contains("~")) {
238                                value.append(line.substring(0, line.indexOf("~")));
239                                return new Entry(key, value.toString());
240                        }
241                        value.append(line);
242                        /*
243                        if (line.contains("~")) {
244                                String lastLine = line.substring(0, line.indexOf("~"));
245                                if (lastLine.length() > 0) {
246                                        appendToValue(value, lastLine);
247                                }
248                                return new Entry(key, value.toString());
249                        }
250                        appendToValue(value, line);
251                        */
252                }
253                return null;
254        }
255
256        @Override
257        public void load(SourceInterface source) throws Exception {
258                //TODO not clearing values, because get from manager gives only fields, not children
259                //this.clearValues();
260
261                Entry entry;
262                while ((entry = readEntry(source)) != null) {
263                        Param param = getParam(entry.key);
264                        if (param == null) {
265                                continue;
266                        }
267                        if (!(param instanceof ValueParam)) {
268                                log.warn("param " + param + " is not a ValueParam");
269                                continue;
270                        }
271                        if ((param.getFlags() & Flags.DONTLOAD) != 0) {
272                                log.debug("DontLoad flag was set - not loading...");
273                        } else {
274                                int retFlags = this.set((ValueParam) param, entry.value);
275                                if ((retFlags & (Flags.PSET_HITMIN | Flags.PSET_HITMAX)) != 0) {
276                                        String which = ((retFlags & Flags.PSET_HITMIN) != 0) ? "small" : "big";
277                                        log.warn("value of key '" + entry.key + "' was too " + which + ", adjusted");
278                                }
279                        }
280                }
281        }
282
283        protected abstract <T> void internalSet(ValueParam param, T value);
284
285        @Override
286        public Collection<Param> getParams() {
287                return framsClass.getParamEntries();
288        }
289
290        /*
291        protected <T extends Comparable<T>> int setAndCut(Param param, Object value, Class<T> type) {
292                int flags = 0;
293                T val = type.cast(value);
294                T min = param.getMin(type);
295                T max = param.getMax(type);
296                if (min != null && val.compareTo(min) < 0) {
297                        val = min;
298                        flags |= Flags.PSET_HITMIN;
299                }
300                if (max != null && val.compareTo(max) > 0) {
301                        val = max;
302                        flags |= Flags.PSET_HITMAX;
303                }
304                internalSet(param, val);
305                return flags;
306        }*/
307
308
309}
Note: See TracBrowser for help on using the repository browser.