Changeset 1415 for trunk/src/org/expeditee/gui/Frame.java
- Timestamp:
- 07/24/19 11:23:45 (5 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/expeditee/gui/Frame.java
r1414 r1415 77 77 * 78 78 */ 79 public class Frame /*implements Cloneable*/{79 public class Frame { 80 80 81 81 /** The frame number to indicate this is a virtual frame. */ … … 126 126 127 127 private boolean _isLocal = true; 128 129 private boolean _sorted = true;130 128 131 129 /** Whether the frame has changed and therefore needs saving. */ … … 139 137 private Stack<History> _redo = new Stack<History>(); 140 138 141 private List<Item> _body = new ArrayList<Item>(); 142 private List<Item> _bodyHiddenDueToPermissions = new ArrayList<Item>(); 143 private List<Item> _surrogates = new ArrayList<Item>(); 139 private ItemsList _body = new ItemsList(); 140 private ItemsList _bodyHiddenDueToPermissions = new ItemsList(); 141 private ItemsList _primaryItemsBody = new ItemsList(); 142 private ItemsList _surrogateItemsBody = new ItemsList(); 143 144 //private List<Item> _body = new ArrayList<Item>(); 145 //private List<Item> _bodyHiddenDueToPermissions = new ArrayList<Item>(); 146 //private List<Item> _primaryItemsBody = new ArrayList<Item>(); 147 //private List<Item> _surrogateItemsBody = new ArrayList<Item>(); 144 148 145 149 // for drawing purposes … … 184 188 private String _groupFrameName; 185 189 private Frame _groupFrame = null; 190 191 public enum BodyType { 192 BodyDisplay, PrimaryBody, SurrogateBody; 193 } 186 194 187 195 /** Default constructor, nothing is set. */ 188 196 public Frame() { 189 197 } 190 191 // public Frame clone() { 192 // Frame clone = new Frame(); 193 // for (Item i: this._body) { 194 // Item copy = i.copy(); 195 // copy.setID(i.getID()); 196 // clone._body.add(copy); 197 // } 198 // for(Item i: this._bodyHiddenDueToPermissions) { 199 // Item copy = i.copy(); 200 // copy.setID(i.getID()); 201 // clone._bodyHiddenDueToPermissions.add(i); 202 // } 203 // if (this._frameName != null) { 204 // clone._frameName = this._frameName.copy(); 205 // } 206 // for(Overlay key: this._overlays.keySet()) { 207 // clone._overlays.put(key, this._overlays.get(key)); 208 // } 209 // for(Vector v: this._vectors) { 210 // clone._vectors.add(v); 211 // } 212 // for (Item i: this._interactableItems) { 213 // clone._interactableItems.add(i.copy()); 214 // } 215 // for (Item i: this._overlayItems) { 216 // clone._overlayItems.add(i.copy()); 217 // } 218 // for (Item i: this._vectorItems) { 219 // clone._vectorItems.add(i.copy()); 220 // } 221 // clone._frameData = this._frameData; 222 // clone._frameset = this._frameset; 223 // clone._number = this._number; 224 // clone._version = this._version; 225 // clone._permissionTriple = this._permissionTriple; 226 // clone._owner = this._owner; 227 // clone._creationDate = this._creationDate; 228 // clone._modifiedUser = this._modifiedUser; 229 // clone._modifiedDate = this._modifiedDate; 230 // clone._modifiedDatePrecise = this._modifiedDatePrecise; 231 // clone._frozenDate = this._frozenDate; 232 // clone._background = this._background; 233 // clone._foreground = this._foreground; 234 // clone.path = this.path; 235 // clone._isLocal = this._isLocal; 236 // clone._sorted = this._sorted; 237 // clone._change = this._change; 238 // clone._saved = this._saved; 239 // clone._lineCount = this._lineCount; 240 // clone._itemCount = this._itemCount; 241 // clone._buffer = this._buffer; 242 // clone._validBuffer = this._validBuffer; 243 // clone._activeTime = (Time) this._activeTime.clone(); 244 // clone._darkTime = (Time) this._darkTime.clone(); 245 // clone._dotTemplate = this._dotTemplate.copy(); 246 // 247 // return clone; 248 // } 249 250 public boolean isReadOnly() 251 { 198 199 public boolean isReadOnly() { 252 200 return !_frameName.hasPermission(UserAppliedPermission.full) && !_protectionChanged; 253 201 } 254 202 255 public void reset() 256 { 203 public void reset() { 257 204 refreshItemPermissions(UserAppliedPermission.full); 258 205 resetDot(); … … 403 350 * @return The list of Item objects that are on this Frame. 404 351 */ 405 public List<Item> getItems(boolean visible) 406 { 407 if (!_sorted) { 408 for(int i = 0; i < _body.size();) { 409 if(_body.get(i) == null) { 410 _body.remove(i); 411 } else { 412 i++; 413 } 414 } 415 Collections.sort(_body); 416 _sorted = true; 417 } 418 352 public List<Item> getSortedItems(boolean requireVisible) { 353 ItemsList listToLoopOver = _body; 354 listToLoopOver.sort(); 355 return getItems(requireVisible, listToLoopOver); 356 } 357 358 public List<Item> getItems(boolean requireVisible, ItemsList listToLoopOver) { 419 359 List<Item> items = new ArrayList<Item>(); 420 360 421 for (Item i : _body) {361 for (Item i: listToLoopOver) { 422 362 if (i == null) { 423 363 continue; 424 364 } 425 365 426 if ( i.isVisible() || (!visible && !i.isDeleted())) {366 if (meetsVisibilityRequirements(requireVisible, i)) { 427 367 items.add(i); 428 368 } … … 431 371 return items; 432 372 } 433 373 434 374 /** TODO: Comment. cts16 */ 435 public List<Item> get Items()436 { 437 return get Items(false);375 public List<Item> getSortedItems() 376 { 377 return getSortedItems(false); 438 378 } 439 379 … … 443 383 * @return True if this frame contains i. 444 384 */ 445 public boolean containsItem(Item i) 446 { 385 public boolean containsItem(Item i) { 447 386 if (i == null) { 448 387 throw new NullPointerException("i"); 449 } 450 388 } 451 389 return _body.contains(i); 452 390 } … … 468 406 List<Text> bodyTextItems = new ArrayList<Text>(); 469 407 470 for (Item i : get Items(true)) {408 for (Item i : getSortedItems(true)) { 471 409 // only add up normal body text items 472 410 if ((i instanceof Text) && ((includeAnnotations && !((Text) i).isSpecialAnnotation()) || !i.isAnnotation()) && !i.isLineEnd()) { … … 483 421 { 484 422 Collection<Item> items = new ArrayList<Item>(); 485 for (Item i : get Items(true)) {423 for (Item i : getSortedItems(true)) { 486 424 // only add up normal body text items 487 425 if (!i.isAnnotation()) { … … 505 443 public Item getLastNonAnnotationTextItem() 506 444 { 507 List<Item> items = get Items();445 List<Item> items = getSortedItems(); 508 446 509 447 // find the last non-annotation text item … … 910 848 * The date to set as the last frozen date. 911 849 */ 912 public void setFrozenDate(String date) 850 public void setFrozenDate(String date) { 913 851 _frozenDate = date; 914 852 } 915 853 916 public void setResort(boolean value) 917 { 918 _sorted = !value; 854 public void invalidateSorted() { 855 _body.invalidateSorted(); 919 856 } 920 857 … … 925 862 * The Item to add to this Frame. 926 863 */ 927 public void addItem(Item item) 928 { 864 public void addItem(Item item) { 929 865 addItem(item, true); 930 866 } 931 867 932 public void addItem(Item item, boolean recalculate) 933 { 934 if (item == null || item.equals(_frameName) || _body.contains(item)) { 868 public void addItem(Item item, boolean recalculate) { 869 if (item == null) { return; } 870 871 addItem(item, recalculate, _body); 872 if (item.isSurrogate()) { 873 addItem(item, recalculate, _surrogateItemsBody); 874 } else { 875 addItem(item, recalculate, _primaryItemsBody); 876 } 877 } 878 879 protected void addItem(Item item, boolean recalculate, ItemsList list) { 880 if (item == null || item.equals(_frameName) || list.contains(item)) { 935 881 return; 936 882 } … … 948 894 _itemCount = Math.max(_itemCount, item.getID()); 949 895 950 _body.add(item);896 list.add(item); 951 897 item.setParent(this); 952 898 item.setFloating(false); // esnure that it is anchored … … 959 905 item.setPosition(item.getPosition()); 960 906 } 961 962 _sorted = false;907 908 list.invalidateSorted(); 963 909 964 910 // item.setMaxWidth(FrameGraphics.getMaxFrameSize().width); … … 983 929 } 984 930 985 public void addToSurrogates (Item surrogate) {986 this._surrogates.add(surrogate);931 public void addToSurrogatesOnLoad(Item surrogate, Item parent) { 932 parent.addToSurrogates(surrogate); 987 933 } 988 934 … … 991 937 boolean bReparse = false; 992 938 993 for (Item i : get Items()) {939 for (Item i : getSortedItems()) { 994 940 Integer anchorLeft = i.getAnchorLeft(); 995 941 Integer anchorRight = i.getAnchorRight(); … … 1039 985 } 1040 986 1041 public void addAllItems(Collection<Item> toAdd) 1042 { 987 public void addAllItems(Collection<Item> toAdd) { 988 addAllItems(toAdd, _body); 989 addAllItems(toAdd, _primaryItemsBody); 990 } 991 992 protected void addAllItems(Collection<Item> toAdd, ItemsList list) { 1043 993 for (Item i : toAdd) { 1044 994 // If an annotation is being deleted clear the annotation list … … 1047 997 } 1048 998 // TODO Improve efficiency when addAll is called 1049 addItem(i); 1050 } 1051 } 1052 1053 public void removeAllItems(Collection<Item> toRemove) 1054 { 999 addItem(i, true, list); 1000 } 1001 } 1002 1003 public void removeAllItems(Collection<Item> toRemove) { 1055 1004 for (Item i : toRemove) { 1056 1005 // If an annotation is being deleted clear the annotation list … … 1066 1015 } 1067 1016 1068 public void removeItem(Item item, boolean recalculate) 1069 { 1017 public void removeItem(Item item, boolean recalculate) { 1018 removeItem(item, recalculate, _body); 1019 if (item.isSurrogate()) { 1020 removeItem(item, recalculate, _surrogateItemsBody); 1021 } else { 1022 removeItem(item, recalculate, _primaryItemsBody); 1023 } 1024 } 1025 1026 protected void removeItem(Item item, boolean recalculate, ItemsList toRemoveFrom) { 1070 1027 // If an annotation is being deleted clear the annotation list 1071 1028 if (item.isAnnotation()) { … … 1073 1030 } 1074 1031 1075 if ( _body.remove(item)) {1032 if (toRemoveFrom.remove(item)) { 1076 1033 change(); 1077 1034 // Remove widgets from the widget list … … 1098 1055 * @param type The type of event that occurred 1099 1056 */ 1100 private void addToUndo(Collection<Item> items, History.Type type) 1101 { 1057 private void addToUndo(ItemsList items, History.Type type) { 1102 1058 if (items.size() < 1) { 1103 1059 return; … … 1107 1063 } 1108 1064 1109 public void addToUndoDelete(Collection<Item> items) 1110 { 1065 public void addToUndoDelete(ItemsList items) { 1111 1066 addToUndo(items, History.Type.deletion); 1112 1067 } 1113 1068 1114 public void addToUndoMove(Collection<Item> items) 1115 { 1069 public void addToUndoMove(ItemsList items) { 1116 1070 addToUndo(items, History.Type.movement); 1117 1071 } … … 1147 1101 break; 1148 1102 case movement: 1149 List<Item> changed = new LinkedList<Item>(_body);1150 changed.retain All(undo.items);1103 ItemsList changed = new ItemsList(_body); 1104 changed.retainHistory(undo.items); 1151 1105 _redo.push(new History(changed, History.Type.movement)); 1152 1106 for(Item i : undo.items) { … … 1207 1161 break; 1208 1162 case movement: 1209 List<Item> changed = new LinkedList<Item>(_body);1210 changed.retain All(redo.items);1163 ItemsList changed = new ItemsList(_body); 1164 changed.retainHistory(redo.items); 1211 1165 _undo.push(new History(changed, History.Type.movement)); 1212 1166 for(Item i : redo.items) { … … 1677 1631 { 1678 1632 for (Overlay o : _overlays.keySet()) { 1679 for (Item i : o.Frame.get Items()) {1633 for (Item i : o.Frame.getSortedItems()) { 1680 1634 i.onParentStateChanged(new ItemParentStateChangedEvent( 1681 1635 this, … … 1695 1649 _overlays.remove(o); 1696 1650 1697 for (Item i : f.get Items()) {1651 for (Item i : f.getSortedItems()) { 1698 1652 _overlayItems.remove(i); 1699 1653 i.onParentStateChanged(new ItemParentStateChangedEvent( … … 1788 1742 } 1789 1743 1790 List<Item> copies = ItemUtils.CopyItems(toMergeWith.get Items());1744 List<Item> copies = ItemUtils.CopyItems(toMergeWith.getSortedItems()); 1791 1745 copies.remove(toMergeWith.getNameItem()); 1792 1746 … … 1834 1788 public void clear(boolean keepAnnotations) 1835 1789 { 1836 List<Item> newBody = new ArrayList<Item>(0);1790 ItemsList newBody = new ItemsList(); 1837 1791 1838 1792 Item title = getTitleItem(); … … 2010 1964 // reset attributes 2011 1965 dot.setParent(this); 1966 dot.setOwner(template.getOwner()); 2012 1967 return dot; 2013 1968 } … … 2018 1973 2019 1974 // check for an updated template... 2020 for (Item i : this.get Items()) {1975 for (Item i : this.getSortedItems()) { 2021 1976 if (ItemUtils.startsWithTag(i, templateTag)) { 2022 1977 t = (Text) i; … … 2032 1987 t = defaultTemplate; 2033 1988 } 2034 1989 2035 1990 // If the item is linked apply any attribute pairs on the child frame 2036 1991 String link = t.getAbsoluteLink(); … … 2038 1993 // need to get link first because copy doesnt copy the link 2039 1994 t = t.copy(); 1995 // If the template does not have a owner then it should be set to the current user. 1996 if (t.getOwner() == null) { 1997 t.setOwner(UserSettings.UserName.get()); 1998 } 2040 1999 t.setTooltip(null); 2041 2000 if (link != null) { … … 2202 2161 public boolean moveMouseToTextItem(int index) 2203 2162 { 2204 List<Item> items = get Items();2163 List<Item> items = getSortedItems(); 2205 2164 int itemsFound = 0; 2206 2165 for (int i = 0; i < items.size(); i++) { … … 2228 2187 public boolean moveMouseToDefaultLocation() 2229 2188 { 2230 List<Item> items = get Items();2189 List<Item> items = getSortedItems(); 2231 2190 2232 2191 for (Item it : items) { … … 2527 2486 public List<Item> getVisibleItems() 2528 2487 { 2529 return get Items(true);2488 return getSortedItems(true); 2530 2489 } 2531 2490 … … 2559 2518 * @return the list of items to be saved to a text file 2560 2519 */ 2561 public List<Item> getItemsToSave() {2520 /*public List<Item> getItemsToSave() { 2562 2521 if (!_sorted) { 2563 2522 Collections.sort(_body); … … 2599 2558 2600 2559 return toSave; 2601 } 2602 2603 public List<Item> getSurrogateItemsToSave() { 2604 List<Item> surrogates = new ArrayList<Item>(); 2605 _body.forEach(item -> surrogates.addAll(item.getSurrogates())); 2606 return surrogates; 2560 }*/ 2561 2562 public List<Item> getItemsToSave() { 2563 return getItemsToSave(BodyType.PrimaryBody); 2564 } 2565 2566 public List<Item> getItemsToSave(BodyType type) { 2567 assert(!type.equals(BodyType.BodyDisplay)); 2568 switch (type) { 2569 case SurrogateBody: 2570 return getItemsToSave(_surrogateItemsBody); 2571 case BodyDisplay: 2572 return getItemsToSave(_body); 2573 case PrimaryBody: 2574 default: 2575 return getItemsToSave(_primaryItemsBody); 2576 } 2577 } 2578 2579 private List<Item> getItemsToSave(ItemsList body) { 2580 body.sort(); 2581 List<Widget> seenWidgets = new ArrayList<Widget>(); 2582 2583 List<Item> toSave = new ArrayList<Item>(); 2584 2585 body.removeIf(item -> item.dontSave()); 2586 for (Item item: body) { 2587 if (item instanceof WidgetCorner) { 2588 // Save the widget source. 2589 // Each widget has multiple WidgetCorner's..ignore them if we already have the source. 2590 Widget iw = ((WidgetCorner) item).getWidgetSource(); 2591 if (seenWidgets.contains(iw)) { continue; } 2592 seenWidgets.add(iw); 2593 toSave.add(iw.getSource()); 2594 } else if (item instanceof XRayable) { 2595 // XRayable Items have their sources saved. 2596 XRayable x = (XRayable) item; 2597 toSave.addAll(x.getItemsToSave()); 2598 } else if (item.hasEnclosures()) { 2599 // Deals with Circle objects only? 2600 continue; 2601 } else { 2602 toSave.add(item); 2603 } 2604 } 2605 2606 return toSave; 2607 2607 } 2608 2608 … … 2623 2623 } 2624 2624 2625 public Collection<Item> getAllItems() 2626 { 2627 Collection<Item> allItems = new LinkedHashSet<Item>(_body); 2625 public Collection<Item> getAllItems() { 2626 ItemsList allItems = new ItemsList(_body); 2628 2627 2629 2628 allItems.addAll(_overlayItems); 2630 2629 allItems.addAll(_vectorItems); 2631 return allItems ;2630 return allItems.underlying(); 2632 2631 } 2633 2632 … … 2648 2647 Collection<Text> textItems = new ArrayList<Text>(); 2649 2648 2650 for (Item i : get Items(true)) {2649 for (Item i : getSortedItems(true)) { 2651 2650 // only add up normal body text items 2652 2651 if ((i instanceof Text)) { … … 2669 2668 public void recalculate() 2670 2669 { 2671 for (Item i : get Items()) {2670 for (Item i : getSortedItems()) { 2672 2671 if (i.hasFormula() && !i.isAnnotation()) { 2673 2672 i.calculate(i.getFormula()); … … 2700 2699 Collection<Text> items = new LinkedHashSet<Text>(); 2701 2700 2702 for (Item i : get Items(true)) {2701 for (Item i : getSortedItems(true)) { 2703 2702 // only add up normal body text items 2704 2703 if (i instanceof Text && !i.isAnnotation()) { … … 2788 2787 } 2789 2788 2790 public List<Item> getBodyItemsWithInsufficientPermissions() {2791 return _bodyHiddenDueToPermissions ;2789 public Collection<Item> getBodyItemsWithInsufficientPermissions() { 2790 return _bodyHiddenDueToPermissions.underlying(); 2792 2791 } 2793 2792 … … 2898 2897 return members; 2899 2898 } 2899 2900 public boolean hasSurrogates() { 2901 return !_surrogateItemsBody.isEmpty(); 2902 } 2903 2904 private boolean meetsVisibilityRequirements(boolean requireVisible, Item i) { 2905 return i.isVisible() || (!requireVisible && !i.isDeleted()); 2906 } 2900 2907 2901 2908 private static final class History { … … 2906 2913 } 2907 2914 2908 public final List<Item>items;2915 public final ItemsList items; 2909 2916 2910 2917 public final Type type; 2911 2918 2912 public History( Collection<Item> items, Type type)2919 public History(ItemsList changed, Type type) 2913 2920 { 2914 this.items = new LinkedList<Item>(items);2921 this.items = new ItemsList(changed); 2915 2922 this.type = type; 2916 2923 } … … 2922 2929 } 2923 2930 } 2931 2932 protected boolean hasAnnotations() { 2933 return _annotations != null && _annotations.size() > 0; 2934 } 2935 2936 protected ItemsList getBody() { 2937 return _body; 2938 } 2939 protected ItemsList getPrimaryBody() { 2940 return _primaryItemsBody; 2941 } 2942 protected ItemsList getSurrogateBody() { 2943 return _surrogateItemsBody; 2944 } 2924 2945 }
Note:
See TracChangeset
for help on using the changeset viewer.