1 /*
   2  * Copyright (c) 1999, 2015, 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.awt.windows;
  27 
  28 import java.awt.*;
  29 import java.awt.event.FocusEvent.Cause;
  30 import java.awt.peer.DialogPeer;
  31 import java.awt.peer.ComponentPeer;
  32 import java.awt.dnd.DropTarget;
  33 import java.util.Vector;
  34 import sun.awt.AWTAccessor;
  35 import sun.misc.ManagedLocalsThread;
  36 
  37 class WPrintDialogPeer extends WWindowPeer implements DialogPeer {
  38 
  39     static {
  40         initIDs();
  41     }
  42 
  43     private WComponentPeer parent;
  44 
  45     private Vector<WWindowPeer> blockedWindows = new Vector<>();
  46 
  47     WPrintDialogPeer(WPrintDialog target) {
  48         super(target);
  49     }
  50 
  51     @Override
  52     void create(WComponentPeer parent) {
  53         this.parent = parent;
  54     }
  55 
  56     // fix for CR 6178323:
  57     // don't use checkCreation() from WComponentPeer to avoid hwnd check
  58     @Override
  59     protected void checkCreation() {
  60     }
  61 
  62     @Override
  63     protected void disposeImpl() {
  64         WToolkit.targetDisposedPeer(target, this);
  65     }
  66 
  67     private native boolean _show();
  68 
  69     @Override
  70     public void show() {
  71         Runnable runnable = () -> {
  72             try {
  73                 ((WPrintDialog)target).setRetVal(_show());
  74             } catch (Exception e) {
  75                 // No exception should be thrown by native dialog code,
  76                 // but if it is we need to trap it so the thread does
  77                 // not hide is called and the thread doesn't hang.
  78             }
  79             ((WPrintDialog)target).setVisible(false);
  80         };
  81         new ManagedLocalsThread(runnable).start();
  82     }
  83 
  84     synchronized void setHWnd(long hwnd) {
  85         this.hwnd = hwnd;
  86         for (WWindowPeer window : blockedWindows) {
  87             if (hwnd != 0) {
  88                 window.modalDisable((Dialog)target, hwnd);
  89             } else {
  90                 window.modalEnable((Dialog)target);
  91             }
  92         }
  93     }
  94 
  95     synchronized void blockWindow(WWindowPeer window) {
  96         blockedWindows.add(window);
  97         if (hwnd != 0) {
  98             window.modalDisable((Dialog)target, hwnd);
  99         }
 100     }
 101     synchronized void unblockWindow(WWindowPeer window) {
 102         blockedWindows.remove(window);
 103         if (hwnd != 0) {
 104             window.modalEnable((Dialog)target);
 105         }
 106     }
 107 
 108     @Override
 109     public void blockWindows(java.util.List<Window> toBlock) {
 110         for (Window w : toBlock) {
 111             WWindowPeer wp = AWTAccessor.getComponentAccessor().getPeer(w);
 112             if (wp != null) {
 113                 blockWindow(wp);
 114             }
 115         }
 116     }
 117 
 118     @Override
 119     public native void toFront();
 120     @Override
 121     public native void toBack();
 122 
 123     // unused methods.  Overridden to disable this functionality as
 124     // it requires HWND which is not available for FileDialog
 125     @Override
 126     void initialize() {}
 127     @Override
 128     public void updateAlwaysOnTopState() {}
 129     @Override
 130     public void setResizable(boolean resizable) {}
 131     @Override
 132     void hide() {}
 133     @Override
 134     void enable() {}
 135     @Override
 136     void disable() {}
 137     @Override
 138     public void reshape(int x, int y, int width, int height) {}
 139     public boolean handleEvent(Event e) { return false; }
 140     @Override
 141     public void setForeground(Color c) {}
 142     @Override
 143     public void setBackground(Color c) {}
 144     @Override
 145     public void setFont(Font f) {}
 146     @Override
 147     public void updateMinimumSize() {}
 148     @Override
 149     public void updateIconImages() {}
 150     public boolean requestFocus(boolean temporary, boolean focusedWindowChangeAllowed) {
 151         return false;
 152     }
 153 
 154     @Override
 155     public boolean requestFocus
 156          (Component lightweightChild, boolean temporary,
 157           boolean focusedWindowChangeAllowed, long time, Cause cause)
 158     {
 159 
 160         return false;
 161     }
 162 
 163     @Override
 164     public void updateFocusableWindowState() {}
 165     @Override
 166     void start() {}
 167     @Override
 168     public void beginValidate() {}
 169     @Override
 170     public void endValidate() {}
 171     void invalidate(int x, int y, int width, int height) {}
 172     @Override
 173     public void addDropTarget(DropTarget dt) {}
 174     @Override
 175     public void removeDropTarget(DropTarget dt) {}
 176     @Override
 177     public void setZOrder(ComponentPeer above) {}
 178 
 179     /**
 180      * Initialize JNI field and method ids
 181      */
 182     private static native void initIDs();
 183 
 184     // The effects are not supported for system dialogs.
 185     @Override
 186     public void applyShape(sun.java2d.pipe.Region shape) {}
 187     @Override
 188     public void setOpacity(float opacity) {}
 189     @Override
 190     public void setOpaque(boolean isOpaque) {}
 191     public void updateWindow(java.awt.image.BufferedImage backBuffer) {}
 192 
 193     // the file/print dialogs are native dialogs and
 194     // the native system does their own rendering
 195     @Override
 196     public void createScreenSurface(boolean isResize) {}
 197     @Override
 198     public void replaceSurfaceData() {}
 199 }