Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/src/java.desktop/macosx/classes/sun/lwawt/LWTextAreaPeer.java
41153 views
1
/*
2
* Copyright (c) 2011, 2019, Oracle and/or its affiliates. All rights reserved.
3
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4
*
5
* This code is free software; you can redistribute it and/or modify it
6
* under the terms of the GNU General Public License version 2 only, as
7
* published by the Free Software Foundation. Oracle designates this
8
* particular file as subject to the "Classpath" exception as provided
9
* by Oracle in the LICENSE file that accompanied this code.
10
*
11
* This code is distributed in the hope that it will be useful, but WITHOUT
12
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14
* version 2 for more details (a copy is included in the LICENSE file that
15
* accompanied this code).
16
*
17
* You should have received a copy of the GNU General Public License version
18
* 2 along with this work; if not, write to the Free Software Foundation,
19
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20
*
21
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22
* or visit www.oracle.com if you need additional information or have any
23
* questions.
24
*/
25
26
package sun.lwawt;
27
28
import java.awt.Component;
29
import java.awt.Cursor;
30
import java.awt.Dimension;
31
import java.awt.Insets;
32
import java.awt.Point;
33
import java.awt.TextArea;
34
import java.awt.event.TextEvent;
35
import java.awt.peer.TextAreaPeer;
36
37
import javax.swing.JScrollBar;
38
import javax.swing.JScrollPane;
39
import javax.swing.JTextArea;
40
import javax.swing.ScrollPaneConstants;
41
import javax.swing.TransferHandler;
42
import javax.swing.text.Document;
43
44
import sun.awt.AWTAccessor;
45
46
/**
47
* Lightweight implementation of {@link TextAreaPeer}. Delegates most of the
48
* work to the {@link JTextArea} inside {@link JScrollPane}.
49
*/
50
final class LWTextAreaPeer
51
extends LWTextComponentPeer<TextArea, LWTextAreaPeer.ScrollableJTextArea>
52
implements TextAreaPeer {
53
54
/**
55
* The default number of visible columns.
56
*/
57
private static final int DEFAULT_COLUMNS = 60;
58
59
/**
60
* The default number of visible rows.
61
*/
62
private static final int DEFAULT_ROWS = 10;
63
64
LWTextAreaPeer(final TextArea target,
65
final PlatformComponent platformComponent) {
66
super(target, platformComponent);
67
}
68
69
@Override
70
ScrollableJTextArea createDelegate() {
71
return new ScrollableJTextArea();
72
}
73
74
@Override
75
void initializeImpl() {
76
super.initializeImpl();
77
final int visibility = getTarget().getScrollbarVisibility();
78
synchronized (getDelegateLock()) {
79
getTextComponent().setWrapStyleWord(true);
80
setScrollBarVisibility(visibility);
81
}
82
}
83
84
@Override
85
JTextArea getTextComponent() {
86
return getDelegate().getView();
87
}
88
89
@Override
90
Cursor getCursor(final Point p) {
91
final boolean isContains;
92
synchronized (getDelegateLock()) {
93
isContains = getDelegate().getViewport().getBounds().contains(p);
94
}
95
return isContains ? super.getCursor(p) : null;
96
}
97
98
@Override
99
Component getDelegateFocusOwner() {
100
return getTextComponent();
101
}
102
103
@Override
104
public Dimension getPreferredSize() {
105
return getMinimumSize();
106
}
107
108
@Override
109
public Dimension getMinimumSize() {
110
return getMinimumSize(DEFAULT_ROWS, DEFAULT_COLUMNS);
111
}
112
113
@Override
114
public Dimension getPreferredSize(final int rows, final int columns) {
115
return getMinimumSize(rows, columns);
116
}
117
118
@Override
119
public Dimension getMinimumSize(final int rows, final int columns) {
120
final Dimension size = super.getMinimumSize(rows, columns);
121
synchronized (getDelegateLock()) {
122
// JScrollPane insets
123
final Insets pi = getDelegate().getInsets();
124
size.width += pi.left + pi.right;
125
size.height += pi.top + pi.bottom;
126
// Take scrollbars into account.
127
final int vsbPolicy = getDelegate().getVerticalScrollBarPolicy();
128
if (vsbPolicy == ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS) {
129
final JScrollBar vbar = getDelegate().getVerticalScrollBar();
130
size.width += vbar != null ? vbar.getMinimumSize().width : 0;
131
}
132
final int hsbPolicy = getDelegate().getHorizontalScrollBarPolicy();
133
if (hsbPolicy == ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS) {
134
final JScrollBar hbar = getDelegate().getHorizontalScrollBar();
135
size.height += hbar != null ? hbar.getMinimumSize().height : 0;
136
}
137
}
138
return size;
139
}
140
141
@Override
142
public void insert(final String text, final int pos) {
143
final ScrollableJTextArea pane = getDelegate();
144
synchronized (getDelegateLock()) {
145
final JTextArea area = pane.getView();
146
final boolean doScroll = pos >= area.getDocument().getLength()
147
&& area.getDocument().getLength() != 0;
148
area.insert(text, pos);
149
revalidate();
150
if (doScroll) {
151
final JScrollBar vbar = pane.getVerticalScrollBar();
152
if (vbar != null) {
153
vbar.setValue(vbar.getMaximum() - vbar.getVisibleAmount());
154
}
155
}
156
}
157
repaintPeer();
158
}
159
160
@Override
161
public void replaceRange(final String text, final int start,
162
final int end) {
163
synchronized (getDelegateLock()) {
164
// JTextArea.replaceRange() posts two different events.
165
// Since we make no differences between text events,
166
// the document listener has to be disabled while
167
// JTextArea.replaceRange() is called.
168
final Document document = getTextComponent().getDocument();
169
document.removeDocumentListener(this);
170
getTextComponent().replaceRange(text, start, end);
171
revalidate();
172
postEvent(new TextEvent(getTarget(), TextEvent.TEXT_VALUE_CHANGED));
173
document.addDocumentListener(this);
174
}
175
repaintPeer();
176
}
177
178
private void setScrollBarVisibility(final int visibility) {
179
final ScrollableJTextArea pane = getDelegate();
180
final JTextArea view = pane.getView();
181
view.setLineWrap(false);
182
183
switch (visibility) {
184
case TextArea.SCROLLBARS_NONE:
185
pane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
186
pane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_NEVER);
187
view.setLineWrap(true);
188
break;
189
case TextArea.SCROLLBARS_VERTICAL_ONLY:
190
pane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
191
pane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
192
view.setLineWrap(true);
193
break;
194
case TextArea.SCROLLBARS_HORIZONTAL_ONLY:
195
pane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_NEVER);
196
pane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
197
break;
198
default:
199
pane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
200
pane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
201
break;
202
}
203
}
204
205
@SuppressWarnings("serial")// Safe: outer class is non-serializable.
206
final class ScrollableJTextArea extends JScrollPane {
207
208
ScrollableJTextArea() {
209
super();
210
getViewport().setView(new JTextAreaDelegate());
211
}
212
213
public JTextArea getView() {
214
return (JTextArea) getViewport().getView();
215
}
216
217
@Override
218
public void setEnabled(final boolean enabled) {
219
getViewport().getView().setEnabled(enabled);
220
super.setEnabled(enabled);
221
}
222
223
private final class JTextAreaDelegate extends JTextArea {
224
225
@Override
226
public void replaceSelection(String content) {
227
getDocument().removeDocumentListener(LWTextAreaPeer.this);
228
super.replaceSelection(content);
229
// post only one text event in this case
230
postTextEvent();
231
getDocument().addDocumentListener(LWTextAreaPeer.this);
232
}
233
234
@Override
235
public boolean hasFocus() {
236
return getTarget().hasFocus();
237
}
238
239
@Override
240
public Point getLocationOnScreen() {
241
return LWTextAreaPeer.this.getLocationOnScreen();
242
}
243
244
@Override
245
public void setTransferHandler(final TransferHandler newHandler) {
246
// override the default implementation to avoid loading
247
// SystemFlavorMap and associated classes
248
Object key = AWTAccessor.getClientPropertyKeyAccessor()
249
.getJComponent_TRANSFER_HANDLER();
250
Object oldHandler = getClientProperty(key);
251
putClientProperty(key, newHandler);
252
firePropertyChange("transferHandler", oldHandler, newHandler);
253
}
254
}
255
}
256
}
257
258