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

Last change on this file since 1258 was 1258, checked in by davidb, 5 years ago

Changed how DisplayController width, height and size are retrieved. Now does this top-level, rather than going through the AxisAlignmentBox. In doing so, can now control for when the window size has you yet been correctly mapped to the screen, and fall back to pre-defined MINIMUM defaults

File size: 8.4 KB
Line 
1/**
2 * FrameCreator.java
3 * Copyright (C) 2010 New Zealand Digital Library, http://expeditee.org
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19package org.expeditee.gui;
20
21import java.util.LinkedList;
22import java.util.List;
23
24import org.expeditee.agents.ExistingFramesetException;
25import org.expeditee.core.Colour;
26import org.expeditee.items.Item;
27import org.expeditee.items.Text;
28
29public class FrameCreator {
30
31 public static final int INDENT_FROM_TITLE = 20;
32
33 private int START_Y;
34
35 private int START_X;
36
37 private Frame _current = null;
38
39 private int _lastX;
40
41 private int _maxX = 0;
42
43 private int _lastY;
44
45 // Master copy of next & previous links
46 private Item _Mnext;
47
48 private Item _Mprev;
49
50 private Item _Mfirst;
51
52 // Next and previous links for the current frame
53 // private Item _next;
54
55 // private Item _prev;
56
57 private Frame _firstFrame;
58
59 private boolean _multiColumn;
60
61 private final List<Frame> framesCreated = new LinkedList<Frame>();
62
63 public FrameCreator(String frameTitle) {
64 this(DisplayController.getCurrentFrame().getFramesetName(), DisplayController
65 .getCurrentFrame().getPath(), frameTitle, false, false);
66 }
67
68 /**
69 * Creates a text item that looks like a clickable button.
70 *
71 * @param text
72 * the caption for the button
73 * @param x
74 * the x position for the button. Null if the button is anchored
75 * to the right of the screen.
76 * @param y
77 * the y position for the button. Null if the button is anchored
78 * to the bottom of the screen.
79 * @param right
80 * the distance the button is anchored from the right of this
81 * screen. Null if an absolute position is used.
82 * @param bottom
83 * the distance the button is to be anchored from the bottom of
84 * the screen. Null if the button is given an absolute position.
85 * @return the newly created button.
86 */
87 public static Item createButton(String text, Integer x, Integer y, Integer right,
88 Integer bottom) {
89 Text button = new Text(text);
90
91 button.setBackgroundColor(Colour.LIGHT_GREY);
92 button.setBorderColor(Colour.DARK_GREY);
93 button.setThickness(2.0F);
94 if (bottom != null)
95 button.setAnchorBottom(bottom);
96 if (x != null)
97 button.setX(x);
98 if (right != null)
99 button.setAnchorRight(right);
100 if (y != null)
101 button.setY(y);
102
103 button.invalidateBounds();
104
105 return button;
106 }
107
108 public FrameCreator(String name, String path, String frameTitle, boolean recreate, boolean multiColumn)
109 {
110 _multiColumn = multiColumn;
111 _Mnext = createButton("@Next", null, null, 10, 15);
112
113 _Mprev = createButton("@Previous", null, null, _Mnext.getBoundsWidth()
114 + _Mnext.getAnchorRight() + 20, 15);
115
116 _Mfirst = createButton("@First", null, null, _Mprev.getBoundsWidth()
117 + _Mprev.getAnchorRight() + 20, 15);
118
119 Frame toUse = null;
120 try {
121 toUse = FrameIO.CreateFrameset(name, path, recreate);
122 } catch (ExistingFramesetException efe) {
123 // Need a comment here to explain why this catch() clause is empty
124 // (or else it should be throwing an exception)
125 } catch (Exception e) {
126 e.printStackTrace();
127 }
128
129 if (toUse == null) {
130 toUse = FrameIO.CreateFrame(name, frameTitle, null);
131 this.framesCreated.add(toUse);
132 }
133
134 resetGlobals(toUse);
135 _firstFrame = toUse;
136
137 // set positions of next\prev frame links
138 // _Mnext.setPosition(FrameGraphics.getMaxSize().width - 100,
139 // FrameGraphics.getMaxSize().height - 15);
140 // _Mprev.setPosition(50, FrameGraphics.getMaxSize().height - 15);
141 }
142
143 public String getName() {
144 return _firstFrame.getName();
145 }
146
147 /**
148 * Creates the next frame in the frameset, with a previous button already
149 * added and linked to the last frame. _current then gets updated to point
150 * at the newly created Frame, and _lastY is reset
151 */
152 public boolean createNextFrame() {
153 try {
154 Frame newFrame = FrameIO.CreateFrame(_current.getFramesetName(),
155 _current.getTitle(), null);
156
157 this.framesCreated.add(newFrame);
158
159 // add link to previous frame
160 // _prev =
161 addPreviousButton(newFrame, _current.getName());
162
163 // add link to new frame
164 // _next =
165 addNextButton(_current, newFrame.getName());
166
167 // add link to new frame
168 addFirstButton(newFrame, _firstFrame.getName());
169
170 FrameIO.SaveFrame(_current, false);
171
172 resetGlobals(newFrame);
173 _maxX = 0;
174 return true;
175 } catch (Exception e) {
176 return false;
177 }
178 }
179
180 public List<Frame> getFramesCreated() {
181 return this.framesCreated;
182 }
183
184 private void resetGlobals(Frame toUse)
185 {
186 Text title = toUse.getTitleItem();
187 START_X = INDENT_FROM_TITLE + title.getX();
188 START_Y = getYStart(title);
189 _lastY = START_Y;
190 _lastX = START_X;
191 // Check for @Start
192 for (Item it : toUse.getItems()) {
193 if (it instanceof Text) {
194 Text t = (Text) it;
195 if (t.getText().toLowerCase().equals("@start")
196 || t.getText().toLowerCase().startsWith("@start:")) {
197 t.stripFirstWord();
198
199 if (t.getText().equals("")) {
200 _lastY = t.getY();
201 _lastX = t.getX();
202 t.delete();
203 break;
204 }
205 }
206 }
207 }
208 _current = toUse;
209 }
210
211 public boolean addItem(Item toAdd, boolean bSave) {
212 try {
213 // if we have reached the end of the Y axis, try moving over on the
214 // X axis
215 if (_lastY >= _Mprev.getY() - _Mprev.getBoundsHeight()) {
216 _lastX = _maxX + 10;
217 _lastY = START_Y;
218
219 // if there is no more room on the X axis, we have to start a
220 // new frame
221 if (!_multiColumn
222 || toAdd.getBoundsWidth() + _lastX > DisplayController.getFramePaintAreaWidth()) {
223 // Make sure text items that are created on the current
224 // frame are removed
225 _current.removeItem(toAdd);
226 createNextFrame();
227 }
228 }
229
230 toAdd.setPosition(_lastX, _lastY + toAdd.getBoundsHeight() / 2);
231 toAdd.setOffset(0, 0);
232 toAdd.setID(_current.getNextItemID());
233 toAdd.setRightMargin(DisplayController.getFramePaintAreaWidth(), true);
234
235 _current.addItem(toAdd);
236 // _current.addAllItems(items);
237 if (bSave)
238 save();
239
240 _lastY = toAdd.getY() + toAdd.getBoundsHeight() / 2;
241 _maxX = Math.max(toAdd.getX() + toAdd.getBoundsWidth(), _maxX);
242
243 return true;
244 } catch (Exception e) {
245 return false;
246 }
247 }
248
249 public Text addText(String toAdd, Colour c, String link, String action,
250 boolean bSave) {
251 Text text = _current.createNewText(toAdd);
252 if (c != null)
253 text.setColor(c);
254 text.setLink(link);
255 text.setAction(action);
256
257 addItem(text, bSave);
258
259 return text;
260 }
261
262 public void save() {
263 FrameIO.ForceSaveFrame(_current);
264 }
265
266 public int getLastY() {
267 return _lastY;
268 }
269
270 public void setLastY(int lastY) {
271 _lastY = lastY;
272 }
273
274 public Frame getCurrentFrame() {
275 return _current;
276 }
277
278 /**
279 * Returns the Frame name of the current frame for the FrameCreator
280 *
281 * @return The current frame for the FrameCreator
282 */
283 public String getCurrent() {
284 if (_current == null)
285 return null;
286
287 return _current.getName();
288 }
289
290 public Frame getFirstFrame() {
291 return _firstFrame;
292 }
293
294 public Item addNextButton(Frame current, String link) {
295 return addButton(_Mnext, current, link);
296 }
297
298 public Item addPreviousButton(Frame current, String link) {
299 return addButton(_Mprev, current, link);
300 }
301
302 public Item addFirstButton(Frame current, String link) {
303 return addButton(_Mfirst, current, link);
304 }
305
306 public Item addButton(Item template, Frame current, String link) {
307 // add link to new frame
308 Item previousButton = template.copy();
309 previousButton.setID(current.getNextItemID());
310 previousButton.setLink(link);
311 previousButton.setLinkHistory(false);
312 previousButton.setLinkMark(false);
313 // previousButton.setPermission(new PermissionPair(UserAppliedPermission.followLinks));
314 current.addItem(previousButton);
315
316 return previousButton;
317 }
318
319 public void addSpace(int space) {
320 _lastY += space;
321 }
322
323 public static int getYStart(Item title) {
324 return title.getY() + title.getBoundsHeight();
325 }
326
327 public void setTitle(String titleText) {
328 _current.setTitle(titleText);
329 }
330}
Note: See TracBrowser for help on using the repository browser.