source: trunk/src/org/expeditee/settings/Settings.java@ 656

Last change on this file since 656 was 656, checked in by jts21, 10 years ago

Implement tooltips

File size: 7.1 KB
Line 
1package org.expeditee.settings;
2
3import java.lang.reflect.Field;
4import java.lang.reflect.Method;
5import java.util.HashMap;
6import java.util.LinkedList;
7import java.util.List;
8
9import org.expeditee.gui.AttributeValuePair;
10import org.expeditee.gui.DisplayIO;
11import org.expeditee.gui.Frame;
12import org.expeditee.gui.FrameIO;
13import org.expeditee.gui.FrameUtils;
14import org.expeditee.items.Item;
15import org.expeditee.items.Text;
16import org.expeditee.items.widgets.Password;
17import org.expeditee.reflection.PackageLoader;
18import org.expeditee.setting.GenericSetting;
19import org.expeditee.setting.Setting;
20import org.expeditee.setting.VariableSetting;
21
22public abstract class Settings {
23
24 public static final String SETTINGS_PACKAGE_PARENT = "org.expeditee.";
25 public static final String SETTINGS_PACKAGE = SETTINGS_PACKAGE_PARENT + "settings.";
26
27 private static final class PageDescriptor {
28
29 private final HashMap<String, Setting> settings = new HashMap<String, Setting>();
30 private final List<String> orderedEntries = new LinkedList<String>();
31 private final List<VariableSetting> hasDefault = new LinkedList<VariableSetting>();
32 private final Method onParsed;
33
34 private PageDescriptor(Class<?> clazz) {
35
36 // populate map of settings
37 for(Field f : clazz.getFields()) {
38 // only allow valid types
39 if(! Setting.class.isAssignableFrom(f.getType())) {
40 continue;
41 }
42 try {
43 Setting s = (Setting) f.get(null);
44 settings.put(f.getName().toLowerCase(), s);
45 if(s instanceof VariableSetting && ((VariableSetting) s).hasDefault()) {
46 hasDefault.add((VariableSetting) s);
47 }
48 orderedEntries.add(f.getName());
49 } catch (Exception e) {
50 e.printStackTrace();
51 }
52 }
53 Method m = null;
54 try {
55 m = clazz.getMethod("onParsed", Text.class);
56 } catch(Exception e) {
57 // System.err.println(clazz.getName() + " has no onParsed(Text t) callback");
58 }
59 this.onParsed = m;
60 }
61 }
62 private static HashMap<String, PageDescriptor> _pages = new HashMap<String, PageDescriptor>();
63
64 public static void Init() {
65 try {
66 for(Class<?> clazz : PackageLoader.getClassesNew(SETTINGS_PACKAGE)) {
67 // Ignore this class since it's the controller
68 if(clazz.equals(Settings.class)) {
69 continue;
70 }
71 String settingsPage = clazz.getPackage().getName().toLowerCase().substring(SETTINGS_PACKAGE_PARENT.length());
72 // System.out.println(settingsPage + " : " + clazz.getName());
73 _pages.put(settingsPage, new PageDescriptor(clazz));
74 }
75
76 } catch (Exception e) {
77 e.printStackTrace();
78 }
79 FrameUtils.ParseProfile(FrameIO.LoadProfile(UserSettings.ProfileName.get()));
80 }
81
82 public static void parseSettings(Text text) {
83 parseSettings(text, "");
84 }
85
86 /**
87 * Sets all the simple settings.
88 *
89 * @param text
90 * @param prefix
91 */
92 private static void parseSettings(Text text, String prefix) {
93
94 Frame child = text.getChild();
95 if(child == null) {
96 return;
97 }
98 String settingsPage = prefix + text.getText().trim().toLowerCase().replaceAll("^@", "");
99 PageDescriptor pd = _pages.get(settingsPage);
100 if(pd == null) {
101 return;
102 }
103 try {
104 if (text.getChild() == null) {
105 return;
106 }
107 List<VariableSetting> toDefault = new LinkedList<VariableSetting>(pd.hasDefault);
108 // set the fields
109 for (Text t : text.getChild().getBodyTextItems(true)) {
110 AttributeValuePair avp = new AttributeValuePair(t.getText(), false);
111 try {
112 // System.out.println(avp.getAttributeOrValue().trim().toLowerCase().replaceAll("^@", ""));
113 Setting s = pd.settings.get(avp.getAttributeOrValue().trim().toLowerCase().replaceAll("^@", ""));
114 if(s == null) {
115 continue;
116 }
117 if(s.setSetting(t)) {
118 toDefault.remove(s);
119 }
120 } catch (Exception e) {
121 e.printStackTrace();
122 }
123 }
124 // set any unset fields to their default
125 for(VariableSetting s : toDefault) {
126 try {
127 // System.out.println("Resetting " + s.getTooltip());
128 s.reset();
129 // System.out.println("Set " + f.getName() + " to default value " + f.get(null));
130 } catch (Exception e) {
131 e.printStackTrace();
132 }
133 }
134 // call the onParsed method if one exists
135 if(pd.onParsed != null) {
136 pd.onParsed.invoke(null, text);
137 }
138 } catch (Exception e) {
139 e.printStackTrace();
140 return;
141 }
142 // if the page was a settings page, check if it has any subpages
143 for(Text t : child.getTextItems()) {
144 parseSettings(t, settingsPage + ".");
145 }
146 }
147
148 public static Text generateSettingsTree() {
149 return generateSettingsTree("settings", DisplayIO.getCurrentFrame().createNewText("Settings"));
150 }
151
152 /**
153 *
154 * Generates settings tree
155 * TODO: merge with @see FrameCreator
156 *
157 */
158 private static Text generateSettingsTree(String page, Text text) {
159 Frame frame = FrameIO.CreateNewFrame(text);
160 text.setLink(frame.getName());
161
162 int x = 50, y = 100;
163 int dY = UserSettings.ItemTemplate.get().getPolygon().getBounds().height + 1;
164 int maxY = UserSettings.InitialHeight.get() - 150;
165
166 // add subpages of the current page
167 for(String k : _pages.keySet()) {
168 if(k.startsWith(page.toLowerCase()) && !k.equals(page)) {
169 String name = k.substring(page.length() + 1);
170 if(name.indexOf('.') != -1) {
171 continue;
172 }
173 System.out.println("Generating " + name);
174 generateSettingsTree(k, frame.addText(x, y, name.substring(0, 1).toUpperCase() + name.substring(1), null));
175 y += dY;
176 if(y >= maxY) {
177 x += 300;
178 y = 100;
179 }
180 }
181 }
182
183 x += 200;
184 y = 100;
185
186 // add settings of the current page
187 PageDescriptor pd = _pages.get(page);
188 if(pd != null) {
189 for(String str : pd.orderedEntries) {
190 String key = str.toLowerCase();
191 Setting s = pd.settings.get(key);
192 if(s == null) {
193 continue;
194 }
195 String name = str.substring(0, 1).toUpperCase() + str.substring(1);
196 String value = "";
197 if(s instanceof GenericSetting && ((GenericSetting) s).isPrimitive()) {
198 System.out.println("VALUE: " + ((GenericSetting) s).get());
199 if(((GenericSetting) s).get() != null) {
200 value = ": " + ((GenericSetting) s).get();
201 } else {
202 value = ": ";
203 }
204 }
205 if(key.equals("pass")) {
206 Text t = frame.addText(x, y, "iw: org.expeditee.items.widgets.Password", null);
207 Password pw = new Password(t, null);
208 pw.setPassword((String) value);
209 frame.removeItem(t);
210 frame.addAllItems(pw.getItems());
211 y += pw.getHeight();
212 } else if(s instanceof GenericSetting && ((GenericSetting) s).getType().equals(Text.class)) {
213 Text t = ((GenericSetting<Text>) s).get();
214 if(t == null) continue;
215 t = t.copy();
216 int h = t.getPolygon().getBounds().height + 1;
217 t.setID(frame.getNextItemID());
218 t.setX(x);
219 t.setY(y + h / 2);
220 t.setText(name);
221 t.setTooltip(s.getTooltip());
222 frame.addItem(t);
223 y += h;
224 } else {
225 Text t = frame.addText(x, y + dY / 2, name + value, null);
226 t.setTooltip(s.getTooltip());
227 y += dY;
228 }
229 if(y >= maxY) {
230 x += 300;
231 y = 100;
232 }
233 }
234 }
235
236 FrameIO.SaveFrame(frame);
237 return text;
238 }
239}
Note: See TracBrowser for help on using the repository browser.