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

Last change on this file since 940 was 940, checked in by bln4, 9 years ago

Continued work on update to tooltips to be actual items so that data can be stored.

File size: 8.2 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 scree.
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 } catch (Exception e) {
122 e.printStackTrace();
123 }
124
125 if (toUse == null) {
126 toUse = FrameIO.CreateFrame(name, frameTitle, null);
127 this.framesCreated.add(toUse);
128 }
129
130 resetGlobals(toUse);
131 _firstFrame = toUse;
132
133 // set positions of next\prev frame links
134 // _Mnext.setPosition(FrameGraphics.getMaxSize().width - 100,
135 // FrameGraphics.getMaxSize().height - 15);
136 // _Mprev.setPosition(50, FrameGraphics.getMaxSize().height - 15);
137 }
138
139 public String getName() {
140 return _firstFrame.getName();
141 }
142
143 /**
144 * Creates the next frame in the frameset, with a previous button already
145 * added and linked to the last frame. _current then gets updated to point
146 * at the newly created Frame, and _lastY is reset
147 */
148 public boolean createNextFrame() {
149 try {
150 Frame newFrame = FrameIO.CreateFrame(_current.getFramesetName(),
151 _current.getTitle(), null);
152
153 this.framesCreated.add(newFrame);
154
155 // add link to previous frame
156 // _prev =
157 addPreviousButton(newFrame, _current.getName());
158
159 // add link to new frame
160 // _next =
161 addNextButton(_current, newFrame.getName());
162
163 // add link to new frame
164 addFirstButton(newFrame, _firstFrame.getName());
165
166 FrameIO.SaveFrame(_current, false);
167
168 resetGlobals(newFrame);
169 _maxX = 0;
170 return true;
171 } catch (Exception e) {
172 return false;
173 }
174 }
175
176 public List<Frame> getFramesCreated() {
177 return this.framesCreated;
178 }
179
180 private void resetGlobals(Frame toUse) {
181 Text title = toUse.getTitleItem();
182 START_X = INDENT_FROM_TITLE + title.getX();
183 START_Y = getYStart(title);
184 _lastY = START_Y;
185 _lastX = START_X;
186 // Check for @Start
187 for (Item it : toUse.getItems()) {
188 if (it instanceof Text) {
189 Text t = (Text) it;
190 if (t.getText().toLowerCase().equals("@start")
191 || t.getText().toLowerCase().startsWith("@start:")) {
192 t.stripFirstWord();
193
194 if (t.getText().equals("")) {
195 _lastY = t.getY();
196 _lastX = t.getX();
197 t.delete();
198 break;
199 }
200 }
201 }
202 }
203 _current = toUse;
204 }
205
206 public boolean addItem(Item toAdd, boolean bSave) {
207 try {
208 // if we have reached the end of the Y axis, try moving over on the
209 // X axis
210 if (_lastY >= _Mprev.getY() - _Mprev.getBoundsHeight()) {
211 _lastX = _maxX + 10;
212 _lastY = START_Y;
213
214 // if there is no more room on the X axis, we have to start a
215 // new frame
216 if (!_multiColumn
217 || toAdd.getBoundsWidth() + _lastX > FrameGraphics
218 .getMaxSize().width) {
219 // Make sure text items that are created on the current
220 // frame are removed
221 _current.removeItem(toAdd);
222 createNextFrame();
223 }
224 }
225
226 toAdd.setPosition(_lastX, _lastY + toAdd.getBoundsHeight() / 2);
227 toAdd.setOffset(0, 0);
228 toAdd.setID(_current.getNextItemID());
229 toAdd.setRightMargin(FrameGraphics.getMaxFrameSize().width, true);
230
231 _current.addItem(toAdd);
232 // _current.addAllItems(items);
233 if (bSave)
234 save();
235
236 _lastY = toAdd.getY() + toAdd.getBoundsHeight() / 2;
237 _maxX = Math.max(toAdd.getX() + toAdd.getBoundsWidth(), _maxX);
238
239 return true;
240 } catch (Exception e) {
241 return false;
242 }
243 }
244
245 public Text addText(String toAdd, Color c, String link, String action,
246 boolean bSave) {
247 Text text = _current.createNewText(toAdd);
248 if (c != null)
249 text.setColor(c);
250 text.setLink(link);
251 text.setAction(action);
252
253 addItem(text, bSave);
254
255 return text;
256 }
257
258 public void save() {
259 FrameIO.ForceSaveFrame(_current);
260 }
261
262 public int getLastY() {
263 return _lastY;
264 }
265
266 public void setLastY(int lastY) {
267 _lastY = lastY;
268 }
269
270 public Frame getCurrentFrame() {
271 return _current;
272 }
273
274 /**
275 * Returns the Frame name of the current frame for the FrameCreator
276 *
277 * @return The current frame for the FrameCreator
278 */
279 public String getCurrent() {
280 if (_current == null)
281 return null;
282
283 return _current.getName();
284 }
285
286 public Frame getFirstFrame() {
287 return _firstFrame;
288 }
289
290 public Item addNextButton(Frame current, String link) {
291 return addButton(_Mnext, current, link);
292 }
293
294 public Item addPreviousButton(Frame current, String link) {
295 return addButton(_Mprev, current, link);
296 }
297
298 public Item addFirstButton(Frame current, String link) {
299 return addButton(_Mfirst, current, link);
300 }
301
302 public Item addButton(Item template, Frame current, String link) {
303 // add link to new frame
304 Item previousButton = template.copy();
305 previousButton.setID(current.getNextItemID());
306 previousButton.setLink(link);
307 previousButton.setLinkHistory(false);
308 previousButton.setLinkMark(false);
309 // previousButton.setPermission(new PermissionPair(UserAppliedPermission.followLinks));
310 current.addItem(previousButton);
311
312 return previousButton;
313 }
314
315 public void addSpace(int space) {
316 _lastY += space;
317 }
318
319 public static int getYStart(Item title) {
320 return title.getY() + title.getBoundsHeight();
321 }
322
323 public void setTitle(String titleText) {
324 _current.setTitle(titleText);
325 }
326}
Note: See TracBrowser for help on using the repository browser.