source: java/main/src/main/java/com/framsticks/params/FramsClass.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: 8.1 KB
Line 
1package com.framsticks.params;
2
3import com.framsticks.params.types.StringParam;
4import com.framsticks.parsers.FileSource;
5import com.framsticks.parsers.Loaders;
6import com.framsticks.util.lang.Casting;
7
8import org.apache.log4j.Logger;
9
10import java.io.InputStream;
11import java.lang.reflect.*;
12import java.util.*;
13
14/**
15 * The class FramsClass represents the class / schema of connected parameters
16 * (such as parameters within the class). It differs from C++ version by storing
17 * information about the class that parameters belong to.
18 *
19 * Based loosely on c++ class Param located in cpp/gdk/param.*
20 *
21 * @author Jarek Szymczak <name.surname@gmail.com>, Mateusz Jarus (please
22 *         replace name and surname with my personal data)
23 *
24 * @author Piotr Sniegowski
25 */
26public final class FramsClass {
27
28        private final static Logger log = Logger.getLogger(FramsClass.class.getName());
29
30        /**
31          * The Class which represents group.
32          */
33
34        /** The offset of the parameter (applied for newly added parameter). */
35        protected int fieldsNumber;
36
37        /** The groups. */
38        protected List<Group> groups = new ArrayList<Group>();
39
40        /**
41         * The param entry map <parameterId, param> (for fast accessing of parameters
42         * by their name)
43         */
44        protected Map<String, Param> paramEntryMap = new LinkedHashMap<String, Param>();
45
46        /** The param list (for accessing parameters by offset in O(1) time. */
47        protected List<Param> paramList = new ArrayList<Param>();
48
49        /** The param getId map (for fast lookup of offset based on name */
50        protected Map<String, Integer> paramIdMap = new HashMap<String, Integer>();
51
52        protected String id;
53
54        protected String name;
55
56        protected String description;
57
58        public Collection<Param> getParamEntries() {
59                return paramList;
60        }
61
62        public FramsClass() {
63        }
64
65        public FramsClass(String id, String name, String description) {
66                this.setId(id);
67                this.setName(name);
68                this.setDescription(description);
69        }
70
71        /**
72         * Adds new param entry.
73         *
74         * @param param
75         *            the new param entry
76         */
77        public FramsClass append(Param param) {
78                paramEntryMap.put(param.getId(), param);
79                //paramEntryMap.put(param.getInternalId(), param);
80                paramList.add(param);
81                try {
82                        Group group = groups.get(param.getGroup());
83                        if (group != null) {
84                                group.addProperty(param);
85                        }
86                } catch (IndexOutOfBoundsException ignored) {
87
88                }
89
90                return this;
91        }
92
93        /**
94         * Adds new group.
95         */
96        public FramsClass appendGroup(Group group) {
97                groups.add(group);
98                return this;
99        }
100
101        public String getDescription() {
102                return description;
103        }
104
105        public int getGroupCount() {
106                return groups.size();
107        }
108
109        /**
110         * Gets the group member.
111         *
112         * @param gi
113         *            the offset of group
114         * @param pi
115         *            the offset of member within a group
116         * @return the pi-th member of group gi
117         */
118        public Param getGroupMember(int gi, int pi) {
119                if (gi < 0 || pi < 0 || gi >= groups.size()) {
120                        return null;
121                }
122                Group group = groups.get(gi);
123                return (group != null ? group.getProperty(pi) : null);
124        }
125
126        /**
127         * Gets the group getName.
128         *
129         * @param gi
130         *            the offset of group
131         * @return the group getName
132         */
133        public String getGroupName(int gi) {
134                if (gi < 0 || gi >= groups.size())
135                        return null;
136                return groups.get(gi).name;
137        }
138
139        public String getId() {
140                return id;
141        }
142
143        public String getName() {
144                return name;
145        }
146
147        public String getNiceName() {
148                return name != null ? name : id;
149        }
150
151        /**
152         * Gets the param entry.
153         *
154         * @param i
155         *            the offset of parameter
156         * @return the param entry
157         */
158        public <T extends Param> T getParamEntry(int i, Class<T> type) {
159                if (i < 0 || i >= paramList.size()) {
160                        return null;
161                }
162                return Casting.tryCast(type, paramList.get(i));
163        }
164
165        /**
166         * Gets the param entry.
167         *
168         * @param id
169         *            the getId of parameter
170         * @return the param entry
171         */
172        public <T extends Param> T getParamEntry(String id, Class<T> type) {
173                return Casting.tryCast(type, paramEntryMap.get(id));
174        }
175
176        public int getParamCount() {
177                return paramList.size();
178        }
179
180        @Override
181        public String toString() {
182                return id;
183        }
184
185        public static FramsClass getFramsClass() {
186                return new FramsClass("class", "class", null)
187                        .append(new ParamBuilder().setId("name").setName("Name").setType(StringParam.class).build())
188                        .append(new ParamBuilder().setId("id").setName("id").setType(StringParam.class).build())
189                        .append(new ParamBuilder().setId("desc").setName("Description").setType(StringParam.class).build());
190        }
191
192        public void setId(String id) {
193                this.id = id;
194        }
195
196        public void setName(String name) {
197                this.name = name;
198        }
199
200        public void setDescription(String description) {
201                this.description = description;
202        }
203
204        public static String getParamTypeForNativeType(Type type) {
205                if (type instanceof ParameterizedType) {
206                        ParameterizedType p = (ParameterizedType) type;
207                        Type rawType = p.getRawType();
208                        if (rawType.equals(Map.class)) {
209                                Type containedType = p.getActualTypeArguments()[1];
210                                //TODO uid should be passed along during construction
211                                if (containedType instanceof Class) {
212                                        return "l " + ((Class<?>) containedType).getCanonicalName() + " name";
213                                }
214                        }
215                        if (rawType.equals(List.class)) {
216                                Type containedType = p.getActualTypeArguments()[0];
217                                if (containedType instanceof Class) {
218                                        return "l " + ((Class<?>) containedType).getCanonicalName();
219                                }
220                        }
221                        return null;
222                }
223
224                if (type.equals(Integer.class)) {
225                        return "d";
226                }
227                if (type.equals(String.class)) {
228                        return "s";
229                }
230                if (type.equals(Double.class)) {
231                        return "f";
232                }
233                if (type instanceof Class) {
234                        return "o " + ((Class<?>) type).getCanonicalName();
235                }
236                return null;
237        }
238
239        public static final String GENERATE_HELP_PREFIX = "automatically generated from: ";
240
241        public static FramsClass readFromStream(InputStream stream) {
242                return Loaders.loadFramsClass(new FileSource(stream));
243        }
244
245        public static class Constructor {
246                protected final FramsClass result;
247                protected Class<?> currentClass;
248
249                public Constructor(Class<?> src, String name) {
250                        result = new FramsClass(name, name, GENERATE_HELP_PREFIX + src.toString());
251                        currentClass = src;
252                        while (currentClass != null) {
253                                try {
254                                        currentClass.getMethod("constructFramsClass", Constructor.class).invoke(null, this);
255                                } catch (Exception ignored) {
256                                }
257                                currentClass = currentClass.getSuperclass();
258                        }
259                        currentClass = src;
260                }
261
262                public final FramsClass getResult() {
263                        return result;
264                }
265
266                public Constructor allFields() {
267                        for (Field f : currentClass.getFields()) {
268                                field(f.getName());
269                        }
270                        return this;
271                }
272
273                public Constructor method(String name, Class<?> ... arguments) {
274                        try {
275                                Method method = currentClass.getMethod(name, arguments);
276                                if (!Modifier.isPublic(method.getModifiers())) {
277                                        return this;
278                                }
279                                String returnParamClass = getParamTypeForNativeType(method.getGenericReturnType());
280                                if (returnParamClass == null) {
281                                        return this;
282                                }
283                                Class<?>[] args = method.getParameterTypes();
284                                if (args.length == 0) {
285                                        if (method.getName().startsWith("get")) {
286                                                String fieldName = method.getName().substring(3, 4).toLowerCase() + method.getName().substring(4);
287                                                Param param = new ParamBuilder().setType(returnParamClass).setName(fieldName).setId(fieldName).setHelp(GENERATE_HELP_PREFIX + method.toString()).build();
288                                                assert param != null;
289                                                result.append(param);
290                                                return this;
291                                        }
292                                        return this;
293                                }
294                                return this;
295                        } catch (NoSuchMethodException e) {
296                                log.fatal("method " + name + " was not found in " + currentClass.toString());
297                        }
298                        return this;
299                }
300                public Constructor field(String name) {
301                        try {
302                                Field field = currentClass.getField(name);
303                                if (!Modifier.isPublic(field.getModifiers())) {
304                                        return this;
305                                }
306                                String paramClass = getParamTypeForNativeType(field.getGenericType());
307                                if (paramClass == null) {
308                                        return this;
309                                }
310                                Param param = new ParamBuilder().setType(paramClass).setName(field.getName()).setId(field.getName()).setHelp(GENERATE_HELP_PREFIX + field.toString()).build();
311                                assert param != null;
312                                result.append(param);
313                                return this;
314                        } catch (NoSuchFieldException e) {
315                                log.fatal("field " + name + " was not found in " + currentClass.toString());
316                        }
317                        return this;
318                }
319        }
320}
Note: See TracBrowser for help on using the repository browser.