jdk/src/macosx/classes/sun/lwawt/LWListPeer.java
author mcherkas
Mon, 28 Jul 2014 16:09:26 +0400
changeset 26008 ccab11700f07
parent 20153 d5bf90bfcb6d
permissions -rw-r--r--
8047288: Fixes endless loop on mac caused by invoking Windows.isFocusable() on Appkit thread. Reviewed-by: ant, pchelko Contributed-by: artem.malinko@oracle.com
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
     1
/*
20153
d5bf90bfcb6d 8005255: [macosx] Cleanup warnings in sun.lwawt
serb
parents: 13993
diff changeset
     2
 * Copyright (c) 2011, 2013, Oracle and/or its affiliates. All rights reserved.
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
     4
 *
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    10
 *
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    15
 * accompanied this code).
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    16
 *
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    20
 *
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    23
 * questions.
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    24
 */
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    25
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    26
package sun.lwawt;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    27
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    28
import javax.swing.*;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    29
import javax.swing.event.*;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    30
import java.awt.*;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    31
import java.awt.event.*;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    32
import java.awt.peer.ListPeer;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    33
import java.util.Arrays;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    34
13993
8b3fe3d8badb 7160627: [macosx] TextArea has wrong initial size
serb
parents: 13143
diff changeset
    35
/**
20153
d5bf90bfcb6d 8005255: [macosx] Cleanup warnings in sun.lwawt
serb
parents: 13993
diff changeset
    36
 * Lightweight implementation of {@link ListPeer}. Delegates most of the work to
d5bf90bfcb6d 8005255: [macosx] Cleanup warnings in sun.lwawt
serb
parents: 13993
diff changeset
    37
 * the {@link JList}, which is placed inside {@link JScrollPane}.
13993
8b3fe3d8badb 7160627: [macosx] TextArea has wrong initial size
serb
parents: 13143
diff changeset
    38
 */
8b3fe3d8badb 7160627: [macosx] TextArea has wrong initial size
serb
parents: 13143
diff changeset
    39
