source: trunk/src/org/expeditee/setting/GenericSetting.java@ 1209

Last change on this file since 1209 was 1209, checked in by bln4, 5 years ago
File size: 3.9 KB
Line 
1/**
2 * GenericSetting.java
3 * Copyright (C) 2010 New Zealand Digital Library, http://expeditee.org
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19package org.expeditee.setting;
20
21import org.expeditee.gui.AttributeValuePair;
22import org.expeditee.items.Text;
23
24/**
25 * Generic setting class for storing simple settings
26 *
27 * @author jts21
28 *
29 * @param <T> type of the setting
30 */
31public abstract class GenericSetting<T> extends VariableSetting {
32
33 protected final Class<T> _type;
34 protected T _default;
35 protected T _value;
36
37 /**
38 * Instantiates a new setting
39 * @param type Reference to the type of the setting, since Java's generics implementation apparently got
40 * dropped on it's head at some point, and we can't know what type the generic object is at runtime without
41 * manually passing a reference to the class
42 * @param tooltip Tooltip text to display (once tooltips are implemented)
43 * @param value Default value
44 */
45 public GenericSetting(Class<T> type, String tooltip, T value) {
46 super(tooltip);
47 _type = type;
48 _default = value;
49 _value = value;
50 }
51
52 /**
53 * Instantiates a new setting with no default value
54 */
55 public GenericSetting(Class<T> type, String tooltip) {
56 this(type, tooltip, (T) saneInitialValue(type));
57 }
58
59 /**
60 * @return true if this setting is a String, Number or Boolean
61 */
62 public boolean isPrimitive() {
63 return _type.equals(String.class) || _type.equals(Integer.class) || _type.equals(Float.class) || _type.equals(Double.class) || _type.equals(Boolean.class);
64 }
65
66 /**
67 * Provides a sane initial value for settings with no default value
68 */
69 private static Object saneInitialValue(Class<?> type) {
70 if(type.equals(String.class)) {
71 return "";
72 } else if(type.equals(Integer.class) || type.equals(Float.class) || type.equals(Double.class)) {
73 return 0;
74 } else if(type.equals(Boolean.class)) {
75 return false;
76 }
77 return null;
78 }
79
80 /**
81 * @return the type of this setting
82 */
83 public Class<T> getType() {
84 return _type;
85 }
86
87 /**
88 * @return the value of this setting
89 */
90 public T get() {
91 return _value;
92 }
93
94 /**
95 * Sets the value directly
96 */
97 public void set(T value) {
98 _value = value;
99 }
100
101 public boolean setSetting(Text text) {
102 AttributeValuePair avp = new AttributeValuePair(text.getText(), false);
103 if(avp.hasAttribute() && avp.getValue().trim().length() != 0) {
104 if(_type.equals(String.class)) {
105 String value = avp.getValue();
106 if(value.trim().length() == 0) {
107 return false;
108 }
109 set((T) value);
110 } else if(_type.equals(Integer.class)) {
111 set((T) avp.getIntegerValue());
112 } else if(_type.equals(Float.class)) {
113 set((T) (Float) avp.getDoubleValue().floatValue());
114 } else if(_type.equals(Double.class)) {
115 set((T) avp.getDoubleValue());
116 } else if(_type.equals(Boolean.class)) {
117 set((T) (Boolean) avp.getBooleanValue());
118 } else if(_type.equals(Text.class)) {
119 set((T) text);
120 } else {
121 System.err.println("Invalid type: " + _type.getName());
122 return false;
123 }
124 return true;
125 }
126 return false;
127 }
128
129 public void setDefault(T value) {
130 _default = value;
131 }
132
133 /**
134 * Reset the value back to the default, if a default value was specified
135 */
136 public void reset() {
137 _value = _default;
138 }
139
140}
Note: See TracBrowser for help on using the repository browser.