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

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

Auto-generate settings tree when creating a new profile

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 private static boolean _init = false;
65 public static void Init() {
66 if(_init) return;
67 _init = true;
68 try {
69 for(Class<?> clazz : PackageLoader.getClassesNew(SETTINGS_PACKAGE)) {
70 // Ignore this class since it's the controller
71 if(clazz.equals(Settings.class)) {
72 continue;
73 }
74 String settingsPage = clazz.getPackage().getName().toLowerCase().substring(SETTINGS_PACKAGE_PARENT.length());
75 // System.out.println(settingsPage + " : " + clazz.getName());
76 _pages.put(settingsPage, new PageDescriptor(clazz));
77 }
78
79 } catch (Exception e) {
80 e.printStackTrace();
81 }
82 FrameUtils.ParseProfile(FrameIO.LoadProfile(UserSettings.ProfileName.get()));
83 }
84
85 public static void parseSettings(Text text) {
86 parseSettings(text, "");
87 }
88
89 /**
90 * Sets all the simple settings.
91 *
92 * @param text
93 * @param prefix
94 */
95 private static void parseSettings(Text text, String prefix) {
96
97 Frame child = text.getChild();
98 if(child == null) {
99 return;
100 }
101 String settingsPage = prefix + text.getText().trim().toLowerCase().replaceAll("^@", "");
102 PageDescriptor pd = _pages.get(settingsPage);
103 if(pd == null) {
104 return;
105 }
106 try {
107 if (text.getChild() == null) {
108 return;
109 }
110 List<VariableSetting> toDefault = new LinkedList<VariableSetting>(pd.hasDefault);
111 // set the fields
112 for (Text t : text.getChild().getBodyTextItems(true)) {
113 AttributeValuePair avp = new AttributeValuePair(t.getText(), false);
114 try {
115 // System.out.println(avp.getAttributeOrValue().trim().toLowerCase().replaceAll("^@", ""));
116 Setting s = pd.settings.get(avp.getAttributeOrValue().trim().toLowerCase().replaceAll("^@", ""));
117 if(s == null) {
118 continue;
119 }
120 if(s.setSetting(t)) {
121 toDefault.remove(s);
122 }
123 } catch (Exception e) {
124 e.printStackTrace();
125 }
126 }
127 // set any unset fields to their default
128 for(VariableSetting s : toDefault) {
129 try {
130 // System.out.println("Resetting " + s.getTooltip());
131 s.reset();
132 // System.out.println("Set " + f.getName() + " to default value " + f.get(null));
133 } catch (Exception e) {
134 e.printStackTrace();
135 }
136 }
137 // call the onParsed method if one exists
138 if(pd.onParsed != null) {
139 pd.onParsed.invoke(null, text);
140 }
141 } catch (Exception e) {
142 e.printStackTrace();
143 return;
144 }
145 // if the page was a settings page, check if it has any subpages
146 for(Text t : child.getTextItems()) {
147 parseSettings(t, settingsPage + ".");
148 }
149 }
150
151 public static void generateSettingsTree(Text link) {
152 generateSettingsTree("settings", link);
153 }
154
155 /**
156 *
157 * Generates settings tree
158 * TODO: merge with @see FrameCreator
159 *
160 */
161 private static void generateSettingsTree(String page, Text text) {
162 Frame frame = FrameIO.CreateFrame(text.getParentOrCurrentFrame().getFramesetName(), page, null);
163 text.setLink(frame.getName());
164
165 int x = 50, y = 100;
166 int dY = UserSettings.ItemTemplate.get().getPolygon().getBounds().height + 1;
167 int maxY = UserSettings.InitialHeight.get() - 150;
168
169 // add subpages of the current page
170 for(String k : _pages.keySet()) {
171 if(k.startsWith(page.toLowerCase()) && !k.equals(page)) {
172 String name = k.substring(page.length() + 1);
173 if(name.indexOf('.') != -1) {
174 continue;
175 }
176 System.out.println("Generating " + name);
177 generateSettingsTree(k, frame.addText(x, y, name.substring(0, 1).toUpperCase() + name.substring(1), null));
178 y += dY;
179 if(y >= maxY) {
180 x += 300;
181 y = 100;
182 }
183 }
184 }
185
186 x += 200;
187 y = 100;
188
189 // add settings of the current page
190 PageDescriptor pd = _pages.get(page);
191 if(pd != null) {
192 for(String str : pd.orderedEntries) {
193 String key = str.toLowerCase();
194 Setting s = pd.settings.get(key);
195 if(s == null) {
196 continue;
197 }
198 String name = str.substring(0, 1).toUpperCase() + str.substring(1);
199 String value = "";
200 if(s instanceof GenericSetting && ((GenericSetting) s).isPrimitive()) {
201 if(((GenericSetting) s).get() != null) {
202 value = ": " + ((GenericSetting) s).get();
203 } else {
204 value = ": ";
205 }
206 }
207 if(key.equals("pass")) {
208 Text t = frame.addText(x, y, "iw: org.expeditee.items.widgets.Password", null);
209 Password pw = new Password(t, null);
210 pw.setPassword((String) value);
211 frame.removeItem(t);
212 frame.addAllItems(pw.getItems());
213 y += pw.getHeight();
214 } else if(s instanceof GenericSetting && ((GenericSetting) s).getType().equals(Text.class)) {
215 Text t = ((GenericSetting<Text>) s).get();
216 if(t == null) continue;
217 t = t.copy();
218 int h = t.getPolygon().getBounds().height + 1;
219 t.setID(frame.getNextItemID());
220 t.setX(x);
221 t.setY(y + h / 2);
222 t.setText(name);
223 t.setTooltip(s.getTooltip());
224 frame.addItem(t);
225 y += h;
226 } else {
227 Text t = frame.addText(x, y + dY / 2, name + value, null);
228 t.setTooltip(s.getTooltip());
229 y += dY;
230 }
231 if(y >= maxY) {
232 x += 300;
233 y = 100;
234 }
235 }
236 }
237
238 FrameIO.SaveFrame(frame);
239 }
240}
Note: See TracBrowser for help on using the repository browser.