1:
37:
38:
39: package ;
40:
41: import ;
42: import ;
43: import ;
44: import ;
45: import ;
46: import ;
47: import ;
48:
49: public class PlainView extends View
50: implements TabExpander
51: {
52: Color selectedColor;
53: Color unselectedColor;
54: Font font;
55:
56: protected FontMetrics metrics;
57:
58: public PlainView(Element elem)
59: {
60: super(elem);
61: }
62:
63:
66: protected void updateMetrics()
67: {
68: Component component = getContainer();
69: Font font = component.getFont();
70:
71: if (this.font != font)
72: {
73: this.font = font;
74: metrics = component.getFontMetrics(font);
75: }
76: }
77:
78:
81: protected Rectangle lineToRect(Shape a, int line)
82: {
83:
84: updateMetrics();
85:
86: Rectangle rect = a.getBounds();
87: int fontHeight = metrics.getHeight();
88: return new Rectangle(rect.x, rect.y + (line * fontHeight),
89: rect.width, fontHeight);
90: }
91:
92: public Shape modelToView(int position, Shape a, Position.Bias b)
93: throws BadLocationException
94: {
95:
96: updateMetrics();
97:
98: Document document = getDocument();
99:
100:
101: int lineIndex = getElement().getElementIndex(position);
102: Rectangle rect = lineToRect(a, lineIndex);
103:
104:
105: Element line = getElement().getElement(lineIndex);
106: int lineStart = line.getStartOffset();
107: Segment segment = new Segment();
108: document.getText(lineStart, position - lineStart, segment);
109: int xoffset = Utilities.getTabbedTextWidth(segment, metrics, rect.x,
110: this, lineStart);
111:
112:
113: rect.x += xoffset;
114: rect.width = 1;
115: rect.height = metrics.getHeight();
116:
117: return rect;
118: }
119:
120: protected void drawLine(int lineIndex, Graphics g, int x, int y)
121: {
122: try
123: {
124: metrics = g.getFontMetrics();
125:
126: Element line = getElement().getElement(lineIndex);
127: drawUnselectedText(g, x, y, line.getStartOffset(), line.getEndOffset());
128:
129: }
130: catch (BadLocationException e)
131: {
132:
133: }
134: }
135:
136: protected int drawSelectedText(Graphics g, int x, int y, int p0, int p1)
137: throws BadLocationException
138: {
139: g.setColor(selectedColor);
140: Segment segment = new Segment();
141: getDocument().getText(p0, p1 - p0, segment);
142: return Utilities.drawTabbedText(segment, x, y, g, this, 0);
143: }
144:
145: protected int drawUnselectedText(Graphics g, int x, int y, int p0, int p1)
146: throws BadLocationException
147: {
148: g.setColor(unselectedColor);
149: Segment segment = new Segment();
150: getDocument().getText(p0, p1 - p0, segment);
151: return Utilities.drawTabbedText(segment, x, y, g, this, segment.offset);
152: }
153:
154: public void paint(Graphics g, Shape s)
155: {
156:
157: updateMetrics();
158:
159: JTextComponent textComponent = (JTextComponent) getContainer();
160:
161: g.setFont(textComponent.getFont());
162: selectedColor = textComponent.getSelectedTextColor();
163: unselectedColor = textComponent.getForeground();
164:
165: Rectangle rect = s.getBounds();
166:
167:
168: Document document = textComponent.getDocument();
169: Element root = document.getDefaultRootElement();
170: int y = rect.y;
171:
172: for (int i = 0; i < root.getElementCount(); i++)
173: {
174: drawLine(i, g, rect.x, y);
175: y += metrics.getHeight();
176: }
177: }
178:
179: protected int getTabSize()
180: {
181: return 8;
182: }
183:
184:
192: public float nextTabStop(float x, int tabStop)
193: {
194: float tabSizePixels = getTabSize() + metrics.charWidth('m');
195: return (float) (Math.floor(x / tabSizePixels) + 1) * tabSizePixels;
196: }
197:
198: public float getPreferredSpan(int axis)
199: {
200: if (axis != X_AXIS && axis != Y_AXIS)
201: throw new IllegalArgumentException();
202:
203:
204: updateMetrics();
205:
206: float span = 0;
207: Element el = getElement();
208: Document doc = el.getDocument();
209: Segment seg = new Segment();
210:
211: switch (axis)
212: {
213: case X_AXIS:
214:
215: for (int i = 0; i < el.getElementCount(); i++)
216: {
217: Element child = el.getElement(i);
218: int start = child.getStartOffset();
219: int end = child.getEndOffset();
220: try {
221: doc.getText(start, start + end, seg);
222: }
223: catch (BadLocationException ex)
224: {
225:
226:
227: }
228: int width = metrics.charsWidth(seg.array, seg.offset, seg.count);
229: span = Math.max(span, width);
230: }
231: break;
232: case Y_AXIS:
233: default:
234: span = metrics.getHeight() * el.getElementCount();
235: break;
236: }
237:
238: return span;
239: }
240:
241:
253: public int viewToModel(float x, float y, Shape a, Position.Bias[] b)
254: {
255:
256: return 0;
257: }
258: }