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

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

When loading in messages from the mail databases now uses the timestamp last-accessed companion file to only load in new ones.

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() + File.separator);
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 } catch (InvalidFramesetNameException e) {
150 e.printStackTrace();
151 } catch (ExistingFramesetException e) {
152 // This should never happen as we are allowing for an override of existing frameset.
153 }
154 }
155
156 /**
157 * Creates a text item that looks like a clickable button.
158 *
159 * @param text
160 * the caption for the button
161 * @param x
162 * the x position for the button. Null if the button is anchored to
163 * the right of the screen.
164 * @param y
165 * the y position for the button. Null if the button is anchored to
166 * the bottom of the screen.
167 * @param right
168 * the distance the button is anchored from the right of this screen.
169 * Null if an absolute position is used.
170 * @param bottom
171 * the distance the button is to be anchored from the bottom of the
172 * screen. Null if the button is given an absolute position.
173 * @return the newly created button.
174 */
175 public static Item createButton(String text, Integer x, Integer y, Integer right, Integer bottom) {
176 Text button = new Text(text);
177
178 button.setBackgroundColor(Colour.LIGHT_GREY);
179 button.setBorderColor(Colour.DARK_GREY);
180 button.setThickness(2.0F);
181 if (bottom != null)
182 button.setAnchorBottom(bottom);
183 if (x != null)
184 button.setX(x);
185 if (right != null)
186 button.setAnchorRight(right);
187 if (y != null)
188 button.setY(y);
189
190 button.invalidateBounds();
191
192 return button;
193 }
194
195 public String getName() {
196 return _firstFrame.getName();
197 }
198
199 /**
200 * Creates the next frame in the frameset, with a previous button already added
201 * and linked to the last frame. _current then gets updated to point at the
202 * newly created Frame, and _lastY is reset
203 */
204 public boolean createNextFrame() {
205 try {
206 Frame newFrame = FrameIO.CreateFrame(_current.getFramesetName(), _current.getTitle(), null);
207
208 this.framesCreated.add(newFrame);
209
210 // add link to previous frame
211 // _prev =
212 addPreviousButton(newFrame, _current.getName());
213
214 // add link to new frame
215 // _next =
216 addNextButton(_current, newFrame.getName());
217
218 // add link to new frame
219 addFirstButton(newFrame, _firstFrame.getName());
220
221 FrameIO.SaveFrame(_current, false);
222
223 resetGlobals(newFrame);
224 _maxX = 0;
225 return true;
226 } catch (Exception e) {
227 return false;
228 }
229 }
230
231 public List<Frame> getFramesCreated() {
232 return this.framesCreated;
233 }
234
235 private void resetGlobals(Frame toUse) {
236 Text title = toUse.getTitleItem();
237 START_X = INDENT_FROM_TITLE + title.getX();
238 START_Y = getYStart(title);
239 _lastY = START_Y;
240 _lastX = START_X;
241 // Check for @Start
242 for (Item it : toUse.getItems()) {
243 if (it instanceof Text) {
244 Text t = (Text) it;
245 if (t.getText().toLowerCase().equals("@start") || t.getText().toLowerCase().startsWith("@start:")) {
246 t.stripFirstWord();
247
248 if (t.getText().equals("")) {
249 _lastY = t.getY();
250 _lastX = t.getX();
251 t.delete();
252 break;
253 }
254 }
255 }
256 }
257 _current = toUse;
258 }
259
260 public boolean addItem(Item toAdd, boolean bSave) {
261 try {
262 // if we have reached the end of the Y axis, try moving over on the
263 // X axis
264 if (_lastY >= _Mprev.getY() - _Mprev.getBoundsHeight()) {
265 _lastX = _maxX + 10;
266 _lastY = START_Y;
267
268 // if there is no more room on the X axis, we have to start a
269 // new frame
270 if (!_multiColumn || toAdd.getBoundsWidth() + _lastX > DisplayController.getFramePaintAreaWidth()) {
271 // Make sure text items that are created on the current
272 // frame are removed
273 _current.removeItem(toAdd);
274 createNextFrame();
275 }
276 }
277
278 toAdd.setPosition(_lastX, _lastY + toAdd.getBoundsHeight() / 2);
279 toAdd.setOffset(0, 0);
280 toAdd.setID(_current.getNextItemID());
281 toAdd.setRightMargin(DisplayController.getFramePaintAreaWidth(), true);
282
283 _current.addItem(toAdd);
284 // _current.addAllItems(items);
285 if (bSave)
286 save();
287
288 _lastY = toAdd.getY() + toAdd.getBoundsHeight() / 2;
289 _maxX = Math.max(toAdd.getX() + toAdd.getBoundsWidth(), _maxX);
290
291 return true;
292 } catch (Exception e) {
293 return false;
294 }
295 }
296
297 public Text addText(String toAdd, Colour c, String link, String action, boolean bSave) {
298 Text text = _current.createNewText(toAdd);
299 if (c != null)
300 text.setColor(c);
301 text.setLink(link);
302 text.setAction(action);
303
304 addItem(text, bSave);
305
306 return text;
307 }
308
309 public void save() {
310 FrameIO.ForceSaveFrame(_current);
311 }
312
313 public int getLastY() {
314 return _lastY;
315 }
316
317 public void setLastY(int lastY) {
318 _lastY = lastY;
319 }
320
321 public Frame getCurrentFrame() {
322 return _current;
323 }
324
325 /**
326 * Returns the Frame name of the current frame for the FrameCreator
327 *
328 * @return The current frame for the FrameCreator
329 */
330 public String getCurrent() {
331 if (_current == null)
332 return null;
333
334 return _current.getName();
335 }
336
337 public Frame getFirstFrame() {
338 return _firstFrame;
339 }
340
341 public Item addNextButton(Frame current, String link) {
342 return addButton(_Mnext, current, link);
343 }
344
345 public Item addPreviousButton(Frame current, String link) {
346 return addButton(_Mprev, current, link);
347 }
348
349 public Item addFirstButton(Frame current, String link) {
350 return addButton(_Mfirst, current, link);
351 }
352
353 public Item addButton(Item template, Frame current, String link) {
354 // add link to new frame
355 Item previousButton = template.copy();
356 previousButton.setID(current.getNextItemID());
357 previousButton.setLink(link);
358 previousButton.setLinkHistory(false);
359 previousButton.setLinkMark(false);
360 // previousButton.setPermission(new
361 // PermissionPair(UserAppliedPermission.followLinks));
362 current.addItem(previousButton);
363
364 return previousButton;
365 }
366
367 public void addSpace(int space) {
368 _lastY += space;
369 }
370
371 public static int getYStart(Item title) {
372 return title.getY() + title.getBoundsHeight();
373 }
374
375 public void setTitle(String titleText) {
376 _current.setTitle(titleText);
377 }
378}
Note: See TracBrowser for help on using the repository browser.