1 | package org.expeditee.gui;
|
---|
2 |
|
---|
3 | import java.awt.AWTException;
|
---|
4 | import java.awt.Color;
|
---|
5 | import java.awt.Cursor;
|
---|
6 | import java.awt.Image;
|
---|
7 | import java.awt.Point;
|
---|
8 | import java.awt.Robot;
|
---|
9 | import java.awt.Toolkit;
|
---|
10 | import java.awt.geom.Point2D;
|
---|
11 | import java.awt.image.MemoryImageSource;
|
---|
12 | import java.util.ArrayList;
|
---|
13 | import java.util.Collection;
|
---|
14 | import java.util.LinkedList;
|
---|
15 | import java.util.List;
|
---|
16 | import java.util.Stack;
|
---|
17 |
|
---|
18 | import javax.swing.JOptionPane;
|
---|
19 |
|
---|
20 | import org.expeditee.items.Item;
|
---|
21 | import org.expeditee.items.ItemParentStateChangedEvent;
|
---|
22 | import org.expeditee.items.Picture;
|
---|
23 | import org.expeditee.items.Text;
|
---|
24 | import org.expeditee.items.widgets.WidgetCacheManager;
|
---|
25 | import org.expeditee.stats.SessionStats;
|
---|
26 | import org.expeditee.taskmanagement.EntitySaveManager;
|
---|
27 |
|
---|
28 | /**
|
---|
29 | * This Interface is used by the Frame to control all display input and output.
|
---|
30 | *
|
---|
31 | * @author jdm18
|
---|
32 | *
|
---|
33 | */
|
---|
34 | public class DisplayIO {
|
---|
35 |
|
---|
36 | private static final int SMALL_CURSOR_SIZE = 16;
|
---|
37 |
|
---|
38 | private static final int MEDIUM_CURSOR_SIZE = 32;
|
---|
39 |
|
---|
40 | private static final int LARGE_CURSOR_SIZE = 64;
|
---|
41 |
|
---|
42 | /**
|
---|
43 | * The color to be used to highlight the linked parent item, when the user
|
---|
44 | * navigates backwards.
|
---|
45 | */
|
---|
46 | public static final Color BACK_HIGHLIGHT_COLOR = Color.MAGENTA;
|
---|
47 |
|
---|
48 | private static Browser _Browser;
|
---|
49 |
|
---|
50 | // The current Frame being displayed on the screen.
|
---|
51 | private static Frame _CurrentFrames[] = new Frame[2];
|
---|
52 |
|
---|
53 | // Maintains the list of frames visited thus-far for back-tracking
|
---|
54 | @SuppressWarnings("unchecked")
|
---|
55 | private static Stack<String>[] _VisitedFrames = new Stack[2];
|
---|
56 | @SuppressWarnings("unchecked")
|
---|
57 | private static Stack<String>[] _BackedUpFrames = new Stack[2];
|
---|
58 |
|
---|
59 | // used to change the mouse cursor position on the screen
|
---|
60 | private static Robot _Robot;
|
---|
61 |
|
---|
62 | private static boolean _TwinFrames = false;
|
---|
63 |
|
---|
64 | /**
|
---|
65 | * The title to display in the Title bar.
|
---|
66 | */
|
---|
67 | public static final String TITLE = "Exp08Aug2008A";
|
---|
68 |
|
---|
69 | private DisplayIO() {
|
---|
70 | }
|
---|
71 |
|
---|
72 | public static void Init(Browser browser) {
|
---|
73 | _Browser = browser;
|
---|
74 | try {
|
---|
75 | _Robot = new Robot();
|
---|
76 | } catch (AWTException e) {
|
---|
77 | e.printStackTrace();
|
---|
78 | }
|
---|
79 |
|
---|
80 | Point mouse = _Browser.getMousePosition();
|
---|
81 | if (mouse != null) {
|
---|
82 | FrameMouseActions.MouseX = mouse.x;
|
---|
83 | FrameMouseActions.MouseY = mouse.y;
|
---|
84 | }
|
---|
85 |
|
---|
86 | _VisitedFrames[0] = new Stack<String>();
|
---|
87 | _VisitedFrames[1] = new Stack<String>();
|
---|
88 | _BackedUpFrames[0] = new Stack<String>();
|
---|
89 | _BackedUpFrames[1] = new Stack<String>();
|
---|
90 | }
|
---|
91 |
|
---|
92 | public static void setTextCursor(Text text, int cursorMovement) {
|
---|
93 | setTextCursor(text, cursorMovement, false, false);
|
---|
94 | }
|
---|
95 |
|
---|
96 | public static void setTextCursor(Text text, int cursorMovement,
|
---|
97 | boolean newSize, boolean isShiftDown) {
|
---|
98 |
|
---|
99 | int size = Math.round(text.getSize());
|
---|
100 | Point2D.Float newMouse = text.moveCursor(cursorMovement, DisplayIO
|
---|
101 | .getFloatMouseX(), FrameMouseActions.MouseY, isShiftDown);
|
---|
102 |
|
---|
103 | if (!newSize && cursorType == Item.TEXT_CURSOR) {
|
---|
104 | if (cursorMovement != 0)
|
---|
105 | DisplayIO.setCursorPosition(newMouse, false);
|
---|
106 | return;
|
---|
107 | }
|
---|
108 |
|
---|
109 | cursorType = Item.TEXT_CURSOR;
|
---|
110 |
|
---|
111 | // Do some stuff to adjust the cursor size based on the font size
|
---|
112 | final int MEDIUM_CURSOR_CUTOFF = 31;
|
---|
113 | final int LARGE_CURSOR_CUTOFF = 62;
|
---|
114 |
|
---|
115 | int cursorSize = LARGE_CURSOR_SIZE;
|
---|
116 | int hotspotPos = 0;
|
---|
117 | int start = 0;
|
---|
118 |
|
---|
119 | if (size < MEDIUM_CURSOR_CUTOFF) {
|
---|
120 | cursorSize = MEDIUM_CURSOR_SIZE;
|
---|
121 | start = cursorSize - size - 2;
|
---|
122 | hotspotPos = cursorSize - (size + 2) / 4;
|
---|
123 | } else if (size < LARGE_CURSOR_CUTOFF) {
|
---|
124 | hotspotPos = cursorSize - (size - 5) / 4;
|
---|
125 | start = cursorSize - size - 2;
|
---|
126 | } else {
|
---|
127 | int FIXED_CURSOR_MIN = 77;
|
---|
128 | if (size >= FIXED_CURSOR_MIN) {
|
---|
129 | hotspotPos = cursorSize - 2;
|
---|
130 | } else {
|
---|
131 | hotspotPos = size - (FIXED_CURSOR_MIN - cursorSize);
|
---|
132 | }
|
---|
133 | }
|
---|
134 |
|
---|
135 | int[] pixels = new int[cursorSize * cursorSize];
|
---|
136 |
|
---|
137 | for (int i = start; i < cursorSize; i++)
|
---|
138 | pixels[i * cursorSize] = pixels[(i * cursorSize) + 1] = 0xFF000000;
|
---|
139 |
|
---|
140 | Image image = Toolkit.getDefaultToolkit().createImage(
|
---|
141 | new MemoryImageSource(cursorSize, cursorSize, pixels, 0,
|
---|
142 | cursorSize));
|
---|
143 | Cursor textCursor = Toolkit.getDefaultToolkit().createCustomCursor(
|
---|
144 | image, new Point(0, hotspotPos), "textcursor");
|
---|
145 | _Browser.setCursor(textCursor);
|
---|
146 | if (cursorMovement != Text.NONE)
|
---|
147 | DisplayIO.setCursorPosition(newMouse, false);
|
---|
148 | }
|
---|
149 |
|
---|
150 | /**
|
---|
151 | * Sets the type of cursor the display should be using
|
---|
152 | *
|
---|
153 | * @param type
|
---|
154 | * The type of cursor to display, using constants defined in the
|
---|
155 | * Cursor class.
|
---|
156 | */
|
---|
157 | public static void setCursor(int type) {
|
---|
158 | // avoid flicker when not changing
|
---|
159 | if (type == cursorType || type == Item.UNCHANGED_CURSOR)
|
---|
160 | return;
|
---|
161 |
|
---|
162 | cursorType = type;
|
---|
163 |
|
---|
164 | if (type == Item.HIDDEN_CURSOR) {
|
---|
165 | int[] pixels = new int[SMALL_CURSOR_SIZE * SMALL_CURSOR_SIZE];
|
---|
166 | Image image = Toolkit.getDefaultToolkit().createImage(
|
---|
167 | new MemoryImageSource(SMALL_CURSOR_SIZE, SMALL_CURSOR_SIZE,
|
---|
168 | pixels, 0, SMALL_CURSOR_SIZE));
|
---|
169 | Cursor transparentCursor = Toolkit.getDefaultToolkit()
|
---|
170 | .createCustomCursor(image, new Point(0, 0),
|
---|
171 | "invisiblecursor");
|
---|
172 | _Browser.setCursor(transparentCursor);
|
---|
173 | } else
|
---|
174 | _Browser.setCursor(new Cursor(type));
|
---|
175 | }
|
---|
176 |
|
---|
177 | private static int cursorType = Item.DEFAULT_CURSOR;
|
---|
178 |
|
---|
179 | public static int getCursor() {
|
---|
180 | return cursorType;
|
---|
181 | }
|
---|
182 |
|
---|
183 | /**
|
---|
184 | * Moves the mouse cursor to the given x,y coordinates on the screen
|
---|
185 | *
|
---|
186 | * @param x
|
---|
187 | * The x coordinate
|
---|
188 | * @param y
|
---|
189 | * The y coordinate
|
---|
190 | */
|
---|
191 | public static void setCursorPosition(float x, float y) {
|
---|
192 | setCursorPosition(x, y, true);
|
---|
193 | }
|
---|
194 |
|
---|
195 | public static void setCursorPosition(float x, float y, boolean forceArrow) {
|
---|
196 | // Adjust the position to move the mouse to to account for being in
|
---|
197 | // TwinFramesMode
|
---|
198 | if (_TwinFrames) {
|
---|
199 | if (getCurrentSide() == 1) {
|
---|
200 | int middle = getMiddle();
|
---|
201 | x += middle;
|
---|
202 | }
|
---|
203 | }
|
---|
204 |
|
---|
205 | float deltax = x - FrameMouseActions.MouseX;
|
---|
206 | float deltay = y - FrameMouseActions.MouseY;
|
---|
207 |
|
---|
208 | // When the Robot moves the cursor... a short time later a mouseMoved
|
---|
209 | // event is generated...
|
---|
210 | // We want to ignore this event by remembering the location the robot
|
---|
211 | // was shifted to.
|
---|
212 | FrameMouseActions.setLastRobotMove(x, y);
|
---|
213 |
|
---|
214 | if (FreeItems.itemAttachedToCursor()) {
|
---|
215 | List<Item> toMove = FreeItems.getInstance();
|
---|
216 | for (Item move : toMove) {
|
---|
217 | move.setPosition(move.getX() + deltax, move.getY() + deltay);
|
---|
218 | }
|
---|
219 | }
|
---|
220 |
|
---|
221 | // cheat
|
---|
222 | FrameMouseActions.setForceArrow(forceArrow);
|
---|
223 | int mouseX = (int) _Browser.getContentPane().getLocationOnScreen()
|
---|
224 | .getX()
|
---|
225 | + Math.round(x);
|
---|
226 | int mouseY = (int) _Browser.getContentPane().getLocationOnScreen()
|
---|
227 | .getY()
|
---|
228 | + Math.round(y);
|
---|
229 | _Robot.mouseMove(mouseX, mouseY);
|
---|
230 | // System.out.println("MouseMoved: " + x + "," + y);
|
---|
231 | }
|
---|
232 |
|
---|
233 | public static void resetCursorOffset() {
|
---|
234 | FrameMouseActions.resetOffset();
|
---|
235 | }
|
---|
236 |
|
---|
237 | /**
|
---|
238 | * Sets the current cursor position in the current frame
|
---|
239 | *
|
---|
240 | * @param pos
|
---|
241 | */
|
---|
242 | public static void setCursorPosition(Point2D.Float pos) {
|
---|
243 | setCursorPosition(pos.x, pos.y);
|
---|
244 | }
|
---|
245 |
|
---|
246 | public static void setCursorPosition(Point pos) {
|
---|
247 | setCursorPosition(pos.x, pos.y);
|
---|
248 | }
|
---|
249 |
|
---|
250 | public static void setCursorPosition(Point pos, boolean forceArrow) {
|
---|
251 | setCursorPosition(pos.x, pos.y, forceArrow);
|
---|
252 | }
|
---|
253 |
|
---|
254 | public static void setCursorPosition(Point2D.Float pos, boolean forceArrow) {
|
---|
255 | setCursorPosition(pos.x, pos.y, forceArrow);
|
---|
256 | }
|
---|
257 |
|
---|
258 | /**
|
---|
259 | * Returns the top item (last added) of the Back-Stack (which is popped off)
|
---|
260 | *
|
---|
261 | * @return The name of the last Frame added to the back-stack
|
---|
262 | */
|
---|
263 | public static String getLastFrame() {
|
---|
264 | int side = getCurrentSide();
|
---|
265 |
|
---|
266 | if (_VisitedFrames[side].size() > 0)
|
---|
267 | return _VisitedFrames[side].pop();
|
---|
268 | else
|
---|
269 | return null;
|
---|
270 | }
|
---|
271 |
|
---|
272 | /**
|
---|
273 | * Adds the given Frame to the back-stack
|
---|
274 | *
|
---|
275 | * @param frame
|
---|
276 | * The Frame to add
|
---|
277 | */
|
---|
278 | public static void addToBack(Frame toAdd) {
|
---|
279 | int side = getCurrentSide();
|
---|
280 |
|
---|
281 | // // do not allow duplicate frames
|
---|
282 | // if (_VisitedFrames[side].size() > 0)
|
---|
283 | // if (_VisitedFrames[side].peek().equals(toAdd.getName())) {
|
---|
284 | // return;
|
---|
285 | // }
|
---|
286 |
|
---|
287 | Item ip = FrameUtils.getCurrentItem();
|
---|
288 | if (ip == null)
|
---|
289 | _VisitedFrames[side].push(toAdd.getName());
|
---|
290 | else
|
---|
291 | _VisitedFrames[side].push(toAdd.getName());
|
---|
292 | // System.out.println("Added: " + _VisitedFrames[side].size());
|
---|
293 | }
|
---|
294 |
|
---|
295 | public static String removeFromBack() {
|
---|
296 | int side = getCurrentSide();
|
---|
297 |
|
---|
298 | // there must be a frame to go back to
|
---|
299 | if (_VisitedFrames[side].size() > 0) {
|
---|
300 | return _VisitedFrames[side].pop();
|
---|
301 | }
|
---|
302 | return null;
|
---|
303 | }
|
---|
304 |
|
---|
305 | /**
|
---|
306 | * Returns a 'peek' at the end element on the back-stack of the current
|
---|
307 | * side. If the back-stack is empty, null is returned.
|
---|
308 | *
|
---|
309 | * @return The name of the most recent Frame added to the back-stack, or
|
---|
310 | * null if the back-stack is empty.
|
---|
311 | */
|
---|
312 | public static String peekFromBackUpStack() {
|
---|
313 | int side = getCurrentSide();
|
---|
314 |
|
---|
315 | // check that the stack is not empty
|
---|
316 | if (_VisitedFrames[side].size() > 0)
|
---|
317 | return _VisitedFrames[side].peek();
|
---|
318 |
|
---|
319 | // if the stack is empty, return null
|
---|
320 | return null;
|
---|
321 | }
|
---|
322 |
|
---|
323 | public static void setCurrentFrame(Frame frame, boolean incrementStats) {
|
---|
324 | if (frame == null)
|
---|
325 | return;
|
---|
326 |
|
---|
327 | // Remove any popups that are showing on the current frame
|
---|
328 | PopupManager.getInstance().hideAutohidePopups();
|
---|
329 |
|
---|
330 | if (_TwinFrames) {
|
---|
331 | if (_CurrentFrames[0] == null) {
|
---|
332 | _CurrentFrames[0] = frame;
|
---|
333 | return;
|
---|
334 | }
|
---|
335 | if (_CurrentFrames[1] == null) {
|
---|
336 | _CurrentFrames[1] = frame;
|
---|
337 | return;
|
---|
338 | }
|
---|
339 | }
|
---|
340 |
|
---|
341 | // if this is already the current frame
|
---|
342 | if (frame == getCurrentFrame()) {
|
---|
343 | FrameGraphics.Repaint();
|
---|
344 | MessageBay.displayMessage(frame.getName()
|
---|
345 | + " is already the current frame.");
|
---|
346 | return;
|
---|
347 | } else if(incrementStats) {
|
---|
348 | SessionStats.AccessedFrame();
|
---|
349 | }
|
---|
350 |
|
---|
351 | // Invalidate free items
|
---|
352 | if (!FreeItems.getInstance().isEmpty() && getCurrentFrame() != null) {
|
---|
353 |
|
---|
354 | // Empty free items temporarily so that the old frames buffer is
|
---|
355 | // repainted
|
---|
356 | // without the free items.
|
---|
357 | ArrayList<? extends Item> tmp = (ArrayList<? extends Item>) FreeItems
|
---|
358 | .getInstance().clone();
|
---|
359 | FreeItems.getInstance().clear(); // NOTE: This will invalidate
|
---|
360 | // all the cleared free items
|
---|
361 | FrameGraphics.refresh(true);
|
---|
362 | FreeItems.getInstance().addAll(tmp);
|
---|
363 |
|
---|
364 | }
|
---|
365 |
|
---|
366 | // Changing frames is a Save point for saveable entities:
|
---|
367 | EntitySaveManager.getInstance().saveAll();
|
---|
368 | if (_TwinFrames) {
|
---|
369 | // if the same frame is being shown in both sides, load a fresh
|
---|
370 | // copy from disk
|
---|
371 | if (_CurrentFrames[getOppositeSide()] == frame
|
---|
372 | || _CurrentFrames[getOppositeSide()].hasOverlay(frame)) {
|
---|
373 | FrameIO.SuspendCache();
|
---|
374 | frame = FrameIO.LoadFrame(frame.getName());
|
---|
375 | FrameIO.ResumeCache();
|
---|
376 | }
|
---|
377 |
|
---|
378 | // If the frames are the same then the items for the
|
---|
379 | // frame that is just about to hide will still be in view
|
---|
380 | // so only notify items that they are hidden if the
|
---|
381 | // frames differ.
|
---|
382 | if (_CurrentFrames[getCurrentSide()] != null
|
---|
383 | && _CurrentFrames[0] != _CurrentFrames[1]) {
|
---|
384 | for (Item i : _CurrentFrames[getCurrentSide()].getItems()) {
|
---|
385 | i.onParentStateChanged(new ItemParentStateChangedEvent(
|
---|
386 | _CurrentFrames[getCurrentSide()],
|
---|
387 | ItemParentStateChangedEvent.EVENT_TYPE_HIDDEN));
|
---|
388 | }
|
---|
389 | }
|
---|
390 | _CurrentFrames[getCurrentSide()] = frame;
|
---|
391 |
|
---|
392 | // BROOK : TODO... overlays and loadable widgets
|
---|
393 | for (Item i : _CurrentFrames[getCurrentSide()].getItems()) {
|
---|
394 | i.onParentStateChanged(new ItemParentStateChangedEvent(
|
---|
395 | _CurrentFrames[getCurrentSide()],
|
---|
396 | ItemParentStateChangedEvent.EVENT_TYPE_SHOWN));
|
---|
397 | }
|
---|
398 | } else {
|
---|
399 |
|
---|
400 | // Notifying items on the frame being hidden that they
|
---|
401 | // are about to be hidden.
|
---|
402 | // ie. Widgets use this method to remove themselves from the JPanel
|
---|
403 | List<Frame> currentOnlyOverlays = new LinkedList<Frame>();
|
---|
404 | List<Frame> nextOnlyOverlays = new LinkedList<Frame>();
|
---|
405 | List<Frame> sharedOverlays = new LinkedList<Frame>();
|
---|
406 |
|
---|
407 | // Get all overlayed frames seen by the next frame
|
---|
408 | for (Overlay o : frame.getOverlays()) {
|
---|
409 | if (!nextOnlyOverlays.contains(o))
|
---|
410 | nextOnlyOverlays.add(o.Frame);
|
---|
411 | }
|
---|
412 |
|
---|
413 | // Get all overlayed frames seen by the current frame
|
---|
414 | if (_CurrentFrames[getCurrentSide()] != null) {
|
---|
415 | for (Overlay o : _CurrentFrames[getCurrentSide()].getOverlays()) {
|
---|
416 | if (!currentOnlyOverlays.contains(o))
|
---|
417 | currentOnlyOverlays.add(o.Frame);
|
---|
418 | }
|
---|
419 | }
|
---|
420 |
|
---|
421 | // Extract shared overlays between the current and next frame
|
---|
422 | for (Frame of : currentOnlyOverlays) {
|
---|
423 | if (nextOnlyOverlays.contains(of)) {
|
---|
424 | sharedOverlays.add(of);
|
---|
425 | }
|
---|
426 | }
|
---|
427 |
|
---|
428 | // The first set, currentOnlyOverlays, must be notified that they
|
---|
429 | // are hidden
|
---|
430 | Collection<Item> items = new LinkedList<Item>();
|
---|
431 |
|
---|
432 | // Notify items that will not be in view any more
|
---|
433 | if (_CurrentFrames[getCurrentSide()] != null) {
|
---|
434 | List<Frame> seen = new LinkedList<Frame>();
|
---|
435 | seen.addAll(sharedOverlays); // Signify that seen all shared
|
---|
436 | // overlays
|
---|
437 | seen.remove(_CurrentFrames[getCurrentSide()]); // must ensure
|
---|
438 | // excluded
|
---|
439 |
|
---|
440 | // Get all items seen from the current frame - including all
|
---|
441 | // possible non-shared overlays
|
---|
442 | items = _CurrentFrames[getCurrentSide()].getAllItems();
|
---|
443 | for (Frame f : seen)
|
---|
444 | items.removeAll(f.getAllItems());
|
---|
445 |
|
---|
446 | // Notify items that they are hidden
|
---|
447 | for (Item i : items) {
|
---|
448 | i.onParentStateChanged(new ItemParentStateChangedEvent(
|
---|
449 | _CurrentFrames[getCurrentSide()],
|
---|
450 | ItemParentStateChangedEvent.EVENT_TYPE_HIDDEN));
|
---|
451 | }
|
---|
452 | }
|
---|
453 |
|
---|
454 | // Set the new frame
|
---|
455 | _CurrentFrames[getCurrentSide()] = frame;
|
---|
456 | frame.refreshSize(FrameGraphics.getMaxFrameSize());
|
---|
457 |
|
---|
458 | // Notify items on the frame being displayed that they are in view
|
---|
459 | // ie. widgets use this method to add themselves to the content pane
|
---|
460 | items.clear();
|
---|
461 |
|
---|
462 | // Notify overlay items that they are shown
|
---|
463 | for (Item i : frame.getOverlayItems()) {
|
---|
464 | Overlay owner = frame.getOverlayOwner(i);
|
---|
465 | // if (owner == null) i.onParentFameShown(false, 0);
|
---|
466 | // else ...
|
---|
467 | assert (owner != null);
|
---|
468 | i
|
---|
469 | .onParentStateChanged(new ItemParentStateChangedEvent(
|
---|
470 | frame,
|
---|
471 | ItemParentStateChangedEvent.EVENT_TYPE_SHOWN_VIA_OVERLAY,
|
---|
472 | owner.permission));
|
---|
473 | }
|
---|
474 |
|
---|
475 | for (Item i : frame.getItems()) {
|
---|
476 | i.onParentStateChanged(new ItemParentStateChangedEvent(frame,
|
---|
477 | ItemParentStateChangedEvent.EVENT_TYPE_SHOWN));
|
---|
478 | }
|
---|
479 | }
|
---|
480 |
|
---|
481 | // Heavyduty widgets with lots of data may need to unload
|
---|
482 | WidgetCacheManager.onFrameChanged();
|
---|
483 |
|
---|
484 | FrameGraphics.refresh(false);
|
---|
485 | }
|
---|
486 |
|
---|
487 | public static void UpdateTitle() {
|
---|
488 | StringBuffer title = new StringBuffer(TITLE);
|
---|
489 |
|
---|
490 | if (FrameGraphics.isAudienceMode())
|
---|
491 | title.append(" - Audience Mode");
|
---|
492 | else if (FrameGraphics.isXRayMode())
|
---|
493 | title.append(" - X-Ray Mode");
|
---|
494 | else
|
---|
495 | title.append(" [").append(SessionStats.getShortStats()).append(']');
|
---|
496 |
|
---|
497 | _Browser.setTitle(title.toString());
|
---|
498 | }
|
---|
499 |
|
---|
500 | public static int getCurrentSide() {
|
---|
501 | if (_Browser == null)
|
---|
502 | return 0;
|
---|
503 |
|
---|
504 | if (_TwinFrames
|
---|
505 | && FrameMouseActions.MouseX >= (_Browser.getWidth() / 2F)
|
---|
506 | && _CurrentFrames[1] != null)
|
---|
507 | return 1;
|
---|
508 |
|
---|
509 | if (_CurrentFrames[0] == null && _CurrentFrames[1] != null)
|
---|
510 | return 1;
|
---|
511 |
|
---|
512 | return 0;
|
---|
513 | }
|
---|
514 |
|
---|
515 | private static int getOppositeSide() {
|
---|
516 | if (getCurrentSide() == 0)
|
---|
517 | return 1;
|
---|
518 |
|
---|
519 | return 0;
|
---|
520 | }
|
---|
521 |
|
---|
522 | public static int FrameOnSide(Frame toFind) {
|
---|
523 | if (_CurrentFrames[0] == toFind)
|
---|
524 | return 0;
|
---|
525 |
|
---|
526 | if (_CurrentFrames[1] == toFind)
|
---|
527 | return 1;
|
---|
528 |
|
---|
529 | return -1;
|
---|
530 | }
|
---|
531 |
|
---|
532 | /**
|
---|
533 | * Returns the Frame currently being displayed on the screen.
|
---|
534 | *
|
---|
535 | * @return The Frame currently displayed.
|
---|
536 | */
|
---|
537 | public static Frame getCurrentFrame() {
|
---|
538 | return _CurrentFrames[getCurrentSide()];
|
---|
539 | }
|
---|
540 |
|
---|
541 | public static Frame getOppositeFrame() {
|
---|
542 | return _CurrentFrames[getOppositeSide()];
|
---|
543 | }
|
---|
544 |
|
---|
545 | public static Frame[] getFrames() {
|
---|
546 | return _CurrentFrames;
|
---|
547 | }
|
---|
548 |
|
---|
549 | public static int getMiddle() {
|
---|
550 | return _Browser.getWidth() / 2;
|
---|
551 | }
|
---|
552 |
|
---|
553 | public static int getHeight() {
|
---|
554 | return _Browser.getHeight();
|
---|
555 | }
|
---|
556 |
|
---|
557 | /**
|
---|
558 | * Returns the current mouse X coordinate. This coordinate is relative to
|
---|
559 | * the left edge of the frame the mouse is in. It takes into account the
|
---|
560 | * user being in twin frames mode.
|
---|
561 | *
|
---|
562 | * @return The X coordinate of the mouse.
|
---|
563 | */
|
---|
564 | public static float getFloatMouseX() {
|
---|
565 | if (_TwinFrames
|
---|
566 | && FrameMouseActions.MouseY < FrameGraphics.getMaxSize().height)
|
---|
567 | return FrameMouseActions.MouseX % (_Browser.getWidth() / 2);
|
---|
568 |
|
---|
569 | return FrameMouseActions.MouseX;
|
---|
570 | }
|
---|
571 |
|
---|
572 | /**
|
---|
573 | * Returns the current mouse X coordinate. This coordinate is relative to
|
---|
574 | * the left edge of the frame the mouse is in. It takes into account the
|
---|
575 | * user being in twin frames mode.
|
---|
576 | *
|
---|
577 | * @return The X coordinate of the mouse.
|
---|
578 | */
|
---|
579 | public static int getMouseX() {
|
---|
580 | return Math.round(getFloatMouseX());
|
---|
581 | }
|
---|
582 |
|
---|
583 | public static boolean Back() {
|
---|
584 | int side = getCurrentSide();
|
---|
585 |
|
---|
586 | // there must be a frame to go back to
|
---|
587 | if (_VisitedFrames[side].size() < 1) {
|
---|
588 | MessageBay.displayMessageOnce("You are already on the home frame");
|
---|
589 | return false;
|
---|
590 | }
|
---|
591 |
|
---|
592 | if (!FrameUtils.LeavingFrame(getCurrentFrame())) {
|
---|
593 | MessageBay.displayMessage("Error navigating back");
|
---|
594 | return false;
|
---|
595 | }
|
---|
596 |
|
---|
597 | String oldFrame = getCurrentFrame().getName().toLowerCase();
|
---|
598 |
|
---|
599 | // do not get a cached version (in case it is in the other window)
|
---|
600 | if (isTwinFramesOn())
|
---|
601 | FrameIO.SuspendCache();
|
---|
602 | Frame frame = FrameIO.LoadFrame(removeFromBack());
|
---|
603 | // If the top frame on the backup stack is the current frame go back
|
---|
604 | // again... or if it has been deleted
|
---|
605 | //Recursively backup the stack
|
---|
606 | if(frame == null || frame.equals(getCurrentFrame())) {
|
---|
607 | Back();
|
---|
608 | return false;
|
---|
609 | }
|
---|
610 |
|
---|
611 | if (isTwinFramesOn()) {
|
---|
612 | FrameIO.ResumeCache();
|
---|
613 | }
|
---|
614 | _BackedUpFrames[side].push(oldFrame);
|
---|
615 | FrameUtils.DisplayFrame(frame, false);
|
---|
616 | FrameMouseActions.setHighlightHold(true);
|
---|
617 |
|
---|
618 | for (Item i : frame.getItems()) {
|
---|
619 | if (i.getLink() != null
|
---|
620 | && i.getAbsoluteLink().toLowerCase().equals(oldFrame)) {
|
---|
621 | if (i.getHighlightMode() != Item.HighlightMode.Normal) {
|
---|
622 | i.setHighlightMode(Item.HighlightMode.Normal,
|
---|
623 | BACK_HIGHLIGHT_COLOR);
|
---|
624 | }
|
---|
625 | // check if its an @f item and if so update the buffer
|
---|
626 | if (i instanceof Picture) {
|
---|
627 | Picture p = (Picture) i;
|
---|
628 | p.refresh();
|
---|
629 | }
|
---|
630 | }
|
---|
631 | }
|
---|
632 | FrameGraphics.requestRefresh(true);
|
---|
633 | return true;
|
---|
634 | }
|
---|
635 |
|
---|
636 | public static boolean Forward() {
|
---|
637 | int side = getCurrentSide();
|
---|
638 |
|
---|
639 | // there must be a frame to go back to
|
---|
640 | if (_BackedUpFrames[side].size() == 0) {
|
---|
641 | return false;
|
---|
642 | }
|
---|
643 |
|
---|
644 | if (!FrameUtils.LeavingFrame(getCurrentFrame())) {
|
---|
645 | MessageBay.displayMessage("Error navigating forward");
|
---|
646 | return false;
|
---|
647 | }
|
---|
648 |
|
---|
649 | String oldFrame = getCurrentFrame().getName().toLowerCase();
|
---|
650 |
|
---|
651 | // do not get a cached version (in case it is in the other window)
|
---|
652 | if (isTwinFramesOn())
|
---|
653 | FrameIO.SuspendCache();
|
---|
654 | Frame frame = FrameIO.LoadFrame(_BackedUpFrames[side].pop());
|
---|
655 | // If the top frame on the backup stack is the current frame go back
|
---|
656 | // again... or if it has been deleted
|
---|
657 | //Recursively backup the stack
|
---|
658 | if(frame == null || frame.equals(getCurrentFrame())) {
|
---|
659 | Forward();
|
---|
660 | return false;
|
---|
661 | }
|
---|
662 |
|
---|
663 | if (isTwinFramesOn()) {
|
---|
664 | FrameIO.ResumeCache();
|
---|
665 | }
|
---|
666 | _VisitedFrames[side].push(oldFrame);
|
---|
667 | FrameUtils.DisplayFrame(frame, false);
|
---|
668 | FrameGraphics.requestRefresh(true);
|
---|
669 | return true;
|
---|
670 | }
|
---|
671 |
|
---|
672 | /**
|
---|
673 | * Toggles the display of frames between TwinFrames mode and Single frame
|
---|
674 | * mode.
|
---|
675 | */
|
---|
676 | public static void ToggleTwinFrames() {
|
---|
677 | // determine which side is the active side
|
---|
678 | int opposite = getOppositeSide();
|
---|
679 | int current = getCurrentSide();
|
---|
680 | _TwinFrames = !_TwinFrames;
|
---|
681 |
|
---|
682 | // if TwinFrames is being turned on
|
---|
683 | if (_TwinFrames) {
|
---|
684 | // if this is the first time TwinFrames has been toggled on,
|
---|
685 | // load the user's first frame
|
---|
686 | if (_VisitedFrames[opposite].size() == 0) {
|
---|
687 | FrameIO.SuspendCache();
|
---|
688 | setCurrentFrame(FrameIO.LoadFrame(UserSettings.FirstFrame), true);
|
---|
689 | FrameIO.ResumeCache();
|
---|
690 | } else {
|
---|
691 | // otherwise, restore the frame from the side's back-stack
|
---|
692 | setCurrentFrame(FrameIO.LoadFrame(_VisitedFrames[opposite]
|
---|
693 | .pop()), true);
|
---|
694 | }
|
---|
695 |
|
---|
696 | // else, TwinFrames is being turned off
|
---|
697 | } else {
|
---|
698 | // add the frame to the back-stack
|
---|
699 | Frame hiding = _CurrentFrames[opposite];
|
---|
700 | FrameUtils.LeavingFrame(hiding);
|
---|
701 | _VisitedFrames[opposite].add(hiding.getName());
|
---|
702 | _CurrentFrames[opposite] = null;
|
---|
703 | _CurrentFrames[current].refreshSize(FrameGraphics.getMaxFrameSize());
|
---|
704 | }
|
---|
705 | if (_CurrentFrames[current] != null)
|
---|
706 | _CurrentFrames[current].refreshSize(FrameGraphics.getMaxFrameSize());
|
---|
707 | if (_CurrentFrames[opposite] != null)
|
---|
708 | _CurrentFrames[opposite]
|
---|
709 | .refreshSize(FrameGraphics.getMaxFrameSize());
|
---|
710 |
|
---|
711 | FrameGraphics.Clear();
|
---|
712 | FrameGraphics.requestRefresh(false);
|
---|
713 | FrameGraphics.Repaint();
|
---|
714 | }
|
---|
715 |
|
---|
716 | public static boolean isTwinFramesOn() {
|
---|
717 | return _TwinFrames;
|
---|
718 | }
|
---|
719 |
|
---|
720 | public static void Reload(int side) {
|
---|
721 | if (side < 0)
|
---|
722 | return;
|
---|
723 |
|
---|
724 | FrameIO.SuspendCache();
|
---|
725 | _CurrentFrames[side] = FrameIO
|
---|
726 | .LoadFrame(_CurrentFrames[side].getName());
|
---|
727 | FrameIO.ResumeCache();
|
---|
728 | }
|
---|
729 |
|
---|
730 | public static boolean DisplayConfirmDialog(String message, String title,
|
---|
731 | int type, int options, int res) {
|
---|
732 | return JOptionPane.showConfirmDialog(_Browser, message, title, options,
|
---|
733 | type) == res;
|
---|
734 | }
|
---|
735 |
|
---|
736 | public static final int RESULT_OK = JOptionPane.OK_OPTION;
|
---|
737 |
|
---|
738 | public static final int OPTIONS_OK_CANCEL = JOptionPane.OK_CANCEL_OPTION;
|
---|
739 |
|
---|
740 | public static final int TYPE_WARNING = JOptionPane.WARNING_MESSAGE;
|
---|
741 |
|
---|
742 | public static void pressMouse(int buttons) {
|
---|
743 | _Robot.mousePress(buttons);
|
---|
744 | }
|
---|
745 |
|
---|
746 | public static void releaseMouse(int buttons) {
|
---|
747 | _Robot.mouseRelease(buttons);
|
---|
748 | }
|
---|
749 |
|
---|
750 | public static void clickMouse(int buttons) {
|
---|
751 | _Robot.mousePress(buttons);
|
---|
752 | _Robot.mouseRelease(buttons);
|
---|
753 | }
|
---|
754 |
|
---|
755 | /**
|
---|
756 | * Moves the cursor the end of this item.
|
---|
757 | *
|
---|
758 | * @param i
|
---|
759 | */
|
---|
760 | public static void MoveCursorToEndOfItem(Item i) {
|
---|
761 | setTextCursor((Text) i, Text.END, true, false);
|
---|
762 | }
|
---|
763 |
|
---|
764 | public static void translateCursor(int deltaX, int deltaY) {
|
---|
765 | setCursorPosition(FrameMouseActions.MouseX + deltaX,
|
---|
766 | FrameMouseActions.MouseY + deltaY, false);
|
---|
767 | }
|
---|
768 |
|
---|
769 | public static void clearBackedUpFrames() {
|
---|
770 | _BackedUpFrames[getCurrentSide()].clear();
|
---|
771 | }
|
---|
772 | }
|
---|