- /*
- * @(#)SynthTextUI.java 1.11 03/02/18
- *
- * Copyright 2003 Sun Microsystems, Inc. All rights reserved.
- * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
- */
- package com.sun.java.swing.plaf.gtk;
- import javax.swing.*;
- import javax.swing.event.*;
- import javax.swing.text.*;
- import javax.swing.plaf.*;
- import javax.swing.border.Border;
- import java.beans.*;
- import java.awt.*;
- import java.awt.event.*;
- import java.awt.dnd.*;
- import java.awt.im.InputContext;
- import java.awt.datatransfer.*;
- import java.util.*;
- import java.io.*;
- /**
- * <p>
- * Basis of a text components look-and-feel in the Synth look and
- * feel. This provides the
- * basic editor view and controller services that may be useful
- * when creating a look-and-feel for an extension of
- * <code>JTextComponent</code>.
- * <p>
- * Most state is held in the associated <code>JTextComponent</code>
- * as bound properties, and the UI installs default values for the
- * various properties. This default will install something for
- * all of the properties. Typically, a LAF implementation will
- * do more however. At a minimum, a LAF would generally install
- * key bindings.
- * <p>
- * This class also provides some concurrency support if the
- * <code>Document</code> associated with the JTextComponent is a subclass of
- * <code>AbstractDocument</code>. Access to the View (or View hierarchy) is
- * serialized between any thread mutating the model and the Swing
- * event thread (which is expected to render, do model/view coordinate
- * translation, etc). <em>Any access to the root view should first
- * acquire a read-lock on the AbstractDocument and release that lock
- * in a finally block.</em>
- * <p>
- * An important method to define is the {@link #getPropertyPrefix} method
- * which is used as the basis of the keys used to fetch defaults
- * from the UIManager. The string should reflect the type of
- * TextUI (eg. TextField, TextArea, etc) without the particular
- * LAF part of the name (eg Metal, Motif, etc).
- * <p>
- * To build a view of the model, one of the following strategies
- * can be employed.
- * <ol>
- * <li>
- * One strategy is to simply redefine the
- * ViewFactory interface in the UI. By default, this UI itself acts
- * as the factory for View implementations. This is useful
- * for simple factories. To do this reimplement the
- * {@link #create} method.
- * <li>
- * A common strategy for creating more complex types of documents
- * is to have the EditorKit implementation return a factory. Since
- * the EditorKit ties all of the pieces necessary to maintain a type
- * of document, the factory is typically an important part of that
- * and should be produced by the EditorKit implementation.
- * <li>
- * A less common way to create more complex types is to have
- * the UI implementation create a.
- * separate object for the factory. To do this, the
- * {@link #createViewFactory} method should be reimplemented to
- * return some factory.
- * </ol>
- * <p>
- * <strong>Warning:</strong>
- * Serialized objects of this class will not be compatible with
- * future Swing releases. The current serialization support is
- * appropriate for short term storage or RMI between applications running
- * the same version of Swing. As of 1.4, support for long term storage
- * of all JavaBeans<sup><font size="-2">TM</font></sup>
- * has been added to the <code>java.beans</code> package.
- * Please see {@link java.beans.XMLEncoder}.
- *
- * @author Shannon Hickey
- * @version 1.11 02/18/03 (based on revision 1.82 of BasicTextUI)
- */
- abstract class SynthTextUI extends TextUI implements SynthUI, ViewFactory {
- private SynthStyle style;
- private static final EditorKit defaultKit = new DefaultEditorKit();
- transient JTextComponent editor;
- transient boolean painted;
- transient RootView rootView = new RootView();
- transient UpdateHandler updateHandler = new UpdateHandler();
- private static final TransferHandler defaultTransferHandler = new TextTransferHandler();
- private static DropTargetListener defaultDropTargetListener = null;
- private static final TextDragGestureRecognizer defaultDragRecognizer = new TextDragGestureRecognizer();
- private static final Position.Bias[] discardBias = new Position.Bias[1];
- public static class SynthCaret extends DefaultCaret implements UIResource {}
- public static class SynthHighlighter extends DefaultHighlighter implements UIResource {}
- void forceFetchStyle(JTextComponent comp) {
- style = null;
- fetchStyle(comp);
- }
- private void fetchStyle(JTextComponent comp) {
- SynthContext context = getContext(comp, ENABLED);
- SynthStyle oldStyle = style;
- style = SynthLookAndFeel.updateStyle(context, this);
- if (style != oldStyle) {
- String prefix = getPropertyPrefix();
- Color color = editor.getCaretColor();
- if (color == null || color instanceof UIResource) {
- editor.setCaretColor((Color)style.get(context, prefix + ".caretForeground"));
- }
- Color fg = editor.getForeground();
- if (fg == null || fg instanceof UIResource) {
- editor.setSelectionColor(style.getColor(context, ColorType.TEXT_FOREGROUND));
- }
- context.setComponentState(SELECTED | FOCUSED);
- Color s = editor.getSelectionColor();
- if (s == null || s instanceof UIResource) {
- editor.setSelectionColor(style.getColor(context, ColorType.TEXT_BACKGROUND));
- }
- Color sfg = editor.getSelectedTextColor();
- if (sfg == null || sfg instanceof UIResource) {
- editor.setSelectedTextColor(style.getColor(context, ColorType.TEXT_FOREGROUND));
- }
- context.setComponentState(DISABLED);
- Color dfg = editor.getDisabledTextColor();
- if (dfg == null || dfg instanceof UIResource) {
- editor.setDisabledTextColor((Color)style.get(context, ColorType.FOREGROUND));
- }
- Insets margin = editor.getMargin();
- if (margin == null || margin instanceof UIResource) {
- margin = (Insets)style.get(context, prefix + ".margin");
- if (margin == null) {
- // Some places assume margins are non-null.
- margin = SynthLookAndFeel.EMPTY_UIRESOURCE_INSETS;
- }
- editor.setMargin(margin);
- }
- Caret caret = editor.getCaret();
- if (caret instanceof UIResource) {
- Object o = style.get(context, prefix + ".caretBlinkRate");
- if (o != null && o instanceof Integer) {
- Integer rate = (Integer)o;
- caret.setBlinkRate(rate.intValue());
- }
- }
- }
- context.dispose();
- }
- public SynthContext getContext(JComponent c) {
- return getContext(c, getComponentState(c));
- }
- private SynthContext getContext(JComponent c, int state) {
- return SynthContext.getContext(SynthContext.class, c,
- SynthLookAndFeel.getRegion(c), style, state);
- }
- private Region getRegion(JComponent c) {
- return SynthLookAndFeel.getRegion(c);
- }
- private int getComponentState(JComponent c) {
- return SynthLookAndFeel.getComponentState(c);
- }
- public void update(Graphics g, JComponent c) {
- SynthContext context = getContext(c);
- SynthLookAndFeel.update(context, g);
- paint(context, g);
- context.dispose();
- }
- public void paint(Graphics g, JComponent c) {
- SynthContext context = getContext(c);
- paint(context, g);
- context.dispose();
- }
- /**
- * Paints the interface. This is routed to the
- * paintSafely method under the guarantee that
- * the model won't change from the view of this thread
- * while it's rendering (if the associated model is
- * derived from AbstractDocument). This enables the
- * model to potentially be updated asynchronously.
- */
- protected void paint(SynthContext context, Graphics g) {
- if ((rootView.getViewCount() > 0) && (rootView.getView(0) != null)) {
- Document doc = editor.getDocument();
- if (doc instanceof AbstractDocument) {
- ((AbstractDocument)doc).readLock();
- }
- try {
- paintSafely(g);
- } finally {
- if (doc instanceof AbstractDocument) {
- ((AbstractDocument)doc).readUnlock();
- }
- }
- }
- }
- /**
- * Creates a new UI.
- */
- public SynthTextUI() {
- painted = false;
- }
- /**
- * Creates the object to use for a caret. By default an
- * instance of SynthCaret is created. This method
- * can be redefined to provide something else that implements
- * the InputPosition interface or a subclass of Caret.
- *
- * @return the caret object
- */
- protected Caret createCaret() {
- return new SynthCaret();
- }
- /**
- * Creates the object to use for adding highlights. By default
- * an instance of SynthHighlighter is created. This method
- * can be redefined to provide something else that implements
- * the Highlighter interface or a subclass of DefaultHighlighter.
- *
- * @return the highlighter
- */
- protected Highlighter createHighlighter() {
- return new SynthHighlighter();
- }
- /**
- * This method gets called when a bound property is changed
- * on the associated JTextComponent. This is a hook
- * which UI implementations may change to reflect how the
- * UI displays bound properties of JTextComponent subclasses.
- * This is implemented to do nothing (i.e. the response to
- * properties in JTextComponent itself are handled prior
- * to calling this method).
- *
- * @param evt the property change event
- */
- protected void propertyChange(PropertyChangeEvent evt) {
- }
- /**
- * Gets the name used as a key to look up properties through the
- * UIManager. This is used as a prefix to all the standard
- * text properties.
- *
- * @return the name
- */
- protected abstract String getPropertyPrefix();
- protected void installDefaults() {
- editor.addMouseListener(defaultDragRecognizer);
- editor.addMouseMotionListener(defaultDragRecognizer);
- String prefix = getPropertyPrefix();
- Caret caret = editor.getCaret();
- if (caret == null || caret instanceof UIResource) {
- editor.setCaret(createCaret());
- }
- Highlighter highlighter = editor.getHighlighter();
- if (highlighter == null || highlighter instanceof UIResource) {
- editor.setHighlighter(createHighlighter());
- }
- // this must come after creation of the caret since
- // fetchStyle might have to set the caret's blink rate
- fetchStyle(editor);
- TransferHandler th = editor.getTransferHandler();
- if (th == null || th instanceof UIResource) {
- editor.setTransferHandler(getTransferHandler());
- }
- DropTarget dropTarget = editor.getDropTarget();
- if (dropTarget instanceof UIResource) {
- if (defaultDropTargetListener == null) {
- defaultDropTargetListener = new TextDropTargetListener();
- }
- try {
- dropTarget.addDropTargetListener(defaultDropTargetListener);
- } catch (TooManyListenersException tmle) {
- // should not happen... swing drop target is multicast
- }
- }
- }
- protected void uninstallDefaults() {
- SynthContext context = getContext(editor, ENABLED);
- style.uninstallDefaults(context);
- context.dispose();
- style = null;
- editor.removeMouseListener(defaultDragRecognizer);
- editor.removeMouseMotionListener(defaultDragRecognizer);
- if (editor.getCaretColor() instanceof UIResource) {
- editor.setCaretColor(null);
- }
- if (editor.getSelectionColor() instanceof UIResource) {
- editor.setSelectionColor(null);
- }
- if (editor.getDisabledTextColor() instanceof UIResource) {
- editor.setDisabledTextColor(null);
- }
- if (editor.getSelectedTextColor() instanceof UIResource) {
- editor.setSelectedTextColor(null);
- }
- if (editor.getMargin() instanceof UIResource) {
- editor.setMargin(null);
- }
- if (editor.getCaret() instanceof UIResource) {
- editor.setCaret(null);
- }
- if (editor.getHighlighter() instanceof UIResource) {
- editor.setHighlighter(null);
- }
- if (editor.getTransferHandler() instanceof UIResource) {
- editor.setTransferHandler(null);
- }
- }
- /**
- * Installs listeners for the UI.
- */
- protected void installListeners() {
- }
- /**
- * Uninstalls listeners for the UI.
- */
- protected void uninstallListeners() {
- }
- protected void installKeyboardActions() {
- editor.setKeymap(JTextComponent.getKeymap(JTextComponent.DEFAULT_KEYMAP));
- InputMap km = getInputMap();
- if (km != null) {
- SwingUtilities.replaceUIInputMap(editor, JComponent.WHEN_FOCUSED,
- km);
- }
- ActionMap map = getActionMap();
- if (map != null) {
- SwingUtilities.replaceUIActionMap(editor, map);
- }
- updateFocusAcceleratorBinding(false);
- }
- /**
- * Get the InputMap to use for the UI.
- */
- InputMap getInputMap() {
- SynthContext context = getContext(editor, ENABLED);
- SynthStyle style = context.getStyle();
- InputMap map = new InputMapUIResource();
- InputMap shared =
- (InputMap)style.get(context, getPropertyPrefix() + ".focusInputMap");
- if (shared != null) {
- map.setParent(shared);
- }
- context.dispose();
- return map;
- }
- /**
- * Invoked when the focus accelerator changes, this will update the
- * key bindings as necessary.
- */
- void updateFocusAcceleratorBinding(boolean changed) {
- char accelerator = editor.getFocusAccelerator();
- if (changed || accelerator != '\0') {
- InputMap km = SwingUtilities.getUIInputMap
- (editor, JComponent.WHEN_IN_FOCUSED_WINDOW);
- if (km == null && accelerator != '\0') {
- km = new ComponentInputMapUIResource(editor);
- SwingUtilities.replaceUIInputMap(editor, JComponent.
- WHEN_IN_FOCUSED_WINDOW, km);
- ActionMap am = getActionMap();
- SwingUtilities.replaceUIActionMap(editor, am);
- }
- if (km != null) {
- km.clear();
- if (accelerator != '\0') {
- km.put(KeyStroke.getKeyStroke(accelerator,
- ActionEvent.ALT_MASK),
- "requestFocus");
- }
- }
- }
- }
- /**
- * Invoked when editable property is changed.
- *
- * removing 'TAB' and 'SHIFT-TAB' from traversalKeysSet in case
- * editor is editable
- * adding 'TAB' and 'SHIFT-TAB' to traversalKeysSet in case
- * editor is non editable
- */
- void updateFocusTraversalKeys() {
- /*
- * Fix for 4514331 Non-editable JTextArea and similar
- * should allow Tab to keyboard - accessibility
- */
- EditorKit editorKit = getEditorKit(editor);
- if ( editorKit != null
- && editorKit instanceof DefaultEditorKit) {
- Set storedForwardTraversalKeys = editor.
- getFocusTraversalKeys(KeyboardFocusManager.
- FORWARD_TRAVERSAL_KEYS);
- Set storedBackwardTraversalKeys = editor.
- getFocusTraversalKeys(KeyboardFocusManager.
- BACKWARD_TRAVERSAL_KEYS);
- Set forwardTraversalKeys =
- new HashSet(storedForwardTraversalKeys);
- Set backwardTraversalKeys =
- new HashSet(storedBackwardTraversalKeys);
- if (editor.isEditable()) {
- forwardTraversalKeys.
- remove(KeyStroke.getKeyStroke(KeyEvent.VK_TAB, 0));
- backwardTraversalKeys.
- remove(KeyStroke.getKeyStroke(KeyEvent.VK_TAB,
- InputEvent.SHIFT_MASK));
- } else {
- forwardTraversalKeys.add(KeyStroke.
- getKeyStroke(KeyEvent.VK_TAB, 0));
- backwardTraversalKeys.
- add(KeyStroke.
- getKeyStroke(KeyEvent.VK_TAB, InputEvent.SHIFT_MASK));
- }
- editor.setFocusTraversalKeys(KeyboardFocusManager.
- FORWARD_TRAVERSAL_KEYS,
- forwardTraversalKeys);
- editor.setFocusTraversalKeys(KeyboardFocusManager.
- BACKWARD_TRAVERSAL_KEYS,
- backwardTraversalKeys);
- }
- }
- /**
- * Returns the <code>TransferHandler</code> that will be installed if
- * their isn't one installed on the <code>JTextComponent</code>.
- */
- TransferHandler getTransferHandler() {
- return defaultTransferHandler;
- }
- /**
- * Fetch an action map to use.
- */
- ActionMap getActionMap() {
- String mapName = getPropertyPrefix() + ".actionMap";
- ActionMap map = (ActionMap)UIManager.get(mapName);
- if (map == null) {
- map = createActionMap();
- if (map != null) {
- UIManager.getLookAndFeelDefaults().put(mapName, map);
- }
- }
- ActionMap componentMap = new ActionMapUIResource();
- componentMap.put("requestFocus", new FocusAction());
- /*
- * fix for bug 4515750
- * JTextField & non-editable JTextArea bind return key - default btn not accessible
- *
- * Wrap the return action so that it is only enabled when the
- * component is editable. This allows the default button to be
- * processed when the text component has focus and isn't editable.
- *
- */
- if (getEditorKit(editor) instanceof DefaultEditorKit) {
- if (map != null) {
- Object obj = map.get(DefaultEditorKit.insertBreakAction);
- if (obj != null
- && obj instanceof DefaultEditorKit.InsertBreakAction) {
- Action action = new TextActionWrapper((TextAction)obj);
- componentMap.put(action.getValue(Action.NAME),action);
- }
- }
- }
- if (map != null) {
- componentMap.setParent(map);
- }
- return componentMap;
- }
- /**
- * Create a default action map. This is basically the
- * set of actions found exported by the component.
- */
- ActionMap createActionMap() {
- ActionMap map = new ActionMapUIResource();
- Action[] actions = editor.getActions();
- //System.out.println("building map for UI: " + getPropertyPrefix());
- int n = actions.length;
- for (int i = 0; i < n; i++) {
- Action a = actions[i];
- map.put(a.getValue(Action.NAME), a);
- //System.out.println(" " + a.getValue(Action.NAME));
- }
- map.put(TransferHandler.getCutAction().getValue(Action.NAME),
- TransferHandler.getCutAction());
- map.put(TransferHandler.getCopyAction().getValue(Action.NAME),
- TransferHandler.getCopyAction());
- map.put(TransferHandler.getPasteAction().getValue(Action.NAME),
- TransferHandler.getPasteAction());
- return map;
- }
- protected void uninstallKeyboardActions() {
- editor.setKeymap(null);
- SwingUtilities.replaceUIInputMap(editor, JComponent.
- WHEN_IN_FOCUSED_WINDOW, null);
- SwingUtilities.replaceUIActionMap(editor, null);
- }
- /**
- * Fetches the text component associated with this
- * UI implementation. This will be null until
- * the ui has been installed.
- *
- * @return the editor component
- */
- protected final JTextComponent getComponent() {
- return editor;
- }
- /**
- * Flags model changes.
- * This is called whenever the model has changed.
- * It is implemented to rebuild the view hierarchy
- * to represent the default root element of the
- * associated model.
- */
- protected void modelChanged() {
- // create a view hierarchy
- ViewFactory f = rootView.getViewFactory();
- Document doc = editor.getDocument();
- Element elem = doc.getDefaultRootElement();
- setView(f.create(elem));
- }
- /**
- * Sets the current root of the view hierarchy and calls invalidate().
- * If there were any child components, they will be removed (i.e.
- * there are assumed to have come from components embedded in views).
- *
- * @param v the root view
- */
- protected final void setView(View v) {
- editor.removeAll();
- rootView.setView(v);
- painted = false;
- editor.revalidate();
- editor.repaint();
- }
- /**
- * Paints the interface safely with a guarantee that
- * the model won't change from the view of this thread.
- * This does the following things, rendering from
- * back to front.
- * <ol>
- * <li>
- * The highlights (if any) are painted.
- * <li>
- * The view hierarchy is painted.
- * <li>
- * The caret is painted.
- * </ol>
- *
- * @param g the graphics context
- */
- protected void paintSafely(Graphics g) {
- painted = true;
- Highlighter highlighter = editor.getHighlighter();
- Caret caret = editor.getCaret();
- // paint the highlights
- if (highlighter != null) {
- highlighter.paint(g);
- }
- // paint the view hierarchy
- Rectangle alloc = getVisibleEditorRect();
- rootView.paint(g, alloc);
- // paint the caret
- if (caret != null) {
- caret.paint(g);
- }
- }
- /**
- * Installs the UI for a component. This does the following
- * things.
- * <ol>
- * <li>
- * Set the associated component to opaque (can be changed
- * easily by a subclass or on JTextComponent directly),
- * which is the most common case. This will cause the
- * component's background color to be painted.
- * <li>
- * Install the default caret and highlighter into the
- * associated component.
- * <li>
- * Attach to the editor and model. If there is no
- * model, a default one is created.
- * <li>
- * create the view factory and the view hierarchy used
- * to represent the model.
- * </ol>
- *
- * @param c the editor component
- * @see ComponentUI#installUI
- */
- public void installUI(JComponent c) {
- if (c instanceof JTextComponent) {
- editor = (JTextComponent) c;
- // install defaults
- installDefaults();
- editor.setAutoscrolls(true);
- // attach to the model and editor
- editor.addPropertyChangeListener(updateHandler);
- Document doc = editor.getDocument();
- if (doc == null) {
- // no model, create a default one. This will
- // fire a notification to the updateHandler
- // which takes care of the rest.
- editor.setDocument(getEditorKit(editor).createDefaultDocument());
- } else {
- doc.addDocumentListener(updateHandler);
- modelChanged();
- }
- // install keymap
- installListeners();
- installKeyboardActions();
- LayoutManager oldLayout = editor.getLayout();
- if ((oldLayout == null) || (oldLayout instanceof UIResource)) {
- // by default, use default LayoutManger implementation that
- // will position the components associated with a View object.
- editor.setLayout(updateHandler);
- }
- } else {
- throw new Error("TextUI needs JTextComponent");
- }
- }
- /**
- * Deinstalls the UI for a component. This removes the listeners,
- * uninstalls the highlighter, removes views, and nulls out the keymap.
- *
- * @param c the editor component
- * @see ComponentUI#uninstallUI
- */
- public void uninstallUI(JComponent c) {
- // detach from the model
- editor.removePropertyChangeListener(updateHandler);
- editor.getDocument().removeDocumentListener(updateHandler);
- // view part
- painted = false;
- uninstallDefaults();
- rootView.setView(null);
- c.removeAll();
- LayoutManager lm = c.getLayout();
- if (lm instanceof UIResource) {
- c.setLayout(null);
- }
- // controller part
- uninstallKeyboardActions();
- uninstallListeners();
- editor = null;
- }
- /**
- * Gets the preferred size for the editor component. If the component
- * has been given a size prior to receiving this request, it will
- * set the size of the view hierarchy to reflect the size of the component
- * before requesting the preferred size of the view hierarchy. This
- * allows formatted views to format to the current component size before
- * answering the request. Other views don't care about currently formatted
- * size and give the same answer either way.
- *
- * @param c the editor component
- * @return the size
- */
- public Dimension getPreferredSize(JComponent c) {
- Document doc = editor.getDocument();
- Insets i = c.getInsets();
- Dimension d = c.getSize();
- if (doc instanceof AbstractDocument) {
- ((AbstractDocument)doc).readLock();
- }
- try {
- if ((d.width > (i.left + i.right)) && (d.height > (i.top + i.bottom))) {
- rootView.setSize(d.width - i.left - i.right, d.height - i.top - i.bottom);
- }
- else if (d.width == 0 && d.height == 0) {
- // Probably haven't been layed out yet, force some sort of
- // initial sizing.
- rootView.setSize(Integer.MAX_VALUE, Integer.MAX_VALUE);
- }
- d.width = (int) Math.min((long) rootView.getPreferredSpan(View.X_AXIS) +
- (long) i.left + (long) i.right, Integer.MAX_VALUE);
- d.height = (int) Math.min((long) rootView.getPreferredSpan(View.Y_AXIS) +
- (long) i.top + (long) i.bottom, Integer.MAX_VALUE);
- } finally {
- if (doc instanceof AbstractDocument) {
- ((AbstractDocument)doc).readUnlock();
- }
- }
- return d;
- }
- /**
- * Gets the minimum size for the editor component.
- *
- * @param c the editor component
- * @return the size
- */
- public Dimension getMinimumSize(JComponent c) {
- Document doc = editor.getDocument();
- Insets i = c.getInsets();
- Dimension d = new Dimension();
- if (doc instanceof AbstractDocument) {
- ((AbstractDocument)doc).readLock();
- }
- try {
- d.width = (int) rootView.getMinimumSpan(View.X_AXIS) + i.left + i.right;
- d.height = (int) rootView.getMinimumSpan(View.Y_AXIS) + i.top + i.bottom;
- } finally {
- if (doc instanceof AbstractDocument) {
- ((AbstractDocument)doc).readUnlock();
- }
- }
- return d;
- }
- /**
- * Gets the maximum size for the editor component.
- *
- * @param c the editor component
- * @return the size
- */
- public Dimension getMaximumSize(JComponent c) {
- Document doc = editor.getDocument();
- Insets i = c.getInsets();
- Dimension d = new Dimension();
- if (doc instanceof AbstractDocument) {
- ((AbstractDocument)doc).readLock();
- }
- try {
- d.width = (int) Math.min((long) rootView.getMaximumSpan(View.X_AXIS) +
- (long) i.left + (long) i.right, Integer.MAX_VALUE);
- d.height = (int) Math.min((long) rootView.getMaximumSpan(View.Y_AXIS) +
- (long) i.top + (long) i.bottom, Integer.MAX_VALUE);
- } finally {
- if (doc instanceof AbstractDocument) {
- ((AbstractDocument)doc).readUnlock();
- }
- }
- return d;
- }
- // ---- TextUI methods -------------------------------------------
- /**
- * Gets the allocation to give the root View. Due
- * to an unfortunate set of historical events this
- * method is inappropriately named. The Rectangle
- * returned has nothing to do with visibility.
- * The component must have a non-zero positive size for
- * this translation to be computed.
- *
- * @return the bounding box for the root view
- */
- protected Rectangle getVisibleEditorRect() {
- Rectangle alloc = editor.getBounds();
- if ((alloc.width > 0) && (alloc.height > 0)) {
- alloc.x = alloc.y = 0;
- Insets insets = editor.getInsets();
- alloc.x += insets.left;
- alloc.y += insets.top;
- alloc.width -= insets.left + insets.right;
- alloc.height -= insets.top + insets.bottom;
- return alloc;
- }
- return null;
- }
- /**
- * Converts the given location in the model to a place in
- * the view coordinate system.
- * The component must have a non-zero positive size for
- * this translation to be computed.
- *
- * @param tc the text component for which this UI is installed
- * @param pos the local location in the model to translate >= 0
- * @return the coordinates as a rectangle, null if the model is not painted
- * @exception BadLocationException if the given position does not
- * represent a valid location in the associated document
- * @see TextUI#modelToView
- */
- public Rectangle modelToView(JTextComponent tc, int pos) throws BadLocationException {
- return modelToView(tc, pos, Position.Bias.Forward);
- }
- /**
- * Converts the given location in the model to a place in
- * the view coordinate system.
- * The component must have a non-zero positive size for
- * this translation to be computed.
- *
- * @param tc the text component for which this UI is installed
- * @param pos the local location in the model to translate >= 0
- * @return the coordinates as a rectangle, null if the model is not painted
- * @exception BadLocationException if the given position does not
- * represent a valid location in the associated document
- * @see TextUI#modelToView
- */
- public Rectangle modelToView(JTextComponent tc, int pos, Position.Bias bias) throws BadLocationException {
- Document doc = editor.getDocument();
- if (doc instanceof AbstractDocument) {
- ((AbstractDocument)doc).readLock();
- }
- try {
- Rectangle alloc = getVisibleEditorRect();
- if (alloc != null) {
- rootView.setSize(alloc.width, alloc.height);
- Shape s = rootView.modelToView(pos, alloc, bias);
- if (s != null) {
- return s.getBounds();
- }
- }
- } finally {
- if (doc instanceof AbstractDocument) {
- ((AbstractDocument)doc).readUnlock();
- }
- }
- return null;
- }
- /**
- * Converts the given place in the view coordinate system
- * to the nearest representative location in the model.
- * The component must have a non-zero positive size for
- * this translation to be computed.
- *
- * @param tc the text component for which this UI is installed
- * @param pt the location in the view to translate. This
- * should be in the same coordinate system as the mouse events.
- * @return the offset from the start of the document >= 0,
- * -1 if not painted
- * @see TextUI#viewToModel
- */
- public int viewToModel(JTextComponent tc, Point pt) {
- return viewToModel(tc, pt, discardBias);
- }
- /**
- * Converts the given place in the view coordinate system
- * to the nearest representative location in the model.
- * The component must have a non-zero positive size for
- * this translation to be computed.
- *
- * @param tc the text component for which this UI is installed
- * @param pt the location in the view to translate. This
- * should be in the same coordinate system as the mouse events.
- * @return the offset from the start of the document >= 0,
- * -1 if the component doesn't yet have a positive size.
- * @see TextUI#viewToModel
- */
- public int viewToModel(JTextComponent tc, Point pt,
- Position.Bias[] biasReturn) {
- int offs = -1;
- Document doc = editor.getDocument();
- if (doc instanceof AbstractDocument) {
- ((AbstractDocument)doc).readLock();
- }
- try {
- Rectangle alloc = getVisibleEditorRect();
- if (alloc != null) {
- rootView.setSize(alloc.width, alloc.height);
- offs = rootView.viewToModel(pt.x, pt.y, alloc, biasReturn);
- }
- } finally {
- if (doc instanceof AbstractDocument) {
- ((AbstractDocument)doc).readUnlock();
- }
- }
- return offs;
- }
- /**
- * Provides a way to determine the next visually represented model
- * location that one might place a caret. Some views may not be visible,
- * they might not be in the same order found in the model, or they just
- * might not allow access to some of the locations in the model.
- *
- * @param pos the position to convert >= 0
- * @param a the allocated region to render into
- * @param direction the direction from the current position that can
- * be thought of as the arrow keys typically found on a keyboard.
- * This may be SwingConstants.WEST, SwingConstants.EAST,
- * SwingConstants.NORTH, or SwingConstants.SOUTH.
- * @return the location within the model that best represents the next
- * location visual position.
- * @exception BadLocationException
- * @exception IllegalArgumentException for an invalid direction
- */
- public int getNextVisualPositionFrom(JTextComponent t, int pos,
- Position.Bias b, int direction, Position.Bias[] biasRet)
- throws BadLocationException{
- Document doc = editor.getDocument();
- if (doc instanceof AbstractDocument) {
- ((AbstractDocument)doc).readLock();
- }
- try {
- if (painted) {
- Rectangle alloc = getVisibleEditorRect();
- rootView.setSize(alloc.width, alloc.height);
- return rootView.getNextVisualPositionFrom(pos, b, alloc, direction,
- biasRet);
- }
- } finally {
- if (doc instanceof AbstractDocument) {
- ((AbstractDocument)doc).readUnlock();
- }
- }
- return -1;
- }
- /**
- * Causes the portion of the view responsible for the
- * given part of the model to be repainted. Does nothing if
- * the view is not currently painted.
- *
- * @param tc the text component for which this UI is installed
- * @param p0 the beginning of the range >= 0
- * @param p1 the end of the range >= p0
- * @see TextUI#damageRange
- */
- public void damageRange(JTextComponent tc, int p0, int p1) {
- damageRange(tc, p0, p1, Position.Bias.Forward, Position.Bias.Backward);
- }
- /**
- * Causes the portion of the view responsible for the
- * given part of the model to be repainted.
- *
- * @param p0 the beginning of the range >= 0
- * @param p1 the end of the range >= p0
- */
- public void damageRange(JTextComponent t, int p0, int p1,
- Position.Bias p0Bias, Position.Bias p1Bias) {
- if (painted) {
- Rectangle alloc = getVisibleEditorRect();
- Document doc = t.getDocument();
- if (doc instanceof AbstractDocument) {
- ((AbstractDocument)doc).readLock();
- }
- try {
- rootView.setSize(alloc.width, alloc.height);
- Shape toDamage = rootView.modelToView(p0, p0Bias,
- p1, p1Bias, alloc);
- Rectangle rect = (toDamage instanceof Rectangle) ?
- (Rectangle)toDamage : toDamage.getBounds();
- editor.repaint(rect.x, rect.y, rect.width, rect.height);
- } catch (BadLocationException e) {
- } finally {
- if (doc instanceof AbstractDocument) {
- ((AbstractDocument)doc).readUnlock();
- }
- }
- }
- }
- /**
- * Fetches the EditorKit for the UI.
- *
- * @param tc the text component for which this UI is installed
- * @return the editor capabilities
- * @see TextUI#getEditorKit
- */
- public EditorKit getEditorKit(JTextComponent tc) {
- return defaultKit;
- }
- /**
- * Fetches a View with the allocation of the associated
- * text component (i.e. the root of the hierarchy) that
- * can be traversed to determine how the model is being
- * represented spatially.
- * <p>
- * <font color=red><b>NOTE:</b>The View hierarchy can
- * be traversed from the root view, and other things
- * can be done as well. Things done in this way cannot
- * be protected like simple method calls through the TextUI.
- * Therefore, proper operation in the presence of concurrency
- * must be arranged by any logic that calls this method!
- * </font>
- *
- * @param tc the text component for which this UI is installed
- * @return the view
- * @see TextUI#getRootView
- */
- public View getRootView(JTextComponent tc) {
- return rootView;
- }
- /**
- * Returns the string to be used as the tooltip at the passed in location.
- * This forwards the method onto the root View.
- *
- * @see javax.swing.text.JTextComponent#getToolTipText
- * @see javax.swing.text.View#getToolTipText
- * @since 1.4
- */
- public String getToolTipText(JTextComponent t, Point pt) {
- if (!painted) {
- return null;
- }
- Document doc = editor.getDocument();
- String tt = null;
- Rectangle alloc = getVisibleEditorRect();
- if (doc instanceof AbstractDocument) {
- ((AbstractDocument)doc).readLock();
- }
- try {
- tt = rootView.getToolTipText(pt.x, pt.y, alloc);
- } finally {
- if (doc instanceof AbstractDocument) {
- ((AbstractDocument)doc).readUnlock();
- }
- }
- return tt;
- }
- // --- ViewFactory methods ------------------------------
- /**
- * Creates a view for an element.
- * If a subclass wishes to directly implement the factory
- * producing the view(s), it should reimplement this
- * method. By default it simply returns null indicating
- * it is unable to represent the element.
- *
- * @param elem the element
- * @return the view
- */
- public View create(Element elem) {
- return null;
- }
- /**
- * Creates a view for an element.
- * If a subclass wishes to directly implement the factory
- * producing the view(s), it should reimplement this
- * method. By default it simply returns null indicating
- * it is unable to represent the part of the element.
- *
- * @param elem the element
- * @param p0 the starting offset >= 0
- * @param p1 the ending offset >= p0
- * @return the view
- */
- public View create(Element elem, int p0, int p1) {
- return null;
- }
- /**
- * Root view that acts as a gateway between the component
- * and the View hierarchy.
- */
- class RootView extends View {
- RootView() {
- super(null);
- }
- void setView(View v) {
- if (view != null) {
- // get rid of back reference so that the old
- // hierarchy can be garbage collected.
- view.setParent(null);
- }
- view = v;
- if (view != null) {
- view.setParent(this);
- }
- }
- /**
- * Fetches the attributes to use when rendering. At the root
- * level there are no attributes. If an attribute is resolved
- * up the view hierarchy this is the end of the line.
- */
- public AttributeSet getAttributes() {
- return null;
- }
- /**
- * Determines the preferred span for this view along an axis.
- *
- * @param axis may be either X_AXIS or Y_AXIS
- * @return the span the view would like to be rendered into.
- * Typically the view is told to render into the span
- * that is returned, although there is no guarantee.
- * The parent may choose to resize or break the view.
- */
- public float getPreferredSpan(int axis) {
- if (view != null) {
- return view.getPreferredSpan(axis);
- }
- return 10;
- }
- /**
- * Determines the minimum span for this view along an axis.
- *
- * @param axis may be either X_AXIS or Y_AXIS
- * @return the span the view would like to be rendered into.
- * Typically the view is told to render into the span
- * that is returned, although there is no guarantee.
- * The parent may choose to resize or break the view.
- */
- public float getMinimumSpan(int axis) {
- if (view != null) {
- return view.getMinimumSpan(axis);
- }
- return 10;
- }
- /**
- * Determines the maximum span for this view along an axis.
- *
- * @param axis may be either X_AXIS or Y_AXIS
- * @return the span the view would like to be rendered into.
- * Typically the view is told to render into the span
- * that is returned, although there is no guarantee.
- * The parent may choose to resize or break the view.
- */
- public float getMaximumSpan(int axis) {
- return Integer.MAX_VALUE;
- }
- /**
- * Specifies that a preference has changed.
- * Child views can call this on the parent to indicate that
- * the preference has changed. The root view routes this to
- * invalidate on the hosting component.
- * <p>
- * This can be called on a different thread from the
- * event dispatching thread and is basically unsafe to
- * propagate into the component. To make this safe,
- * the operation is transferred over to the event dispatching
- * thread for completion. It is a design goal that all view
- * methods be safe to call without concern for concurrency,
- * and this behavior helps make that true.
- *
- * @param child the child view
- * @param width true if the width preference has changed
- * @param height true if the height preference has changed
- */
- public void preferenceChanged(View child, boolean width, boolean height) {
- editor.revalidate();
- }
- /**
- * Determines the desired alignment for this view along an axis.
- *
- * @param axis may be either X_AXIS or Y_AXIS
- * @return the desired alignment, where 0.0 indicates the origin
- * and 1.0 the full span away from the origin
- */
- public float getAlignment(int axis) {
- if (view != null) {
- return view.getAlignment(axis);
- }
- return 0;
- }
- /**
- * Renders the view.
- *
- * @param g the graphics context
- * @param allocation the region to render into
- */
- public void paint(Graphics g, Shape allocation) {
- if (view != null) {
- Rectangle alloc = (allocation instanceof Rectangle) ?
- (Rectangle)allocation : allocation.getBounds();
- setSize(alloc.width, alloc.height);
- view.paint(g, allocation);
- }
- }
- /**
- * Sets the view parent.
- *
- * @param parent the parent view
- */
- public void setParent(View parent) {
- throw new Error("Can't set parent on root view");
- }
- /**
- * Returns the number of views in this view. Since
- * this view simply wraps the root of the view hierarchy
- * it has exactly one child.
- *
- * @return the number of views
- * @see #getView
- */
- public int getViewCount() {
- return 1;
- }
- /**
- * Gets the n-th view in this container.
- *
- * @param n the number of the view to get
- * @return the view
- */
- public View getView(int n) {
- return view;
- }
- /**
- * Returns the child view index representing the given position in
- * the model. This is implemented to return the index of the only
- * child.
- *
- * @param pos the position >= 0
- * @return index of the view representing the given position, or
- * -1 if no view represents that position
- * @since 1.3
- */
- public int getViewIndex(int pos, Position.Bias b) {
- return 0;
- }
- /**
- * Fetches the allocation for the given child view.
- * This enables finding out where various views
- * are located, without assuming the views store
- * their location. This returns the given allocation
- * since this view simply acts as a gateway between
- * the view hierarchy and the associated component.
- *
- * @param index the index of the child
- * @param a the allocation to this view.
- * @return the allocation to the child
- */
- public Shape getChildAllocation(int index, Shape a) {
- return a;
- }
- /**
- * Provides a mapping from the document model coordinate space
- * to the coordinate space of the view mapped to it.
- *
- * @param pos the position to convert
- * @param a the allocated region to render into
- * @return the bounding box of the given position
- */
- public Shape modelToView(int pos, Shape a, Position.Bias b) throws BadLocationException {
- if (view != null) {
- return view.modelToView(pos, a, b);
- }
- return null;
- }
- /**
- * Provides a mapping from the document model coordinate space
- * to the coordinate space of the view mapped to it.
- *
- * @param p0 the position to convert >= 0
- * @param b0 the bias toward the previous character or the
- * next character represented by p0, in case the
- * position is a boundary of two views.
- * @param p1 the position to convert >= 0
- * @param b1 the bias toward the previous character or the
- * next character represented by p1, in case the
- * position is a boundary