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