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