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

Last change on this file since 1063 was 1063, checked in by davidb, 8 years ago

Comment tidy up

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