package org.expeditee.settings; import java.awt.Color; import java.awt.Font; import java.awt.GraphicsEnvironment; import java.io.File; import java.io.FileNotFoundException; import java.io.IOException; import java.util.ArrayList; import java.util.Arrays; import java.util.LinkedList; import java.util.List; import org.expeditee.agents.SearchGreenstone; import org.expeditee.agents.mail.MailSession; import org.expeditee.agents.wordprocessing.JSpellChecker; import org.expeditee.gui.AttributeValuePair; import org.expeditee.gui.DisplayIO; import org.expeditee.gui.Frame; import org.expeditee.gui.FrameIO; import org.expeditee.gui.FrameUtils; import org.expeditee.gui.FreeItems; import org.expeditee.gui.MessageBay; import org.expeditee.items.Item; import org.expeditee.items.ItemUtils; import org.expeditee.items.Text; import org.expeditee.setting.ArraySetting; import org.expeditee.setting.BooleanSetting; import org.expeditee.setting.FloatSetting; import org.expeditee.setting.FrameSetting; import org.expeditee.setting.FunctionSetting; import org.expeditee.setting.IntegerSetting; import org.expeditee.setting.ListSetting; import org.expeditee.setting.Setting; import org.expeditee.setting.StringSetting; import org.expeditee.setting.TextSetting; /** * Central class to contain all values that can be set by the user on their * profile frame. These values should be updated only by * FrameUtils.ParseProfile. */ public abstract class UserSettings { public final static String DEFAULT_PROFILE_NAME = "default"; public static final IntegerSetting Gravity = new IntegerSetting("Distance the cursor has to be from a text item to select the text item", 3); public static final StringSetting StartFrame = new StringSetting("The frame to go to when Expeditee is started (defaults to the profile frame)", null); /* * Stuff that goes first */ public static final StringSetting HomeFrame = new StringSetting("The home frame", null) { @Override public boolean setSetting(Text text) { if(text.getText().indexOf(':') == -1 || !text.hasLink()) { text.setLink(FrameIO.LoadProfile(UserSettings.ProfileName.get()).getName()); } String first = FrameUtils.getLink(text, UserSettings.HomeFrame.get()); // do not use non-existant frames as the first frame if (FrameIO.isValidFrameName(first)) { _value = first; } // warn the user else { // MessageBay.warningMessage("Home frame: " + first // + " is not a valid frame."); _value = FrameIO.LoadProfile(UserSettings.ProfileName.get()).getName(); } return true; } }; public static final StringSetting DefaultFrame = new StringSetting("The default frame", null) { @Override public boolean setSetting(Text text) { _value = FrameUtils.getLink(text, _value); return true; } }; public static final IntegerSetting InitialWidth = new IntegerSetting("Initial width of Expeditee window", 1024); public static final IntegerSetting InitialHeight = new IntegerSetting("Initial height of Expeditee window", 768); public static final TextSetting ItemTemplate = new TextSetting("Template for normal text items") { @Override public Text generateText() { return new Text("ItemTemplate"); } }; public static final TextSetting AnnotationTemplate = new TextSetting("Template for annotation text items") { @Override public Text generateText() { Text t = new Text("AnnotationTemplate"); t.setColor(Color.gray); return t; } }; public static final TextSetting CommentTemplate = new TextSetting("Template for code comment text items") { @Override public Text generateText() { Text t = new Text("CommentTemplate"); t.setColor(Color.green.darker()); return t; } }; public static final TextSetting StatTemplate = new TextSetting("Template for statistics (e.g. extracted attributes) text items") { @Override public Text generateText() { Text t = new Text("StatsTemplate"); t.setColor(Color.BLACK); t.setBackgroundColor(new Color(0.9F, 0.9F, 0.9F)); t.setFamily(Text.MONOSPACED_FONT); t.setSize(14); return t; } }; /* * General settings (no setter functions) */ public static final FloatSetting ScaleFactor = new FloatSetting("Scale Factor for drawing (TODO: does this even do anything?)", 1F); public static final FloatSetting FormatSpacingMin = new FloatSetting("Minimum spacing ratio", null); public static final FloatSetting FormatSpacingMax = new FloatSetting("Maximum spacing ratio", null); public static final IntegerSetting LineStraightenThreshold = new IntegerSetting("Threshold for straightening a line (TODO: does this even do anything?)", 15); public static final IntegerSetting NoOpThreshold = new IntegerSetting("Distance the cursor may be dragged while clicking before the operation is cancelled", 60); public static final IntegerSetting TitlePosition = new IntegerSetting("Position of title item in frame (TODO: find whether this is x-offset or y-offset)", 150); public static final StringSetting ProfileName = new StringSetting("Profile name", FrameIO.ConvertToValidFramesetName(System.getProperty("user.name"))); public static final StringSetting UserName = new StringSetting("User name", ProfileName.get()); public static final BooleanSetting AntiAlias = new BooleanSetting("Whether anti-aliasing should be enabled", false); public static final BooleanSetting LineHighlight = new BooleanSetting("Whether lines should be highlighted", false); public static final BooleanSetting Logging = new BooleanSetting("Whether logging should be enabled", false); public static final BooleanSetting LogStats = new BooleanSetting("Whether stats should be logged", true); public static final BooleanSetting Threading = new BooleanSetting("Whether threading should be enabled", true); /* * Frames */ public static final StringSetting StatisticsFrameset = new StringSetting("The statistics frameset", null); public static final StringSetting MenuFrame = new StringSetting("The menu frame", null); /* * Directories */ public static final ListSetting FrameDirs = new ListSetting("Directories to look in for frames") { @Override public boolean setSetting(Text text) { _value.addAll(FrameUtils.getDirs(text)); return true; } }; public static final Setting FramesetDir = new Setting("Adds a directory to look in for frames") { @Override public boolean setSetting(Text text) { if(text.getText().indexOf(':') == -1) { return false; } AttributeValuePair avp = new AttributeValuePair(text.getText()); if(avp.getValue().trim().length() != 0) { String dir = FrameUtils.getDir(avp.getValue()); if (dir != null) { UserSettings.FrameDirs.get().add(dir); return true; } } return false; } }; public static ListSetting ImageDirs = new ListSetting("Directories to look in for images") { @Override public boolean setSetting(Text text) { _value.addAll(FrameUtils.getDirs(text)); return true; } }; public static final Setting ImageDir = new Setting("Adds a directory to look in for images") { @Override public boolean setSetting(Text text) { if(text.getText().indexOf(':') == -1) { return false; } AttributeValuePair avp = new AttributeValuePair(text.getText()); if(avp.getValue().trim().length() != 0) { String dir = FrameUtils.getDir(avp.getValue()); if(dir != null) { UserSettings.ImageDirs.get().add(0, dir); return true; } } return false; } }; public static final Setting LogDir = new Setting("Set the directory to save logs") { @Override public boolean setSetting(Text text) { if(text.getText().indexOf(':') == -1) { return false; } AttributeValuePair avp = new AttributeValuePair(text.getText()); if(avp.getValue().trim().length() != 0) { FrameIO.LOGS_DIR = FrameUtils.getDir(avp.getValue()); } return true; } }; /* * Templates */ public static final TextSetting TitleTemplate = new TextSetting("Template for Title text item") { @Override public Text generateText() { Text t = new Text("TitleTemplate"); t.setSize(30); t.setFontStyle("Bold"); t.setFamily("SansSerif"); t.setColor(Color.BLUE); t.setPosition(25, 50); return t; } }; public static final TextSetting DotTemplate = new TextSetting("Template for dot items") { @Override public Text generateText() { return new Text("DotTemplate"); } }; public static final TextSetting TooltipTemplate = new TextSetting("Template for tooltips") { @Override public Text generateText() { Text t = new Text("TooltipTemplate"); t.setColor(Color.BLACK); t.setBackgroundColor(new Color(0.7F, 0.7F, 0.9F)); // t.setFamily(Text.MONOSPACED_FONT); t.setSize(14); return t; } }; /* * Colorwheels */ public static final ArraySetting ColorWheel = new ArraySetting("The colours of items in the child frame are used to populate the colour wheel", new Color[] { Color.BLACK, Color.RED, Color.BLUE, Item.GREEN, Color.MAGENTA, Color.YELLOW.darker(), Color.WHITE }) { @Override public boolean setSetting(Text text) { Frame child = text.getChild(); if (child == null) { return false; } _value = FrameUtils.getColorWheel(child); return true; } }; public static final ArraySetting FillColorWheel = new ArraySetting("The colours of items in the child frame are used to populate the colour wheel", new Color[] { new Color(255, 150, 150), new Color(150, 150, 255), new Color(150, 255, 150), new Color(255, 150, 255), new Color(255, 255, 100), Color.WHITE, Color.BLACK }) { @Override public boolean setSetting(Text text) { Frame child = text.getChild(); if (child == null) { return false; } _value = FrameUtils.getColorWheel(child); return true; } }; public static final ArraySetting BackgroundColorWheel = new ArraySetting("The colours of items in the child frame are used to populate the colour wheel", new Color[] { new Color(235, 235, 235), new Color(225, 225, 255), new Color(195, 255, 255), new Color(225, 255, 225), new Color(255, 255, 195), new Color(255, 225, 225), new Color(255, 195, 255), Color.WHITE, Color.GRAY, Color.DARK_GRAY, Color.BLACK, null }) { @Override public boolean setSetting(Text text) { Frame child = text.getChild(); if (child == null) { return false; } _value = FrameUtils.getColorWheel(child); return true; } }; /* * Other */ public static final ListSetting Style = new ListSetting("Set the style (TODO: what does this do?)") { @Override public boolean setSetting(Text text) { Frame child = text.getChild(); if (child == null) { _value = new LinkedList(); return true; } List style = new ArrayList(8); for (int i = 0; i < 10; i++) { style.add(null); } for (Text t : child.getBodyTextItems(false)) { String type = t.getText(); char lastChar = type.charAt(type.length() - 1); if (Character.isDigit(lastChar)) { style.set(lastChar - '0', t); } else { style.set(0, t); } } _value = style; return true; } }; public static final FunctionSetting SpellChecker = new FunctionSetting("Enables the dictionary with the default dictionary") { @Override public void run() { try { JSpellChecker.create(); } catch (FileNotFoundException e) { MessageBay.errorMessage("Could not find dictionary: " + e.getMessage()); } catch (IOException e) { e.printStackTrace(); } } }; public static final FrameSetting Spelling = new FrameSetting("Enables the dictionary and adds the items in the child frame to the dictionary") { @Override public void run(Frame frame) { try { JSpellChecker.create(frame); } catch (Exception e) { e.printStackTrace(); } } }; public static final FrameSetting GreenstoneSettings = new FrameSetting("Greenstone settings (TODO: What are these for?)") { @Override public void run(Frame frame) { SearchGreenstone.init(frame); } }; public static final FrameSetting Reminders = new FrameSetting("Reminders (TODO: What are these for?)") { @Override public void run(Frame frame) { org.expeditee.gui.Reminders.init(frame); } }; public static final FrameSetting MailSettings = new FrameSetting("Mail Settings (TODO: How does this work?)") { @Override public void run(Frame frame) { MailSession.init(frame); } }; 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)") { @Override public void run(Frame frame) { FreeItems.getCursor().addAll(ItemUtils.CopyItems(frame.getAllItems())); for (Item i : FreeItems.getCursor()) { i.setParent(null); } DisplayIO.setCursor(Item.HIDDEN_CURSOR); DisplayIO.setCursor(Item.DEFAULT_CURSOR); } }; protected static final String filenameExtension(String fileName) { // Code excerpt from: // http://stackoverflow.com/questions/3571223/how-do-i-get-the-file-extension-of-a-file-in-java // packed up here as a method String extension = ""; int i = fileName.lastIndexOf('.'); int p = Math.max(fileName.lastIndexOf('/'), fileName.lastIndexOf('\\')); if (i > p) { extension = fileName.substring(i+1); } return extension; } // add default values static { String expeditee_home = System.getProperty("expeditee.home"); if (expeditee_home != null) { FrameIO.changeParentFolder(expeditee_home + File.separator); } else { FrameIO.changeParentFolder(getSaveLocation()); } UserSettings.FrameDirs.get().add(FrameIO.FRAME_PATH); UserSettings.FrameDirs.get().add(FrameIO.PUBLIC_PATH); UserSettings.FrameDirs.get().add(FrameIO.PROFILE_PATH); UserSettings.FrameDirs.get().add(FrameIO.HELP_PATH); UserSettings.FrameDirs.get().add(FrameIO.MESSAGES_PATH); UserSettings.FrameDirs.setDefault(UserSettings.FrameDirs.get()); UserSettings.ImageDirs.get().add(FrameIO.IMAGES_PATH); UserSettings.ImageDirs.setDefault(UserSettings.ImageDirs.get()); try { GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment(); if (ge != null) { File fontDirectory = new File(FrameIO.FONT_PATH); if (fontDirectory != null) { File[] fontFiles = fontDirectory.listFiles(); if (fontFiles != null) { if (fontFiles.length>0) { System.out.println("Loading custom fonts:"); } boolean first_item = true; for (File fontFile : fontFiles) { String ext = filenameExtension(fontFile.getName().toLowerCase()); if (ext.equals("ttf")) { if (first_item) { System.out.print(" " + fontFile.getName()); } else { System.out.print(", " + fontFile.getName()); } System.out.flush(); Font font = Font.createFont(Font.TRUETYPE_FONT, fontFile); ge.registerFont(font); first_item = false; } } System.out.println(); } } } else { System.err.println("No graphics environment detected. Skipping the loading of the custom font Metamorphous"); } } catch (Exception e) { System.err.println("Failed to load custom font Metamorphous"); e.printStackTrace(); } } /** * Find the appropriate directory to store application settings in for * the current OS. * This has only been tested on Linux so far, so if it doesn't work it * may need to be modified or reverted. Should return: * Linux: ~/.expeditee * Windows: %appdata%\.expeditee * Mac: ~/Library/Application\ Support/.expeditee * @return the path to store expeditee's settings */ public static String getSaveLocation() { String OS=System.getProperty("os.name").toLowerCase(); if(OS.indexOf("win")>=0) { //windoze return System.getenv("APPDATA")+File.separator+".expeditee"+File.separator; } else if(OS.indexOf("mac")>=0) { //mac return System.getProperty("user.home")+File.separator+"Library"+File.separator+"Application Support"+File.separator+".expeditee"+File.separator; } else { //linux or other return System.getProperty("user.home")+File.separator+".expeditee"+File.separator; } } }