1 /*
2 * Copyright (c) 2013, 2016, 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
37 import java.awt.event.ContainerListener;
38 import java.awt.geom.AffineTransform;
39 import java.awt.image.BufferedImage;
40 import java.awt.image.DataBufferInt;
41 import java.beans.PropertyChangeEvent;
42 import java.beans.PropertyChangeListener;
43 import java.security.AccessController;
44 import javax.swing.JComponent;
45
46 import javax.swing.JLayeredPane;
47 import javax.swing.JPanel;
48 import javax.swing.JRootPane;
49 import javax.swing.LayoutFocusTraversalPolicy;
50 import javax.swing.RepaintManager;
51 import javax.swing.RootPaneContainer;
52 import javax.swing.SwingUtilities;
53
54 import sun.awt.AWTAccessor;
55 import sun.awt.DisplayChangedListener;
56 import sun.awt.LightweightFrame;
57 import sun.security.action.GetPropertyAction;
58 import sun.swing.SwingUtilities2.RepaintListener;
59
60 /**
61 * The frame serves as a lightweight container which paints its content
62 * to an offscreen image and provides access to the image's data via the
63 * {@link LightweightContent} interface. Note, that it may not be shown
64 * as a standalone toplevel frame. Its purpose is to provide functionality
65 * for lightweight embedding.
66 *
67 * @author Artem Ananiev
68 * @author Anton Tarasov
69 */
70 @SuppressWarnings("serial") // JDK-implementation class
71 public final class JLightweightFrame extends LightweightFrame implements RootPaneContainer {
72
73 private final JRootPane rootPane = new JRootPane();
74
75 private LightweightContent content;
76
87 * A duplicating (copy) buffer is created for the original pixel buffer.
88 * The copy buffer is synchronized with the original buffer every time the
89 * latter changes. {@code JLightweightFrame} passes the copy buffer array
90 * to the {@link LightweightContent#imageBufferReset} method. The code spot
91 * which synchronizes two buffers becomes the only critical section guarded
92 * by the lock (managed with the {@link LightweightContent#paintLock()},
93 * {@link LightweightContent#paintUnlock()} methods).
94 */
95 private static boolean copyBufferEnabled;
96 private int[] copyBuffer;
97
98 private PropertyChangeListener layoutSizeListener;
99 private RepaintListener repaintListener;
100
101 static {
102 SwingAccessor.setJLightweightFrameAccessor(new SwingAccessor.JLightweightFrameAccessor() {
103 @Override
104 public void updateCursor(JLightweightFrame frame) {
105 frame.updateClientCursor();
106 }
107 });
108 copyBufferEnabled = "true".equals(AccessController.
109 doPrivileged(new GetPropertyAction("swing.jlf.copyBufferEnabled", "true")));
110 }
111
112 /**
113 * Constructs a new, initially invisible {@code JLightweightFrame}
114 * instance.
115 */
116 public JLightweightFrame() {
117 super();
118 AffineTransform defaultTransform =
119 getGraphicsConfiguration().getDefaultTransform();
120 scaleFactorX = defaultTransform.getScaleX();
121 scaleFactorY = defaultTransform.getScaleY();
122 copyBufferEnabled = "true".equals(AccessController.
123 doPrivileged(new GetPropertyAction("swing.jlf.copyBufferEnabled", "true")));
124
125 add(rootPane, BorderLayout.CENTER);
126 setFocusTraversalPolicy(new LayoutFocusTraversalPolicy());
509 * and could not be overridden.
510 */
511 private void updateClientCursor() {
512 PointerInfo pointerInfo = MouseInfo.getPointerInfo();
513 if (pointerInfo == null) {
514 /*
515 * This can happen when multiple graphics device cannot decide
516 * which graphics device contains the current mouse position
517 * or on systems without a mouse
518 */
519 return;
520 }
521 Point p = pointerInfo.getLocation();
522 SwingUtilities.convertPointFromScreen(p, this);
523 Component target = SwingUtilities.getDeepestComponentAt(this, p.x, p.y);
524 if (target != null) {
525 content.setCursor(target.getCursor());
526 }
527 }
528
529 public <T extends DragGestureRecognizer> T createDragGestureRecognizer(
530 Class<T> abstractRecognizerClass,
531 DragSource ds, Component c, int srcActions,
532 DragGestureListener dgl)
533 {
534 return content == null ? null : content.createDragGestureRecognizer(
535 abstractRecognizerClass, ds, c, srcActions, dgl);
536 }
537
538 public DragSourceContextPeer createDragSourceContextPeer(DragGestureEvent dge) throws InvalidDnDOperationException {
539 return content == null ? null : content.createDragSourceContextPeer(dge);
540 }
541
542 public void addDropTarget(DropTarget dt) {
543 if (content == null) return;
544 content.addDropTarget(dt);
545 }
546
547 public void removeDropTarget(DropTarget dt) {
548 if (content == null) return;
|
1 /*
2 * Copyright (c) 2013, 2017, 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
37 import java.awt.event.ContainerListener;
38 import java.awt.geom.AffineTransform;
39 import java.awt.image.BufferedImage;
40 import java.awt.image.DataBufferInt;
41 import java.beans.PropertyChangeEvent;
42 import java.beans.PropertyChangeListener;
43 import java.security.AccessController;
44 import javax.swing.JComponent;
45
46 import javax.swing.JLayeredPane;
47 import javax.swing.JPanel;
48 import javax.swing.JRootPane;
49 import javax.swing.LayoutFocusTraversalPolicy;
50 import javax.swing.RepaintManager;
51 import javax.swing.RootPaneContainer;
52 import javax.swing.SwingUtilities;
53
54 import sun.awt.AWTAccessor;
55 import sun.awt.DisplayChangedListener;
56 import sun.awt.LightweightFrame;
57 import sun.awt.OverrideNativeWindowHandle;
58 import sun.security.action.GetPropertyAction;
59 import sun.swing.SwingUtilities2.RepaintListener;
60
61 /**
62 * The frame serves as a lightweight container which paints its content
63 * to an offscreen image and provides access to the image's data via the
64 * {@link LightweightContent} interface. Note, that it may not be shown
65 * as a standalone toplevel frame. Its purpose is to provide functionality
66 * for lightweight embedding.
67 *
68 * @author Artem Ananiev
69 * @author Anton Tarasov
70 */
71 @SuppressWarnings("serial") // JDK-implementation class
72 public final class JLightweightFrame extends LightweightFrame implements RootPaneContainer {
73
74 private final JRootPane rootPane = new JRootPane();
75
76 private LightweightContent content;
77
88 * A duplicating (copy) buffer is created for the original pixel buffer.
89 * The copy buffer is synchronized with the original buffer every time the
90 * latter changes. {@code JLightweightFrame} passes the copy buffer array
91 * to the {@link LightweightContent#imageBufferReset} method. The code spot
92 * which synchronizes two buffers becomes the only critical section guarded
93 * by the lock (managed with the {@link LightweightContent#paintLock()},
94 * {@link LightweightContent#paintUnlock()} methods).
95 */
96 private static boolean copyBufferEnabled;
97 private int[] copyBuffer;
98
99 private PropertyChangeListener layoutSizeListener;
100 private RepaintListener repaintListener;
101
102 static {
103 SwingAccessor.setJLightweightFrameAccessor(new SwingAccessor.JLightweightFrameAccessor() {
104 @Override
105 public void updateCursor(JLightweightFrame frame) {
106 frame.updateClientCursor();
107 }
108
109 @Override
110 public void overrideNativeWindowHandle(JLightweightFrame frame, long ptr) {
111 if (frame != null) {
112 frame.overrideNativeWindowHandle(ptr);
113 }
114 }
115 });
116 copyBufferEnabled = "true".equals(AccessController.
117 doPrivileged(new GetPropertyAction("swing.jlf.copyBufferEnabled", "true")));
118 }
119
120 /**
121 * Constructs a new, initially invisible {@code JLightweightFrame}
122 * instance.
123 */
124 public JLightweightFrame() {
125 super();
126 AffineTransform defaultTransform =
127 getGraphicsConfiguration().getDefaultTransform();
128 scaleFactorX = defaultTransform.getScaleX();
129 scaleFactorY = defaultTransform.getScaleY();
130 copyBufferEnabled = "true".equals(AccessController.
131 doPrivileged(new GetPropertyAction("swing.jlf.copyBufferEnabled", "true")));
132
133 add(rootPane, BorderLayout.CENTER);
134 setFocusTraversalPolicy(new LayoutFocusTraversalPolicy());
517 * and could not be overridden.
518 */
519 private void updateClientCursor() {
520 PointerInfo pointerInfo = MouseInfo.getPointerInfo();
521 if (pointerInfo == null) {
522 /*
523 * This can happen when multiple graphics device cannot decide
524 * which graphics device contains the current mouse position
525 * or on systems without a mouse
526 */
527 return;
528 }
529 Point p = pointerInfo.getLocation();
530 SwingUtilities.convertPointFromScreen(p, this);
531 Component target = SwingUtilities.getDeepestComponentAt(this, p.x, p.y);
532 if (target != null) {
533 content.setCursor(target.getCursor());
534 }
535 }
536
537 private void overrideNativeWindowHandle(long handle) {
538 final Object peer = AWTAccessor.getComponentAccessor().getPeer(this);
539 if (peer instanceof OverrideNativeWindowHandle) {
540 ((OverrideNativeWindowHandle) peer).overrideWindowHandle(handle);
541 }
542 }
543
544 public <T extends DragGestureRecognizer> T createDragGestureRecognizer(
545 Class<T> abstractRecognizerClass,
546 DragSource ds, Component c, int srcActions,
547 DragGestureListener dgl)
548 {
549 return content == null ? null : content.createDragGestureRecognizer(
550 abstractRecognizerClass, ds, c, srcActions, dgl);
551 }
552
553 public DragSourceContextPeer createDragSourceContextPeer(DragGestureEvent dge) throws InvalidDnDOperationException {
554 return content == null ? null : content.createDragSourceContextPeer(dge);
555 }
556
557 public void addDropTarget(DropTarget dt) {
558 if (content == null) return;
559 content.addDropTarget(dt);
560 }
561
562 public void removeDropTarget(DropTarget dt) {
563 if (content == null) return;
|