source: trunk/org/expeditee/actions/Misc.java@ 4

Last change on this file since 4 was 4, checked in by davidb, 16 years ago

Starting source code to Expeditee

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