1 /*
   2  * Copyright (c) 2000, 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;
  27 
  28 import java.awt.*;
  29 import java.awt.datatransfer.Clipboard;
  30 import java.awt.dnd.DragGestureListener;
  31 import java.awt.dnd.DragGestureRecognizer;
  32 import java.awt.dnd.DragSource;
  33 import java.awt.event.AWTEventListener;
  34 import java.awt.font.TextAttribute;
  35 import java.awt.im.InputMethodHighlight;
  36 import java.awt.image.ColorModel;
  37 import java.awt.image.ImageObserver;
  38 import java.awt.image.ImageProducer;
  39 import java.awt.peer.FontPeer;
  40 import java.awt.peer.KeyboardFocusManagerPeer;
  41 import java.awt.peer.SystemTrayPeer;
  42 import java.awt.peer.TrayIconPeer;
  43 import java.beans.PropertyChangeListener;
  44 import java.net.URL;
  45 import java.util.Map;
  46 import java.util.Properties;
  47 
  48 public final class HeadlessToolkit extends Toolkit
  49     implements ComponentFactory, KeyboardFocusManagerPeerProvider {
  50 
  51     private static final KeyboardFocusManagerPeer kfmPeer = new KeyboardFocusManagerPeer() {
  52         @Override
  53         public void setCurrentFocusedWindow(Window win) {}
  54         @Override
  55         public Window getCurrentFocusedWindow() { return null; }
  56         @Override
  57         public void setCurrentFocusOwner(Component comp) {}
  58         @Override
  59         public Component getCurrentFocusOwner() { return null; }
  60         @Override
  61         public void clearGlobalFocusOwner(Window activeWindow) {}
  62     };
  63 
  64     private final Toolkit tk;
  65     private ComponentFactory componentFactory;
  66 
  67     public HeadlessToolkit(Toolkit tk) {
  68         this.tk = tk;
  69         if (tk instanceof ComponentFactory) {
  70             componentFactory = (ComponentFactory)tk;
  71         }
  72     }
  73 
  74     public Toolkit getUnderlyingToolkit() {
  75         return tk;
  76     }
  77 
  78     @Override
  79     public KeyboardFocusManagerPeer getKeyboardFocusManagerPeer() {
  80         // See 6833019.
  81         return kfmPeer;
  82     }
  83 
  84     public TrayIconPeer createTrayIcon(TrayIcon target)
  85       throws HeadlessException {
  86         throw new HeadlessException();
  87     }
  88 
  89     public SystemTrayPeer createSystemTray(SystemTray target)
  90       throws HeadlessException {
  91         throw new HeadlessException();
  92     }
  93 
  94     public boolean isTraySupported() {
  95         return false;
  96     }
  97 
  98     public GlobalCursorManager getGlobalCursorManager()
  99         throws HeadlessException {
 100         throw new HeadlessException();
 101     }
 102 
 103     /*
 104      * Headless toolkit - unsupported.
 105      */
 106     @Override
 107     protected void loadSystemColors(int[] systemColors)
 108         throws HeadlessException {
 109         throw new HeadlessException();
 110     }
 111 
 112     @Override
 113     public ColorModel getColorModel()
 114         throws HeadlessException {
 115         throw new HeadlessException();
 116     }
 117 
 118     @Override
 119     public int getScreenResolution()
 120         throws HeadlessException {
 121         throw new HeadlessException();
 122     }
 123 
 124     @Override
 125     public Map<TextAttribute, ?> mapInputMethodHighlight(InputMethodHighlight highlight)
 126         throws HeadlessException {
 127         throw new HeadlessException();
 128     }
 129 
 130     @Override
 131     public int getMenuShortcutKeyMask()
 132         throws HeadlessException {
 133         throw new HeadlessException();
 134     }
 135 
 136     @Override
 137     public boolean getLockingKeyState(int keyCode)
 138         throws UnsupportedOperationException {
 139         throw new HeadlessException();
 140     }
 141 
 142     @Override
 143     public void setLockingKeyState(int keyCode, boolean on)
 144         throws UnsupportedOperationException {
 145         throw new HeadlessException();
 146     }
 147 
 148     @Override
 149     public Cursor createCustomCursor(Image cursor, Point hotSpot, String name)
 150         throws IndexOutOfBoundsException, HeadlessException {
 151         throw new HeadlessException();
 152     }
 153 
 154     @Override
 155     public Dimension getBestCursorSize(int preferredWidth, int preferredHeight)
 156         throws HeadlessException {
 157         throw new HeadlessException();
 158     }
 159 
 160     @Override
 161     public int getMaximumCursorColors()
 162         throws HeadlessException {
 163         throw new HeadlessException();
 164     }
 165 
 166     @Override
 167     public <T extends DragGestureRecognizer> T
 168         createDragGestureRecognizer(Class<T> abstractRecognizerClass,
 169                                     DragSource ds, Component c,
 170                                     int srcActions, DragGestureListener dgl)
 171     {
 172         return null;
 173     }
 174 
 175     @Override
 176     public Dimension getScreenSize()
 177         throws HeadlessException {
 178         throw new HeadlessException();
 179     }
 180 
 181     @Override
 182     public Insets getScreenInsets(GraphicsConfiguration gc)
 183         throws HeadlessException {
 184         throw new HeadlessException();
 185     }
 186 
 187     @Override
 188     public void setDynamicLayout(boolean dynamic)
 189         throws HeadlessException {
 190         throw new HeadlessException();
 191     }
 192 
 193     @Override
 194     protected boolean isDynamicLayoutSet()
 195         throws HeadlessException {
 196         throw new HeadlessException();
 197     }
 198 
 199     @Override
 200     public boolean isDynamicLayoutActive()
 201         throws HeadlessException {
 202         throw new HeadlessException();
 203     }
 204 
 205     @Override
 206     public Clipboard getSystemClipboard()
 207         throws HeadlessException {
 208         throw new HeadlessException();
 209     }
 210 
 211     /*
 212      * Printing
 213      */
 214     @Override
 215     public PrintJob getPrintJob(Frame frame, String jobtitle,
 216         JobAttributes jobAttributes,
 217         PageAttributes pageAttributes) {
 218         if (frame != null) {
 219             // Should never happen
 220             throw new HeadlessException();
 221         }
 222         throw new NullPointerException("frame must not be null");
 223     }
 224 
 225     @Override
 226     public PrintJob getPrintJob(Frame frame, String doctitle, Properties props)
 227     {
 228         if (frame != null) {
 229             // Should never happen
 230             throw new HeadlessException();
 231         }
 232         throw new NullPointerException("frame must not be null");
 233     }
 234 
 235     /*
 236      * Headless toolkit - supported.
 237      */
 238 
 239     @Override
 240     public void sync() {
 241         // Do nothing
 242     }
 243 
 244     @Override
 245     public void beep() {
 246         // Send alert character
 247         System.out.write(0x07);
 248     }
 249 
 250     /*
 251      * Event Queue
 252      */
 253     @Override
 254     public EventQueue getSystemEventQueueImpl() {
 255         return SunToolkit.getSystemEventQueueImplPP();
 256     }
 257 
 258     /*
 259      * Images.
 260      */
 261     @Override
 262     public int checkImage(Image img, int w, int h, ImageObserver o) {
 263         return tk.checkImage(img, w, h, o);
 264     }
 265 
 266     @Override
 267     public boolean prepareImage(
 268         Image img, int w, int h, ImageObserver o) {
 269         return tk.prepareImage(img, w, h, o);
 270     }
 271 
 272     @Override
 273     public Image getImage(String filename) {
 274         return tk.getImage(filename);
 275     }
 276 
 277     @Override
 278     public Image getImage(URL url) {
 279         return tk.getImage(url);
 280     }
 281 
 282     @Override
 283     public Image createImage(String filename) {
 284         return tk.createImage(filename);
 285     }
 286 
 287     @Override
 288     public Image createImage(URL url) {
 289         return tk.createImage(url);
 290     }
 291 
 292     @Override
 293     public Image createImage(byte[] data, int offset, int length) {
 294         return tk.createImage(data, offset, length);
 295     }
 296 
 297     @Override
 298     public Image createImage(ImageProducer producer) {
 299         return tk.createImage(producer);
 300     }
 301 
 302     @Override
 303     public Image createImage(byte[] imagedata) {
 304         return tk.createImage(imagedata);
 305     }
 306 
 307 
 308     /*
 309      * Fonts
 310      */
 311     @Override
 312     public FontPeer getFontPeer(String name, int style) {
 313         if (componentFactory != null) {
 314             return componentFactory.getFontPeer(name, style);
 315         }
 316         return null;
 317     }
 318 
 319     @Override
 320     @SuppressWarnings("deprecation")
 321     public FontMetrics getFontMetrics(Font font) {
 322         return tk.getFontMetrics(font);
 323     }
 324 
 325     @Override
 326     @SuppressWarnings("deprecation")
 327     public String[] getFontList() {
 328         return tk.getFontList();
 329     }
 330 
 331     /*
 332      * Desktop properties
 333      */
 334 
 335     @Override
 336     public void addPropertyChangeListener(String name,
 337         PropertyChangeListener pcl) {
 338         tk.addPropertyChangeListener(name, pcl);
 339     }
 340 
 341     @Override
 342     public void removePropertyChangeListener(String name,
 343         PropertyChangeListener pcl) {
 344         tk.removePropertyChangeListener(name, pcl);
 345     }
 346 
 347     /*
 348      * Modality
 349      */
 350     @Override
 351     public boolean isModalityTypeSupported(Dialog.ModalityType modalityType) {
 352         return false;
 353     }
 354 
 355     @Override
 356     public boolean isModalExclusionTypeSupported(Dialog.ModalExclusionType exclusionType) {
 357         return false;
 358     }
 359 
 360     /*
 361      * Always on top
 362      */
 363     @Override
 364     public boolean isAlwaysOnTopSupported() {
 365         return false;
 366     }
 367 
 368     /*
 369      * AWT Event listeners
 370      */
 371 
 372     @Override
 373     public void addAWTEventListener(AWTEventListener listener,
 374         long eventMask) {
 375         tk.addAWTEventListener(listener, eventMask);
 376     }
 377 
 378     @Override
 379     public void removeAWTEventListener(AWTEventListener listener) {
 380         tk.removeAWTEventListener(listener);
 381     }
 382 
 383     @Override
 384     public AWTEventListener[] getAWTEventListeners() {
 385         return tk.getAWTEventListeners();
 386     }
 387 
 388     @Override
 389     public AWTEventListener[] getAWTEventListeners(long eventMask) {
 390         return tk.getAWTEventListeners(eventMask);
 391     }
 392 
 393     public boolean isDesktopSupported() {
 394         return false;
 395     }
 396 
 397     @Override
 398     public boolean areExtraMouseButtonsEnabled() throws HeadlessException{
 399         throw new HeadlessException();
 400     }
 401 }