LWWindowPeer.java revision 5642
4632N/A/*
4767N/A * Copyright (c) 2011, 2012, Oracle and/or its affiliates. All rights reserved.
4632N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4632N/A *
4632N/A * This code is free software; you can redistribute it and/or modify it
4632N/A * under the terms of the GNU General Public License version 2 only, as
4632N/A * published by the Free Software Foundation. Oracle designates this
4632N/A * particular file as subject to the "Classpath" exception as provided
4632N/A * by Oracle in the LICENSE file that accompanied this code.
4632N/A *
4632N/A * This code is distributed in the hope that it will be useful, but WITHOUT
4632N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
4632N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
4632N/A * version 2 for more details (a copy is included in the LICENSE file that
4632N/A * accompanied this code).
4632N/A *
4632N/A * You should have received a copy of the GNU General Public License version
4632N/A * 2 along with this work; if not, write to the Free Software Foundation,
4632N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
4632N/A *
4632N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
4632N/A * or visit www.oracle.com if you need additional information or have any
4632N/A * questions.
4632N/A */
4632N/A
4632N/Apackage sun.lwawt;
4632N/A
4632N/Aimport java.awt.*;
4632N/Aimport java.awt.event.*;
4632N/Aimport java.awt.image.BufferedImage;
4632N/Aimport java.awt.peer.*;
4632N/Aimport java.util.List;
4632N/A
4632N/Aimport javax.swing.*;
4632N/A
4632N/Aimport sun.awt.*;
4632N/Aimport sun.java2d.*;
4639N/Aimport sun.java2d.loops.Blit;
4639N/Aimport sun.java2d.loops.CompositeType;
4717N/Aimport sun.java2d.pipe.Region;
4632N/Aimport sun.util.logging.PlatformLogger;
4632N/A
4632N/Apublic class LWWindowPeer
4632N/A extends LWContainerPeer<Window, JComponent>
4632N/A implements WindowPeer, FramePeer, DialogPeer, FullScreenCapable
4632N/A{
4632N/A public static enum PeerType {
4632N/A SIMPLEWINDOW,
4632N/A FRAME,
4632N/A DIALOG,
4632N/A EMBEDDED_FRAME,
4632N/A VIEW_EMBEDDED_FRAME
4717N/A }
4717N/A
4632N/A private static final PlatformLogger focusLog = PlatformLogger.getLogger("sun.lwawt.focus.LWWindowPeer");
4632N/A
4632N/A private PlatformWindow platformWindow;
4632N/A
4632N/A // Window bounds reported by the native system (as opposed to
4632N/A // regular bounds inherited from LWComponentPeer which are
4632N/A // requested by user and may haven't been applied yet because
4632N/A // of asynchronous requests to the windowing system)
4632N/A private int sysX;
4632N/A private int sysY;
4632N/A private int sysW;
4639N/A private int sysH;
4639N/A
4639N/A private static final int MINIMUM_WIDTH = 1;
4632N/A private static final int MINIMUM_HEIGHT = 1;
4632N/A
4632N/A private Insets insets = new Insets(0, 0, 0, 0);
4632N/A
4632N/A private GraphicsDevice graphicsDevice;
4632N/A private GraphicsConfiguration graphicsConfig;
4686N/A
4632N/A private SurfaceData surfaceData;
4632N/A private final Object surfaceDataLock = new Object();
4632N/A
4632N/A private int backBufferCount;
4632N/A private BufferCapabilities backBufferCaps;
4632N/A
4632N/A // The back buffer is used for two purposes:
4632N/A // 1. To render all the lightweight peers
4632N/A // 2. To provide user with a BufferStrategy
4632N/A // Need to check if a single back buffer can be used for both
4632N/A// TODO: VolatileImage
4632N/A// private VolatileImage backBuffer;
4632N/A private volatile BufferedImage backBuffer;
4632N/A
4632N/A private volatile int windowState = Frame.NORMAL;
4632N/A
4632N/A // A peer where the last mouse event came to. Used to generate
4632N/A // MOUSE_ENTERED/EXITED notifications and by cursor manager to
4632N/A // find the component under cursor
4632N/A private static volatile LWComponentPeer lastMouseEventPeer = null;
4632N/A
4632N/A // Peers where all dragged/released events should come to,
4632N/A // depending on what mouse button is being dragged according to Cocoa
4632N/A private static LWComponentPeer mouseDownTarget[] = new LWComponentPeer[3];
4632N/A
4632N/A // A bitmask that indicates what mouse buttons produce MOUSE_CLICKED events
4632N/A // on MOUSE_RELEASE. Click events are only generated if there were no drag
4632N/A // events between MOUSE_PRESSED and MOUSE_RELEASED for particular button
4632N/A private static int mouseClickButtons = 0;
4632N/A
4691N/A private volatile boolean isOpaque = true;
4632N/A
4691N/A private static final Font DEFAULT_FONT = new Font("Lucida Grande", Font.PLAIN, 13);
4639N/A
4665N/A private static LWWindowPeer grabbingWindow;
4665N/A
4717N/A private volatile boolean skipNextFocusChange;
4717N/A
4632N/A private static final Color nonOpaqueBackground = new Color(0, 0, 0, 0);
4632N/A
4632N/A private volatile boolean textured;
4632N/A
4632N/A private final PeerType peerType;
4632N/A
4632N/A /**
4632N/A * Current modal blocker or null.
4632N/A *
4632N/A * Synchronization: peerTreeLock.
4632N/A */
4632N/A private LWWindowPeer blocker;
4632N/A
4632N/A public LWWindowPeer(Window target, PlatformComponent platformComponent,
4632N/A PlatformWindow platformWindow, PeerType peerType)
4632N/A {
4632N/A super(target, platformComponent);
4632N/A this.platformWindow = platformWindow;
4632N/A this.peerType = peerType;
4632N/A
4632N/A Window owner = target.getOwner();
4632N/A LWWindowPeer ownerPeer = (owner != null) ? (LWWindowPeer)owner.getPeer() : null;
4632N/A PlatformWindow ownerDelegate = (ownerPeer != null) ? ownerPeer.getPlatformWindow() : null;
4632N/A
4632N/A // The delegate.initialize() needs a non-null GC on X11.
4691N/A GraphicsConfiguration gc = getTarget().getGraphicsConfiguration();
4691N/A synchronized (getStateLock()) {
4691N/A // graphicsConfig should be updated according to the real window
4691N/A // bounds when the window is shown, see 4868278
4691N/A this.graphicsConfig = gc;
4691N/A }
4691N/A
4691N/A if (!target.isFontSet()) {
4691N/A target.setFont(DEFAULT_FONT);
4691N/A }
4691N/A
4691N/A if (!target.isBackgroundSet()) {
4691N/A target.setBackground(SystemColor.window);
4691N/A } else {
4691N/A // first we check if user provided alpha for background. This is
4691N/A // similar to what Apple's Java do.
4691N/A // Since JDK7 we should rely on setOpacity() only.
4691N/A // this.opacity = c.getAlpha();
4691N/A }
4691N/A
4691N/A if (!target.isForegroundSet()) {
4632N/A target.setForeground(SystemColor.windowText);
4632N/A // we should not call setForeground because it will call a repaint
4632N/A // which the peer may not be ready to do yet.
4632N/A }
4632N/A
4632N/A platformWindow.initialize(target, this, ownerDelegate);
4632N/A }
4632N/A
4632N/A @Override
4632N/A void initializeImpl() {
4632N/A super.initializeImpl();
4632N/A if (getTarget() instanceof Frame) {
4632N/A setTitle(((Frame) getTarget()).getTitle());
4632N/A setState(((Frame) getTarget()).getExtendedState());
4632N/A } else if (getTarget() instanceof Dialog) {
4632N/A setTitle(((Dialog) getTarget()).getTitle());
4632N/A }
4632N/A
4639N/A setAlwaysOnTop(getTarget().isAlwaysOnTop());
4632N/A updateMinimumSize();
4767N/A
4767N/A final Shape shape = getTarget().getShape();
4767N/A if (shape != null) {
4632N/A applyShape(Region.getInstance(shape, null));
4632N/A }
4632N/A
4632N/A final float opacity = getTarget().getOpacity();
4632N/A if (opacity < 1.0f) {
4632N/A setOpacity(opacity);
4632N/A }
4632N/A
4632N/A setOpaque(getTarget().isOpaque());
4632N/A
4632N/A updateInsets(platformWindow.getInsets());
4632N/A if (getSurfaceData() == null) {
4632N/A replaceSurfaceData(false);
4632N/A }
4632N/A }
4632N/A
4632N/A // Just a helper method
4632N/A public PlatformWindow getPlatformWindow() {
4632N/A return platformWindow;
4632N/A }
4632N/A
4639N/A @Override
4686N/A protected LWWindowPeer getWindowPeerOrSelf() {
4686N/A return this;
4686N/A }
4632N/A
4632N/A @Override
4632N/A protected void initializeContainerPeer() {
4665N/A // No-op as LWWindowPeer doesn't have any containerPeer
4665N/A }
4665N/A
4693N/A // ---- PEER METHODS ---- //
4632N/A
4632N/A @Override
4632N/A protected void disposeImpl() {
4632N/A SurfaceData oldData = getSurfaceData();
4632N/A synchronized (surfaceDataLock){
4722N/A surfaceData = null;
4639N/A }
4639N/A if (oldData != null) {
4639N/A oldData.invalidate();
4639N/A }
4722N/A if (isGrabbing()) {
4632N/A ungrab();
4632N/A }
4722N/A destroyBuffers();
4639N/A platformWindow.dispose();
4632N/A super.disposeImpl();
4632N/A }
4632N/A
4632N/A @Override
4639N/A protected void setVisibleImpl(final boolean visible) {
4632N/A super.setVisibleImpl(visible);
4639N/A // TODO: update graphicsConfig, see 4868278
4639N/A platformWindow.setVisible(visible);
4632N/A if (isSimpleWindow()) {
4632N/A KeyboardFocusManagerPeer kfmPeer = LWKeyboardFocusManagerPeer.getInstance();
4632N/A
4632N/A if (visible) {
4722N/A if (!getTarget().isAutoRequestFocus()) {
4632N/A return;
4722N/A } else {
4722N/A requestWindowFocus(CausedFocusEvent.Cause.ACTIVATION);
4722N/A }
4722N/A } else if (kfmPeer.getCurrentFocusedWindow() == getTarget()) {
4722N/A // Transfer focus to the owner.
4722N/A LWWindowPeer owner = getOwnerFrameDialog(LWWindowPeer.this);
4722N/A if (owner != null) {
4722N/A owner.requestWindowFocus(CausedFocusEvent.Cause.ACTIVATION);
4722N/A }
4722N/A }
4722N/A }
4722N/A }
4722N/A
4722N/A @Override
4722N/A public GraphicsConfiguration getGraphicsConfiguration() {
4632N/A return graphicsConfig;
4632N/A }
4632N/A
4632N/A @Override
4632N/A public boolean updateGraphicsData(GraphicsConfiguration gc) {
4632N/A setGraphicsConfig(gc);
4632N/A return false;
4632N/A }
4632N/A
4632N/A protected final Graphics getOnscreenGraphics(Color fg, Color bg, Font f) {
4632N/A if (getSurfaceData() == null) {
4632N/A return null;
4632N/A }
4632N/A if (fg == null) {
4632N/A fg = SystemColor.windowText;
4632N/A }
4686N/A if (bg == null) {
4639N/A bg = SystemColor.window;
4639N/A }
4639N/A if (f == null) {
4632N/A f = DEFAULT_FONT;
4632N/A }
4632N/A return platformWindow.transformGraphics(new SunGraphics2D(getSurfaceData(), fg, bg, f));
4632N/A }
4632N/A
4632N/A @Override
4632N/A public void createBuffers(int numBuffers, BufferCapabilities caps)
4632N/A throws AWTException
4632N/A {
4639N/A try {
4632N/A // Assume this method is never called with numBuffers <= 1, as 0 is
4632N/A // unsupported, and 1 corresponds to a SingleBufferStrategy which
4632N/A // doesn't depend on the peer. Screen is considered as a separate
4632N/A // "buffer", that's why numBuffers - 1
4632N/A assert numBuffers > 1;
4632N/A
4632N/A replaceSurfaceData(numBuffers - 1, caps, false);
4632N/A } catch (InvalidPipeException z) {
4632N/A throw new AWTException(z.toString());
4632N/A }
4632N/A }
4632N/A
4632N/A @Override
4632N/A public final Image getBackBuffer() {
4632N/A synchronized (getStateLock()) {
4632N/A return backBuffer;
4632N/A }
4632N/A }
4632N/A
4632N/A @Override
4693N/A public void flip(int x1, int y1, int x2, int y2,
4693N/A BufferCapabilities.FlipContents flipAction)
4693N/A {
4693N/A final BufferedImage buffer = (BufferedImage)getBackBuffer();
4632N/A if (buffer == null) {
4632N/A throw new IllegalStateException("Buffers have not been created");
4632N/A }
4632N/A final Graphics g = getGraphics();
4632N/A try {
4632N/A g.drawImage(buffer, x1, y1, x2, y2, x1, y1, x2, y2, null);
4632N/A } finally {
4632N/A g.dispose();
4632N/A }
4632N/A if (flipAction == BufferCapabilities.FlipContents.BACKGROUND) {
4693N/A final Graphics2D bg = (Graphics2D) buffer.getGraphics();
4693N/A try {
4693N/A bg.setBackground(getBackground());
4693N/A bg.clearRect(0, 0, buffer.getWidth(), buffer.getHeight());
4693N/A } finally {
4693N/A bg.dispose();
4693N/A }
4693N/A }
4632N/A }
4632N/A
4632N/A @Override
4632N/A public final void destroyBuffers() {
4632N/A final Image oldBB = getBackBuffer();
4632N/A synchronized (getStateLock()) {
4632N/A backBuffer = null;
4632N/A }
4632N/A if (oldBB != null) {
4632N/A oldBB.flush();
4632N/A }
4632N/A }
4639N/A
4639N/A @Override
4639N/A public void setBounds(int x, int y, int w, int h, int op) {
4639N/A
4639N/A if((op & NO_EMBEDDED_CHECK) == 0 && getPeerType() == PeerType.VIEW_EMBEDDED_FRAME) {
4639N/A return;
4632N/A }
4632N/A
4632N/A if ((op & SET_CLIENT_SIZE) != 0) {
4639N/A // SET_CLIENT_SIZE is only applicable to window peers, so handle it here
4632N/A // instead of pulling 'insets' field up to LWComponentPeer
4632N/A // no need to add insets since Window's notion of width and height includes insets.
4632N/A op &= ~SET_CLIENT_SIZE;
4632N/A op |= SET_SIZE;
4632N/A }
4632N/A
4632N/A if (w < MINIMUM_WIDTH) {
4632N/A w = MINIMUM_WIDTH;
4632N/A }
4632N/A if (h < MINIMUM_HEIGHT) {
4639N/A h = MINIMUM_HEIGHT;
4632N/A }
4632N/A
4632N/A if (graphicsConfig instanceof TextureSizeConstraining) {
4632N/A final int maxW = ((TextureSizeConstraining)graphicsConfig).getMaxTextureWidth();
4632N/A final int maxH = ((TextureSizeConstraining)graphicsConfig).getMaxTextureHeight();
4632N/A
4632N/A if (w > maxW) {
4632N/A w = maxW;
4632N/A }
4632N/A if (h > maxH) {
4632N/A h = maxH;
4632N/A }
4632N/A }
4632N/A
4632N/A // Don't post ComponentMoved/Resized and Paint events
4632N/A // until we've got a notification from the delegate
4632N/A setBounds(x, y, w, h, op, false, false);
4632N/A // Get updated bounds, so we don't have to handle 'op' here manually
4632N/A Rectangle r = getBounds();
4632N/A platformWindow.setBounds(r.x, r.y, r.width, r.height);
4632N/A }
4632N/A
4632N/A @Override
4632N/A public Point getLocationOnScreen() {
4632N/A return platformWindow.getLocationOnScreen();
4632N/A }
4632N/A
4632N/A /**
4632N/A * Overridden from LWContainerPeer to return the correct insets.
4632N/A * Insets are queried from the delegate and are kept up to date by
4632N/A * requiering when needed (i.e. when the window geometry is changed).
4632N/A */
4632N/A @Override
4632N/A public Insets getInsets() {
4632N/A synchronized (getStateLock()) {
4632N/A return insets;
4632N/A }
4632N/A }
4632N/A
4632N/A @Override
4632N/A public FontMetrics getFontMetrics(Font f) {
4632N/A // TODO: check for "use platform metrics" settings
4632N/A return platformWindow.getFontMetrics(f);
4632N/A }
4632N/A
4632N/A @Override
4802N/A public void toFront() {
4802N/A platformWindow.toFront();
4802N/A }
4802N/A
4802N/A @Override
4632N/A public void toBack() {
4632N/A platformWindow.toBack();
4632N/A }
4632N/A
4632N/A @Override
4632N/A public void setZOrder(ComponentPeer above) {
4632N/A throw new RuntimeException("not implemented");
4632N/A }
4632N/A
4632N/A @Override
4639N/A public void setAlwaysOnTop(boolean value) {
4632N/A platformWindow.setAlwaysOnTop(value);
4639N/A }
4632N/A
4632N/A @Override
4632N/A public void updateFocusableWindowState() {
4632N/A platformWindow.updateFocusableWindowState();
4632N/A }
4632N/A
4632N/A @Override
4632N/A public void setModalBlocked(Dialog blocker, boolean blocked) {
4632N/A synchronized (getPeerTreeLock()) {
4632N/A if(blocked && blocker.getPeer() instanceof LWWindowPeer) {
4767N/A this.blocker = (LWWindowPeer)blocker.getPeer();
4632N/A } else {
4632N/A this.blocker = null;
4632N/A }
4767N/A }
4639N/A
4639N/A platformWindow.setModalBlocked(blocked);
4639N/A }
4639N/A
4767N/A @Override
4639N/A public void updateMinimumSize() {
4639N/A final Dimension min;
4639N/A if (getTarget().isMinimumSizeSet()) {
4767N/A min = getTarget().getMinimumSize();
4639N/A min.width = Math.max(min.width, MINIMUM_WIDTH);
4632N/A min.height = Math.max(min.height, MINIMUM_HEIGHT);
4632N/A } else {
4632N/A min = new Dimension(MINIMUM_WIDTH, MINIMUM_HEIGHT);
4632N/A }
4639N/A
4632N/A final int maxW, maxH;
4632N/A if (graphicsConfig instanceof TextureSizeConstraining) {
4632N/A maxW = ((TextureSizeConstraining)graphicsConfig).getMaxTextureWidth();
4632N/A maxH = ((TextureSizeConstraining)graphicsConfig).getMaxTextureHeight();
4632N/A } else {
4632N/A maxW = maxH = Integer.MAX_VALUE;
4632N/A }
4632N/A
4632N/A final Dimension max;
4632N/A if (getTarget().isMaximumSizeSet()) {
4632N/A max = getTarget().getMaximumSize();
4632N/A max.width = Math.min(max.width, maxW);
4632N/A max.height = Math.min(max.height, maxH);
4632N/A } else {
4632N/A max = new Dimension(maxW, maxH);
4632N/A }
4632N/A
4632N/A platformWindow.setSizeConstraints(min.width, min.height, max.width, max.height);
4632N/A }
4632N/A
4632N/A @Override
4632N/A public void updateIconImages() {
4632N/A getPlatformWindow().updateIconImages();
4632N/A }
4632N/A
4632N/A @Override
4632N/A public void setOpacity(float opacity) {
4632N/A getPlatformWindow().setOpacity(opacity);
4632N/A repaintPeer();
4632N/A }
4632N/A
4632N/A @Override
4632N/A public final void setOpaque(final boolean isOpaque) {
4632N/A if (this.isOpaque != isOpaque) {
4632N/A this.isOpaque = isOpaque;
4632N/A updateOpaque();
4632N/A }
4632N/A }
4632N/A
4632N/A private void updateOpaque() {
4632N/A getPlatformWindow().setOpaque(!isTranslucent());
4655N/A replaceSurfaceData(false);
4632N/A repaintPeer();
4632N/A }
4632N/A
4632N/A @Override
4632N/A public void updateWindow() {
4632N/A }
4632N/A
4632N/A public final boolean isTextured() {
4632N/A return textured;
4632N/A }
4632N/A
4632N/A public final void setTextured(final boolean isTextured) {
4632N/A textured = isTextured;
4632N/A }
4632N/A
4632N/A public final boolean isTranslucent() {
4632N/A synchronized (getStateLock()) {
4632N/A /*
4632N/A * Textured window is a special case of translucent window.
4632N/A * The difference is only in nswindow background. So when we set
4632N/A * texture property our peer became fully translucent. It doesn't
4632N/A * fill background, create non opaque backbuffers and layer etc.
4632N/A */
4632N/A return !isOpaque || isShaped() || isTextured();
4632N/A }
4632N/A }
4632N/A
4632N/A @Override
4632N/A final void applyShapeImpl(final Region shape) {
4632N/A super.applyShapeImpl(shape);
4632N/A updateOpaque();
4632N/A }
4632N/A
4632N/A @Override
4632N/A public void repositionSecurityWarning() {
4632N/A throw new RuntimeException("not implemented");
4632N/A }
4632N/A
4632N/A // ---- FRAME PEER METHODS ---- //
4632N/A
4632N/A @Override // FramePeer and DialogPeer
4632N/A public void setTitle(String title) {
4632N/A platformWindow.setTitle(title == null ? "" : title);
4632N/A }
4632N/A
4632N/A @Override
4632N/A public void setMenuBar(MenuBar mb) {
4632N/A platformWindow.setMenuBar(mb);
4632N/A }
4632N/A
4632N/A @Override // FramePeer and DialogPeer
4639N/A public void setResizable(boolean resizable) {
4632N/A platformWindow.setResizable(resizable);
4632N/A }
4632N/A
4632N/A @Override
4632N/A public void setState(int state) {
4632N/A platformWindow.setWindowState(state);
4632N/A }
4632N/A
4632N/A @Override
4632N/A public int getState() {
4632N/A return windowState;
4639N/A }
4639N/A
4632N/A @Override
4632N/A public void setMaximizedBounds(Rectangle bounds) {
4639N/A // TODO: not implemented
4632N/A }
4632N/A
4632N/A @Override
4632N/A public void setBoundsPrivate(int x, int y, int width, int height) {
4639N/A setBounds(x, y, width, height, SET_BOUNDS | NO_EMBEDDED_CHECK);
4632N/A }
4632N/A
4632N/A @Override
4632N/A public Rectangle getBoundsPrivate() {
4632N/A throw new RuntimeException("not implemented");
4632N/A }
4632N/A
4632N/A // ---- DIALOG PEER METHODS ---- //
4632N/A
4632N/A @Override
4632N/A public void blockWindows(List<Window> windows) {
4632N/A //TODO: LWX will probably need some collectJavaToplevels to speed this up
4632N/A for (Window w : windows) {
4632N/A WindowPeer wp = (WindowPeer)w.getPeer();
4632N/A if (wp != null) {
4632N/A wp.setModalBlocked((Dialog)getTarget(), true);
4639N/A }
4632N/A }
4632N/A }
4632N/A
4632N/A // ---- PEER NOTIFICATIONS ---- //
4632N/A
4639N/A public void notifyIconify(boolean iconify) {
4632N/A //The toplevel target is Frame and states are applicable to it.
4632N/A //Otherwise, the target is Window and it don't have state property.
4639N/A //Hopefully, no such events are posted in the queue so consider the
4632N/A //target as Frame in all cases.
4639N/A
4632N/A // REMIND: should we send it anyway if the state not changed since last
4632N/A // time?
4639N/A WindowEvent iconifyEvent = new WindowEvent(getTarget(),
4639N/A iconify ? WindowEvent.WINDOW_ICONIFIED
4639N/A : WindowEvent.WINDOW_DEICONIFIED);
4639N/A postEvent(iconifyEvent);
4639N/A
4639N/A int newWindowState = iconify ? Frame.ICONIFIED : Frame.NORMAL;
4639N/A postWindowStateChangedEvent(newWindowState);
4639N/A
4639N/A // REMIND: RepaintManager doesn't repaint iconified windows and
4639N/A // hence ignores any repaint request during deiconification.
4639N/A // So, we need to repaint window explicitly when it becomes normal.
4639N/A if (!iconify) {
4639N/A repaintPeer();
4632N/A }
4632N/A }
4632N/A
4632N/A public void notifyZoom(boolean isZoomed) {
4632N/A int newWindowState = isZoomed ? Frame.MAXIMIZED_BOTH : Frame.NORMAL;
4632N/A postWindowStateChangedEvent(newWindowState);
4632N/A }
4632N/A
4632N/A /**
4632N/A * Called by the delegate when any part of the window should be repainted.
4632N/A */
4665N/A public void notifyExpose(final int x, final int y, final int w, final int h) {
4665N/A // TODO: there's a serious problem with Swing here: it handles
4665N/A // the exposition internally, so SwingPaintEventDispatcher always
4665N/A // return null from createPaintEvent(). However, we flush the
4722N/A // back buffer here unconditionally, so some flickering may appear.
4665N/A // A possible solution is to split postPaintEvent() into two parts,
4665N/A // and override that part which is only called after if
4665N/A // createPaintEvent() returned non-null value and flush the buffer
4665N/A // from the overridden method
4665N/A flushOnscreenGraphics();
4632N/A repaintPeer(new Rectangle(x, y, w, h));
4632N/A }
4632N/A
4632N/A /**
4632N/A * Called by the delegate when this window is moved/resized by user.
4632N/A * There's no notifyReshape() in LWComponentPeer as the only
4632N/A * components which could be resized by user are top-level windows.
4632N/A */
4632N/A public final void notifyReshape(int x, int y, int w, int h) {
4632N/A boolean moved = false;
4632N/A boolean resized = false;
4632N/A synchronized (getStateLock()) {
4632N/A moved = (x != sysX) || (y != sysY);
4632N/A resized = (w != sysW) || (h != sysH);
4632N/A sysX = x;
4632N/A sysY = y;
4632N/A sysW = w;
4632N/A sysH = h;
4632N/A }
4632N/A
4632N/A // Check if anything changed
4632N/A if (!moved && !resized) {
4632N/A return;
4632N/A }
4632N/A // First, update peer's bounds
4632N/A setBounds(x, y, w, h, SET_BOUNDS, false, false);
4632N/A
4632N/A // Second, update the graphics config and surface data
4632N/A checkIfOnNewScreen();
4632N/A if (resized) {
4632N/A replaceSurfaceData();
4632N/A flushOnscreenGraphics();
4632N/A }
4632N/A
4632N/A // Third, COMPONENT_MOVED/COMPONENT_RESIZED events
4632N/A if (moved) {
4632N/A handleMove(x, y, true);
4632N/A }
4632N/A if (resized) {
4632N/A handleResize(w, h,true);
4632N/A }
4632N/A }
4632N/A
4632N/A private void clearBackground(final int w, final int h) {
4632N/A final Graphics g = getOnscreenGraphics(getForeground(), getBackground(),
4632N/A getFont());
4632N/A if (g != null) {
4632N/A try {
4632N/A if (g instanceof Graphics2D) {
4632N/A ((Graphics2D) g).setComposite(AlphaComposite.Src);
4632N/A }
4632N/A if (isTranslucent()) {
4632N/A g.setColor(nonOpaqueBackground);
4632N/A g.fillRect(0, 0, w, h);
4632N/A }
4632N/A if (!isTextured()) {
4632N/A if (g instanceof SunGraphics2D) {
4632N/A SG2DConstraint((SunGraphics2D) g, getRegion());
4632N/A }
4632N/A g.setColor(getBackground());
4632N/A g.fillRect(0, 0, w, h);
4632N/A }
4632N/A } finally {
4632N/A g.dispose();
4632N/A }
4632N/A }
4632N/A }
4632N/A
4632N/A public void notifyUpdateCursor() {
4632N/A getLWToolkit().getCursorManager().updateCursorLater(this);
4632N/A }
4632N/A
4632N/A public void notifyActivation(boolean activation) {
4632N/A changeFocusedWindow(activation);
4632N/A }
4734N/A
4734N/A // MouseDown in non-client area
4734N/A public void notifyNCMouseDown() {
4734N/A // Ungrab except for a click on a Dialog with the grabbing owner
4632N/A if (grabbingWindow != null &&
4734N/A grabbingWindow != getOwnerFrameDialog(this))
4632N/A {
4632N/A grabbingWindow.ungrab();
4632N/A }
4632N/A }
4632N/A
4632N/A // ---- EVENTS ---- //
4665N/A
4665N/A /*
4665N/A * Called by the delegate to dispatch the event to Java. Event
4722N/A * coordinates are relative to non-client window are, i.e. the top-left
4665N/A * point of the client area is (insets.top, insets.left).
4665N/A */
4665N/A public void dispatchMouseEvent(int id, long when, int button,
4632N/A int x, int y, int screenX, int screenY,
4632N/A int modifiers, int clickCount, boolean popupTrigger,
4632N/A byte[] bdata)
4632N/A {
4632N/A // TODO: fill "bdata" member of AWTEvent
4632N/A Rectangle r = getBounds();
4632N/A // findPeerAt() expects parent coordinates
4632N/A LWComponentPeer targetPeer = findPeerAt(r.x + x, r.y + y);
4632N/A LWWindowPeer lastWindowPeer =
4632N/A (lastMouseEventPeer != null) ? lastMouseEventPeer.getWindowPeerOrSelf() : null;
4632N/A LWWindowPeer curWindowPeer =
4632N/A (targetPeer != null) ? targetPeer.getWindowPeerOrSelf() : null;
4734N/A
4632N/A if (id == MouseEvent.MOUSE_EXITED) {
4632N/A // Sometimes we may get MOUSE_EXITED after lastMouseEventPeer is switched
4632N/A // to a peer from another window. So we must first check if this peer is
4632N/A // the same as lastWindowPeer
4632N/A if (lastWindowPeer == this) {
4632N/A if (isEnabled()) {
4632N/A Point lp = lastMouseEventPeer.windowToLocal(x, y,
4632N/A lastWindowPeer);
4734N/A postEvent(new MouseEvent(lastMouseEventPeer.getTarget(),
4632N/A MouseEvent.MOUSE_EXITED, when,
4632N/A modifiers, lp.x, lp.y, screenX,
4632N/A screenY, clickCount, popupTrigger,
4632N/A button));
4632N/A }
4632N/A lastMouseEventPeer = null;
4632N/A }
4632N/A } else {
4632N/A if (targetPeer != lastMouseEventPeer) {
4632N/A // lastMouseEventPeer may be null if mouse was out of Java windows
4632N/A if (lastMouseEventPeer != null && lastMouseEventPeer.isEnabled()) {
4632N/A // Sometimes, MOUSE_EXITED is not sent by delegate (or is sent a bit
4632N/A // later), in which case lastWindowPeer is another window
4632N/A if (lastWindowPeer != this) {
4632N/A Point oldp = lastMouseEventPeer.windowToLocal(x, y, lastWindowPeer);
4632N/A // Additionally translate from this to lastWindowPeer coordinates
4632N/A Rectangle lr = lastWindowPeer.getBounds();
4632N/A oldp.x += r.x - lr.x;
4632N/A oldp.y += r.y - lr.y;
4632N/A postEvent(new MouseEvent(lastMouseEventPeer.getTarget(),
4632N/A MouseEvent.MOUSE_EXITED,
4632N/A when, modifiers,
4632N/A oldp.x, oldp.y, screenX, screenY,
4632N/A clickCount, popupTrigger, button));
4632N/A } else {
4632N/A Point oldp = lastMouseEventPeer.windowToLocal(x, y, this);
4632N/A postEvent(new MouseEvent(lastMouseEventPeer.getTarget(),
4632N/A MouseEvent.MOUSE_EXITED,
4632N/A when, modifiers,
4632N/A oldp.x, oldp.y, screenX, screenY,
4632N/A clickCount, popupTrigger, button));
4632N/A }
4632N/A }
4632N/A lastMouseEventPeer = targetPeer;
4632N/A if (targetPeer != null && targetPeer.isEnabled() && id != MouseEvent.MOUSE_ENTERED) {
4632N/A Point newp = targetPeer.windowToLocal(x, y, curWindowPeer);
4632N/A postEvent(new MouseEvent(targetPeer.getTarget(),
4632N/A MouseEvent.MOUSE_ENTERED,
4632N/A when, modifiers,
4632N/A newp.x, newp.y, screenX, screenY,
4632N/A clickCount, popupTrigger, button));
4632N/A }
4632N/A }
4632N/A // TODO: fill "bdata" member of AWTEvent
4632N/A
4632N/A int eventButtonMask = (button > 0)? MouseEvent.getMaskForButton(button) : 0;
4632N/A int otherButtonsPressed = modifiers & ~eventButtonMask;
4632N/A
4632N/A // For pressed/dragged/released events OS X treats other
4632N/A // mouse buttons as if they were BUTTON2, so we do the same
4632N/A int targetIdx = (button > 3) ? MouseEvent.BUTTON2 - 1 : button - 1;
4632N/A
4632N/A // MOUSE_ENTERED/EXITED are generated for the components strictly under
4632N/A // mouse even when dragging. That's why we first update lastMouseEventPeer
4632N/A // based on initial targetPeer value and only then recalculate targetPeer
4632N/A // for MOUSE_DRAGGED/RELEASED events
4632N/A if (id == MouseEvent.MOUSE_PRESSED) {
4632N/A
4632N/A // Ungrab only if this window is not an owned window of the grabbing one.
4632N/A if (!isGrabbing() && grabbingWindow != null &&
4632N/A grabbingWindow != getOwnerFrameDialog(this))
4632N/A {
4632N/A grabbingWindow.ungrab();
4632N/A }
4632N/A if (otherButtonsPressed == 0) {
4632N/A mouseClickButtons = eventButtonMask;
4632N/A } else {
4632N/A mouseClickButtons |= eventButtonMask;
4632N/A }
4632N/A
4632N/A mouseDownTarget[targetIdx] = targetPeer;
4632N/A } else if (id == MouseEvent.MOUSE_DRAGGED) {
4632N/A // Cocoa dragged event has the information about which mouse
4632N/A // button is being dragged. Use it to determine the peer that
4632N/A // should receive the dragged event.
4632N/A targetPeer = mouseDownTarget[targetIdx];
4639N/A mouseClickButtons &= ~modifiers;
4639N/A } else if (id == MouseEvent.MOUSE_RELEASED) {
4632N/A // TODO: currently, mouse released event goes to the same component
4632N/A // that received corresponding mouse pressed event. For most cases,
4632N/A // it's OK, however, we need to make sure that our behavior is consistent
4632N/A // with 1.6 for cases where component in question have been
4632N/A // hidden/removed in between of mouse pressed/released events.
4632N/A targetPeer = mouseDownTarget[targetIdx];
4632N/A
4632N/A if ((modifiers & eventButtonMask) == 0) {
4632N/A mouseDownTarget[targetIdx] = null;
4632N/A }
4632N/A
4632N/A // mouseClickButtons is updated below, after MOUSE_CLICK is sent
4632N/A }
4632N/A
4632N/A // check if we receive mouseEvent from outside the window's bounds
4632N/A // it can be either mouseDragged or mouseReleased
4632N/A if (curWindowPeer == null) {
4632N/A //TODO This can happen if this window is invisible. this is correct behavior in this case?
4632N/A curWindowPeer = this;
4632N/A }
4632N/A if (targetPeer == null) {
4639N/A //TODO This can happen if this window is invisible. this is correct behavior in this case?
4632N/A targetPeer = this;
4632N/A }
4632N/A
4632N/A
4632N/A Point lp = targetPeer.windowToLocal(x, y, curWindowPeer);
4632N/A if (targetPeer.isEnabled()) {
4632N/A MouseEvent event = new MouseEvent(targetPeer.getTarget(), id,
4632N/A when, modifiers, lp.x, lp.y,
4632N/A screenX, screenY, clickCount,
4632N/A popupTrigger, button);
4632N/A postEvent(event);
4632N/A }
4632N/A
4632N/A if (id == MouseEvent.MOUSE_RELEASED) {
4632N/A if ((mouseClickButtons & eventButtonMask) != 0
4632N/A && targetPeer.isEnabled()) {
4632N/A postEvent(new MouseEvent(targetPeer.getTarget(),
4632N/A MouseEvent.MOUSE_CLICKED,
4632N/A when, modifiers,
4632N/A lp.x, lp.y, screenX, screenY,
4632N/A clickCount, popupTrigger, button));
4632N/A }
4632N/A mouseClickButtons &= ~eventButtonMask;
4632N/A }
4632N/A }
4632N/A notifyUpdateCursor();
4632N/A }
4632N/A
4632N/A public void dispatchMouseWheelEvent(long when, int x, int y, int modifiers,
4632N/A int scrollType, int scrollAmount,
4632N/A int wheelRotation, double preciseWheelRotation,
4632N/A byte[] bdata)
4632N/A {
4632N/A // TODO: could we just use the last mouse event target here?
4632N/A Rectangle r = getBounds();
4632N/A // findPeerAt() expects parent coordinates
4632N/A final LWComponentPeer targetPeer = findPeerAt(r.x + x, r.y + y);
4632N/A if (targetPeer == null || !targetPeer.isEnabled()) {
4632N/A return;
4632N/A }
4632N/A
4632N/A Point lp = targetPeer.windowToLocal(x, y, this);
4632N/A // TODO: fill "bdata" member of AWTEvent
4632N/A // TODO: screenX/screenY
4632N/A postEvent(new MouseWheelEvent(targetPeer.getTarget(),
4632N/A MouseEvent.MOUSE_WHEEL,
4632N/A when, modifiers,
4632N/A lp.x, lp.y,
4632N/A 0, 0, /* screenX, Y */
4632N/A 0 /* clickCount */, false /* popupTrigger */,
4632N/A scrollType, scrollAmount,
4632N/A wheelRotation, preciseWheelRotation));
4632N/A }
4632N/A
4632N/A /*
4632N/A * Called by the delegate when a key is pressed.
4632N/A */
4632N/A public void dispatchKeyEvent(int id, long when, int modifiers,
4632N/A int keyCode, char keyChar, int keyLocation)
4632N/A {
4632N/A LWKeyboardFocusManagerPeer kfmPeer = LWKeyboardFocusManagerPeer.getInstance();
4632N/A Component focusOwner = kfmPeer.getCurrentFocusOwner();
4632N/A
4632N/A if (focusOwner == null) {
4632N/A focusOwner = kfmPeer.getCurrentFocusedWindow();
4632N/A if (focusOwner == null) {
4632N/A focusOwner = this.getTarget();
4632N/A }
4632N/A }
4632N/A postEvent(new KeyEvent(focusOwner, id, when, modifiers, keyCode, keyChar, keyLocation));
4632N/A }
4632N/A
4632N/A
4632N/A // ---- UTILITY METHODS ---- //
4639N/A
4632N/A private void postWindowStateChangedEvent(int newWindowState) {
4632N/A if (getTarget() instanceof Frame) {
4632N/A AWTAccessor.getFrameAccessor().setExtendedState(
4632N/A (Frame)getTarget(), newWindowState);
4632N/A }
4639N/A WindowEvent stateChangedEvent = new WindowEvent(getTarget(),
4632N/A WindowEvent.WINDOW_STATE_CHANGED,
4632N/A windowState, newWindowState);
4632N/A postEvent(stateChangedEvent);
4632N/A windowState = newWindowState;
4632N/A }
4632N/A
4632N/A private static int getGraphicsConfigScreen(GraphicsConfiguration gc) {
4632N/A // TODO: this method can be implemented in a more
4632N/A // efficient way by forwarding to the delegate
4632N/A GraphicsDevice gd = gc.getDevice();
4632N/A GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
4632N/A GraphicsDevice[] gds = ge.getScreenDevices();
4632N/A for (int i = 0; i < gds.length; i++) {
4632N/A if (gds[i] == gd) {
4632N/A return i;
4632N/A }
4632N/A }
4632N/A // Should never happen if gc is a screen device config
4632N/A return 0;
4632N/A }
4632N/A
4632N/A /*
4632N/A * This method is called when window's graphics config is changed from
4632N/A * the app code (e.g. when the window is made non-opaque) or when
4632N/A * the window is moved to another screen by user.
4686N/A *
4686N/A * Returns true if the graphics config has been changed, false otherwise.
4686N/A */
4686N/A private boolean setGraphicsConfig(GraphicsConfiguration gc) {
4632N/A synchronized (getStateLock()) {
4632N/A if (graphicsConfig == gc) {
4632N/A return false;
4632N/A }
4632N/A // If window's graphics config is changed from the app code, the
4632N/A // config correspond to the same device as before; when the window
4686N/A // is moved by user, graphicsDevice is updated in checkIfOnNewScreen().
4639N/A // In either case, there's nothing to do with screenOn here
4686N/A graphicsConfig = gc;
4686N/A }
4686N/A // SurfaceData is replaced later in updateGraphicsData()
4686N/A return true;
4686N/A }
4686N/A
4686N/A private void checkIfOnNewScreen() {
4686N/A GraphicsDevice newGraphicsDevice = platformWindow.getGraphicsDevice();
4686N/A synchronized (getStateLock()) {
4686N/A if (graphicsDevice == newGraphicsDevice) {
4686N/A return;
4686N/A }
4686N/A graphicsDevice = newGraphicsDevice;
4639N/A }
4686N/A
4686N/A // TODO: DisplayChangedListener stuff
4686N/A final GraphicsConfiguration newGC = newGraphicsDevice.getDefaultConfiguration();
4686N/A
4686N/A if (!setGraphicsConfig(newGC)) return;
4639N/A
4686N/A SunToolkit.executeOnEventHandlerThread(getTarget(), new Runnable() {
4686N/A public void run() {
4686N/A AWTAccessor.getComponentAccessor().setGraphicsConfiguration(getTarget(), newGC);
4686N/A }
4686N/A });
4686N/A }
4686N/A
4686N/A /*
4686N/A * May be called by delegate to provide SD to Java2D code.
4686N/A */
4686N/A public SurfaceData getSurfaceData() {
4686N/A synchronized (surfaceDataLock) {
4686N/A return surfaceData;
4686N/A }
4686N/A }
4686N/A
4632N/A private void replaceSurfaceData() {
4632N/A replaceSurfaceData(true);
4632N/A }
4632N/A
4632N/A private void replaceSurfaceData(boolean blit) {
4639N/A replaceSurfaceData(backBufferCount, backBufferCaps, blit);
4639N/A }
4639N/A
4639N/A private void replaceSurfaceData(int newBackBufferCount,
4639N/A BufferCapabilities newBackBufferCaps,
4639N/A boolean blit) {
4639N/A synchronized (surfaceDataLock) {
4639N/A final SurfaceData oldData = getSurfaceData();
4639N/A surfaceData = platformWindow.replaceSurfaceData();
4639N/A // TODO: volatile image
4639N/A // VolatileImage oldBB = backBuffer;
4639N/A BufferedImage oldBB = backBuffer;
4639N/A backBufferCount = newBackBufferCount;
4639N/A backBufferCaps = newBackBufferCaps;
4639N/A final Rectangle size = getSize();
4639N/A if (getSurfaceData() != null && oldData != getSurfaceData()) {
4639N/A clearBackground(size.width, size.height);
4632N/A }
4632N/A
4632N/A if (blit) {
4632N/A blitSurfaceData(oldData, getSurfaceData());
4632N/A }
4632N/A
4632N/A if (oldData != null && oldData != getSurfaceData()) {
4632N/A // TODO: drop oldData for D3D/WGL pipelines
4632N/A // This can only happen when this peer is being created
4632N/A oldData.flush();
4632N/A }
4632N/A
4632N/A // TODO: volatile image
4632N/A // backBuffer = (VolatileImage)delegate.createBackBuffer();
4632N/A backBuffer = (BufferedImage) platformWindow.createBackBuffer();
4632N/A if (backBuffer != null) {
4632N/A Graphics g = backBuffer.getGraphics();
4632N/A try {
4632N/A Rectangle r = getBounds();
4632N/A if (g instanceof Graphics2D) {
4632N/A ((Graphics2D) g).setComposite(AlphaComposite.Src);
4632N/A }
4632N/A g.setColor(nonOpaqueBackground);
4632N/A g.fillRect(0, 0, r.width, r.height);
4632N/A if (g instanceof SunGraphics2D) {
4767N/A SG2DConstraint((SunGraphics2D) g, getRegion());
4632N/A }
4632N/A if (!isTextured()) {
4632N/A g.setColor(getBackground());
4632N/A g.fillRect(0, 0, r.width, r.height);
4632N/A }
4632N/A if (oldBB != null) {
4632N/A // Draw the old back buffer to the new one
4632N/A g.drawImage(oldBB, 0, 0, null);
4632N/A oldBB.flush();
4632N/A }
4717N/A } finally {
4717N/A g.dispose();
4717N/A }
4717N/A }
4632N/A }
4717N/A }
4717N/A
4717N/A private void blitSurfaceData(final SurfaceData src, final SurfaceData dst) {
4717N/A //TODO blit. proof-of-concept
4717N/A if (src != dst && src != null && dst != null
4717N/A && !(dst instanceof NullSurfaceData)
4717N/A && !(src instanceof NullSurfaceData)
4717N/A && src.getSurfaceType().equals(dst.getSurfaceType())) {
4717N/A final Rectangle size = getSize();
4632N/A final Blit blit = Blit.locate(src.getSurfaceType(),
4632N/A CompositeType.Src,
4717N/A dst.getSurfaceType());
4717N/A if (blit != null) {
4717N/A blit.Blit(src, dst, AlphaComposite.Src,
4717N/A getRegion(), 0, 0, 0, 0, size.width, size.height);
4717N/A }
4717N/A }
4722N/A }
4717N/A
4717N/A public int getBackBufferCount() {
4717N/A return backBufferCount;
4722N/A }
4717N/A
4722N/A public BufferCapabilities getBackBufferCaps() {
4717N/A return backBufferCaps;
4717N/A }
4717N/A
4717N/A /*
4717N/A * Request the window insets from the delegate and compares it
4717N/A * with the current one. This method is mostly called by the
4717N/A * delegate, e.g. when the window state is changed and insets
4717N/A * should be recalculated.
4717N/A *
4717N/A * This method may be called on the toolkit thread.
4717N/A */
4722N/A public boolean updateInsets(Insets newInsets) {
4717N/A boolean changed = false;
4722N/A synchronized (getStateLock()) {
4717N/A changed = (insets.equals(newInsets));
4717N/A insets = newInsets;
4717N/A }
4717N/A
4717N/A if (changed) {
4717N/A replaceSurfaceData();
4717N/A repaintPeer();
4717N/A }
4717N/A
4717N/A return changed;
4717N/A }
4717N/A
4717N/A public static LWWindowPeer getWindowUnderCursor() {
4717N/A return lastMouseEventPeer != null ? lastMouseEventPeer.getWindowPeerOrSelf() : null;
4632N/A }
4632N/A
4632N/A public static LWComponentPeer<?, ?> getPeerUnderCursor() {
4632N/A return lastMouseEventPeer;
4632N/A }
4632N/A
4632N/A /*
4632N/A * Requests platform to set native focus on a frame/dialog.
4632N/A * In case of a simple window, triggers appropriate java focus change.
4632N/A */
4632N/A public boolean requestWindowFocus(CausedFocusEvent.Cause cause) {
4632N/A if (focusLog.isLoggable(PlatformLogger.FINE)) {
4632N/A focusLog.fine("requesting native focus to " + this);
4632N/A }
4632N/A
4632N/A if (!focusAllowedFor()) {
4632N/A focusLog.fine("focus is not allowed");
4717N/A return false;
4717N/A }
4717N/A
4717N/A if (platformWindow.rejectFocusRequest(cause)) {
4717N/A return false;
4717N/A }
4632N/A
4632N/A Window currentActive = KeyboardFocusManager.
4632N/A getCurrentKeyboardFocusManager().getActiveWindow();
4632N/A
4632N/A // Make the owner active window.
4632N/A if (isSimpleWindow()) {
4632N/A LWWindowPeer owner = getOwnerFrameDialog(this);
4632N/A
4632N/A // If owner is not natively active, request native
4717N/A // activation on it w/o sending events up to java.
4717N/A if (owner != null && !owner.platformWindow.isActive()) {
4717N/A if (focusLog.isLoggable(PlatformLogger.FINE)) {
4632N/A focusLog.fine("requesting native focus to the owner " + owner);
4632N/A }
4632N/A LWWindowPeer currentActivePeer = (currentActive != null ?
4632N/A (LWWindowPeer)currentActive.getPeer() : null);
4632N/A
4639N/A // Ensure the opposite is natively active and suppress sending events.
4639N/A if (currentActivePeer != null && currentActivePeer.platformWindow.isActive()) {
4632N/A if (focusLog.isLoggable(PlatformLogger.FINE)) {
4632N/A focusLog.fine("the opposite is " + currentActivePeer);
4665N/A }
4665N/A currentActivePeer.skipNextFocusChange = true;
4665N/A }
4665N/A owner.skipNextFocusChange = true;
4665N/A
4722N/A owner.platformWindow.requestWindowFocus();
4665N/A }
4717N/A
4665N/A // DKFM will synthesize all the focus/activation events correctly.
4665N/A changeFocusedWindow(true);
4665N/A return true;
4665N/A
4632N/A // In case the toplevel is active but not focused, change focus directly,
4632N/A // as requesting native focus on it will not have effect.
4632N/A } else if (getTarget() == currentActive && !getTarget().hasFocus()) {
4632N/A
4632N/A changeFocusedWindow(true);
4632N/A return true;
4632N/A }
4632N/A return platformWindow.requestWindowFocus();
4632N/A }
4722N/A
4665N/A private boolean focusAllowedFor() {
4665N/A Window window = getTarget();
4665N/A // TODO: check if modal blocked
4665N/A return window.isVisible() && window.isEnabled() && isFocusableWindow();
4722N/A }
4665N/A
4665N/A private boolean isFocusableWindow() {
4632N/A boolean focusable = getTarget().isFocusableWindow();
4632N/A if (isSimpleWindow()) {
4632N/A LWWindowPeer ownerPeer = getOwnerFrameDialog(this);
4632N/A if (ownerPeer == null) {
4632N/A return false;
4632N/A }
4632N/A return focusable && ownerPeer.getTarget().isFocusableWindow();
4632N/A }
4632N/A return focusable;
4632N/A }
4632N/A
4632N/A public boolean isSimpleWindow() {
4632N/A Window window = getTarget();
4632N/A return !(window instanceof Dialog || window instanceof Frame);
4632N/A }
4632N/A
4632N/A /*
4632N/A * Changes focused window on java level.
4632N/A */
4632N/A private void changeFocusedWindow(boolean becomesFocused) {
4632N/A if (focusLog.isLoggable(PlatformLogger.FINE)) {
4632N/A focusLog.fine((becomesFocused?"gaining":"loosing") + " focus window: " + this);
4632N/A }
4655N/A if (skipNextFocusChange) {
4655N/A focusLog.fine("skipping focus change");
4655N/A skipNextFocusChange = false;
4655N/A return;
4665N/A }
4665N/A if (!isFocusableWindow() && becomesFocused) {
4665N/A focusLog.fine("the window is not focusable");
4665N/A return;
4665N/A }
4665N/A if (becomesFocused) {
4665N/A synchronized (getPeerTreeLock()) {
4665N/A if (blocker != null) {
4665N/A if (focusLog.isLoggable(PlatformLogger.FINEST)) {
4665N/A focusLog.finest("the window is blocked by " + blocker);
4665N/A }
4665N/A return;
4665N/A }
4665N/A }
4665N/A }
4665N/A
4665N/A KeyboardFocusManagerPeer kfmPeer = LWKeyboardFocusManagerPeer.getInstance();
4665N/A Window oppositeWindow = becomesFocused ? kfmPeer.getCurrentFocusedWindow() : null;
4717N/A
4717N/A // Note, the method is not called:
4717N/A // - when the opposite (gaining focus) window is an owned/owner window.
4717N/A // - for a simple window in any case.
4717N/A if (!becomesFocused &&
4632N/A (isGrabbing() || getOwnerFrameDialog(grabbingWindow) == this))
{
focusLog.fine("ungrabbing on " + grabbingWindow);
// ungrab a simple window if its owner looses activation.
grabbingWindow.ungrab();
}
kfmPeer.setCurrentFocusedWindow(becomesFocused ? getTarget() : null);
int eventID = becomesFocused ? WindowEvent.WINDOW_GAINED_FOCUS : WindowEvent.WINDOW_LOST_FOCUS;
WindowEvent windowEvent = new WindowEvent(getTarget(), eventID, oppositeWindow);
// TODO: wrap in SequencedEvent
postEvent(windowEvent);
}
static LWWindowPeer getOwnerFrameDialog(LWWindowPeer peer) {
Window owner = (peer != null ? peer.getTarget().getOwner() : null);
while (owner != null && !(owner instanceof Frame || owner instanceof Dialog)) {
owner = owner.getOwner();
}
return owner != null ? (LWWindowPeer)owner.getPeer() : null;
}
/**
* Returns the foremost modal blocker of this window, or null.
*/
public LWWindowPeer getBlocker() {
synchronized (getPeerTreeLock()) {
LWWindowPeer blocker = this.blocker;
if (blocker == null) {
return null;
}
while (blocker.blocker != null) {
blocker = blocker.blocker;
}
return blocker;
}
}
public void enterFullScreenMode() {
platformWindow.enterFullScreenMode();
}
public void exitFullScreenMode() {
platformWindow.exitFullScreenMode();
}
public long getLayerPtr() {
return getPlatformWindow().getLayerPtr();
}
void grab() {
if (grabbingWindow != null && !isGrabbing()) {
grabbingWindow.ungrab();
}
grabbingWindow = this;
}
void ungrab() {
if (isGrabbing()) {
grabbingWindow = null;
postEvent(new UngrabEvent(getTarget()));
}
}
private boolean isGrabbing() {
return this == grabbingWindow;
}
public PeerType getPeerType() {
return peerType;
}
@Override
public String toString() {
return super.toString() + " [target is " + getTarget() + "]";
}
}