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