1 | package org.expeditee.actions;
|
---|
2 |
|
---|
3 | import java.awt.Color;
|
---|
4 | import java.awt.Dimension;
|
---|
5 | import java.awt.GraphicsEnvironment;
|
---|
6 | import java.awt.image.BufferedImage;
|
---|
7 | import java.io.File;
|
---|
8 | import java.io.FileNotFoundException;
|
---|
9 | import java.io.IOException;
|
---|
10 | import java.util.List;
|
---|
11 |
|
---|
12 | import javax.imageio.ImageIO;
|
---|
13 |
|
---|
14 | import org.expeditee.gui.DisplayIO;
|
---|
15 | import org.expeditee.gui.Frame;
|
---|
16 | import org.expeditee.gui.FrameGraphics;
|
---|
17 | import org.expeditee.gui.FrameIO;
|
---|
18 | import org.expeditee.gui.FrameMouseActions;
|
---|
19 | import org.expeditee.items.Item;
|
---|
20 | import org.expeditee.items.Picture;
|
---|
21 | import org.expeditee.items.Text;
|
---|
22 | import org.expeditee.stats.SessionStats;
|
---|
23 | import org.expeditee.stats.StatsLogger;
|
---|
24 |
|
---|
25 | /**
|
---|
26 | * A list of miscellaneous KMS Actions and Actions specific to Expeditee
|
---|
27 | *
|
---|
28 | */
|
---|
29 | public class Misc {
|
---|
30 | private static final int STATS_FONT_SIZE = 18;
|
---|
31 |
|
---|
32 | private static final int FRAME_FILE_FONT_SIZE = 10;
|
---|
33 |
|
---|
34 | /**
|
---|
35 | * Causes the system to beep
|
---|
36 | */
|
---|
37 | public static void Beep() {
|
---|
38 | java.awt.Toolkit.getDefaultToolkit().beep();
|
---|
39 | }
|
---|
40 |
|
---|
41 | /**
|
---|
42 | * Forces a repaint of the current Frame
|
---|
43 | */
|
---|
44 | public static void Display() {
|
---|
45 | FrameGraphics.ForceRepaint();
|
---|
46 | }
|
---|
47 |
|
---|
48 | /**
|
---|
49 | * Restores the current frame to the last saved version currently on the
|
---|
50 | * hard disk
|
---|
51 | */
|
---|
52 | public static void Restore() {
|
---|
53 | FrameIO.Reload();
|
---|
54 |
|
---|
55 | FrameGraphics.DisplayMessage("Restoration complete.");
|
---|
56 | }
|
---|
57 |
|
---|
58 | /**
|
---|
59 | * Toggles AudienceMode on or off
|
---|
60 | */
|
---|
61 | public static void ToggleAudienceMode() {
|
---|
62 | FrameGraphics.ToggleAudienceMode();
|
---|
63 | }
|
---|
64 |
|
---|
65 | /**
|
---|
66 | * Toggles TwinFrames mode on or off
|
---|
67 | */
|
---|
68 | public static void ToggleTwinFramesMode() {
|
---|
69 | DisplayIO.ToggleTwinFrames();
|
---|
70 | }
|
---|
71 |
|
---|
72 | /**
|
---|
73 | * Exits the System
|
---|
74 | */
|
---|
75 | public static void Exit() {
|
---|
76 | /**
|
---|
77 | * TODO: Prompt the user etc.
|
---|
78 | */
|
---|
79 | FrameIO.SaveFrame(DisplayIO.getCurrentFrame());
|
---|
80 |
|
---|
81 | while(FrameIO.DeleteFrameset("messages"))
|
---|
82 | ;
|
---|
83 |
|
---|
84 | StatsLogger.WriteStatsFile();
|
---|
85 |
|
---|
86 | System.exit(0);
|
---|
87 | }
|
---|
88 |
|
---|
89 | /**
|
---|
90 | * If the given Item is a Text Item, then the text of the Item is
|
---|
91 | * interpreted as actions, if not this method does nothing.
|
---|
92 | *
|
---|
93 | * @param current
|
---|
94 | * The Item to read the Actions from
|
---|
95 | */
|
---|
96 | public static void RunCurrentItem(Item current) {
|
---|
97 | if (current instanceof Text) {
|
---|
98 | List<String> actions = ((Text) current).getText();
|
---|
99 |
|
---|
100 | for (String action : actions) {
|
---|
101 | Actions.PerformAction(DisplayIO.getCurrentFrame(), current,
|
---|
102 | action);
|
---|
103 | }
|
---|
104 | }
|
---|
105 |
|
---|
106 | }
|
---|
107 |
|
---|
108 | /**
|
---|
109 | * Prompts the user to confirm deletion of the current Frame, and deletes if
|
---|
110 | * the user chooses. After deletion this action calls back(), to ensure the
|
---|
111 | * deleted frame is not still being shown
|
---|
112 | *
|
---|
113 | */
|
---|
114 | public static void DeleteFrame() {
|
---|
115 | Frame toDelete = DisplayIO.getCurrentFrame();
|
---|
116 | DisplayIO.Back();
|
---|
117 | String deletedFrame = toDelete.getFrameName();
|
---|
118 | String deletedFrameNameLowercase = deletedFrame.toLowerCase();
|
---|
119 | try {
|
---|
120 | boolean del = FrameIO.DeleteFrame(toDelete);
|
---|
121 | if (!del) {
|
---|
122 | FrameGraphics.ErrorMessage("Error trying to delete "
|
---|
123 | + toDelete.getFrameName());
|
---|
124 | } else {
|
---|
125 | Frame current = DisplayIO.getCurrentFrame();
|
---|
126 | for (Item i : current.getItems())
|
---|
127 | if (i.getLink() != null
|
---|
128 | && i.getAbsoluteLink().toLowerCase().equals(
|
---|
129 | deletedFrameNameLowercase)) {
|
---|
130 | i.setLink(null);
|
---|
131 | }
|
---|
132 |
|
---|
133 | FrameGraphics.Repaint();
|
---|
134 | FrameGraphics.DisplayMessage(deletedFrame + " Deleted.");
|
---|
135 | }
|
---|
136 | } catch (IOException ioe) {
|
---|
137 | FrameGraphics.ErrorMessage("Error trying to delete " + deletedFrame
|
---|
138 | + ":\n" + ioe.getMessage());
|
---|
139 | }
|
---|
140 | }
|
---|
141 |
|
---|
142 | /**
|
---|
143 | * Loads the Frame linked to by the given Item. The first Item on the Frame
|
---|
144 | * that is not the title or name is then placed on the cursor. If the given
|
---|
145 | * Item has no link, or no item is found then this is a no-op.
|
---|
146 | *
|
---|
147 | * @param current
|
---|
148 | * The Item that links to the Frame that the Item will be loaded
|
---|
149 | * from.
|
---|
150 | */
|
---|
151 | public static void GetItemFromChildFrame(Item current) {
|
---|
152 | Item item = getFirstBodyItemOnChildFrame(current);
|
---|
153 | // if no item was found
|
---|
154 | if (item == null) {
|
---|
155 | return;
|
---|
156 | }
|
---|
157 |
|
---|
158 | // copy the item and switch
|
---|
159 | item = item.copy();
|
---|
160 | item.setPosition(DisplayIO.getMouseX(), DisplayIO.getMouseY());
|
---|
161 | item.setParent(null);
|
---|
162 |
|
---|
163 | FrameMouseActions.pickup(item);
|
---|
164 | FrameGraphics.Repaint();
|
---|
165 | }
|
---|
166 |
|
---|
167 | /**
|
---|
168 | * Sets the given Item to have the Given Color. Color can be null (for
|
---|
169 | * default)
|
---|
170 | *
|
---|
171 | * @param toChange
|
---|
172 | * The Item to set the Color.
|
---|
173 | * @param toUse
|
---|
174 | * The Color to give the Item.
|
---|
175 | */
|
---|
176 | public static void SetCurrentItemBackgroundColor(Item toChange, Color toUse) {
|
---|
177 | if (toChange == null)
|
---|
178 | return;
|
---|
179 |
|
---|
180 | toChange.setBackgroundColor(toUse);
|
---|
181 | FrameGraphics.Repaint();
|
---|
182 | }
|
---|
183 |
|
---|
184 | /**
|
---|
185 | * Sets the given Item to have the Given Color. Color can be null (for
|
---|
186 | * default)
|
---|
187 | *
|
---|
188 | * @param toChange
|
---|
189 | * The Item to set the Color.
|
---|
190 | * @param toUse
|
---|
191 | * The Color to give the Item.
|
---|
192 | */
|
---|
193 | public static void SetCurrentItemColor(Item toChange, Color toUse) {
|
---|
194 | if (toChange == null)
|
---|
195 | return;
|
---|
196 |
|
---|
197 | toChange.setColor(toUse);
|
---|
198 | FrameGraphics.Repaint();
|
---|
199 | }
|
---|
200 |
|
---|
201 | /**
|
---|
202 | * Creates a new Text Object containing the Stats that are in the the
|
---|
203 | * JFrame's Title bar, The newly created Text Object is then attached to the
|
---|
204 | * cursor via FrameMouseActions.pickup(Item)
|
---|
205 | */
|
---|
206 | public static void GetSessionStats() {
|
---|
207 | String stats = SessionStats.getCurrentStats();
|
---|
208 |
|
---|
209 | Text text = DisplayIO.getCurrentFrame().createNewText();
|
---|
210 | // We dont want the stats to wrap at all
|
---|
211 | text.setMaxSize(new Dimension(Integer.MAX_VALUE, Integer.MAX_VALUE));
|
---|
212 |
|
---|
213 | text.setText(stats);
|
---|
214 |
|
---|
215 | text.setPosition(DisplayIO.getMouseX(), DisplayIO.getMouseY());
|
---|
216 | text.setSize(STATS_FONT_SIZE);
|
---|
217 |
|
---|
218 | FrameMouseActions.pickup(text);
|
---|
219 |
|
---|
220 | FrameGraphics.Repaint();
|
---|
221 | }
|
---|
222 |
|
---|
223 | /**
|
---|
224 | * Creates a new Text Object containing the contents of the current frames
|
---|
225 | * file.
|
---|
226 | */
|
---|
227 | public static void GetCurrentFrameFile() {
|
---|
228 |
|
---|
229 | String fileContents = FrameIO.SaveFrame(DisplayIO.getCurrentFrame());
|
---|
230 |
|
---|
231 | Text text = DisplayIO.getCurrentFrame().createNewText();
|
---|
232 | // We dont want the stats to wrap at all
|
---|
233 | text.setMaxSize(new Dimension(Integer.MAX_VALUE, Integer.MAX_VALUE));
|
---|
234 |
|
---|
235 | text.setText(fileContents);
|
---|
236 |
|
---|
237 | text.setPosition(DisplayIO.getMouseX(), DisplayIO.getMouseY());
|
---|
238 | text.setSize(FRAME_FILE_FONT_SIZE);
|
---|
239 |
|
---|
240 | FrameMouseActions.pickup(text);
|
---|
241 |
|
---|
242 | FrameGraphics.Repaint();
|
---|
243 | }
|
---|
244 |
|
---|
245 | /**
|
---|
246 | * Creates a new Text Object containing the available fonts.
|
---|
247 | */
|
---|
248 | public static void GetAvailableFontFamilyNames() {
|
---|
249 |
|
---|
250 | String[] availableFonts = GraphicsEnvironment
|
---|
251 | .getLocalGraphicsEnvironment().getAvailableFontFamilyNames();
|
---|
252 | StringBuilder fontsList = new StringBuilder();
|
---|
253 | for (String s : availableFonts) {
|
---|
254 | fontsList.append(s).append('\n');
|
---|
255 | }
|
---|
256 |
|
---|
257 | Text text = DisplayIO.getCurrentFrame().createNewText();
|
---|
258 | // We dont want the stats to wrap at all
|
---|
259 | text.setMaxSize(new Dimension(Integer.MAX_VALUE, Integer.MAX_VALUE));
|
---|
260 | text.setText(fontsList.toString());
|
---|
261 |
|
---|
262 | text.setPosition(DisplayIO.getMouseX(), DisplayIO.getMouseY());
|
---|
263 | text.setSize(FRAME_FILE_FONT_SIZE);
|
---|
264 |
|
---|
265 | FrameMouseActions.pickup(text);
|
---|
266 |
|
---|
267 | FrameGraphics.Repaint();
|
---|
268 | }
|
---|
269 |
|
---|
270 | /**
|
---|
271 | * Resets the statistics back to zero.
|
---|
272 | */
|
---|
273 | public static void ResetStats() {
|
---|
274 | StatsLogger.WriteStatsFile();
|
---|
275 | SessionStats.resetStats();
|
---|
276 | }
|
---|
277 |
|
---|
278 | /**
|
---|
279 | * Loads a frame with the given name and saves it as a JPEG image.
|
---|
280 | *
|
---|
281 | * @param framename
|
---|
282 | * The name of the Frame to save
|
---|
283 | */
|
---|
284 | public static void JpegFrame(String framename) {
|
---|
285 | ImageFrame(framename, "JPG");
|
---|
286 | }
|
---|
287 |
|
---|
288 | /**
|
---|
289 | * Saves the current frame as a JPEG image. This is the same as calling
|
---|
290 | * JpegFrame(currentFrame.getFrameName())
|
---|
291 | */
|
---|
292 | public static void JpegFrame() {
|
---|
293 | ImageFrame(DisplayIO.getCurrentFrame().getFrameName(), "JPG");
|
---|
294 | }
|
---|
295 |
|
---|
296 | /**
|
---|
297 | * Loads a frame with the given name and saves it as a PNG image.
|
---|
298 | *
|
---|
299 | * @param framename
|
---|
300 | * The name of the Frame to save
|
---|
301 | */
|
---|
302 | public static void PNGFrame(String framename) {
|
---|
303 | ImageFrame(framename, "PNG");
|
---|
304 | }
|
---|
305 |
|
---|
306 | /**
|
---|
307 | * Saves the current frame as a PNG image. This is the same as calling
|
---|
308 | * PNGFrame(currentFrame.getFrameName())
|
---|
309 | */
|
---|
310 | public static void PNGFrame() {
|
---|
311 | ImageFrame(DisplayIO.getCurrentFrame().getFrameName(), "PNG");
|
---|
312 | }
|
---|
313 |
|
---|
314 | /**
|
---|
315 | * Saves the Frame with the given Framename as an image of the given format.
|
---|
316 | *
|
---|
317 | * @param framename
|
---|
318 | * The name of the Frame to save as an image
|
---|
319 | * @param format
|
---|
320 | * The Image format to use (i.e. "PNG", "BMP", etc)
|
---|
321 | */
|
---|
322 | public static void ImageFrame(String framename, String format) {
|
---|
323 | Frame loaded = FrameIO.LoadFrame(framename);
|
---|
324 | String fileName = loaded.getExportFileName();
|
---|
325 |
|
---|
326 | // if the frame was loaded successfully
|
---|
327 | if (loaded != null) {
|
---|
328 | // check if the buffer needs to be redrawn
|
---|
329 | // if (loaded.getBuffer() == null)
|
---|
330 | FrameGraphics.UpdateBuffer(loaded, false);
|
---|
331 |
|
---|
332 | BufferedImage screen = loaded.getBuffer().getSnapshot();
|
---|
333 |
|
---|
334 | try {
|
---|
335 | // set up the file for output
|
---|
336 | File out = new File(FrameIO.EXPORTS_DIR + fileName + "."
|
---|
337 | + format.toLowerCase());
|
---|
338 | if (!out.getParentFile().exists())
|
---|
339 | out.mkdirs();
|
---|
340 |
|
---|
341 | ImageIO.write(screen, format, out);
|
---|
342 | FrameGraphics.DisplayMessage("Frame successfully saved to "
|
---|
343 | + FrameIO.EXPORTS_DIR + out.getName());
|
---|
344 | } catch (IOException e) {
|
---|
345 | FrameGraphics.ErrorMessage(e.getMessage());
|
---|
346 | }
|
---|
347 | // if the frame was not loaded successfully, alert the user
|
---|
348 | } else
|
---|
349 | FrameGraphics.DisplayMessage("Frame '" + framename
|
---|
350 | + "' could not be found.");
|
---|
351 | }
|
---|
352 |
|
---|
353 | /**
|
---|
354 | * Displays a message in the message box area.
|
---|
355 | *
|
---|
356 | * @param message
|
---|
357 | * the message to display
|
---|
358 | */
|
---|
359 | public static void MessageLn(String message) {
|
---|
360 | FrameGraphics.DisplayMessage(message);
|
---|
361 | }
|
---|
362 |
|
---|
363 | public static void MessageLn2(String message, String message2) {
|
---|
364 | FrameGraphics.DisplayMessage(message + " " + message2);
|
---|
365 | }
|
---|
366 |
|
---|
367 | public static void CopyFile(String existingFile, String newFileName) {
|
---|
368 | try {
|
---|
369 | // TODO is there a built in method which will do this faster?
|
---|
370 |
|
---|
371 | FrameGraphics.DisplayMessage("Copying file " + existingFile
|
---|
372 | + " to " + newFileName + "...");
|
---|
373 | FrameIO.copyFile(existingFile, newFileName);
|
---|
374 | FrameGraphics.DisplayMessage("File copied successfully");
|
---|
375 | } catch (FileNotFoundException e) {
|
---|
376 | FrameGraphics.DisplayMessage("Error opening file: " + existingFile);
|
---|
377 | } catch (Exception e) {
|
---|
378 | FrameGraphics.DisplayMessage("File could not be copied");
|
---|
379 | }
|
---|
380 | }
|
---|
381 |
|
---|
382 | /**
|
---|
383 | * Loads the Frame linked to by the given Item. The first Item on the Frame
|
---|
384 | * that is not the title or name is then placed on the current frame. The
|
---|
385 | * item that was clicked on is placed on the frame it was linked to and the
|
---|
386 | * link is switched to the item from the child frame. If the given Item has
|
---|
387 | * no link, or no item is found then this is a no-op.
|
---|
388 | *
|
---|
389 | * @param current
|
---|
390 | * The Item that links to the Frame that the Item will be loaded
|
---|
391 | * from.
|
---|
392 | */
|
---|
393 | public static void SwapItemWithItemOnChildFrame(Item current) {
|
---|
394 | Item item = getFirstBodyItemOnChildFrame(current);
|
---|
395 | // if no item was found
|
---|
396 | if (item == null) {
|
---|
397 | return;
|
---|
398 | }
|
---|
399 |
|
---|
400 | // swap the items parents
|
---|
401 | Frame parentFrame = current.getParent();
|
---|
402 | Frame childFrame = item.getParent();
|
---|
403 | current.setParent(childFrame);
|
---|
404 | item.setParent(parentFrame);
|
---|
405 |
|
---|
406 | // swap the items on the frames
|
---|
407 | parentFrame.removeItem(current);
|
---|
408 | childFrame.removeItem(item);
|
---|
409 | parentFrame.addItem(item);
|
---|
410 | childFrame.addItem(current);
|
---|
411 |
|
---|
412 | // swap the items links
|
---|
413 | item.setAction(current.getAction());
|
---|
414 | item.setLink(childFrame.getFrameName());
|
---|
415 | current.setLink(parentFrame.getFrameName());
|
---|
416 | // current.setLink(null);
|
---|
417 | current.setAction(null);
|
---|
418 |
|
---|
419 | FrameGraphics.Repaint();
|
---|
420 | }
|
---|
421 |
|
---|
422 | private static Item getFirstBodyItemOnChildFrame(Item current) {
|
---|
423 | // the item must link to a frame
|
---|
424 | if (current.getLink() == null) {
|
---|
425 | FrameGraphics
|
---|
426 | .DisplayMessage("Cannot get item from child - this item has no link");
|
---|
427 | return null;
|
---|
428 | }
|
---|
429 |
|
---|
430 | Frame child = FrameIO.LoadFrame(current.getAbsoluteLink());
|
---|
431 |
|
---|
432 | // if the frame could not be loaded
|
---|
433 | if (child == null) {
|
---|
434 | FrameGraphics.ErrorMessage("Could not load child frame.");
|
---|
435 | return null;
|
---|
436 | }
|
---|
437 |
|
---|
438 | // find the first non-title and non-name item
|
---|
439 | List<Item> body = child.getItems();
|
---|
440 | Item item = null;
|
---|
441 |
|
---|
442 | for (Item i : body)
|
---|
443 | if (i != child.getTitle() && i != child.getFrameNameItem()
|
---|
444 | && !i.isAnnotation()) {
|
---|
445 | item = i;
|
---|
446 | break;
|
---|
447 | }
|
---|
448 |
|
---|
449 | // if no item was found
|
---|
450 | if (item == null) {
|
---|
451 | FrameGraphics.DisplayMessage("No item found to copy");
|
---|
452 | return null;
|
---|
453 | }
|
---|
454 |
|
---|
455 | return item;
|
---|
456 | }
|
---|
457 | }
|
---|