Merge from mainline (gomp-merge-2005-02-26).
[official-gcc.git] / libjava / javax / swing / text / PlainView.java
blobc852c1d69b42d26fac9e5998f976907f4a807700
1 /* PlainView.java --
2 Copyright (C) 2004, 2005 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19 02111-1307 USA.
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library. Thus, the terms and
23 conditions of the GNU General Public License cover the whole
24 combination.
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module. An independent module is a module which is not derived from
33 or based on this library. If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so. If you do not wish to do so, delete this
36 exception statement from your version. */
39 package javax.swing.text;
41 import java.awt.Color;
42 import java.awt.Component;
43 import java.awt.Font;
44 import java.awt.FontMetrics;
45 import java.awt.Graphics;
46 import java.awt.Rectangle;
47 import java.awt.Shape;
49 public class PlainView extends View
50 implements TabExpander
52 Color selectedColor;
53 Color unselectedColor;
54 Font font;
56 protected FontMetrics metrics;
58 public PlainView(Element elem)
60 super(elem);
63 /**
64 * @since 1.4
66 protected void updateMetrics()
68 Component component = getContainer();
69 Font font = component.getFont();
71 if (this.font != font)
73 this.font = font;
74 metrics = component.getFontMetrics(font);
78 /**
79 * @since 1.4
81 protected Rectangle lineToRect(Shape a, int line)
83 // Ensure metrics are up-to-date.
84 updateMetrics();
86 Rectangle rect = a.getBounds();
87 int fontHeight = metrics.getHeight();
88 return new Rectangle(rect.x, rect.y + (line * fontHeight),
89 rect.width, fontHeight);
92 public Shape modelToView(int position, Shape a, Position.Bias b)
93 throws BadLocationException
95 // Ensure metrics are up-to-date.
96 updateMetrics();
98 Document document = getDocument();
100 // Get rectangle of the line containing position.
101 int lineIndex = getElement().getElementIndex(position);
102 Rectangle rect = lineToRect(a, lineIndex);
104 // Get the rectangle for position.
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);
112 // Calc the real rectangle.
113 rect.x += xoffset;
114 rect.width = 1;
115 rect.height = metrics.getHeight();
117 return rect;
120 protected void drawLine(int lineIndex, Graphics g, int x, int y)
124 metrics = g.getFontMetrics();
125 // FIXME: Selected text are not drawn yet.
126 Element line = getDocument().getDefaultRootElement().getElement(lineIndex);
127 drawUnselectedText(g, x, y, line.getStartOffset(), line.getEndOffset());
128 //drawSelectedText(g, , , , );
130 catch (BadLocationException e)
132 // This should never happen.
136 protected int drawSelectedText(Graphics g, int x, int y, int p0, int p1)
137 throws BadLocationException
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);
145 protected int drawUnselectedText(Graphics g, int x, int y, int p0, int p1)
146 throws BadLocationException
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, 0);
154 public void paint(Graphics g, Shape s)
156 // Ensure metrics are up-to-date.
157 updateMetrics();
159 JTextComponent textComponent = (JTextComponent) getContainer();
161 g.setFont(textComponent.getFont());
162 selectedColor = textComponent.getSelectedTextColor();
163 unselectedColor = textComponent.getForeground();
165 Rectangle rect = s.getBounds();
167 // FIXME: Text may be scrolled.
168 Document document = textComponent.getDocument();
169 Element root = document.getDefaultRootElement();
170 int y = rect.y;
172 for (int i = 0; i < root.getElementCount(); i++)
174 drawLine(i, g, rect.x, y);
175 y += metrics.getHeight();
179 protected int getTabSize()
181 return 8;
185 * Returns the next tab stop position after a given reference position.
187 * This implementation ignores the <code>tabStop</code> argument.
189 * @param x the current x position in pixels
190 * @param tabStop the position within the text stream that the tab occured at
192 public float nextTabStop(float x, int tabStop)
194 float tabSizePixels = getTabSize() + metrics.charWidth('m');
195 return (float) (Math.floor(x / tabSizePixels) + 1) * tabSizePixels;
198 public float getPreferredSpan(int axis)
200 if (axis != X_AXIS && axis != Y_AXIS)
201 throw new IllegalArgumentException();
203 return 10;