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