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

Last change on this file since 1488 was 1488, checked in by davidb, 4 years ago

added helper method

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