1 /*
   2  * Copyright (c) 2000, 2012, 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.dnd.*;
  30 import java.awt.dnd.peer.DragSourceContextPeer;
  31 import java.awt.event.*;
  32 import java.awt.font.TextAttribute;
  33 import java.awt.im.InputMethodHighlight;
  34 import java.awt.image.*;
  35 import java.awt.datatransfer.Clipboard;
  36 import java.awt.peer.*;
  37 import java.beans.PropertyChangeListener;
  38 import java.net.URL;
  39 import java.util.Map;
  40 import java.util.Properties;
  41 
  42 public class HeadlessToolkit extends Toolkit
  43     implements ComponentFactory, KeyboardFocusManagerPeerProvider {
  44 
  45     private static final KeyboardFocusManagerPeer kfmPeer = new KeyboardFocusManagerPeer() {
  46         public void setCurrentFocusedWindow(Window win) {}
  47         public Window getCurrentFocusedWindow() { return null; }
  48         public void setCurrentFocusOwner(Component comp) {}
  49         public Component getCurrentFocusOwner() { return null; }
  50         public void clearGlobalFocusOwner(Window activeWindow) {}
  51     };
  52 
  53     private Toolkit tk;
  54     private ComponentFactory componentFactory;
  55 
  56     public HeadlessToolkit(Toolkit tk) {
  57         this.tk = tk;
  58         if (tk instanceof ComponentFactory) {
  59             componentFactory = (ComponentFactory)tk;
  60         }
  61     }
  62 
  63     public Toolkit getUnderlyingToolkit() {
  64         return tk;
  65     }
  66 
  67     /*
  68      * Component peer objects.
  69      */
  70 
  71     /* Lightweight implementation of Canvas and Panel */
  72 
  73     public CanvasPeer createCanvas(Canvas target) {
  74         return (CanvasPeer)createComponent(target);
  75     }
  76 
  77     public PanelPeer createPanel(Panel target) {
  78         return (PanelPeer)createComponent(target);
  79     }
  80 
  81     /*
  82      * Component peer objects - unsupported.
  83      */
  84 
  85     public WindowPeer createWindow(Window target)
  86         throws HeadlessException {
  87         throw new HeadlessException();
  88     }
  89 
  90     public FramePeer createFrame(Frame target)
  91         throws HeadlessException {
  92         throw new HeadlessException();
  93     }
  94 
  95     public DialogPeer createDialog(Dialog target)
  96         throws HeadlessException {
  97         throw new HeadlessException();
  98     }
  99 
 100     public ButtonPeer createButton(Button target)
 101         throws HeadlessException {
 102         throw new HeadlessException();
 103     }
 104 
 105     public TextFieldPeer createTextField(TextField target)
 106         throws HeadlessException {
 107         throw new HeadlessException();
 108     }
 109 
 110     public ChoicePeer createChoice(Choice target)
 111         throws HeadlessException {
 112         throw new HeadlessException();
 113     }
 114 
 115     public LabelPeer createLabel(Label target)
 116         throws HeadlessException {
 117         throw new HeadlessException();
 118     }
 119 
 120     public ListPeer createList(List target)
 121         throws HeadlessException {
 122         throw new HeadlessException();
 123     }
 124 
 125     public CheckboxPeer createCheckbox(Checkbox target)
 126         throws HeadlessException {
 127         throw new HeadlessException();
 128     }
 129 
 130     public ScrollbarPeer createScrollbar(Scrollbar target)
 131         throws HeadlessException {
 132         throw new HeadlessException();
 133     }
 134 
 135     public ScrollPanePeer createScrollPane(ScrollPane target)
 136         throws HeadlessException {
 137         throw new HeadlessException();
 138     }
 139 
 140     public TextAreaPeer createTextArea(TextArea target)
 141         throws HeadlessException {
 142         throw new HeadlessException();
 143     }
 144 
 145     public FileDialogPeer createFileDialog(FileDialog target)
 146         throws HeadlessException {
 147         throw new HeadlessException();
 148     }
 149 
 150     public MenuBarPeer createMenuBar(MenuBar target)
 151         throws HeadlessException {
 152         throw new HeadlessException();
 153     }
 154 
 155     public MenuPeer createMenu(Menu target)
 156         throws HeadlessException {
 157         throw new HeadlessException();
 158     }
 159 
 160     public PopupMenuPeer createPopupMenu(PopupMenu target)
 161         throws HeadlessException {
 162         throw new HeadlessException();
 163     }
 164 
 165     public MenuItemPeer createMenuItem(MenuItem target)
 166         throws HeadlessException {
 167         throw new HeadlessException();
 168     }
 169 
 170     public CheckboxMenuItemPeer createCheckboxMenuItem(CheckboxMenuItem target)
 171         throws HeadlessException {
 172         throw new HeadlessException();
 173     }
 174 
 175     public DragSourceContextPeer createDragSourceContextPeer(
 176         DragGestureEvent dge)
 177         throws InvalidDnDOperationException {
 178         throw new InvalidDnDOperationException("Headless environment");
 179     }
 180 
 181     public RobotPeer createRobot(Robot target, GraphicsDevice screen)
 182         throws AWTException, HeadlessException {
 183         throw new HeadlessException();
 184     }
 185 
 186     public KeyboardFocusManagerPeer getKeyboardFocusManagerPeer() {
 187         // See 6833019.
 188         return kfmPeer;
 189     }
 190 
 191     public TrayIconPeer createTrayIcon(TrayIcon target)
 192       throws HeadlessException {
 193         throw new HeadlessException();
 194     }
 195 
 196     public SystemTrayPeer createSystemTray(SystemTray target)
 197       throws HeadlessException {
 198         throw new HeadlessException();
 199     }
 200 
 201     public boolean isTraySupported() {
 202         return false;
 203     }
 204 
 205     public GlobalCursorManager getGlobalCursorManager()
 206         throws HeadlessException {
 207         throw new HeadlessException();
 208     }
 209 
 210     /*
 211      * Headless toolkit - unsupported.
 212      */
 213     protected void loadSystemColors(int[] systemColors)
 214         throws HeadlessException {
 215         throw new HeadlessException();
 216     }
 217 
 218     public ColorModel getColorModel()
 219         throws HeadlessException {
 220         throw new HeadlessException();
 221     }
 222 
 223     public int getScreenResolution()
 224         throws HeadlessException {
 225         throw new HeadlessException();
 226     }
 227 
 228     public Map<TextAttribute, ?> mapInputMethodHighlight(InputMethodHighlight highlight)
 229         throws HeadlessException {
 230         throw new HeadlessException();
 231     }
 232 
 233     public int getMenuShortcutKeyMask()
 234         throws HeadlessException {
 235         throw new HeadlessException();
 236     }
 237 
 238     public boolean getLockingKeyState(int keyCode)
 239         throws UnsupportedOperationException {
 240         throw new HeadlessException();
 241     }
 242 
 243     public void setLockingKeyState(int keyCode, boolean on)
 244         throws UnsupportedOperationException {
 245         throw new HeadlessException();
 246     }
 247 
 248     public Cursor createCustomCursor(Image cursor, Point hotSpot, String name)
 249         throws IndexOutOfBoundsException, HeadlessException {
 250         throw new HeadlessException();
 251     }
 252 
 253     public Dimension getBestCursorSize(int preferredWidth, int preferredHeight)
 254         throws HeadlessException {
 255         throw new HeadlessException();
 256     }
 257 
 258     public int getMaximumCursorColors()
 259         throws HeadlessException {
 260         throw new HeadlessException();
 261     }
 262 
 263     public <T extends DragGestureRecognizer> T
 264         createDragGestureRecognizer(Class<T> abstractRecognizerClass,
 265                                     DragSource ds, Component c,
 266                                     int srcActions, DragGestureListener dgl)
 267     {
 268         return null;
 269     }
 270 
 271     public int getScreenHeight()
 272         throws HeadlessException {
 273         throw new HeadlessException();
 274     }
 275 
 276     public int getScreenWidth()
 277         throws HeadlessException {
 278         throw new HeadlessException();
 279     }
 280 
 281     public Dimension getScreenSize()
 282         throws HeadlessException {
 283         throw new HeadlessException();
 284     }
 285 
 286     public Insets getScreenInsets(GraphicsConfiguration gc)
 287         throws HeadlessException {
 288         throw new HeadlessException();
 289     }
 290 
 291     public void setDynamicLayout(boolean dynamic)
 292         throws HeadlessException {
 293         throw new HeadlessException();
 294     }
 295 
 296     protected boolean isDynamicLayoutSet()
 297         throws HeadlessException {
 298         throw new HeadlessException();
 299     }
 300 
 301     public boolean isDynamicLayoutActive()
 302         throws HeadlessException {
 303         throw new HeadlessException();
 304     }
 305 
 306     public Clipboard getSystemClipboard()
 307         throws HeadlessException {
 308         throw new HeadlessException();
 309     }
 310 
 311     /*
 312      * Printing
 313      */
 314     public PrintJob getPrintJob(Frame frame, String jobtitle,
 315         JobAttributes jobAttributes,
 316         PageAttributes pageAttributes) {
 317         if (frame != null) {
 318             // Should never happen
 319             throw new HeadlessException();
 320         }
 321         throw new NullPointerException("frame must not be null");
 322     }
 323 
 324     public PrintJob getPrintJob(Frame frame, String doctitle, Properties props)
 325     {
 326         if (frame != null) {
 327             // Should never happen
 328             throw new HeadlessException();
 329         }
 330         throw new NullPointerException("frame must not be null");
 331     }
 332 
 333     /*
 334      * Headless toolkit - supported.
 335      */
 336 
 337     public void sync() {
 338         // Do nothing
 339     }
 340 
 341     public void beep() {
 342         // Send alert character
 343         System.out.write(0x07);
 344     }
 345 
 346     /*
 347      * Event Queue
 348      */
 349     public EventQueue getSystemEventQueueImpl() {
 350         return SunToolkit.getSystemEventQueueImplPP();
 351     }
 352 
 353     /*
 354      * Images.
 355      */
 356     public int checkImage(Image img, int w, int h, ImageObserver o) {
 357         return tk.checkImage(img, w, h, o);
 358     }
 359 
 360     public boolean prepareImage(
 361         Image img, int w, int h, ImageObserver o) {
 362         return tk.prepareImage(img, w, h, o);
 363     }
 364 
 365     public Image getImage(String filename) {
 366         return tk.getImage(filename);
 367     }
 368 
 369     public Image getImage(URL url) {
 370         return tk.getImage(url);
 371     }
 372 
 373     public Image createImage(String filename) {
 374         return tk.createImage(filename);
 375     }
 376 
 377     public Image createImage(URL url) {
 378         return tk.createImage(url);
 379     }
 380 
 381     public Image createImage(byte[] data, int offset, int length) {
 382         return tk.createImage(data, offset, length);
 383     }
 384 
 385     public Image createImage(ImageProducer producer) {
 386         return tk.createImage(producer);
 387     }
 388 
 389     public Image createImage(byte[] imagedata) {
 390         return tk.createImage(imagedata);
 391     }
 392 
 393 
 394     /*
 395      * Fonts
 396      */
 397     @SuppressWarnings("deprecation")
 398     public FontPeer getFontPeer(String name, int style) {
 399         if (componentFactory != null) {
 400             return componentFactory.getFontPeer(name, style);
 401         }
 402         return null;
 403     }
 404 
 405     @SuppressWarnings("deprecation")
 406     public FontMetrics getFontMetrics(Font font) {
 407         return tk.getFontMetrics(font);
 408     }
 409 
 410     @SuppressWarnings("deprecation")
 411     public String[] getFontList() {
 412         return tk.getFontList();
 413     }
 414 
 415     /*
 416      * Desktop properties
 417      */
 418 
 419     public void addPropertyChangeListener(String name,
 420         PropertyChangeListener pcl) {
 421         tk.addPropertyChangeListener(name, pcl);
 422     }
 423 
 424     public void removePropertyChangeListener(String name,
 425         PropertyChangeListener pcl) {
 426         tk.removePropertyChangeListener(name, pcl);
 427     }
 428 
 429     /*
 430      * Modality
 431      */
 432     public boolean isModalityTypeSupported(Dialog.ModalityType modalityType) {
 433         return false;
 434     }
 435 
 436     public boolean isModalExclusionTypeSupported(Dialog.ModalExclusionType exclusionType) {
 437         return false;
 438     }
 439 
 440     /*
 441      * Always on top
 442      */
 443     public boolean isAlwaysOnTopSupported() {
 444         return false;
 445     }
 446 
 447     /*
 448      * AWT Event listeners
 449      */
 450 
 451     public void addAWTEventListener(AWTEventListener listener,
 452         long eventMask) {
 453         tk.addAWTEventListener(listener, eventMask);
 454     }
 455 
 456     public void removeAWTEventListener(AWTEventListener listener) {
 457         tk.removeAWTEventListener(listener);
 458     }
 459 
 460     public AWTEventListener[] getAWTEventListeners() {
 461         return tk.getAWTEventListeners();
 462     }
 463 
 464     public AWTEventListener[] getAWTEventListeners(long eventMask) {
 465         return tk.getAWTEventListeners(eventMask);
 466     }
 467 
 468     public boolean isDesktopSupported() {
 469         return false;
 470     }
 471 
 472     public DesktopPeer createDesktopPeer(Desktop target)
 473     throws HeadlessException{
 474         throw new HeadlessException();
 475     }
 476 
 477     public boolean areExtraMouseButtonsEnabled() throws HeadlessException{
 478         throw new HeadlessException();
 479     }
 480 }