1 | package org.expeditee.gui;
|
---|
2 |
|
---|
3 | import java.awt.Color;
|
---|
4 |
|
---|
5 | import org.expeditee.agents.ExistingFramesetException;
|
---|
6 | import org.expeditee.items.Item;
|
---|
7 | import org.expeditee.items.Text;
|
---|
8 |
|
---|
9 | public class FrameCreator {
|
---|
10 | public static final int INDENT_FROM_TITLE = 20;
|
---|
11 |
|
---|
12 | private int START_Y;
|
---|
13 |
|
---|
14 | private int START_X;
|
---|
15 |
|
---|
16 | private Frame _current = null;
|
---|
17 |
|
---|
18 | private int _lastX;
|
---|
19 |
|
---|
20 | private int _maxX = 0;
|
---|
21 |
|
---|
22 | private int _lastY;
|
---|
23 |
|
---|
24 | // Master copy of next & previous links
|
---|
25 | private Item _Mnext;
|
---|
26 |
|
---|
27 | private Item _Mprev;
|
---|
28 |
|
---|
29 | private Item _Mfirst;
|
---|
30 |
|
---|
31 | // Next and previous links for the current frame
|
---|
32 | // private Item _next;
|
---|
33 |
|
---|
34 | // private Item _prev;
|
---|
35 |
|
---|
36 | private Frame _firstFrame;
|
---|
37 |
|
---|
38 | private boolean _multiColumn;
|
---|
39 |
|
---|
40 | public FrameCreator(String frameTitle) {
|
---|
41 | this(DisplayIO.getCurrentFrame().getFramesetName(), DisplayIO
|
---|
42 | .getCurrentFrame().getPath(), frameTitle, false, false);
|
---|
43 | }
|
---|
44 |
|
---|
45 | /**
|
---|
46 | * Creates a text item that looks like a clickable button.
|
---|
47 | *
|
---|
48 | * @param text
|
---|
49 | * the caption for the button
|
---|
50 | * @param x
|
---|
51 | * the x position for the button. Null if the button is anchored
|
---|
52 | * to the right of the screen.
|
---|
53 | * @param y
|
---|
54 | * the y position for the button. Null if the button is anchored
|
---|
55 | * to the bottom of the scree.
|
---|
56 | * @param right
|
---|
57 | * the distance the button is anchored from the right of this
|
---|
58 | * screen. Null if an absolute position is used.
|
---|
59 | * @param bottom
|
---|
60 | * the distance the button is to be anchored from the bottom of
|
---|
61 | * the screen. Null if the button is given an absolute position.
|
---|
62 | * @return the newly created button.
|
---|
63 | */
|
---|
64 | public static Item createButton(String text, Float x, Float y, Float right,
|
---|
65 | Float bottom) {
|
---|
66 | Text button = new Text(text);
|
---|
67 |
|
---|
68 | button.setBackgroundColor(Color.LIGHT_GRAY);
|
---|
69 | button.setBorderColor(Color.DARK_GRAY);
|
---|
70 | button.setThickness(2.0F);
|
---|
71 | if (bottom != null)
|
---|
72 | button.setAnchorBottom(bottom);
|
---|
73 | if (x != null)
|
---|
74 | button.setX(x);
|
---|
75 | if (right != null)
|
---|
76 | button.setAnchorRight(right);
|
---|
77 | if (y != null)
|
---|
78 | button.setY(y);
|
---|
79 |
|
---|
80 | button.updatePolygon();
|
---|
81 |
|
---|
82 | return button;
|
---|
83 | }
|
---|
84 |
|
---|
85 | public FrameCreator(String name, String path, String frameTitle,
|
---|
86 | boolean recreate, boolean multiColumn) {
|
---|
87 | _multiColumn = multiColumn;
|
---|
88 | _Mnext = createButton("@Next", null, null, 10F, 15F);
|
---|
89 |
|
---|
90 | _Mprev = createButton("@Previous", null, null, _Mnext.getBoundsWidth()
|
---|
91 | + _Mnext.getAnchorRight() + 20F, 15F);
|
---|
92 |
|
---|
93 | _Mfirst = createButton("@First", null, null, _Mprev.getBoundsWidth()
|
---|
94 | + _Mprev.getAnchorRight() + 20F, 15F);
|
---|
95 |
|
---|
96 | Frame toUse = null;
|
---|
97 | try {
|
---|
98 | toUse = FrameIO.CreateFrameset(name, path, recreate);
|
---|
99 | } catch (ExistingFramesetException efe) {
|
---|
100 | } catch (Exception e) {
|
---|
101 | e.printStackTrace();
|
---|
102 | }
|
---|
103 |
|
---|
104 | if (toUse == null) {
|
---|
105 | toUse = FrameIO.CreateFrame(name, frameTitle, null);
|
---|
106 | }
|
---|
107 |
|
---|
108 | resetGlobals(toUse);
|
---|
109 | _firstFrame = toUse;
|
---|
110 |
|
---|
111 | // set positions of next\prev frame links
|
---|
112 | // _Mnext.setPosition(FrameGraphics.getMaxSize().width - 100,
|
---|
113 | // FrameGraphics.getMaxSize().height - 15);
|
---|
114 | // _Mprev.setPosition(50, FrameGraphics.getMaxSize().height - 15);
|
---|
115 | }
|
---|
116 |
|
---|
117 | public String getName() {
|
---|
118 | return _firstFrame.getName();
|
---|
119 | }
|
---|
120 |
|
---|
121 | /**
|
---|
122 | * Creates the next frame in the frameset, with a previous button already
|
---|
123 | * added and linked to the last frame. _current then gets updated to point
|
---|
124 | * at the newly created Frame, and _lastY is reset
|
---|
125 | */
|
---|
126 | public boolean createNextFrame() {
|
---|
127 | try {
|
---|
128 | Frame newFrame = FrameIO.CreateFrame(_current.getFramesetName(),
|
---|
129 | _current.getTitle(), null);
|
---|
130 |
|
---|
131 | // add link to previous frame
|
---|
132 | // _prev =
|
---|
133 | addPreviousButton(newFrame, _current.getName());
|
---|
134 |
|
---|
135 | // add link to new frame
|
---|
136 | // _next =
|
---|
137 | addNextButton(_current, newFrame.getName());
|
---|
138 |
|
---|
139 | // add link to new frame
|
---|
140 | addFirstButton(newFrame, _firstFrame.getName());
|
---|
141 |
|
---|
142 | FrameIO.SaveFrame(_current, false);
|
---|
143 |
|
---|
144 | resetGlobals(newFrame);
|
---|
145 | _maxX = 0;
|
---|
146 | return true;
|
---|
147 | } catch (Exception e) {
|
---|
148 | return false;
|
---|
149 | }
|
---|
150 | }
|
---|
151 |
|
---|
152 | private void resetGlobals(Frame toUse) {
|
---|
153 | Text title = toUse.getTitleItem();
|
---|
154 | START_X = INDENT_FROM_TITLE + title.getX();
|
---|
155 | START_Y = getYStart(title);
|
---|
156 | _lastY = START_Y;
|
---|
157 | _lastX = START_X;
|
---|
158 | // Check for @Start
|
---|
159 | for (Item it : toUse.getItems()) {
|
---|
160 | if (it instanceof Text) {
|
---|
161 | Text t = (Text) it;
|
---|
162 | if (t.getText().toLowerCase().equals("@start")
|
---|
163 | || t.getText().toLowerCase().startsWith("@start:")) {
|
---|
164 | t.stripFirstWord();
|
---|
165 |
|
---|
166 | if (t.getText().equals("")) {
|
---|
167 | _lastY = t.getY();
|
---|
168 | _lastX = t.getX();
|
---|
169 | t.delete();
|
---|
170 | break;
|
---|
171 | }
|
---|
172 | }
|
---|
173 | }
|
---|
174 | }
|
---|
175 | _current = toUse;
|
---|
176 | }
|
---|
177 |
|
---|
178 | public boolean addItem(Item toAdd, boolean bSave) {
|
---|
179 | try {
|
---|
180 | // if we have reached the end of the Y axis, try moving over on the
|
---|
181 | // X axis
|
---|
182 | if (_lastY >= _Mprev.getY() - _Mprev.getBoundsHeight()) {
|
---|
183 | _lastX = _maxX + 10;
|
---|
184 | _lastY = START_Y;
|
---|
185 |
|
---|
186 | // if there is no more room on the X axis, we have to start a
|
---|
187 | // new frame
|
---|
188 | if (!_multiColumn
|
---|
189 | || toAdd.getBoundsWidth() + _lastX > FrameGraphics
|
---|
190 | .getMaxSize().width) {
|
---|
191 | // Make sure text items that are created on the current
|
---|
192 | // frame are removed
|
---|
193 | _current.removeItem(toAdd);
|
---|
194 | createNextFrame();
|
---|
195 | }
|
---|
196 | }
|
---|
197 |
|
---|
198 | toAdd.setPosition(_lastX, _lastY);
|
---|
199 | toAdd.setOffset(0, 0);
|
---|
200 | toAdd.setID(_current.getNextItemID());
|
---|
201 | toAdd.setRightMargin(FrameGraphics.getMaxFrameSize().width, true);
|
---|
202 |
|
---|
203 | _current.addItem(toAdd);
|
---|
204 | // _current.addAllItems(items);
|
---|
205 | if (bSave)
|
---|
206 | save();
|
---|
207 |
|
---|
208 | _lastY = toAdd.getY() + toAdd.getBoundsHeight();
|
---|
209 | _maxX = Math.max(toAdd.getX() + toAdd.getBoundsWidth(), _maxX);
|
---|
210 |
|
---|
211 | return true;
|
---|
212 | } catch (Exception e) {
|
---|
213 | return false;
|
---|
214 | }
|
---|
215 | }
|
---|
216 |
|
---|
217 | public Text addText(String toAdd, Color c, String link, String action,
|
---|
218 | boolean bSave) {
|
---|
219 | Text text = _current.createNewText(toAdd);
|
---|
220 | if (c != null)
|
---|
221 | text.setColor(c);
|
---|
222 | text.setLink(link);
|
---|
223 | text.setAction(action);
|
---|
224 |
|
---|
225 | addItem(text, bSave);
|
---|
226 |
|
---|
227 | return text;
|
---|
228 | }
|
---|
229 |
|
---|
230 | public void save() {
|
---|
231 | FrameIO.ForceSaveFrame(_current);
|
---|
232 | }
|
---|
233 |
|
---|
234 | /**
|
---|
235 | * Returns the Frame name of the current frame for the FrameCreator
|
---|
236 | *
|
---|
237 | * @return The current frame for the FrameCreator
|
---|
238 | */
|
---|
239 | public String getCurrent() {
|
---|
240 | if (_current == null)
|
---|
241 | return null;
|
---|
242 |
|
---|
243 | return _current.getName();
|
---|
244 | }
|
---|
245 |
|
---|
246 | public Frame getFirstFrame() {
|
---|
247 | return _firstFrame;
|
---|
248 | }
|
---|
249 |
|
---|
250 | public Item addNextButton(Frame current, String link) {
|
---|
251 | return addButton(_Mnext, current, link);
|
---|
252 | }
|
---|
253 |
|
---|
254 | public Item addPreviousButton(Frame current, String link) {
|
---|
255 | return addButton(_Mprev, current, link);
|
---|
256 | }
|
---|
257 |
|
---|
258 | public Item addFirstButton(Frame current, String link) {
|
---|
259 | return addButton(_Mfirst, current, link);
|
---|
260 | }
|
---|
261 |
|
---|
262 | public Item addButton(Item template, Frame current, String link) {
|
---|
263 | // add link to new frame
|
---|
264 | Item previousButton = template.copy();
|
---|
265 | previousButton.setID(current.getNextItemID());
|
---|
266 | previousButton.setLink(link);
|
---|
267 | current.addItem(previousButton);
|
---|
268 |
|
---|
269 | return previousButton;
|
---|
270 | }
|
---|
271 |
|
---|
272 | public void addSpace(int space) {
|
---|
273 | _lastY += space;
|
---|
274 | }
|
---|
275 |
|
---|
276 | public static int getYStart(Item title) {
|
---|
277 | return title.getY() + title.getBoundsHeight();
|
---|
278 | }
|
---|
279 |
|
---|
280 | public void setTitle(String titleText) {
|
---|
281 | _current.setTitle(titleText);
|
---|
282 | }
|
---|
283 | }
|
---|