39 |
package javax.swing.text; |
package javax.swing.text; |
40 |
|
|
41 |
import java.awt.Color; |
import java.awt.Color; |
42 |
|
import java.awt.Component; |
43 |
|
import java.awt.Font; |
44 |
import java.awt.FontMetrics; |
import java.awt.FontMetrics; |
45 |
import java.awt.Graphics; |
import java.awt.Graphics; |
46 |
import java.awt.Rectangle; |
import java.awt.Rectangle; |
50 |
public class PlainView extends View |
public class PlainView extends View |
51 |
implements TabExpander |
implements TabExpander |
52 |
{ |
{ |
53 |
|
private Color selectedColor; |
54 |
|
private Color unselectedColor; |
55 |
|
private Font font; |
56 |
|
|
57 |
protected FontMetrics metrics; |
protected FontMetrics metrics; |
58 |
|
|
59 |
public PlainView(Element elem) |
public PlainView(Element elem) |
60 |
{ |
{ |
61 |
super(elem); |
super(elem); |
62 |
} |
} |
63 |
|
|
64 |
|
/** |
65 |
|
* @since 1.4 |
66 |
|
*/ |
67 |
|
protected void updateMetrics() |
68 |
|
{ |
69 |
|
Component component = getContainer(); |
70 |
|
Font font = component.getFont(); |
71 |
|
|
72 |
|
if (this.font != font) |
73 |
|
{ |
74 |
|
this.font = font; |
75 |
|
metrics = component.getFontMetrics(font); |
76 |
|
} |
77 |
|
} |
78 |
|
|
79 |
|
/** |
80 |
|
* @since 1.4 |
81 |
|
*/ |
82 |
|
protected Rectangle lineToRect(Shape a, int line) |
83 |
|
{ |
84 |
|
// Ensure metrics are up-to-date. |
85 |
|
updateMetrics(); |
86 |
|
|
87 |
|
Rectangle rect = a.getBounds(); |
88 |
|
int fontHeight = metrics.getHeight(); |
89 |
|
return new Rectangle(rect.x, rect.y + (line * fontHeight), |
90 |
|
rect.width, fontHeight); |
91 |
|
} |
92 |
|
|
93 |
|
public Shape modelToView(int position, Shape a, Position.Bias b) |
94 |
|
throws BadLocationException |
95 |
|
{ |
96 |
|
Document document = getDocument(); |
97 |
|
|
98 |
|
// Get rectangle of the line containing position. |
99 |
|
int lineIndex = getElement().getElementIndex(position); |
100 |
|
Rectangle rect = lineToRect(a, lineIndex); |
101 |
|
|
102 |
|
// Get the rectangle for position. |
103 |
|
Element line = getElement().getElement(lineIndex); |
104 |
|
int lineStart = line.getStartOffset(); |
105 |
|
Segment segment = new Segment(); |
106 |
|
document.getText(lineStart, position - lineStart, segment); |
107 |
|
int xoffset = Utilities.getTabbedTextWidth(segment, metrics, rect.x, |
108 |
|
this, lineStart); |
109 |
|
|
110 |
|
// Calc the real rectangle. |
111 |
|
rect.x += xoffset; |
112 |
|
rect.width = 1; |
113 |
|
rect.height = metrics.getHeight(); |
114 |
|
|
115 |
|
return rect; |
116 |
|
} |
117 |
|
|
118 |
public void drawLine(int lineIndex, Graphics g, int x, int y) |
public void drawLine(int lineIndex, Graphics g, int x, int y) |
119 |
{ |
{ |
133 |
public int drawSelectedText(Graphics g, int x, int y, int p0, int p1) |
public int drawSelectedText(Graphics g, int x, int y, int p0, int p1) |
134 |
throws BadLocationException |
throws BadLocationException |
135 |
{ |
{ |
136 |
String text = getDocument().getText(p0, p1); |
g.setColor(selectedColor); |
137 |
g.setColor(Color.WHITE); |
Segment segment = new Segment(); |
138 |
g.drawString(text, x, y); |
getDocument().getText(p0, p1 - p0, segment); |
139 |
return metrics.stringWidth(text); |
return Utilities.drawTabbedText(segment, x, y, g, this, 0); |
140 |
} |
} |
141 |
|
|
142 |
public int drawUnselectedText(Graphics g, int x, int y, int p0, int p1) |
public int drawUnselectedText(Graphics g, int x, int y, int p0, int p1) |
143 |
throws BadLocationException |
throws BadLocationException |
144 |
{ |
{ |
145 |
String text = getDocument().getText(p0, p1); |
g.setColor(unselectedColor); |
146 |
g.setColor(Color.BLACK); |
Segment segment = new Segment(); |
147 |
g.drawString(text, x, y); |
getDocument().getText(p0, p1 - p0, segment); |
148 |
return metrics.stringWidth(text); |
return Utilities.drawTabbedText(segment, x, y, g, this, 0); |
149 |
} |
} |
150 |
|
|
151 |
public void paint(Graphics g, Shape s) |
public void paint(Graphics g, Shape s) |
152 |
{ |
{ |
153 |
System.out.println("Michael: PlainView.paint"); |
JTextComponent textComponent = (JTextComponent) getContainer(); |
154 |
|
|
155 |
|
g.setFont(textComponent.getFont()); |
156 |
|
selectedColor = textComponent.getSelectedTextColor(); |
157 |
|
unselectedColor = textComponent.getForeground(); |
158 |
|
|
159 |
Rectangle rect = s.getBounds(); |
Rectangle rect = s.getBounds(); |
160 |
|
|
|
g.setColor(Color.WHITE); |
|
|
g.fillRect(rect.x, rect.y, rect.width, rect.height); |
|
|
|
|
161 |
// FIXME: Text may be scrolled. |
// FIXME: Text may be scrolled. |
162 |
drawLine(0, g, rect.x, rect.y); |
drawLine(0, g, rect.x, rect.y); |
163 |
} |
} |
167 |
return 8; |
return 8; |
168 |
} |
} |
169 |
|
|
170 |
|
/** |
171 |
|
* Returns the next tab stop position after a given reference position. |
172 |
|
* |
173 |
|
* This implementation ignores the <code>tabStop</code> argument. |
174 |
|
* |
175 |
|
* @param x the current x position in pixels |
176 |
|
* @param tabStop the position within the text stream that the tab occured at |
177 |
|
*/ |
178 |
public float nextTabStop(float x, int tabStop) |
public float nextTabStop(float x, int tabStop) |
179 |
{ |
{ |
180 |
System.out.println("Michael: PlainView.nextTabpStop: missing implementation"); |
float tabSizePixels = getTabSize() + metrics.charWidth('m'); |
181 |
return x; |
return (float) (Math.floor(x / tabSizePixels) + 1) * tabSizePixels; |
182 |
} |
} |
183 |
|
|
184 |
public float getPreferredSpan(int axis) |
public float getPreferredSpan(int axis) |
188 |
|
|
189 |
return 10; |
return 10; |
190 |
} |
} |
|
} |
|
191 |
|
} |
192 |
|
|