source: trunk/src/org/expeditee/actions/Misc.java@ 70

Last change on this file since 70 was 70, checked in by ra33, 16 years ago

Made LOTS of changes...
Added DisplayComet
A whole bunch more stats for items and events
Changed lots of stuff for drawing better especially using text as line endpoints

File size: 13.3 KB
Line 
1package org.expeditee.actions;
2
3import java.awt.Color;
4import java.awt.Dimension;
5import java.awt.GraphicsEnvironment;
6import java.awt.image.BufferedImage;
7import java.io.File;
8import java.io.FileNotFoundException;
9import java.io.IOException;
10import java.util.ArrayList;
11import java.util.List;
12
13import javax.imageio.ImageIO;
14
15import org.expeditee.gui.DisplayIO;
16import org.expeditee.gui.Frame;
17import org.expeditee.gui.FrameGraphics;
18import org.expeditee.gui.FrameIO;
19import org.expeditee.gui.FrameMouseActions;
20import org.expeditee.items.Item;
21import org.expeditee.items.Text;
22import org.expeditee.stats.SessionStats;
23import org.expeditee.stats.StatsLogger;
24
25/**
26 * A list of miscellaneous KMS Actions and Actions specific to Expeditee
27 *
28 */
29public 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 getFromChildFrame(current, false);
153 }
154
155 /**
156 * Loads the Frame linked to by the given Item. The first Text Item on the Frame
157 * that is not the title or name is then placed on the cursor. If the given
158 * Item has no link, or no item is found then this is a no-op.
159 *
160 * @param current
161 * The Item that links to the Frame that the Item will be loaded
162 * from.
163 */
164 public static void GetTextFromChildFrame(Item current) {
165 getFromChildFrame(current, true);
166 }
167
168 private static void getFromChildFrame(Item current, boolean textOnly){
169 Item item = getFirstBodyItemOnChildFrame(current, textOnly);
170 // if no item was found
171 if (item == null) {
172 return;
173 }
174
175 // copy the item and switch
176 item = item.copy();
177 item.setPosition(DisplayIO.getMouseX(), DisplayIO.getMouseY());
178 item.setParent(null);
179
180 FrameMouseActions.pickup(item);
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 SetCurrentItemBackgroundColor(Item toChange, Color toUse) {
194 if (toChange == null)
195 return;
196
197 toChange.setBackgroundColor(toUse);
198 FrameGraphics.Repaint();
199 }
200
201 /**
202 * Sets the given Item to have the Given Color. Color can be null (for
203 * default)
204 *
205 * @param toChange
206 * The Item to set the Color.
207 * @param toUse
208 * The Color to give the Item.
209 */
210 public static void SetCurrentItemColor(Item toChange, Color toUse) {
211 if (toChange == null)
212 return;
213
214 toChange.setColor(toUse);
215 FrameGraphics.Repaint();
216 }
217
218 /**
219 * Creates a new Text Object containing general statistics for the current
220 * session. The newly created Text Object is then attached to the cursor via
221 * FrameMouseActions.pickup(Item)
222 */
223 public static void GetSessionStats() {
224 CreateTextItem(SessionStats.getCurrentStats());
225 }
226
227 public static void CreateTextItem(String itemText) {
228 Text text = DisplayIO.getCurrentFrame().createNewText();
229 // We dont want the stats to wrap at all
230 text.setMaxSize(new Dimension(Integer.MAX_VALUE, Integer.MAX_VALUE));
231
232 text.setText(itemText);
233
234 text.setPosition(DisplayIO.getMouseX(), DisplayIO.getMouseY());
235 text.setSize(STATS_FONT_SIZE);
236
237 FrameMouseActions.pickup(text);
238
239 FrameGraphics.Repaint();
240 }
241
242 /**
243 * Creates a new Text Object containing statistics for moving, deleting and
244 * creating items in the current session. The newly created Text Object is
245 * then attached to the cursor via FrameMouseActions.pickup(Item)
246 */
247 public static void GetItemStats() {
248 CreateTextItem(SessionStats.getItemStats());
249 }
250
251 /**
252 * Creates a new Text Object containing statistics for the time between
253 * events triggered by the user through mouse clicks and key presses. The
254 * newly created Text Object is then attached to the cursor via
255 * FrameMouseActions.pickup(Item)
256 */
257 public static void GetEventStats() {
258 CreateTextItem(SessionStats.getEventStats());
259 }
260
261 /**
262 * Creates a new Text Object containing the contents of the current frames
263 * file.
264 */
265 public static void GetCurrentFrameFile() {
266
267 String fileContents = FrameIO.SaveFrame(DisplayIO.getCurrentFrame());
268
269 Text text = DisplayIO.getCurrentFrame().createNewText();
270 // We dont want the stats to wrap at all
271 text.setMaxSize(new Dimension(Integer.MAX_VALUE, Integer.MAX_VALUE));
272
273 text.setText(fileContents);
274
275 text.setPosition(DisplayIO.getMouseX(), DisplayIO.getMouseY());
276 text.setSize(FRAME_FILE_FONT_SIZE);
277
278 FrameMouseActions.pickup(text);
279
280 FrameGraphics.Repaint();
281 }
282
283 /**
284 * Creates a new Text Object containing the available fonts.
285 */
286 public static void GetAvailableFontFamilyNames() {
287
288 String[] availableFonts = GraphicsEnvironment
289 .getLocalGraphicsEnvironment().getAvailableFontFamilyNames();
290 StringBuilder fontsList = new StringBuilder();
291 for (String s : availableFonts) {
292 fontsList.append(s).append('\n');
293 }
294
295 Text text = DisplayIO.getCurrentFrame().createNewText();
296 // We dont want the stats to wrap at all
297 text.setMaxSize(new Dimension(Integer.MAX_VALUE, Integer.MAX_VALUE));
298 text.setText(fontsList.toString());
299
300 text.setPosition(DisplayIO.getMouseX(), DisplayIO.getMouseY());
301 text.setSize(FRAME_FILE_FONT_SIZE);
302
303 FrameMouseActions.pickup(text);
304
305 FrameGraphics.Repaint();
306 }
307
308 /**
309 * Resets the statistics back to zero.
310 */
311 public static void ResetStats() {
312 StatsLogger.WriteStatsFile();
313 SessionStats.resetStats();
314 }
315
316 /**
317 * Loads a frame with the given name and saves it as a JPEG image.
318 *
319 * @param framename
320 * The name of the Frame to save
321 */
322 public static void JpegFrame(String framename) {
323 ImageFrame(framename, "JPG");
324 }
325
326 /**
327 * Saves the current frame as a JPEG image. This is the same as calling
328 * JpegFrame(currentFrame.getFrameName())
329 */
330 public static void JpegFrame() {
331 ImageFrame(DisplayIO.getCurrentFrame().getFrameName(), "JPG");
332 }
333
334 /**
335 * Loads a frame with the given name and saves it as a PNG image.
336 *
337 * @param framename
338 * The name of the Frame to save
339 */
340 public static void PNGFrame(String framename) {
341 ImageFrame(framename, "PNG");
342 }
343
344 /**
345 * Saves the current frame as a PNG image. This is the same as calling
346 * PNGFrame(currentFrame.getFrameName())
347 */
348 public static void PNGFrame() {
349 ImageFrame(DisplayIO.getCurrentFrame().getFrameName(), "PNG");
350 }
351
352 /**
353 * Saves the Frame with the given Framename as an image of the given format.
354 *
355 * @param framename
356 * The name of the Frame to save as an image
357 * @param format
358 * The Image format to use (i.e. "PNG", "BMP", etc)
359 */
360 public static void ImageFrame(String framename, String format) {
361 Frame loaded = FrameIO.LoadFrame(framename);
362 String fileName = loaded.getExportFileName();
363
364 // if the frame was loaded successfully
365 if (loaded != null) {
366 // check if the buffer needs to be redrawn
367 // if (loaded.getBuffer() == null)
368 FrameGraphics.UpdateBuffer(loaded, false);
369
370 BufferedImage screen = loaded.getBuffer().getSnapshot();
371
372 try {
373 // set up the file for output
374 File out = new File(FrameIO.EXPORTS_DIR + fileName + "."
375 + format.toLowerCase());
376 if (!out.getParentFile().exists())
377 out.mkdirs();
378
379 ImageIO.write(screen, format, out);
380 FrameGraphics.DisplayMessage("Frame successfully saved to "
381 + FrameIO.EXPORTS_DIR + out.getName());
382 } catch (IOException e) {
383 FrameGraphics.ErrorMessage(e.getMessage());
384 }
385 // if the frame was not loaded successfully, alert the user
386 } else
387 FrameGraphics.DisplayMessage("Frame '" + framename
388 + "' could not be found.");
389 }
390
391 /**
392 * Displays a message in the message box area.
393 *
394 * @param message
395 * the message to display
396 */
397 public static void MessageLn(String message) {
398 FrameGraphics.DisplayMessage(message);
399 }
400
401 public static void MessageLn2(String message, String message2) {
402 FrameGraphics.DisplayMessage(message + " " + message2);
403 }
404
405 public static void CopyFile(String existingFile, String newFileName) {
406 try {
407 // TODO is there a built in method which will do this faster?
408
409 FrameGraphics.DisplayMessage("Copying file " + existingFile
410 + " to " + newFileName + "...");
411 FrameIO.copyFile(existingFile, newFileName);
412 FrameGraphics.DisplayMessage("File copied successfully");
413 } catch (FileNotFoundException e) {
414 FrameGraphics.DisplayMessage("Error opening file: " + existingFile);
415 } catch (Exception e) {
416 FrameGraphics.DisplayMessage("File could not be copied");
417 }
418 }
419
420 /**
421 * Loads the Frame linked to by the given Item. The first Item on the Frame
422 * that is not the title or name is then placed on the current frame. The
423 * item that was clicked on is placed on the frame it was linked to and the
424 * link is switched to the item from the child frame. If the given Item has
425 * no link, or no item is found then this is a no-op.
426 *
427 * @param current
428 * The Item that links to the Frame that the Item will be loaded
429 * from.
430 */
431 public static void SwapItemWithItemOnChildFrame(Item current) {
432 Item item = getFirstBodyItemOnChildFrame(current, false);
433 // if no item was found
434 if (item == null) {
435 return;
436 }
437
438 // swap the items parents
439 Frame parentFrame = current.getParent();
440 Frame childFrame = item.getParent();
441 current.setParent(childFrame);
442 item.setParent(parentFrame);
443
444 // swap the items on the frames
445 parentFrame.removeItem(current);
446 childFrame.removeItem(item);
447 parentFrame.addItem(item);
448 childFrame.addItem(current);
449
450 // swap the items links
451 item.setAction(current.getAction());
452 item.setLink(childFrame.getFrameName());
453 current.setLink(parentFrame.getFrameName());
454 // current.setLink(null);
455 current.setAction(null);
456
457 FrameGraphics.Repaint();
458 }
459
460 private static Item getFirstBodyItemOnChildFrame(Item current, boolean textOnly) {
461 // the item must link to a frame
462 if (current.getLink() == null) {
463 FrameGraphics
464 .DisplayMessage("Cannot get item from child - this item has no link");
465 return null;
466 }
467
468 Frame child = FrameIO.LoadFrame(current.getAbsoluteLink());
469
470 // if the frame could not be loaded
471 if (child == null) {
472 FrameGraphics.ErrorMessage("Could not load child frame.");
473 return null;
474 }
475
476 // find the first non-title and non-name item
477 List<Item> body = new ArrayList<Item>();
478 if(textOnly)
479 body.addAll(child.getBodyTextItems(false));
480 else
481 body.addAll(child.getItems());
482 Item item = null;
483
484 for (Item i : body)
485 if (i != child.getTitle() && !i.isAnnotation()) {
486 item = i;
487 break;
488 }
489
490 // if no item was found
491 if (item == null) {
492 FrameGraphics.DisplayMessage("No item found to copy");
493 return null;
494 }
495
496 return item;
497 }
498}
Note: See TracBrowser for help on using the repository browser.