Ignore:
Timestamp:
01/07/14 10:59:52 (10 years ago)
Author:
jts21
Message:

Switch to using specialised objects for settings so they make more a bit more sense (now each setting is a single object instead of multiple, and setter functions and default values are less hackish)
Also added tooltips (help strings) to settings, will need to add a way of displaying these (maybe add the idea of a tooltip which is a text item which only appears when hovering over another item?)
Converted all settings over to new format, everything seems to be working fine

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/expeditee/settings/Settings.java

    r641 r655  
    33import java.lang.reflect.Field;
    44import java.lang.reflect.Method;
    5 import java.lang.reflect.Modifier;
    65import java.util.HashMap;
    76import java.util.LinkedList;
     
    1312import org.expeditee.gui.FrameIO;
    1413import org.expeditee.gui.FrameUtils;
     14import org.expeditee.items.Item;
    1515import org.expeditee.items.Text;
    1616import org.expeditee.items.widgets.Password;
    1717import org.expeditee.reflection.PackageLoader;
     18import org.expeditee.setting.GenericSetting;
     19import org.expeditee.setting.Setting;
     20import org.expeditee.setting.VariableSetting;
    1821
    1922public abstract class Settings {
     
    2427        private static final class PageDescriptor {
    2528               
    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>();
    3930                private final List<String> orderedEntries = new LinkedList<String>();
     31                private final List<VariableSetting> hasDefault = new LinkedList<VariableSetting>();
    4032                private final Method onParsed;
    4133               
    4234                private PageDescriptor(Class<?> clazz) {
    4335                       
    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
    5637                        for(Field f : clazz.getFields()) {
    57                                 Class<?> type = f.getType();
    5838                                // 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                }
    8352                        }
    8453                        Method m = null;
     
    10877                        e.printStackTrace();
    10978                }
    110                 FrameUtils.ParseProfile(FrameIO.LoadProfile(UserSettings.ProfileName));
     79                FrameUtils.ParseProfile(FrameIO.LoadProfile(UserSettings.ProfileName.get()));
    11180        }
    11281       
     
    136105                                return;
    137106                        }
    138                         List<Field> toDefault = new LinkedList<Field>(pd.hasDefault);
     107                        List<VariableSetting> toDefault = new LinkedList<VariableSetting>(pd.hasDefault);
    139108                        // set the fields
    140109                        for (Text t : text.getChild().getBodyTextItems(true)) {
     
    142111                                try {
    143112                                        // 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) {
    146115                                                continue;
    147116                                        }
    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);
    180119                                        }
    181120                                } catch (Exception e) {
     
    184123                        }
    185124                        // set any unset fields to their default
    186                         for(Field f : toDefault) {
     125                        for(VariableSetting s : toDefault) {
    187126                                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();
    189129                            // System.out.println("Set " + f.getName() + " to default value " + f.get(null));
    190130                    } catch (Exception e) {
     
    210150        }
    211151       
     152        /**
     153         *
     154         * Generates settings tree
     155         * TODO: merge with @see FrameCreator
     156         *
     157         */
    212158        private static Text generateSettingsTree(String page, Text text) {
    213159                Frame frame = FrameIO.CreateNewFrame(text);
     
    215161               
    216162                int x = 50, y = 100;
    217                 int dY = UserSettings.ItemTemplate.getPolygon().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;
    219165               
    220166                // add subpages of the current page
     
    243189                        for(String str : pd.orderedEntries) {
    244190                                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                                        }
    264204                                }
    265205                                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);
    267208                                        pw.setPassword((String) value);
    268                                         pw.setPosition(x, y);
     209                                        frame.removeItem(t);
    269210                                        frame.addAllItems(pw.getItems());
    270211                                        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;
    271223                                } 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                                }
    275227                                if(y >= maxY) {
    276228                                        x += 300;
Note: See TracChangeset for help on using the changeset viewer.