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/UserSettings.java

    r642 r655  
    11package org.expeditee.settings;
    22
     3import java.awt.Color;
    34import java.io.File;
    45import java.io.FileNotFoundException;
     
    1819import org.expeditee.gui.FreeItems;
    1920import org.expeditee.gui.MessageBay;
    20 import org.expeditee.gui.Reminders;
    2121import org.expeditee.items.Item;
    2222import org.expeditee.items.ItemUtils;
    2323import org.expeditee.items.Text;
     24import org.expeditee.setting.FrameSetting;
     25import org.expeditee.setting.FunctionSetting;
     26import org.expeditee.setting.GenericSetting;
     27import org.expeditee.setting.ListSetting;
     28import org.expeditee.setting.Setting;
    2429
    2530/**
     
    3540         * General settings (no setter functions)
    3641         */
    37         public static final int defaultGravity = 3;
    38         public static int Gravity = defaultGravity;
    39        
    40         public static final float defaultScaleFactor = 1F;
    41         public static float ScaleFactor = defaultScaleFactor;
    42 
    43         public static final int defaultLineStraightenThreshold = 15;
    44         public static int LineStraightenThreshold = defaultLineStraightenThreshold;
    45 
    46         public static final int defaultNoOpThreshold = 60;
    47         public static int NoOpThreshold = defaultNoOpThreshold;
    48        
    49         public static final int defaultTitlePosition = 150;
    50         public static int TitlePosition = defaultTitlePosition;
    51 
    52         public static final int defaultInitialWidth = 1024;
    53         public static int InitialWidth = defaultInitialWidth;
    54        
    55         public static final int defaultInitialHeight = 768;
    56         public static int InitialHeight = defaultInitialHeight;
    57        
    58         public static String ProfileName;
    59        
    60         public static String UserName;
    61        
    62         public static final boolean defaultAntiAlias = false;
    63         public static boolean AntiAlias = defaultAntiAlias;
    64 
    65         public static final boolean defaultLineHighlight = false;
    66         public static boolean LineHighlight = defaultLineHighlight;
    67 
    68         public static final boolean defaultLogging = false;
    69         public static boolean Logging = defaultLogging;
    70 
    71         public static final boolean defaultLogStats = true;
    72         public static boolean LogStats = defaultLogStats;
    73 
    74         public static final boolean defaultThreading = true;
    75         public static boolean Threading = defaultThreading;
     42       
     43        public static final GenericSetting<Float> ScaleFactor = new GenericSetting<Float>(Float.class, "Scale Factor for drawing (TODO: does this even do anything?)", 1F);
     44       
     45        public static final GenericSetting<Float> FormatSpacingMin = new GenericSetting<Float>(Float.class, "Minimum spacing ratio", null);
     46       
     47        public static final GenericSetting<Float> FormatSpacingMax = new GenericSetting<Float>(Float.class, "Maximum spacing ratio", null);
     48       
     49        public static final GenericSetting<Integer> Gravity = new GenericSetting<Integer>(Integer.class, "Distance the cursor has to be from a text item to select the text item", 3);
     50
     51        public static final GenericSetting<Integer> LineStraightenThreshold = new GenericSetting<Integer>(Integer.class, "Threshold for straightening a line (TODO: does this even do anything?)", 15);
     52
     53        public static final GenericSetting<Integer> NoOpThreshold = new GenericSetting<Integer>(Integer.class, "Distance the cursor may be dragged while clicking before the operation is cancelled", 60);
     54       
     55        public static final GenericSetting<Integer> TitlePosition = new GenericSetting<Integer>(Integer.class, "Position of title item in frame (TODO: find whether this is x-offset or y-offset)", 150);
     56       
     57        public static final GenericSetting<Integer> InitialWidth = new GenericSetting<Integer>(Integer.class, "Initial width of Expeditee window", 1024);
     58       
     59        public static final GenericSetting<Integer> InitialHeight = new GenericSetting<Integer>(Integer.class, "Initial height of Expeditee window", 768);
     60       
     61        public static final GenericSetting<String> ProfileName = new GenericSetting<String>(String.class, "Profile name", FrameIO.ConvertToValidFramesetName(System.getProperty("user.name")));
     62       
     63        public static final GenericSetting<String> UserName = new GenericSetting<String>(String.class, "User name", ProfileName.get());
     64       
     65        public static final GenericSetting<Boolean> AntiAlias = new GenericSetting<Boolean>(Boolean.class, "Whether anti-aliasing should be enabled", false);
     66
     67        public static final GenericSetting<Boolean> LineHighlight = new GenericSetting<Boolean>(Boolean.class, "Whether lines should be highlighted", false);
     68
     69        public static final GenericSetting<Boolean> Logging = new GenericSetting<Boolean>(Boolean.class, "Whether logging should be enabled", false);
     70
     71        public static final GenericSetting<Boolean> LogStats = new GenericSetting<Boolean>(Boolean.class, "Whether stats should be logged", true);
     72
     73        public static final GenericSetting<Boolean> Threading = new GenericSetting<Boolean>(Boolean.class, "Whether threading should be enabled", true);
    7674       
    7775       
     
    7977         * Frames
    8078         */
    81         public static String DefaultFrame = null;
    82         public static void setDefaultFrame(Text t) {
    83                 UserSettings.DefaultFrame = FrameUtils.getLink(t, UserSettings.DefaultFrame);
    84         }
    85        
    86         public static String StatisticsFrameset = null;
    87 
    88         public static String MenuFrame = null;
    89 
    90         public static String HomeFrame = null;
    91         public static void setHomeFrame(Text t) {
    92                 if(t.getText().indexOf(':') == -1 || !t.hasLink()) {
    93                         t.setLink(FrameIO.LoadProfile(UserSettings.ProfileName).getName());
    94                 }
    95                 String first = FrameUtils.getLink(t, UserSettings.HomeFrame);
    96                 // do not use non-existant frames as the first frame
    97                 if (FrameIO.isValidFrameName(first)) {
    98                         UserSettings.HomeFrame = first;
    99                 }
    100                 // warn the user
    101                 else {
    102                         // MessageBay.warningMessage("Home frame: " + first
    103                         // + " is not a valid frame.");
    104                         UserSettings.HomeFrame = FrameIO.LoadProfile(UserSettings.ProfileName).getName();
    105                 }
    106         }
     79        public static final GenericSetting<String> DefaultFrame = new GenericSetting<String>(String.class, "The default frame", null) {
     80                @Override
     81                public boolean setSetting(Text text) {
     82                        _value = FrameUtils.getLink(text, _value);
     83                        return true;
     84                }
     85        };
     86       
     87        public static final GenericSetting<String> StatisticsFrameset = new GenericSetting<String>(String.class, "The statistics frameset", null);
     88
     89        public static final GenericSetting<String> MenuFrame = new GenericSetting<String>(String.class, "The menu frame", null);
     90
     91        public static final GenericSetting<String> HomeFrame = new GenericSetting<String>(String.class, "The home frame", null) {
     92                @Override
     93                public boolean setSetting(Text text) {
     94                if(text.getText().indexOf(':') == -1 || !text.hasLink()) {
     95                        text.setLink(FrameIO.LoadProfile(UserSettings.ProfileName.get()).getName());
     96                }
     97                String first = FrameUtils.getLink(text, UserSettings.HomeFrame.get());
     98                // do not use non-existant frames as the first frame
     99                if (FrameIO.isValidFrameName(first)) {
     100                        _value = first;
     101                }
     102                // warn the user
     103                else {
     104                        // MessageBay.warningMessage("Home frame: " + first
     105                        // + " is not a valid frame.");
     106                        _value = FrameIO.LoadProfile(UserSettings.ProfileName.get()).getName();
     107                }
     108                return true;
     109                }
     110        };
    107111
    108112        /*
    109113         *  Directories
    110114         */
    111         public static List<String> FrameDirs = new LinkedList<String>();
    112         public static void setFrameDirs(Text t) {
    113                 UserSettings.FrameDirs.addAll(FrameUtils.getDirs(t));
    114         }
    115         public static void setFramesetDir(Text t) {
    116                 if(t.getText().indexOf(':') == -1) {
    117                         return;
    118                 }
    119                 String dir = null;
    120                 AttributeValuePair avp = new AttributeValuePair(t.getText());
    121                 if(avp.getValue().trim().length() != 0) {
    122                         dir = FrameUtils.getDir(avp.getValue());
    123                 }
    124                 if (dir != null) {
    125                         UserSettings.FrameDirs.add(dir);
    126                 }
    127         }
    128 
    129         public static List<String> ImageDirs = new LinkedList<String>();
    130         public static void setImageDirs(Text t) {
    131                 UserSettings.ImageDirs.addAll(FrameUtils.getDirs(t));
    132         }
    133         public static void setImageDir(Text t) {
    134                 if(t.getText().indexOf(':') == -1) {
    135                         return;
    136                 }
    137                 String dir = null;
    138                 AttributeValuePair avp = new AttributeValuePair(t.getText());
    139                 if(avp.getValue().trim().length() != 0) {
    140                         dir = FrameUtils.getDir(avp.getValue());
    141                 }
    142                 if (dir != null) {
    143                         UserSettings.ImageDirs.add(0, dir);
    144                 }
    145         }
    146        
    147         public static void setLogDir(Text t) {
    148                 if(t.getText().indexOf(':') == -1) {
    149                         return;
    150                 }
    151                 AttributeValuePair avp = new AttributeValuePair(t.getText());
    152                 if(avp.getValue().trim().length() != 0) {
    153                         FrameIO.LOGS_DIR = FrameUtils.getDir(avp.getValue());
    154                 }
    155         }
     115        public static final ListSetting<String> FrameDirs = new ListSetting<String>("Directories to look in for frames") {
     116                @Override
     117                public boolean setSetting(Text text) {
     118                        _value.addAll(FrameUtils.getDirs(text));
     119                        return true;
     120                }
     121        };
     122        public static final Setting FramesetDir = new Setting("Adds a directory to look in for frames") {
     123                @Override
     124                public boolean setSetting(Text text) {
     125                        if(text.getText().indexOf(':') == -1) {
     126                                return false;
     127                }
     128                AttributeValuePair avp = new AttributeValuePair(text.getText());
     129                if(avp.getValue().trim().length() != 0) {
     130                        String dir = FrameUtils.getDir(avp.getValue());
     131                        if (dir != null) {
     132                                UserSettings.FrameDirs.get().add(dir);
     133                                return true;
     134                        }
     135                }
     136                return false;
     137                }
     138        };
     139
     140        public static ListSetting<String> ImageDirs = new ListSetting<String>("Directories to look in for images") {
     141                @Override
     142                public boolean setSetting(Text text) {
     143                        _value.addAll(FrameUtils.getDirs(text));
     144                        return true;
     145                }
     146        };
     147        public static final Setting ImageDir = new Setting("Adds a directory to look in for images") {
     148                @Override
     149                public boolean setSetting(Text text) {
     150                if(text.getText().indexOf(':') == -1) {
     151                        return false;
     152                }
     153                AttributeValuePair avp = new AttributeValuePair(text.getText());
     154                if(avp.getValue().trim().length() != 0) {
     155                        String dir = FrameUtils.getDir(avp.getValue());
     156                        if(dir != null) {
     157                                UserSettings.ImageDirs.get().add(0, dir);
     158                                return true;
     159                        }
     160                }
     161                return false;
     162                }
     163        };
     164       
     165        public static final Setting LogDir = new Setting("Set the directory to save logs") {
     166                @Override
     167                public boolean setSetting(Text text) {
     168                        if(text.getText().indexOf(':') == -1) {
     169                        return false;
     170                }
     171                AttributeValuePair avp = new AttributeValuePair(text.getText());
     172                if(avp.getValue().trim().length() != 0) {
     173                        FrameIO.LOGS_DIR = FrameUtils.getDir(avp.getValue());
     174                }
     175                return true;
     176                }
     177        };
    156178
    157179        /*
    158180         *  Templates
    159181         */
    160         public static Text TitleTemplate = null;
    161        
    162         public static Text ItemTemplate = new Text("@ItemTemplate");
    163         public static void setItemTemplate(Text t) {
    164                 UserSettings.ItemTemplate = t.getTemplateForm();
    165         }
    166        
    167         public static Text DotTemplate = new Text("@DotTemplate");
    168         public static void setDotTemplate(Text t) {
    169                 UserSettings.DotTemplate = t.getTemplateForm();
    170         }
    171 
    172         public static Text AnnotationTemplate = null;
    173         public static void setAnnotationTemplate(Text t) {
    174                 UserSettings.AnnotationTemplate = t.getTemplateForm();
    175         }
    176 
    177         public static Text CodeCommentTemplate = null;
    178         public static void setCommentTemplate(Text t) {
    179                 UserSettings.CodeCommentTemplate = t.getTemplateForm();
    180         }
    181 
    182         public static Text StatTemplate = null;
    183         public static void setStatsTemplate(Text t) {
    184                 UserSettings.StatTemplate = t.getTemplateForm();
    185         }
     182        public static final GenericSetting<Text> TitleTemplate = new GenericSetting<Text>(Text.class, "Template for Title text item");
     183       
     184        public static final GenericSetting<Text> ItemTemplate = new GenericSetting<Text>(Text.class, "Template for normal text items", new Text("@ItemTemplate")) {
     185                @Override
     186                public boolean setSetting(Text text) {
     187                        _value = text.getTemplateForm();
     188                        return true;
     189                }
     190        };
     191       
     192        public static final GenericSetting<Text> DotTemplate = new GenericSetting<Text>(Text.class, "Template for dot items", new Text("@DotTemplate")) {
     193                @Override
     194                public boolean setSetting(Text text) {
     195                        _value = text.getTemplateForm();
     196                        return true;
     197                }
     198        };
     199
     200        private static final Text annotationTemplate() {
     201                Text t = new Text("@AnnotationTemplate");
     202                t.setColor(Color.gray);
     203                return t;
     204        }
     205        public static final GenericSetting<Text> AnnotationTemplate = new GenericSetting<Text>(Text.class, "Template for annotation text items", annotationTemplate()) {
     206                @Override
     207                public boolean setSetting(Text text) {
     208                        _value = text.getTemplateForm();
     209                        return true;
     210                }
     211        };
     212
     213        private static final Text codeCommentTemplate() {
     214                Text t = new Text("@CommentTemplate");
     215                t.setColor(Color.green.darker());
     216                return t;
     217        }
     218        public static final GenericSetting<Text> CodeCommentTemplate = new GenericSetting<Text>(Text.class, "Template for code comment text items", codeCommentTemplate()) {
     219                @Override
     220                public boolean setSetting(Text text) {
     221                        _value = text.getTemplateForm();
     222                        return true;
     223                }
     224        };
     225
     226        private static final Text statTemplate() {
     227                Text t = new Text("@StatsTemplate");
     228                t.setColor(Color.BLACK);
     229                t.setBackgroundColor(new Color(0.9F, 0.9F, 0.9F));
     230                t.setFamily(Text.MONOSPACED_FONT);
     231                t.setSize(14);
     232                return t;
     233        }
     234        public static final GenericSetting<Text> StatTemplate = new GenericSetting<Text>(Text.class, "Template for statistics (e.g. extracted attributes) text items", statTemplate()) {
     235                @Override
     236                public boolean setSetting(Text text) {
     237                        _value = text.getTemplateForm();
     238                        return true;
     239                }
     240        };
    186241       
    187242        /*
    188243         * Colorwheels
    189244         */
    190         public static void setColorWheel(Text t) {
    191                 if(t.getChild() != null) Item.COLOR_WHEEL = FrameUtils.getColorWheel(t);
    192         }
    193        
    194         public static void setFillColorWheel(Text t) {
    195                 if(t.getChild() != null) Item.FILL_COLOR_WHEEL = FrameUtils.getColorWheel(t);
    196         }
    197        
    198         public static void setBackgroundColorWheel(Text t) {
    199                 if(t.getChild() != null) Frame.COLOR_WHEEL = FrameUtils.getColorWheel(t);
    200         }
     245        public static final FrameSetting ColorWheel = new FrameSetting("The colours of items in the child frame are used to populate the colour wheel") {
     246                @Override
     247                protected void run(Frame frame) {
     248                        Item.COLOR_WHEEL = FrameUtils.getColorWheel(frame);
     249                }
     250        };
     251       
     252        public static final FrameSetting FillColorWheel = new FrameSetting("The colours of items in the child frame are used to populate the colour wheel") {
     253                @Override
     254                protected void run(Frame frame) {
     255                        Item.FILL_COLOR_WHEEL = FrameUtils.getColorWheel(frame);
     256                }
     257        };
     258       
     259        public static final FrameSetting BackgroundColorWheel = new FrameSetting("The colours of items in the child frame are used to populate the colour wheel") {
     260                @Override
     261                protected void run(Frame frame) {
     262                        Frame.COLOR_WHEEL = FrameUtils.getColorWheel(frame);
     263                }
     264        };
    201265       
    202266        /*
    203267         * Other
    204268         */
    205         public static List<Text> Style = new LinkedList<Text>();
    206         public static void setStyle(Text t) {
    207                 Frame child = t.getChild();
    208                 if (child == null) {
    209                         UserSettings.Style = new LinkedList<Text>();
    210                         return;
    211                 }
    212                
    213 
    214                 List<Text> style = new ArrayList<Text>(8);
    215                 for (int i = 0; i < 10; i++) {
    216                         style.add(null);
    217                 }
    218 
    219                 for (Text text : child.getBodyTextItems(false)) {
    220                         String type = text.getText();
    221                         char lastChar = type.charAt(type.length() - 1);
    222                         if (Character.isDigit(lastChar)) {
    223                                 style.set(lastChar - '0', text);
    224                         } else {
    225                                 style.set(0, text);
    226                         }
    227                 }
    228                 UserSettings.Style = style;
    229         }
    230        
    231         public static void setSpellChecker(Text t) {
    232                 AttributeValuePair avp = new AttributeValuePair(t.getText());
    233                 if(avp.getBooleanValue()) {
    234                 try {
     269        public static final ListSetting<Text> Style = new ListSetting<Text>("Set the style (TODO: what does this do?)") {               
     270                @Override
     271                public boolean setSetting(Text text) {
     272                        Frame child = text.getChild();
     273                if (child == null) {
     274                        _value = new LinkedList<Text>();
     275                        return true;
     276                }
     277               
     278   
     279                List<Text> style = new ArrayList<Text>(8);
     280                for (int i = 0; i < 10; i++) {
     281                        style.add(null);
     282                }
     283   
     284                for (Text t : child.getBodyTextItems(false)) {
     285                        String type = t.getText();
     286                        char lastChar = type.charAt(type.length() - 1);
     287                        if (Character.isDigit(lastChar)) {
     288                                style.set(lastChar - '0', t);
     289                        } else {
     290                                style.set(0, t);
     291                        }
     292                }
     293                _value = style;
     294                return true;
     295                }
     296        };
     297       
     298        public static final FunctionSetting SpellChecker = new FunctionSetting("Enables the dictionary with the default dictionary") {
     299                @Override
     300                public void run() {
     301                        try {
    235302                        JSpellChecker.create();
    236303                } catch (FileNotFoundException e) {
    237                         MessageBay.errorMessage("Could not find dictionary: "
    238                                         + e.getMessage());
     304                        MessageBay.errorMessage("Could not find dictionary: " + e.getMessage());
    239305                } catch (IOException e) {
    240306                        e.printStackTrace();
    241307                }
    242308                }
    243         }
    244         public static void setSpelling(Text t) {
    245                 if(t.getChild() != null) {
    246                 try {
    247                 JSpellChecker.create(t.getChild());
    248             } catch (Exception e) {
    249                 e.printStackTrace();
    250             }
    251                 }
    252         }
    253        
    254         public static void setGreenstoneSettings(Text t) {
    255                 if(t.getChild() != null) SearchGreenstone.init(t.getChild());
    256         }
    257        
    258         public static void setReminders(Text t) {
    259                 if(t.getChild() != null) Reminders.init(t.getChild());
    260         }
    261        
    262         public static void setFormatSpacingMin(Text t) {
    263                 double value = new AttributeValuePair(t.getText()).getDoubleValue();
    264                 if(value != Double.NaN) {
    265                         FrameUtils.MINIMUM_SPACING_RATIO = value;
    266                 }
    267         }
    268        
    269         public static void setFormatSpacingMax(Text t) {
    270                 double value = new AttributeValuePair(t.getText()).getDoubleValue();
    271                 if(value != Double.NaN) {
    272                         FrameUtils.MAXIMUM_SPACING_RATIO = value;
    273                 }
    274         }
    275        
    276         public static void setMailSettings(Text t) {
    277                 if(t.getChild() != null) MailSession.init(t.getChild());
    278         }
    279        
    280         public static void setCursorFrame(Text t) {
    281                 if (t.getChild() != null) {
    282                         FreeItems.getCursor().addAll(ItemUtils.CopyItems(t.getChild().getAllItems()));
     309        };
     310        public static final FrameSetting Spelling = new FrameSetting("Enables the dictionary and adds the items in the child frame to the dictionary") {
     311                @Override
     312                public void run(Frame frame) {
     313                try {
     314                JSpellChecker.create(frame);
     315                } catch (Exception e) {
     316                        e.printStackTrace();
     317                        }
     318                }
     319        };
     320       
     321        public static final FrameSetting GreenstoneSettings = new FrameSetting("Greenstone settings (TODO: What are these for?)") {
     322                @Override
     323                public void run(Frame frame) {
     324                        SearchGreenstone.init(frame);
     325                }
     326        };
     327       
     328        public static final FrameSetting Reminders = new FrameSetting("Reminders (TODO: What are these for?)") {
     329                @Override
     330                public void run(Frame frame) {
     331                        org.expeditee.gui.Reminders.init(frame);
     332                }
     333        };
     334       
     335        public static final FrameSetting MailSettings = new FrameSetting("Mail Settings (TODO: How does this work?)") {
     336                @Override
     337                public void run(Frame frame) {
     338                        MailSession.init(frame);
     339                }
     340        };
     341       
     342        public static final FrameSetting CursorFrame = new FrameSetting("Items on this frame will be used as the cursor (clearing the frame or removing the link will default back to a normal cursor)") {
     343                @Override
     344                public void run(Frame frame) {
     345                        FreeItems.getCursor().addAll(ItemUtils.CopyItems(frame.getAllItems()));
    283346                        for (Item i : FreeItems.getCursor()) {
    284347                                i.setParent(null);
     
    287350                        DisplayIO.setCursor(Item.DEFAULT_CURSOR);
    288351                }
    289         }
     352        };
    290353       
    291354       
     
    299362        }
    300363         
    301                 UserSettings.FrameDirs.add(FrameIO.FRAME_PATH);
    302                 UserSettings.FrameDirs.add(FrameIO.PUBLIC_PATH);
    303                 UserSettings.FrameDirs.add(FrameIO.PROFILE_PATH);
    304                 UserSettings.FrameDirs.add(FrameIO.HELP_PATH);
    305                 UserSettings.ImageDirs.add(FrameIO.IMAGES_PATH);
    306                 UserSettings.FrameDirs.add(FrameIO.MESSAGES_PATH);
     364                UserSettings.FrameDirs.get().add(FrameIO.FRAME_PATH);
     365                UserSettings.FrameDirs.get().add(FrameIO.PUBLIC_PATH);
     366                UserSettings.FrameDirs.get().add(FrameIO.PROFILE_PATH);
     367                UserSettings.FrameDirs.get().add(FrameIO.HELP_PATH);
     368                UserSettings.ImageDirs.get().add(FrameIO.IMAGES_PATH);
     369                UserSettings.FrameDirs.get().add(FrameIO.MESSAGES_PATH);
    307370        }
    308371
Note: See TracChangeset for help on using the changeset viewer.