source: trunk/src/org/expeditee/gui/Frame.java@ 1448

Last change on this file since 1448 was 1448, checked in by bnemhaus, 5 years ago

When deleting a picture on a expedite frame; if shift is held down, then the associated image file is now moved to the trash directory (FrameIO.TRASH_PATH).
When undoing the deletion of a picture, the associated image file is restored.

File size: 74.5 KB
Line 
1/**
2 * Frame.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.nio.file.StandardCopyOption;
27import java.sql.Time;
28import java.util.ArrayList;
29import java.util.Collection;
30import java.util.Collections;
31import java.util.HashMap;
32import java.util.HashSet;
33import java.util.Iterator;
34import java.util.LinkedHashSet;
35import java.util.LinkedList;
36import java.util.List;
37import java.util.Map;
38import java.util.Set;
39import java.util.Stack;
40import java.util.stream.Collectors;
41import java.util.stream.Stream;
42
43import org.expeditee.actions.Simple;
44import org.expeditee.core.Colour;
45import org.expeditee.core.Image;
46import org.expeditee.core.bounds.PolygonBounds;
47import org.expeditee.encryption.items.EncryptionPermissionTriple;
48import org.expeditee.encryption.items.surrogates.Label;
49import org.expeditee.gio.EcosystemManager;
50import org.expeditee.gio.gesture.Gesture;
51import org.expeditee.gio.gesture.Gesture.GestureType;
52import org.expeditee.gio.gesture.StandardGestureActions;
53import org.expeditee.gio.gesture.StandardGestureActions.StandardGestureType;
54import org.expeditee.gio.gesture.data.RefreshGestureData;
55import org.expeditee.gio.input.KBMInputEvent.Key;
56import org.expeditee.gio.input.StandardInputEventListeners;
57import org.expeditee.io.Conversion;
58import org.expeditee.io.ExpReader;
59import org.expeditee.items.Constraint;
60import org.expeditee.items.Dot;
61import org.expeditee.items.Item;
62import org.expeditee.items.Item.HighlightMode;
63import org.expeditee.items.ItemAppearence;
64import org.expeditee.items.ItemParentStateChangedEvent;
65import org.expeditee.items.ItemUtils;
66import org.expeditee.items.Line;
67import org.expeditee.items.PermissionTriple;
68import org.expeditee.items.Text;
69import org.expeditee.items.UserAppliedPermission;
70import org.expeditee.items.XRayable;
71import org.expeditee.items.widgets.Widget;
72import org.expeditee.items.widgets.WidgetCorner;
73import org.expeditee.settings.UserSettings;
74import org.expeditee.settings.templates.TemplateSettings;
75import org.expeditee.simple.UnitTestFailedException;
76import org.expeditee.stats.Formatter;
77import org.expeditee.stats.SessionStats;
78
79/**
80 * Represents a Expeditee Frame that is displayed on the screen. Also is a
81 * registered MouseListener on the Browser, and processes any MouseEvents
82 * directly.
83 *
84 * @author jdm18
85 *
86 */
87public class Frame {
88
89 /** The frame number to indicate this is a virtual frame. */
90 public static final int VIRTUAL_FRAME_NUMBER = -1;
91
92 /** The background colour the frame name should take if the frame has user permission level 'none'. */
93 public static final Colour FRAME_NAME_BACKGROUND_COLOUR_FOR_PERMISSION_NONE = Colour.FromRGB255(255, 220, 220);
94 /** The background colour the frame name should take if the frame has user permission level 'followLinks'. */
95 public static final Colour FRAME_NAME_BACKGROUND_COLOUR_FOR_PERMISSION_FOLLOW_LINKS = Colour.FromRGB255(255, 230, 135);
96 /** The background colour the frame name should take if the frame has user permission level 'copy'. */
97 public static final Colour FRAME_NAME_BACKGROUND_COLOUR_FOR_PERMISSION_COPY = Colour.FromRGB255(255, 255, 155);
98 /** The background colour the frame name should take if the frame has user permission level 'createFrames'. */
99 public static final Colour FRAME_NAME_BACKGROUND_COLOUR_FOR_PERMISSION_CREATE_FRAMES = Colour.FromRGB255(220, 255, 220);
100 /** The background colour the frame name should take if the frame has user permission level 'full'. */
101 public static final Colour FRAME_NAME_BACKGROUND_COLOUR_FOR_PERMISSION_FULL = null;
102
103 private boolean _protectionChanged = false;
104
105 // The various attributes of this Frame
106 private String _frameset = null;
107
108 private int _number = -1;
109
110 private int _version = 0;
111
112 private PermissionTriple _permissionTriple = null;
113
114 private EncryptionPermissionTriple _encPermissionTriple = null;
115
116 private String _owner = null;
117
118 private String _creationDate = null;
119
120 private String _modifiedUser = null;
121
122 private String _modifiedDate = null;
123 private long _modifiedDatePrecise;
124
125 private String _frozenDate = null;
126
127 // Background color is clear
128 private Colour _background = null;
129
130 // Foreground color is automatic by default
131 private Colour _foreground = null;
132
133 private String path;
134
135 private boolean _isLocal = true;
136
137 /** Whether the frame has changed and therefore needs saving. */
138 private boolean _change = false;
139
140 /** Whether the frame has been saved. */
141 private boolean _saved = false;
142
143 // list of deleted items that can be restored
144 private Stack<History> _undo = new Stack<History>();
145 private Stack<History> _redo = new Stack<History>();
146
147 private ItemsList _body = new ItemsList();
148 private ItemsList _bodyHiddenDueToPermissions = new ItemsList();
149 private ItemsList _primaryItemsBody = new ItemsList();
150 private ItemsList _surrogateItemsBody = new ItemsList();
151
152 //private List<Item> _body = new ArrayList<Item>();
153 //private List<Item> _bodyHiddenDueToPermissions = new ArrayList<Item>();
154 //private List<Item> _primaryItemsBody = new ArrayList<Item>();
155 //private List<Item> _surrogateItemsBody = new ArrayList<Item>();
156
157 // for drawing purposes
158 private List<Widget> _iWidgets = new ArrayList<Widget>();
159
160 // frame data
161 private List<String> _frameData = null;
162
163 private int _lineCount = 0;
164
165 private int _itemCount = 1;
166
167 // The frameName to display on the screen
168 private Text _frameName = null;
169
170 private Map<Overlay, Frame> _overlays = new HashMap<Overlay, Frame>();
171
172 private List<Vector> _vectors = new ArrayList<Vector>();
173
174 private Image _buffer = null;
175
176 private boolean _validBuffer = true;
177
178 private Time _activeTime = new Time(0);
179
180 private Time _darkTime = new Time(0);
181
182 private Collection<Item> _interactableItems = new LinkedHashSet<Item>();
183
184 private Collection<Item> _overlayItems = new LinkedHashSet<Item>();
185
186 private Collection<Item> _vectorItems = new LinkedHashSet<Item>();
187
188 private Text _dotTemplate = TemplateSettings.DotTemplate.get().copy();
189
190 private Map<String, Text> _annotations = null;
191
192 private Collection<FrameObserver> _observers = new HashSet<FrameObserver>();
193
194 private String _encryptionLabel;
195
196 private String _groupFrameName;
197 private Frame _groupFrame = null;
198
199 private List<String> labelsOnLastBodySet;
200
201 public enum BodyType {
202 BodyDisplay, PrimaryBody, SurrogateBody;
203 }
204
205 /** Default constructor, nothing is set. */
206 public Frame() {
207 }
208
209 public boolean isReadOnly() {
210 return !_frameName.hasPermission(UserAppliedPermission.full) && !_protectionChanged;
211 }
212
213 public void reset() {
214 refreshItemPermissions(UserAppliedPermission.full);
215 resetDot();
216 SessionStats.NewFrameSession();
217 }
218
219 private void resetDot()
220 {
221 _dotTemplate.setColor(TemplateSettings.ColorWheel.getSafe(1));
222 _dotTemplate.setFillColor(TemplateSettings.FillColorWheel.getSafe(0));
223 }
224
225 public void nextDot()
226 {
227 _dotTemplate.setFillColor(ColorUtils.getNextColor(_dotTemplate.getFillColor(), TemplateSettings.FillColorWheel.get(), null));
228 _dotTemplate.setColor(ColorUtils.getNextColor(_dotTemplate.getColor(), TemplateSettings.ColorWheel.get(), null));
229 if (_dotTemplate.getColor() == null || _dotTemplate.getColor().equals(Colour.WHITE)) {
230 resetDot();
231 }
232 }
233
234 public Image getBuffer()
235 {
236 return _buffer;
237 }
238
239 public void setBuffer(Image newBuffer)
240 {
241 _buffer = newBuffer;
242 }
243
244 public boolean isBufferValid()
245 {
246 if (_buffer == null) {
247 return false;
248 }
249
250 return _validBuffer;
251 }
252
253 private void setBufferValid(boolean newValue)
254 {
255 _validBuffer = newValue;
256 }
257
258 public int getNextItemID()
259 {
260 return ++_itemCount;
261 }
262
263 public void updateIDs(List<Item> items)
264 {
265 for (Item i : items) {
266 if (!(i instanceof Line)) {
267 i.setID(getNextItemID());
268 } else {
269 i.setID(++_lineCount);
270 }
271 }
272 }
273
274 /**
275 *
276 * @return The interactive widgets that are currently anchored in this frame.
277 * Hence it excludes free-widgets. Returns a copy
278 */
279 public List<Widget> getInteractiveWidgets()
280 {
281 LinkedList<Widget> clone = new LinkedList<Widget>();
282 clone.addAll(this._iWidgets);
283 return clone;
284 }
285
286 /**
287 * Returns whether this Frame has been changed and required saving to disk.
288 *
289 * @return True if this Frame has been altered, false otherwise.
290 */
291 public boolean hasChanged()
292 {
293 // virtual frames are never saved
294 if (_number == VIRTUAL_FRAME_NUMBER) {
295 return false;
296 }
297
298 return _change;
299 }
300
301 /**
302 * Sets whether this Frame should be saved to disk.
303 *
304 * @param value
305 * True if this Frame should be saved to disk, False otherwise.
306 */
307 public void setChanged(boolean value)
308 {
309 if (_change == value) {
310 return;
311 }
312
313 _change = value;
314
315 if (_change) {
316 setBufferValid(false);
317 _saved = false;
318 }
319 }
320
321 /**
322 * Notify items observing the data on this frame that the frame content has
323 * changed.
324 *
325 * @param recalculate
326 * true if the frame should be recalculated first.
327 */
328 public void notifyObservers(boolean bRecalculate) {
329 if (bRecalculate) {
330 recalculate();
331 }
332 // Notify the frame listeners that the frame has changed
333 /*
334 * Avoid ConcurrentMod Exceptions when user anchors an item onto this
335 * frame which is observing this frame, by NOT using foreach loop.
336 * Calling update on a dataFrameWidget resets its subjects hence
337 * changing this frames observer list.
338 */
339 Collection<FrameObserver> observersCopy = new LinkedList<FrameObserver>(_observers);
340 // System.out.println(++updateCount + " update");
341
342 for (FrameObserver fl : observersCopy) {
343 if (fl.isVisible()) {
344 fl.update();
345 }
346 }
347 }
348
349 // indicates the frame has changed
350 public void change()
351 {
352 setChanged(true);
353 _interactableItems.clear();
354 }
355
356 /**
357 * Returns an ArrayList of all Items currently on the Frame (excludes Items
358 * attached to the cursor).
359 *
360 * @return The list of Item objects that are on this Frame.
361 */
362 public List<Item> getSortedItems(boolean requireVisible) {
363 ItemsList listToLoopOver = getBody(true);
364 listToLoopOver.sort();
365 return getItems(requireVisible, listToLoopOver);
366 }
367
368 public List<Item> getItems(boolean requireVisible, ItemsList listToLoopOver) {
369 List<Item> items = new ArrayList<Item>();
370
371 for (Item i: listToLoopOver) {
372 if (i == null) {
373 continue;
374 }
375
376 if (meetsVisibilityRequirements(requireVisible, i)) {
377 items.add(i);
378 }
379 }
380
381 return items;
382 }
383
384 /** TODO: Comment. cts16 */
385 public List<Item> getSortedItems()
386 {
387 return getSortedItems(false);
388 }
389
390 /**
391 * @param i
392 * Item to check if contained in this frame
393 * @return True if this frame contains i.
394 */
395 public boolean containsItem(Item i) {
396 if (i == null) {
397 throw new NullPointerException("i");
398 }
399 return getBody(true).contains(i);
400 }
401
402 /**
403 * Returns a list of all the non annotation text items on the frame which
404 * are not the title or frame name or special annotation items.
405 *
406 * @param includeAnnotations
407 * true if annotation items without special meaning should be
408 * included
409 * @param includeLineEnds
410 * true if text on the end of lines should be included in the
411 * list
412 * @return the list of body text items.
413 */
414 public List<Text> getBodyTextItems(boolean includeAnnotations) {
415 ensureBody();
416
417 List<Text> bodyTextItems = new ArrayList<Text>();
418
419 for (Item i : getSortedItems(true)) {
420 // only add up normal body text items
421 if ((i instanceof Text) && ((includeAnnotations && !((Text) i).isSpecialAnnotation()) || !i.isAnnotation()) && !i.isLineEnd()) {
422 bodyTextItems.add((Text) i);
423 }
424 }
425
426 bodyTextItems.remove(getTitleItem());
427
428 return bodyTextItems;
429 }
430
431 public Collection<Item> getNonAnnotationItems(boolean removeTitle)
432 {
433 Collection<Item> items = new ArrayList<Item>();
434 for (Item i : getSortedItems(true)) {
435 // only add up normal body text items
436 if (!i.isAnnotation()) {
437 items.add(i);
438 }
439 }
440
441 if (removeTitle) {
442 items.remove(getTitleItem());
443 }
444
445 return items;
446 }
447
448 /**
449 * Gets the last item on the frame that is a non annotation item but is also
450 * text.
451 *
452 * @return the last non annotation text item.
453 */
454 public Item getLastNonAnnotationTextItem()
455 {
456 List<Item> items = getSortedItems();
457
458 // find the last non-annotation text item
459 for (int i = (items.size() - 1); i >= 0; i--) {
460 Item it = items.get(i);
461
462 if (it instanceof Text && !it.isAnnotation()) {
463 return it;
464 }
465 }
466 return null;
467 }
468
469 /**
470 * Iterates through the list of items on the frame, and returns one with the
471 * given id if one exists, otherwise returns null.
472 *
473 * @param id
474 * The id to search for in the list of items
475 * @return The item on this frame with the given ID, or null if one is not
476 * found.
477 */
478 public Item getItemWithID(int id) {
479 for (Item i : getAllFrameItemsRaw()) {
480 if (i.getID() == id) {
481 return i;
482 }
483 }
484 return null;
485 }
486
487 /**
488 * Sets this Frame's Title which is displayed in the top left corner.
489 *
490 * @param title
491 * The title to assign to this Frame
492 */
493 public void setTitle(String title)
494 {
495 if (title == null || title.equals("")) {
496 return;
497 }
498
499 boolean oldchange = _change;
500
501 // remove any numbering this title has
502 title = title.replaceAll("^\\d*[.] *", "");
503 Text frameTitle = getTitleItem();
504
505 if (frameTitle == null) {
506 if (TemplateSettings.TitleTemplate.get() == null) {
507 frameTitle = new Text(getNextItemID(), title);
508 } else {
509 frameTitle = TemplateSettings.TitleTemplate.get().copy();
510 frameTitle.setID(this.getNextItemID());
511 frameTitle.setText(title);
512 }
513 /*
514 * Need to set the parent otherwise an exception is thrown when
515 * new profile is created
516 */
517 frameTitle.setParent(this);
518 frameTitle.resetTitlePosition();
519 addItem(frameTitle);
520 } else {
521 // If it begins with a tag remove it
522
523 // Remove the @ symbol if it is there
524 // title = ItemUtils.StripTagSymbol(title);
525 frameTitle.setText(title);
526 // If the @ symbol is followed by numbering or a bullet remove that too
527 String autoBulletText = StandardGestureActions.getAutoBullet(title);
528 if (autoBulletText.length() > 0) {
529 frameTitle.stripFirstWord();
530 }
531 }
532 // TODO Widgets... check this out
533 // Brook: Cannot figure what is going on above... widget annot titles
534 // should be stripped always
535 if (ItemUtils.startsWithTag(frameTitle, ItemUtils.GetTag(ItemUtils.TAG_IWIDGET))) {
536 frameTitle.stripFirstWord();
537 }
538
539 FrameUtils.Parse(this);
540
541 // do not save if this is the only change
542 setChanged(oldchange);
543 }
544
545 public Text getTitleItem()
546 {
547 List<Item> items = getVisibleItems();
548
549 for (Item i : items) {
550 if (i instanceof Text && i.getX() < UserSettings.TitlePosition.get() && i.getY() < UserSettings.TitlePosition.get()) {
551 return (Text) i;
552 }
553 }
554
555 return null;
556 }
557
558 public String getTitle()
559 {
560 Text title = getTitleItem();
561 if (title == null) {
562 return getName();
563 }
564
565 return title.getFirstLine();
566 }
567
568 public Item getNameItem() {
569 //Text ret = _frameName;
570 if (this.getEncryptionLabel() != null && this.getEncryptionLabel().length() > 0) {
571 _frameName.setText("\uD83D\uDD12" + getFramesetName() + _number);
572 _frameName.resetFrameNamePosition();
573 }
574 return _frameName;
575 }
576
577 public Text getItemTemplate()
578 {
579 return getTemplate(TemplateSettings.ItemTemplate.get(), ItemUtils.TAG_ITEM_TEMPLATE);
580 }
581
582 public Text getAnnotationTemplate()
583 {
584 Text t = getTemplate(TemplateSettings.AnnotationTemplate.get(), ItemUtils.TAG_ANNOTATION_TEMPLATE);
585
586 if (t == null) {
587 t = getItemTemplate();
588 }
589
590 return t;
591 }
592
593 public Text getStatTemplate()
594 {
595 SessionStats.CreatedText();
596 Text t = getTemplate(TemplateSettings.StatTemplate.get(), ItemUtils.TAG_STAT_TEMPLATE);
597
598 if (t == null) {
599 t = getItemTemplate();
600 }
601
602 return t;
603 }
604
605 public Item getTooltipTextItem(String tooltipText)
606 {
607 return getTextItem(tooltipText, TemplateSettings.TooltipTemplate.get().copy());
608 }
609
610 public Item getStatsTextItem(String itemText)
611 {
612 return getTextItem(itemText, getStatTemplate());
613 }
614
615 public Item getTextItem(String itemText)
616 {
617 return getTextItem(itemText, getItemTemplate());
618 }
619
620 private Item getTextItem(String itemText, Text template)
621 {
622 Text t = template;
623 // We dont want the stats to wrap at all
624 // t.setMaxWidth(Integer.MAX_VALUE);
625 t.setPosition(DisplayController.getMousePosition());
626 // The next line is needed to make sure the item is removed from the
627 // frame when picked up
628 t.setParent(this);
629 t.setText(itemText);
630 return t;
631 }
632
633 public Text getCodeCommentTemplate()
634 {
635 Text t = getTemplate(TemplateSettings.CommentTemplate.get(), ItemUtils.TAG_CODE_COMMENT_TEMPLATE);
636
637 if (t == null) {
638 t = getItemTemplate();
639 }
640
641 return t;
642 }
643
644
645 /**
646 * Returns any items on this frame that are within the given Shape. Also
647 * returns any Items on overlay frames that are within the Shape.
648 *
649 * @param shape
650 * The Shape to search for Items in
651 * @return All Items on this Frame or overlayed Frames for which
652 * Item.intersects(shape) return true.
653 */
654 public Collection<Item> getItemsWithin(PolygonBounds poly)
655 {
656 Collection<Item> results = new LinkedHashSet<Item>();
657 for (Item i : getVisibleItems()) {
658 if (i.intersects(poly)) {
659 if (i instanceof XRayable) {
660 results.addAll(i.getConnected());
661 // Dont add circle centers
662 // TODO change this to be isCircle center
663 } else if (!i.hasEnclosures()) {
664 results.add(i);
665 }
666 }
667 }
668
669 for (Overlay o : _overlays.keySet()) {
670 results.addAll(o.Frame.getItemsWithin(poly));
671 }
672
673 for (Item i : getVectorItems()) {
674 if (i.intersects(poly)) {
675 // This assumes a results is a set
676 results.add(i.getEditTarget());
677 }
678 }
679
680 return results;
681 }
682
683 /**
684 * Sets the name of this Frame to the given String, to be displayed in the
685 * upper right corner.
686 *
687 * @param name
688 * The name to use for this Frame.
689 */
690 public void setFrameset(String name)
691 {
692 _frameset = name;
693 }
694
695 public void setName(String framename)
696 {
697 int num = Conversion.getFrameNumber(framename);
698 String frameset = Conversion.getFramesetName(framename, false);
699
700 setName(frameset, num);
701 }
702
703 /**
704 * Sets the frame number of this Frame to the given integer
705 *
706 * @param number
707 * The number to set as the frame number
708 */
709 public void setFrameNumber(int number)
710 {
711 assert (number >= 0);
712
713 if (_number == number) {
714 return;
715 }
716
717 _number = number;
718 boolean oldchange = _change;
719
720 int id;
721
722 if (_frameName != null) {
723 id = _frameName.getID();
724 } else {
725 id = -1 * getNextItemID();
726 }
727
728 _frameName = new Text(id);
729 _frameName.setParent(this);
730 _frameName.setOwner(this.getOwner());
731 _frameName.setText(getFramesetName() + _number);
732 _frameName.resetFrameNamePosition();
733 setChanged(oldchange);
734 }
735
736 /**
737 * Returns the number of this Frame.
738 *
739 * @return The Frame number of this Frame or -1 if it is not set.
740 */
741 public int getNumber()
742 {
743 return _number;
744 }
745
746 /**
747 * Increments the version of this Frame to the given String.
748 *
749 * @param version
750 * The version to use for this Frame.
751 */
752 public void setVersion(int version)
753 {
754 _version = version;
755 }
756
757 /**
758 * Sets the protection of this Frame to the given String.
759 *
760 * @param protection
761 * The protection to use for this Frame.
762 */
763 public void setPermission(PermissionTriple permission) {
764 List<String> groupMembers = getGroupMembers();
765
766 if (_permissionTriple != null && !_permissionTriple.getPermission(this._owner, groupMembers).equals(permission.getPermission(this._owner, groupMembers))) {
767 _protectionChanged = true;
768 }
769
770 _permissionTriple = new PermissionTriple(permission);
771
772 if (getBody(false).size() > 0) {
773 refreshItemPermissions(permission.getPermission(_owner, groupMembers));
774 }
775 }
776
777 /**
778 * Sets the owner of this Frame to the given String.
779 *
780 * @param owner
781 * The owner to use for this Frame.
782 */
783 public void setOwner(String owner)
784 {
785 _owner = owner;
786 }
787
788 /**
789 * Sets the created date of this Frame to the given String.
790 *
791 * @param date
792 * The date to use for this Frame.
793 */
794 public void setDateCreated(String date)
795 {
796 _creationDate = date;
797 _modifiedDate = date;
798 for (Item i : getAllFrameItemsRaw()) {
799 i.setDateCreated(date);
800 }
801 }
802
803 /**
804 * Resets the dates and version numbers for newly created frames.
805 *
806 */
807 public void resetDateCreated()
808 {
809 setDateCreated(Formatter.getDateTime());
810 resetTimes();
811 setVersion(0);
812 }
813
814 private void resetTimes()
815 {
816 setActiveTime(new Time(0));
817 setDarkTime(new Time(0));
818 }
819
820 /**
821 * Sets the last modifying user of this Frame to the given String.
822 *
823 * @param user
824 * The user to set as the last modifying user.
825 */
826 public void setLastModifyUser(String user)
827 {
828 _modifiedUser = user;
829 }
830
831 /**
832 * Sets the last modified date of this Frame to the given String.
833 *
834 * @param date
835 * The date to set as the last modified date.
836 * @param precise The millisecond precision last modified date.
837 */
838 public void setLastModifyDate(String date, long precise) {
839 _modifiedDate = date;
840 _modifiedDatePrecise = precise;
841 }
842
843 /**
844 * Sets the last modified date of this Frame to the given String.
845 * Used during startup. If able to be more precise then use the overloaded function.
846 *
847 * @param date
848 * The date to set as the last modified date.
849 */
850 public void setLastModifyDate(String date) {
851 _modifiedDate = date;
852 _modifiedDatePrecise = -1l;
853 }
854
855 /**
856 * Sets the last frozen date of this Frame to the given String.
857 *
858 * @param date
859 * The date to set as the last frozen date.
860 */
861 public void setFrozenDate(String date) {
862 _frozenDate = date;
863 }
864
865 public void invalidateSorted() {
866 getBody(false).invalidateSorted();
867 }
868
869 /**
870 * Adds the given Item to the body of this Frame.
871 *
872 * @param item
873 * The Item to add to this Frame.
874 */
875 public void addItem(Item item) {
876 addItem(item, true);
877 }
878
879 public void addItem(Item item, boolean recalculate) {
880 if (item == null) { return; }
881
882 addItem(item, recalculate, getBody(false));
883 if (item.isSurrogate()) {
884 addItem(item, recalculate, getSurrogateBody());
885 } else {
886 addItem(item, recalculate, getPrimaryBody());
887 }
888 }
889
890 protected void addItem(Item item, boolean recalculate, ItemsList list) {
891 if (item == null || item.equals(_frameName) || list.contains(item)) {
892 return;
893 }
894
895 // When an annotation item is anchored the annotation list must be
896 // refreshed
897 if (item.isAnnotation()) {
898 clearAnnotations();
899 }
900
901 if (item instanceof Line) {
902 _lineCount++;
903 }
904
905 _itemCount = Math.max(_itemCount, item.getID());
906
907 list.add(item);
908 item.setParent(this);
909 item.setFloating(false); // esnure that it is anchored
910
911 item.invalidateCommonTrait(ItemAppearence.Added);
912
913 // If the item is a line end and has constraints with items already
914 // on the frame then make sure the constraints hold
915 if (item.isLineEnd()) {
916 item.setPosition(item.getPosition());
917 }
918
919 list.invalidateSorted();
920
921 // item.setMaxWidth(FrameGraphics.getMaxFrameSize().width);
922 // add widget items to the list of widgets
923 if (item instanceof WidgetCorner) {
924 Widget iw = ((WidgetCorner) item).getWidgetSource();
925 if (!this._iWidgets.contains(iw)) { // A set would have been
926 if (StandardInputEventListeners.kbmStateListener.isKeyDown(Key.CTRL)) {
927 _iWidgets.add(iw);
928 } else {
929 _iWidgets.add(0, iw);
930 }
931 }
932 }
933
934 item.onParentStateChanged(new ItemParentStateChangedEvent(this, ItemParentStateChangedEvent.EVENT_TYPE_ADDED));
935
936 // if (recalculate && item.recalculateWhenChanged())
937 // recalculate();
938
939 change();
940 }
941
942 public void addToSurrogatesOnLoad(Item surrogate, Item parent) {
943 parent.addToSurrogates(surrogate);
944 }
945
946 public void refreshSize()
947 {
948 boolean bReparse = false;
949
950 for (Item i : getSortedItems()) {
951 Integer anchorLeft = i.getAnchorLeft();
952 Integer anchorRight = i.getAnchorRight();
953 Integer anchorTop = i.getAnchorTop();
954 Integer anchorBottom = i.getAnchorBottom();
955
956
957 if (anchorLeft != null) {
958 i.setAnchorLeft(anchorLeft);
959 if (i.hasVector()) {
960 bReparse = true;
961 }
962 }
963
964 if (anchorRight != null) {
965 i.setAnchorRight(anchorRight);
966 if (i.hasVector()) {
967 bReparse = true;
968 }
969 }
970
971 if (anchorTop != null) {
972 i.setAnchorTop(anchorTop);
973 if (i.hasVector()) {
974 bReparse = true;
975 }
976 }
977
978 if (anchorBottom != null) {
979 i.setAnchorBottom(anchorBottom);
980 if (i.hasVector()) {
981 bReparse = true;
982 }
983 }
984 }
985
986 // Do the anchors on the overlays
987 for (Overlay o : getOverlays()) {
988 o.Frame.refreshSize();
989 }
990
991 if (bReparse) {
992 FrameUtils.Parse(this, false);
993 }
994
995 _frameName.resetFrameNamePosition();
996 }
997
998 public void addAllItems(Collection<Item> toAdd) {
999 addAllItems(toAdd, getBody(false));
1000 addAllItems(toAdd, getPrimaryBody());
1001 }
1002
1003 protected void addAllItems(Collection<Item> toAdd, ItemsList list) {
1004 for (Item i : toAdd) {
1005 // If an annotation is being deleted clear the annotation list
1006 if (i.isAnnotation()) {
1007 i.getParentOrCurrentFrame().clearAnnotations();
1008 }
1009 // TODO Improve efficiency when addAll is called
1010 addItem(i, true, list);
1011 }
1012 }
1013
1014 public void removeAllItems(Collection<Item> toRemove) {
1015 for (Item i : toRemove) {
1016 // If an annotation is being deleted clear the annotation list
1017 if (i.isAnnotation()) {
1018 i.getParentOrCurrentFrame().clearAnnotations();
1019 }
1020 removeItem(i);
1021 }
1022 }
1023
1024 public void removeItem(Item item) {
1025 removeItem(item, true);
1026 }
1027
1028 public void removeItem(Item item, boolean recalculate) {
1029 removeItem(item, recalculate, getBody(false));
1030 if (item.isSurrogate()) {
1031 removeItem(item, recalculate, getSurrogateBody());
1032 Set<Item> primariesSurrogates = item.getPrimary().getSurrogates();
1033 primariesSurrogates.remove(item);
1034 } else {
1035 removeItem(item, recalculate, getPrimaryBody());
1036 }
1037 }
1038
1039 protected void removeItem(Item item, boolean recalculate, ItemsList toRemoveFrom) {
1040 // If an annotation is being deleted clear the annotation list
1041 if (item.isAnnotation()) {
1042 item.getParentOrCurrentFrame().clearAnnotations();
1043 }
1044
1045 if (toRemoveFrom.remove(item)) {
1046 change();
1047 // Remove widgets from the widget list
1048 if (item != null) {
1049 item.onParentStateChanged(new ItemParentStateChangedEvent(this, ItemParentStateChangedEvent.EVENT_TYPE_REMOVED));
1050
1051 if (item instanceof WidgetCorner) {
1052 _iWidgets.remove(((WidgetCorner) item).getWidgetSource());
1053 }
1054
1055 item.invalidateCommonTrait(ItemAppearence.Removed);
1056 }
1057 // TODO Improve efficiency when removeAll is called
1058 // if (recalculate && item.recalculateWhenChanged())
1059 // recalculate();
1060 }
1061 }
1062
1063 /**
1064 * Adds the given History event to the stack.
1065 *
1066 * @param stack The stack to add to
1067 * @param items The items to put in the event
1068 * @param type The type of event that occurred
1069 */
1070 private void addToUndo(ItemsList items, History.Type type) {
1071 if (items.size() < 1) {
1072 return;
1073 }
1074
1075 _undo.push(new History(items, type));
1076 }
1077
1078 public void addToUndoDelete(ItemsList items) {
1079 addToUndo(items, History.Type.deletion);
1080 }
1081
1082 public void addToUndoMove(ItemsList items) {
1083 addToUndo(items, History.Type.movement);
1084 }
1085
1086 public void undo() {
1087 boolean reparse = false;
1088 boolean recalculate = false;
1089
1090 if (_undo.size() <= 0) {
1091 return;
1092 }
1093
1094 History undo = _undo.pop();
1095
1096 // System.out.println("Undoing: " + undo);
1097
1098 switch(undo.type) {
1099 case deletion:
1100 _redo.push(undo);
1101 for(Item i : undo.items) {
1102 if (i instanceof org.expeditee.items.Picture) {
1103 String destination = ((org.expeditee.items.Picture) i).getPath();
1104 Path destinationPath = Paths.get(destination);
1105 Path sourcePath = Paths.get(FrameIO.TRASH_PATH).resolve(destinationPath.getFileName());
1106 try {
1107 Files.move(sourcePath, destinationPath, StandardCopyOption.ATOMIC_MOVE);
1108 } catch (IOException e) {
1109 MessageBay.displayMessage("Unable to restore image file from trash, not undoing deletion of image.");
1110 continue;
1111 }
1112 }
1113 this.addItem(i);
1114 reparse |= i.hasOverlay();
1115 recalculate |= i.recalculateWhenChanged();
1116 if (i instanceof Line) {
1117 Line line = (Line) i;
1118 line.getStartItem().addLine(line);
1119 line.getEndItem().addLine(line);
1120 } else {
1121 i.setOffset(0, 0);
1122 }
1123 }
1124 break;
1125 case movement:
1126 ItemsList body = getBody(true);
1127 ItemsList changed = new ItemsList(body);
1128 changed.retainAll(undo.items);
1129 _redo.push(new History(changed, History.Type.movement));
1130 for(Item i : undo.items) {
1131 int index;
1132 if(i.isVisible() && (index = body.indexOf(i)) != -1) {
1133 body.set(index, i);
1134 }
1135 }
1136 break;
1137 }
1138
1139 change();
1140
1141 StandardGestureActions.refreshHighlights();
1142
1143 if (reparse) {
1144 FrameUtils.Parse(this, false, false);
1145 } else {
1146 notifyObservers(recalculate);
1147 }
1148
1149 // always request a refresh otherwise filled shapes
1150 // that were broken by a deletion and then reconnected by the undo
1151 // don't get filled until the user otherwise causes them to redraw
1152 DisplayController.requestRefresh(false);
1153 // ItemUtils.EnclosedCheck(_body);
1154 ItemUtils.Justify(this);
1155 }
1156
1157 public void redo()
1158 {
1159 boolean bReparse = false;
1160 boolean bRecalculate = false;
1161
1162 if (_redo.size() <= 0) {
1163 return;
1164 }
1165
1166 History redo = _redo.pop();
1167
1168 // System.out.println("Redoing: " + redo);
1169
1170 switch(redo.type) {
1171 case deletion:
1172 _undo.push(redo);
1173 for(Item i : redo.items) {
1174 this.removeItem(i);
1175 //_body.remove(i);
1176 bReparse |= i.hasOverlay();
1177 bRecalculate |= i.recalculateWhenChanged();
1178 if (i instanceof Line) {
1179 Line line = (Line) i;
1180 line.getStartItem().removeLine(line);
1181 line.getEndItem().removeLine(line);
1182 } else {
1183 i.setOffset(0, 0);
1184 }
1185 }
1186 break;
1187 case movement:
1188 ItemsList body = getBody(true);
1189 ItemsList changed = new ItemsList(body);
1190 changed.retainAll(redo.items);
1191 _undo.push(new History(changed, History.Type.movement));
1192 for(Item i : redo.items) {
1193 int index;
1194 if(i.isVisible() && (index = body.indexOf(i)) != -1) {
1195 body.set(index, i);
1196 }
1197 }
1198 break;
1199 }
1200
1201 change();
1202
1203 StandardGestureActions.refreshHighlights();
1204
1205 if (bReparse) {
1206 FrameUtils.Parse(this, false, false);
1207 } else {
1208 notifyObservers(bRecalculate);
1209 }
1210
1211 // always request a refresh otherwise filled shapes
1212 // that were broken by a deletion and then reconnected by the undo
1213 // don't get filled until the user otherwise causes them to redraw
1214 DisplayController.requestRefresh(false);
1215 // ItemUtils.EnclosedCheck(_body);
1216 ItemUtils.Justify(this);
1217 }
1218
1219 /**
1220 * Returns the frameset of this Frame
1221 *
1222 * @return The name of this Frame's frameset.
1223 */
1224 public String getFramesetName()
1225 {
1226 return _frameset;
1227 }
1228
1229 public String getName()
1230 {
1231 return getFramesetName() + _number;
1232 }
1233
1234 /**
1235 * Returns the format version of this Frame
1236 *
1237 * @return The version of this Frame.
1238 */
1239 public int getVersion()
1240 {
1241 return _version;
1242 }
1243
1244 public PermissionTriple getPermission() {
1245 return _permissionTriple;
1246 }
1247
1248 public UserAppliedPermission getUserAppliedPermission() {
1249 return getUserAppliedPermission(UserAppliedPermission.full);
1250 }
1251
1252 public UserAppliedPermission getUserAppliedPermission(UserAppliedPermission defaultPermission) {
1253 if (_permissionTriple == null) {
1254 return defaultPermission;
1255 }
1256
1257 return _permissionTriple.getPermission(_owner, getGroupMembers());
1258 }
1259
1260 public String getOwner()
1261 {
1262 return _owner;
1263 }
1264
1265 public String getDateCreated()
1266 {
1267 return _creationDate;
1268 }
1269
1270 public String getLastModifyUser()
1271 {
1272 return _modifiedUser;
1273 }
1274
1275 public String getLastModifyDate() {
1276 return _modifiedDate;
1277 }
1278
1279 public long getLastModifyPrecise() {
1280 return _modifiedDatePrecise;
1281 }
1282
1283 public String getFrozenDate()
1284 {
1285 return _frozenDate;
1286 }
1287
1288 public void setBackgroundColor(Colour back)
1289 {
1290 _background = back;
1291
1292 change();
1293
1294 if (this == DisplayController.getCurrentFrame()) {
1295 DisplayController.requestRefresh(false);
1296 }
1297 }
1298
1299 public Colour getBackgroundColor()
1300 {
1301 return _background;
1302 }
1303
1304 public Colour getPaintBackgroundColor()
1305 {
1306 // If null... return white
1307 if (_background == null) {
1308 return Item.DEFAULT_BACKGROUND;
1309 }
1310
1311 return _background;
1312 }
1313
1314 public void setForegroundColor(Colour front)
1315 {
1316 _foreground = front;
1317
1318 change();
1319 }
1320
1321 public Colour getForegroundColor()
1322 {
1323 return _foreground;
1324 }
1325
1326 public Colour getPaintForegroundColor()
1327 {
1328 final int GRAY = Colour.GREY.getBlue();
1329 final int THRESHOLD = Colour.FromComponent255(10);
1330
1331 if (_foreground == null) {
1332 Colour back = getPaintBackgroundColor();
1333 if (Math.abs(back.getRed() - GRAY) < THRESHOLD
1334 && Math.abs(back.getBlue() - GRAY) < THRESHOLD
1335 && Math.abs(back.getGreen() - GRAY) < THRESHOLD)
1336 {
1337 return Colour.WHITE;
1338 }
1339
1340 Colour fore = back.inverse();
1341
1342 return fore;
1343 }
1344
1345 return _foreground;
1346 }
1347
1348 @Override
1349 public String toString()
1350 {
1351 StringBuilder s = new StringBuilder();
1352 s.append(String.format("Name: %s%d%n", _frameset, _number));
1353 s.append(String.format("Version: %d%n", _version));
1354 // s.append(String.format("Permission: %s%n", _permission.toString()));
1355 // s.append(String.format("Owner: %s%n", _owner));
1356 // s.append(String.format("Date Created: %s%n", _creationDate));
1357 // s.append(String.format("Last Mod. User: %s%n", _modifiedUser));
1358 // s.append(String.format("Last Mod. Date: %s%n", _modifiedDate));
1359 s.append(String.format("Items: %d%n", getAllFrameItemsRaw().size()));
1360 return s.toString();
1361 }
1362
1363 public Text getTextAbove(Text current)
1364 {
1365 Collection<Text> currentTextItems = FrameUtils.getCurrentTextItems();
1366 List<Text> toCheck = new ArrayList<Text>();
1367
1368 if (currentTextItems.contains(current)) {
1369 toCheck.addAll(currentTextItems);
1370 } else {
1371 toCheck.addAll(getTextItems());
1372 }
1373
1374 // Make sure the items are sorted
1375 Collections.sort(toCheck);
1376
1377 int ind = toCheck.indexOf(current);
1378 if (ind == -1) {
1379 return null;
1380 }
1381
1382 // loop through all items above this one, return the first match
1383 for (int i = ind - 1; i >= 0; i--) {
1384 Text check = toCheck.get(i);
1385 if (FrameUtils.inSameColumn(check, current)) {
1386 return check;
1387 }
1388 }
1389
1390 return null;
1391 }
1392
1393 /**
1394 * Gets the text items that are in the same column and below a specified
1395 * item. Frame title and name are excluded from the column list.
1396 *
1397 * @param from
1398 * The Item to get the column for.
1399 */
1400 public List<Text> getColumn(Item from)
1401 {
1402 // Check that this item is on the current frame
1403 if (!getBody(true).contains(from)) {
1404 return null;
1405 }
1406
1407 if (from == null) {
1408 from = getLastNonAnnotationTextItem();
1409 }
1410
1411 if (from == null) {
1412 return null;
1413 }
1414
1415 // Get the enclosedItems
1416 Collection<Text> enclosed = FrameUtils.getCurrentTextItems();
1417 List<Text> toCheck = null;
1418
1419 if (enclosed.contains(from)) {
1420 toCheck = new ArrayList<Text>();
1421 toCheck.addAll(enclosed);
1422 } else {
1423 toCheck = getBodyTextItems(true);
1424 }
1425
1426 List<Text> column = new ArrayList<Text>();
1427
1428 if (toCheck.size() > 0) {
1429 // Make sure the items are sorted
1430 Collections.sort(toCheck);
1431
1432 // Create a list of items consisting of the item 'from' and all the
1433 // items below it which are also in the same column as it
1434 int index = toCheck.indexOf(from);
1435
1436 // If its the title index will be 0
1437 if (index < 0) {
1438 index = 0;
1439 }
1440
1441 for (int i = index; i < toCheck.size(); i++) {
1442 Text item = toCheck.get(i);
1443 if (FrameUtils.inSameColumn(from, item)) {
1444 column.add(item);
1445 }
1446 }
1447 }
1448
1449 return column;
1450 }
1451
1452 /**
1453 * Adds the given Vector to the list of vector Frames being drawn with this
1454 * Frame.
1455 *
1456 * @param vector
1457 * The Vector to add
1458 *
1459 * @throws NullPointerException
1460 * If overlay is null.
1461 */
1462 protected boolean addVector(Vector toAdd)
1463 {
1464 // make sure we dont add this frame as an overlay of itself
1465 if (toAdd.Frame == this) {
1466 return false;
1467 }
1468
1469 _vectors.add(toAdd);
1470
1471 // Items must be notified that they have been added or removed from this
1472 // frame via the vector...
1473 int maxX = 0;
1474 int maxY = 0;
1475
1476 HighlightMode mode = toAdd.Source.getHighlightMode();
1477 if (mode != HighlightMode.None) {
1478 mode = HighlightMode.Connected;
1479 }
1480
1481 Colour highlightColor = toAdd.Source.getHighlightColor();
1482
1483 for (Item i : ItemUtils.CopyItems(toAdd.Frame.getVectorItems(), toAdd)) {
1484 i.onParentStateChanged(new ItemParentStateChangedEvent(this, ItemParentStateChangedEvent.EVENT_TYPE_ADDED_VIA_OVERLAY, toAdd.permission));
1485 i.setEditTarget(toAdd.Source);
1486 i.setHighlightModeAndColour(mode, highlightColor);
1487 _vectorItems.add(i);
1488 i.invalidateAll();
1489 i.invalidateFill();
1490
1491 // Get the right most x and bottom most y pos
1492 int itemRight = i.getX() + i.getBoundsWidth();
1493 if (itemRight > maxX) {
1494 maxX = itemRight;
1495 }
1496
1497 int itemBottom = i.getY() + i.getBoundsHeight();
1498 if (itemBottom > maxY) {
1499 maxY = itemBottom;
1500 }
1501 }
1502
1503 toAdd.setSize(maxX, maxY);
1504
1505 return true;
1506 }
1507
1508 public Collection<Vector> getVectors()
1509 {
1510 Collection<Vector> l = new LinkedList<Vector>();
1511 l.addAll(_vectors);
1512 return l;
1513 }
1514
1515 public Collection<Overlay> getOverlays()
1516 {
1517 return new LinkedList<Overlay>(_overlays.keySet());
1518 }
1519
1520 /**
1521 * @return All vectors seen by this frame (including its vector's vectors).
1522 */
1523 public List<Vector> getVectorsDeep()
1524 {
1525 List<Vector> l = new LinkedList<Vector>();
1526 getVectorsDeep(l, this, new LinkedList<Frame>());
1527 return l;
1528 }
1529
1530 private boolean getVectorsDeep(List<Vector> vectors, Frame vector, List<Frame> seenVectors)
1531 {
1532 if (seenVectors.contains(vector)) {
1533 return false;
1534 }
1535
1536 seenVectors.add(vector);
1537
1538 for (Vector v : vector.getVectors()) {
1539 if (getVectorsDeep(vectors, v.Frame, seenVectors)) {
1540 vectors.add(v);
1541 }
1542 }
1543
1544 return true;
1545 }
1546
1547 public List<Overlay> getOverlaysDeep()
1548 {
1549 List<Overlay> ret = new LinkedList<Overlay>();
1550
1551 getOverlaysDeep(ret, new LinkedList<Frame>());
1552
1553 return ret;
1554 }
1555
1556 private boolean getOverlaysDeep(List<Overlay> overlays, List<Frame> seenOverlays)
1557 {
1558 if (seenOverlays.contains(this)) {
1559 return false;
1560 }
1561
1562 seenOverlays.add(this);
1563
1564 for (Overlay o : this.getOverlays()) {
1565 if (o.Frame.getOverlaysDeep(overlays, seenOverlays)) {
1566 overlays.add(o);
1567 }
1568 }
1569 return true;
1570 }
1571
1572 /**
1573 * Recursive function similar to AddAllOverlayItems.
1574 *
1575 * @param widgets
1576 * The collection the widgets will be added to
1577 * @param overlay
1578 * An "overlay" frame - this initially will be the parent frame
1579 * @param seenOverlays
1580 * Used for state in the recursion stack. Pass as an empty
1581 * (non-null) list.
1582 */
1583 public List<Widget> getAllOverlayWidgets()
1584 {
1585 List<Widget> widgets = new LinkedList<Widget>();
1586
1587 for (Overlay o : getOverlaysDeep()) {
1588 widgets.addAll(o.Frame.getInteractiveWidgets());
1589 }
1590
1591 return widgets;
1592 }
1593
1594 /**
1595 * Gets the overlay on this frame which owns the given item.
1596 *
1597 * @param item
1598 * The item - must not be null.
1599 * @return The overlay that contains the item. Null if no overlay owns the
1600 * item.
1601 */
1602 public Overlay getOverlayOwner(Item item)
1603 {
1604 if (item == null) {
1605 throw new NullPointerException("item");
1606 }
1607
1608 for (Overlay l : getOverlays()) {
1609 if (item.getParent() == l.Frame) {
1610 return l;
1611 }
1612 }
1613
1614 // TODO return the correct vector... not just the first vector matching
1615 // the vector frame
1616 for (Vector v : getVectors()) {
1617 if (item.getParent() == v.Frame) {
1618 return v;
1619 }
1620 }
1621
1622 return null;
1623 }
1624
1625 public void clearVectors()
1626 {
1627 _vectors.clear();
1628
1629 for (Item i : _vectorItems) { // TODO: Rethink where this should live
1630 i.invalidateAll();
1631 i.invalidateFill();
1632 }
1633 _vectorItems.clear();
1634
1635 }
1636
1637 protected boolean removeVector(Vector toRemove)
1638 {
1639 if (!_vectors.remove(toRemove)) {
1640 return false;
1641 }
1642
1643 for (Item i : toRemove.Frame.getVectorItems()) {
1644 i.invalidateAll();
1645 i.invalidateFill();
1646 _vectorItems.remove(i);
1647 i.onParentStateChanged(new ItemParentStateChangedEvent(this,
1648 ItemParentStateChangedEvent.EVENT_TYPE_REMOVED_VIA_OVERLAY,
1649 toRemove.permission));
1650
1651 }
1652
1653 return true;
1654 }
1655
1656 public void clearOverlays()
1657 {
1658 for (Overlay o : _overlays.keySet()) {
1659 for (Item i : o.Frame.getSortedItems()) {
1660 i.onParentStateChanged(new ItemParentStateChangedEvent(
1661 this,
1662 ItemParentStateChangedEvent.EVENT_TYPE_REMOVED_VIA_OVERLAY,
1663 o.permission));
1664 }
1665 }
1666 _overlayItems.clear();
1667 _overlays.clear();
1668 assert (_overlays.isEmpty());
1669 }
1670
1671 protected boolean removeOverlay(Frame f)
1672 {
1673 for (Overlay o : _overlays.keySet()) {
1674 if (o.Frame == f) {
1675 _overlays.remove(o);
1676
1677 for (Item i : f.getSortedItems()) {
1678 _overlayItems.remove(i);
1679 i.onParentStateChanged(new ItemParentStateChangedEvent(
1680 this,
1681 ItemParentStateChangedEvent.EVENT_TYPE_REMOVED_VIA_OVERLAY,
1682 o.permission));
1683 }
1684
1685 return true;
1686 }
1687 }
1688
1689 return false;
1690 }
1691
1692 public void addAllVectors(List<Vector> vectors)
1693 {
1694 for (Vector v : vectors) {
1695 addVector(v);
1696 }
1697 }
1698
1699 public void addAllOverlays(Collection<Overlay> overlays)
1700 {
1701 for (Overlay o : overlays) {
1702 addOverlay(o);
1703 }
1704 }
1705
1706 protected boolean addOverlay(Overlay toAdd)
1707 {
1708 // make sure we dont add this frame as an overlay of itself
1709 if (toAdd.Frame == this) {
1710 return false;
1711 }
1712
1713 // Dont add the overlay if there is already one for this frame
1714 if (_overlays.values().contains(toAdd.Frame)) {
1715 return false;
1716 }
1717
1718 // Add the overlay to the map of overlays on this frame
1719 _overlays.put(toAdd, toAdd.Frame);
1720
1721 // Add all the overlays from the overlay frame to this frame
1722 // TODO: Can this cause a recursion loop? If A and B are overlays of each other? cts16
1723 for (Overlay o : toAdd.Frame.getOverlays()) {
1724 addOverlay(o);
1725 }
1726
1727 // Add all the vectors from the overlay frame to this frame
1728 for (Vector v : toAdd.Frame.getVectors()) {
1729 addVector(v);
1730 }
1731
1732 // Now add the items for this overlay
1733 UserAppliedPermission permission = UserAppliedPermission.min(toAdd.Frame.getUserAppliedPermission(), toAdd.permission);
1734
1735 // Items must be notified that they have been added or removed from this
1736 // frame via the overlay...
1737 for (Item i : toAdd.Frame.getVisibleItems()) {
1738 i.onParentStateChanged(new ItemParentStateChangedEvent(this, ItemParentStateChangedEvent.EVENT_TYPE_ADDED_VIA_OVERLAY, permission));
1739 _overlayItems.add(i);
1740 }
1741
1742 return true;
1743 }
1744
1745 @Override
1746 public boolean equals(Object o)
1747 {
1748 if (o instanceof String) {
1749 return (String.CASE_INSENSITIVE_ORDER.compare((String) o, getName()) == 0);
1750 }
1751
1752 if (o instanceof Frame) {
1753 return getName().equals(((Frame) o).getName());
1754 }
1755
1756 return super.equals(o);
1757 }
1758
1759 /**
1760 * Merge one frames contents into another.
1761 *
1762 * @param toMergeWith
1763 */
1764 private void merge(Frame toMergeWith)
1765 {
1766 if (toMergeWith == null) {
1767 return;
1768 }
1769
1770 List<Item> copies = ItemUtils.CopyItems(toMergeWith.getSortedItems());
1771 copies.remove(toMergeWith.getNameItem());
1772
1773 for (Item i : copies) {
1774 if (i.getID() >= 0) {
1775 i.setID(this.getNextItemID());
1776 addItem(i);
1777 }
1778 }
1779 }
1780
1781 /**
1782 * This method is for merging frames or setting frame attributes via
1783 * injecting a text item into the frameName item.
1784 *
1785 * @param toMerge
1786 * @return the items that cant be merged
1787 */
1788 public List<Item> merge(List<Item> toMerge)
1789 {
1790 ArrayList<Item> remain = new ArrayList<Item>(0);
1791
1792 for (Item i : toMerge) {
1793 if (!(i instanceof Text)) {
1794 remain.add(i);
1795 } else {
1796 if (!AttributeUtils.setAttribute(this, (Text) i)) {
1797 if (i.getLink() != null) {
1798 merge(FrameIO.LoadFrame(i.getAbsoluteLink()));
1799 } else if (FrameIO.isValidFrameName(((Text) i).getFirstLine())) {
1800 // If we get hear we are merging frames
1801 merge(FrameIO.LoadFrame(((Text) i).getFirstLine()));
1802 }
1803 }
1804 }
1805 }
1806
1807 return remain;
1808 }
1809
1810 /**
1811 * Removes all non-title non-annotation items from this Frame. All removed
1812 * items are added to the backup-stack.
1813 */
1814 @Deprecated
1815 public void clearDeprecated(boolean keepAnnotations)
1816 {
1817 ItemsList newBody = new ItemsList();
1818
1819 Item title = getTitleItem();
1820
1821 if (title != null) {
1822 newBody.add(title);
1823 _body.remove(title);
1824 }
1825
1826 if (keepAnnotations) {
1827 for (Item i : _body) {
1828 if (i.isAnnotation()) {
1829 newBody.add(i);
1830 }
1831 }
1832 }
1833
1834 _body.removeAll(newBody);
1835 addToUndoDelete(_body);
1836 _body = newBody;
1837 change();
1838
1839 if (!keepAnnotations && _annotations != null) {
1840 _annotations.clear();
1841 }
1842 }
1843
1844 /**
1845 * Removes all items from the Frame except the Title Item and optionally the annotations.
1846 * All removed items are added to the backup-stack.
1847 *
1848 * @param keepAnnotations true is annotations are not to be removed from the frame.
1849 */
1850 public void clear(boolean keepAnnotations) {
1851 ItemsList body = getBody(true);
1852 ItemsList deleted = new ItemsList();
1853
1854 for (Item bodyItem: body) {
1855 boolean isAnnotationToKeep = bodyItem.isAnnotation() && keepAnnotations;
1856 boolean isFrameTitle = bodyItem.isFrameTitle();
1857 boolean isToBeRetained = isFrameTitle || isAnnotationToKeep;
1858 if (isToBeRetained) {
1859 continue;
1860 }
1861
1862 this.removeItem(bodyItem);
1863 deleted.add(bodyItem);
1864 }
1865
1866 addToUndoDelete(deleted);
1867 change();
1868
1869 if (!keepAnnotations && _annotations != null) {
1870 _annotations.clear();
1871 }
1872 }
1873
1874 /**
1875 * Creates a new text item with the given text.
1876 *
1877 * @param text
1878 * @return
1879 */
1880 public Text createNewText(String text)
1881 {
1882 Text t = createBlankText(text);
1883 t.setText(text);
1884 return t;
1885 }
1886
1887 /**
1888 * Creates a new Text Item with no text. The newly created Item is a copy
1889 * the ItemTemplate if one is present, and inherits all the attributes of
1890 * the Template
1891 *
1892 * @return The newly created Text Item
1893 */
1894 public Text createBlankText(String templateType) {
1895 File file = new File(getFramePathReal());
1896 long fileLastModified = file.lastModified();
1897 long frameLastModified = this.getLastModifyPrecise();
1898 //if (ExpReader.getVersion(getFramePathReal()) > this._version) {
1899 if (fileLastModified > frameLastModified) {
1900 GestureType refreshGestureType = StandardGestureActions.getInstance().gestureType(StandardGestureType.REFRESH);
1901 RefreshGestureData refreshGestureData = new RefreshGestureData(true, false);
1902 try {
1903 StandardGestureActions.getInstance().onGesture(new Gesture(refreshGestureType, refreshGestureData));
1904 EcosystemManager.getMiscManager().beep();
1905 } catch (NullPointerException e) {
1906 //Detected more recent data on file system than on Frame in memory. Unfortunately not in a position to cause a refresh.
1907 }
1908 }
1909
1910 SessionStats.CreatedText();
1911 Text t;
1912
1913 if (templateType.length() == 0) {
1914 t = getItemTemplate().copy();
1915 } else {
1916 t = getItemTemplate(templateType.charAt(0));
1917 }
1918
1919 // reset attributes
1920 t.setID(getNextItemID());
1921 t.setPosition(DisplayController.getMousePosition());
1922 t.setText("");
1923 t.setParent(this);
1924
1925 // Set the width if the template doesnt have a width
1926 // Make it the width of the page
1927 // t.setMaxWidth(FrameGraphics.getMaxFrameSize().width);
1928 // if (t.getWidth() <= 0) {
1929 // String maxWidthString = getAnnotationValue("maxwidth");
1930 // int width = FrameGraphics.getMaxFrameSize().width;
1931 // if (maxWidthString != null) {
1932 // try {
1933 // width = Math.min(width, Integer.parseInt(maxWidthString));
1934 // } catch (NumberFormatException nfe) {
1935 // }
1936 // }
1937 //
1938 // t.setRightMargin(width);
1939 // }
1940 addItem(t);
1941 return t;
1942 }
1943
1944 /**
1945 * Returns the data associated with the frame.
1946 * @return
1947 */
1948 public List<String> getData() {
1949 return _frameData;
1950 }
1951
1952 /**
1953 * Adds a piece of data to be associated with the frame.
1954 * @param dataItem
1955 */
1956 public void addToData(String dataItem) {
1957 if (dataItem != null) {
1958 if (_frameData == null)
1959 _frameData = new LinkedList<String>();
1960 _frameData.add(dataItem);
1961 }
1962 }
1963
1964 /**
1965 * Returns the path (String) to the .exp file that this Frame represents.
1966 * This follows redirects, meaning that it provides the actual file from which
1967 * the frames data is drawn from.
1968 * @return The path to the .exp file that this Frame represents
1969 * @see getFramePathLogical
1970 * @see getFramesetPath
1971 */
1972 public String getFramePathReal() {
1973 String framesetPath = getFramesetPath();
1974 String redirect = ExpReader.redirectTo(getFramePathLogical());
1975
1976 if (redirect == null) {
1977 return getFramePathLogical();
1978 }
1979
1980 while (redirect != null) {
1981 framesetPath = getFramesetPath() + redirect;
1982 redirect = ExpReader.redirectTo(redirect);
1983 }
1984 return framesetPath;
1985 }
1986
1987 /**
1988 * Returns the path (String) to the .exp file that this Frame represents.
1989 * Does not follow redirects, opting to instead provide the logical path to this file.
1990 * @return The path to the .exp file that this Frame represents
1991 * @see getFramePathReal
1992 * @see getFramesetPath
1993 */
1994 public String getFramePathLogical() {
1995 return getFramesetPath() + this.getNumber() + ExpReader.EXTENTION;
1996 }
1997
1998 /**
1999 * Returns the path (String) to the frameset directory that the file that this Frame represents is contained within.
2000 * @return The path to this Frames frameset directory
2001 * @see getFramesetPathLogical
2002 * @see getFramesetPathReal
2003 */
2004 public String getFramesetPath() {
2005 return Paths.get(this.getPath()).resolve(this.getFramesetName()).toString() + File.separator;
2006 }
2007
2008 public Item createDot() {
2009 Item dot = new Dot(DisplayController.getMouseX(), DisplayController.getMouseY(), getNextItemID());
2010
2011 Item template = getTemplate(_dotTemplate, ItemUtils.TAG_DOT_TEMPLATE);
2012 float thickness = template.getThickness();
2013 if (thickness > 0) {
2014 dot.setThickness(template.getThickness());
2015 }
2016 if (template.getLinePattern() != null) {
2017 dot.setLinePattern(template.getLinePattern());
2018 }
2019 dot.setColor(template.getColor());
2020 dot.setFillColor(template.getFillColor());
2021 // reset attributes
2022 dot.setParent(this);
2023 dot.setOwner(template.getOwner());
2024 return dot;
2025 }
2026
2027 private Text getTemplate(Text defaultTemplate, int templateTag)
2028 {
2029 Text t = null;
2030
2031 // check for an updated template...
2032 for (Item i : this.getSortedItems()) {
2033 if (ItemUtils.startsWithTag(i, templateTag)) {
2034 t = (Text) i;
2035 break;
2036 }
2037 }
2038
2039 if (t == null) {
2040 if (defaultTemplate == null) {
2041 return null;
2042 }
2043
2044 t = defaultTemplate;
2045 }
2046
2047 // If the item is linked apply any attribute pairs on the child frame
2048 String link = t.getAbsoluteLink();
2049
2050 // need to get link first because copy doesnt copy the link
2051 t = t.copy();
2052 // If the template does not have a owner then it should be set to the current user.
2053 if (t.getOwner() == null) {
2054 t.setOwner(UserSettings.UserName.get());
2055 }
2056 t.setTooltip(null);
2057 if (link != null) {
2058 t.setLink(null);
2059 Frame childFrame = FrameIO.LoadFrame(link);
2060 if (childFrame != null) {
2061 // read in attribute value pairs
2062 for (Text attribute : childFrame.getBodyTextItems(false)) {
2063 AttributeUtils.setAttribute(t, attribute);
2064 }
2065 }
2066 }
2067 return t;
2068 }
2069
2070 /**
2071 * TODO: Comment. cts16
2072 * TODO: Remove magic constants. cts16
2073 */
2074 public Text getItemTemplate(char firstChar)
2075 {
2076 switch (firstChar) {
2077 case '@':
2078 return getAnnotationTemplate();
2079 case '/':
2080 case '#':
2081 return getCodeCommentTemplate();
2082 default:
2083 return getItemTemplate();
2084 }
2085 }
2086
2087 public Text createNewText()
2088 {
2089 return createNewText("");
2090 }
2091
2092 public Text addText(int x, int y, String text, String action)
2093 {
2094 Text t = createNewText(text);
2095 t.setPosition(x, y);
2096 t.addAction(action);
2097 return t;
2098 }
2099
2100 public Text addText(int x, int y, String text, String action, String link)
2101 {
2102 Text t = addText(x, y, text, action);
2103 t.setLink(link);
2104 return t;
2105 }
2106
2107 public Dot addDot(int x, int y)
2108 {
2109 Dot d = new Dot(x, y, getNextItemID());
2110 addItem(d);
2111 return d;
2112 }
2113
2114 /**
2115 * Adds a rectangle to the frame
2116 *
2117 * @param x
2118 * X coordinate of the top-left corner of the rectangle
2119 * @param y
2120 * Y coordinate of the top-left corner of the rectangle
2121 * @param width
2122 * Width of the rectangle
2123 * @param height
2124 * Height of the rectangle
2125 * @param borderThickness
2126 * Thickness, in pixels, of the rectangle's border/outline
2127 * @param borderColor
2128 * Color of the rectangle's border/outline
2129 * @param fillColor
2130 * Color to fill the rectangle with
2131 */
2132 public List<Item> addRectangle(int x, int y, int width, int height, float borderThickness, Colour borderColor, Colour fillColor)
2133 {
2134 List<Item> rectComponents = new ArrayList<Item>();
2135 Item[] corners = new Item[4];
2136
2137 // Top Left
2138 corners[0] = this.createDot();
2139 corners[0].setPosition(x, y);
2140
2141 // Top Right
2142 corners[1] = this.createDot();
2143 corners[1].setPosition(x + width, y);
2144
2145 // Bottom Right
2146 corners[2] = this.createDot();
2147 corners[2].setPosition(x + width, y + height);
2148
2149 // Bottom Left
2150 corners[3] = this.createDot();
2151 corners[3].setPosition(x, y + height);
2152
2153 // Add corners to the collection and setting their attributes
2154 for (int i = 0; i < corners.length; i++) {
2155 corners[i].setThickness(borderThickness);
2156 corners[i].setColor(borderColor);
2157 corners[i].setFillColor(fillColor);
2158 rectComponents.add(corners[i]);
2159 }
2160
2161 // create lines between the corners
2162 rectComponents.add(new Line(corners[0], corners[1], this.getNextItemID()));
2163 rectComponents.add(new Line(corners[1], corners[2], this.getNextItemID()));
2164 rectComponents.add(new Line(corners[2], corners[3], this.getNextItemID()));
2165 rectComponents.add(new Line(corners[3], corners[0], this.getNextItemID()));
2166
2167 // Add constraints between each corner
2168 new Constraint(corners[0], corners[1], this.getNextItemID(), Constraint.HORIZONTAL);
2169 new Constraint(corners[2], corners[3], this.getNextItemID(), Constraint.HORIZONTAL);
2170 new Constraint(corners[1], corners[2], this.getNextItemID(), Constraint.VERTICAL);
2171 new Constraint(corners[3], corners[0], this.getNextItemID(), Constraint.VERTICAL);
2172
2173 List<Item> rect = new ArrayList<Item>(rectComponents);
2174 this.addAllItems(rectComponents);
2175 StandardGestureActions.anchor(rectComponents);
2176 return rect;
2177 }
2178
2179 public boolean isSaved()
2180 {
2181 return _saved;
2182 }
2183
2184 public void setSaved()
2185 {
2186 _saved = true;
2187 _change = false;
2188 }
2189
2190 public static boolean rubberbandingLine()
2191 {
2192 return FreeItems.getInstance().size() == 2 &&
2193 (FreeItems.getInstance().get(0) instanceof Line || FreeItems.getInstance().get(1) instanceof Line);
2194 }
2195
2196 /**
2197 * Tests if an item is a non title, non frame name, non special annotation
2198 * text item.
2199 *
2200 * @param it
2201 * the item to be tested
2202 * @return true if the item is a normal text item
2203 */
2204 public boolean isNormalTextItem(Item it)
2205 {
2206 if (it instanceof Text && it != getTitleItem() && it != _frameName && !((Text) it).isSpecialAnnotation()) {
2207 return true;
2208 }
2209
2210 return false;
2211 }
2212
2213 /**
2214 * Moves the mouse to the end of the text item with a specified index.
2215 *
2216 * @param index
2217 */
2218 public boolean moveMouseToTextItem(int index)
2219 {
2220 List<Item> items = getSortedItems();
2221 int itemsFound = 0;
2222 for (int i = 0; i < items.size(); i++) {
2223 Item it = items.get(i);
2224 if (isNormalTextItem(it)) {
2225 itemsFound++;
2226 }
2227 if (itemsFound > index) {
2228 DisplayController.setCursorPosition(((Text) it).getParagraphEndPosition().getX(), it.getY());
2229 DisplayController.resetCursorOffset();
2230 DisplayController.requestRefresh(true);
2231 return true;
2232 }
2233 }
2234
2235 return false;
2236 }
2237
2238 /**
2239 * Searches for an annotation item called start to be used as the default
2240 * cursor location when TDFC occurs.
2241 *
2242 * TODO: Remove magic constants. cts16
2243 */
2244 public boolean moveMouseToDefaultLocation()
2245 {
2246 List<Item> items = getSortedItems();
2247
2248 for (Item it : items) {
2249 if (it instanceof Text) {
2250 Text t = (Text) it;
2251 if (t.getText().toLowerCase().startsWith("@start") || t.getText().toLowerCase().equals("@start:")) {
2252 // Used to allow users the option of putting an initial
2253 // bullet after the @start
2254 // This was replaced by width
2255 // t.stripFirstWord();
2256 t.setText("");
2257
2258 if (t.getText().equals("")) {
2259 DisplayController.getCurrentFrame().removeItem(t);
2260 }
2261
2262 if (!FreeItems.hasItemsAttachedToCursor()) {
2263 DisplayController.setCursorPosition(((Text) it).getParagraphEndPosition());
2264 DisplayController.resetCursorOffset();
2265 }
2266
2267 DisplayController.requestRefresh(true);
2268
2269 return true;
2270 }
2271 }
2272 }
2273
2274 return false;
2275 }
2276
2277 /**
2278 * Gets the file name that actions should use to export files created by
2279 * running actions from this frame.
2280 *
2281 * @return the fileName if the frame contains an '@file' tag. Returns the
2282 * name of the frame if the tag isnt on the frame.
2283 */
2284 public String getExportFileName()
2285 {
2286 String fileName = getExportFileTagValue();
2287
2288 if (fileName == null) {
2289 fileName = getTitle();
2290
2291 if (fileName == null) {
2292 fileName = getName();
2293 }
2294 }
2295
2296 return fileName;
2297 }
2298
2299 public void toggleBackgroundColor()
2300 {
2301 setBackgroundColor(ColorUtils.getNextColor(_background, TemplateSettings.BackgroundColorWheel.get(), null));
2302 }
2303
2304 public void setName(String frameset, int i)
2305 {
2306 setFrameset(frameset);
2307 setFrameNumber(i);
2308 }
2309
2310 /**
2311 * Sets the item permissions to match the protection for the frame.
2312 * No longer sets item permissions, since items can have their own permissions now (but still default to frame permissions)
2313 *
2314 */
2315 public void refreshItemPermissions(UserAppliedPermission maxPermission)
2316 {
2317 if(_frameName == null) {
2318 return;
2319 }
2320
2321 UserAppliedPermission permission = UserAppliedPermission.min(maxPermission, getUserAppliedPermission());
2322
2323 switch (permission) {
2324 case none:
2325 _frameName.setBackgroundColor(FRAME_NAME_BACKGROUND_COLOUR_FOR_PERMISSION_NONE);
2326 break;
2327 case followLinks:
2328 _frameName.setBackgroundColor(FRAME_NAME_BACKGROUND_COLOUR_FOR_PERMISSION_FOLLOW_LINKS);
2329 break;
2330 case copy:
2331 _frameName.setBackgroundColor(FRAME_NAME_BACKGROUND_COLOUR_FOR_PERMISSION_COPY);
2332 break;
2333 case createFrames:
2334 _frameName.setBackgroundColor(FRAME_NAME_BACKGROUND_COLOUR_FOR_PERMISSION_CREATE_FRAMES);
2335 break;
2336 case full:
2337 _frameName.setBackgroundColor(FRAME_NAME_BACKGROUND_COLOUR_FOR_PERMISSION_FULL);
2338 break;
2339 default:
2340 assert (false);
2341 break;
2342 }
2343
2344 for (Overlay o : getOverlays()) {
2345 for(Item i : o.Frame.getBody(false)) {
2346 i.setOverlayPermission(o.permission);
2347 }
2348 o.Frame.refreshItemPermissions(o.permission);
2349 }
2350 }
2351
2352 public boolean isTestFrame()
2353 {
2354 Text title = getTitleItem();
2355 if (title == null) {
2356 return false;
2357 }
2358 String action = title.getFirstAction();
2359 if (action == null) {
2360 return false;
2361 }
2362 action = action.toLowerCase();
2363 return action.startsWith(Simple.RUN_FRAME_ACTION) || action.startsWith(Simple.DEBUG_FRAME_ACTION);
2364 }
2365
2366 public void setActiveTime(String activeTime)
2367 {
2368 try {
2369 _activeTime = new Time(Time.valueOf(activeTime).getTime() + 12 * 60 * 60 * 1000);
2370 } catch (Exception e) {
2371 _activeTime = new Time(0);
2372 }
2373 }
2374
2375 public void setActiveTime(Time activeTime)
2376 {
2377 _activeTime = activeTime;
2378 }
2379
2380 public void setDarkTime(Time darkTime)
2381 {
2382 _darkTime = darkTime;
2383 }
2384
2385 public void setDarkTime(String darkTime)
2386 {
2387 try {
2388 _darkTime = new Time(Time.valueOf(darkTime).getTime() + 12 * 60 * 60 * 1000);
2389 } catch (Exception e) {
2390 _darkTime = new Time(0);
2391 }
2392 }
2393
2394 /**
2395 * Returns null if their is no backup frame or if it is invalid.
2396 *
2397 * @return the backup frame for this frame
2398 */
2399 public Frame getBackupFrame()
2400 {
2401 Text backupTag = _annotations.get("old");
2402 if (backupTag == null) {
2403 return null;
2404 }
2405
2406 // TODO want another way to deal with updating of annotations items
2407 // without F12 refresh
2408 // Reparse the frame if annotation item has been modified
2409 String[] processedText = backupTag.getProcessedText();
2410 if (processedText == null) {
2411 // Reparse the frame if this item has not yet been parsed
2412 FrameUtils.Parse(this);
2413 return getBackupFrame();
2414 }
2415
2416 // Now return the name of the backed up frame
2417 String link = backupTag.getAbsoluteLink();
2418 if (link == null || link.equalsIgnoreCase(getName())) {
2419 return null;
2420 }
2421
2422 Frame backup = FrameIO.LoadFrame(link);
2423 return backup;
2424 }
2425
2426 public Time getDarkTime()
2427 {
2428 return _darkTime;
2429 }
2430
2431 public Time getActiveTime()
2432 {
2433 return _activeTime;
2434 }
2435
2436 /**
2437 * Gets the number of backed up versions of this frame are saved plus 1 for
2438 * this frame.
2439 *
2440 * @return the number of frames in the backed up comet
2441 */
2442 public int getCometLength()
2443 {
2444 Frame backup = getBackupFrame();
2445 return 1 + (backup == null ? 0 : backup.getCometLength());
2446 }
2447
2448 public void addAnnotation(Text item)
2449 {
2450 if (_annotations == null) {
2451 _annotations = new HashMap<String, Text>();
2452 }
2453
2454 // Check if this item has already been processed
2455 String[] tokens = item.getProcessedText();
2456 if (tokens != null) {
2457 if (tokens.length > 0) {
2458 _annotations.put(tokens[0], item);
2459 }
2460 return;
2461 }
2462
2463 String text = item.getText().trim();
2464 assert (text.charAt(0) == '@');
2465
2466 // Ignore annotations with spaces after the tag symbol
2467 if (text.length() < 2 || !Character.isLetter(text.charAt(1))) {
2468 item.setProcessedText(new String[0]);
2469 return;
2470 }
2471
2472 // The separator char must come before the first non letter otherwise we
2473 // ignore the annotation item
2474 for (int i = 2; i < text.length(); i++) {
2475 char ch = text.charAt(i);
2476 if (!Character.isLetterOrDigit(ch)) {
2477 // Must have an attribute value pair
2478 if (ch == AttributeValuePair.SEPARATOR_CHAR) {
2479 // Get the attribute
2480 String attribute = text.substring(1, i).toLowerCase();
2481 String value = "";
2482 if (text.length() > 1 + i) {
2483 value = text.substring(i + 1).trim();
2484 }
2485 item.setProcessedText(new String[] { attribute, value });
2486 _annotations.put(attribute, item);
2487 return;
2488 } else {
2489 item.setProcessedText(new String[0]);
2490 return;
2491 }
2492 }
2493 }
2494
2495 // If it was nothing but letters and digits save the tag
2496 String lowerCaseText = text.substring(1).toLowerCase();
2497 item.setProcessedText(new String[] { lowerCaseText });
2498 _annotations.put(lowerCaseText, item);
2499 }
2500
2501 public boolean hasAnnotation(String annotation)
2502 {
2503 if (_annotations == null) {
2504 refreshAnnotationList();
2505 }
2506
2507 return _annotations.containsKey(annotation.toLowerCase());
2508 }
2509
2510 /**
2511 * Returns the annotation value in full case.
2512 *
2513 * @param annotation
2514 * the annotation to retrieve the value of.
2515 * @return the annotation item value in full case or null if the annotation
2516 * is not on the frame or has no value.
2517 */
2518 public String getAnnotationValue(String annotation)
2519 {
2520 if (_annotations == null) {
2521 refreshAnnotationList();
2522 }
2523
2524 Text text = _annotations.get(annotation.toLowerCase());
2525 if (text == null) {
2526 return null;
2527 }
2528
2529 String[] tokens = text.getProcessedText();
2530
2531 if (tokens != null && tokens.length > 1) {
2532 return tokens[1];
2533 }
2534
2535 return null;
2536 }
2537
2538 public void clearAnnotations()
2539 {
2540 _annotations = null;
2541 }
2542
2543 public List<Item> getVisibleItems()
2544 {
2545 return getSortedItems(true);
2546 }
2547
2548 private void refreshAnnotationList()
2549 {
2550 if (_annotations == null) {
2551 _annotations = new HashMap<String, Text>();
2552 } else {
2553 _annotations.clear();
2554 }
2555
2556 for (Text text : getTextItems()) {
2557 if (text.isAnnotation()) {
2558 addAnnotation(text);
2559 }
2560 }
2561 }
2562
2563 public Collection<Text> getAnnotationItems()
2564 {
2565 if (_annotations == null) {
2566 refreshAnnotationList();
2567 }
2568
2569 return _annotations.values();
2570 }
2571
2572 /**
2573 * Gets a list of items to be saved to file by text file writers.
2574 *
2575 * @return the list of items to be saved to a text file
2576 */
2577 /*public List<Item> getItemsToSave() {
2578 if (!_sorted) {
2579 Collections.sort(_body);
2580 _sorted = true;
2581 }
2582
2583 // iWidgets are handled specially since 8 items are written as one
2584 Collection<Widget> seenWidgets = new LinkedHashSet<Widget>();
2585
2586 List<Item> toSave = new ArrayList<Item>();
2587
2588 for (Item i : _body) {
2589 if (i == null || i.dontSave()) {
2590 continue;
2591 }
2592
2593 // Ensure only one of the WidgetCorners represent a single widget
2594 if (i instanceof WidgetCorner) {
2595 Widget iw = ((WidgetCorner) i).getWidgetSource();
2596 if (seenWidgets.contains(iw)) {
2597 continue;
2598 }
2599 seenWidgets.add(iw);
2600 toSave.add(iw.getSource());
2601 } else if (i instanceof XRayable) {
2602 XRayable x = (XRayable) i;
2603 toSave.addAll(x.getItemsToSave());
2604 // Circle centers are items with attached enclosures
2605 } else if (i.hasEnclosures()) {
2606 continue;
2607 } else {
2608 toSave.add(i);
2609 }
2610 }
2611
2612 for (Vector v : getVectors()) {
2613 toSave.add(v.Source);
2614 }
2615
2616 return toSave;
2617 }*/
2618
2619 public List<Item> getItemsToSave() {
2620 return getItemsToSave(BodyType.PrimaryBody);
2621 }
2622
2623 /**
2624 * Returns a list of items for the specified BodyType.
2625 *
2626 * Asking for the primary or surrogate items gives you exactly those.
2627 *
2628 * Asking for the body items is a weird case because the body list is
2629 * transitory. Therefore, when asking for the body items, this
2630 * function assumes that you want all items, reguardless of if they
2631 * are primaries or surrogates. As of 20/08/2019, there are no places
2632 * in the code that asks for the body items to save.
2633 * @param type
2634 * @return
2635 */
2636 public List<Item> getItemsToSave(BodyType type) {
2637 assert(!type.equals(BodyType.BodyDisplay));
2638 switch (type) {
2639 case SurrogateBody:
2640 return getItemsToSave(_surrogateItemsBody);
2641 case BodyDisplay:
2642 return getItemsToSave(new ItemsList(getAllFrameItemsRaw()));
2643 case PrimaryBody:
2644 default:
2645 return getItemsToSave(_primaryItemsBody);
2646 }
2647 }
2648
2649 private List<Item> getItemsToSave(ItemsList body) {
2650 body.sort();
2651 List<Widget> seenWidgets = new ArrayList<Widget>();
2652
2653 List<Item> toSave = new ArrayList<Item>();
2654
2655 for (Item item: body) {
2656
2657 if (item == null || item.dontSave()) {
2658 continue;
2659 }
2660
2661 if (item instanceof WidgetCorner) {
2662 // Save the widget source.
2663 // Each widget has multiple WidgetCorner's..ignore them if we already have the source.
2664 Widget iw = ((WidgetCorner) item).getWidgetSource();
2665 if (seenWidgets.contains(iw)) { continue; }
2666 seenWidgets.add(iw);
2667 toSave.add(iw.getSource());
2668 } else if (item instanceof XRayable) {
2669 // XRayable Items have their sources saved.
2670 XRayable x = (XRayable) item;
2671 toSave.addAll(x.getItemsToSave());
2672 } else if (item.hasEnclosures()) {
2673 // Deals with Circle objects only?
2674 continue;
2675 } else {
2676 toSave.add(item);
2677 }
2678 }
2679
2680 return toSave;
2681 }
2682
2683 public Collection<Item> getOverlayItems()
2684 {
2685 return _overlayItems;
2686 }
2687
2688 /**
2689 * Returns true if this frame has and overlays for the specified frame.
2690 *
2691 * @param frame
2692 * @return
2693 */
2694 public boolean hasOverlay(Frame frame)
2695 {
2696 return _overlays.containsValue(frame);
2697 }
2698
2699 public Collection<Item> getAllItems() {
2700 ItemsList allItems = new ItemsList(getBody(true));
2701
2702 allItems.addAll(_overlayItems);
2703 allItems.addAll(_vectorItems);
2704 return allItems.underlying();
2705 }
2706
2707 public Collection<Item> getVectorItems()
2708 {
2709 Collection<Item> vectorItems = new LinkedHashSet<Item>(_vectorItems);
2710 vectorItems.addAll(getNonAnnotationItems(false));
2711 return vectorItems;
2712 }
2713
2714 /**
2715 * Gets a list of all the text items on the frame.
2716 *
2717 * @return
2718 */
2719 public Collection<Text> getTextItems()
2720 {
2721 Collection<Text> textItems = new ArrayList<Text>();
2722
2723 for (Item i : getSortedItems(true)) {
2724 // only add up normal body text items
2725 if ((i instanceof Text)) {
2726 textItems.add((Text) i);
2727 }
2728 }
2729
2730 return textItems;
2731 }
2732
2733 public Text getAnnotation(String annotation)
2734 {
2735 if (_annotations == null) {
2736 refreshAnnotationList();
2737 }
2738
2739 return _annotations.get(annotation.toLowerCase());
2740 }
2741
2742 public void recalculate()
2743 {
2744 for (Item i : getSortedItems()) {
2745 if (i.hasFormula() && !i.isAnnotation()) {
2746 i.calculate(i.getFormula());
2747 }
2748 }
2749 }
2750
2751 public void removeObserver(FrameObserver observer)
2752 {
2753 _observers.remove(observer);
2754 }
2755
2756 public void addObserver(FrameObserver observer)
2757 {
2758 _observers.add(observer);
2759 }
2760
2761 public void clearObservers()
2762 {
2763 for (FrameObserver fl : _observers) {
2764 fl.removeSubject(this);
2765 }
2766
2767 // The frame listener will call the frames removeListener method
2768 assert (_observers.size() == 0);
2769 }
2770
2771 public Collection<Text> getNonAnnotationText(boolean removeTitle)
2772 {
2773 Collection<Text> items = new LinkedHashSet<Text>();
2774
2775 for (Item i : getSortedItems(true)) {
2776 // only add up normal body text items
2777 if (i instanceof Text && !i.isAnnotation()) {
2778 items.add((Text) i);
2779 }
2780 }
2781
2782 if (removeTitle) {
2783 items.remove(getTitleItem());
2784 }
2785
2786 return items;
2787 }
2788
2789 @Deprecated
2790 public void disposeDeprecated() {
2791 clearObservers();
2792
2793 for (Item i : _body) {
2794 i.dispose();
2795 }
2796
2797 _frameName.dispose();
2798 _body = null;
2799 _frameName = null;
2800 }
2801
2802 /**
2803 * Disposes off all references associated with this frame.
2804 * This operation is NOT REVERSEABLE through the history.
2805 */
2806 public void dispose() {
2807 clearObservers();
2808
2809 List<Item> allFrameItems = getAllFrameItemsRaw();
2810
2811 for (Item i: allFrameItems) {
2812 i.dispose();
2813 }
2814
2815 _frameName.dispose();
2816 _frameName = null;
2817 getBody(false).clear();
2818 getPrimaryBody().clear();
2819 getSurrogateBody().clear();
2820 }
2821
2822 public void parse()
2823 {
2824 for (Overlay o : getOverlays()) {
2825 o.Frame.parse();
2826 }
2827
2828 // Must parse the frame AFTER the overlays
2829 FrameUtils.Parse(this);
2830 }
2831
2832 public void setPath(String path)
2833 {
2834 this.path = path;
2835 }
2836
2837 public String getPath()
2838 {
2839 return path;
2840 }
2841
2842 public void setLocal(boolean isLocal)
2843 {
2844 this._isLocal = isLocal;
2845 }
2846
2847 public boolean isLocal()
2848 {
2849 return _isLocal;
2850 }
2851
2852 public String getExportFileTagValue()
2853 {
2854 return getAnnotationValue("file");
2855 }
2856
2857 public void assertEquals(Frame frame2)
2858 {
2859 // Check that all the items on the frame are the same
2860 List<Item> items1 = getVisibleItems();
2861 List<Item> items2 = frame2.getVisibleItems();
2862
2863 if (items1.size() != items2.size()) {
2864 throw new UnitTestFailedException(items1.size() + " items", items2.size() + " items");
2865 } else {
2866 for (int i = 0; i < items1.size(); i++) {
2867 Item i1 = items1.get(i);
2868 Item i2 = items2.get(i);
2869 String s1 = i1.getText();
2870 String s2 = i2.getText();
2871 if (!s1.equals(s2)) {
2872 throw new UnitTestFailedException(s1, s2);
2873 }
2874 }
2875 }
2876 }
2877
2878 public boolean hasObservers()
2879 {
2880 return _observers != null && _observers.size() > 0;
2881 }
2882
2883 public Collection<Item> getBodyItemsWithInsufficientPermissions() {
2884 return _bodyHiddenDueToPermissions.underlying();
2885 }
2886
2887 public void moveItemToBodyHiddenDueToPermission(final Item i) {
2888 getBody(true).remove(i);
2889 _bodyHiddenDueToPermissions.add(i);
2890 }
2891
2892 public void moveItemFromBodyHiddenDueToPermission(Item i, PermissionTriple newPermission) {
2893 if (_bodyHiddenDueToPermissions.contains(i)) {
2894 _bodyHiddenDueToPermissions.remove(i);
2895 i.setPermission(newPermission);
2896 getBody(true).add(i);
2897 }
2898 }
2899
2900 public Collection<? extends Item> getInteractableItems() {
2901 /*
2902 * TODO: Cache the interactableItems list so we dont have to recreate it
2903 * every time this method is called
2904 */
2905 if (_interactableItems.size() > 0) {
2906 return _interactableItems;
2907 }
2908
2909 for (Item i : getBody(false)) {
2910 if (i == null) {
2911 continue;
2912 }
2913 if (i.isVisible()) {
2914 _interactableItems.add(i);
2915 } else {
2916 Collection<? extends XRayable> enclosures = i.getEnclosures();
2917 if (enclosures != null && !enclosures.isEmpty()) {
2918 Iterator<? extends XRayable> iterator = enclosures.iterator();
2919 while (iterator.hasNext()) {
2920 _interactableItems.add(iterator.next());
2921 }
2922 }
2923 }
2924 }
2925
2926 for (Item i : _overlayItems) {
2927 if (i.hasPermission(UserAppliedPermission.followLinks)) {
2928 _interactableItems.add(i);
2929 }
2930 }
2931
2932 for (Item i : _vectorItems) {
2933 if (i.hasPermission(UserAppliedPermission.none)) {
2934 _interactableItems.add(i);
2935 }
2936 }
2937
2938 return _interactableItems;
2939 }
2940
2941 public String getEncryptionLabel() {
2942 return _encryptionLabel;
2943 }
2944
2945 public void setEncryptionLabel(String label) {
2946 this.setChanged(true);
2947 _encryptionLabel = label;
2948 }
2949
2950 public EncryptionPermissionTriple getEncryptionPermission() {
2951 return _encPermissionTriple;
2952 }
2953
2954 public void setEncryptionPermission(EncryptionPermissionTriple p) {
2955 _encPermissionTriple = p;
2956 }
2957
2958 public String getGroup() {
2959 return _groupFrameName;
2960 }
2961
2962 public void setGroup(String _groupFrame) {
2963 this._groupFrameName = _groupFrame;
2964 this._groupFrame = null;
2965 }
2966
2967 public Frame getGroupFrame() {
2968 if (this._groupFrame != null) {
2969 return this._groupFrame;
2970 } else {
2971 this._groupFrame = FrameIO.LoadFrame(this._groupFrameName + 1, FrameIO.GROUP_PATH);
2972 return this._groupFrame;
2973 }
2974 }
2975
2976 public void setGroupFrame(Frame frame) {
2977 this._groupFrame = frame;
2978 }
2979
2980 public List<String> getGroupMembers() {
2981 List<String> members = new ArrayList<String>();
2982 if (getGroupFrame() != null) {
2983 Collection<Text> textItems = getGroupFrame().getTextItems();
2984 Stream<Text> memberLists = textItems.stream().filter(t ->
2985 t.getText().toLowerCase().startsWith("@owner: ") ||
2986 t.getText().toLowerCase().startsWith("@members: "));
2987 for(Text t: memberLists.collect(Collectors.toList())) {
2988 if (t.getText().toLowerCase().startsWith("@owner: ")) {
2989 members.add(t.getText().substring(8));
2990 } else if (t.getText().toLowerCase().startsWith("@members: ")) {
2991 //10
2992 String[] split = t.getText().substring(10).split(",");
2993 for (String m: split) {
2994 members.add(m.trim());
2995 }
2996 }
2997 }
2998 }
2999 return members;
3000 }
3001
3002 public boolean hasSurrogates() {
3003 return !_surrogateItemsBody.isEmpty();
3004 }
3005
3006 private boolean meetsVisibilityRequirements(boolean requireVisible, Item i) {
3007 return i.isVisible() || (!requireVisible && !i.isDeleted());
3008 }
3009
3010 private static final class History {
3011
3012 public enum Type {
3013 deletion,
3014 movement
3015 }
3016
3017 public final ItemsList items;
3018
3019 public final Type type;
3020
3021 public History(ItemsList changed, Type type)
3022 {
3023 this.items = new ItemsList(changed);
3024 this.type = type;
3025 }
3026
3027 @Override
3028 public String toString()
3029 {
3030 return this.type.toString() + ":\n" + this.items.toString();
3031 }
3032 }
3033
3034 protected boolean hasAnnotations() {
3035 return _annotations != null && _annotations.size() > 0;
3036 }
3037
3038 public ItemsList getBody(boolean respectSurrogateMode) {
3039 if (respectSurrogateMode) { ensureBody(); }
3040 return _body;
3041 }
3042
3043 private void ensureBody() {
3044 List<String> accessibleLabelsNames = Label.getAccessibleLabelsNames(getPrimaryBody());
3045 if (!accessibleLabelsNames.equals(labelsOnLastBodySet)) {
3046 this.parse();
3047 }
3048 }
3049
3050 protected void setBody(List<Item> newBody, List<String> labelsOnSet) {
3051 _body.clear();
3052 _body.addAll(newBody);
3053 this.labelsOnLastBodySet = labelsOnSet;
3054 }
3055
3056 protected ItemsList getPrimaryBody() {
3057 return _primaryItemsBody;
3058 }
3059 protected ItemsList getSurrogateBody() {
3060 return _surrogateItemsBody;
3061 }
3062
3063
3064 /**
3065 * Gets all the items on the frame, regardless of whether they are primary or surrogate items.
3066 *
3067 * Bryce says: This function will likely only ever be used inside Frame itself, as callers from
3068 * outside Frame should care about what the state of the Frame.
3069 * @return
3070 */
3071 private List<Item> getAllFrameItemsRaw() {
3072 Collection<Item> primaries = getPrimaryBody().underlying();
3073 Collection<Item> surrogateBody = getSurrogateBody().underlying();
3074 primaries.addAll(surrogateBody);
3075 List<Item> allFrameItems = primaries.stream().distinct().collect(Collectors.toList());
3076 return allFrameItems;
3077 }
3078}
Note: See TracBrowser for help on using the repository browser.