1 /*
   2  * Copyright (c) 2018, 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 javax.print.attribute.standard;
  27 
  28 import java.awt.Window;
  29 import javax.print.attribute.Attribute;
  30 import javax.print.attribute.PrintRequestAttribute;
  31 import sun.print.DialogOwnerAccessor;
  32 
  33 /**
  34  * An attribute class used to support requesting a print or page setup dialog
  35  * be kept displayed on top of all windows or some specific window.
  36  * <p>
  37  * Constructed without any arguments it will request that a print or page
  38  * setup dialog be configured as if the application directly was to specify
  39  * {@code java.awt.Window.setAlwaysOnTop(true)}, subject to permission checks.
  40  * <p>
  41  * Constructed with a {@link java.awt.Window} parameter, it requests that
  42  * the dialog be owned by the specified window.
  43  *
  44  * @since 11
  45  */
  46 public final class DialogOwner implements PrintRequestAttribute {
  47 
  48     private static class Accessor extends DialogOwnerAccessor {
  49 
  50          public long getOwnerID(DialogOwner owner) {
  51              return owner.getID();
  52          }
  53     }
  54 
  55     static private Accessor accessor = new Accessor();
  56     static {
  57              DialogOwnerAccessor.setAccessor(accessor);
  58     }
  59 
  60     private static final long serialVersionUID = -1901909867156076547L;
  61 
  62     private Window owner;
  63     private transient long id;
  64 
  65     /**
  66      * Constructs an instance which can be used to request
  67      * {@code java.awt.Window.setAlwaysOnTop(true)} behaviour.
  68      * This should be used where there is no application preferred owner window.
  69      * Whether this has any effect depends on if always on top is supported
  70      * for this platform and the particular dialog to be displayed.
  71      */
  72     public DialogOwner() {
  73     }
  74 
  75     /**
  76      * Constructs an instance which can be used to request that the
  77      * specified {@link java.awt.Window} be the owner of the dialog.
  78      * @param owner window.
  79      */
  80     public DialogOwner(Window owner) {
  81         this.owner = owner;
  82     }
  83 
  84     /**
  85      * Constructs an instance which requests that the dialog be displayed
  86      * as if it were a child of a native platform window, specified
  87      * using its opqaue platform identifier or handle.
  88      * This is useful mainly for the case where the id represents a window
  89      * which may not be an AWT {@code Window}, but instead was created by
  90      * another UI toolkit, such as OpenJFX.
  91      * Any effect is platform dependent.
  92      * @param id a native window identifier or handle
  93      */
  94     DialogOwner(long id) {
  95         this.id = id;
  96     }
  97 
  98     /**
  99      * Returns a native platform id or handle, if one was specified,
 100      * otherwise, zero.
 101      * @return a native platform id.
 102      */
 103     long getID() {
 104         return id;
 105     }
 106 
 107     /**
 108      * Returns a {@code Window owner}, if one was specified,
 109      * otherwise {@code null}.
 110      * @return an owner window.
 111      */
 112     public Window getOwner() {
 113         return owner;
 114     }
 115 
 116     /**
 117      * Get the printing attribute class which is to be used as the "category"
 118      * for this printing attribute value.
 119      * <p>
 120      * For class {@code DialogOwner}, the category is class
 121      * {@code DialogOwner} itself.
 122      *
 123      * @return printing attribute class (category), an instance of class
 124      *         {@link Class java.lang.Class}
 125      */
 126     public final Class<? extends Attribute> getCategory() {
 127         return DialogOwner.class;
 128     }
 129 
 130     /**
 131      * Get the name of the category of which this attribute value is an
 132      * instance.
 133      * <p>
 134      * For class {@code DialogOwner}, the category name is
 135      * {@code "dialog-owner"}.
 136      *
 137      */
 138     public final String getName() {
 139         return "dialog-owner";
 140 
 141     }
 142 }