Ignore:
Timestamp:
07/30/08 14:53:21 (16 years ago)
Author:
ra33
Message:
 
File:
1 edited

Legend:

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

    r156 r176  
    2727import org.expeditee.gui.Frame;
    2828import org.expeditee.gui.FrameGraphics;
     29import org.expeditee.gui.FrameIO;
    2930import org.expeditee.gui.FrameKeyboardActions;
    3031import org.expeditee.gui.FrameMouseActions;
     32import org.expeditee.simple.ExpediteeJEP;
     33import org.nfunk.jep.Node;
    3134
    3235/**
     
    242245         *            The String to display on the screen when drawing this Item.
    243246         */
     247        @Override
    244248        public void setText(String text) {
    245249                if (_text != null && text.length() < _text.length())
     
    430434        }
    431435
    432         public void setSelectionStart(int mouseX, int mouseY) {
     436        public void setSelectionStart(float mouseX, float mouseY) {
    433437                // determine what line is being pointed to
    434438                int line = getLinePosition(mouseY);
     
    440444        }
    441445
    442         public void setSelectionEnd(int mouseX, int mouseY) {
     446        public void setSelectionEnd(float mouseX, float mouseY) {
    443447                // determine what line is being pointed to
    444448                int line = getLinePosition(mouseY);
     
    700704        }
    701705
    702         public Point2D.Float moveCursor(int direction, float mouseX, float mouseY) {
     706        public Point2D.Float moveCursor(int direction, float mouseX, float mouseY,
     707                        boolean setSelection) {
     708                if (setSelection) {
     709                        if (getSelectionSize() <= 0) {
     710                                setSelectionStart(mouseX, mouseY);
     711                        }
     712                }
     713
     714                Point2D.Float resultPos = null;
     715
    703716                // check for home or end keys
    704717                switch (direction) {
    705718                case HOME:
    706                         return getParagraphStartPosition();
     719                        resultPos = getParagraphStartPosition();
     720                        break;
    707721                case END:
    708                         return getParagraphEndPosition();
     722                        resultPos = getParagraphEndPosition();
     723                        break;
    709724                case LINE_HOME:
    710                         return getLineStartPosition(mouseY);
     725                        resultPos = getLineStartPosition(mouseY);
     726                        break;
    711727                case LINE_END:
    712                         return getLineEndPosition(mouseY);
    713                 }
    714 
    715                 TextHitInfo hit;
    716                 TextLayout current;
    717                 int line;
    718 
    719                 // if there is no text yet
    720                 if (_text == null || _text.length() == 0) {
    721                         return new Point2D.Float(mouseX, mouseY);
    722                         // otherwise, move the cursor
    723                 } else {
    724                         // determine the line of text to check
    725                         line = getLinePosition(mouseY);
    726                         if (line < 0)
    727                                 line = _textLayouts.size() - 1;
    728 
    729                         // if the cursor is moving up or down, change the line
    730                         if (direction == UP)
    731                                 line = Math.max(line - 1, 0);
    732                         else if (direction == DOWN)
    733                                 line = Math.min(line + 1, _textLayouts.size() - 1);
    734 
    735                         hit = getCharPosition(line, mouseX);
    736 
    737                         if (direction == LEFT) {
    738                                 if (hit.getInsertionIndex() > 0) {
    739                                         hit = _textLayouts.get(line).getNextLeftHit(hit);
    740                                         // This takes care of the case where the user has put a hard
    741                                         // line break in
    742                                         if (line > 0
    743                                                         && _text.charAt(hit.getInsertionIndex()
    744                                                                         + _lineOffsets.get(line)) == '\n') {
     728                        resultPos = getLineEndPosition(mouseY);
     729                        break;
     730                default:
     731                        TextHitInfo hit;
     732                        TextLayout current;
     733                        int line;
     734
     735                        // if there is no text yet
     736                        if (_text == null || _text.length() == 0) {
     737                                return new Point2D.Float(mouseX, mouseY);
     738                                // otherwise, move the cursor
     739                        } else {
     740                                // determine the line of text to check
     741                                line = getLinePosition(mouseY);
     742                                if (line < 0)
     743                                        line = _textLayouts.size() - 1;
     744
     745                                // if the cursor is moving up or down, change the line
     746                                if (direction == UP)
     747                                        line = Math.max(line - 1, 0);
     748                                else if (direction == DOWN)
     749                                        line = Math.min(line + 1, _textLayouts.size() - 1);
     750
     751                                hit = getCharPosition(line, mouseX);
     752
     753                                if (direction == LEFT) {
     754                                        if (hit.getInsertionIndex() > 0) {
     755                                                hit = _textLayouts.get(line).getNextLeftHit(hit);
     756                                                // This takes care of the case where the user has put a
     757                                                // hard
     758                                                // line break in
     759                                                if (line > 0
     760                                                                && _text.charAt(hit.getInsertionIndex()
     761                                                                                + _lineOffsets.get(line)) == '\n') {
     762                                                        line--;
     763                                                        hit = _textLayouts.get(line)
     764                                                                        .getNextRightHit(
     765                                                                                        _textLayouts.get(line)
     766                                                                                                        .getCharacterCount() - 1);
     767                                                }
     768                                                // This takes care of soft line breaks.
     769                                        } else if (line > 0) {
    745770                                                line--;
    746771                                                hit = _textLayouts.get(line).getNextRightHit(
    747772                                                                _textLayouts.get(line).getCharacterCount() - 1);
     773                                                while (hit.getCharIndex() > 0
     774                                                                && _text.charAt(_lineOffsets.get(line)
     775                                                                                + hit.getCharIndex() - 1) == ' ') {
     776                                                        hit = _textLayouts.get(line).getNextLeftHit(hit);
     777                                                }
    748778                                        }
    749                                         // This takes care of soft line breaks.
    750                                 } else if (line > 0) {
    751                                         line--;
    752                                         hit = _textLayouts.get(line).getNextRightHit(
    753                                                         _textLayouts.get(line).getCharacterCount() - 1);
    754                                         while (hit.getCharIndex() > 0
    755                                                         && _text.charAt(_lineOffsets.get(line)
    756                                                                         + hit.getCharIndex() - 1) == ' ') {
    757                                                 hit = _textLayouts.get(line).getNextLeftHit(hit);
     779                                } else if (direction == RIGHT) {
     780                                        if (hit.getInsertionIndex() < _textLayouts.get(line)
     781                                                        .getCharacterCount()) {
     782                                                hit = _textLayouts.get(line).getNextRightHit(hit);
     783                                                while (hit.getCharIndex() > 0
     784                                                                && hit.getCharIndex() < _textLayouts.get(line)
     785                                                                                .getCharacterCount()
     786                                                                && _text.charAt(_lineOffsets.get(line)
     787                                                                                + hit.getCharIndex() - 1) == '\t')
     788                                                        hit = _textLayouts.get(line).getNextRightHit(hit);
     789                                        } else if (line < _textLayouts.size() - 1) {
     790                                                line++;
     791                                                hit = _textLayouts.get(line).getNextLeftHit(1);
    758792                                        }
    759793                                }
    760                         } else if (direction == RIGHT) {
    761                                 if (hit.getInsertionIndex() < _textLayouts.get(line)
    762                                                 .getCharacterCount()) {
    763                                         hit = _textLayouts.get(line).getNextRightHit(hit);
    764                                         while (hit.getCharIndex() > 0
    765                                                         && hit.getCharIndex() < _textLayouts.get(line)
    766                                                                         .getCharacterCount()
    767                                                         && _text.charAt(_lineOffsets.get(line)
    768                                                                         + hit.getCharIndex() - 1) == '\t')
    769                                                 hit = _textLayouts.get(line).getNextRightHit(hit);
    770                                 } else if (line < _textLayouts.size() - 1) {
    771                                         line++;
    772                                         hit = _textLayouts.get(line).getNextLeftHit(1);
    773                                 }
    774                         }
    775 
    776                         current = _textLayouts.get(line);
    777                 }
    778 
    779                 // move the cursor to the new location
    780                 float[] caret = current.getCaretInfo(hit);
    781                 float y = getLineDrop(current) * line;
    782 
    783                 return new Point2D.Float(getX() + caret[0] + getJustOffset(current),
    784                                 getY() + y + caret[1]);
     794                                current = _textLayouts.get(line);
     795                        }
     796
     797                        // move the cursor to the new location
     798                        float[] caret = current.getCaretInfo(hit);
     799                        float y = getLineDrop(current) * line;
     800
     801                        resultPos = new Point2D.Float(getX() + caret[0]
     802                                        + getJustOffset(current), getY() + y + caret[1]);
     803                        break;
     804                }
     805                if (setSelection)
     806                        setSelectionEnd(resultPos.x, resultPos.y);
     807                return resultPos;
    785808        }
    786809
     
    11701193                if (isAnnotation()) {
    11711194                        Frame parent = getParent();
    1172                         //parent can be null when running tests
     1195                        // parent can be null when running tests
    11731196                        if (parent != null) {
    11741197                                parent.clearAnnotations();
     
    18361859
    18371860        public void setRightMargin(int i) {
    1838                 setWidth(i - getX() - Item.MARGIN_RIGHT);
     1861                setWidth(i - getX() - Item.MARGIN_LEFT);
     1862        }
     1863       
     1864        @Override
     1865        public void calculate(String formula) {
     1866                super.calculate(formula);
     1867                if(formula == null || formula.length() == 0){
     1868                        return;
     1869                }
     1870               
     1871                ExpediteeJEP myParser = new ExpediteeJEP();
     1872                myParser.addVariables(this.getParentOrCurrentFrame());
     1873                String linkedFrame = getAbsoluteLink();
     1874                if (linkedFrame != null) {
     1875                        myParser.addVariables(FrameIO.LoadFrame(linkedFrame));
     1876                }
     1877                myParser.resetObserver();
     1878                Node node = myParser.parseExpression(formula);
     1879                if (node != null) {
     1880                        String result = myParser.getResult();
     1881                        if (result != null) {
     1882                                this.setText(result);
     1883                        }
     1884                }
    18391885        }
    18401886}
Note: See TracChangeset for help on using the changeset viewer.