Ignore:
Timestamp:
05/16/08 10:25:28 (16 years ago)
Author:
ra33
Message:

A whole day of big changes.
Adding the ability to have Text at the end of Lines.
Also a lot of refactoring to improve the quality of code relating to constraints and lines

File:
1 edited

Legend:

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

    r21 r50  
    8181
    8282                // initialise the line size
    83                 float thick;
    84 
    85                 if (_start instanceof Dot && _end instanceof Dot)
    86                         thick = (((Dot) _start).getThickness() + ((Dot) _end)
    87                                         .getThickness()) / 2.0f;
    88                 else if (_start instanceof Dot)
    89                         thick = ((Dot) _start).getThickness();
    90                 else if (_end instanceof Dot)
    91                         thick = ((Dot) _end).getThickness();
    92                 else
     83                float thick = (_start.getThickness() + _end.getThickness()) / 2.0f;
     84
     85                if (thick < 0)
    9386                        thick = 1;
     87                /*
     88                 * if (_start instanceof Dot && _end instanceof Dot) thick = (((Dot)
     89                 * _start).getThickness() + ((Dot) _end) .getThickness()) / 2.0f; else
     90                 * if (_start instanceof Dot) thick = ((Dot) _start).getThickness();
     91                 * else if (_end instanceof Dot) thick = ((Dot) _end).getThickness();
     92                 * else thick = 1;
     93                 */
    9494
    9595                setThickness(thick);
     
    134134
    135135        /**
    136          * Replaces either the start or end point of this Line with the given Dot.
    137          * This is used when merging two Dots.
     136         * Replaces either the start or end point of this Line with the given
     137         * LineEnd. This is used when merging LineEnds. It will also add and remove
     138         * the line from the LineEnds as well as adjust constraints.
    138139         *
    139140         * @param replace
     
    144145         */
    145146        public void replaceEnd(Item replace, Item with) {
    146                 if (_start == replace)
     147                Item otherEnd = null;
     148                if (_start == replace) {
    147149                        setStartItem(with);
    148                 else
     150                        otherEnd = _end;
     151                } else if (_end == replace) {
    149152                        setEndItem(with);
     153                        otherEnd = _start;
     154                }
     155                // if no end was replaced
     156                if (otherEnd == null)
     157                        return;
     158                // Copy the constraints list so the endPoints list can be modified
     159                List<Constraint> constraints = new ArrayList<Constraint>(replace
     160                                .getConstraints());
     161                // Now change all the constraints for this line only
     162                for (int i = 0; i < constraints.size(); i++) {
     163                        Constraint c = constraints.get(i);
     164                        if (c.contains(otherEnd)) {
     165                                c.replaceEnd(replace, with);
     166                        }
     167                }
    150168        }
    151169
     
    154172                        _start.removeLine(this);
    155173
    156                         if (!(start instanceof Dot)) {
    157                                 _startOffset.x = _start.getX() - start.getX();
    158                                 _startOffset.y = _start.getY() - start.getY();
    159                         } else {
     174                        /*
     175                         * if (!(start instanceof Dot)) { _startOffset.x = _start.getX() -
     176                         * start.getX(); _startOffset.y = _start.getY() - start.getY(); }
     177                         * else
     178                         */{
    160179                                _startOffset.x = 0;
    161180                                _startOffset.y = 0;
     
    172191                        _end.removeLine(this);
    173192
    174                         if (!(end instanceof Dot)) {
    175                                 _endOffset.x = _end.getX() - end.getX();
    176                                 _endOffset.y = _end.getY() - end.getY();
    177                         } else {
     193                        /*
     194                         * if (!(end instanceof Dot)) { _endOffset.x = _end.getX() -
     195                         * end.getX(); _endOffset.y = _end.getY() - end.getY(); } else
     196                         */{
    178197                                _endOffset.x = 0;
    179198                                _endOffset.y = 0;
     
    211230        }
    212231
     232        @Override
    213233        public void setX(int x) {
    214234        }
    215235
     236        @Override
    216237        public void setY(int y) {
    217238        }
     
    287308                g.setColor(getPaintColor());
    288309
    289                 //if (this._mode == Item.SelectedMode.Disconnect)
    290                 //      System.out.println("Disconnect mode!");
    291                
     310                // if (this._mode == Item.SelectedMode.Disconnect)
     311                // System.out.println("Disconnect mode!");
     312
    292313                if (_start instanceof Text && ((Text) _start).startsWith("@c")) {
    293314                        g.setStroke(_lineStroke);
     
    340361                        Point endOffset) {
    341362                // only draw an arrowhead if necessary
    342                 if (!(this._mode == Item.SelectedMode.Disconnect
    343                                 && withArrow._mode == Item.SelectedMode.Disconnect)
    344                                 && (!withArrow.hasVisibleArrow()
    345                                 || withArrow.getLines().size() > 1))
     363                if (!(this._mode == Item.SelectedMode.Disconnect && withArrow._mode == Item.SelectedMode.Disconnect)
     364                                && (!withArrow.hasVisibleArrow() || withArrow.getLines().size() > 1))
    346365                        return;
    347366
     
    356375                double arrowRatio = withArrow.getArrowheadRatio();
    357376
    358                 //set the size of the disconnect indicator arrowhead
    359                 if (this._mode == Item.SelectedMode.Disconnect){
    360                         //System.out.println("Disconnect mode!");
     377                // set the size of the disconnect indicator arrowhead
     378                if (this._mode == Item.SelectedMode.Disconnect) {
     379                        // System.out.println("Disconnect mode!");
    361380                        arrowLength = 15;
    362381                        arrowRatio = 0.3;
    363                         //Make sure the arrowhead positions get recalculated
     382                        // Make sure the arrowhead positions get recalculated
    364383                        withArrow.setArrowhead(null);
    365384                }
    366                
     385
    367386                // if the arrowhead is 'auto', then one and only one end must be
    368387                // floating
     
    489508                        return end;
    490509                } else if (distStart < DISCONNECT_THRESHHOLD) {
    491                         start._mode = Item.SelectedMode.Disconnect;
     510                        if (start.getLines().size() > 1
     511                                        || start.getConstraints().size() > 0)
     512                                start._mode = Item.SelectedMode.Disconnect;
     513                        else
     514                                start._mode = Item.SelectedMode.Normal;
    492515                        return start;
    493516                } else if (distEnd < DISCONNECT_THRESHHOLD) {
    494                         end._mode = Item.SelectedMode.Disconnect;
     517                        if (end.getLines().size() > 1 || end.getConstraints().size() > 0)
     518                                end._mode = Item.SelectedMode.Disconnect;
     519                        else
     520                                end._mode = Item.SelectedMode.Normal;
    495521                        return end;
    496522                }
     
    520546                super.setColor(c);
    521547
    522                 if (_start.getColor() != c)
    523                         _start.setColor(c);
    524 
    525                 if (_end.getColor() != c)
    526                         _end.setColor(c);
    527         }
    528 
    529         @Override
    530         public int getSize() {
    531                 return _start.getSize();
    532         }
    533 
    534         @Override
    535         public void setSize(int size) {
     548                _start.lineColorChanged(c);
     549                _end.lineColorChanged(c);
    536550        }
    537551
     
    543557                        thick = DEFAULT_THICKNESS;
    544558
    545                 if (_start instanceof Dot) {
    546                         Dot start = (Dot) _start;
    547                         if (start.getThickness() != thick)
    548                                 start.setThickness(thick);
    549                 }
    550 
    551                 if (_end instanceof Dot) {
    552                         Dot end = (Dot) _end;
    553                         if (end.getThickness() != thick)
    554                                 end.setThickness(thick);
    555                 }
     559                if (_start.getThickness() != thick)
     560                        _start.setThickness(thick);
     561
     562                if (_end.getThickness() != thick)
     563                        _end.setThickness(thick);
    556564
    557565                int[] pattern = _start.getLinePattern();
     
    734742        private Dot _virtualSpot = null;
    735743
    736         public void showVirtualSpot(Dot orig, int mouseX, int mouseY) {
     744        public void showVirtualSpot(Item orig, int mouseX, int mouseY) {
    737745                if (orig.getLines().size() != 1)
    738746                        return;
     
    742750                        return;
    743751
    744                 Dot spot = orig.copy();
     752                Dot spot = new Dot(-1, orig.getX(), orig.getY());
     753                Item.DuplicateItem(orig, spot);
    745754                spot.setThickness(Math.max(this.getThickness(), 5));
    746755                if (this.getColor() != Color.RED)
     
    817826        }
    818827
    819         public Item forceMerge(Item merger, int mouseX, int mouseY) {
    820                 Dot spot = (Dot) merger;
    821 
    822                 if (spot.getConstraints() != null)
    823                         spot.getConstraints().clear();
    824 
    825                 if (_end.getConstraints() != null)
    826                         _end.getConstraints().clear();
    827 
    828                 if (_start.getConstraints() != null)
    829                         _start.getConstraints().clear();
     828        public Item forceMerge(Item spot, int mouseX, int mouseY) {
     829
     830                spot.removeAllConstraints();
     831
     832                _end.removeAllConstraints();
     833
     834                _start.removeAllConstraints();
    830835
    831836                // calculate nearest point on line from spot
     
    840845                // if the line is horizontal
    841846                if (slope1 == 0) {
    842                         x = merger.getX();
     847                        x = spot.getX();
    843848                        y = _start.getY();
    844849                        // if the line is vertical
    845850                } else if (slope2 == 0) {
    846851                        x = _start.getX();
    847                         y = merger.getY();
     852                        y = spot.getY();
    848853                        // otherwise, the line is sloped
    849854                } else {
     
    875880        @Override
    876881        public Item merge(Item merger, int mouseX, int mouseY) {
    877                 if (!(merger instanceof Dot))
     882                if (!(merger.isLineEnd()))
    878883                        return merger;
    879884
    880                 Dot spot = (Dot) merger;
     885                Item spot = merger;
    881886
    882887                // dots with multiple lines cannot be attached, nor can dots with no
     
    10151020                        _end.delete();
    10161021        }
     1022
     1023        @Override
     1024        public void anchor() {
     1025                super.anchor();
     1026                fixArrowheadLength();
     1027        }
     1028
     1029        /**
     1030         * Removes constraints from the end points of this line.
     1031         */
     1032        @Override
     1033        public void removeAllConstraints() {
     1034                // Find all constraints that include both the start and end.
     1035                for (Constraint c : _start.getConstraints()) {
     1036                        if (c.contains(_end)) {
     1037                                _start.removeConstraint(c);
     1038                                _end.removeConstraint(c);
     1039                        }
     1040                }
     1041        }
    10171042}
Note: See TracChangeset for help on using the changeset viewer.