final class LWListPeer extends LWComponentPeer<List, LWListPeer.ScrollableJList>
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    40
        implements ListPeer {
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    41
13993
8b3fe3d8badb 7160627: [macosx] TextArea has wrong initial size
serb
parents: 13143
diff changeset
    42
    /**
8b3fe3d8badb 7160627: [macosx] TextArea has wrong initial size
serb
parents: 13143
diff changeset
    43
     * The default number of visible rows.
8b3fe3d8badb 7160627: [macosx] TextArea has wrong initial size
serb
parents: 13143
diff changeset
    44
     */
8b3fe3d8badb 7160627: [macosx] TextArea has wrong initial size
serb
parents: 13143
diff changeset
    45
    private static final int DEFAULT_VISIBLE_ROWS = 4; // From java.awt.List,
8b3fe3d8badb 7160627: [macosx] TextArea has wrong initial size
serb
parents: 13143
diff changeset
    46
8b3fe3d8badb 7160627: [macosx] TextArea has wrong initial size
serb
parents: 13143
diff changeset
    47
    /**
8b3fe3d8badb 7160627: [macosx] TextArea has wrong initial size
serb
parents: 13143
diff changeset
    48
     * This text is used for cell bounds calculation.
8b3fe3d8badb 7160627: [macosx] TextArea has wrong initial size
serb
parents: 13143
diff changeset
    49
     */
8b3fe3d8badb 7160627: [macosx] TextArea has wrong initial size
serb
parents: 13143
diff changeset
    50
    private static final String TEXT = "0123456789abcde";
8b3fe3d8badb 7160627: [macosx] TextArea has wrong initial size
serb
parents: 13143
diff changeset
    51
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    52
    LWListPeer(final List target, final PlatformComponent platformComponent) {
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    53
        super(target, platformComponent);
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    54
        if (!getTarget().isBackgroundSet()) {
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    55
            getTarget().setBackground(SystemColor.text);
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    56
        }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    57
    }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    58
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    59
    @Override
20153
d5bf90bfcb6d 8005255: [macosx] Cleanup warnings in sun.lwawt
serb
parents: 13993
diff changeset
    60
    ScrollableJList createDelegate() {
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    61
        return new ScrollableJList();
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    62
    }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    63
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    64
    @Override
13143
31c70a66a053 7142091: [macosx] RFE: Refactoring of peer initialization/disposing
serb
parents: 12835
diff changeset
    65
    void initializeImpl() {
31c70a66a053 7142091: [macosx] RFE: Refactoring of peer initialization/disposing
serb
parents: 12835
diff changeset
    66
        super.initializeImpl();
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    67
        setMultipleMode(getTarget().isMultipleMode());
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    68
        final int[] selectedIndices = getTarget().getSelectedIndexes();
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    69
        synchronized (getDelegateLock()) {
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    70
            getDelegate().setSkipStateChangedEvent(true);
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    71
            getDelegate().getView().setSelectedIndices(selectedIndices);
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    72
            getDelegate().setSkipStateChangedEvent(false);
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    73
        }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    74
    }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    75
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    76
    @Override
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    77
    public boolean isFocusable() {
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    78
        return true;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    79
    }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    80
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    81
    @Override
20153
d5bf90bfcb6d 8005255: [macosx] Cleanup warnings in sun.lwawt
serb
parents: 13993
diff changeset
    82
    Component getDelegateFocusOwner() {
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    83
        return getDelegate().getView();
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    84
    }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    85
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    86
    @Override
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    87
    public int[] getSelectedIndexes() {
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    88
        synchronized (getDelegateLock()) {
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    89
            return getDelegate().getView().getSelectedIndices();
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    90
        }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    91
    }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    92
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    93
    @Override
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    94
    public void add(final String item, final int index) {
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    95
        synchronized (getDelegateLock()) {
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    96
            getDelegate().getModel().add(index, item);
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    97
            revalidate();
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    98
        }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    99
    }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   100
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   101
    @Override
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   102
    public void delItems(final int start, final int end) {
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   103
        synchronized (getDelegateLock()) {
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   104
            getDelegate().getModel().removeRange(start, end);
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   105
            revalidate();
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   106
        }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   107
    }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   108
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   109
    @Override
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   110
    public void removeAll() {
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   111
        synchronized (getDelegateLock()) {
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   112
            getDelegate().getModel().removeAllElements();
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   113
            revalidate();
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   114
        }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   115
    }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   116
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   117
    @Override
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   118
    public void select(final int index) {
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   119
        synchronized (getDelegateLock()) {
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   120
            getDelegate().setSkipStateChangedEvent(true);
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   121
            getDelegate().getView().setSelectedIndex(index);
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   122
            getDelegate().setSkipStateChangedEvent(false);
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   123
        }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   124
    }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   125
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   126
    @Override
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   127
    public void deselect(final int index) {
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   128
        synchronized (getDelegateLock()) {
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   129
            getDelegate().getView().getSelectionModel().
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   130
                    removeSelectionInterval(index, index);
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   131
        }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   132
    }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   133
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   134
    @Override
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   135
    public void makeVisible(final int index) {
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   136
        synchronized (getDelegateLock()) {
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   137
            getDelegate().getView().ensureIndexIsVisible(index);
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   138
        }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   139
    }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   140
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   141
    @Override
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   142
    public void setMultipleMode(final boolean m) {
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   143
        synchronized (getDelegateLock()) {
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   144
            getDelegate().getView().setSelectionMode(m ?
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   145
                    ListSelectionModel.MULTIPLE_INTERVAL_SELECTION
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   146
                    : ListSelectionModel.SINGLE_SELECTION);
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   147
        }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   148
    }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   149
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   150
    @Override
13993
8b3fe3d8badb 7160627: [macosx] TextArea has wrong initial size
serb
parents: 13143
diff changeset
   151
    public Dimension getPreferredSize() {
8b3fe3d8badb 7160627: [macosx] TextArea has wrong initial size
serb
parents: 13143
diff changeset
   152
        return getMinimumSize();
8b3fe3d8badb 7160627: [macosx] TextArea has wrong initial size
serb
parents: 13143
diff changeset
   153
    }
8b3fe3d8badb 7160627: [macosx] TextArea has wrong initial size
serb
parents: 13143
diff changeset
   154
8b3fe3d8badb 7160627: [macosx] TextArea has wrong initial size
serb
parents: 13143
diff changeset
   155
    @Override
8b3fe3d8badb 7160627: [macosx] TextArea has wrong initial size
serb
parents: 13143
diff changeset
   156
    public Dimension getMinimumSize() {
8b3fe3d8badb 7160627: [macosx] TextArea has wrong initial size
serb
parents: 13143
diff changeset
   157
        return getMinimumSize(DEFAULT_VISIBLE_ROWS);
8b3fe3d8badb 7160627: [macosx] TextArea has wrong initial size
serb
parents: 13143
diff changeset
   158
    }
8b3fe3d8badb 7160627: [macosx] TextArea has wrong initial size
serb
parents: 13143
diff changeset
   159
8b3fe3d8badb 7160627: [macosx] TextArea has wrong initial size
serb
parents: 13143
diff changeset
   160
    @Override
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   161
    public Dimension getPreferredSize(final int rows) {
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   162
        return getMinimumSize(rows);
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   163
    }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   164
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   165
    @Override
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   166
    public Dimension getMinimumSize(final int rows) {
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   167
        synchronized (getDelegateLock()) {
13993
8b3fe3d8badb 7160627: [macosx] TextArea has wrong initial size
serb
parents: 13143
diff changeset
   168
            final Dimension size = getCellSize();
8b3fe3d8badb 7160627: [macosx] TextArea has wrong initial size
serb
parents: 13143
diff changeset
   169
            size.height *= rows;
8b3fe3d8badb 7160627: [macosx] TextArea has wrong initial size
serb
parents: 13143
diff changeset
   170
            // Always take vertical scrollbar into account.
8b3fe3d8badb 7160627: [macosx] TextArea has wrong initial size
serb
parents: 13143
diff changeset
   171
            final JScrollBar vbar = getDelegate().getVerticalScrollBar();
8b3fe3d8badb 7160627: [macosx] TextArea has wrong initial size
serb
parents: 13143
diff changeset
   172
            size.width += vbar != null ? vbar.getMinimumSize().width : 0;
8b3fe3d8badb 7160627: [macosx] TextArea has wrong initial size
serb
parents: 13143
diff changeset
   173
            // JScrollPane and JList insets
8b3fe3d8badb 7160627: [macosx] TextArea has wrong initial size
serb
parents: 13143
diff changeset
   174
            final Insets pi = getDelegate().getInsets();
8b3fe3d8badb 7160627: [macosx] TextArea has wrong initial size
serb
parents: 13143
diff changeset
   175
            final Insets vi = getDelegate().getView().getInsets();
8b3fe3d8badb 7160627: [macosx] TextArea has wrong initial size
serb
parents: 13143
diff changeset
   176
            size.width += pi.left + pi.right + vi.left + vi.right;
8b3fe3d8badb 7160627: [macosx] TextArea has wrong initial size
serb
parents: 13143
diff changeset
   177
            size.height += pi.top + pi.bottom + vi.top + vi.bottom;
8b3fe3d8badb 7160627: [macosx] TextArea has wrong initial size
serb
parents: 13143
diff changeset
   178
            return size;
8b3fe3d8badb 7160627: [macosx] TextArea has wrong initial size
serb
parents: 13143
diff changeset
   179
        }
8b3fe3d8badb 7160627: [macosx] TextArea has wrong initial size
serb
parents: 13143
diff changeset
   180
    }
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   181
13993
8b3fe3d8badb 7160627: [macosx] TextArea has wrong initial size
serb
parents: 13143
diff changeset
   182
    private Dimension getCellSize() {
8b3fe3d8badb 7160627: [macosx] TextArea has wrong initial size
serb
parents: 13143
diff changeset
   183
        final JList<String> jList = getDelegate().getView();
8b3fe3d8badb 7160627: [macosx] TextArea has wrong initial size
serb
parents: 13143
diff changeset
   184
        final ListCellRenderer<? super String> cr = jList.getCellRenderer();
8b3fe3d8badb 7160627: [macosx] TextArea has wrong initial size
serb
parents: 13143
diff changeset
   185
        final Component cell = cr.getListCellRendererComponent(jList, TEXT, 0,
8b3fe3d8badb 7160627: [macosx] TextArea has wrong initial size
serb
parents: 13143
diff changeset
   186
                                                               false, false);
8b3fe3d8badb 7160627: [macosx] TextArea has wrong initial size
serb
parents: 13143
diff changeset
   187
        return cell.getPreferredSize();
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   188
    }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   189
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   190
    private void revalidate() {
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   191
        synchronized (getDelegateLock()) {
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   192
            getDelegate().getView().invalidate();
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   193
            getDelegate().validate();
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   194
        }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   195
    }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   196
20153
d5bf90bfcb6d 8005255: [macosx] Cleanup warnings in sun.lwawt
serb
parents: 13993
diff changeset
   197
    @SuppressWarnings("serial")// Safe: outer class is non-serializable.
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   198
    final class ScrollableJList extends JScrollPane implements ListSelectionListener {
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   199
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   200
        private boolean skipStateChangedEvent;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   201
13993
8b3fe3d8badb 7160627: [macosx] TextArea has wrong initial size
serb
parents: 13143
diff changeset
   202
        private final DefaultListModel<String> model =
8b3fe3d8badb 7160627: [macosx] TextArea has wrong initial size
serb
parents: 13143
diff changeset
   203
                new DefaultListModel<String>() {
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   204
                    @Override
13993
8b3fe3d8badb 7160627: [macosx] TextArea has wrong initial size
serb
parents: 13143
diff changeset
   205
                    public void add(final int index, final String element) {
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   206
                        if (index == -1) {
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   207
                            addElement(element);
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   208
                        } else {
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   209
                            super.add(index, element);
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   210
                        }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   211
                    }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   212
                };
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   213
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   214
        private int[] oldSelectedIndices = new int[0];
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   215
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   216
        ScrollableJList() {
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   217
            getViewport().setScrollMode(JViewport.SIMPLE_SCROLL_MODE);
13993
8b3fe3d8badb 7160627: [macosx] TextArea has wrong initial size
serb
parents: 13143
diff changeset
   218
            final JList<String> list = new JListDelegate();
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   219
            list.addListSelectionListener(this);
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   220
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   221
            getViewport().setView(list);
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   222
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   223
            // Pull the items from the target.
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   224
            final String[] items = getTarget().getItems();
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   225
            for (int i = 0; i < items.length; i++) {
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   226
                model.add(i, items[i]);
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   227
            }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   228
        }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   229
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   230
        public boolean isSkipStateChangedEvent() {
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   231
            return skipStateChangedEvent;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   232
        }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   233
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   234
        public void setSkipStateChangedEvent(boolean skipStateChangedEvent) {
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   235
            this.skipStateChangedEvent = skipStateChangedEvent;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   236
        }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   237
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   238
        @Override
20153
d5bf90bfcb6d 8005255: [macosx] Cleanup warnings in sun.lwawt
serb
parents: 13993
diff changeset
   239
        @SuppressWarnings("unchecked")
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   240
        public void valueChanged(final ListSelectionEvent e) {
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   241
            if (!e.getValueIsAdjusting() && !isSkipStateChangedEvent()) {
20153
d5bf90bfcb6d 8005255: [macosx] Cleanup warnings in sun.lwawt
serb
parents: 13993
diff changeset
   242
                final JList<?> source = (JList<?>) e.getSource();
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   243
                for(int i = 0 ; i < source.getModel().getSize(); i++) {
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   244
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   245
                    final boolean wasSelected = Arrays.binarySearch(oldSelectedIndices, i) >= 0;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   246
                    final boolean isSelected = source.isSelectedIndex(i);
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   247
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   248
                    if (wasSelected == isSelected) {
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   249
                        continue;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   250
                    }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   251
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   252
                    final int state = !wasSelected && isSelected ? ItemEvent.SELECTED: ItemEvent.DESELECTED;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   253
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   254
                    LWListPeer.this.postEvent(new ItemEvent(getTarget(), ItemEvent.ITEM_STATE_CHANGED,
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   255
                            i, state));
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   256
                }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   257
                oldSelectedIndices = source.getSelectedIndices();
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   258
            }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   259
        }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   260
20153
d5bf90bfcb6d 8005255: [macosx] Cleanup warnings in sun.lwawt
serb
parents: 13993
diff changeset
   261
        @SuppressWarnings("unchecked")
13993
8b3fe3d8badb 7160627: [macosx] TextArea has wrong initial size
serb
parents: 13143
diff changeset
   262
        public JList<String> getView() {
8b3fe3d8badb 7160627: [macosx] TextArea has wrong initial size
serb
parents: 13143
diff changeset
   263
            return (JList<String>) getViewport().getView();
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   264
        }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   265
13993
8b3fe3d8badb 7160627: [macosx] TextArea has wrong initial size
serb
parents: 13143
diff changeset
   266
        public DefaultListModel<String> getModel() {
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   267
            return model;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   268
        }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   269
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   270
        @Override
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   271
        public void setEnabled(final boolean enabled) {
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   272
            getView().setEnabled(enabled);
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   273
            super.setEnabled(enabled);
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   274
        }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   275
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   276
        @Override
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   277
        public void setOpaque(final boolean isOpaque) {
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   278
            super.setOpaque(isOpaque);
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   279
            if (getView() != null) {
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   280
                getView().setOpaque(isOpaque);
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   281
            }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   282
        }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   283
12835
e547680fe05b 7152952: [macosx] List rows overlap with enlarged font
alexsch
parents: 12047
diff changeset
   284
        @Override
e547680fe05b 7152952: [macosx] List rows overlap with enlarged font
alexsch
parents: 12047
diff changeset
   285
        public void setFont(Font font) {
e547680fe05b 7152952: [macosx] List rows overlap with enlarged font
alexsch
parents: 12047
diff changeset
   286
            super.setFont(font);
e547680fe05b 7152952: [macosx] List rows overlap with enlarged font
alexsch
parents: 12047
diff changeset
   287
            if (getView() != null) {
e547680fe05b 7152952: [macosx] List rows overlap with enlarged font
alexsch
parents: 12047
diff changeset
   288
                getView().setFont(font);
e547680fe05b 7152952: [macosx] List rows overlap with enlarged font
alexsch
parents: 12047
diff changeset
   289
                LWListPeer.this.revalidate();
e547680fe05b 7152952: [macosx] List rows overlap with enlarged font
alexsch
parents: 12047
diff changeset
   290
            }
e547680fe05b 7152952: [macosx] List rows overlap with enlarged font
alexsch
parents: 12047
diff changeset
   291
        }
e547680fe05b 7152952: [macosx] List rows overlap with enlarged font
alexsch
parents: 12047
diff changeset
   292
13993
8b3fe3d8badb 7160627: [macosx] TextArea has wrong initial size
serb
parents: 13143
diff changeset
   293
        private final class JListDelegate extends JList<String> {
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   294
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   295
            JListDelegate() {
20153
d5bf90bfcb6d 8005255: [macosx] Cleanup warnings in sun.lwawt
serb
parents: 13993
diff changeset
   296
                super(model);
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   297
            }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   298
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   299
            @Override
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   300
            public boolean hasFocus() {
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   301
                return getTarget().hasFocus();
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   302
            }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   303
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   304
            @Override
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   305
            protected void processMouseEvent(final MouseEvent e) {
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   306
                super.processMouseEvent(e);
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   307
                if (e.getID() == MouseEvent.MOUSE_CLICKED && e.getClickCount() == 2) {
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   308
                    final int index = locationToIndex(e.getPoint());
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   309
                    if (0 <= index && index < getModel().getSize()) {
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   310
                        LWListPeer.this.postEvent(new ActionEvent(getTarget(), ActionEvent.ACTION_PERFORMED,
13993
8b3fe3d8badb 7160627: [macosx] TextArea has wrong initial size
serb
parents: 13143
diff changeset
   311
                            getModel().getElementAt(index), e.getWhen(), e.getModifiers()));
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   312
                    }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   313
                }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   314
            }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   315
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   316
            @Override
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   317
            protected void processKeyEvent(final KeyEvent e) {
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   318
                super.processKeyEvent(e);
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   319
                if (e.getID() == KeyEvent.KEY_PRESSED && e.getKeyCode() == KeyEvent.VK_ENTER) {
13993
8b3fe3d8badb 7160627: [macosx] TextArea has wrong initial size
serb
parents: 13143
diff changeset
   320
                    final String selectedValue = getSelectedValue();
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   321
                    if(selectedValue != null){
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   322
                        LWListPeer.this.postEvent(new ActionEvent(getTarget(), ActionEvent.ACTION_PERFORMED,
13993
8b3fe3d8badb 7160627: [macosx] TextArea has wrong initial size
serb
parents: 13143
diff changeset
   323
                            selectedValue, e.getWhen(), e.getModifiers()));
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   324
                    }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   325
                }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   326
            }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   327
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   328
            //Needed for Autoscroller.
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   329
            @Override
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   330
            public Point getLocationOnScreen() {
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   331
                return LWListPeer.this.getLocationOnScreen();
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   332
            }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   333
        }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   334
    }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   335
}