001: package com.xoetrope.swing;
002:
003: import java.awt.Component;
004: import java.awt.Container;
005: import java.awt.Dimension;
006: import java.awt.Point;
007: import java.awt.Polygon;
008: import java.awt.Rectangle;
009: import java.awt.geom.Area;
010: import java.util.StringTokenizer;
011: import net.xoetrope.xui.XAttributedComponent;
012:
013: /**
014: * A component that flows text in a user defined area. The area is a polygon and
015: * need not be composed of rectangular blocks.
016: *
017: * <p> Copyright (c) Xoetrope Ltd., 2001-2006, This software is licensed under
018: * the GNU Public License (GPL), please see license.txt for more details. If
019: * you make commercial use of this software you must purchase a commercial
020: * license from Xoetrope.</p>
021: * <p> $Revision: 1.14 $</p>
022: */
023: public class XPolygonalTextArea extends XFlowedTextComponent implements
024: XAttributedComponent {
025: /**
026: * Create a new polygonal test area
027: */
028: public XPolygonalTextArea() {
029: }
030:
031: /**
032: * Set one or more attributes of the component. Currently this handles the
033: * attributes
034: * <OL>
035: * <li>text - set the text</li>
036: * <li>content - set the text</li>
037: * <li>increment - set the step size</li>
038: * <li>points - the set of points that forms the polygon into which the text
039: * is flowed</li>
040: * </OL>
041: * @param attribName the attribute name
042: * @param attribValue the attribute value
043: * @return 0 for success, non zero otherwise
044: */
045: public int setAttribute(String attribName, Object attribValue) {
046: String attribNameLwr = attribName.toLowerCase();
047: String attribValueStr = (String) attribValue;
048: String attribValueLwr = attribValueStr.toLowerCase();
049: if (attribNameLwr.equals("points"))
050: setPoints(attribValueStr);
051:
052: super .setAttribute(attribName, attribValue);
053:
054: return 0;
055: }
056:
057: /**
058: * Attempts to load and reference the associated view/datastore.
059: */
060: public void init() {
061: super .init();
062: Point p = getLocation();
063: Dimension size = getSize();
064: oX = p.x;
065: oY = p.y;
066: oW = size.width;
067: oH = size.height;
068:
069: if (polyArea == null)
070: polyArea = new Polygon();
071:
072: flowArea = new Area(polyArea);
073: if (!clip)
074: return;
075:
076: Container parent = (Container) getParent();
077: int count = parent.getComponentCount();
078: try {
079: for (int i = 0; i < count; i++) {
080: Component cc = parent.getComponent(i);
081: if ((cc != this ) && cc.isVisible()) {
082: Rectangle rCC = cc.getBounds();
083: rCC.grow(10, 15);
084: if (flowArea != null)
085: flowArea.subtract(new Area(rCC));
086: }
087: }
088: } catch (Exception ex1) {
089: }
090: }
091:
092: /**
093: * Add a point to teh polygon
094: * @param x the x position of the point
095: * @param y the y position of the point
096: */
097: public void addPoint(int x, int y) {
098: Point p = getLocation();
099: if (polyArea == null)
100: polyArea = new Polygon();
101: polyArea.addPoint(p.x + x, p.y + y);
102: }
103:
104: /**
105: * Set the polygon point as a string of comma separated x-y values
106: * @param pts the xy pairs
107: */
108: public void setPoints(String pts) {
109: StringTokenizer tokenizer = new StringTokenizer(pts, ",");
110: while (tokenizer.hasMoreTokens()) {
111: int x = Integer.parseInt(tokenizer.nextToken());
112: int y = Integer.parseInt(tokenizer.nextToken());
113: addPoint(x, y);
114: }
115: }
116:
117: public String getPoints() {
118: String s = "";
119: if (polyArea != null) {
120: for (int i = 0; i < polyArea.npoints; i++)
121: s += (i > 0 ? "," : "") + polyArea.xpoints[i] + ","
122: + polyArea.ypoints[i];
123: }
124: return s;
125: }
126:
127: /**
128: * The polygonal area
129: */
130: protected Polygon polyArea;
131: }
|