1 /*
   2  * Copyright (c) 2016 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 /**
  25  * test of mouse move messages to lightweight components
  26  * @bug 4059999 8005918
  27  * @author Jeff.Dunn
  28  * @run applet/manual=yesno LightweightEventTest.html
  29  */
  30 
  31 
  32 import java.applet.*;
  33 import java.awt.*;
  34 import java.awt.event.*;
  35 import java.lang.Exception;
  36 import java.io.PrintStream;
  37 
  38 
  39 //Manual tests should run as applet tests if possible because they
  40 // get the pass and fail buttons handled for us, and no worries about
  41 // dealing with the main thread returning before the test is complete.
  42 
  43 public class LightweightEventTest extends Applet
  44  {
  45    static Font  desiredFont = null;
  46 
  47    public void init()
  48     {
  49       //here, create the items that will be tested for correct
  50       // behavior
  51         EventBug e = new EventBug();
  52         Button b = (Button)e.add("Center", new Button("Heavy"));
  53 
  54         EventBug e1 = new EventBug();
  55         BorderedLabel b1 = (BorderedLabel)e1.add("Center", new BorderedLabel("Lite"));
  56 
  57         e.addListeners(b);
  58         e1.addListeners(b1);
  59 
  60         e1.setLocation(200,0);
  61         e.setVisible(true);
  62         e1.setVisible(true);
  63 
  64      }//End  init()
  65 
  66  }// class LightweightEventTest
  67 
  68 
  69 /**
  70  * Lightweight component
  71  */
  72 class BorderedLabel extends Component {
  73 
  74     boolean superIsButton = false;
  75     String labelString;
  76 
  77     BorderedLabel(String labelString) {
  78         this.labelString = labelString;
  79 
  80         Component thisComponent = this;
  81         superIsButton = (thisComponent instanceof Button);
  82         if(superIsButton) {
  83             ((Button)thisComponent).setLabel(labelString);
  84         }
  85     }
  86 
  87     public Dimension getMinimumSize() {
  88         Dimension minSize = new Dimension();
  89 
  90         if (superIsButton) {
  91             minSize = super.getMinimumSize();
  92         } else {
  93 
  94             Graphics g = getGraphics();
  95             FontMetrics metrics = g.getFontMetrics();
  96 
  97             minSize.width = metrics.stringWidth(labelString) + 14;
  98             minSize.height = metrics.getMaxAscent() + metrics.getMaxDescent() + 9;
  99 
 100             g.dispose(); g = null;
 101         }
 102         //System.out.println("minimumSize = "+minSize);
 103         return minSize;
 104     }
 105 
 106     public Dimension getPreferredSize() {
 107         Dimension prefSize = new Dimension();
 108         if (superIsButton) {
 109             prefSize = super.getPreferredSize();
 110         } else {
 111             prefSize = getMinimumSize();
 112         }
 113         //System.out.println("preferredSize = "+prefSize);
 114         return prefSize;
 115     }
 116 
 117     public void paint(Graphics g){
 118 
 119         super.paint(g);
 120         Rectangle bounds = getBounds();
 121         if (superIsButton) {
 122             return;
 123         }
 124         Dimension size = getSize();
 125         Color oldColor = g.getColor();
 126 
 127         // draw border
 128         g.setColor(getBackground());
 129         g.fill3DRect(0,0, size.width, size.height, false);
 130         g.fill3DRect(3,3, size.width-6, size.height-6, true);
 131 
 132         // draw text
 133         FontMetrics metrics = g.getFontMetrics();
 134         int centerX = size.width/2;
 135         int centerY = size.height/2;
 136         int textX = centerX - (metrics.stringWidth(labelString)/2);
 137         int textY = centerY + ((metrics.getMaxAscent()+metrics.getMaxDescent())/2);
 138         g.setColor(getForeground());
 139         g.drawString(labelString, textX, textY);
 140 
 141 
 142         g.setColor(oldColor);
 143     }
 144 } // class BorderedLabel
 145 
 146 class EventBug extends Container {
 147     Frame w;
 148     int frameEnters = 0;
 149     int frameExits = 0;
 150     int buttonEnters = 0;
 151     int buttonExits = 0;
 152 
 153     public EventBug() {
 154         super();
 155         this.w = new Frame();
 156         w.setLayout(new BorderLayout());
 157         this.setLayout(new BorderLayout());
 158         w.add("Center", this);
 159         w.pack();
 160         w.show();
 161     }
 162 
 163     public Dimension getPreferredSize() {
 164         return new Dimension(100,100);
 165     }
 166     
 167     public Insets getInsets()
 168     {
 169        return new Insets(20,20,20,20);
 170     }
 171 
 172     // Forward to the Window
 173     public void setLocation(int x, int y) { w.setLocation(x,y); }
 174     public void setVisible(boolean b) { w.setVisible(b); }
 175 
 176     // Add listeners to Frame and button
 177     public void addListeners(Component b) {
 178         b.setName("Button");
 179         b.addMouseListener(new MouseAdapter() {
 180             public void mouseEntered(MouseEvent e) {
 181                 SysOut.println("Button Enter");
 182                 buttonEnters++;
 183             }
 184             public void mouseExited(MouseEvent e) {
 185                 SysOut.println("Button Exit");
 186                 buttonExits++;
 187             }
 188             public void mouseClicked(MouseEvent e) {
 189                 SysOut.println("Button enter = "
 190                                    + buttonEnters +
 191                                    " Button Exit = "
 192                                    + buttonExits +
 193                                    " Frame enter = "
 194                                    + frameEnters +
 195                                    " Frame Exit = "
 196                                    + frameExits);
 197             }
 198         });
 199         w.addMouseListener(new MouseAdapter() {
 200             public void mouseEntered(MouseEvent e) {
 201                 SysOut.println("Frame Enter");
 202                 frameEnters++;
 203             }
 204             public void mouseExited(MouseEvent e) {
 205                 SysOut.println("Frame Exit");
 206                 frameExits++;
 207             }
 208         });
 209     }
 210 
 211 } // class EventBug
 212 
 213 
 214 class TestDialog extends Dialog
 215  {
 216 
 217    TextArea output;
 218    TextField display;
 219 
 220    public TestDialog(Frame frame, String name, String[] message)
 221     {
 222       super(frame, name);
 223       int maxStringLength = 0;
 224       for (int i=0; i<message.length; i++)
 225        {
 226          maxStringLength = Math.max(maxStringLength, message[i].length());
 227        }
 228       output = new TextArea(message.length, maxStringLength);
 229       add("North", output);
 230       for (int i=0; i<message.length; i++)
 231        {
 232          if (i==(message.length-1)) output.append(message[i]);
 233 
 234          else output.append(message[i] + "\n");
 235        }
 236 
 237       display = new TextField( 32 );
 238       add("South", display);
 239 
 240       pack();
 241 
 242       show();
 243     }//End TestDialog()
 244 
 245    public void displayMessage( String messageIn )
 246     {
 247       display.setText( messageIn );
 248     }
 249  }
 250 
 251 
 252 class SysOut
 253  {
 254    private static TestDialog dialog;
 255 
 256    //Static initializer will automatically pop up the dialog with
 257    // the instructions for the user in it -- no instantiation
 258    // needed!
 259    static
 260     {
 261       //need to pop up a window with instructions for the user
 262       String[] message =
 263        {
 264          "[ There are 3 steps to this test. ]",
 265          "\n1. You should see two frames (application windows).",
 266          "\n2. Move the mouse across both frames.",
 267          "\n3. As you move the mouse in and out of the frames, you should",
 268          "\n   see 'Button Enter' and 'Frame Enter' messages. Both frames",
 269          "\n   should produce exactly the same output."
 270        };
 271 
 272       dialog = new TestDialog( new Frame(), "Instructions", message );
 273       dialog.show();
 274     }// static
 275 
 276    public static void println( String messageIn )
 277     {
 278       dialog.displayMessage( messageIn );
 279     }
 280 
 281  }// SysOut  class