1 /*
   2  * Copyright (c) 2007, 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.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 import javax.swing.*;
  25 import javax.swing.tree.DefaultMutableTreeNode;
  26 import javax.swing.tree.DefaultTreeCellRenderer;
  27 import javax.swing.tree.DefaultTreeModel;
  28 import java.awt.*;
  29 import java.util.Locale;
  30 
  31 /*
  32  * @test
  33  * @summary Check that JTree constructors and methods do not throw unexpected
  34  *          exceptions in headless mode
  35  * @run main/othervm -Djava.awt.headless=true HeadlessJTree
  36  */
  37 
  38 public class HeadlessJTree {
  39     public static void main(String args[]) {
  40         JTree t;
  41 
  42         DefaultMutableTreeNode root = new DefaultMutableTreeNode("JTree");
  43         DefaultMutableTreeNode parent;
  44 
  45         parent = new DefaultMutableTreeNode("colors");
  46         root.add(parent);
  47         parent.add(new DefaultMutableTreeNode("blue"));
  48         parent.add(new DefaultMutableTreeNode("violet"));
  49         parent.add(new DefaultMutableTreeNode("red"));
  50         parent.add(new DefaultMutableTreeNode("yellow"));
  51 
  52         parent = new DefaultMutableTreeNode("sports");
  53         root.add(parent);
  54         parent.add(new DefaultMutableTreeNode("basketball"));
  55         parent.add(new DefaultMutableTreeNode("soccer"));
  56         parent.add(new DefaultMutableTreeNode("football"));
  57         parent.add(new DefaultMutableTreeNode("hockey"));
  58 
  59         parent = new DefaultMutableTreeNode("food");
  60         root.add(parent);
  61         parent.add(new DefaultMutableTreeNode("hot dogs"));
  62         parent.add(new DefaultMutableTreeNode("pizza"));
  63         parent.add(new DefaultMutableTreeNode("ravioli"));
  64         parent.add(new DefaultMutableTreeNode("bananas"));
  65 
  66         t = new JTree(root);
  67         t = new JTree(new DefaultTreeModel(root));
  68         t.setCellRenderer(new DefaultTreeCellRenderer() {
  69             public Component getTreeCellRendererComponent(
  70                     JTree tree, Object value, boolean selected, boolean expanded,
  71                     boolean leaf, int row, boolean hasFocus) {
  72                 return super.getTreeCellRendererComponent(
  73                         tree, value, selected, expanded,
  74                         leaf, row, hasFocus);
  75             }
  76         });
  77         t.getCellRenderer();
  78 
  79         t.getAccessibleContext();
  80         t.isFocusTraversable();
  81         t.setEnabled(false);
  82         t.setEnabled(true);
  83         t.requestFocus();
  84         t.requestFocusInWindow();
  85         t.getPreferredSize();
  86         t.getMaximumSize();
  87         t.getMinimumSize();
  88         t.contains(1, 2);
  89         Component c1 = t.add(new Component(){});
  90         Component c2 = t.add(new Component(){});
  91         Component c3 = t.add(new Component(){});
  92         Insets ins = t.getInsets();
  93         t.getAlignmentY();
  94         t.getAlignmentX();
  95         t.getGraphics();
  96         t.setVisible(false);
  97         t.setVisible(true);
  98         t.setForeground(Color.red);
  99         t.setBackground(Color.red);
 100         for (String font : Toolkit.getDefaultToolkit().getFontList()) {
 101             for (int j = 8; j < 17; j++) {
 102                 Font f1 = new Font(font, Font.PLAIN, j);
 103                 Font f2 = new Font(font, Font.BOLD, j);
 104                 Font f3 = new Font(font, Font.ITALIC, j);
 105                 Font f4 = new Font(font, Font.BOLD | Font.ITALIC, j);
 106 
 107                 t.setFont(f1);
 108                 t.setFont(f2);
 109                 t.setFont(f3);
 110                 t.setFont(f4);
 111 
 112                 t.getFontMetrics(f1);
 113                 t.getFontMetrics(f2);
 114                 t.getFontMetrics(f3);
 115                 t.getFontMetrics(f4);
 116             }
 117         }
 118         t.enable();
 119         t.disable();
 120         t.reshape(10, 10, 10, 10);
 121         t.getBounds(new Rectangle(1, 1, 1, 1));
 122         t.getSize(new Dimension(1, 2));
 123         t.getLocation(new Point(1, 2));
 124         t.getX();
 125         t.getY();
 126         t.getWidth();
 127         t.getHeight();
 128         t.isOpaque();
 129         t.isValidateRoot();
 130         t.isOptimizedDrawingEnabled();
 131         t.isDoubleBuffered();
 132         t.getComponentCount();
 133         t.countComponents();
 134         t.getComponent(1);
 135         t.getComponent(2);
 136         Component[] cs = t.getComponents();
 137         t.getLayout();
 138         t.setLayout(new FlowLayout());
 139         t.doLayout();
 140         t.layout();
 141         t.invalidate();
 142         t.validate();
 143         t.remove(0);
 144         t.remove(c2);
 145         t.removeAll();
 146         t.preferredSize();
 147         t.minimumSize();
 148         t.getComponentAt(1, 2);
 149         t.locate(1, 2);
 150         t.getComponentAt(new Point(1, 2));
 151         t.isFocusCycleRoot(new Container());
 152         t.transferFocusBackward();
 153         t.setName("goober");
 154         t.getName();
 155         t.getParent();
 156         t.getGraphicsConfiguration();
 157         t.getTreeLock();
 158         t.getToolkit();
 159         t.isValid();
 160         t.isDisplayable();
 161         t.isVisible();
 162         t.isShowing();
 163         t.isEnabled();
 164         t.enable(false);
 165         t.enable(true);
 166         t.enableInputMethods(false);
 167         t.enableInputMethods(true);
 168         t.show();
 169         t.show(false);
 170         t.show(true);
 171         t.hide();
 172         t.getForeground();
 173         t.isForegroundSet();
 174         t.getBackground();
 175         t.isBackgroundSet();
 176         t.getFont();
 177         t.isFontSet();
 178         Container c = new Container();
 179         c.add(t);
 180         t.getLocale();
 181         for (Locale locale : Locale.getAvailableLocales())
 182             t.setLocale(locale);
 183 
 184         t.getColorModel();
 185         t.getLocation();
 186 
 187         boolean exceptions = false;
 188         try {
 189             t.getLocationOnScreen();
 190         } catch (IllegalComponentStateException e) {
 191             exceptions = true;
 192         }
 193         if (!exceptions)
 194             throw new RuntimeException("IllegalComponentStateException did not occur when expected");
 195 
 196         t.location();
 197         t.setLocation(1, 2);
 198         t.move(1, 2);
 199         t.setLocation(new Point(1, 2));
 200         t.getSize();
 201         t.size();
 202         t.setSize(1, 32);
 203         t.resize(1, 32);
 204         t.setSize(new Dimension(1, 32));
 205         t.resize(new Dimension(1, 32));
 206         t.getBounds();
 207         t.bounds();
 208         t.setBounds(10, 10, 10, 10);
 209         t.setBounds(new Rectangle(10, 10, 10, 10));
 210         t.isLightweight();
 211         t.setCursor(new Cursor(Cursor.CROSSHAIR_CURSOR));
 212         t.getCursor();
 213         t.isCursorSet();
 214         t.inside(1, 2);
 215         t.contains(new Point(1, 2));
 216         t.isFocusable();
 217         t.setFocusable(true);
 218         t.setFocusable(false);
 219         t.transferFocus();
 220         t.getFocusCycleRootAncestor();
 221         t.nextFocus();
 222         t.transferFocusUpCycle();
 223         t.hasFocus();
 224         t.isFocusOwner();
 225         t.toString();
 226         t.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
 227         t.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
 228         t.setComponentOrientation(ComponentOrientation.UNKNOWN);
 229         t.getComponentOrientation();
 230     }
 231 }