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