Ignore:
Timestamp:
06/27/08 16:44:21 (16 years ago)
Author:
ra33
Message:

Heaps of changes!!!!
Added circles...
Better drawing of lines etc etc

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/expeditee/items/Item.java

    r107 r108  
    99import java.awt.Polygon;
    1010import java.awt.Rectangle;
     11import java.awt.Stroke;
    1112import java.awt.geom.AffineTransform;
    1213import java.awt.geom.Area;
     14import java.awt.geom.Point2D;
     15import java.awt.geom.Rectangle2D;
    1316import java.util.ArrayList;
    1417import java.util.Collection;
    1518import java.util.ConcurrentModificationException;
     19import java.util.HashSet;
    1620import java.util.LinkedHashSet;
    1721import java.util.LinkedList;
     
    3943public abstract class Item implements Comparable<Item>, Runnable {
    4044
     45        public static final Float DEFAULT_THICKNESS = 2f;
     46
     47        public static final Float MINIMUM_THICKNESS = 1f;
     48
    4149        protected final int JOIN = BasicStroke.JOIN_ROUND;
    4250
    4351        protected final int CAP = BasicStroke.CAP_BUTT;
    44        
     52
     53        protected final Stroke HIGHLIGHT_STROKE = new BasicStroke(
     54                        MINIMUM_THICKNESS, CAP, JOIN, 4.0F);
     55
    4556        // contains all dots (including this one) that form an enclosure
    4657        // if this dot is part of an enclosing shape
     
    4960        public static final int LEFT_MARGIN = 13;
    5061
     62        // indicates which end the arrowhead should be drawn at
     63        protected Polygon _poly = null;
     64
    5165        protected boolean _connectedToAnnotation = false;
    52 
    53         public static final int PERMISSION_NONE = 0;
    54 
    55         public static final int PERMISSION_FOLLOW_LINKS = 1;
    56 
    57         public static final int PERMISSION_COPY = 2;
    58 
    59         public static final int PERMISSION_TDFC = 3;
    60 
    61         public static final int PERMISSION_FULL = 4;
    6266
    6367        public static final int NEAR_DISTANCE = 15;
     
    103107         * this Item.
    104108         */
    105         public static Color[] COLOR_WHEEL = { Color.RED, Color.BLUE, Item.GREEN,
    106                         Color.MAGENTA, Color.YELLOW.darker(), Color.BLACK, Color.DARK_GRAY,
    107                         Color.GRAY, Color.WHITE, null };
    108 
     109        public static Color[] COLOR_WHEEL = { Color.BLACK, Color.RED, Color.BLUE,
     110                        Item.GREEN, Color.MAGENTA, Color.YELLOW.darker(), Color.WHITE };
     111
     112        // TODO Have shift toggle through a black and white color wheel?
    109113        public static Color[] FILL_COLOR_WHEEL = { new Color(255, 150, 150),
    110114                        new Color(150, 150, 255), new Color(150, 255, 150),
    111115                        new Color(255, 150, 255), new Color(255, 255, 100), Color.WHITE,
    112                         Color.GRAY, Color.DARK_GRAY, Color.BLACK, null };
     116                         Color.BLACK};
     117                                                                                                                                 /* Color.GRAY,
     118                                                                                                                                 * Color.DARK_GRAY,
     119                                                                                                                                 */
    113120
    114121        public static final int UNCHANGED_CURSOR = -100;
     
    167174
    168175                dest.setParent(source.getParent());
    169                 dest._mode = SelectedMode.None;
    170                 dest._lastMode = source._mode;
    171 
     176                dest._mode = source._mode;// SelectedMode.None;
     177                dest._visible = source._visible;
    172178                Frame parent = source.getParentOrCurrentFrame();
    173179                // TODO MIKE says maybe we could tighten up and only give items ID's if
     
    199205        private Point _offset = new Point(0, 0);
    200206
    201         private float _x;
    202 
    203         private float _y;
     207        protected float _x;
     208
     209        protected float _y;
    204210
    205211        private int _id;
     
    229235        private List<String> _actionLeaveFrame = null;
    230236
    231         public int Permission = PERMISSION_FULL;
     237        public Permission _permission = Permission.full;
     238       
     239        public void setPermission(Permission permission){
     240                _permission = permission;
     241        }
     242       
     243        public boolean hasPermission(Permission permission){
     244                return _permission.ordinal() >= permission.ordinal();
     245        }
    232246
    233247        // A fill color of null represents transparent
     
    256270
    257271        // arrowhead parameters
    258         private int _arrowheadLength = 0;
     272        private float _arrowheadLength = 0;
    259273
    260274        private double _arrowheadRatio = DEFAULT_ARROWHEAD_RATIO;
     
    283297
    284298        private boolean _visible = true;
    285 
    286         private SelectedMode _lastMode = SelectedMode.None;
    287299
    288300        private float _thickness = -1.0F;
     
    303315                }
    304316
    305                 if (_actions == null)
     317                if (_actions == null){
     318                        _poly = null;
    306319                        _actions = new LinkedList<String>();
     320                }
    307321                _actions.add(action);
    308322        }
     
    312326                        connected.add(this);
    313327
    314                 for (Line line : getLines()) {
    315                         if (!connected.contains(line))
    316                                 line.addAllConnected(connected);
     328                for (Item item : getConnected()) {
     329                        if (!connected.contains(item))
     330                                item.addAllConnected(connected);
    317331                }
    318332        }
     
    383397
    384398        public void delete() {
    385                 setVisible(false);
    386         }
    387 
     399                _deleted = true;
     400        }
     401
     402        @Override
    388403        public boolean equals(Object o) {
    389                 if (o instanceof Item) {
     404                if (o == null)
     405                        return false;
     406                if (getClass().equals(o.getClass())) {
    390407                        Item i = (Item) o;
    391408                        return i.getID() == getID()
    392409                                        && ((i.getParent() == _parent) || (i.getParent() != null && i
    393410                                                        .getParent().equals(_parent)));
    394 
    395411                } else
    396412                        return false;
     
    463479        }
    464480
    465         public int getArrowheadLength() {
     481        public float getArrowheadLength() {
    466482                return _arrowheadLength;
    467483        }
     
    559575                List<Item> conn = new LinkedList<Item>();
    560576                conn.add(this);
    561 
     577                conn.addAll(getEnclosures());
    562578                conn.addAll(getLines());
    563579                return conn;
     
    698714
    699715        public Color getPaintBackgroundColor() {
    700                 if (_colorBackground == null) {
     716                Color colorBackground = getBackgroundColor();
     717                if (colorBackground == null) {
    701718                        if (getParent() != null && getParent().getBackgroundColor() != null)
    702719                                return getParent().getBackgroundColor();
     
    705722                }
    706723
    707                 return _colorBackground;
     724                return colorBackground;
    708725        }
    709726
     
    713730         * @return The Color of this item (foreground)
    714731         */
    715         public Color getPaintColor() {
     732        public final Color getPaintColor() {
    716733                // If color is null then get the paint foregroundColor for the frame the
    717734                // item is on which is a color adjusted to suit the background
    718                 if (_color == null) {
     735                Color color = getColor();
     736
     737                if (color == null) {
    719738                        if (getParent() != null)
    720739                                return getParent().getPaintForegroundColor();
     
    727746                }
    728747
    729                 return _color;
     748                return color;
    730749        }
    731750
     
    739758        }
    740759
    741         public Frame getParent() {
     760        public final Frame getParent() {
    742761                return _parent;
    743762        }
    744763
    745         /**
    746          * Returns the Shape that surrounds this Item representing this Item's
    747          * 'gravity'.
    748          *
    749          * @return The Shape (rectangle) surrounding this Item, which represents
    750          *         this Items 'gravity'.
    751          */
    752         public abstract Polygon getPolygon();
    753 
    754         public Point getPosition() {
     764        public final Point getPosition() {
    755765                return new Point(getX(), getY());
    756766        }
     
    762772         * @return The size of this Item.
    763773         */
    764         public int getSize() {
    765                 return -1;
     774        public float getSize() {
     775                return -1.0F;
    766776        }
    767777
     
    809819        /**
    810820         * Checks if the given Shape intersects with the Shape around this Item.
    811          * Note: Both Shape objects should be rectangles for this to work properly.
    812821         *
    813822         * @param s
     
    819828                        return false;
    820829
    821                 // return p.getBounds().intersects(getArea().getBounds());
    822830                Area a = new Area(p);
    823 
    824831                a.intersect(this.getArea());
     832
    825833                return !a.isEmpty();
    826834        }
     
    859867         */
    860868        public boolean isHighlighted() {
     869                if (isFloating())
     870                        return false;
    861871                return _mode != SelectedMode.None;
    862872        }
     
    917927
    918928        public void paintFill(Graphics2D g) {
    919                 if (getFillColor() != null && getEnclosingDots() != null) {
    920                         g.setColor(getFillColor());
     929                Color fillColor = getFillColor();
     930                if (fillColor != null && getEnclosingDots() != null) {
     931                        if(isFloating()){
     932                                //TODO experiment with adding alpha when picking up filled items...
     933                                //Slows things down quite alot!!
     934                                fillColor = new Color(fillColor.getRed(), fillColor.getGreen(), fillColor.getBlue());
     935                        }                       
     936                        g.setColor(fillColor);
    921937                        // The painting is not efficient enough for gradients...
    922938                        // Shape s = getEnclosedShape();
     
    10211037                // Check if all the characters are digits and hence it is a relative
    10221038                // link
    1023                 for (int i = 0; i < link.length(); i++) {
    1024                         if (!Character.isDigit(link.charAt(i)))
    1025                                 return;
    1026                 }
     1039                if(!FrameIO.isPositiveInteger(link))
     1040                        return;
    10271041
    10281042                // Make it an absolute link
     
    10381052
    10391053        /**
    1040          * Sets any action code (KMS action language) that should be associated with
     1054         * Sets any action code that should be associated with
    10411055         * this Item Each entry in the list is one line of code
    10421056         *
     
    10451059         */
    10461060        public void setActions(List<String> actions) {
     1061                //Want to resize the highlight box for text items if actions have been added
     1062                _poly = null;
    10471063                if (actions == null || actions.size() == 0)
    10481064                        _actions = null;
     
    10751091
    10761092        public void setActionMark(boolean val) {
     1093                _poly = null;
    10771094                _actionMark = val;
    10781095        }
     
    10961113         *            The ratio of the arrow's length to its width.
    10971114         */
    1098         public void setArrow(int length, double ratio) {
     1115        public void setArrow(float length, double ratio) {
    10991116                _arrowheadLength = length;
    11001117                _arrowheadRatio = ratio;
     
    11061123        }
    11071124
    1108         public void setArrowheadLength(int length) {
     1125        public void setArrowheadLength(float length) {
    11091126                _arrowheadLength = length;
    11101127                updateArrowPolygon();
     
    12501267
    12511268        public void setLinkMark(boolean val) {
     1269                _poly = null;
    12521270                _linkMark = val;
    12531271        }
     
    13071325                _y = y;
    13081326
     1327                for (Item i : getEnclosures()) {
     1328                        i.updatePolygon();
     1329                }
    13091330                updatePolygon();
    13101331
     
    14001421                        return;
    14011422                assert (_parent != null);
     1423               
     1424                if(FrameIO.isPositiveInteger(link))
     1425                        return;
     1426               
    14021427                // Check if the link is for the current frameset
    14031428                if (_parent.getFramesetName().equalsIgnoreCase(
     
    14111436         * Dots this is the thickness.
    14121437         */
    1413         public void setSize(int size) {
     1438        public void setSize(float size) {
    14141439        }
    14151440
     
    15001525                else {
    15011526                        _arrowhead = new Polygon();
    1502                         _arrowhead.addPoint((int) getX(), (int) getY());
     1527                        _arrowhead.addPoint(Math.round(getX()), Math.round(getY()));
     1528                        _arrowhead.addPoint((Math.round(getX() - getArrowheadLength())),
     1529                                        ((int) Math.round(getY()
     1530                                                        - (getArrowheadLength() * getArrowheadRatio()))));
     1531                        _arrowhead.addPoint(Math.round(getX()), (int) getY());
    15031532                        _arrowhead
    15041533                                        .addPoint(
    1505                                                         (int) getX() - getArrowheadLength(),
    1506                                                         (int) (getY() - (getArrowheadLength() * getArrowheadRatio())));
    1507                         _arrowhead.addPoint((int) getX(), (int) getY());
    1508                         _arrowhead
    1509                                         .addPoint(
    1510                                                         (int) getX() - getArrowheadLength(),
    1511                                                         (int) (getY() + (getArrowheadLength() * getArrowheadRatio())));
     1534                                                        (int) Math.round(getX() - getArrowheadLength()),
     1535                                                        (int) Math
     1536                                                                        .round((getY() + (getArrowheadLength() * getArrowheadRatio()))));
    15121537                }
    15131538        }
    15141539
    15151540        protected abstract void updatePolygon();
     1541
     1542        public void setHidden(boolean state) {
     1543                this._visible = !state;
     1544        }
     1545       
    15161546
    15171547        public void setVisible(boolean state) {
     
    15201550
    15211551        public boolean isVisible() {
    1522                 return _visible;
     1552                return _visible && !_deleted;
    15231553        }
    15241554
     
    15401570                }
    15411571                setSelectionColor(color);
    1542                 _lastMode = _mode;
    15431572                _mode = mode;
    1544         }
    1545 
    1546         public void restoreLastMode(Color selectionColor) {
    1547                 setSelectionColor(selectionColor);
    1548                 if (_mode != SelectedMode.None)
    1549                         return;
    1550                 _mode = _lastMode;
    1551                 _lastMode = SelectedMode.None;
    15521573        }
    15531574
     
    16591680        }
    16601681
     1682        /**
     1683         * True if this item is the end of a line.
     1684         *
     1685         * @return
     1686         */
    16611687        public boolean isLineEnd() {
     1688                // TODO this will need to be redone when enclosure class is added...
     1689                // At the moment enclosures are only circles...we dont want circle
     1690                // centers to be lineEnds
    16621691                return _lines.size() > 0;
     1692        }
     1693       
     1694        public boolean hasEnclosures(){
     1695                return _enclosures.size() > 0;
    16631696        }
    16641697
     
    17171750
    17181751        public void setAction(String string) {
     1752                //Want to resize the highlight box for text items if actions are been added
     1753                if(_actions == null || _actions.size() == 0){
     1754                        _poly = null;
     1755                }
    17191756                _actions = new LinkedList<String>();
    17201757                _actions.add(string);
     
    17321769        protected void paintLink(Graphics2D g) {
    17331770                if (getLink() != null || getAction() != null) {
     1771                        g.setStroke(HIGHLIGHT_STROKE);
    17341772                        if (getLink() != null && getAction() != null) {
    17351773                                g.setColor(LINK_ACTION_COLOR);
     
    18061844                        return null;
    18071845                // assert (_parent!= null);
     1846                Frame parent = getParentOrCurrentFrame();
    18081847                if (_parent == null) {
    18091848                        // if parent is null it is an item on the message box
    18101849                        // so it must already be absolute
    1811                         assert (!FrameIO.isPositiveInteger(link));
    1812                         return link;
     1850                        //assert (!FrameIO.isPositiveInteger(link));
     1851                        //return link;
     1852                       
    18131853                }
    18141854
    18151855                // if its a relative link then return absolute
    18161856                if (FrameIO.isPositiveInteger(link)) {
    1817                         return _parent.getFramesetName() + link;
     1857                        return parent.getFramesetName() + link;
    18181858                }
    18191859                return link;
     
    18281868         *            new y position
    18291869         */
    1830         public void setXY(int x, int y) {
     1870        public void setXY(float x, float y) {
    18311871                _x = x;
    18321872                _y = y;
     
    18771917
    18781918        /**
    1879          * Gets the size of the enclosure that this item is part of.
    1880          * @return the area of the box surrounding the enclosed shape that this item is part of
    1881          */
    1882         public Integer getEnclosedArea() {
     1919         * Gets the size of the enclosure that this item is part of. Used to
     1920         * determine the paint order of items, with smaller items being painted
     1921         * first.
     1922         *
     1923         * @return the area of the box surrounding the enclosed shape that this item
     1924         *         is part of
     1925         */
     1926        public double getEnclosedArea() {
    18831927                if (_enclosure == null)
    1884                         return 0;
    1885                 Rectangle box = getEnclosedShape().getBounds();
    1886                 Integer area = box.width * box.height;
    1887                
    1888                 if (getFillColor() == null)
    1889                         return -area;
    1890                
    1891                 return area;
     1928                        return 0.0;
     1929                Rectangle2D box = getEnclosedShape().getBounds2D();
     1930                return box.getWidth() * box.getHeight();
     1931        }
     1932
     1933        /**
     1934         * Returns the Shape that surrounds this Item representing this Item's
     1935         * 'gravity'.
     1936         *
     1937         * @return The Shape (rectangle) surrounding this Item, which represents
     1938         *         this Items 'gravity'.
     1939         */
     1940        public final Polygon getPolygon() {
     1941                if (_poly == null)
     1942                        updatePolygon();
     1943
     1944                return new Polygon(_poly.xpoints, _poly.ypoints, _poly.npoints);
     1945        }
     1946
     1947        /**
     1948         * Shifts the position of the item along the line between this items
     1949         * location and a specified point.
     1950         *
     1951         * @param origin
     1952         * @param ratio
     1953         */
     1954        public void translate(Point2D origin, double ratio) {           
     1955                _x = (float) (origin.getX() + ratio * (_x - origin.getX()));
     1956                _y = (float) (origin.getY() + ratio * (_y - origin.getY()));
     1957                updatePolygon();
     1958                for (Line line : getLines())
     1959                        line.updatePolygon();
     1960        }
     1961
     1962        private static int[] LinePatterns = new int[] { 0, 10, 20 };
     1963
     1964        /**
     1965         * The rotates through a wheel of dashed lines.
     1966         *
     1967         * @param amount
     1968         *            number of rotations around the wheel to toggle by.
     1969         */
     1970        public void toggleDashed(int amount) {
     1971                // find the index of the current line pattern
     1972                int[] currentPattern = getLinePattern();
     1973
     1974                // Find the current pattern and move to the next pattern in the wheel
     1975                for (int i = 0; i < LinePatterns.length; i++) {
     1976                        if (currentPattern == null || currentPattern[0] == LinePatterns[i]) {
     1977                                i += LinePatterns.length + amount;
     1978                                i %= LinePatterns.length;
     1979
     1980                                // if we are at the start of the wheel make it 'null' (solid
     1981                                // line)
     1982                                if (i == 0) {
     1983                                        setLinePattern(null);
     1984                                } else {
     1985                                        setLinePattern(new int[] { LinePatterns[i], LinePatterns[i] });
     1986                                }
     1987                                return;
     1988                        }
     1989                }
     1990        }
     1991
     1992        Collection<XRayable> _enclosures = new HashSet<XRayable>();
     1993
     1994        private boolean _deleted = false;
     1995
     1996        /**
     1997         * For now there can only be one enclosure per item
     1998         * @param enclosure
     1999         */
     2000        public void addEnclosure(XRayable enclosure) {
     2001                _enclosures.clear();
     2002                _enclosures.add(enclosure);
     2003        }
     2004
     2005        public Collection<? extends XRayable> getEnclosures() {
     2006                return _enclosures;
     2007        }
     2008
     2009        public void removeEnclosure(Item i) {
     2010                _enclosures.remove(i);
     2011
     2012        }
     2013
     2014        public boolean isDeleted() {
     2015                return _deleted;
    18922016        }
    18932017}
Note: See TracChangeset for help on using the changeset viewer.