Changeset 400 for trunk/src/org/expeditee/items/Text.java
- Timestamp:
- 11/18/08 13:34:54 (16 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/expeditee/items/Text.java
r390 r400 47 47 */ 48 48 public class Text extends Item { 49 private static final int ADJUST_WIDTH_THRESHOLD = 200; 50 49 51 public static String LINE_SEPARATOR = System.getProperty("line.separator"); 50 52 … … 98 100 public static final int PAGE_UP = 8; 99 101 100 private int _maxWidth = -1;102 private Integer _maxWidth = null; 101 103 102 104 private Justification _justification = Justification.left; … … 181 183 */ 182 184 @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) { 188 189 setJustification(Justification.left); 190 setRightMargin(FrameGraphics.getMaxFrameSize().width, false); 191 return; 189 192 } 190 193 … … 194 197 } 195 198 199 public Integer getWidthToSave() { 200 return _maxWidth; 201 } 202 196 203 /** 197 204 * 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 200 206 * 201 207 * @return The maximum width of this Text item when justification is used 202 208 */ 203 209 @Override 204 public int getWidth() { 210 public Integer getWidth() { 211 if (_maxWidth == null || _maxWidth <= 0) 212 return null; 205 213 return _maxWidth; 214 } 215 216 public Integer getAbsoluteWidth() { 217 if (_maxWidth == null) { 218 return Integer.MAX_VALUE; 219 } 220 221 return Math.abs(_maxWidth); 206 222 } 207 223 … … 224 240 225 241 // Only justification left works with 0 width 226 if (just != Justification.left && getWidth() <= 0) {227 228 229 }242 // if (just != null && just != Justification.left && !hasWidth()) { 243 // // TODO Tighten this up so it subtracts the margin widths 244 // setWidth(getBoundsWidth()); 245 // } 230 246 231 247 _justification = just; … … 247 263 248 264 private int getJustOffset(TextLayout layout) { 249 if (getWidth() < 0)250 return 0;251 252 265 if (getJustification() == Justification.center) 253 return (int) ((get Width() - layout.getAdvance()) / 2);266 return (int) ((getAbsoluteWidth() - layout.getAdvance()) / 2); 254 267 else if (getJustification() == Justification.right) 255 return (int) (get Width() - layout.getAdvance());268 return (int) (getAbsoluteWidth() - layout.getAdvance()); 256 269 257 270 return 0; … … 269 282 setText(text, false); 270 283 } 271 284 272 285 public void setText(String text, Boolean clearCache) { 273 286 // if (_text != null && text.length() < _text.length()) … … 279 292 * mode 280 293 */ 281 if (clearCache){294 if (clearCache) { 282 295 clearCache(); 283 } 284 296 } 297 285 298 rebuild(true); 286 299 invalidateAll(); … … 1314 1327 // If its justification right or center then DONT limit the width 1315 1328 if (getJustification() != null) { 1316 maxX = Item.MARGIN_RIGHT + get Width();1329 maxX = Item.MARGIN_RIGHT + getAbsoluteWidth(); 1317 1330 } 1318 1331 … … 1368 1381 1369 1382 if (limitWidth) { 1370 if (getWidth() > 0) 1371 width = getWidth(); 1383 width = getAbsoluteWidth(); 1372 1384 // else if (getMaxWidth() > 0) 1373 1385 // width = Math.max(50, getMaxWidth() - getX() … … 1400 1412 _lineOffsets.add(_lineBreaker.getPosition()); 1401 1413 1402 if ( getWidth() > 0 &&getJustification() == Justification.full1414 if (/* hasWidth() && */getJustification() == Justification.full 1403 1415 && _lineBreaker.getPosition() < _text.length()) 1404 1416 layout = layout.getJustifiedLayout(width); … … 1409 1421 updatePolygon(); 1410 1422 1423 } 1424 1425 private boolean hasFixedWidth() { 1426 assert (_maxWidth != null); 1427 if(_maxWidth == null){ 1428 justify(false); 1429 } 1430 return _maxWidth > 0; 1411 1431 } 1412 1432 … … 1658 1678 copy.setSpacing(getSpacing()); 1659 1679 copy.setInitialSpacing(getInitialSpacing()); 1680 1681 copy.setWidth(getWidth()); 1660 1682 copy.setJustification(getJustification()); 1661 1683 copy.setLetterSpacing(getLetterSpacing()); … … 1669 1691 } 1670 1692 copy.setHidden(!isVisible()); 1671 // copy.setWidth(getWidth());1672 1693 return copy; 1673 1694 } … … 1856 1877 setPosition(MARGIN_LEFT, MARGIN_LEFT + getBoundsHeight()); 1857 1878 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); 1861 1881 } 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); 1864 1885 } 1865 1886 } … … 1943 1964 if (isLineEnd()) 1944 1965 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 } 1945 2010 } 1946 2011 … … 2210 2275 public void scale(Float scale, int originX, int originY) { 2211 2276 setSize(getSize() * scale); 2277 2212 2278 setWidth(Math.round(getWidth() * scale)); 2213 2279 super.scale(scale, originX, originY);
Note:
See TracChangeset
for help on using the changeset viewer.