jdk/src/share/classes/javax/swing/plaf/basic/BasicTextUI.java
author duke
Sat, 01 Dec 2007 00:00:00 +0000
changeset 2 90ce3da70b43
child 1290 da8902cd496c
permissions -rw-r--r--
Initial load
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * Copyright 1997-2006 Sun Microsystems, Inc.  All Rights Reserved.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * have any questions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
package javax.swing.plaf.basic;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.awt.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.awt.event.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.awt.font.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.awt.datatransfer.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.awt.dnd.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.awt.im.InputContext;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.beans.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.net.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import javax.swing.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import javax.swing.plaf.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import javax.swing.text.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import javax.swing.event.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import javax.swing.border.Border;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
import javax.swing.plaf.UIResource;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
import sun.swing.DefaultLookup;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
import sun.awt.AppContext;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
import javax.swing.plaf.basic.DragRecognitionSupport.BeforeDrag;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * Basis of a text components look-and-feel.  This provides the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * basic editor view and controller services that may be useful
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * when creating a look-and-feel for an extension of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * <code>JTextComponent</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * Most state is held in the associated <code>JTextComponent</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * as bound properties, and the UI installs default values for the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * various properties.  This default will install something for
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * all of the properties.  Typically, a LAF implementation will
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * do more however.  At a minimum, a LAF would generally install
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * key bindings.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 * This class also provides some concurrency support if the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 * <code>Document</code> associated with the JTextComponent is a subclass of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 * <code>AbstractDocument</code>.  Access to the View (or View hierarchy) is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 * serialized between any thread mutating the model and the Swing
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 * event thread (which is expected to render, do model/view coordinate
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 * translation, etc).  <em>Any access to the root view should first
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 * acquire a read-lock on the AbstractDocument and release that lock
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 * in a finally block.</em>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 * An important method to define is the {@link #getPropertyPrefix} method
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
 * which is used as the basis of the keys used to fetch defaults
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
 * from the UIManager.  The string should reflect the type of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
 * TextUI (eg. TextField, TextArea, etc) without the particular
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
 * LAF part of the name (eg Metal, Motif, etc).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
 * To build a view of the model, one of the following strategies
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
 * can be employed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
 * <ol>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
 * <li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
 * One strategy is to simply redefine the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
 * ViewFactory interface in the UI.  By default, this UI itself acts
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
 * as the factory for View implementations.  This is useful
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
 * for simple factories.  To do this reimplement the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
 * {@link #create} method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
 * <li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
 * A common strategy for creating more complex types of documents
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
 * is to have the EditorKit implementation return a factory.  Since
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
 * the EditorKit ties all of the pieces necessary to maintain a type
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
 * of document, the factory is typically an important part of that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
 * and should be produced by the EditorKit implementation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
 * </ol>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
 * <strong>Warning:</strong>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
 * Serialized objects of this class will not be compatible with
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
 * future Swing releases. The current serialization support is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
 * appropriate for short term storage or RMI between applications running
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
 * the same version of Swing.  As of 1.4, support for long term storage
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
 * of all JavaBeans<sup><font size="-2">TM</font></sup>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
 * has been added to the <code>java.beans</code> package.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
 * Please see {@link java.beans.XMLEncoder}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
 * @author Timothy Prinzing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
 * @author Shannon Hickey (drag and drop)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
public abstract class BasicTextUI extends TextUI implements ViewFactory {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     * Creates a new UI.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    public BasicTextUI() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        painted = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     * Creates the object to use for a caret.  By default an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     * instance of BasicCaret is created.  This method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     * can be redefined to provide something else that implements
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     * the InputPosition interface or a subclass of JCaret.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     * @return the caret object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    protected Caret createCaret() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        return new BasicCaret();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     * Creates the object to use for adding highlights.  By default
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     * an instance of BasicHighlighter is created.  This method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     * can be redefined to provide something else that implements
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     * the Highlighter interface or a subclass of DefaultHighlighter.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     * @return the highlighter
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
    protected Highlighter createHighlighter() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        return new BasicHighlighter();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     * Fetches the name of the keymap that will be installed/used
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     * by default for this UI. This is implemented to create a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     * name based upon the classname.  The name is the the name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     * of the class with the package prefix removed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     * @return the name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
    protected String getKeymapName() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        String nm = getClass().getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        int index = nm.lastIndexOf('.');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        if (index >= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
            nm = nm.substring(index+1, nm.length());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        return nm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     * Creates the keymap to use for the text component, and installs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     * any necessary bindings into it.  By default, the keymap is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     * shared between all instances of this type of TextUI. The
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     * keymap has the name defined by the getKeymapName method.  If the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     * keymap is not found, then DEFAULT_KEYMAP from JTextComponent is used.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
     * The set of bindings used to create the keymap is fetched
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
     * from the UIManager using a key formed by combining the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
     * {@link #getPropertyPrefix} method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     * and the string <code>.keyBindings</code>.  The type is expected
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
     * to be <code>JTextComponent.KeyBinding[]</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     * @return the keymap
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     * @see #getKeymapName
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     * @see javax.swing.text.JTextComponent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
    protected Keymap createKeymap() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        String nm = getKeymapName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        Keymap map = JTextComponent.getKeymap(nm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        if (map == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
            Keymap parent = JTextComponent.getKeymap(JTextComponent.DEFAULT_KEYMAP);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
            map = JTextComponent.addKeymap(nm, parent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
            String prefix = getPropertyPrefix();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
            Object o = DefaultLookup.get(editor, this,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
                prefix + ".keyBindings");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
            if ((o != null) && (o instanceof JTextComponent.KeyBinding[])) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
                JTextComponent.KeyBinding[] bindings = (JTextComponent.KeyBinding[]) o;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
                JTextComponent.loadKeymap(map, bindings, getComponent().getActions());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
        return map;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
     * This method gets called when a bound property is changed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
     * on the associated JTextComponent.  This is a hook
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
     * which UI implementations may change to reflect how the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
     * UI displays bound properties of JTextComponent subclasses.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
     * This is implemented to do nothing (i.e. the response to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
     * properties in JTextComponent itself are handled prior
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
     * to calling this method).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
     * This implementation updates the background of the text
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
     * component if the editable and/or enabled state changes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
     * @param evt the property change event
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
    protected void propertyChange(PropertyChangeEvent evt) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
        if (evt.getPropertyName().equals("editable") ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
                evt.getPropertyName().equals("enabled")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
            updateBackground((JTextComponent)evt.getSource());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
     * Updates the background of the text component based on whether the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
     * text component is editable and/or enabled.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
     * @param c the JTextComponent that needs its background color updated
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
    private void updateBackground(JTextComponent c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
        // This is a temporary workaround.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
        // This code does not correctly deal with Synth (Synth doesn't use
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
        // properties like this), nor does it deal with the situation where
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
        // the developer grabs the color from a JLabel and sets it as
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
        // the background for a JTextArea in all look and feels. The problem
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
        // scenario results if the Color obtained for the Label and TextArea
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
        // is ==, which is the case for the windows look and feel.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
        // Until an appropriate solution is found, the code is being
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
        // reverted to what it was before the original fix.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
        if (this instanceof sun.swing.plaf.synth.SynthUI ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
                (c instanceof JTextArea)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
        Color background = c.getBackground();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
        if (background instanceof UIResource) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
            String prefix = getPropertyPrefix();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
            Color disabledBG =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
                DefaultLookup.getColor(c, this, prefix + ".disabledBackground", null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
            Color inactiveBG =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
                DefaultLookup.getColor(c, this, prefix + ".inactiveBackground", null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
            Color bg =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
                DefaultLookup.getColor(c, this, prefix + ".background", null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
            /* In an ideal situation, the following check would not be necessary
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
             * and we would replace the color any time the previous color was a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
             * UIResouce. However, it turns out that there is existing code that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
             * uses the following inadvisable pattern to turn a text area into
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
             * what appears to be a multi-line label:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
             *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
             * JLabel label = new JLabel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
             * JTextArea area = new JTextArea();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
             * area.setBackground(label.getBackground());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
             * area.setEditable(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
             *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
             * JLabel's default background is a UIResource. As such, just
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
             * checking for UIResource would have us always changing the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
             * background away from what the developer wanted.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
             *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
             * Therefore, for JTextArea/JEditorPane, we'll additionally check
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
             * that the color we're about to replace matches one that was
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
             * installed by us from the UIDefaults.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
            if ((c instanceof JTextArea || c instanceof JEditorPane)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
                    && background != disabledBG
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
                    && background != inactiveBG
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
                    && background != bg) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
            Color newColor = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
            if (!c.isEnabled()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
                newColor = disabledBG;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
            if (newColor == null && !c.isEditable()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
                newColor = inactiveBG;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
            if (newColor == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
                newColor = bg;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
            if (newColor != null && newColor != background) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
                c.setBackground(newColor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
     * Gets the name used as a key to look up properties through the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
     * UIManager.  This is used as a prefix to all the standard
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
     * text properties.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
     * @return the name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
    protected abstract String getPropertyPrefix();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
     * Initializes component properties, e.g. font, foreground,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
     * background, caret color, selection color, selected text color,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
     * disabled text color, and border color.  The font, foreground, and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
     * background properties are only set if their current value is either null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
     * or a UIResource, other properties are set if the current
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
     * value is null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
     * @see #uninstallDefaults
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
     * @see #installUI
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
    protected void installDefaults()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
        String prefix = getPropertyPrefix();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
        Font f = editor.getFont();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
        if ((f == null) || (f instanceof UIResource)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
            editor.setFont(UIManager.getFont(prefix + ".font"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
        Color bg = editor.getBackground();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
        if ((bg == null) || (bg instanceof UIResource)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
            editor.setBackground(UIManager.getColor(prefix + ".background"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
        Color fg = editor.getForeground();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
        if ((fg == null) || (fg instanceof UIResource)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
            editor.setForeground(UIManager.getColor(prefix + ".foreground"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
        Color color = editor.getCaretColor();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
        if ((color == null) || (color instanceof UIResource)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
            editor.setCaretColor(UIManager.getColor(prefix + ".caretForeground"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
        Color s = editor.getSelectionColor();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
        if ((s == null) || (s instanceof UIResource)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
            editor.setSelectionColor(UIManager.getColor(prefix + ".selectionBackground"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
        Color sfg = editor.getSelectedTextColor();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
        if ((sfg == null) || (sfg instanceof UIResource)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
            editor.setSelectedTextColor(UIManager.getColor(prefix + ".selectionForeground"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
        Color dfg = editor.getDisabledTextColor();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
        if ((dfg == null) || (dfg instanceof UIResource)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
            editor.setDisabledTextColor(UIManager.getColor(prefix + ".inactiveForeground"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
        Border b = editor.getBorder();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
        if ((b == null) || (b instanceof UIResource)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
            editor.setBorder(UIManager.getBorder(prefix + ".border"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
        Insets margin = editor.getMargin();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
        if (margin == null || margin instanceof UIResource) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
            editor.setMargin(UIManager.getInsets(prefix + ".margin"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
        updateCursor();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
    private void installDefaults2() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
        editor.addMouseListener(dragListener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
        editor.addMouseMotionListener(dragListener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
        String prefix = getPropertyPrefix();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
        Caret caret = editor.getCaret();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
        if (caret == null || caret instanceof UIResource) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
            caret = createCaret();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
            editor.setCaret(caret);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
            int rate = DefaultLookup.getInt(getComponent(), this, prefix + ".caretBlinkRate", 500);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
            caret.setBlinkRate(rate);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
        Highlighter highlighter = editor.getHighlighter();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
        if (highlighter == null || highlighter instanceof UIResource) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
            editor.setHighlighter(createHighlighter());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
        TransferHandler th = editor.getTransferHandler();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
        if (th == null || th instanceof UIResource) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
            editor.setTransferHandler(getTransferHandler());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
     * Sets the component properties that haven't been explicitly overridden to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
     * null.  A property is considered overridden if its current value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
     * is not a UIResource.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
     * @see #installDefaults
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
     * @see #uninstallUI
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
    protected void uninstallDefaults()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
        editor.removeMouseListener(dragListener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
        editor.removeMouseMotionListener(dragListener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
        if (editor.getCaretColor() instanceof UIResource) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
            editor.setCaretColor(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
        if (editor.getSelectionColor() instanceof UIResource) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
            editor.setSelectionColor(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
        if (editor.getDisabledTextColor() instanceof UIResource) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
            editor.setDisabledTextColor(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
        if (editor.getSelectedTextColor() instanceof UIResource) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
            editor.setSelectedTextColor(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
        if (editor.getBorder() instanceof UIResource) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
            editor.setBorder(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
        if (editor.getMargin() instanceof UIResource) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
            editor.setMargin(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
        if (editor.getCaret() instanceof UIResource) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
            editor.setCaret(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
        if (editor.getHighlighter() instanceof UIResource) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
            editor.setHighlighter(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
        if (editor.getTransferHandler() instanceof UIResource) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
            editor.setTransferHandler(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
        if (editor.getCursor() instanceof UIResource) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
            editor.setCursor(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
     * Installs listeners for the UI.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
    protected void installListeners() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
     * Uninstalls listeners for the UI.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
    protected void uninstallListeners() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
    protected void installKeyboardActions() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
        // backward compatibility support... keymaps for the UI
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
        // are now installed in the more friendly input map.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
        editor.setKeymap(createKeymap());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
        InputMap km = getInputMap();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
        if (km != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
            SwingUtilities.replaceUIInputMap(editor, JComponent.WHEN_FOCUSED,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
                                             km);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
        ActionMap map = getActionMap();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
        if (map != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
            SwingUtilities.replaceUIActionMap(editor, map);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
        updateFocusAcceleratorBinding(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
     * Get the InputMap to use for the UI.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
    InputMap getInputMap() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
        InputMap map = new InputMapUIResource();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
        InputMap shared =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
            (InputMap)DefaultLookup.get(editor, this,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
            getPropertyPrefix() + ".focusInputMap");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
        if (shared != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
            map.setParent(shared);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
        return map;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
     * Invoked when the focus accelerator changes, this will update the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
     * key bindings as necessary.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
    void updateFocusAcceleratorBinding(boolean changed) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
        char accelerator = editor.getFocusAccelerator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
        if (changed || accelerator != '\0') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
            InputMap km = SwingUtilities.getUIInputMap
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
                        (editor, JComponent.WHEN_IN_FOCUSED_WINDOW);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
            if (km == null && accelerator != '\0') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
                km = new ComponentInputMapUIResource(editor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
                SwingUtilities.replaceUIInputMap(editor, JComponent.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
                                                 WHEN_IN_FOCUSED_WINDOW, km);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
                ActionMap am = getActionMap();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
                SwingUtilities.replaceUIActionMap(editor, am);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
            if (km != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
                km.clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
                if (accelerator != '\0') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
                    km.put(KeyStroke.getKeyStroke(accelerator,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
                                                  ActionEvent.ALT_MASK),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
                           "requestFocus");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
     * Invoked when editable property is changed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
     * removing 'TAB' and 'SHIFT-TAB' from traversalKeysSet in case
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
     * editor is editable
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
     * adding 'TAB' and 'SHIFT-TAB' to traversalKeysSet in case
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
     * editor is non editable
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
    void updateFocusTraversalKeys() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
         * Fix for 4514331 Non-editable JTextArea and similar
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
         * should allow Tab to keyboard - accessibility
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
        EditorKit editorKit = getEditorKit(editor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
        if ( editorKit != null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
             && editorKit instanceof DefaultEditorKit) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
            Set storedForwardTraversalKeys = editor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
                getFocusTraversalKeys(KeyboardFocusManager.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
                                      FORWARD_TRAVERSAL_KEYS);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
            Set storedBackwardTraversalKeys = editor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
                getFocusTraversalKeys(KeyboardFocusManager.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
                                      BACKWARD_TRAVERSAL_KEYS);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
            Set forwardTraversalKeys =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
                new HashSet(storedForwardTraversalKeys);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
            Set backwardTraversalKeys =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
                new HashSet(storedBackwardTraversalKeys);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
            if (editor.isEditable()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
                forwardTraversalKeys.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
                    remove(KeyStroke.getKeyStroke(KeyEvent.VK_TAB, 0));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
                backwardTraversalKeys.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
                    remove(KeyStroke.getKeyStroke(KeyEvent.VK_TAB,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
                                                  InputEvent.SHIFT_MASK));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
                forwardTraversalKeys.add(KeyStroke.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
                                         getKeyStroke(KeyEvent.VK_TAB, 0));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
                backwardTraversalKeys.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
                    add(KeyStroke.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
                        getKeyStroke(KeyEvent.VK_TAB, InputEvent.SHIFT_MASK));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
            LookAndFeel.installProperty(editor,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
                                        "focusTraversalKeysForward",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
                                         forwardTraversalKeys);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
            LookAndFeel.installProperty(editor,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
                                        "focusTraversalKeysBackward",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
                                         backwardTraversalKeys);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
     * As needed updates cursor for the target editor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
    private void updateCursor() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
        if ((! editor.isCursorSet())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
               || editor.getCursor() instanceof UIResource) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
            Cursor cursor = (editor.isEditable()) ? textCursor : null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
            editor.setCursor(cursor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
     * Returns the <code>TransferHandler</code> that will be installed if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
     * their isn't one installed on the <code>JTextComponent</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
    TransferHandler getTransferHandler() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
        return defaultTransferHandler;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
     * Fetch an action map to use.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
    ActionMap getActionMap() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
        String mapName = getPropertyPrefix() + ".actionMap";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
        ActionMap map = (ActionMap)UIManager.get(mapName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
        if (map == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
            map = createActionMap();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
            if (map != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
                UIManager.getLookAndFeelDefaults().put(mapName, map);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
        ActionMap componentMap = new ActionMapUIResource();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
        componentMap.put("requestFocus", new FocusAction());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
         * fix for bug 4515750
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
         * JTextField & non-editable JTextArea bind return key - default btn not accessible
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
         * Wrap the return action so that it is only enabled when the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
         * component is editable. This allows the default button to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
         * processed when the text component has focus and isn't editable.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
        if (getEditorKit(editor) instanceof DefaultEditorKit) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
            if (map != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
                Object obj = map.get(DefaultEditorKit.insertBreakAction);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
                if (obj != null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
                    && obj instanceof DefaultEditorKit.InsertBreakAction) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
                    Action action =  new TextActionWrapper((TextAction)obj);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
                    componentMap.put(action.getValue(Action.NAME),action);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
        if (map != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
            componentMap.setParent(map);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
        return componentMap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
     * Create a default action map.  This is basically the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
     * set of actions found exported by the component.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
    ActionMap createActionMap() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
        ActionMap map = new ActionMapUIResource();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
        Action[] actions = editor.getActions();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
        //System.out.println("building map for UI: " + getPropertyPrefix());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
        int n = actions.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
        for (int i = 0; i < n; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
            Action a = actions[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
            map.put(a.getValue(Action.NAME), a);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
            //System.out.println("  " + a.getValue(Action.NAME));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
        map.put(TransferHandler.getCutAction().getValue(Action.NAME),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
                TransferHandler.getCutAction());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
        map.put(TransferHandler.getCopyAction().getValue(Action.NAME),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
                TransferHandler.getCopyAction());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
        map.put(TransferHandler.getPasteAction().getValue(Action.NAME),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
                TransferHandler.getPasteAction());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
        return map;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
    protected void uninstallKeyboardActions() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
        editor.setKeymap(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
        SwingUtilities.replaceUIInputMap(editor, JComponent.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
                                         WHEN_IN_FOCUSED_WINDOW, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
        SwingUtilities.replaceUIActionMap(editor, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
     * Paints a background for the view.  This will only be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
     * called if isOpaque() on the associated component is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
     * true.  The default is to paint the background color
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
     * of the component.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
     * @param g the graphics context
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
    protected void paintBackground(Graphics g) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
        g.setColor(editor.getBackground());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
        g.fillRect(0, 0, editor.getWidth(), editor.getHeight());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
     * Fetches the text component associated with this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
     * UI implementation.  This will be null until
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
     * the ui has been installed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
     * @return the editor component
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
    protected final JTextComponent getComponent() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
        return editor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
     * Flags model changes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
     * This is called whenever the model has changed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
     * It is implemented to rebuild the view hierarchy
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
     * to represent the default root element of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
     * associated model.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
    protected void modelChanged() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
        // create a view hierarchy
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
        ViewFactory f = rootView.getViewFactory();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
        Document doc = editor.getDocument();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
        Element elem = doc.getDefaultRootElement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
        setView(f.create(elem));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
     * Sets the current root of the view hierarchy and calls invalidate().
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
     * If there were any child components, they will be removed (i.e.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
     * there are assumed to have come from components embedded in views).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
     * @param v the root view
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
    protected final void setView(View v) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
        rootView.setView(v);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
        painted = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
        editor.revalidate();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
        editor.repaint();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
     * Paints the interface safely with a guarantee that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
     * the model won't change from the view of this thread.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
     * This does the following things, rendering from
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
     * back to front.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
     * <ol>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
     * <li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
     * If the component is marked as opaque, the background
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
     * is painted in the current background color of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
     * component.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
     * <li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
     * The highlights (if any) are painted.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
     * <li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
     * The view hierarchy is painted.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
     * <li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
     * The caret is painted.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
     * </ol>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
     * @param g the graphics context
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
    protected void paintSafely(Graphics g) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
        painted = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
        Highlighter highlighter = editor.getHighlighter();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
        Caret caret = editor.getCaret();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
        // paint the background
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
        if (editor.isOpaque()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
            paintBackground(g);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
        // paint the highlights
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
        if (highlighter != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
            highlighter.paint(g);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
        // paint the view hierarchy
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
        Rectangle alloc = getVisibleEditorRect();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
        if (alloc != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
            rootView.paint(g, alloc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
        // paint the caret
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
        if (caret != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
            caret.paint(g);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
        if (dropCaret != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
            dropCaret.paint(g);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
    // --- ComponentUI methods --------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
     * Installs the UI for a component.  This does the following
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
     * things.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
     * <ol>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
     * <li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
     * Set the associated component to opaque (can be changed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
     * easily by a subclass or on JTextComponent directly),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
     * which is the most common case.  This will cause the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
     * component's background color to be painted.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
     * <li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
     * Install the default caret and highlighter into the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
     * associated component.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
     * <li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
     * Attach to the editor and model.  If there is no
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
     * model, a default one is created.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
     * <li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
     * create the view factory and the view hierarchy used
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
     * to represent the model.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
     * </ol>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
     * @param c the editor component
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
     * @see ComponentUI#installUI
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
    public void installUI(JComponent c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
        if (c instanceof JTextComponent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
            editor = (JTextComponent) c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
            // install defaults
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
            installDefaults();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
            installDefaults2();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
            // common case is background painted... this can
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
            // easily be changed by subclasses or from outside
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
            // of the component.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
            LookAndFeel.installProperty(editor, "opaque", Boolean.TRUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
            LookAndFeel.installProperty(editor, "autoscrolls", Boolean.TRUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
            // attach to the model and editor
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
            editor.addPropertyChangeListener(updateHandler);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
            Document doc = editor.getDocument();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
            if (doc == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
                // no model, create a default one.  This will
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
                // fire a notification to the updateHandler
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
                // which takes care of the rest.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
                editor.setDocument(getEditorKit(editor).createDefaultDocument());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
                doc.addDocumentListener(updateHandler);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
                modelChanged();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
            // install keymap
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
            installListeners();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
            installKeyboardActions();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
            LayoutManager oldLayout = editor.getLayout();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
            if ((oldLayout == null) || (oldLayout instanceof UIResource)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
                // by default, use default LayoutManger implementation that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
                // will position the components associated with a View object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
                editor.setLayout(updateHandler);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
            updateBackground(editor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
            throw new Error("TextUI needs JTextComponent");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
     * Deinstalls the UI for a component.  This removes the listeners,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
     * uninstalls the highlighter, removes views, and nulls out the keymap.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
     * @param c the editor component
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
     * @see ComponentUI#uninstallUI
90ce3da70b43 Initial load
duke
parents:
diff changeset
   830
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   831
    public void uninstallUI(JComponent c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
        // detach from the model
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
        editor.removePropertyChangeListener(updateHandler);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
        editor.getDocument().removeDocumentListener(updateHandler);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
        // view part
90ce3da70b43 Initial load
duke
parents:
diff changeset
   837
        painted = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
        uninstallDefaults();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
        rootView.setView(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
        c.removeAll();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
        LayoutManager lm = c.getLayout();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   842
        if (lm instanceof UIResource) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   843
            c.setLayout(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   844
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   845
90ce3da70b43 Initial load
duke
parents:
diff changeset
   846
        // controller part
90ce3da70b43 Initial load
duke
parents:
diff changeset
   847
        uninstallKeyboardActions();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   848
        uninstallListeners();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   849
90ce3da70b43 Initial load
duke
parents:
diff changeset
   850
        editor = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   851
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   852
90ce3da70b43 Initial load
duke
parents:
diff changeset
   853
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   854
     * Superclass paints background in an uncontrollable way
90ce3da70b43 Initial load
duke
parents:
diff changeset
   855
     * (i.e. one might want an image tiled into the background).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   856
     * To prevent this from happening twice, this method is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   857
     * reimplemented to simply paint.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   858
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   859
     * <em>NOTE:</em> Superclass is also not thread-safe in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   860
     * it's rendering of the background, although that's not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   861
     * an issue with the default rendering.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   862
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   863
    public void update(Graphics g, JComponent c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   864
        paint(g, c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   865
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   866
90ce3da70b43 Initial load
duke
parents:
diff changeset
   867
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   868
     * Paints the interface.  This is routed to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   869
     * paintSafely method under the guarantee that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   870
     * the model won't change from the view of this thread
90ce3da70b43 Initial load
duke
parents:
diff changeset
   871
     * while it's rendering (if the associated model is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   872
     * derived from AbstractDocument).  This enables the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   873
     * model to potentially be updated asynchronously.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   874
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   875
     * @param g the graphics context
90ce3da70b43 Initial load
duke
parents:
diff changeset
   876
     * @param c the editor component
90ce3da70b43 Initial load
duke
parents:
diff changeset
   877
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   878
    public final void paint(Graphics g, JComponent c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   879
        if ((rootView.getViewCount() > 0) && (rootView.getView(0) != null)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   880
            Document doc = editor.getDocument();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   881
            if (doc instanceof AbstractDocument) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   882
                ((AbstractDocument)doc).readLock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   883
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   884
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   885
                paintSafely(g);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   886
            } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   887
                if (doc instanceof AbstractDocument) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   888
                    ((AbstractDocument)doc).readUnlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   889
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   890
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   891
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   892
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   893
90ce3da70b43 Initial load
duke
parents:
diff changeset
   894
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   895
     * Gets the preferred size for the editor component.  If the component
90ce3da70b43 Initial load
duke
parents:
diff changeset
   896
     * has been given a size prior to receiving this request, it will
90ce3da70b43 Initial load
duke
parents:
diff changeset
   897
     * set the size of the view hierarchy to reflect the size of the component
90ce3da70b43 Initial load
duke
parents:
diff changeset
   898
     * before requesting the preferred size of the view hierarchy.  This
90ce3da70b43 Initial load
duke
parents:
diff changeset
   899
     * allows formatted views to format to the current component size before
90ce3da70b43 Initial load
duke
parents:
diff changeset
   900
     * answering the request.  Other views don't care about currently formatted
90ce3da70b43 Initial load
duke
parents:
diff changeset
   901
     * size and give the same answer either way.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   902
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   903
     * @param c the editor component
90ce3da70b43 Initial load
duke
parents:
diff changeset
   904
     * @return the size
90ce3da70b43 Initial load
duke
parents:
diff changeset
   905
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   906
    public Dimension getPreferredSize(JComponent c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   907
        Document doc = editor.getDocument();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   908
        Insets i = c.getInsets();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   909
        Dimension d = c.getSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   910
90ce3da70b43 Initial load
duke
parents:
diff changeset
   911
        if (doc instanceof AbstractDocument) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   912
            ((AbstractDocument)doc).readLock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   913
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   914
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   915
            if ((d.width > (i.left + i.right)) && (d.height > (i.top + i.bottom))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   916
                rootView.setSize(d.width - i.left - i.right, d.height - i.top - i.bottom);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   917
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   918
            else if (d.width == 0 && d.height == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   919
                // Probably haven't been layed out yet, force some sort of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   920
                // initial sizing.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   921
                rootView.setSize(Integer.MAX_VALUE, Integer.MAX_VALUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   922
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   923
            d.width = (int) Math.min((long) rootView.getPreferredSpan(View.X_AXIS) +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   924
                                     (long) i.left + (long) i.right, Integer.MAX_VALUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   925
            d.height = (int) Math.min((long) rootView.getPreferredSpan(View.Y_AXIS) +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   926
                                      (long) i.top + (long) i.bottom, Integer.MAX_VALUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   927
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   928
            if (doc instanceof AbstractDocument) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   929
                ((AbstractDocument)doc).readUnlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   930
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   931
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   932
        return d;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   933
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   934
90ce3da70b43 Initial load
duke
parents:
diff changeset
   935
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   936
     * Gets the minimum size for the editor component.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   937
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   938
     * @param c the editor component
90ce3da70b43 Initial load
duke
parents:
diff changeset
   939
     * @return the size
90ce3da70b43 Initial load
duke
parents:
diff changeset
   940
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   941
    public Dimension getMinimumSize(JComponent c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   942
        Document doc = editor.getDocument();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   943
        Insets i = c.getInsets();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   944
        Dimension d = new Dimension();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   945
        if (doc instanceof AbstractDocument) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   946
            ((AbstractDocument)doc).readLock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   947
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   948
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   949
            d.width = (int) rootView.getMinimumSpan(View.X_AXIS) + i.left + i.right;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   950
            d.height = (int)  rootView.getMinimumSpan(View.Y_AXIS) + i.top + i.bottom;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   951
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   952
            if (doc instanceof AbstractDocument) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   953
                ((AbstractDocument)doc).readUnlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   954
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   955
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   956
        return d;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   957
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   958
90ce3da70b43 Initial load
duke
parents:
diff changeset
   959
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   960
     * Gets the maximum size for the editor component.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   961
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   962
     * @param c the editor component
90ce3da70b43 Initial load
duke
parents:
diff changeset
   963
     * @return the size
90ce3da70b43 Initial load
duke
parents:
diff changeset
   964
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   965
    public Dimension getMaximumSize(JComponent c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   966
        Document doc = editor.getDocument();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   967
        Insets i = c.getInsets();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   968
        Dimension d = new Dimension();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   969
        if (doc instanceof AbstractDocument) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   970
            ((AbstractDocument)doc).readLock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   971
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   972
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   973
            d.width = (int) Math.min((long) rootView.getMaximumSpan(View.X_AXIS) +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   974
                                     (long) i.left + (long) i.right, Integer.MAX_VALUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   975
            d.height = (int) Math.min((long) rootView.getMaximumSpan(View.Y_AXIS) +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   976
                                      (long) i.top + (long) i.bottom, Integer.MAX_VALUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   977
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   978
            if (doc instanceof AbstractDocument) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   979
                ((AbstractDocument)doc).readUnlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   980
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   981
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   982
        return d;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   983
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   984
90ce3da70b43 Initial load
duke
parents:
diff changeset
   985
    // ---- TextUI methods -------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
   986
90ce3da70b43 Initial load
duke
parents:
diff changeset
   987
90ce3da70b43 Initial load
duke
parents:
diff changeset
   988
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   989
     * Gets the allocation to give the root View.  Due
90ce3da70b43 Initial load
duke
parents:
diff changeset
   990
     * to an unfortunate set of historical events this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   991
     * method is inappropriately named.  The Rectangle
90ce3da70b43 Initial load
duke
parents:
diff changeset
   992
     * returned has nothing to do with visibility.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   993
     * The component must have a non-zero positive size for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   994
     * this translation to be computed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   995
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   996
     * @return the bounding box for the root view
90ce3da70b43 Initial load
duke
parents:
diff changeset
   997
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   998
    protected Rectangle getVisibleEditorRect() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   999
        Rectangle alloc = editor.getBounds();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1000
        if ((alloc.width > 0) && (alloc.height > 0)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1001
            alloc.x = alloc.y = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1002
            Insets insets = editor.getInsets();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1003
            alloc.x += insets.left;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1004
            alloc.y += insets.top;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1005
            alloc.width -= insets.left + insets.right;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1006
            alloc.height -= insets.top + insets.bottom;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1007
            return alloc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1008
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1009
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1010
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1011
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1012
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1013
     * Converts the given location in the model to a place in
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1014
     * the view coordinate system.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1015
     * The component must have a non-zero positive size for
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1016
     * this translation to be computed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1017
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1018
     * @param tc the text component for which this UI is installed
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1019
     * @param pos the local location in the model to translate >= 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1020
     * @return the coordinates as a rectangle, null if the model is not painted
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1021
     * @exception BadLocationException  if the given position does not
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1022
     *   represent a valid location in the associated document
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1023
     * @see TextUI#modelToView
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1024
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1025
    public Rectangle modelToView(JTextComponent tc, int pos) throws BadLocationException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1026
        return modelToView(tc, pos, Position.Bias.Forward);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1027
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1028
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1029
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1030
     * Converts the given location in the model to a place in
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1031
     * the view coordinate system.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1032
     * The component must have a non-zero positive size for
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1033
     * this translation to be computed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1034
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1035
     * @param tc the text component for which this UI is installed
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1036
     * @param pos the local location in the model to translate >= 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1037
     * @return the coordinates as a rectangle, null if the model is not painted
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1038
     * @exception BadLocationException  if the given position does not
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1039
     *   represent a valid location in the associated document
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1040
     * @see TextUI#modelToView
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1041
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1042
    public Rectangle modelToView(JTextComponent tc, int pos, Position.Bias bias) throws BadLocationException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1043
        Document doc = editor.getDocument();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1044
        if (doc instanceof AbstractDocument) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1045
            ((AbstractDocument)doc).readLock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1046
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1047
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1048
            Rectangle alloc = getVisibleEditorRect();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1049
            if (alloc != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1050
                rootView.setSize(alloc.width, alloc.height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1051
                Shape s = rootView.modelToView(pos, alloc, bias);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1052
                if (s != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1053
                  return s.getBounds();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1054
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1055
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1056
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1057
            if (doc instanceof AbstractDocument) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1058
                ((AbstractDocument)doc).readUnlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1059
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1060
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1061
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1062
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1063
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1064
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1065
     * Converts the given place in the view coordinate system
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1066
     * to the nearest representative location in the model.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1067
     * The component must have a non-zero positive size for
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1068
     * this translation to be computed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1069
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1070
     * @param tc the text component for which this UI is installed
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1071
     * @param pt the location in the view to translate.  This
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1072
     *  should be in the same coordinate system as the mouse events.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1073
     * @return the offset from the start of the document >= 0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1074
     *   -1 if not painted
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1075
     * @see TextUI#viewToModel
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1076
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1077
    public int viewToModel(JTextComponent tc, Point pt) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1078
        return viewToModel(tc, pt, discardBias);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1079
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1080
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1081
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1082
     * Converts the given place in the view coordinate system
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1083
     * to the nearest representative location in the model.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1084
     * The component must have a non-zero positive size for
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1085
     * this translation to be computed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1086
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1087
     * @param tc the text component for which this UI is installed
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1088
     * @param pt the location in the view to translate.  This
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1089
     *  should be in the same coordinate system as the mouse events.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1090
     * @return the offset from the start of the document >= 0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1091
     *   -1 if the component doesn't yet have a positive size.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1092
     * @see TextUI#viewToModel
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1093
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1094
    public int viewToModel(JTextComponent tc, Point pt,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1095
                           Position.Bias[] biasReturn) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1096
        int offs = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1097
        Document doc = editor.getDocument();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1098
        if (doc instanceof AbstractDocument) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1099
            ((AbstractDocument)doc).readLock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1100
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1101
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1102
            Rectangle alloc = getVisibleEditorRect();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1103
            if (alloc != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1104
                rootView.setSize(alloc.width, alloc.height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1105
                offs = rootView.viewToModel(pt.x, pt.y, alloc, biasReturn);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1106
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1107
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1108
            if (doc instanceof AbstractDocument) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1109
                ((AbstractDocument)doc).readUnlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1110
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1111
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1112
        return offs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1113
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1114
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1115
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1116
     * {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1117
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1118
    public int getNextVisualPositionFrom(JTextComponent t, int pos,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1119
                    Position.Bias b, int direction, Position.Bias[] biasRet)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1120
                    throws BadLocationException{
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1121
        Document doc = editor.getDocument();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1122
        if (doc instanceof AbstractDocument) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1123
            ((AbstractDocument)doc).readLock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1124
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1125
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1126
            if (painted) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1127
                Rectangle alloc = getVisibleEditorRect();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1128
                if (alloc != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1129
                    rootView.setSize(alloc.width, alloc.height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1130
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1131
                return rootView.getNextVisualPositionFrom(pos, b, alloc, direction,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1132
                                                          biasRet);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1133
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1134
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1135
            if (doc instanceof AbstractDocument) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1136
                ((AbstractDocument)doc).readUnlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1137
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1138
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1139
        return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1140
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1141
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1142
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1143
     * Causes the portion of the view responsible for the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1144
     * given part of the model to be repainted.  Does nothing if
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1145
     * the view is not currently painted.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1146
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1147
     * @param tc the text component for which this UI is installed
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1148
     * @param p0 the beginning of the range >= 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1149
     * @param p1 the end of the range >= p0
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1150
     * @see TextUI#damageRange
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1151
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1152
    public void damageRange(JTextComponent tc, int p0, int p1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1153
        damageRange(tc, p0, p1, Position.Bias.Forward, Position.Bias.Backward);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1154
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1155
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1156
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1157
     * Causes the portion of the view responsible for the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1158
     * given part of the model to be repainted.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1159
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1160
     * @param p0 the beginning of the range >= 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1161
     * @param p1 the end of the range >= p0
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1162
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1163
    public void damageRange(JTextComponent t, int p0, int p1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1164
                            Position.Bias p0Bias, Position.Bias p1Bias) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1165
        if (painted) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1166
            Rectangle alloc = getVisibleEditorRect();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1167
            if (alloc != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1168
                Document doc = t.getDocument();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1169
                if (doc instanceof AbstractDocument) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1170
                    ((AbstractDocument)doc).readLock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1171
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1172
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1173
                    rootView.setSize(alloc.width, alloc.height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1174
                    Shape toDamage = rootView.modelToView(p0, p0Bias,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1175
                            p1, p1Bias, alloc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1176
                    Rectangle rect = (toDamage instanceof Rectangle) ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1177
                            (Rectangle)toDamage : toDamage.getBounds();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1178
                    editor.repaint(rect.x, rect.y, rect.width, rect.height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1179
                } catch (BadLocationException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1180
                } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1181
                    if (doc instanceof AbstractDocument) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1182
                        ((AbstractDocument)doc).readUnlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1183
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1184
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1185
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1186
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1187
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1188
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1189
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1190
     * Fetches the EditorKit for the UI.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1191
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1192
     * @param tc the text component for which this UI is installed
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1193
     * @return the editor capabilities
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1194
     * @see TextUI#getEditorKit
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1195
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1196
    public EditorKit getEditorKit(JTextComponent tc) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1197
        return defaultKit;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1198
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1199
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1200
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1201
     * Fetches a View with the allocation of the associated
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1202
     * text component (i.e. the root of the hierarchy) that
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1203
     * can be traversed to determine how the model is being
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1204
     * represented spatially.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1205
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1206
     * <font color=red><b>NOTE:</b>The View hierarchy can
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1207
     * be traversed from the root view, and other things
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1208
     * can be done as well.  Things done in this way cannot
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1209
     * be protected like simple method calls through the TextUI.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1210
     * Therefore, proper operation in the presence of concurrency
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1211
     * must be arranged by any logic that calls this method!
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1212
     * </font>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1213
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1214
     * @param tc the text component for which this UI is installed
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1215
     * @return the view
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1216
     * @see TextUI#getRootView
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1217
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1218
    public View getRootView(JTextComponent tc) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1219
        return rootView;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1220
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1221
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1222
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1223
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1224
     * Returns the string to be used as the tooltip at the passed in location.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1225
     * This forwards the method onto the root View.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1226
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1227
     * @see javax.swing.text.JTextComponent#getToolTipText
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1228
     * @see javax.swing.text.View#getToolTipText
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1229
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1230
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1231
    public String getToolTipText(JTextComponent t, Point pt) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1232
        if (!painted) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1233
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1234
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1235
        Document doc = editor.getDocument();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1236
        String tt = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1237
        Rectangle alloc = getVisibleEditorRect();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1238
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1239
        if (alloc != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1240
            if (doc instanceof AbstractDocument) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1241
                ((AbstractDocument)doc).readLock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1242
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1243
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1244
                tt = rootView.getToolTipText(pt.x, pt.y, alloc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1245
            } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1246
                if (doc instanceof AbstractDocument) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1247
                    ((AbstractDocument)doc).readUnlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1248
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1249
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1250
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1251
        return tt;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1252
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1253
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1254
    // --- ViewFactory methods ------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1255
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1256
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1257
     * Creates a view for an element.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1258
     * If a subclass wishes to directly implement the factory
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1259
     * producing the view(s), it should reimplement this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1260
     * method.  By default it simply returns null indicating
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1261
     * it is unable to represent the element.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1262
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1263
     * @param elem the element
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1264
     * @return the view
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1265
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1266
    public View create(Element elem) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1267
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1268
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1269
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1270
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1271
     * Creates a view for an element.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1272
     * If a subclass wishes to directly implement the factory
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1273
     * producing the view(s), it should reimplement this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1274
     * method.  By default it simply returns null indicating
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1275
     * it is unable to represent the part of the element.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1276
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1277
     * @param elem the element
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1278
     * @param p0 the starting offset >= 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1279
     * @param p1 the ending offset >= p0
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1280
     * @return the view
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1281
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1282
    public View create(Element elem, int p0, int p1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1283
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1284
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1285
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1286
    public static class BasicCaret extends DefaultCaret implements UIResource {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1287
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1288
    public static class BasicHighlighter extends DefaultHighlighter implements UIResource {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1289
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1290
    static class BasicCursor extends Cursor implements UIResource {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1291
        BasicCursor(int type) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1292
            super(type);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1293
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1294
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1295
        BasicCursor(String name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1296
            super(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1297
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1298
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1299
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1300
    private static BasicCursor textCursor = new BasicCursor(Cursor.TEXT_CURSOR);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1301
    // ----- member variables ---------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1302
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1303
    private static final EditorKit defaultKit = new DefaultEditorKit();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1304
    transient JTextComponent editor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1305
    transient boolean painted;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1306
    transient RootView rootView = new RootView();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1307
    transient UpdateHandler updateHandler = new UpdateHandler();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1308
    private static final TransferHandler defaultTransferHandler = new TextTransferHandler();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1309
    private final DragListener dragListener = getDragListener();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1310
    private static final Position.Bias[] discardBias = new Position.Bias[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1311
    private DefaultCaret dropCaret;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1312
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1313
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1314
     * Root view that acts as a gateway between the component
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1315
     * and the View hierarchy.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1316
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1317
    class RootView extends View {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1318
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1319
        RootView() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1320
            super(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1321
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1322
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1323
        void setView(View v) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1324
            View oldView = view;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1325
            view = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1326
            if (oldView != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1327
                // get rid of back reference so that the old
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1328
                // hierarchy can be garbage collected.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1329
                oldView.setParent(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1330
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1331
            if (v != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1332
                v.setParent(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1333
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1334
            view = v;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1335
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1336
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1337
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1338
         * Fetches the attributes to use when rendering.  At the root
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1339
         * level there are no attributes.  If an attribute is resolved
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1340
         * up the view hierarchy this is the end of the line.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1341
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1342
        public AttributeSet getAttributes() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1343
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1344
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1345
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1346
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1347
         * Determines the preferred span for this view along an axis.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1348
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1349
         * @param axis may be either X_AXIS or Y_AXIS
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1350
         * @return the span the view would like to be rendered into.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1351
         *         Typically the view is told to render into the span
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1352
         *         that is returned, although there is no guarantee.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1353
         *         The parent may choose to resize or break the view.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1354
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1355
        public float getPreferredSpan(int axis) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1356
            if (view != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1357
                return view.getPreferredSpan(axis);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1358
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1359
            return 10;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1360
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1361
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1362
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1363
         * Determines the minimum span for this view along an axis.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1364
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1365
         * @param axis may be either X_AXIS or Y_AXIS
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1366
         * @return the span the view would like to be rendered into.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1367
         *         Typically the view is told to render into the span
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1368
         *         that is returned, although there is no guarantee.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1369
         *         The parent may choose to resize or break the view.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1370
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1371
        public float getMinimumSpan(int axis) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1372
            if (view != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1373
                return view.getMinimumSpan(axis);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1374
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1375
            return 10;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1376
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1377
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1378
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1379
         * Determines the maximum span for this view along an axis.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1380
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1381
         * @param axis may be either X_AXIS or Y_AXIS
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1382
         * @return the span the view would like to be rendered into.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1383
         *         Typically the view is told to render into the span
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1384
         *         that is returned, although there is no guarantee.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1385
         *         The parent may choose to resize or break the view.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1386
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1387
        public float getMaximumSpan(int axis) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1388
            return Integer.MAX_VALUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1389
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1390
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1391
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1392
         * Specifies that a preference has changed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1393
         * Child views can call this on the parent to indicate that
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1394
         * the preference has changed.  The root view routes this to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1395
         * invalidate on the hosting component.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1396
         * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1397
         * This can be called on a different thread from the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1398
         * event dispatching thread and is basically unsafe to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1399
         * propagate into the component.  To make this safe,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1400
         * the operation is transferred over to the event dispatching
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1401
         * thread for completion.  It is a design goal that all view
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1402
         * methods be safe to call without concern for concurrency,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1403
         * and this behavior helps make that true.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1404
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1405
         * @param child the child view
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1406
         * @param width true if the width preference has changed
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1407
         * @param height true if the height preference has changed
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1408
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1409
        public void preferenceChanged(View child, boolean width, boolean height) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1410
            editor.revalidate();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1411
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1412
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1413
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1414
         * Determines the desired alignment for this view along an axis.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1415
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1416
         * @param axis may be either X_AXIS or Y_AXIS
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1417
         * @return the desired alignment, where 0.0 indicates the origin
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1418
         *     and 1.0 the full span away from the origin
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1419
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1420
        public float getAlignment(int axis) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1421
            if (view != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1422
                return view.getAlignment(axis);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1423
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1424
            return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1425
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1426
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1427
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1428
         * Renders the view.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1429
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1430
         * @param g the graphics context
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1431
         * @param allocation the region to render into
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1432
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1433
        public void paint(Graphics g, Shape allocation) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1434
            if (view != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1435
                Rectangle alloc = (allocation instanceof Rectangle) ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1436
                          (Rectangle)allocation : allocation.getBounds();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1437
                setSize(alloc.width, alloc.height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1438
                view.paint(g, allocation);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1439
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1440
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1441
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1442
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1443
         * Sets the view parent.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1444
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1445
         * @param parent the parent view
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1446
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1447
        public void setParent(View parent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1448
            throw new Error("Can't set parent on root view");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1449
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1450
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1451
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1452
         * Returns the number of views in this view.  Since
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1453
         * this view simply wraps the root of the view hierarchy
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1454
         * it has exactly one child.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1455
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1456
         * @return the number of views
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1457
         * @see #getView
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1458
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1459
        public int getViewCount() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1460
            return 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1461
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1462
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1463
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1464
         * Gets the n-th view in this container.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1465
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1466
         * @param n the number of the view to get
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1467
         * @return the view
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1468
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1469
        public View getView(int n) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1470
            return view;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1471
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1472
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1473
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1474
         * Returns the child view index representing the given position in
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1475
         * the model.  This is implemented to return the index of the only
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1476
         * child.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1477
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1478
         * @param pos the position >= 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1479
         * @return  index of the view representing the given position, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1480
         *   -1 if no view represents that position
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1481
         * @since 1.3
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1482
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1483
        public int getViewIndex(int pos, Position.Bias b) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1484
            return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1485
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1486
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1487
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1488
         * Fetches the allocation for the given child view.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1489
         * This enables finding out where various views
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1490
         * are located, without assuming the views store
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1491
         * their location.  This returns the given allocation
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1492
         * since this view simply acts as a gateway between
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1493
         * the view hierarchy and the associated component.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1494
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1495
         * @param index the index of the child
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1496
         * @param a  the allocation to this view.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1497
         * @return the allocation to the child
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1498
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1499
        public Shape getChildAllocation(int index, Shape a) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1500
            return a;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1501
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1502
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1503
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1504
         * Provides a mapping from the document model coordinate space
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1505
         * to the coordinate space of the view mapped to it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1506
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1507
         * @param pos the position to convert
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1508
         * @param a the allocated region to render into
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1509
         * @return the bounding box of the given position
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1510
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1511
        public Shape modelToView(int pos, Shape a, Position.Bias b) throws BadLocationException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1512
            if (view != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1513
                return view.modelToView(pos, a, b);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1514
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1515
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1516
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1517
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1518
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1519
         * Provides a mapping from the document model coordinate space
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1520
         * to the coordinate space of the view mapped to it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1521
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1522
         * @param p0 the position to convert >= 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1523
         * @param b0 the bias toward the previous character or the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1524
         *  next character represented by p0, in case the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1525
         *  position is a boundary of two views.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1526
         * @param p1 the position to convert >= 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1527
         * @param b1 the bias toward the previous character or the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1528
         *  next character represented by p1, in case the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1529
         *  position is a boundary of two views.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1530
         * @param a the allocated region to render into
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1531
         * @return the bounding box of the given position is returned
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1532
         * @exception BadLocationException  if the given position does
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1533
         *   not represent a valid location in the associated document
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1534
         * @exception IllegalArgumentException for an invalid bias argument
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1535
         * @see View#viewToModel
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1536
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1537
        public Shape modelToView(int p0, Position.Bias b0, int p1, Position.Bias b1, Shape a) throws BadLocationException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1538
            if (view != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1539
                return view.modelToView(p0, b0, p1, b1, a);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1540
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1541
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1542
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1543
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1544
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1545
         * Provides a mapping from the view coordinate space to the logical
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1546
         * coordinate space of the model.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1547
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1548
         * @param x x coordinate of the view location to convert
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1549
         * @param y y coordinate of the view location to convert
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1550
         * @param a the allocated region to render into
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1551
         * @return the location within the model that best represents the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1552
         *    given point in the view
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1553
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1554
        public int viewToModel(float x, float y, Shape a, Position.Bias[] bias) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1555
            if (view != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1556
                int retValue = view.viewToModel(x, y, a, bias);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1557
                return retValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1558
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1559
            return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1560
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1561
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1562
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1563
         * Provides a way to determine the next visually represented model
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1564
         * location that one might place a caret.  Some views may not be visible,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1565
         * they might not be in the same order found in the model, or they just
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1566
         * might not allow access to some of the locations in the model.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1567
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1568
         * @param pos the position to convert >= 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1569
         * @param a the allocated region to render into
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1570
         * @param direction the direction from the current position that can
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1571
         *  be thought of as the arrow keys typically found on a keyboard.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1572
         *  This may be SwingConstants.WEST, SwingConstants.EAST,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1573
         *  SwingConstants.NORTH, or SwingConstants.SOUTH.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1574
         * @return the location within the model that best represents the next
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1575
         *  location visual position.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1576
         * @exception BadLocationException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1577
         * @exception IllegalArgumentException for an invalid direction
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1578
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1579
        public int getNextVisualPositionFrom(int pos, Position.Bias b, Shape a,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1580
                                             int direction,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1581
                                             Position.Bias[] biasRet)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1582
            throws BadLocationException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1583
            if( view != null ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1584
                int nextPos = view.getNextVisualPositionFrom(pos, b, a,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1585
                                                     direction, biasRet);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1586
                if(nextPos != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1587
                    pos = nextPos;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1588
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1589
                else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1590
                    biasRet[0] = b;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1591
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1592
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1593
            return pos;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1594
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1595
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1596
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1597
         * Gives notification that something was inserted into the document
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1598
         * in a location that this view is responsible for.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1599
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1600
         * @param e the change information from the associated document
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1601
         * @param a the current allocation of the view
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1602
         * @param f the factory to use to rebuild if the view has children
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1603
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1604
        public void insertUpdate(DocumentEvent e, Shape a, ViewFactory f) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1605
            if (view != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1606
                view.insertUpdate(e, a, f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1607
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1608
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1609
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1610
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1611
         * Gives notification that something was removed from the document
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1612
         * in a location that this view is responsible for.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1613
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1614
         * @param e the change information from the associated document
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1615
         * @param a the current allocation of the view
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1616
         * @param f the factory to use to rebuild if the view has children
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1617
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1618
        public void removeUpdate(DocumentEvent e, Shape a, ViewFactory f) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1619
            if (view != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1620
                view.removeUpdate(e, a, f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1621
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1622
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1623
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1624
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1625
         * Gives notification from the document that attributes were changed
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1626
         * in a location that this view is responsible for.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1627
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1628
         * @param e the change information from the associated document
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1629
         * @param a the current allocation of the view
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1630
         * @param f the factory to use to rebuild if the view has children
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1631
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1632
        public void changedUpdate(DocumentEvent e, Shape a, ViewFactory f) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1633
            if (view != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1634
                view.changedUpdate(e, a, f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1635
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1636
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1637
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1638
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1639
         * Returns the document model underlying the view.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1640
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1641
         * @return the model
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1642
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1643
        public Document getDocument() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1644
            return editor.getDocument();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1645
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1646
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1647
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1648
         * Returns the starting offset into the model for this view.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1649
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1650
         * @return the starting offset
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1651
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1652
        public int getStartOffset() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1653
            if (view != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1654
                return view.getStartOffset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1655
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1656
            return getElement().getStartOffset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1657
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1658
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1659
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1660
         * Returns the ending offset into the model for this view.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1661
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1662
         * @return the ending offset
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1663
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1664
        public int getEndOffset() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1665
            if (view != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1666
                return view.getEndOffset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1667
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1668
            return getElement().getEndOffset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1669
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1670
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1671
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1672
         * Gets the element that this view is mapped to.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1673
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1674
         * @return the view
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1675
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1676
        public Element getElement() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1677
            if (view != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1678
                return view.getElement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1679
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1680
            return editor.getDocument().getDefaultRootElement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1681
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1682
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1683
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1684
         * Breaks this view on the given axis at the given length.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1685
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1686
         * @param axis may be either X_AXIS or Y_AXIS
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1687
         * @param len specifies where a break is desired in the span
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1688
         * @param the current allocation of the view
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1689
         * @return the fragment of the view that represents the given span
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1690
         *   if the view can be broken, otherwise null
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1691
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1692
        public View breakView(int axis, float len, Shape a) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1693
            throw new Error("Can't break root view");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1694
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1695
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1696
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1697
         * Determines the resizability of the view along the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1698
         * given axis.  A value of 0 or less is not resizable.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1699
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1700
         * @param axis may be either X_AXIS or Y_AXIS
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1701
         * @return the weight
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1702
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1703
        public int getResizeWeight(int axis) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1704
            if (view != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1705
                return view.getResizeWeight(axis);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1706
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1707
            return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1708
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1709
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1710
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1711
         * Sets the view size.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1712
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1713
         * @param width the width
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1714
         * @param height the height
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1715
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1716
        public void setSize(float width, float height) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1717
            if (view != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1718
                view.setSize(width, height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1719
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1720
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1721
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1722
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1723
         * Fetches the container hosting the view.  This is useful for
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1724
         * things like scheduling a repaint, finding out the host
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1725
         * components font, etc.  The default implementation
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1726
         * of this is to forward the query to the parent view.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1727
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1728
         * @return the container
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1729
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1730
        public Container getContainer() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1731
            return editor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1732
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1733
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1734
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1735
         * Fetches the factory to be used for building the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1736
         * various view fragments that make up the view that
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1737
         * represents the model.  This is what determines
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1738
         * how the model will be represented.  This is implemented
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1739
         * to fetch the factory provided by the associated
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1740
         * EditorKit unless that is null, in which case this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1741
         * simply returns the BasicTextUI itself which allows
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1742
         * subclasses to implement a simple factory directly without
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1743
         * creating extra objects.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1744
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1745
         * @return the factory
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1746
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1747
        public ViewFactory getViewFactory() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1748
            EditorKit kit = getEditorKit(editor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1749
            ViewFactory f = kit.getViewFactory();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1750
            if (f != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1751
                return f;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1752
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1753
            return BasicTextUI.this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1754
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1755
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1756
        private View view;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1757
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1758
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1759
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1760
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1761
     * Handles updates from various places.  If the model is changed,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1762
     * this class unregisters as a listener to the old model and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1763
     * registers with the new model.  If the document model changes,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1764
     * the change is forwarded to the root view.  If the focus
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1765
     * accelerator changes, a new keystroke is registered to request
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1766
     * focus.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1767
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1768
    class UpdateHandler implements PropertyChangeListener, DocumentListener, LayoutManager2, UIResource {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1769
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1770
        // --- PropertyChangeListener methods -----------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1771
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1772
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1773
         * This method gets called when a bound property is changed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1774
         * We are looking for document changes on the editor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1775
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1776
        public final void propertyChange(PropertyChangeEvent evt) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1777
            Object oldValue = evt.getOldValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1778
            Object newValue = evt.getNewValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1779
            String propertyName = evt.getPropertyName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1780
            if ((oldValue instanceof Document) || (newValue instanceof Document)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1781
                if (oldValue != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1782
                    ((Document)oldValue).removeDocumentListener(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1783
                    i18nView = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1784
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1785
                if (newValue != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1786
                    ((Document)newValue).addDocumentListener(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1787
                    if ("document" == propertyName) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1788
                        setView(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1789
                        BasicTextUI.this.propertyChange(evt);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1790
                        modelChanged();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1791
                        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1792
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1793
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1794
                modelChanged();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1795
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1796
            if ("focusAccelerator" == propertyName) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1797
                updateFocusAcceleratorBinding(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1798
            } else if ("componentOrientation" == propertyName) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1799
                // Changes in ComponentOrientation require the views to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1800
                // rebuilt.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1801
                modelChanged();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1802
            } else if ("font" == propertyName) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1803
                modelChanged();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1804
            } else if ("dropLocation" == propertyName) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1805
                dropIndexChanged();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1806
            } else if ("editable" == propertyName) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1807
                updateCursor();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1808
                modelChanged();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1809
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1810
            BasicTextUI.this.propertyChange(evt);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1811
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1812
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1813
        private void dropIndexChanged() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1814
            if (editor.getDropMode() == DropMode.USE_SELECTION) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1815
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1816
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1817
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1818
            JTextComponent.DropLocation dropLocation = editor.getDropLocation();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1819
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1820
            if (dropLocation == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1821
                if (dropCaret != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1822
                    dropCaret.deinstall(editor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1823
                    editor.repaint(dropCaret);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1824
                    dropCaret = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1825
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1826
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1827
                if (dropCaret == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1828
                    dropCaret = new BasicCaret();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1829
                    dropCaret.install(editor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1830
                    dropCaret.setVisible(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1831
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1832
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1833
                dropCaret.setDot(dropLocation.getIndex(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1834
                                 dropLocation.getBias());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1835
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1836
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1837
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1838
        // --- DocumentListener methods -----------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1839
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1840
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1841
         * The insert notification.  Gets sent to the root of the view structure
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1842
         * that represents the portion of the model being represented by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1843
         * editor.  The factory is added as an argument to the update so that
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1844
         * the views can update themselves in a dynamic (not hardcoded) way.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1845
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1846
         * @param e  The change notification from the currently associated
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1847
         *  document.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1848
         * @see DocumentListener#insertUpdate
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1849
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1850
        public final void insertUpdate(DocumentEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1851
            Document doc = e.getDocument();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1852
            Object o = doc.getProperty("i18n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1853
            if (o instanceof Boolean) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1854
                Boolean i18nFlag = (Boolean) o;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1855
                if (i18nFlag.booleanValue() != i18nView) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1856
                    // i18n flag changed, rebuild the view
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1857
                    i18nView = i18nFlag.booleanValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1858
                    modelChanged();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1859
                    return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1860
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1861
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1862
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1863
            // normal insert update
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1864
            Rectangle alloc = (painted) ? getVisibleEditorRect() : null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1865
            rootView.insertUpdate(e, alloc, rootView.getViewFactory());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1866
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1867
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1868
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1869
         * The remove notification.  Gets sent to the root of the view structure
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1870
         * that represents the portion of the model being represented by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1871
         * editor.  The factory is added as an argument to the update so that
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1872
         * the views can update themselves in a dynamic (not hardcoded) way.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1873
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1874
         * @param e  The change notification from the currently associated
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1875
         *  document.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1876
         * @see DocumentListener#removeUpdate
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1877
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1878
        public final void removeUpdate(DocumentEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1879
            Rectangle alloc = (painted) ? getVisibleEditorRect() : null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1880
            rootView.removeUpdate(e, alloc, rootView.getViewFactory());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1881
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1882
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1883
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1884
         * The change notification.  Gets sent to the root of the view structure
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1885
         * that represents the portion of the model being represented by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1886
         * editor.  The factory is added as an argument to the update so that
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1887
         * the views can update themselves in a dynamic (not hardcoded) way.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1888
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1889
         * @param e  The change notification from the currently associated
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1890
         *  document.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1891
         * @see DocumentListener#changeUpdate
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1892
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1893
        public final void changedUpdate(DocumentEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1894
            Rectangle alloc = (painted) ? getVisibleEditorRect() : null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1895
            rootView.changedUpdate(e, alloc, rootView.getViewFactory());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1896
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1897
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1898
        // --- LayoutManager2 methods --------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1899
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1900
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1901
         * Adds the specified component with the specified name to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1902
         * the layout.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1903
         * @param name the component name
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1904
         * @param comp the component to be added
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1905
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1906
        public void addLayoutComponent(String name, Component comp) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1907
            // not supported
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1908
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1909
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1910
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1911
         * Removes the specified component from the layout.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1912
         * @param comp the component to be removed
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1913
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1914
        public void removeLayoutComponent(Component comp) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1915
            if (constraints != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1916
                // remove the constraint record
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1917
                constraints.remove(comp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1918
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1919
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1920
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1921
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1922
         * Calculates the preferred size dimensions for the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1923
         * panel given the components in the specified parent container.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1924
         * @param parent the component to be laid out
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1925
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1926
         * @see #minimumLayoutSize
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1927
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1928
        public Dimension preferredLayoutSize(Container parent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1929
            // should not be called (JComponent uses UI instead)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1930
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1931
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1932
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1933
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1934
         * Calculates the minimum size dimensions for the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1935
         * panel given the components in the specified parent container.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1936
         * @param parent the component to be laid out
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1937
         * @see #preferredLayoutSize
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1938
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1939
        public Dimension minimumLayoutSize(Container parent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1940
            // should not be called (JComponent uses UI instead)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1941
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1942
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1943
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1944
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1945
         * Lays out the container in the specified panel.  This is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1946
         * implemented to position all components that were added
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1947
         * with a View object as a constraint.  The current allocation
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1948
         * of the associated View is used as the location of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1949
         * component.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1950
         * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1951
         * A read-lock is acquired on the document to prevent the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1952
         * view tree from being modified while the layout process
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1953
         * is active.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1954
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1955
         * @param parent the component which needs to be laid out
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1956
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1957
        public void layoutContainer(Container parent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1958
            if ((constraints != null) && (! constraints.isEmpty())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1959
                Rectangle alloc = getVisibleEditorRect();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1960
                if (alloc != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1961
                    Document doc = editor.getDocument();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1962
                    if (doc instanceof AbstractDocument) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1963
                        ((AbstractDocument)doc).readLock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1964
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1965
                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1966
                        rootView.setSize(alloc.width, alloc.height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1967
                        Enumeration components = constraints.keys();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1968
                        while (components.hasMoreElements()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1969
                            Component comp = (Component) components.nextElement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1970
                            View v = (View) constraints.get(comp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1971
                            Shape ca = calculateViewPosition(alloc, v);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1972
                            if (ca != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1973
                                Rectangle compAlloc = (ca instanceof Rectangle) ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1974
                                    (Rectangle) ca : ca.getBounds();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1975
                                comp.setBounds(compAlloc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1976
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1977
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1978
                    } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1979
                        if (doc instanceof AbstractDocument) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1980
                            ((AbstractDocument)doc).readUnlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1981
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1982
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1983
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1984
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1985
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1986
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1987
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1988
         * Find the Shape representing the given view.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1989
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1990
        Shape calculateViewPosition(Shape alloc, View v) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1991
            int pos = v.getStartOffset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1992
            View child = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1993
            for (View parent = rootView; (parent != null) && (parent != v); parent = child) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1994
                int index = parent.getViewIndex(pos, Position.Bias.Forward);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1995
                alloc = parent.getChildAllocation(index, alloc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1996
                child = parent.getView(index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1997
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1998
            return (child != null) ? alloc : null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1999
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2000
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2001
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2002
         * Adds the specified component to the layout, using the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2003
         * constraint object.  We only store those components that were added
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2004
         * with a constraint that is of type View.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2005
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2006
         * @param comp the component to be added
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2007
         * @param constraint  where/how the component is added to the layout.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2008
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2009
        public void addLayoutComponent(Component comp, Object constraint) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2010
            if (constraint instanceof View) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2011
                if (constraints == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2012
                    constraints = new Hashtable(7);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2013
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2014
                constraints.put(comp, constraint);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2015
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2016
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2017
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2018
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2019
         * Returns the maximum size of this component.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2020
         * @see java.awt.Component#getMinimumSize()
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2021
         * @see java.awt.Component#getPreferredSize()
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2022
         * @see LayoutManager
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2023
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2024
        public Dimension maximumLayoutSize(Container target) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2025
            // should not be called (JComponent uses UI instead)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2026
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2027
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2028
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2029
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2030
         * Returns the alignment along the x axis.  This specifies how
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2031
         * the component would like to be aligned relative to other
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2032
         * components.  The value should be a number between 0 and 1
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2033
         * where 0 represents alignment along the origin, 1 is aligned
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2034
         * the furthest away from the origin, 0.5 is centered, etc.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2035
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2036
        public float getLayoutAlignmentX(Container target) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2037
            return 0.5f;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2038
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2039
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2040
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2041
         * Returns the alignment along the y axis.  This specifies how
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2042
         * the component would like to be aligned relative to other
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2043
         * components.  The value should be a number between 0 and 1
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2044
         * where 0 represents alignment along the origin, 1 is aligned
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2045
         * the furthest away from the origin, 0.5 is centered, etc.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2046
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2047
        public float getLayoutAlignmentY(Container target) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2048
            return 0.5f;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2049
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2050
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2051
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2052
         * Invalidates the layout, indicating that if the layout manager
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2053
         * has cached information it should be discarded.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2054
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2055
        public void invalidateLayout(Container target) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2056
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2057
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2058
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2059
         * The "layout constraints" for the LayoutManager2 implementation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2060
         * These are View objects for those components that are represented
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2061
         * by a View in the View tree.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2062
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2063
        private Hashtable constraints;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2064
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2065
        private boolean i18nView = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2066
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2067
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2068
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2069
     * Wrapper for text actions to return isEnabled false in case editor is non editable
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2070
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2071
    class TextActionWrapper extends TextAction {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2072
        public TextActionWrapper(TextAction action) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2073
            super((String)action.getValue(Action.NAME));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2074
            this.action = action;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2075
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2076
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2077
         * The operation to perform when this action is triggered.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2078
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2079
         * @param e the action event
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2080
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2081
        public void actionPerformed(ActionEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2082
            action.actionPerformed(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2083
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2084
        public boolean isEnabled() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2085
            return (editor == null || editor.isEditable()) ? action.isEnabled() : false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2086
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2087
        TextAction action = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2088
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2089
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2090
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2091
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2092
     * Registered in the ActionMap.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2093
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2094
    class FocusAction extends AbstractAction {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2095
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2096
        public void actionPerformed(ActionEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2097
            editor.requestFocus();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2098
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2099
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2100
        public boolean isEnabled() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2101
            return editor.isEditable();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2102
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2103
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2104
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2105
    private static DragListener getDragListener() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2106
        synchronized(DragListener.class) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2107
            DragListener listener =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2108
                (DragListener)AppContext.getAppContext().
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2109
                    get(DragListener.class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2110
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2111
            if (listener == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2112
                listener = new DragListener();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2113
                AppContext.getAppContext().put(DragListener.class, listener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2114
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2115
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2116
            return listener;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2117
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2118
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2119
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2120
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2121
     * Listens for mouse events for the purposes of detecting drag gestures.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2122
     * BasicTextUI will maintain one of these per AppContext.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2123
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2124
    static class DragListener extends MouseInputAdapter
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2125
                              implements BeforeDrag {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2126
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2127
        private boolean dragStarted;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2128
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2129
        public void dragStarting(MouseEvent me) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2130
            dragStarted = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2131
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2132
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2133
        public void mousePressed(MouseEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2134
            JTextComponent c = (JTextComponent)e.getSource();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2135
            if (c.getDragEnabled()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2136
                dragStarted = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2137
                if (isDragPossible(e) && DragRecognitionSupport.mousePressed(e)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2138
                    e.consume();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2139
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2140
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2141
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2142
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2143
        public void mouseReleased(MouseEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2144
            JTextComponent c = (JTextComponent)e.getSource();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2145
            if (c.getDragEnabled()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2146
                if (dragStarted) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2147
                    e.consume();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2148
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2149
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2150
                DragRecognitionSupport.mouseReleased(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2151
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2152
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2153
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2154
        public void mouseDragged(MouseEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2155
            JTextComponent c = (JTextComponent)e.getSource();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2156
            if (c.getDragEnabled()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2157
                if (dragStarted || DragRecognitionSupport.mouseDragged(e, this)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2158
                    e.consume();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2159
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2160
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2161
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2162
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2163
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2164
         * Determines if the following are true:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2165
         * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2166
         * <li>the component is enabled
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2167
         * <li>the press event is located over a selection
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2168
         * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2169
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2170
        protected boolean isDragPossible(MouseEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2171
            JTextComponent c = (JTextComponent)e.getSource();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2172
            if (c.isEnabled()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2173
                Caret caret = c.getCaret();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2174
                int dot = caret.getDot();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2175
                int mark = caret.getMark();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2176
                if (dot != mark) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2177
                    Point p = new Point(e.getX(), e.getY());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2178
                    int pos = c.viewToModel(p);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2179
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2180
                    int p0 = Math.min(dot, mark);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2181
                    int p1 = Math.max(dot, mark);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2182
                    if ((pos >= p0) && (pos < p1)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2183
                        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2184
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2185
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2186
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2187
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2188
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2189
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2190
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2191
    static class TextTransferHandler extends TransferHandler implements UIResource {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2192
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2193
        private JTextComponent exportComp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2194
        private boolean shouldRemove;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2195
        private int p0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2196
        private int p1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2197
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2198
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2199
         * Whether or not this is a drop using
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2200
         * <code>DropMode.INSERT</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2201
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2202
        private boolean modeBetween = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2203
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2204
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2205
         * Whether or not this is a drop.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2206
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2207
        private boolean isDrop = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2208
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2209
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2210
         * The drop action.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2211
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2212
        private int dropAction = MOVE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2213
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2214
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2215
         * The drop bias.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2216
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2217
        private Position.Bias dropBias;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2218
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2219
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2220
         * Try to find a flavor that can be used to import a Transferable.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2221
         * The set of usable flavors are tried in the following order:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2222
         * <ol>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2223
         *     <li>First, an attempt is made to find a flavor matching the content type
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2224
         *         of the EditorKit for the component.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2225
         *     <li>Second, an attempt to find a text/plain flavor is made.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2226
         *     <li>Third, an attempt to find a flavor representing a String reference
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2227
         *         in the same VM is made.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2228
         *     <li>Lastly, DataFlavor.stringFlavor is searched for.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2229
         * </ol>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2230
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2231
        protected DataFlavor getImportFlavor(DataFlavor[] flavors, JTextComponent c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2232
            DataFlavor plainFlavor = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2233
            DataFlavor refFlavor = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2234
            DataFlavor stringFlavor = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2235
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2236
            if (c instanceof JEditorPane) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2237
                for (int i = 0; i < flavors.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2238
                    String mime = flavors[i].getMimeType();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2239
                    if (mime.startsWith(((JEditorPane)c).getEditorKit().getContentType())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2240
                        return flavors[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2241
                    } else if (plainFlavor == null && mime.startsWith("text/plain")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2242
                        plainFlavor = flavors[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2243
                    } else if (refFlavor == null && mime.startsWith("application/x-java-jvm-local-objectref")
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2244
                                                 && flavors[i].getRepresentationClass() == java.lang.String.class) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2245
                        refFlavor = flavors[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2246
                    } else if (stringFlavor == null && flavors[i].equals(DataFlavor.stringFlavor)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2247
                        stringFlavor = flavors[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2248
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2249
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2250
                if (plainFlavor != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2251
                    return plainFlavor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2252
                } else if (refFlavor != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2253
                    return refFlavor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2254
                } else if (stringFlavor != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2255
                    return stringFlavor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2256
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2257
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2258
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2259
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2260
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2261
            for (int i = 0; i < flavors.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2262
                String mime = flavors[i].getMimeType();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2263
                if (mime.startsWith("text/plain")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2264
                    return flavors[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2265
                } else if (refFlavor == null && mime.startsWith("application/x-java-jvm-local-objectref")
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2266
                                             && flavors[i].getRepresentationClass() == java.lang.String.class) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2267
                    refFlavor = flavors[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2268
                } else if (stringFlavor == null && flavors[i].equals(DataFlavor.stringFlavor)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2269
                    stringFlavor = flavors[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2270
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2271
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2272
            if (refFlavor != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2273
                return refFlavor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2274
            } else if (stringFlavor != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2275
                return stringFlavor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2276
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2277
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2278
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2279
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2280
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2281
         * Import the given stream data into the text component.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2282
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2283
        protected void handleReaderImport(Reader in, JTextComponent c, boolean useRead)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2284
                                               throws BadLocationException, IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2285
            if (useRead) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2286
                int startPosition = c.getSelectionStart();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2287
                int endPosition = c.getSelectionEnd();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2288
                int length = endPosition - startPosition;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2289
                EditorKit kit = c.getUI().getEditorKit(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2290
                Document doc = c.getDocument();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2291
                if (length > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2292
                    doc.remove(startPosition, length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2293
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2294
                kit.read(in, doc, startPosition);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2295
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2296
                char[] buff = new char[1024];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2297
                int nch;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2298
                boolean lastWasCR = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2299
                int last;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2300
                StringBuffer sbuff = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2301
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2302
                // Read in a block at a time, mapping \r\n to \n, as well as single
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2303
                // \r to \n.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2304
                while ((nch = in.read(buff, 0, buff.length)) != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2305
                    if (sbuff == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2306
                        sbuff = new StringBuffer(nch);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2307
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2308
                    last = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2309
                    for(int counter = 0; counter < nch; counter++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2310
                        switch(buff[counter]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2311
                        case '\r':
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2312
                            if (lastWasCR) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2313
                                if (counter == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2314
                                    sbuff.append('\n');
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2315
                                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2316
                                    buff[counter - 1] = '\n';
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2317
                                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2318
                            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2319
                                lastWasCR = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2320
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2321
                            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2322
                        case '\n':
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2323
                            if (lastWasCR) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2324
                                if (counter > (last + 1)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2325
                                    sbuff.append(buff, last, counter - last - 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2326
                                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2327
                                // else nothing to do, can skip \r, next write will
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2328
                                // write \n
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2329
                                lastWasCR = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2330
                                last = counter;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2331
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2332
                            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2333
                        default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2334
                            if (lastWasCR) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2335
                                if (counter == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2336
                                    sbuff.append('\n');
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2337
                                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2338
                                    buff[counter - 1] = '\n';
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2339
                                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2340
                                lastWasCR = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2341
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2342
                            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2343
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2344
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2345
                    if (last < nch) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2346
                        if (lastWasCR) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2347
                            if (last < (nch - 1)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2348
                                sbuff.append(buff, last, nch - last - 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2349
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2350
                        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2351
                            sbuff.append(buff, last, nch - last);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2352
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2353
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2354
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2355
                if (lastWasCR) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2356
                    sbuff.append('\n');
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2357
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2358
                c.replaceSelection(sbuff != null ? sbuff.toString() : "");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2359
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2360
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2361
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2362
        // --- TransferHandler methods ------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2363
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2364
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2365
         * This is the type of transfer actions supported by the source.  Some models are
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2366
         * not mutable, so a transfer operation of COPY only should
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2367
         * be advertised in that case.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2368
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2369
         * @param c  The component holding the data to be transfered.  This
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2370
         *  argument is provided to enable sharing of TransferHandlers by
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2371
         *  multiple components.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2372
         * @return  This is implemented to return NONE if the component is a JPasswordField
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2373
         *  since exporting data via user gestures is not allowed.  If the text component is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2374
         *  editable, COPY_OR_MOVE is returned, otherwise just COPY is allowed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2375
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2376
        public int getSourceActions(JComponent c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2377
            if (c instanceof JPasswordField &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2378
                c.getClientProperty("JPasswordField.cutCopyAllowed") !=
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2379
                Boolean.TRUE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2380
                return NONE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2381
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2382
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2383
            return ((JTextComponent)c).isEditable() ? COPY_OR_MOVE : COPY;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2384
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2385
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2386
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2387
         * Create a Transferable to use as the source for a data transfer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2388
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2389
         * @param comp  The component holding the data to be transfered.  This
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2390
         *  argument is provided to enable sharing of TransferHandlers by
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2391
         *  multiple components.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2392
         * @return  The representation of the data to be transfered.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2393
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2394
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2395
        protected Transferable createTransferable(JComponent comp) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2396
            exportComp = (JTextComponent)comp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2397
            shouldRemove = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2398
            p0 = exportComp.getSelectionStart();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2399
            p1 = exportComp.getSelectionEnd();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2400
            return (p0 != p1) ? (new TextTransferable(exportComp, p0, p1)) : null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2401
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2402
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2403
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2404
         * This method is called after data has been exported.  This method should remove
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2405
         * the data that was transfered if the action was MOVE.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2406
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2407
         * @param source The component that was the source of the data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2408
         * @param data   The data that was transferred or possibly null
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2409
         *               if the action is <code>NONE</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2410
         * @param action The actual action that was performed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2411
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2412
        protected void exportDone(JComponent source, Transferable data, int action) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2413
            // only remove the text if shouldRemove has not been set to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2414
            // false by importData and only if the action is a move
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2415
            if (shouldRemove && action == MOVE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2416
                TextTransferable t = (TextTransferable)data;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2417
                t.removeText();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2418
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2419
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2420
            exportComp = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2421
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2422
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2423
        public boolean importData(TransferSupport support) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2424
            isDrop = support.isDrop();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2425
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2426
            if (isDrop) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2427
                modeBetween =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2428
                    ((JTextComponent)support.getComponent()).getDropMode() == DropMode.INSERT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2429
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2430
                dropBias = ((JTextComponent.DropLocation)support.getDropLocation()).getBias();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2431
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2432
                dropAction = support.getDropAction();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2433
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2434
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2435
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2436
                return super.importData(support);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2437
            } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2438
                isDrop = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2439
                modeBetween = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2440
                dropBias = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2441
                dropAction = MOVE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2442
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2443
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2444
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2445
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2446
         * This method causes a transfer to a component from a clipboard or a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2447
         * DND drop operation.  The Transferable represents the data to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2448
         * imported into the component.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2449
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2450
         * @param comp  The component to receive the transfer.  This
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2451
         *  argument is provided to enable sharing of TransferHandlers by
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2452
         *  multiple components.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2453
         * @param t     The data to import
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2454
         * @return  true if the data was inserted into the component, false otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2455
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2456
        public boolean importData(JComponent comp, Transferable t) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2457
            JTextComponent c = (JTextComponent)comp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2458
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2459
            int pos = modeBetween
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2460
                      ? ((JTextComponent.DropLocation)c.getDropLocation()).getIndex()
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2461
                      : c.getCaretPosition();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2462
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2463
            // if we are importing to the same component that we exported from
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2464
            // then don't actually do anything if the drop location is inside
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2465
            // the drag location and set shouldRemove to false so that exportDone
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2466
            // knows not to remove any data
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2467
            if (dropAction == MOVE && c == exportComp && pos >= p0 && pos <= p1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2468
                shouldRemove = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2469
                return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2470
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2471
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2472
            boolean imported = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2473
            DataFlavor importFlavor = getImportFlavor(t.getTransferDataFlavors(), c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2474
            if (importFlavor != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2475
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2476
                    boolean useRead = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2477
                    if (comp instanceof JEditorPane) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2478
                        JEditorPane ep = (JEditorPane)comp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2479
                        if (!ep.getContentType().startsWith("text/plain") &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2480
                                importFlavor.getMimeType().startsWith(ep.getContentType())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2481
                            useRead = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2482
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2483
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2484
                    InputContext ic = c.getInputContext();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2485
                    if (ic != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2486
                        ic.endComposition();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2487
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2488
                    Reader r = importFlavor.getReaderForText(t);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2489
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2490
                    if (modeBetween) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2491
                        Caret caret = c.getCaret();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2492
                        if (caret instanceof DefaultCaret) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2493
                            ((DefaultCaret)caret).setDot(pos, dropBias);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2494
                        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2495
                            c.setCaretPosition(pos);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2496
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2497
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2498
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2499
                    handleReaderImport(r, c, useRead);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2500
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2501
                    if (isDrop) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2502
                        c.requestFocus();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2503
                        Caret caret = c.getCaret();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2504
                        if (caret instanceof DefaultCaret) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2505
                            int newPos = caret.getDot();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2506
                            Position.Bias newBias = ((DefaultCaret)caret).getDotBias();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2507
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2508
                            ((DefaultCaret)caret).setDot(pos, dropBias);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2509
                            ((DefaultCaret)caret).moveDot(newPos, newBias);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2510
                        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2511
                            c.select(pos, c.getCaretPosition());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2512
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2513
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2514
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2515
                    imported = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2516
                } catch (UnsupportedFlavorException ufe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2517
                } catch (BadLocationException ble) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2518
                } catch (IOException ioe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2519
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2520
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2521
            return imported;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2522
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2523
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2524
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2525
         * This method indicates if a component would accept an import of the given
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2526
         * set of data flavors prior to actually attempting to import it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2527
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2528
         * @param comp  The component to receive the transfer.  This
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2529
         *  argument is provided to enable sharing of TransferHandlers by
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2530
         *  multiple components.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2531
         * @param flavors  The data formats available
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2532
         * @return  true if the data can be inserted into the component, false otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2533
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2534
        public boolean canImport(JComponent comp, DataFlavor[] flavors) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2535
            JTextComponent c = (JTextComponent)comp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2536
            if (!(c.isEditable() && c.isEnabled())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2537
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2538
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2539
            return (getImportFlavor(flavors, c) != null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2540
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2541
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2542
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2543
         * A possible implementation of the Transferable interface
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2544
         * for text components.  For a JEditorPane with a rich set
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2545
         * of EditorKit implementations, conversions could be made
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2546
         * giving a wider set of formats.  This is implemented to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2547
         * offer up only the active content type and text/plain
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2548
         * (if that is not the active format) since that can be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2549
         * extracted from other formats.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2550
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2551
        static class TextTransferable extends BasicTransferable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2552
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2553
            TextTransferable(JTextComponent c, int start, int end) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2554
                super(null, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2555
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2556
                this.c = c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2557
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2558
                Document doc = c.getDocument();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2559
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2560
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2561
                    p0 = doc.createPosition(start);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2562
                    p1 = doc.createPosition(end);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2563
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2564
                    plainData = c.getSelectedText();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2565
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2566
                    if (c instanceof JEditorPane) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2567
                        JEditorPane ep = (JEditorPane)c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2568
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2569
                        mimeType = ep.getContentType();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2570
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2571
                        if (mimeType.startsWith("text/plain")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2572
                            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2573
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2574
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2575
                        StringWriter sw = new StringWriter(p1.getOffset() - p0.getOffset());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2576
                        ep.getEditorKit().write(sw, doc, p0.getOffset(), p1.getOffset() - p0.getOffset());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2577
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2578
                        if (mimeType.startsWith("text/html")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2579
                            htmlData = sw.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2580
                        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2581
                            richText = sw.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2582
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2583
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2584
                } catch (BadLocationException ble) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2585
                } catch (IOException ioe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2586
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2587
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2588
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2589
            void removeText() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2590
                if ((p0 != null) && (p1 != null) && (p0.getOffset() != p1.getOffset())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2591
                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2592
                        Document doc = c.getDocument();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2593
                        doc.remove(p0.getOffset(), p1.getOffset() - p0.getOffset());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2594
                    } catch (BadLocationException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2595
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2596
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2597
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2598
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2599
            // ---- EditorKit other than plain or HTML text -----------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2600
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2601
            /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2602
             * If the EditorKit is not for text/plain or text/html, that format
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2603
             * is supported through the "richer flavors" part of BasicTransferable.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2604
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2605
            protected DataFlavor[] getRicherFlavors() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2606
                if (richText == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2607
                    return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2608
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2609
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2610
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2611
                    DataFlavor[] flavors = new DataFlavor[3];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2612
                    flavors[0] = new DataFlavor(mimeType + ";class=java.lang.String");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2613
                    flavors[1] = new DataFlavor(mimeType + ";class=java.io.Reader");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2614
                    flavors[2] = new DataFlavor(mimeType + ";class=java.io.InputStream;charset=unicode");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2615
                    return flavors;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2616
                } catch (ClassNotFoundException cle) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2617
                    // fall through to unsupported (should not happen)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2618
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2619
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2620
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2621
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2622
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2623
            /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2624
             * The only richer format supported is the file list flavor
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2625
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2626
            protected Object getRicherData(DataFlavor flavor) throws UnsupportedFlavorException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2627
                if (richText == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2628
                    return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2629
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2630
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2631
                if (String.class.equals(flavor.getRepresentationClass())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2632
                    return richText;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2633
                } else if (Reader.class.equals(flavor.getRepresentationClass())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2634
                    return new StringReader(richText);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2635
                } else if (InputStream.class.equals(flavor.getRepresentationClass())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2636
                    return new StringBufferInputStream(richText);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2637
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2638
                throw new UnsupportedFlavorException(flavor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2639
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2640
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2641
            Position p0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2642
            Position p1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2643
            String mimeType;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2644
            String richText;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2645
            JTextComponent c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2646
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2647
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2648
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2649
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2650
}