Ignore:
Timestamp:
11/18/08 13:34:54 (16 years ago)
Author:
ra33
Message:

Changed the way justification works so that...
Width field has a positive value if explicitly set and under the hood stores a negative value if it has not been set explicitly.

File:
1 edited

Legend:

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

    r390 r400  
    4747 */
    4848public class Text extends Item {
     49        private static final int ADJUST_WIDTH_THRESHOLD = 200;
     50
    4951        public static String LINE_SEPARATOR = System.getProperty("line.separator");
    5052
     
    98100        public static final int PAGE_UP = 8;
    99101
    100         private int _maxWidth = -1;
     102        private Integer _maxWidth = null;
    101103
    102104        private Justification _justification = Justification.left;
     
    181183         */
    182184        @Override
    183         public void setWidth(int width) {
    184                 invalidateAll();
    185                 // 0 is the default
    186                 if (width <= 0) {
    187                         width = -1;
     185        public void setWidth(Integer width) {
     186                invalidateAll();
     187
     188                if (width == null) {
    188189                        setJustification(Justification.left);
     190                        setRightMargin(FrameGraphics.getMaxFrameSize().width, false);
     191                        return;
    189192                }
    190193
     
    194197        }
    195198
     199        public Integer getWidthToSave() {
     200                return _maxWidth;
     201        }
     202       
    196203        /**
    197204         * Returns the maximum width of this Text item when justifcation is used.
    198          * Note the returned value may be -1, which indicates that there is no
    199          * maximum size set
     205         * If the width is negative, it means no explicit width has been set
    200206         *
    201207         * @return The maximum width of this Text item when justification is used
    202208         */
    203209        @Override
    204         public int getWidth() {
     210        public Integer getWidth() {
     211                if (_maxWidth == null || _maxWidth <= 0)
     212                        return null;
    205213                return _maxWidth;
     214        }
     215
     216        public Integer getAbsoluteWidth() {
     217                if (_maxWidth == null) {
     218                        return Integer.MAX_VALUE;
     219                }
     220
     221                return Math.abs(_maxWidth);
    206222        }
    207223
     
    224240
    225241                // Only justification left works with 0 width
    226                 if (just != Justification.left && getWidth() <= 0) {
    227                         // TODO Tighten this up so it subtracts the margin widths
    228                         setWidth(getBoundsWidth());
    229                 }
     242                // if (just != null && just != Justification.left && !hasWidth()) {
     243                // // TODO Tighten this up so it subtracts the margin widths
     244                // setWidth(getBoundsWidth());
     245                // }
    230246
    231247                _justification = just;
     
    247263
    248264        private int getJustOffset(TextLayout layout) {
    249                 if (getWidth() < 0)
    250                         return 0;
    251 
    252265                if (getJustification() == Justification.center)
    253                         return (int) ((getWidth() - layout.getAdvance()) / 2);
     266                        return (int) ((getAbsoluteWidth() - layout.getAdvance()) / 2);
    254267                else if (getJustification() == Justification.right)
    255                         return (int) (getWidth() - layout.getAdvance());
     268                        return (int) (getAbsoluteWidth() - layout.getAdvance());
    256269
    257270                return 0;
     
    269282                setText(text, false);
    270283        }
    271        
     284
    272285        public void setText(String text, Boolean clearCache) {
    273286                // if (_text != null && text.length() < _text.length())
     
    279292                 * mode
    280293                 */
    281                 if(clearCache){
     294                if (clearCache) {
    282295                        clearCache();
    283                 }               
    284                        
     296                }
     297
    285298                rebuild(true);
    286299                invalidateAll();
     
    13141327                // If its justification right or center then DONT limit the width
    13151328                if (getJustification() != null) {
    1316                         maxX = Item.MARGIN_RIGHT + getWidth();
     1329                        maxX = Item.MARGIN_RIGHT + getAbsoluteWidth();
    13171330                }
    13181331
     
    13681381
    13691382                if (limitWidth) {
    1370                         if (getWidth() > 0)
    1371                                 width = getWidth();
     1383                        width = getAbsoluteWidth();
    13721384                        // else if (getMaxWidth() > 0)
    13731385                        // width = Math.max(50, getMaxWidth() - getX()
     
    14001412                        _lineOffsets.add(_lineBreaker.getPosition());
    14011413
    1402                         if (getWidth() > 0 && getJustification() == Justification.full
     1414                        if (/* hasWidth() && */getJustification() == Justification.full
    14031415                                        && _lineBreaker.getPosition() < _text.length())
    14041416                                layout = layout.getJustifiedLayout(width);
     
    14091421                updatePolygon();
    14101422
     1423        }
     1424
     1425        private boolean hasFixedWidth() {
     1426                assert (_maxWidth != null);
     1427                if(_maxWidth == null){
     1428                        justify(false);
     1429                }
     1430                return _maxWidth > 0;
    14111431        }
    14121432
     
    16581678                copy.setSpacing(getSpacing());
    16591679                copy.setInitialSpacing(getInitialSpacing());
     1680
     1681                copy.setWidth(getWidth());
    16601682                copy.setJustification(getJustification());
    16611683                copy.setLetterSpacing(getLetterSpacing());
     
    16691691                }
    16701692                copy.setHidden(!isVisible());
    1671                 // copy.setWidth(getWidth());
    16721693                return copy;
    16731694        }
     
    18561877                setPosition(MARGIN_LEFT, MARGIN_LEFT + getBoundsHeight());
    18571878                Frame modelFrame = getParentOrCurrentFrame();
    1858                 if (modelFrame != null) {
    1859                         setRightMargin(modelFrame.getNameItem().getX()
    1860                                         - MARGIN_LEFT);
     1879                if (modelFrame != null) {
     1880                        setRightMargin(modelFrame.getNameItem().getX() - MARGIN_LEFT, true);
    18611881                } else {
    1862                         System.out.print("Error: text.resetTitlePosition, getParent or currentFrame returned null");
    1863                         setRightMargin(MARGIN_LEFT);
     1882                        System.out
     1883                                        .print("Error: text.resetTitlePosition, getParent or currentFrame returned null");
     1884                        setRightMargin(MARGIN_LEFT, true);
    18641885                }
    18651886        }
     
    19431964                if (isLineEnd())
    19441965                        DisplayIO.setCursor(Item.DEFAULT_CURSOR);
     1966
     1967                if (!hasFixedWidth()) {
     1968                        int frameWidth = FrameGraphics.getMaxFrameSize().width;
     1969                        /*
     1970                         * Only change width if it is more than 150 pixels from the right of
     1971                         * the screen
     1972                         */
     1973                        if (frameWidth - getX() > ADJUST_WIDTH_THRESHOLD) {
     1974                                justify(false);
     1975                                //setRightMargin(frameWidth, false);
     1976                        }
     1977                }
     1978        }
     1979       
     1980        public void justify(boolean fixWidth) {
     1981                Integer width = FrameGraphics.getMaxFrameSize().width;
     1982
     1983                // Check if that text item is inside an enclosing rectangle...
     1984                // Set its max width accordingly
     1985                Polygon enclosure = FrameUtils.getEnlosingPolygon();
     1986                if (enclosure != null) {
     1987                        Rectangle bounds = enclosure.getBounds();
     1988                        if(bounds.width > 200 && getX() < bounds.width / 3 + bounds.x){
     1989                                width = bounds.x + bounds.width;
     1990                        }
     1991                }
     1992
     1993                if (getWidth() == null)
     1994                        setRightMargin(width, fixWidth);
     1995
     1996                // Check for the annotation that restricts the width of text items
     1997                // on the frame
     1998                String widthString;
     1999                if ((widthString = getParentOrCurrentFrame().getAnnotationValue(
     2000                                "maxwidth")) != null) {
     2001                        try {
     2002                                int oldWidth = getWidth();
     2003                                int maxWidth = Integer.parseInt(widthString);
     2004                                if (maxWidth < oldWidth)
     2005                                        setWidth(maxWidth);
     2006                        } catch (NumberFormatException nfe) {
     2007                        }
     2008
     2009                }
    19452010        }
    19462011
     
    22102275        public void scale(Float scale, int originX, int originY) {
    22112276                setSize(getSize() * scale);
     2277
    22122278                setWidth(Math.round(getWidth() * scale));
    22132279                super.scale(scale, originX, originY);
Note: See TracChangeset for help on using the changeset viewer.