1 | package org.expeditee.setting;
|
---|
2 |
|
---|
3 | import org.expeditee.gui.AttributeValuePair;
|
---|
4 | import org.expeditee.items.Item;
|
---|
5 | import org.expeditee.items.Text;
|
---|
6 |
|
---|
7 | /**
|
---|
8 | * Generic setting class for storing simple settings
|
---|
9 | *
|
---|
10 | * @author jts21
|
---|
11 | *
|
---|
12 | * @param <T> type of the setting
|
---|
13 | */
|
---|
14 | public class GenericSetting<T> extends VariableSetting {
|
---|
15 |
|
---|
16 | protected final Class<T> _type;
|
---|
17 | protected final T _default;
|
---|
18 | protected T _value;
|
---|
19 |
|
---|
20 | /**
|
---|
21 | * Instantiates a new setting
|
---|
22 | * @param type Reference to the type of the setting, since Java's generics implementation apparently got
|
---|
23 | * dropped on it's head at some point, and we can't know what type the generic object is at runtime without
|
---|
24 | * manually passing a reference to the class
|
---|
25 | * @param tooltip Tooltip text to display (once tooltips are implemented)
|
---|
26 | * @param value Default value
|
---|
27 | */
|
---|
28 | public GenericSetting(Class<T> type, String tooltip, T value) {
|
---|
29 | super(tooltip);
|
---|
30 | _type = type;
|
---|
31 | _default = value;
|
---|
32 | _value = value;
|
---|
33 | _hasDefault = true;
|
---|
34 | }
|
---|
35 |
|
---|
36 | /**
|
---|
37 | * Instantiates a new setting with no default value
|
---|
38 | */
|
---|
39 | public GenericSetting(Class<T> type, String tooltip) {
|
---|
40 | this(type, tooltip, (T) saneInitialValue(type));
|
---|
41 | _hasDefault = false;
|
---|
42 | }
|
---|
43 |
|
---|
44 | /**
|
---|
45 | * @return true if this setting is a String, Number or Boolean
|
---|
46 | */
|
---|
47 | public boolean isPrimitive() {
|
---|
48 | return _type.equals(String.class) || _type.equals(Integer.class) || _type.equals(Float.class) || _type.equals(Double.class) || _type.equals(Boolean.class);
|
---|
49 | }
|
---|
50 |
|
---|
51 | /**
|
---|
52 | * Provides a sane initial value for settings with no default value
|
---|
53 | */
|
---|
54 | private static Object saneInitialValue(Class<?> type) {
|
---|
55 | if(type.equals(String.class)) {
|
---|
56 | return "";
|
---|
57 | } else if(type.equals(Integer.class) || type.equals(Float.class) || type.equals(Double.class)) {
|
---|
58 | return 0;
|
---|
59 | } else if(type.equals(Boolean.class)) {
|
---|
60 | return false;
|
---|
61 | }
|
---|
62 | return null;
|
---|
63 | }
|
---|
64 |
|
---|
65 | /**
|
---|
66 | * @return the type of this setting
|
---|
67 | */
|
---|
68 | public Class<T> getType() {
|
---|
69 | return _type;
|
---|
70 | }
|
---|
71 |
|
---|
72 | /**
|
---|
73 | * @return the value of this setting
|
---|
74 | */
|
---|
75 | public T get() {
|
---|
76 | return _value;
|
---|
77 | }
|
---|
78 |
|
---|
79 | /**
|
---|
80 | * Sets the value directly
|
---|
81 | */
|
---|
82 | public void set(T value) {
|
---|
83 | _value = value;
|
---|
84 | }
|
---|
85 |
|
---|
86 | public boolean setSetting(Text text) {
|
---|
87 | AttributeValuePair avp = new AttributeValuePair(text.getText(), false);
|
---|
88 | if(avp.hasAttribute() && avp.getValue().trim().length() != 0) {
|
---|
89 | if(_type.equals(String.class)) {
|
---|
90 | String value = avp.getValue();
|
---|
91 | if(value.trim().length() == 0) {
|
---|
92 | return false;
|
---|
93 | }
|
---|
94 | set((T) value);
|
---|
95 | } else if(_type.equals(Integer.class)) {
|
---|
96 | set((T) avp.getIntegerValue());
|
---|
97 | } else if(_type.equals(Float.class)) {
|
---|
98 | set((T) (Float) avp.getDoubleValue().floatValue());
|
---|
99 | } else if(_type.equals(Double.class)) {
|
---|
100 | set((T) avp.getDoubleValue());
|
---|
101 | } else if(_type.equals(Boolean.class)) {
|
---|
102 | set((T) (Boolean) avp.getBooleanValue());
|
---|
103 | } else if(_type.equals(Text.class)) {
|
---|
104 | set((T) text);
|
---|
105 | } else {
|
---|
106 | System.err.println("Invalid type: " + _type.getName());
|
---|
107 | return false;
|
---|
108 | }
|
---|
109 | return true;
|
---|
110 | }
|
---|
111 | return false;
|
---|
112 | }
|
---|
113 |
|
---|
114 | /**
|
---|
115 | * Reset the value back to the default, if a default value was specified
|
---|
116 | */
|
---|
117 | public void reset() {
|
---|
118 | if(!_hasDefault) {
|
---|
119 | return;
|
---|
120 | }
|
---|
121 | if(_default instanceof Text) {
|
---|
122 | int id = ((Item) _value).getID();
|
---|
123 | if(id < 0) {
|
---|
124 | if(((Item) _value).getParentOrCurrentFrame() != null) {
|
---|
125 | id = ((Item) _value).getParentOrCurrentFrame().getNextItemID();
|
---|
126 | } else {
|
---|
127 | id = 0;
|
---|
128 | }
|
---|
129 | }
|
---|
130 | _value = (T) ((Text) _default).copy();
|
---|
131 | ((Item) _value).setID(id);
|
---|
132 | } else {
|
---|
133 | _value = _default;
|
---|
134 | }
|
---|
135 | }
|
---|
136 |
|
---|
137 | }
|
---|