diff options
Diffstat (limited to 'libjava/classpath/javax/swing/plaf/basic/BasicTextUI.java')
-rw-r--r-- | libjava/classpath/javax/swing/plaf/basic/BasicTextUI.java | 167 |
1 files changed, 121 insertions, 46 deletions
diff --git a/libjava/classpath/javax/swing/plaf/basic/BasicTextUI.java b/libjava/classpath/javax/swing/plaf/basic/BasicTextUI.java index 91ccb0056bb..b9de92640c8 100644 --- a/libjava/classpath/javax/swing/plaf/basic/BasicTextUI.java +++ b/libjava/classpath/javax/swing/plaf/basic/BasicTextUI.java @@ -38,6 +38,7 @@ exception statement from your version. */ package javax.swing.plaf.basic; +import java.awt.Color; import java.awt.Container; import java.awt.Dimension; import java.awt.Graphics; @@ -54,6 +55,8 @@ import javax.swing.Action; import javax.swing.ActionMap; import javax.swing.InputMap; import javax.swing.JComponent; +import javax.swing.LookAndFeel; +import javax.swing.SwingConstants; import javax.swing.SwingUtilities; import javax.swing.UIDefaults; import javax.swing.UIManager; @@ -73,7 +76,6 @@ import javax.swing.text.Element; import javax.swing.text.Highlighter; import javax.swing.text.JTextComponent; import javax.swing.text.Keymap; -import javax.swing.text.PlainView; import javax.swing.text.Position; import javax.swing.text.View; import javax.swing.text.ViewFactory; @@ -92,11 +94,11 @@ public abstract class BasicTextUI extends TextUI /** * A {@link DefaultCaret} that implements {@link UIResource}. */ - public static class BasicCaret extends DefaultCaret - implements UIResource + public static class BasicCaret extends DefaultCaret implements UIResource { public BasicCaret() { + // Nothing to do here. } } @@ -108,6 +110,7 @@ public abstract class BasicTextUI extends TextUI { public BasicHighlighter() { + // Nothing to do here. } } @@ -241,7 +244,7 @@ public abstract class BasicTextUI extends TextUI public void paint(Graphics g, Shape s) { if (view != null) - view.paint(g, s); + view.paint(g, s); } @@ -252,10 +255,10 @@ public abstract class BasicTextUI extends TextUI * * This is delegated to the real root view. * - * @param pos the position of the character in the model + * @param position the position of the character in the model * @param a the area that is occupied by the view - * @param bias either {@link Position.Bias.Forward} or - * {@link Position.Bias.Backward} depending on the preferred + * @param bias either {@link Position.Bias#Forward} or + * {@link Position.Bias#Backward} depending on the preferred * direction bias. If <code>null</code> this defaults to * <code>Position.Bias.Forward</code> * @@ -327,12 +330,41 @@ public abstract class BasicTextUI extends TextUI { view.changedUpdate(ev, shape, vf); } + + /** + * Returns the document position that is (visually) nearest to the given + * document position <code>pos</code> in the given direction <code>d</code>. + * + * @param c the text component + * @param pos the document position + * @param b the bias for <code>pos</code> + * @param d the direction, must be either {@link SwingConstants#NORTH}, + * {@link SwingConstants#SOUTH}, {@link SwingConstants#WEST} or + * {@link SwingConstants#EAST} + * @param biasRet an array of {@link Position.Bias} that can hold at least + * one element, which is filled with the bias of the return position + * on method exit + * + * @return the document position that is (visually) nearest to the given + * document position <code>pos</code> in the given direction + * <code>d</code> + * + * @throws BadLocationException if <code>pos</code> is not a valid offset in + * the document model + */ + public int getNextVisualPositionFrom(JTextComponent c, int pos, + Position.Bias b, int d, + Position.Bias[] biasRet) + throws BadLocationException + { + return view.getNextVisualPositionFrom(c, pos, b, d, biasRet); + } } /** * Receives notifications when properties of the text component change. */ - class UpdateHandler implements PropertyChangeListener + class PropertyChangeHandler implements PropertyChangeListener { /** * Notifies when a property of the text component changes. @@ -342,10 +374,12 @@ public abstract class BasicTextUI extends TextUI public void propertyChange(PropertyChangeEvent event) { if (event.getPropertyName().equals("document")) - { + { // Document changed. - modelChanged(); - } + modelChanged(); + } + + BasicTextUI.this.propertyChange(event); } } @@ -364,11 +398,10 @@ public abstract class BasicTextUI extends TextUI */ public void changedUpdate(DocumentEvent ev) { - Dimension size = textComponent.getSize(); - rootView.changedUpdate(ev, new Rectangle(0, 0, size.width, size.height), + rootView.changedUpdate(ev, getVisibleEditorRect(), rootView.getViewFactory()); } - + /** * Notification about a document insert event. * @@ -376,12 +409,8 @@ public abstract class BasicTextUI extends TextUI */ public void insertUpdate(DocumentEvent ev) { - Dimension size = textComponent.getSize(); - rootView.insertUpdate(ev, new Rectangle(0, 0, size.width, size.height), + rootView.insertUpdate(ev, getVisibleEditorRect(), rootView.getViewFactory()); - int caretPos = textComponent.getCaretPosition(); - if (caretPos >= ev.getOffset()) - textComponent.setCaretPosition(caretPos + ev.getLength()); } /** @@ -391,12 +420,8 @@ public abstract class BasicTextUI extends TextUI */ public void removeUpdate(DocumentEvent ev) { - Dimension size = textComponent.getSize(); - rootView.removeUpdate(ev, new Rectangle(0, 0, size.width, size.height), + rootView.removeUpdate(ev, getVisibleEditorRect(), rootView.getViewFactory()); - int caretPos = textComponent.getCaretPosition(); - if (caretPos >= ev.getOffset()) - textComponent.setCaretPosition(ev.getOffset()); } } @@ -419,16 +444,29 @@ public abstract class BasicTextUI extends TextUI /** * Receives notification when the model changes. */ - UpdateHandler updateHandler = new UpdateHandler(); + PropertyChangeHandler updateHandler = new PropertyChangeHandler(); /** The DocumentEvent handler. */ DocumentHandler documentHandler = new DocumentHandler(); /** + * The standard background color. This is the color which is used to paint + * text in enabled text components. + */ + Color background; + + /** + * The inactive background color. This is the color which is used to paint + * text in disabled text components. + */ + Color inactiveBackground; + + /** * Creates a new <code>BasicTextUI</code> instance. */ public BasicTextUI() { + // Nothing to do here. } /** @@ -506,14 +544,20 @@ public abstract class BasicTextUI extends TextUI textComponent.setHighlighter(createHighlighter()); String prefix = getPropertyPrefix(); - UIDefaults defaults = UIManager.getLookAndFeelDefaults(); - textComponent.setBackground(defaults.getColor(prefix + ".background")); - textComponent.setForeground(defaults.getColor(prefix + ".foreground")); - textComponent.setMargin(defaults.getInsets(prefix + ".margin")); - textComponent.setBorder(defaults.getBorder(prefix + ".border")); - textComponent.setFont(defaults.getFont(prefix + ".font")); - - caret.setBlinkRate(defaults.getInt(prefix + ".caretBlinkRate")); + LookAndFeel.installColorsAndFont(textComponent, prefix + ".background", + prefix + ".foreground", prefix + ".font"); + LookAndFeel.installBorder(textComponent, prefix + ".border"); + textComponent.setMargin(UIManager.getInsets(prefix + ".margin")); + + caret.setBlinkRate(UIManager.getInt(prefix + ".caretBlinkRate")); + + // Fetch the colors for enabled/disabled text components. + background = UIManager.getColor(prefix + ".background"); + inactiveBackground = UIManager.getColor(prefix + ".inactiveBackground"); + textComponent.setDisabledTextColor + (UIManager.getColor(prefix + ".inactiveForeground")); + textComponent.setSelectedTextColor(UIManager.getColor(prefix + ".selectionForeground")); + textComponent.setSelectionColor(UIManager.getColor(prefix + ".selectionBackground")); } /** @@ -704,6 +748,7 @@ public abstract class BasicTextUI extends TextUI protected void uninstallListeners() { textComponent.removeFocusListener(focuslistener); + textComponent.getDocument().removeDocumentListener(documentHandler); } /** @@ -757,6 +802,18 @@ public abstract class BasicTextUI extends TextUI } /** + * Returns the minimum size for text components. This returns the size + * of the component's insets. + * + * @return the minimum size for text components + */ + public Dimension getMinimumSize(JComponent c) + { + Insets i = c.getInsets(); + return new Dimension(i.left + i.right, i.top + i.bottom); + } + + /** * Paints the text component. * * @param g the <code>Graphics</code> context to paint to @@ -776,10 +833,10 @@ public abstract class BasicTextUI extends TextUI { Caret caret = textComponent.getCaret(); Highlighter highlighter = textComponent.getHighlighter(); - + if (textComponent.isOpaque()) paintBackground(g); - + if (highlighter != null && textComponent.getSelectionStart() != textComponent.getSelectionEnd()) highlighter.paint(g); @@ -797,8 +854,10 @@ public abstract class BasicTextUI extends TextUI */ protected void paintBackground(Graphics g) { - g.setColor(textComponent.getBackground()); - g.fillRect(0, 0, textComponent.getWidth(), textComponent.getHeight()); + // This method does nothing. All the background filling is done by the + // ComponentUI update method. However, the method is called by paint + // to provide a way for subclasses to draw something different (e.g. + // background images etc) on the background. } /** @@ -885,10 +944,10 @@ public abstract class BasicTextUI extends TextUI /** * Maps a position in the document into the coordinate space of the View. * The output rectangle usually reflects the font height but has a width - * of zero. A bias of {@link Position.Bias.Forward} is used in this method. + * of zero. A bias of {@link Position.Bias#Forward} is used in this method. * + * @param t the text component * @param pos the position of the character in the model - * @param a the area that is occupied by the view * * @return a rectangle that gives the location of the document position * inside the view coordinate space @@ -908,10 +967,10 @@ public abstract class BasicTextUI extends TextUI * The output rectangle usually reflects the font height but has a width * of zero. * + * @param t the text component * @param pos the position of the character in the model - * @param a the area that is occupied by the view - * @param bias either {@link Position.Bias.Forward} or - * {@link Position.Bias.Backward} depending on the preferred + * @param bias either {@link Position.Bias#Forward} or + * {@link Position.Bias#Backward} depending on the preferred * direction bias. If <code>null</code> this defaults to * <code>Position.Bias.Forward</code> * @@ -957,7 +1016,7 @@ public abstract class BasicTextUI extends TextUI */ public int viewToModel(JTextComponent t, Point pt, Position.Bias[] biasReturn) { - return 0; // FIXME: Implement me. + return rootView.viewToModel(pt.x, pt.y, getVisibleEditorRect(), biasReturn); } /** @@ -999,16 +1058,17 @@ public abstract class BasicTextUI extends TextUI */ protected Rectangle getVisibleEditorRect() { + JTextComponent textComponent = getComponent(); int width = textComponent.getWidth(); int height = textComponent.getHeight(); if (width <= 0 || height <= 0) - return null; + return new Rectangle(0, 0, 0, 0); Insets insets = textComponent.getInsets(); return new Rectangle(insets.left, insets.top, - width - insets.left + insets.right, - height - insets.top + insets.bottom); + width - insets.left - insets.right, + height - insets.top - insets.bottom); } /** @@ -1020,6 +1080,8 @@ public abstract class BasicTextUI extends TextUI { rootView.setView(view); view.setParent(rootView); + textComponent.revalidate(); + textComponent.repaint(); } /** @@ -1043,4 +1105,17 @@ public abstract class BasicTextUI extends TextUI View view = factory.create(elem); setView(view); } + + /** + * Receives notification whenever one of the text component's bound + * properties changes. This default implementation does nothing. + * It is a hook that enables subclasses to react to property changes + * on the text component. + * + * @param ev the property change event + */ + protected void propertyChange(PropertyChangeEvent ev) + { + // The default implementation does nothing. + } } |