1 |
/* BasicTextUI.java |
/* BasicTextUI.java |
2 |
Copyright (C) 2002, 2003 Free Software Foundation, Inc. |
Copyright (C) 2002, 2003, 2004 Free Software Foundation, Inc. |
3 |
|
|
4 |
This file is part of GNU Classpath. |
This file is part of GNU Classpath. |
5 |
|
|
35 |
obligated to do so. If you do not wish to do so, delete this |
obligated to do so. If you do not wish to do so, delete this |
36 |
exception statement from your version. */ |
exception statement from your version. */ |
37 |
|
|
38 |
|
|
39 |
package javax.swing.plaf.basic; |
package javax.swing.plaf.basic; |
40 |
|
|
41 |
import java.awt.Color; |
import java.awt.Color; |
42 |
|
import java.awt.Container; |
43 |
import java.awt.Dimension; |
import java.awt.Dimension; |
44 |
import java.awt.Graphics; |
import java.awt.Graphics; |
45 |
|
import java.awt.Insets; |
46 |
import java.awt.Point; |
import java.awt.Point; |
47 |
import java.awt.Rectangle; |
import java.awt.Rectangle; |
48 |
|
import java.awt.Shape; |
49 |
|
|
50 |
import javax.swing.JComponent; |
import javax.swing.JComponent; |
51 |
import javax.swing.plaf.ComponentUI; |
import javax.swing.plaf.ComponentUI; |
52 |
import javax.swing.plaf.TextUI; |
import javax.swing.plaf.TextUI; |
53 |
|
import javax.swing.plaf.UIResource; |
54 |
import javax.swing.text.BadLocationException; |
import javax.swing.text.BadLocationException; |
55 |
|
import javax.swing.text.Caret; |
56 |
|
import javax.swing.text.DefaultCaret; |
57 |
import javax.swing.text.DefaultEditorKit; |
import javax.swing.text.DefaultEditorKit; |
58 |
|
import javax.swing.text.DefaultHighlighter; |
59 |
|
import javax.swing.text.Document; |
60 |
import javax.swing.text.EditorKit; |
import javax.swing.text.EditorKit; |
61 |
import javax.swing.text.Element; |
import javax.swing.text.Element; |
62 |
|
import javax.swing.text.Highlighter; |
63 |
import javax.swing.text.JTextComponent; |
import javax.swing.text.JTextComponent; |
64 |
|
import javax.swing.text.PlainDocument; |
65 |
import javax.swing.text.Position; |
import javax.swing.text.Position; |
66 |
import javax.swing.text.View; |
import javax.swing.text.View; |
67 |
import javax.swing.text.ViewFactory; |
import javax.swing.text.ViewFactory; |
68 |
|
|
69 |
|
|
70 |
public class BasicTextUI extends TextUI |
public abstract class BasicTextUI extends TextUI |
71 |
implements ViewFactory |
implements ViewFactory |
72 |
{ |
{ |
73 |
int gap = 3; |
public static class BasicCaret extends DefaultCaret |
74 |
View view = null; // was: new RootView(); |
implements UIResource |
75 |
Color textColor; |
{ |
76 |
Color disabledTextColor; |
public BasicCaret() |
77 |
Color normalBackgroundColor; |
{ |
78 |
EditorKit kit = new DefaultEditorKit(); |
} |
79 |
|
} |
80 |
|
|
81 |
|
public static class BasicHighlighter extends DefaultHighlighter |
82 |
|
implements UIResource |
83 |
|
{ |
84 |
|
public BasicHighlighter() |
85 |
|
{ |
86 |
|
} |
87 |
|
} |
88 |
|
|
89 |
/* ***************************************************************** |
private class RootView extends View |
|
* This View is way too incomplete to be of any use. To avoid errors |
|
|
* when compiling with the Sun JDK, it has been commented out. |
|
|
* -- Sascha Brawer (brawer@dandelis.ch) |
|
|
* |
|
|
* (begin of commented out section) |
|
|
class RootView extends View |
|
90 |
{ |
{ |
91 |
RootView() |
private JTextComponent textComponent; |
92 |
|
private View view; |
93 |
|
|
94 |
|
public RootView(JTextComponent parent) |
95 |
{ |
{ |
96 |
super(null); |
super(null); |
97 |
|
textComponent = parent; |
98 |
} |
} |
99 |
public void paint(Graphics g, Shape s) |
|
100 |
|
public void setView(View v) |
101 |
{ |
{ |
102 |
if (view != null) |
if (view != null) |
103 |
|
view.setParent(null); |
104 |
|
|
105 |
|
if (v != null) |
106 |
|
v.setParent(null); |
107 |
|
|
108 |
|
view = v; |
109 |
|
} |
110 |
|
|
111 |
|
public Container getContainer() |
112 |
{ |
{ |
113 |
Rectangle r = s.getBounds(); |
return textComponent; |
114 |
|
} |
115 |
|
|
116 |
|
public float getPreferredSpan(int axis) |
117 |
|
{ |
118 |
|
if (view != null) |
119 |
|
return view.getPreferredSpan(axis); |
120 |
|
|
121 |
view.setSize((int)r.getWidth(), |
return Integer.MAX_VALUE; |
|
(int)r.getHeight()); |
|
|
view.paint(g, s); |
|
122 |
} |
} |
123 |
|
|
124 |
|
public void paint(Graphics g, Shape s) |
125 |
|
{ |
126 |
|
System.out.println("Michael: BasicTextUI.RootView.paint"); |
127 |
|
|
128 |
|
if (view != null) |
129 |
|
view.paint(g, s); |
130 |
} |
} |
131 |
} |
} |
132 |
* (end of commented out section) |
|
133 |
*************************************************************** */ |
RootView rootView; |
134 |
|
JTextComponent textComponent; |
135 |
|
int gap = 3; |
136 |
|
EditorKit kit = new DefaultEditorKit(); |
137 |
|
|
138 |
public BasicTextUI() |
public BasicTextUI() |
139 |
{ |
{ |
140 |
} |
} |
141 |
|
|
142 |
public static ComponentUI createUI(final JComponent c) |
protected Caret createCaret() |
143 |
{ |
{ |
144 |
return new BasicTextUI(); |
return new BasicCaret(); |
145 |
|
} |
146 |
|
|
147 |
|
protected Highlighter createHighlighter() |
148 |
|
{ |
149 |
|
return new BasicHighlighter(); |
150 |
|
} |
151 |
|
|
152 |
|
protected final JTextComponent getComponent() |
153 |
|
{ |
154 |
|
return textComponent; |
155 |
} |
} |
156 |
|
|
157 |
public void installUI(final JComponent c) |
public void installUI(final JComponent c) |
158 |
{ |
{ |
159 |
super.installUI(c); |
super.installUI(c); |
160 |
|
c.setOpaque(true); |
161 |
|
|
162 |
textColor = new Color(0, 0, 0); |
textComponent = (JTextComponent) c; |
163 |
disabledTextColor = new Color(130, 130, 130); |
|
164 |
normalBackgroundColor = new Color(192, 192, 192); |
Document doc = textComponent.getDocument(); |
165 |
|
if (doc == null) |
166 |
|
{ |
167 |
|
doc = new PlainDocument(); |
168 |
|
textComponent.setDocument(doc); |
169 |
} |
} |
170 |
|
|
171 |
public Dimension getPreferredSize(JComponent c) |
rootView = new RootView(textComponent); |
172 |
|
setView(create(doc.getDefaultRootElement())); |
173 |
|
|
174 |
|
installDefaults(); |
175 |
|
installListeners(); |
176 |
|
installKeyboardActions(); |
177 |
|
} |
178 |
|
|
179 |
|
protected void installDefaults() |
180 |
|
{ |
181 |
|
} |
182 |
|
|
183 |
|
protected void installListeners() |
184 |
{ |
{ |
185 |
JTextComponent b = (JTextComponent) c; |
} |
186 |
|
|
187 |
View v = getRootView(b); |
protected void installKeyboardActions() |
188 |
|
{ |
189 |
|
} |
190 |
|
|
191 |
|
public void uninstallUI(final JComponent c) |
192 |
|
{ |
193 |
|
super.uninstallUI(c); |
194 |
|
rootView = null; |
195 |
|
|
196 |
|
uninstallDefaults(); |
197 |
|
uninstallListeners(); |
198 |
|
uninstallKeyboardActions(); |
199 |
|
} |
200 |
|
|
201 |
|
protected void uninstallDefaults() |
202 |
|
{ |
203 |
|
} |
204 |
|
|
205 |
|
protected void uninstallListeners() |
206 |
|
{ |
207 |
|
} |
208 |
|
|
209 |
|
protected void uninstallKeyboardActions() |
210 |
|
{ |
211 |
|
} |
212 |
|
|
213 |
|
protected abstract String getPropertyPrefix(); |
214 |
|
|
215 |
|
public Dimension getPreferredSize(JComponent c) |
216 |
|
{ |
217 |
|
View v = getRootView(textComponent); |
218 |
|
|
219 |
float w = v.getPreferredSpan(View.X_AXIS); |
float w = v.getPreferredSpan(View.X_AXIS); |
220 |
float h = v.getPreferredSpan(View.Y_AXIS); |
float h = v.getPreferredSpan(View.Y_AXIS); |
222 |
return new Dimension((int) w, (int) h); |
return new Dimension((int) w, (int) h); |
223 |
} |
} |
224 |
|
|
225 |
public void paint(Graphics g, JComponent c) |
public final void paint(Graphics g, JComponent c) |
226 |
{ |
{ |
227 |
// view.paint( |
paintSafely(g); |
228 |
|
} |
229 |
|
|
230 |
|
protected void paintSafely(Graphics g) |
231 |
|
{ |
232 |
|
Caret caret = textComponent.getCaret(); |
233 |
|
Highlighter highlighter = textComponent.getHighlighter(); |
234 |
|
|
235 |
|
if (textComponent.isOpaque()) |
236 |
|
paintBackground(g); |
237 |
|
|
238 |
|
rootView.paint(g, getVisibleEditorRect()); |
239 |
|
|
240 |
|
if (highlighter != null) |
241 |
|
highlighter.paint(g); |
242 |
|
|
243 |
|
if (caret != null) |
244 |
|
caret.paint(g); |
245 |
|
} |
246 |
|
|
247 |
|
protected void paintBackground(Graphics g) |
248 |
|
{ |
249 |
|
g.setColor(Color.WHITE); // FIXME: set background color |
250 |
|
g.fillRect(0, 0, textComponent.getWidth(), textComponent.getHeight()); |
251 |
} |
} |
252 |
|
|
253 |
public void damageRange(JTextComponent t, int p0, int p1) |
public void damageRange(JTextComponent t, int p0, int p1) |
275 |
|
|
276 |
public View getRootView(JTextComponent t) |
public View getRootView(JTextComponent t) |
277 |
{ |
{ |
278 |
return view; |
return rootView; |
279 |
} |
} |
280 |
|
|
281 |
public Rectangle modelToView(JTextComponent t, int pos) |
public Rectangle modelToView(JTextComponent t, int pos) |
305 |
// subclasses have to implement this to get this functionality |
// subclasses have to implement this to get this functionality |
306 |
return null; |
return null; |
307 |
} |
} |
308 |
|
|
309 |
|
public View create(Element elem, int p0, int p1) |
310 |
|
{ |
311 |
|
// subclasses have to implement this to get this functionality |
312 |
|
return null; |
313 |
|
} |
314 |
|
|
315 |
|
protected Rectangle getVisibleEditorRect() |
316 |
|
{ |
317 |
|
int width = textComponent.getWidth(); |
318 |
|
int height = textComponent.getHeight(); |
319 |
|
|
320 |
|
if (width <= 0 || height <= 0) |
321 |
|
return null; |
322 |
|
|
323 |
|
Insets insets = textComponent.getInsets(); |
324 |
|
return new Rectangle(insets.left, insets.top, |
325 |
|
width - insets.left + insets.right, |
326 |
|
height - insets.top + insets.bottom); |
327 |
|
} |
328 |
|
|
329 |
|
protected final void setView(View view) |
330 |
|
{ |
331 |
|
rootView.setView(view); |
332 |
|
view.setParent(rootView); |
333 |
|
} |
334 |
} |
} |