Changeset 655 for trunk/src/org/expeditee/settings/Settings.java
- Timestamp:
- 01/07/14 10:59:52 (10 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/expeditee/settings/Settings.java
r641 r655 3 3 import java.lang.reflect.Field; 4 4 import java.lang.reflect.Method; 5 import java.lang.reflect.Modifier;6 5 import java.util.HashMap; 7 6 import java.util.LinkedList; … … 13 12 import org.expeditee.gui.FrameIO; 14 13 import org.expeditee.gui.FrameUtils; 14 import org.expeditee.items.Item; 15 15 import org.expeditee.items.Text; 16 16 import org.expeditee.items.widgets.Password; 17 17 import org.expeditee.reflection.PackageLoader; 18 import org.expeditee.setting.GenericSetting; 19 import org.expeditee.setting.Setting; 20 import org.expeditee.setting.VariableSetting; 18 21 19 22 public abstract class Settings { … … 24 27 private static final class PageDescriptor { 25 28 26 private static final class FieldSet { 27 28 private final Field field; 29 private final Method method; 30 31 private FieldSet(Field field, Method method) { 32 this.field = field; 33 this.method = method; 34 } 35 } 36 37 private final HashMap<String, FieldSet> settings = new HashMap<String, FieldSet>(); 38 private final List<Field> hasDefault = new LinkedList<Field>(); 29 private final HashMap<String, Setting> settings = new HashMap<String, Setting>(); 39 30 private final List<String> orderedEntries = new LinkedList<String>(); 31 private final List<VariableSetting> hasDefault = new LinkedList<VariableSetting>(); 40 32 private final Method onParsed; 41 33 42 34 private PageDescriptor(Class<?> clazz) { 43 35 44 // populate map of settings with methods 45 for(Method m : clazz.getMethods()) { 46 // method name must start with "set", and method must take 1 Text item as parameter 47 if(m.getName().toLowerCase().startsWith("set") && m.getParameterTypes().length == 1 && m.getParameterTypes()[0] == Text.class) { 48 // System.out.println(m.getName().toLowerCase()); 49 settings.put(m.getName().substring(3).toLowerCase(), new FieldSet(null, m)); 50 if(!orderedEntries.contains(m.getName().substring(3))) { 51 orderedEntries.add(m.getName().substring(3)); 52 } 53 } 54 } 55 // populate map of settings with fields 36 // populate map of settings 56 37 for(Field f : clazz.getFields()) { 57 Class<?> type = f.getType();58 38 // only allow valid types 59 if(type != String.class && 60 type != Boolean.class && type != boolean.class && 61 type != Integer.class && type != int.class && 62 type != Double.class && type != double.class && 63 type != Float.class && type != float.class) { 64 continue; 65 } 66 FieldSet fs = settings.get(f.getName().toLowerCase()); 67 if(fs == null) { 68 settings.put(f.getName().toLowerCase(), new FieldSet(f, null)); 69 } else { 70 settings.put(f.getName().toLowerCase(), new FieldSet(f, fs.method)); 71 } 72 if(!orderedEntries.contains(f.getName())) { 73 orderedEntries.add(f.getName()); 74 } 75 } 76 // check which fields have default values 77 for(Field f : clazz.getFields()) { 78 if(!Modifier.isFinal(f.getModifiers())) { 79 if(this.settings.get("default" + f.getName().toLowerCase()) != null) { 80 this.hasDefault.add(f); 81 } 82 } 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 } 83 52 } 84 53 Method m = null; … … 108 77 e.printStackTrace(); 109 78 } 110 FrameUtils.ParseProfile(FrameIO.LoadProfile(UserSettings.ProfileName ));79 FrameUtils.ParseProfile(FrameIO.LoadProfile(UserSettings.ProfileName.get())); 111 80 } 112 81 … … 136 105 return; 137 106 } 138 List< Field> toDefault = new LinkedList<Field>(pd.hasDefault);107 List<VariableSetting> toDefault = new LinkedList<VariableSetting>(pd.hasDefault); 139 108 // set the fields 140 109 for (Text t : text.getChild().getBodyTextItems(true)) { … … 142 111 try { 143 112 // System.out.println(avp.getAttributeOrValue().trim().toLowerCase().replaceAll("^@", "")); 144 PageDescriptor.FieldSet fs = pd.settings.get(avp.getAttributeOrValue().trim().toLowerCase().replaceAll("^@", ""));145 if( fs == null) {113 Setting s = pd.settings.get(avp.getAttributeOrValue().trim().toLowerCase().replaceAll("^@", "")); 114 if(s == null) { 146 115 continue; 147 116 } 148 Field f = fs.field; 149 Method m = fs.method; 150 if(m != null) { 151 m.invoke(null, t); 152 // System.out.println("Set " + avp.getAttributeOrValue() + " by method"); 153 // we're setting this field, so remove it from the list of fields to default 154 toDefault.remove(f); 155 } else { 156 if (f != null) { 157 // if the value is empty, set it to the default 158 if(avp.getValue() == null || avp.getValue().trim().length() == 0) { 159 continue; 160 } 161 Class<?> type = f.getType(); 162 if(type == String.class) { 163 f.set(null, avp.getValue()); 164 } else if(type == Boolean.class || type == boolean.class) { 165 f.set(null, avp.getBooleanValue()); 166 } else if(type == Integer.class || type == int.class) { 167 f.set(null, avp.getIntegerValue()); 168 } else if(type == Double.class || type == double.class) { 169 f.set(null, avp.getDoubleValue()); 170 } else if(type == Float.class || type == float.class) { 171 f.set(null, avp.getDoubleValue().floatValue()); 172 } else { 173 System.err.println("Failed to set " + f.getName() + " of " + type); 174 continue; 175 } 176 // we're setting this field, so remove it from the list of fields to default 177 toDefault.remove(f); 178 // System.out.println("Set " + f.getName() + " to " + f.get(null)); 179 } 117 if(s.setSetting(t)) { 118 toDefault.remove(s); 180 119 } 181 120 } catch (Exception e) { … … 184 123 } 185 124 // set any unset fields to their default 186 for( Field f: toDefault) {125 for(VariableSetting s : toDefault) { 187 126 try { 188 f.set(null, pd.settings.get("default" + f.getName().toLowerCase()).field.get(null)); 127 // System.out.println("Resetting " + s.getTooltip()); 128 s.reset(); 189 129 // System.out.println("Set " + f.getName() + " to default value " + f.get(null)); 190 130 } catch (Exception e) { … … 210 150 } 211 151 152 /** 153 * 154 * Generates settings tree 155 * TODO: merge with @see FrameCreator 156 * 157 */ 212 158 private static Text generateSettingsTree(String page, Text text) { 213 159 Frame frame = FrameIO.CreateNewFrame(text); … … 215 161 216 162 int x = 50, y = 100; 217 int dY = UserSettings.ItemTemplate.get Polygon().getBounds().height + 1;218 int maxY = UserSettings.InitialHeight - 150;163 int dY = UserSettings.ItemTemplate.get().getPolygon().getBounds().height + 1; 164 int maxY = UserSettings.InitialHeight.get() - 150; 219 165 220 166 // add subpages of the current page … … 243 189 for(String str : pd.orderedEntries) { 244 190 String key = str.toLowerCase(); 245 if(key.startsWith("default")) { 246 continue; 247 } 248 PageDescriptor.FieldSet fs = pd.settings.get(key); 249 String name = null; 250 Object value = ""; 251 name = str.substring(0, 1).toUpperCase() + str.substring(1); 252 if(fs.method == null && fs.field != null) { 253 try { 254 if(pd.hasDefault.contains(fs.field)) { 255 value = ": " + pd.settings.get("default" + key).field.get(null); 256 } else { 257 value = ": " + fs.field.get(null); 258 } 259 } catch (Exception e) { 260 e.printStackTrace(); 261 } 262 } else if(fs.method == null) { 263 continue; 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 } 264 204 } 265 205 if(key.equals("pass")) { 266 Password pw = new Password(frame.createNewText("@iw: org.expeditee.items.widgets.Password"), null); 206 Text t = frame.addText(x, y, "iw: org.expeditee.items.widgets.Password", null); 207 Password pw = new Password(t, null); 267 208 pw.setPassword((String) value); 268 pw.setPosition(x, y);209 frame.removeItem(t); 269 210 frame.addAllItems(pw.getItems()); 270 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 frame.addItem(t); 222 y += h; 271 223 } else { 272 frame.addText(x, y , name + value, null);273 }274 y += dY;224 frame.addText(x, y + dY / 2, name + value, null); 225 y += dY; 226 } 275 227 if(y >= maxY) { 276 228 x += 300;
Note:
See TracChangeset
for help on using the changeset viewer.