Ignore:
Timestamp:
05/10/18 16:04:51 (6 years ago)
Author:
davidb
Message:

Reworking of the code-base to separate logic from graphics. This version of Expeditee now supports a JFX graphics as an alternative to SWING

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/expeditee/gui/FreeItems.java

    r919 r1102  
    1919package org.expeditee.gui;
    2020
    21 import java.awt.Polygon;
    2221import java.util.ArrayList;
    2322import java.util.Collection;
     
    2928import java.util.Map;
    3029
     30import org.expeditee.core.bounds.PolygonBounds;
    3131import org.expeditee.items.Dot;
    3232import org.expeditee.items.Item;
     
    4242        private static FreeItems _cursor = new FreeItems();
    4343
    44         private FreeItems() {
    45         }
    46 
    47         public static FreeItems getCursor() {
     44        public static FreeItems getCursor()
     45        {
    4846                return _cursor;
    4947        }
    5048       
    51         public static FreeItems getInstance() {
     49        public static FreeItems getInstance()
     50        {
    5251                return _instance;
    5352        }
    5453
    55         @Override
    56         public void clear() {
     54        /** Singleton class (although there are two of them). */
     55        private FreeItems()
     56        {
     57        }
     58       
     59        @Override
     60        public void clear()
     61        {
    5762                for (Item i : this) {
    5863                        i.invalidateAll();
     
    6368
    6469        @Override
    65         public Item remove(int index) {
     70        public Item remove(int index)
     71        {
    6672                Item i = get(index);
    6773                remove(i);
     
    7076
    7177        @Override
    72         public boolean remove(Object o) {
     78        public boolean remove(Object o)
     79        {
    7380                if (o instanceof Item) {
    7481                        ((Item) o).invalidateAll();
    7582                        ((Item) o).invalidateFill();
    7683                }
     84               
    7785                return super.remove(o);
    7886        }
     
    8492         * @return true if at least one item is attached to the cursor.
    8593         */
    86         public static boolean itemsAttachedToCursor() {
     94        public static boolean hasItemsAttachedToCursor()
     95        {
    8796                return getInstance().size() > 0;
    8897        }
     
    94103         *         attached are text items.
    95104         */
    96         public static boolean textOnlyAttachedToCursor() {
    97                 if (!itemsAttachedToCursor())
    98                         return false;
    99                 for (Item i : getInstance()) {
    100                         if (!(i instanceof Text)) {
    101                                 return false;
    102                         }
    103                 }
     105        public static boolean textOnlyAttachedToCursor()
     106        {
     107                if (!hasItemsAttachedToCursor()) return false;
     108               
     109                for (Item i : getInstance()) {
     110                        if (!(i instanceof Text)) return false;
     111                }
     112               
    104113                return true;
    105114        }
    106115
    107         public static Item getItemAttachedToCursor() {
    108                 if (itemsAttachedToCursor())
    109                         return getInstance().get(0);
     116        public static Item getItemAttachedToCursor()
     117        {
     118                if (hasItemsAttachedToCursor()) return getInstance().get(0);
     119               
    110120                return null;
    111121        }
    112122
    113         public static Text getTextAttachedToCursor() {
    114                 if (textOnlyAttachedToCursor())
    115                         return (Text) getInstance().get(0);
     123        public static Text getTextAttachedToCursor()
     124        {
     125                if (textOnlyAttachedToCursor()) return (Text) getInstance().get(0);
     126               
    116127                return null;
    117128        }
     
    121132         * @return the list of free text items
    122133         */
    123         public static Collection<Text> getTextItems() {
     134        public static Collection<Text> getTextItems()
     135        {
    124136                Collection<Text> textItems = new LinkedList<Text>();
    125                 for (Item i : getInstance()) {
    126                         if (i instanceof Text) {
    127                                 textItems.add((Text) i);
    128                         }
     137               
     138                for (Item i : getInstance()) {
     139                        if (i instanceof Text) textItems.add((Text) i);
    129140                }
    130141
     
    132143        }
    133144
    134         public static Map<String, Collection<String>> getGroupedText() {
     145        public static Map<String, Collection<String>> getGroupedText()
     146        {
    135147                Map<String, Collection<String>> groupedText = new HashMap<String, Collection<String>>();
     148               
    136149                // Go throught the lineEnds
    137150                Collection<Item> addedItems = new HashSet<Item>();
    138151                for (Item i : getInstance()) {
    139                         if (!(i instanceof Text) || !i.isLineEnd()) {
    140                                 continue;
    141                         }
     152                        if (!(i instanceof Text) || !i.isLineEnd()) continue;
     153                       
    142154                        // Check for text inside the box
    143155                        Collection<String> textList = new LinkedList<String>();
     
    146158                                addedItems.add(enclosed);
    147159                        }
     160                       
    148161                        if (textList.size() > 0) {
    149162                                groupedText.put(i.getText(), textList);
    150163                        }
    151164                }
     165               
    152166                // Now add the items that were not contained in any of the boxes
    153167                Collection<String> outsideList = new LinkedList<String>();
     
    157171                        }
    158172                }
     173               
    159174                groupedText.put("", outsideList);
    160175
     
    169184         * @return
    170185         */
    171         private Collection<Text> getTextWithin(Item lineEnd) {
    172                 Polygon poly = lineEnd.getEnclosedShape();
     186        private Collection<Text> getTextWithin(Item lineEnd)
     187        {
     188                PolygonBounds poly = lineEnd.getEnclosedShape();
     189               
    173190                Collection<Text> results = new LinkedHashSet<Text>();
    174191                for (Item i : this) {
     
    177194                        }
    178195                }
     196               
    179197                return results;
    180198        }
    181199
    182         public static boolean hasCursor() {
     200        public static boolean hasCursor()
     201        {
    183202                return getCursor().size() > 0;
    184203        }
    185204
    186         public static void setCursor(Collection<Item> cursor) {
     205        public static void setCursor(Collection<Item> cursor)
     206        {
    187207                _cursor.clear();
    188208                _cursor.addAll(cursor);
    189209        }
    190210       
    191         public static boolean hasMultipleVisibleItems() {
     211        public static boolean hasMultipleVisibleItems()
     212        {
    192213                List<Item> toCount = new LinkedList<Item>(getInstance());
     214               
    193215                int c = 0;
    194216                while(!toCount.isEmpty()) {
     
    199221                        }
    200222                }
     223               
    201224                return false;
    202225        }
    203226       
    204         public static boolean isDrawingPolyLine() {
     227        public static boolean isDrawingPolyLine()
     228        {
    205229                List<Item> tmp = getInstance();
     230               
    206231                return tmp.size() == 2 && tmp.get(0) instanceof Dot && tmp.get(1) instanceof Line;
    207232        }
     233
     234        /**
     235         * Checks if lines are being rubber banded.
     236         *
     237         * @return true if the user is rubberBanding one or more lines
     238         */
     239        public static boolean rubberBanding()
     240        {
     241                if (getInstance().size() != 2) return false;
     242
     243                // if rubber-banding, there will be 1 line end and the rest will be lines
     244                boolean foundLineEnd = false;
     245                for (Item i : getInstance()) {
     246                        if (i.isLineEnd()) {
     247                                if (foundLineEnd) return false;
     248                                foundLineEnd = true;
     249                        } else if (!(i instanceof Line) || !i.isVisible()) {
     250                                return false;
     251                        }
     252                }
     253               
     254                return true;
     255        }
     256       
     257        @Override
     258        public FreeItems clone()
     259        {
     260                FreeItems ret = new FreeItems();
     261                ret.addAll(this);
     262                return ret;
     263        }
    208264}
Note: See TracChangeset for help on using the changeset viewer.