source: trunk/src/org/expeditee/settings/UserSettings.java@ 1213

Last change on this file since 1213 was 1213, checked in by bln4, 5 years ago

UserSettings.java -> Flag settings to determine if we are authenticated and also the ability to push settings to the settings frameset.

File size: 10.8 KB
Line 
1/**
2 * UserSettings.java
3 * Copyright (C) 2010 New Zealand Digital Library, http://expeditee.org
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19package org.expeditee.settings;
20
21import java.io.File;
22import java.io.FileNotFoundException;
23import java.io.IOException;
24import java.util.ArrayList;
25import java.util.Collection;
26import java.util.LinkedList;
27import java.util.List;
28
29import org.expeditee.agents.SearchGreenstone;
30import org.expeditee.agents.mail.MailSession;
31import org.expeditee.agents.wordprocessing.JSpellChecker;
32import org.expeditee.gui.Frame;
33import org.expeditee.gui.FrameIO;
34import org.expeditee.gui.FrameUtils;
35import org.expeditee.gui.MessageBay;
36import org.expeditee.items.Item;
37import org.expeditee.items.Text;
38import org.expeditee.setting.BooleanSetting;
39import org.expeditee.setting.FloatSetting;
40import org.expeditee.setting.FrameSetting;
41import org.expeditee.setting.FunctionSetting;
42import org.expeditee.setting.IntegerSetting;
43import org.expeditee.setting.ListSetting;
44import org.expeditee.setting.StringSetting;
45import org.expeditee.settings.folders.FolderSettings;
46
47/**
48 * Central class to contain all values that can be set by the user on their
49 * profile frame. These values should be updated only by
50 * FrameUtils.ParseProfile.
51 */
52public abstract class UserSettings {
53
54 public static String DEFAULT_PROFILE_NAME = "default";
55
56 public static final IntegerSetting Gravity = new IntegerSetting("Distance the cursor has to be from a text item to select the text item", 3);
57
58 public static final StringSetting StartFrame = new StringSetting("The frame to go to when Expeditee is started (defaults to the profile frame)", null);
59
60 /*
61 * Stuff that goes first
62 */
63 public static final StringSetting HomeFrame = new StringSetting("The home frame", null) {
64 @Override
65 public boolean setSetting(Text text) {
66 if(text.getText().indexOf(':') == -1 || !text.hasLink()) {
67 text.setLink(UserSettings.ProfileName.get() + "1");
68 //text.setLink(FrameIO.LoadProfile(UserSettings.ProfileName.get()).getName());
69 }
70 String first = FrameUtils.getLink(text, UserSettings.HomeFrame.get());
71 // do not use non-existant frames as the first frame
72 if (FrameIO.isValidFrameName(first)) {
73 _value = first;
74 }
75 // warn the user
76 else {
77 // MessageBay.warningMessage("Home frame: " + first
78 // + " is not a valid frame.");
79 _value = FrameIO.LoadProfile(UserSettings.ProfileName.get()).getName();
80 }
81 return true;
82 }
83 };
84 public static final IntegerSetting InitialWidth = new IntegerSetting("Initial width of Expeditee window", 1024);
85
86 public static final IntegerSetting InitialHeight = new IntegerSetting("Initial height of Expeditee window", 768);
87
88 /*
89 * General settings (no setter functions)
90 */
91
92 public static final FloatSetting ScaleFactor = new FloatSetting("Scale Factor for drawing (TODO: does this even do anything?)", 1F);
93
94 public static final FloatSetting FormatSpacingMin = new FloatSetting("Minimum spacing ratio", null);
95
96 public static final FloatSetting FormatSpacingMax = new FloatSetting("Maximum spacing ratio", null);
97
98 public static final IntegerSetting LineStraightenThreshold = new IntegerSetting("Threshold for straightening a line (TODO: does this even do anything?)", 15);
99
100 public static final IntegerSetting NoOpThreshold = new IntegerSetting("Distance the cursor may be dragged before Gestures must be reinterpreted. E.g. Copy becomes range.", 10);
101
102 public static final IntegerSetting TitlePosition = new IntegerSetting("Position of title item in frame (TODO: find whether this is x-offset or y-offset)", 150);
103
104 public static final StringSetting ProfileName = new StringSetting("Profile name", FrameIO.ConvertToValidFramesetName(System.getProperty("user.name"))) {
105
106 /** Commit the change to the appropriate frame */
107 public void set(String value) {
108 super.set(value);
109 commitToSettingsFrameItem(value, "ProfileName:");
110 };
111
112 /** If authenticated, then resetting ProfileName should not do anything. */
113 public void reset() {
114 if (!Authenticated.get()) {
115 super.reset();
116 }
117 };
118 };
119
120 public static final StringSetting UserName = new StringSetting("User name", ProfileName.get()) {
121 /** Commit the change to the appropriate frame */
122 public void set(String value) {
123 super.set(value);
124 commitToSettingsFrameItem(value, "UserName:");
125 };
126
127 /** If authenticated, then resetting UserName should not do anything. */
128 public void reset() {
129 if (!Authenticated.get()) {
130 super.reset();
131 }
132 };
133 };
134
135 public static final BooleanSetting AntiAlias = new BooleanSetting("Whether anti-aliasing should be enabled", false);
136
137 public static final BooleanSetting LineHighlight = new BooleanSetting("Whether lines should be highlighted", false);
138
139 public static final BooleanSetting Logging = new BooleanSetting("Whether logging should be enabled", false);
140
141 public static final BooleanSetting LogStats = new BooleanSetting("Whether stats should be logged", true);
142
143 public static final BooleanSetting Threading = new BooleanSetting("Whether threading should be enabled", true);
144
145 public static final BooleanSetting Authenticated = new BooleanSetting("Whether the current user is authenticated", false) {
146 public void set(Boolean value) {
147 super.set(value);
148 commitToSettingsFrameItem(value, "Authenticated:");
149 }
150
151 // Authenticated must be maintained rather than rely on default value.
152 public void reset() {};
153
154 public boolean setSetting(Text text) {
155 return super.setSetting(text);
156 };
157 };
158
159 /*
160 * Frames
161 */
162 public static final StringSetting StatisticsFrameset = new StringSetting("The statistics frameset", null);
163
164 public static final StringSetting MenuFrame = new StringSetting("The menu frame", null);
165
166 /*
167 * Other
168 */
169 public static final ListSetting<Text> Style = new ListSetting<Text>("Set the style (TODO: what does this do?)") {
170 @Override
171 public boolean setSetting(Text text) {
172 Frame child = text.getChild();
173 if (child == null) {
174 _value = new LinkedList<Text>();
175 return true;
176 }
177
178
179 List<Text> style = new ArrayList<Text>(8);
180 for (int i = 0; i < 10; i++) {
181 style.add(null);
182 }
183
184 for (Text t : child.getBodyTextItems(false)) {
185 String type = t.getText();
186 char lastChar = type.charAt(type.length() - 1);
187 if (Character.isDigit(lastChar)) {
188 style.set(lastChar - '0', t);
189 } else {
190 style.set(0, t);
191 }
192 }
193 _value = style;
194 return true;
195 }
196 };
197
198 public static final FunctionSetting SpellChecker = new FunctionSetting("Enables the dictionary with the default dictionary") {
199 @Override
200 public void run() {
201 try {
202 JSpellChecker.create();
203 } catch (FileNotFoundException e) {
204 MessageBay.errorMessage("Could not find dictionary: " + e.getMessage());
205 } catch (IOException e) {
206 e.printStackTrace();
207 }
208 }
209 };
210 public static final FrameSetting Spelling = new FrameSetting("Enables the dictionary and adds the items in the child frame to the dictionary") {
211 @Override
212 public void run(Frame frame) {
213 try {
214 JSpellChecker.create(frame);
215 } catch (Exception e) {
216 e.printStackTrace();
217 }
218 }
219 };
220
221 public static final FrameSetting GreenstoneSettings = new FrameSetting("Greenstone settings (TODO: What are these for?)") {
222 @Override
223 public void run(Frame frame) {
224 SearchGreenstone.init(frame);
225 }
226 };
227
228 public static final FrameSetting Reminders = new FrameSetting("Reminders (TODO: What are these for?)") {
229 @Override
230 public void run(Frame frame) {
231 org.expeditee.gui.Reminders.init(frame);
232 }
233 };
234
235 public static final FrameSetting MailSettings = new FrameSetting("Mail Settings (TODO: How does this work?)") {
236 @Override
237 public void run(Frame frame) {
238 MailSession.init(frame);
239 }
240 };
241
242 // add default values
243 static {
244 setupDefaultFolders();
245 }
246
247 public static void setupDefaultFolders() {
248 String expeditee_home = System.getProperty("expeditee.home");
249 if (expeditee_home != null) {
250 FrameIO.changeParentFolder(expeditee_home + File.separator);
251 } else {
252 FrameIO.changeParentFolder(getSaveLocation());
253 }
254
255 FolderSettings.FrameDirs.get().clear();
256 FolderSettings.ImageDirs.get().clear();
257 appendDefaultFolders();
258 }
259
260 public static void appendDefaultFolders() {
261 FolderSettings.FrameDirs.get().add(FrameIO.FRAME_PATH);
262 FolderSettings.FrameDirs.get().add(FrameIO.PUBLIC_PATH);
263 FolderSettings.FrameDirs.get().add(FrameIO.PROFILE_PATH);
264 FolderSettings.FrameDirs.get().add(FrameIO.HELP_PATH);
265 FolderSettings.FrameDirs.get().add(FrameIO.MESSAGES_PATH);
266 FolderSettings.FrameDirs.setDefault(FolderSettings.FrameDirs.get());
267 FolderSettings.ImageDirs.get().add(FrameIO.IMAGES_PATH);
268 FolderSettings.ImageDirs.setDefault(FolderSettings.ImageDirs.get());
269 }
270
271 /**
272 * Find the appropriate directory to store application settings in for
273 * the current OS.
274 * This has only been tested on Linux so far, so if it doesn't work it
275 * may need to be modified or reverted. Should return:
276 * Linux: ~/.expeditee
277 * Windows: %appdata%\.expeditee
278 * Mac: ~/Library/Application\ Support/.expeditee
279 * @return the path to store expeditee's settings
280 */
281 public static String getSaveLocation() {
282 String OS=System.getProperty("os.name").toLowerCase();
283 if(OS.indexOf("win")>=0) { //windoze
284 return System.getenv("APPDATA")+File.separator+".expeditee"+File.separator;
285 } else if(OS.indexOf("mac")>=0) { //mac
286 return System.getProperty("user.home")+File.separator+"Library"+File.separator+"Application Support"+File.separator+".expeditee"+File.separator;
287 } else { //linux or other
288 return System.getProperty("user.home")+File.separator+".expeditee"+File.separator;
289 }
290 }
291
292 private static void commitToSettingsFrameItem(Object value, String contentMatch) {
293 String profileName = UserSettings.UserName.get();
294 int lastNumber = FrameIO.getLastNumber(profileName);
295 for (int i = 1; i <= lastNumber; i++) {
296 Frame frame = FrameIO.LoadFrame(profileName + i);
297 if (frame != null) {
298 Collection<Item> items = frame.getAllItems();
299 boolean found = false;
300 for (Item item: items) {
301 if (item.getText().startsWith(contentMatch)) {
302 item.setText(contentMatch + " " + value);
303 }
304 }
305 if (found) { break; }
306 }
307 }
308 }
309}
Note: See TracBrowser for help on using the repository browser.