source: trunk/src/org/expeditee/gui/management/ProfileManager.java@ 1540

Last change on this file since 1540 was 1540, checked in by bnemhaus, 4 years ago

Removed reliance of System.getProperty("user.name") by introducing some functions and a variable to Browser to be used instead. All previous occurrences of System.getProperty("user.name") now use these functions.

At the time, introduced new piping into various functions related to the creation and management of profile frames that distinguished between a profile name and a user name. This allows functions to be more specific about what is being used. For example, when modifying the users profile frames (in the profiles directory) that users profile name can be used instead of naming the variable 'username'. This distinction is important because while username's can end with numbers, profile names cannot and therefore get an 'A' on the end.

File size: 10.6 KB
Line 
1package org.expeditee.gui.management;
2
3import java.io.File;
4import java.nio.file.Path;
5import java.nio.file.Paths;
6import java.util.ArrayList;
7import java.util.Collection;
8import java.util.List;
9import java.util.Map;
10import java.util.function.Consumer;
11import java.util.stream.Collectors;
12
13import org.expeditee.agents.ExistingFramesetException;
14import org.expeditee.agents.InvalidFramesetNameException;
15import org.expeditee.core.Colour;
16import org.expeditee.core.Point;
17import org.expeditee.gui.Frame;
18import org.expeditee.gui.FrameIO;
19import org.expeditee.gui.MessageBay;
20import org.expeditee.items.Item;
21import org.expeditee.items.ItemUtils;
22import org.expeditee.items.Text;
23import org.expeditee.setting.Setting;
24import org.expeditee.settings.Settings;
25import org.expeditee.settings.UserSettings;
26import org.expeditee.settings.templates.TemplateSettings;
27
28public class ProfileManager {
29
30 private static final String DEFAULT = UserSettings.DEFAULT_PROFILE_NAME;
31 private static final String[] startPages = { "exploratorysearch", "webbrowser" };
32 public static final String USER_NAME_FLAG = "${" + "USER.NAME" + "}";
33 public static final String PROFILE_NAME_FLAG = "${" + "PROFILE.NAME" + "}";
34
35 public static Frame createProfile(String username, String profileName, Map<String, Setting> specifiedSettings,
36 Map<String, Consumer<Frame>> notifyWhenGenerated) {
37 ensureDefaultProfile();
38 Frame profileOne = null;
39 //String profileName = FrameIO.ConvertToValidFramesetName(username); // TODO: Delete this line before SVN commit
40
41 try {
42 Frame profile = FrameIO.CreateFrameset(profileName, FrameIO.PROFILE_PATH, true, null);
43 profileOne = profile;
44 profile.setTitle(username + "'s Profile");
45 Frame defaultFrame = FrameIO.LoadFrame(DEFAULT + "1");
46 MessageBay.suppressMessages(true);
47 UserSettings.UserName.set(username);
48 UserSettings.ProfileName.set(profileName);
49
50 int lastNumber = FrameIO.getLastNumber(defaultFrame.getFramesetName());
51 for (int i = 1; i <= lastNumber; i++) {
52 // Load in next default, if it doesn't exist continue loop.
53 defaultFrame = FrameIO.LoadFrame(DEFAULT + i);
54 if (defaultFrame == null) { continue; }
55
56 // Create the next next (currently blank) profile frame.
57 // If there is frame gaps in the default (say if there is no 4.exp but there is
58 // a 5.exp) then retain those gaps.
59 // This way copied relative links work.
60 while (profile.getNumber() < defaultFrame.getNumber()) {
61 profile = FrameIO.CreateFrame(profile.getFramesetName(), null, null);
62 }
63 // Ensure we are working from a blank slate.
64 profile.reset();
65 profile.removeAllItems(profile.getAllItems());
66
67 // For each item on defaultFrame:
68 // 1. Set all items to be relatively linked so once copied their links correctly
69 // point to the frame on the created profile rather than the default profile.
70 // 2. Copy item from defaultFrame to the current profile frame being
71 // constructed.
72 // 3. Replace instance of ${USER_NAME} with 'profileFor'
73 // 4. Replace settings values of copied items with those specified in
74 // specifiedSettings (if present)
75 Collection<Item> defaultAllItems = defaultFrame.getAllItems();
76 Collection<Item> allItems = new ArrayList<Item>();
77 for (Item item: defaultAllItems) {
78 Item copyItem = item.copy();
79 allItems.add(copyItem);
80 }
81 profile.addAllItems(allItems);
82
83 for (Item item: profile.getAllItems()) {
84 if (item instanceof Text) {
85 String content = item.getText();
86 String contentWithUserSub = ResourceUtil.substitute(content, ProfileManager.USER_NAME_FLAG, username);
87 String contentWithUserAndProfileSub = ResourceUtil.substitute(contentWithUserSub, ProfileManager.PROFILE_NAME_FLAG, FrameIO.ConvertToValidFramesetName(profileName));
88 item.setText(contentWithUserAndProfileSub);
89 }
90 }
91
92 String category = profile.getTitle();
93 List<String> settingsKeys = null;
94 if (specifiedSettings != null) {
95 settingsKeys = specifiedSettings.keySet().stream().filter(key ->
96 key.startsWith(category)).collect(Collectors.toList());
97 }
98 if (settingsKeys != null) {
99 for (String key: settingsKeys) {
100 Setting setting = specifiedSettings.get(key);
101 String name = setting.getName();
102 Text representation = setting.generateRepresentation(name, profile.getFramesetName());
103 Collection<Text> canditates = profile.getTextItems();
104 canditates.removeIf(text -> !text.getText().startsWith(representation.getText().split(" ")[0]));
105 canditates.forEach(text -> {
106 Point backupPos = text.getPosition();
107 Item.DuplicateItem(representation, text);
108 text.setText(representation.getText());
109 text.setPosition(backupPos);
110 });
111 }
112 }
113 if (notifyWhenGenerated != null && notifyWhenGenerated.containsKey(category)) {
114 notifyWhenGenerated.get(category).accept(profile);
115 }
116
117 FrameIO.SaveFrame(profile);
118 }
119 MessageBay.suppressMessages(false);
120 } catch (InvalidFramesetNameException | ExistingFramesetException e) {
121 String preamble = "Failed to create profile for '" + DEFAULT + "'. ";
122 if (e instanceof InvalidFramesetNameException) {
123 String message = preamble + "Profile names must start and end with a letter and must contain only letters and numbers";
124 MessageBay.errorMessage(message);
125 } else if (e instanceof ExistingFramesetException) {
126 String message = preamble + "A frameset with this name already exists in: " + FrameIO.PROFILE_PATH;
127 MessageBay.errorMessage(message);
128 }
129 }
130
131 return profileOne;
132 }
133
134 /**
135 * Checks if the default profile exists, creates it if it does not.
136 */
137 public static void ensureDefaultProfile() {
138 // If we can load in the default profile, we have nothing to do.
139 Frame defaultFrame = FrameIO.LoadProfile(DEFAULT);
140 if (defaultFrame != null) {
141 return;
142 }
143
144 // We do not have a default profile, generate one.
145 createDefaultProfile();
146 }
147
148 /**
149 * Creates the default profile.
150 */
151 private static void createDefaultProfile() {
152 try {
153 Frame profile = FrameIO.CreateFrameset(DEFAULT, FrameIO.PROFILE_PATH, true, null);
154
155 // Give the first frame in the new profile an appropriate title.
156 Text titleItem = profile.getTitleItem();
157 if (titleItem != null) {
158 titleItem.setText("Default Profile Frame");
159 }
160
161 // initial position for placing items.
162 int xPos = 300;
163 int yPos = 100;
164
165 // Add documentation links
166 Path helpDirectory = Paths.get(FrameIO.HELP_PATH);
167 File[] helpFramesets = helpDirectory.toFile().listFiles();
168 if (helpFramesets != null) {
169 // Add the title for the help index
170 Text helpPagesTitle = profile.addText(xPos, yPos, "@Expeditee Help", null);
171 helpPagesTitle.setSize(25);
172 helpPagesTitle.setFontStyle("Bold");
173 helpPagesTitle.setFamily("SansSerif");
174 helpPagesTitle.setColor(TemplateSettings.ColorWheel.get()[3]);
175
176 xPos += 25;
177 System.out.println("Installing frameset: ");
178
179 boolean first_item = true;
180 for (File helpFrameset : helpFramesets) {
181 String framesetName = helpFrameset.getName();
182 if (!FrameIO.isValidFramesetName(framesetName)) {
183 continue;
184 }
185
186 if (first_item) {
187 System.out.print(" " + framesetName);
188 first_item = false;
189 } else {
190 System.out.print(", " + framesetName);
191 }
192 System.out.flush();
193
194 Frame indexFrame = FrameIO.LoadFrame(framesetName + '1');
195 // Look through the folder for help index pages
196 if (indexFrame != null && ItemUtils.FindTag(indexFrame.getSortedItems(), "@HelpIndex") != null) {
197 // yPos += spacing;
198 yPos += 30;
199 Text helpLink = profile.addText(xPos, yPos, '@' + indexFrame.getFramesetName(), null);
200 helpLink.setLink(indexFrame.getName());
201 helpLink.setColor(Colour.GREY);
202 }
203 }
204 System.out.println();
205 }
206
207 // Reset position
208 xPos = 50;
209 yPos = 100;
210
211 // Add start pages
212 Path framesetDirectory = Paths.get(FrameIO.FRAME_PATH);
213 File[] startPageFramesets = framesetDirectory.toFile().listFiles();
214 if (startPageFramesets != null) {
215 // Add Start Page title
216 Text startPagesTitle = profile.addText(xPos, yPos, "@Start Pages", null);
217 startPagesTitle.setSize(25);
218 startPagesTitle.setFontStyle("Bold");
219 startPagesTitle.setFamily("SansSerif");
220 startPagesTitle.setColor(TemplateSettings.ColorWheel.get()[3]);
221
222 xPos += 25;
223
224 // Start Pages should be the first frame in its own frameset +
225 // frameset name should be present in FrameUtils.startPages[].
226 for (File startpagesFrameset : startPageFramesets) {
227 String framesetName = startpagesFrameset.getName();
228
229 // Only add link if frameset is a startpage
230 for (int i = 0; i < startPages.length; i++) {
231 if (framesetName.equals(startPages[i])) {
232 Frame indexFrame = FrameIO.LoadFrame(framesetName + '1');
233
234 // Add start page link
235 if (indexFrame != null) {
236 yPos += 30;
237 Text startPageLink = profile.addText(xPos, yPos, '@' + indexFrame.getFramesetName(), null);
238 startPageLink.setLink(indexFrame.getName());
239 startPageLink.setColor(Colour.GREY);
240 }
241 }
242 }
243 }
244 }
245
246 // Add 'default' settings frameset
247 Settings.Init();
248 Text settingsLink = profile.addText(550, 100, "@Settings", null);
249 settingsLink.setSize((float) 25.0);
250 settingsLink.setFamily("SansSerif");
251 settingsLink.setFontStyle("Bold");
252 settingsLink.setColor(Colour.GREY);
253 Settings.generateSettingsTree(settingsLink);
254 System.out.println("@Settings: Default settings generation complete.");
255
256 FrameIO.SaveFrame(profile);
257
258 int lastNumber = FrameIO.getLastNumber(profile.getFramesetName());
259 for (int i = 1; i <= lastNumber; i++) {
260 Frame frame = FrameIO.LoadFrame(DEFAULT + i);
261 frame.setOwner(DEFAULT);
262 for(Item item: frame.getAllItems()) {
263 item.setOwner(DEFAULT);
264 item.setRelativeLink();
265 }
266 frame.setChanged(true);
267 FrameIO.SaveFrame(frame);
268 }
269
270 } catch (InvalidFramesetNameException | ExistingFramesetException e) {
271 String preamble = "Failed to create profile for '" + DEFAULT + "'. ";
272 if (e instanceof InvalidFramesetNameException) {
273 String message = preamble + "Profile names must start and end with a letter and must contain only letters and numbers";
274 MessageBay.errorMessage(message);
275 } else if (e instanceof ExistingFramesetException) {
276 String message = preamble + "A frameset with this name already exists in: " + FrameIO.PROFILE_PATH;
277 MessageBay.errorMessage(message);
278 }
279 return;
280 }
281 }
282}
Note: See TracBrowser for help on using the repository browser.