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

Last change on this file since 919 was 919, checked in by jts21, 10 years ago

Added license headers to all files, added full GPL3 license file, moved license header generator script to dev/bin/scripts

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