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

Last change on this file since 1280 was 1280, checked in by bln4, 5 years ago

Renamed MailMode action to ToggleBay
Renamed FrameCreator enums to more desirable names (David request)
Created test for altered functionality of FrameCreator as documented below.

FrameCreator now more cleanly obeys the specification of the enum parameter to its constructor. For example, override existing frameset ensures that the old frameset has been deleted (moved to trash).

File size: 10.6 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.io.File;
22import java.io.IOException;
23import java.nio.file.Files;
24import java.nio.file.Path;
25import java.nio.file.Paths;
26import java.util.LinkedList;
27import java.util.List;
28
29import org.expeditee.agents.ExistingFramesetException;
30import org.expeditee.agents.InvalidFramesetNameException;
31import org.expeditee.core.Colour;
32import org.expeditee.items.Item;
33import org.expeditee.items.Text;
34
35public class FrameCreator {
36
37 public static final int INDENT_FROM_TITLE = 20;
38
39 private int START_Y;
40
41 private int START_X;
42
43 private Frame _current = null;
44
45 private int _lastX;
46
47 private int _maxX = 0;
48
49 private int _lastY;
50
51 // Master copy of next & previous links
52 private Item _Mnext;
53
54 private Item _Mprev;
55
56 private Item _Mfirst;
57
58 private Frame _firstFrame;
59
60 private boolean _multiColumn;
61
62 public enum ExistingFramesetOptions {
63 /**
64 * Do not attempt to use any existing frames in specified framesets.
65 */
66 AppendSegregatedFrames,
67 /**
68 * Override any existing frames in specified framesets.
69 */
70 OverrideExistingFrames,
71 /**
72 * Use the existing frames in the framesets to reobtain what the state of the
73 * FrameCreator should be.
74 */
75 AppendAfterLastItem
76 }
77
78 private final List<Frame> framesCreated = new LinkedList<Frame>();
79
80 public FrameCreator(String frameTitle) {
81 this(DisplayController.getCurrentFrame().getFramesetName(), DisplayController.getCurrentFrame().getPath(),
82 frameTitle, ExistingFramesetOptions.AppendSegregatedFrames, false);
83 }
84
85 public FrameCreator(String framesetName, String path, String frameTitle, ExistingFramesetOptions establishState, boolean multiColumn) {
86 switch (establishState) {
87 case OverrideExistingFrames:
88 try {
89 FrameIO.SuspendCache();
90 Path framesetDir = Files.createTempDirectory("temporaryExpediteeFrameset");
91 initialise(framesetName, framesetDir.toAbsolutePath(), frameTitle, multiColumn);
92 FrameIO.EnableCache();
93 FrameIO.deleteFrameset(framesetName);
94 FrameIO.moveFrameset(framesetName, framesetDir.toAbsolutePath().toString() + File.separator, path);
95 _current.setPath(path);
96 } catch (IOException e) {
97 e.printStackTrace();
98 }
99 break;
100 case AppendSegregatedFrames:
101 initialise(framesetName, Paths.get(path), frameTitle, multiColumn);
102 break;
103 case AppendAfterLastItem:
104 initialiseReobtainState(framesetName, Paths.get(path), frameTitle, multiColumn);
105 break;
106 }
107 }
108
109 private void initialiseReobtainState(String framesetName, Path path, String frameTitle, boolean multiColumn) {
110 _multiColumn = multiColumn;
111 _Mnext = createButton("@Next", null, null, 10, 15);
112 _Mprev = createButton("@Previous", null, null, _Mnext.getBoundsWidth() + _Mnext.getAnchorRight() + 20, 15);
113 _Mfirst = createButton("@First", null, null, _Mprev.getBoundsWidth() + _Mprev.getAnchorRight() + 20, 15);
114
115 int lastNumber = FrameIO.getLastNumber(framesetName);
116 for (int i = 1; i <= lastNumber; i++) {
117 Frame frame = FrameIO.LoadFrame(framesetName + i, path.toAbsolutePath().toString());
118 this.framesCreated.add(frame);
119 }
120 if (this.framesCreated.isEmpty()) {
121 initialise(framesetName, path, frameTitle, multiColumn);
122 return;
123 }
124 _firstFrame = this.framesCreated.get(0);
125 _current = this.framesCreated.get(this.framesCreated.size() - 1);
126
127 createNextFrame();
128
129 Paths.get(_current.getFramePathLogical());
130 }
131
132 private void initialise(String framesetName, Path path, String frameTitle, boolean multiColumn) {
133 _multiColumn = multiColumn;
134 _Mnext = createButton("@Next", null, null, 10, 15);
135 _Mprev = createButton("@Previous", null, null, _Mnext.getBoundsWidth() + _Mnext.getAnchorRight() + 20, 15);
136 _Mfirst = createButton("@First", null, null, _Mprev.getBoundsWidth() + _Mprev.getAnchorRight() + 20, 15);
137
138 try {
139 Frame toUse;
140 if (FrameIO.canAccessFrameset(framesetName, path)) {
141 toUse = FrameIO.CreateFrame(framesetName, frameTitle, null);
142 } else {
143 toUse = FrameIO.CreateFrameset(framesetName, path.toAbsolutePath().toString() + File.separator, true);
144 }
145
146 this.framesCreated.add(toUse);
147 resetGlobals(toUse);
148 _firstFrame = toUse;
149
150 path.toAbsolutePath().resolve(framesetName);
151 } catch (InvalidFramesetNameException e) {
152 e.printStackTrace();
153 } catch (ExistingFramesetException e) {
154 // This should never happen as we are allowing for an override of existing frameset.
155 }
156 }
157
158 /**
159 * Creates a text item that looks like a clickable button.
160 *
161 * @param text
162 * the caption for the button
163 * @param x
164 * the x position for the button. Null if the button is anchored to
165 * the right of the screen.
166 * @param y
167 * the y position for the button. Null if the button is anchored to
168 * the bottom of the screen.
169 * @param right
170 * the distance the button is anchored from the right of this screen.
171 * Null if an absolute position is used.
172 * @param bottom
173 * the distance the button is to be anchored from the bottom of the
174 * screen. Null if the button is given an absolute position.
175 * @return the newly created button.
176 */
177 public static Item createButton(String text, Integer x, Integer y, Integer right, Integer bottom) {
178 Text button = new Text(text);
179
180 button.setBackgroundColor(Colour.LIGHT_GREY);
181 button.setBorderColor(Colour.DARK_GREY);
182 button.setThickness(2.0F);
183 if (bottom != null)
184 button.setAnchorBottom(bottom);
185 if (x != null)
186 button.setX(x);
187 if (right != null)
188 button.setAnchorRight(right);
189 if (y != null)
190 button.setY(y);
191
192 button.invalidateBounds();
193
194 return button;
195 }
196
197 public String getName() {
198 return _firstFrame.getName();
199 }
200
201 /**
202 * Creates the next frame in the frameset, with a previous button already added
203 * and linked to the last frame. _current then gets updated to point at the
204 * newly created Frame, and _lastY is reset
205 */
206 public boolean createNextFrame() {
207 try {
208 Frame newFrame = FrameIO.CreateFrame(_current.getFramesetName(), _current.getTitle(), null);
209
210 this.framesCreated.add(newFrame);
211
212 // add link to previous frame
213 // _prev =
214 addPreviousButton(newFrame, _current.getName());
215
216 // add link to new frame
217 // _next =
218 addNextButton(_current, newFrame.getName());
219
220 // add link to new frame
221 addFirstButton(newFrame, _firstFrame.getName());
222
223 FrameIO.SaveFrame(_current, false);
224
225 resetGlobals(newFrame);
226 _maxX = 0;
227 return true;
228 } catch (Exception e) {
229 return false;
230 }
231 }
232
233 public List<Frame> getFramesCreated() {
234 return this.framesCreated;
235 }
236
237 private void resetGlobals(Frame toUse) {
238 Text title = toUse.getTitleItem();
239 START_X = INDENT_FROM_TITLE + title.getX();
240 START_Y = getYStart(title);
241 _lastY = START_Y;
242 _lastX = START_X;
243 // Check for @Start
244 for (Item it : toUse.getItems()) {
245 if (it instanceof Text) {
246 Text t = (Text) it;
247 if (t.getText().toLowerCase().equals("@start") || t.getText().toLowerCase().startsWith("@start:")) {
248 t.stripFirstWord();
249
250 if (t.getText().equals("")) {
251 _lastY = t.getY();
252 _lastX = t.getX();
253 t.delete();
254 break;
255 }
256 }
257 }
258 }
259 _current = toUse;
260 }
261
262 public boolean addItem(Item toAdd, boolean bSave) {
263 try {
264 // if we have reached the end of the Y axis, try moving over on the
265 // X axis
266 if (_lastY >= _Mprev.getY() - _Mprev.getBoundsHeight()) {
267 _lastX = _maxX + 10;
268 _lastY = START_Y;
269
270 // if there is no more room on the X axis, we have to start a
271 // new frame
272 if (!_multiColumn || toAdd.getBoundsWidth() + _lastX > DisplayController.getFramePaintAreaWidth()) {
273 // Make sure text items that are created on the current
274 // frame are removed
275 _current.removeItem(toAdd);
276 createNextFrame();
277 }
278 }
279
280 toAdd.setPosition(_lastX, _lastY + toAdd.getBoundsHeight() / 2);
281 toAdd.setOffset(0, 0);
282 toAdd.setID(_current.getNextItemID());
283 toAdd.setRightMargin(DisplayController.getFramePaintAreaWidth(), true);
284
285 _current.addItem(toAdd);
286 // _current.addAllItems(items);
287 if (bSave)
288 save();
289
290 _lastY = toAdd.getY() + toAdd.getBoundsHeight() / 2;
291 _maxX = Math.max(toAdd.getX() + toAdd.getBoundsWidth(), _maxX);
292
293 return true;
294 } catch (Exception e) {
295 return false;
296 }
297 }
298
299 public Text addText(String toAdd, Colour c, String link, String action, boolean bSave) {
300 Text text = _current.createNewText(toAdd);
301 if (c != null)
302 text.setColor(c);
303 text.setLink(link);
304 text.setAction(action);
305
306 addItem(text, bSave);
307
308 return text;
309 }
310
311 public void save() {
312 FrameIO.ForceSaveFrame(_current);
313 }
314
315 public int getLastY() {
316 return _lastY;
317 }
318
319 public void setLastY(int lastY) {
320 _lastY = lastY;
321 }
322
323 public Frame getCurrentFrame() {
324 return _current;
325 }
326
327 /**
328 * Returns the Frame name of the current frame for the FrameCreator
329 *
330 * @return The current frame for the FrameCreator
331 */
332 public String getCurrent() {
333 if (_current == null)
334 return null;
335
336 return _current.getName();
337 }
338
339 public Frame getFirstFrame() {
340 return _firstFrame;
341 }
342
343 public Item addNextButton(Frame current, String link) {
344 return addButton(_Mnext, current, link);
345 }
346
347 public Item addPreviousButton(Frame current, String link) {
348 return addButton(_Mprev, current, link);
349 }
350
351 public Item addFirstButton(Frame current, String link) {
352 return addButton(_Mfirst, current, link);
353 }
354
355 public Item addButton(Item template, Frame current, String link) {
356 // add link to new frame
357 Item previousButton = template.copy();
358 previousButton.setID(current.getNextItemID());
359 previousButton.setLink(link);
360 previousButton.setLinkHistory(false);
361 previousButton.setLinkMark(false);
362 // previousButton.setPermission(new
363 // PermissionPair(UserAppliedPermission.followLinks));
364 current.addItem(previousButton);
365
366 return previousButton;
367 }
368
369 public void addSpace(int space) {
370 _lastY += space;
371 }
372
373 public static int getYStart(Item title) {
374 return title.getY() + title.getBoundsHeight();
375 }
376
377 public void setTitle(String titleText) {
378 _current.setTitle(titleText);
379 }
380}
Note: See TracBrowser for help on using the repository browser.