1 | package org.expeditee.gui;
|
---|
2 |
|
---|
3 | import java.awt.Color;
|
---|
4 | import java.util.List;
|
---|
5 |
|
---|
6 | import org.expeditee.items.Item;
|
---|
7 | import org.expeditee.items.Text;
|
---|
8 |
|
---|
9 | public class FrameCreator {
|
---|
10 |
|
---|
11 | private String _name;
|
---|
12 |
|
---|
13 | private final static int START_Y = 40;
|
---|
14 |
|
---|
15 | private final static int START_X = 20;
|
---|
16 |
|
---|
17 | private Frame _current = null;
|
---|
18 |
|
---|
19 | private int _lastX = START_X;
|
---|
20 |
|
---|
21 | private int _maxX = 0;
|
---|
22 |
|
---|
23 | private int _lastY = START_Y;
|
---|
24 |
|
---|
25 | private Text _Mnext = new Text(-1, "@Next");
|
---|
26 |
|
---|
27 | private Text _Mprev = new Text(-1, "@Previous");
|
---|
28 |
|
---|
29 | private Item _next;
|
---|
30 |
|
---|
31 | private Item _prev;
|
---|
32 |
|
---|
33 | public FrameCreator(String name, boolean recreate) {
|
---|
34 | Frame toUse = null;
|
---|
35 | try {
|
---|
36 | toUse = FrameIO
|
---|
37 | .CreateFrameset(name, FrameIO.PROFILE_PATH, recreate);
|
---|
38 | } catch (Exception e) {
|
---|
39 | if (toUse == null)
|
---|
40 | toUse = FrameIO.CreateFrame(name, name, null);
|
---|
41 | }
|
---|
42 | _name = name;
|
---|
43 | resetGlobals(toUse);
|
---|
44 |
|
---|
45 | // set positions of next\prev frame links
|
---|
46 | _Mnext.setPosition(FrameGraphics.getMaxSize().width - 100,
|
---|
47 | FrameGraphics.getMaxSize().height - 15);
|
---|
48 | _Mprev.setPosition(50, FrameGraphics.getMaxSize().height - 15);
|
---|
49 | }
|
---|
50 |
|
---|
51 | /**
|
---|
52 | * Creates the next frame in the frameset, with a previous button already
|
---|
53 | * added and linked to the last frame. _current then gets updated to point
|
---|
54 | * at the newly created Frame, and _lastY is reset
|
---|
55 | */
|
---|
56 | public boolean createNextFrame() {
|
---|
57 | try {
|
---|
58 | Frame newFrame = FrameIO.CreateFrame(_current.getFramesetName(),
|
---|
59 | _name, null);
|
---|
60 |
|
---|
61 | // add link to previous frame
|
---|
62 | _prev = _Mprev.copy();
|
---|
63 | _prev.setID(newFrame.getNextItemID());
|
---|
64 | _prev.setLink(_current.getFrameName());
|
---|
65 | newFrame.addItem(_prev);
|
---|
66 |
|
---|
67 | // add link to new frame
|
---|
68 | _next = _Mnext.copy();
|
---|
69 | _next.setID(_current.getNextItemID());
|
---|
70 | _next.setLink(newFrame.getFrameName());
|
---|
71 | _current.addItem(_next);
|
---|
72 | FrameIO.SaveFrame(_current, false);
|
---|
73 |
|
---|
74 | resetGlobals(newFrame);
|
---|
75 | _maxX = 0;
|
---|
76 | return true;
|
---|
77 | } catch (Exception e) {
|
---|
78 | return false;
|
---|
79 | }
|
---|
80 | }
|
---|
81 |
|
---|
82 | private void resetGlobals(Frame toUse) {
|
---|
83 | _lastY = START_Y + toUse.getTitle().getPosition().y;
|
---|
84 | _lastX = START_X;
|
---|
85 | //Check for @Start
|
---|
86 | for (Item it : toUse.getItems()) {
|
---|
87 | if (it instanceof Text) {
|
---|
88 | Text t = (Text) it;
|
---|
89 | if (t.getTextNoList().toLowerCase().equals("@start") ||
|
---|
90 | t.getTextNoList().toLowerCase().startsWith("@start:")) {
|
---|
91 | t.stripFirstWord();
|
---|
92 |
|
---|
93 | if (t.getTextNoList().equals("")){
|
---|
94 | _lastY = t.getY();
|
---|
95 | _lastX = t.getX();
|
---|
96 | t.delete();
|
---|
97 | break;
|
---|
98 | }
|
---|
99 | }
|
---|
100 | }
|
---|
101 | }
|
---|
102 | _current = toUse;
|
---|
103 | }
|
---|
104 |
|
---|
105 | public boolean addItem(Item toAdd) {
|
---|
106 | try {
|
---|
107 | // if we have reached the end of the Y axis, try moving over on the
|
---|
108 | // X
|
---|
109 | // axis
|
---|
110 | if (_lastY >= _Mprev.getY() - _Mprev.getBoundsHeight()) {
|
---|
111 | _lastX = _maxX + 10;
|
---|
112 | _lastY = START_Y;
|
---|
113 | }
|
---|
114 |
|
---|
115 | // if there is no more room on the X axis, we have to start a new
|
---|
116 | // frame
|
---|
117 | if (toAdd.getBoundsWidth() + _lastX > FrameGraphics.getMaxSize().width) {
|
---|
118 | createNextFrame();
|
---|
119 | }
|
---|
120 |
|
---|
121 | toAdd.setPosition(_lastX, _lastY);
|
---|
122 | toAdd.setOffset(0, 0);
|
---|
123 | toAdd.setID(_current.getNextItemID());
|
---|
124 |
|
---|
125 | _current.addItem(toAdd);
|
---|
126 | // _current.addAllItems(items);
|
---|
127 |
|
---|
128 | FrameIO.SaveFrame(_current, false);
|
---|
129 |
|
---|
130 | _lastY = toAdd.getY() + toAdd.getBoundsHeight();
|
---|
131 | _maxX = Math.max(toAdd.getX() + toAdd.getBoundsWidth(), _maxX);
|
---|
132 | return true;
|
---|
133 | } catch (Exception e) {
|
---|
134 | return false;
|
---|
135 | }
|
---|
136 | }
|
---|
137 |
|
---|
138 | public void addText(String toAdd) {
|
---|
139 | addText(toAdd, null);
|
---|
140 | }
|
---|
141 |
|
---|
142 | public void addText(String toAdd, Color c) {
|
---|
143 | Text text = _current.createNewText();
|
---|
144 | text.setText(toAdd);
|
---|
145 | text.setColor(c);
|
---|
146 |
|
---|
147 | addItem(text);
|
---|
148 | }
|
---|
149 |
|
---|
150 | /**
|
---|
151 | * Returns the Frame name of the current (latest) frame in the message
|
---|
152 | * frameset.
|
---|
153 | *
|
---|
154 | * @return The Frame currently being used in the message frameset
|
---|
155 | */
|
---|
156 | public String getCurrent() {
|
---|
157 | if (_current == null)
|
---|
158 | return null;
|
---|
159 |
|
---|
160 | return _current.getFrameName();
|
---|
161 | }
|
---|
162 | }
|
---|