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.dnd.DragGestureEvent;
  30 import java.awt.dnd.InvalidDnDOperationException;
  31 import java.awt.dnd.peer.DragSourceContextPeer;
  32 import java.awt.peer.*;
  33 
  34 import sun.awt.datatransfer.DataTransferer;
  35 
  36 final class LightweightPeerHolder {
  37     static final LightweightPeer lightweightMarker = new NullComponentPeer();
  38 
  39     private LightweightPeerHolder() {
  40     }
  41 }
  42 
  43 /**
  44  * Interface for component creation support in toolkits.
  45  */
  46 public interface ComponentFactory {
  47 
  48     /**
  49      * Creates a peer for a component or container. This peer is windowless and
  50      * allows the Component and Container classes to be extended directly to
  51      * create windowless components that are defined entirely in java.
  52      *
  53      * @param  target The Component to be created
  54      * @return the peer for the specified component
  55      */
  56     default LightweightPeer createComponent(Component target) {
  57         return LightweightPeerHolder.lightweightMarker;
  58     }
  59 
  60     /**
  61      * Creates this toolkit's implementation of the {@code Desktop} using the
  62      * specified peer interface.
  63      *
  64      * @param  target the desktop to be implemented
  65      * @return this toolkit's implementation of the {@code Desktop}
  66      * @throws HeadlessException if GraphicsEnvironment.isHeadless() returns
  67      *         true
  68      * @see java.awt.GraphicsEnvironment#isHeadless
  69      * @see java.awt.Desktop
  70      * @see java.awt.peer.DesktopPeer
  71      * @since 1.6
  72      */
  73     default DesktopPeer createDesktopPeer(Desktop target) {
  74         throw new HeadlessException();
  75     }
  76 
  77     /**
  78      * Creates this toolkit's implementation of {@code Button} using the
  79      * specified peer interface.
  80      *
  81      * @param  target the button to be implemented
  82      * @return this toolkit's implementation of {@code Button}
  83      * @throws HeadlessException if GraphicsEnvironment.isHeadless() returns
  84      *         true
  85      * @see java.awt.GraphicsEnvironment#isHeadless
  86      * @see java.awt.Button
  87      * @see java.awt.peer.ButtonPeer
  88      */
  89     default ButtonPeer createButton(Button target) {
  90         throw new HeadlessException();
  91     }
  92 
  93     /**
  94      * Creates this toolkit's implementation of {@code TextField} using the
  95      * specified peer interface.
  96      *
  97      * @param  target the text field to be implemented
  98      * @return this toolkit's implementation of {@code TextField}
  99      * @throws HeadlessException if GraphicsEnvironment.isHeadless() returns
 100      *         true
 101      * @see java.awt.GraphicsEnvironment#isHeadless
 102      * @see java.awt.TextField
 103      * @see java.awt.peer.TextFieldPeer
 104      */
 105     default TextFieldPeer createTextField(TextField target) {
 106         throw new HeadlessException();
 107     }
 108 
 109     /**
 110      * Creates this toolkit's implementation of {@code Label} using the
 111      * specified peer interface.
 112      *
 113      * @param  target the label to be implemented
 114      * @return this toolkit's implementation of {@code Label}
 115      * @throws HeadlessException if GraphicsEnvironment.isHeadless() returns
 116      *         true
 117      * @see java.awt.GraphicsEnvironment#isHeadless
 118      * @see java.awt.Label
 119      * @see java.awt.peer.LabelPeer
 120      */
 121     default LabelPeer createLabel(Label target) {
 122         throw new HeadlessException();
 123     }
 124 
 125     /**
 126      * Creates this toolkit's implementation of {@code List} using the specified
 127      * peer interface.
 128      *
 129      * @param  target the list to be implemented
 130      * @return this toolkit's implementation of {@code List}
 131      * @throws HeadlessException if GraphicsEnvironment.isHeadless() returns
 132      *         true
 133      * @see java.awt.GraphicsEnvironment#isHeadless
 134      * @see java.awt.List
 135      * @see java.awt.peer.ListPeer
 136      */
 137     default ListPeer createList(java.awt.List target) {
 138         throw new HeadlessException();
 139     }
 140 
 141     /**
 142      * Creates this toolkit's implementation of {@code Checkbox} using the
 143      * specified peer interface.
 144      *
 145      * @param  target the check box to be implemented
 146      * @return this toolkit's implementation of {@code Checkbox}
 147      * @throws HeadlessException if GraphicsEnvironment.isHeadless() returns
 148      *         true
 149      * @see java.awt.GraphicsEnvironment#isHeadless
 150      * @see java.awt.Checkbox
 151      * @see java.awt.peer.CheckboxPeer
 152      */
 153     default CheckboxPeer createCheckbox(Checkbox target) {
 154         throw new HeadlessException();
 155     }
 156 
 157     /**
 158      * Creates this toolkit's implementation of {@code Scrollbar} using the
 159      * specified peer interface.
 160      *
 161      * @param  target the scroll bar to be implemented
 162      * @return this toolkit's implementation of {@code Scrollbar}
 163      * @throws HeadlessException if GraphicsEnvironment.isHeadless() returns
 164      *         true
 165      * @see java.awt.GraphicsEnvironment#isHeadless
 166      * @see java.awt.Scrollbar
 167      * @see java.awt.peer.ScrollbarPeer
 168      */
 169     default ScrollbarPeer createScrollbar(Scrollbar target) {
 170         throw new HeadlessException();
 171     }
 172 
 173     /**
 174      * Creates this toolkit's implementation of {@code ScrollPane} using the
 175      * specified peer interface.
 176      *
 177      * @param  target the scroll pane to be implemented
 178      * @return this toolkit's implementation of {@code ScrollPane}
 179      * @throws HeadlessException if GraphicsEnvironment.isHeadless() returns
 180      *         true
 181      * @see java.awt.GraphicsEnvironment#isHeadless
 182      * @see java.awt.ScrollPane
 183      * @see java.awt.peer.ScrollPanePeer
 184      * @since 1.1
 185      */
 186     default ScrollPanePeer createScrollPane(ScrollPane target) {
 187         throw new HeadlessException();
 188     }
 189 
 190     /**
 191      * Creates this toolkit's implementation of {@code TextArea} using the
 192      * specified peer interface.
 193      *
 194      * @param  target the text area to be implemented
 195      * @return this toolkit's implementation of {@code TextArea}
 196      * @throws HeadlessException if GraphicsEnvironment.isHeadless() returns
 197      *         true
 198      * @see java.awt.GraphicsEnvironment#isHeadless
 199      * @see java.awt.TextArea
 200      * @see java.awt.peer.TextAreaPeer
 201      */
 202     default TextAreaPeer createTextArea(TextArea target) {
 203         throw new HeadlessException();
 204     }
 205 
 206     /**
 207      * Creates this toolkit's implementation of {@code Choice} using the
 208      * specified peer interface.
 209      *
 210      * @param  target the choice to be implemented
 211      * @return this toolkit's implementation of {@code Choice}
 212      * @throws HeadlessException if GraphicsEnvironment.isHeadless() returns
 213      *         true
 214      * @see java.awt.GraphicsEnvironment#isHeadless
 215      * @see java.awt.Choice
 216      * @see java.awt.peer.ChoicePeer
 217      */
 218     default ChoicePeer createChoice(Choice target) {
 219         throw new HeadlessException();
 220     }
 221 
 222     /**
 223      * Creates this toolkit's implementation of {@code Frame} using the
 224      * specified peer interface.
 225      *
 226      * @param  target the frame to be implemented
 227      * @return this toolkit's implementation of {@code Frame}
 228      * @throws HeadlessException if GraphicsEnvironment.isHeadless() returns
 229      *         true
 230      * @see java.awt.GraphicsEnvironment#isHeadless
 231      * @see java.awt.Frame
 232      * @see java.awt.peer.FramePeer
 233      */
 234     default FramePeer createFrame(Frame target) {
 235         throw new HeadlessException();
 236     }
 237 
 238     /**
 239      * Creates this toolkit's implementation of {@code Canvas} using the
 240      * specified peer interface.
 241      *
 242      * @param  target the canvas to be implemented
 243      * @return this toolkit's implementation of {@code Canvas}
 244      * @see java.awt.Canvas
 245      * @see java.awt.peer.CanvasPeer
 246      */
 247     default CanvasPeer createCanvas(Canvas target) {
 248         return (CanvasPeer) createComponent(target);
 249     }
 250 
 251     /**
 252      * Creates this toolkit's implementation of {@code Panel} using the
 253      * specified peer interface.
 254      *
 255      * @param  target the panel to be implemented
 256      * @return this toolkit's implementation of {@code Panel}
 257      * @see java.awt.Panel
 258      * @see java.awt.peer.PanelPeer
 259      */
 260     default PanelPeer createPanel(Panel target) {
 261         return (PanelPeer) createComponent(target);
 262     }
 263 
 264     /**
 265      * Creates this toolkit's implementation of {@code Window} using the
 266      * specified peer interface.
 267      *
 268      * @param  target the window to be implemented
 269      * @return this toolkit's implementation of {@code Window}
 270      * @throws HeadlessException if GraphicsEnvironment.isHeadless() returns
 271      *         true
 272      * @see java.awt.GraphicsEnvironment#isHeadless
 273      * @see java.awt.Window
 274      * @see java.awt.peer.WindowPeer
 275      */
 276     default WindowPeer createWindow(Window target) {
 277         throw new HeadlessException();
 278     }
 279 
 280     /**
 281      * Creates this toolkit's implementation of {@code Dialog} using the
 282      * specified peer interface.
 283      *
 284      * @param  target the dialog to be implemented
 285      * @return this toolkit's implementation of {@code Dialog}
 286      * @throws HeadlessException if GraphicsEnvironment.isHeadless() returns
 287      *         true
 288      * @see java.awt.GraphicsEnvironment#isHeadless
 289      * @see java.awt.Dialog
 290      * @see java.awt.peer.DialogPeer
 291      */
 292     default DialogPeer createDialog(Dialog target) {
 293         throw new HeadlessException();
 294     }
 295 
 296     /**
 297      * Creates this toolkit's implementation of {@code MenuBar} using the
 298      * specified peer interface.
 299      *
 300      * @param  target the menu bar to be implemented
 301      * @return this toolkit's implementation of {@code MenuBar}
 302      * @throws HeadlessException if GraphicsEnvironment.isHeadless() returns
 303      *         true
 304      * @see java.awt.GraphicsEnvironment#isHeadless
 305      * @see java.awt.MenuBar
 306      * @see java.awt.peer.MenuBarPeer
 307      */
 308     default MenuBarPeer createMenuBar(MenuBar target) {
 309         throw new HeadlessException();
 310     }
 311 
 312     /**
 313      * Creates this toolkit's implementation of {@code Menu} using the specified
 314      * peer interface.
 315      *
 316      * @param  target the menu to be implemented
 317      * @return this toolkit's implementation of {@code Menu}
 318      * @throws HeadlessException if GraphicsEnvironment.isHeadless() returns
 319      *         true
 320      * @see java.awt.GraphicsEnvironment#isHeadless
 321      * @see java.awt.Menu
 322      * @see java.awt.peer.MenuPeer
 323      */
 324     default MenuPeer createMenu(Menu target) {
 325         throw new HeadlessException();
 326     }
 327 
 328     /**
 329      * Creates this toolkit's implementation of {@code PopupMenu} using the
 330      * specified peer interface.
 331      *
 332      * @param  target the popup menu to be implemented
 333      * @return this toolkit's implementation of {@code PopupMenu}
 334      * @throws HeadlessException if GraphicsEnvironment.isHeadless() returns
 335      *         true
 336      * @see java.awt.GraphicsEnvironment#isHeadless
 337      * @see java.awt.PopupMenu
 338      * @see java.awt.peer.PopupMenuPeer
 339      * @since 1.1
 340      */
 341     default PopupMenuPeer createPopupMenu(PopupMenu target) {
 342         throw new HeadlessException();
 343     }
 344 
 345     /**
 346      * Creates this toolkit's implementation of {@code MenuItem} using the
 347      * specified peer interface.
 348      *
 349      * @param  target the menu item to be implemented
 350      * @return this toolkit's implementation of {@code MenuItem}
 351      * @throws HeadlessException if GraphicsEnvironment.isHeadless() returns
 352      *         true
 353      * @see java.awt.GraphicsEnvironment#isHeadless
 354      * @see java.awt.MenuItem
 355      * @see java.awt.peer.MenuItemPeer
 356      */
 357     default MenuItemPeer createMenuItem(MenuItem target) {
 358         throw new HeadlessException();
 359     }
 360 
 361     /**
 362      * Creates this toolkit's implementation of {@code FileDialog} using the
 363      * specified peer interface.
 364      *
 365      * @param  target the file dialog to be implemented
 366      * @return this toolkit's implementation of {@code FileDialog}
 367      * @throws HeadlessException if GraphicsEnvironment.isHeadless() returns
 368      *         true
 369      * @see java.awt.GraphicsEnvironment#isHeadless
 370      * @see java.awt.FileDialog
 371      * @see java.awt.peer.FileDialogPeer
 372      */
 373     default FileDialogPeer createFileDialog(FileDialog target) {
 374         throw new HeadlessException();
 375     }
 376 
 377     /**
 378      * Creates this toolkit's implementation of {@code CheckboxMenuItem} using
 379      * the specified peer interface.
 380      *
 381      * @param  target the checkbox menu item to be implemented
 382      * @return this toolkit's implementation of {@code CheckboxMenuItem}
 383      * @throws HeadlessException if GraphicsEnvironment.isHeadless() returns
 384      *         true
 385      * @see java.awt.GraphicsEnvironment#isHeadless
 386      * @see java.awt.CheckboxMenuItem
 387      * @see java.awt.peer.CheckboxMenuItemPeer
 388      */
 389     default CheckboxMenuItemPeer createCheckboxMenuItem(CheckboxMenuItem target) {
 390         throw new HeadlessException();
 391     }
 392 
 393     /**
 394      * Creates the peer for a DragSourceContext. Always throws
 395      * InvalidDndOperationException if GraphicsEnvironment.isHeadless() returns
 396      * true.
 397      *
 398      * @param  dge the {@code DragGestureEvent}
 399      * @return the peer created
 400      * @see java.awt.GraphicsEnvironment#isHeadless
 401      */
 402     default DragSourceContextPeer createDragSourceContextPeer(DragGestureEvent dge) {
 403         throw new InvalidDnDOperationException("Headless environment");
 404     }
 405 
 406     /**
 407      * Creates this toolkit's implementation of {@code Font} using the specified
 408      * peer interface.
 409      *
 410      * @param name the font to be implemented
 411      * @param style the style of the font, such as {@code PLAIN}, {@code BOLD},
 412      *        {@code ITALIC}, or a combination
 413      * @return this toolkit's implementation of {@code Font}
 414      * @see java.awt.Font
 415      * @see java.awt.peer.FontPeer
 416      * @see java.awt.GraphicsEnvironment#getAllFonts
 417      */
 418     default FontPeer getFontPeer(String name, int style) {
 419         return null;
 420     }
 421 
 422     default RobotPeer createRobot(Robot target, GraphicsDevice screen)
 423             throws AWTException {
 424         throw new HeadlessException();
 425     }
 426 
 427     default DataTransferer getDataTransferer() {
 428         return null;
 429     }
 430 
 431     /**
 432      * Obtains this toolkit's implementation of helper class for {@code
 433      * MouseInfo} operations.
 434      *
 435      * @return this toolkit's implementation of helper for {@code MouseInfo}
 436      * @throws UnsupportedOperationException if this operation is not
 437      *         implemented
 438      * @see java.awt.peer.MouseInfoPeer
 439      * @see java.awt.MouseInfo
 440      * @since 1.5
 441      */
 442     default MouseInfoPeer getMouseInfoPeer() {
 443         throw new UnsupportedOperationException("Not implemented");
 444     }
 445 }