1 /*
   2  * Copyright (c) 1999, 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
  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 
  36 class WPrintDialogPeer extends WWindowPeer implements DialogPeer {
  37 
  38     static {
  39         initIDs();
  40     }
  41 
  42     private WComponentPeer parent;
  43 
  44     private Vector<WWindowPeer> blockedWindows = new Vector<>();
  45 
  46     WPrintDialogPeer(WPrintDialog target) {
  47         super(target);
  48     }
  49 
  50     @Override
  51     void create(WComponentPeer parent) {
  52         this.parent = parent;
  53     }
  54 
  55     // fix for CR 6178323:
  56     // don't use checkCreation() from WComponentPeer to avoid hwnd check
  57     @Override
  58     protected void checkCreation() {
  59     }
  60 
  61     @Override
  62     protected void disposeImpl() {
  63         WToolkit.targetDisposedPeer(target, this);
  64     }
  65 
  66     private native boolean _show();
  67 
  68     @Override
  69     public void show() {
  70         Runnable runnable = () -> {
  71             try {
  72                 ((WPrintDialog)target).setRetVal(_show());
  73             } catch (Exception e) {
  74                 // No exception should be thrown by native dialog code,
  75                 // but if it is we need to trap it so the thread does
  76                 // not hide is called and the thread doesn't hang.
  77             }
  78             ((WPrintDialog)target).setVisible(false);
  79         };
  80         new Thread(null, runnable, "PrintDialog", 0, false).start();
  81     }
  82 
  83     synchronized void setHWnd(long hwnd) {
  84         this.hwnd = hwnd;
  85         for (WWindowPeer window : blockedWindows) {
  86             if (hwnd != 0) {
  87                 window.modalDisable((Dialog)target, hwnd);
  88             } else {
  89                 window.modalEnable((Dialog)target);
  90             }
  91         }
  92     }
  93 
  94     synchronized void blockWindow(WWindowPeer window) {
  95         blockedWindows.add(window);
  96         if (hwnd != 0) {
  97             window.modalDisable((Dialog)target, hwnd);
  98         }
  99     }
 100     synchronized void unblockWindow(WWindowPeer window) {
 101         blockedWindows.remove(window);
 102         if (hwnd != 0) {
 103             window.modalEnable((Dialog)target);
 104         }
 105     }
 106 
 107     @Override
 108     public void blockWindows(java.util.List<Window> toBlock) {
 109         for (Window w : toBlock) {
 110             WWindowPeer wp = AWTAccessor.getComponentAccessor().getPeer(w);
 111             if (wp != null) {
 112                 blockWindow(wp);
 113             }
 114         }
 115     }
 116 
 117     @Override
 118     public native void toFront();
 119     @Override
 120     public native void toBack();
 121 
 122     // unused methods.  Overridden to disable this functionality as
 123     // it requires HWND which is not available for FileDialog
 124     @Override
 125     void initialize() {}
 126     @Override
 127     public void updateAlwaysOnTopState() {}
 128     @Override
 129     public void setResizable(boolean resizable) {}
 130     @Override
 131     void hide() {}
 132     @Override
 133     void enable() {}
 134     @Override
 135     void disable() {}
 136     @Override
 137     public void reshape(int x, int y, int width, int height) {}
 138     @SuppressWarnings("deprecation")
 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 }