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

Last change on this file since 1441 was 1441, checked in by bnemhaus, 5 years ago

Copying images in Expeditee now duplicates the associated file on the filesystem.
Also changed USER_NAME back to USER.NAME on David's direction.

File size: 10.3 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
34 public static Frame createProfile(String profileFor, Map<String, Setting> specifiedSettings,
35 Map<String, Consumer<Frame>> notifyWhenGenerated) {
36 ensureDefaultProfile();
37 Frame profile = null;
38 Frame profileOne = null;
39 profileFor = FrameIO.ConvertToValidFramesetName(profileFor);
40
41 try {
42 profile = FrameIO.CreateFrameset(profileFor, FrameIO.PROFILE_PATH, true);
43 profileOne = profile;
44 profile.setTitle(profileFor + "'s Profile");
45 Frame defaultFrame = FrameIO.LoadFrame(DEFAULT + "1");
46 MessageBay.suppressMessages(true);
47 UserSettings.UserName.set(profileFor);
48 UserSettings.ProfileName.set(profileFor);
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 item.setText(ResourceUtil.substitute(content, ProfileManager.USER_NAME_FLAG, profileFor));
87 }
88 }
89
90 String category = profile.getTitle();
91 List<String> settingsKeys = null;
92 if (specifiedSettings != null) {
93 settingsKeys = specifiedSettings.keySet().stream().filter(key ->
94 key.startsWith(category)).collect(Collectors.toList());
95 }
96 if (settingsKeys != null) {
97 for (String key: settingsKeys) {
98 Setting setting = specifiedSettings.get(key);
99 String name = setting.getName();
100 Text representation = setting.generateRepresentation(name, profile.getFramesetName());
101 Collection<Text> canditates = profile.getTextItems();
102 canditates.removeIf(text -> !text.getText().startsWith(representation.getText().split(" ")[0]));
103 canditates.forEach(text -> {
104 Point backupPos = text.getPosition();
105 Item.DuplicateItem(representation, text);
106 text.setText(representation.getText());
107 text.setPosition(backupPos);
108 });
109 }
110 }
111 if (notifyWhenGenerated != null && notifyWhenGenerated.containsKey(category)) {
112 notifyWhenGenerated.get(category).accept(profile);
113 }
114
115 FrameIO.SaveFrame(profile);
116 }
117 MessageBay.suppressMessages(false);
118 } catch (InvalidFramesetNameException | ExistingFramesetException e) {
119 String preamble = "Failed to create profile for '" + DEFAULT + "'. ";
120 if (e instanceof InvalidFramesetNameException) {
121 String message = preamble + "Profile names must start and end with a letter and must contain only letters and numbers";
122 MessageBay.errorMessage(message);
123 } else if (e instanceof ExistingFramesetException) {
124 String message = preamble + "A frameset with this name already exists in: " + FrameIO.PROFILE_PATH;
125 MessageBay.errorMessage(message);
126 }
127 }
128
129 return profileOne;
130 }
131
132 /**
133 * Checks if the default profile exists, creates it if it does not.
134 */
135 public static void ensureDefaultProfile() {
136 // If we can load in the default profile, we have nothing to do.
137 Frame defaultFrame = FrameIO.LoadProfile(DEFAULT);
138 if (defaultFrame != null) {
139 return;
140 }
141
142 // We do not have a default profile, generate one.
143 createDefaultProfile();
144 }
145
146 /**
147 * Creates the default profile.
148 */
149 private static void createDefaultProfile() {
150 try {
151 Frame profile = FrameIO.CreateFrameset(DEFAULT, FrameIO.PROFILE_PATH, true);
152
153 // Give the first frame in the new profile an appropriate title.
154 Text titleItem = profile.getTitleItem();
155 if (titleItem != null) {
156 titleItem.setText("Default Profile Frame");
157 }
158
159 // initial position for placing items.
160 int xPos = 300;
161 int yPos = 100;
162
163 // Add documentation links
164 Path helpDirectory = Paths.get(FrameIO.HELP_PATH);
165 File[] helpFramesets = helpDirectory.toFile().listFiles();
166 if (helpFramesets != null) {
167 // Add the title for the help index
168 Text helpPagesTitle = profile.addText(xPos, yPos, "@Expeditee Help", null);
169 helpPagesTitle.setSize(25);
170 helpPagesTitle.setFontStyle("Bold");
171 helpPagesTitle.setFamily("SansSerif");
172 helpPagesTitle.setColor(TemplateSettings.ColorWheel.get()[3]);
173
174 xPos += 25;
175 System.out.println("Installing frameset: ");
176
177 boolean first_item = true;
178 for (File helpFrameset : helpFramesets) {
179 String framesetName = helpFrameset.getName();
180 if (!FrameIO.isValidFramesetName(framesetName)) {
181 continue;
182 }
183
184 if (first_item) {
185 System.out.print(" " + framesetName);
186 first_item = false;
187 } else {
188 System.out.print(", " + framesetName);
189 }
190 System.out.flush();
191
192 Frame indexFrame = FrameIO.LoadFrame(framesetName + '1');
193 // Look through the folder for help index pages
194 if (indexFrame != null && ItemUtils.FindTag(indexFrame.getSortedItems(), "@HelpIndex") != null) {
195 // yPos += spacing;
196 yPos += 30;
197 Text helpLink = profile.addText(xPos, yPos, '@' + indexFrame.getFramesetName(), null);
198 helpLink.setLink(indexFrame.getName());
199 helpLink.setColor(Colour.GREY);
200 }
201 }
202 System.out.println();
203 }
204
205 // Reset position
206 xPos = 50;
207 yPos = 100;
208
209 // Add start pages
210 Path framesetDirectory = Paths.get(FrameIO.FRAME_PATH);
211 File[] startPageFramesets = framesetDirectory.toFile().listFiles();
212 if (startPageFramesets != null) {
213 // Add Start Page title
214 Text startPagesTitle = profile.addText(xPos, yPos, "@Start Pages", null);
215 startPagesTitle.setSize(25);
216 startPagesTitle.setFontStyle("Bold");
217 startPagesTitle.setFamily("SansSerif");
218 startPagesTitle.setColor(TemplateSettings.ColorWheel.get()[3]);
219
220 xPos += 25;
221
222 // Start Pages should be the first frame in its own frameset +
223 // frameset name should be present in FrameUtils.startPages[].
224 for (File startpagesFrameset : startPageFramesets) {
225 String framesetName = startpagesFrameset.getName();
226
227 // Only add link if frameset is a startpage
228 for (int i = 0; i < startPages.length; i++) {
229 if (framesetName.equals(startPages[i])) {
230 Frame indexFrame = FrameIO.LoadFrame(framesetName + '1');
231
232 // Add start page link
233 if (indexFrame != null) {
234 yPos += 30;
235 Text startPageLink = profile.addText(xPos, yPos, '@' + indexFrame.getFramesetName(), null);
236 startPageLink.setLink(indexFrame.getName());
237 startPageLink.setColor(Colour.GREY);
238 }
239 }
240 }
241 }
242 }
243
244 // Add 'default' settings frameset
245 Settings.Init();
246 Text settingsLink = profile.addText(550, 100, "@Settings", null);
247 settingsLink.setSize((float) 25.0);
248 settingsLink.setFamily("SansSerif");
249 settingsLink.setFontStyle("Bold");
250 settingsLink.setColor(Colour.GREY);
251 Settings.generateSettingsTree(settingsLink);
252 System.out.println("@Settings: Default settings generation complete.");
253
254 FrameIO.SaveFrame(profile);
255
256 int lastNumber = FrameIO.getLastNumber(profile.getFramesetName());
257 for (int i = 1; i <= lastNumber; i++) {
258 Frame frame = FrameIO.LoadFrame(DEFAULT + i);
259 frame.setOwner(DEFAULT);
260 for(Item item: frame.getAllItems()) {
261 item.setOwner(DEFAULT);
262 item.setRelativeLink();
263 }
264 frame.setChanged(true);
265 FrameIO.SaveFrame(frame);
266 }
267
268 } catch (InvalidFramesetNameException | ExistingFramesetException e) {
269 String preamble = "Failed to create profile for '" + DEFAULT + "'. ";
270 if (e instanceof InvalidFramesetNameException) {
271 String message = preamble + "Profile names must start and end with a letter and must contain only letters and numbers";
272 MessageBay.errorMessage(message);
273 } else if (e instanceof ExistingFramesetException) {
274 String message = preamble + "A frameset with this name already exists in: " + FrameIO.PROFILE_PATH;
275 MessageBay.errorMessage(message);
276 }
277 return;
278 }
279 }
280}
Note: See TracBrowser for help on using the repository browser.