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