source: trunk/src/org/expeditee/gui/FrameCreator.java@ 282

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