1 /*
   2  * Copyright (c) 2004, 2014, 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 sun.awt.EmbeddedFrame;
  25 import sun.awt.X11.XEmbeddedFrame;
  26 
  27 import java.awt.*;
  28 import java.awt.event.*;
  29 import java.io.BufferedReader;
  30 import java.io.File;
  31 import java.io.FileReader;
  32 
  33 /*
  34  * @summary This class creates the SWT shell with AWT Frame embedded in it.
  35  *          The embedded AWT Frame has components added to it, listeners are
  36  *          added and events are triggered to check if the listeners are
  37  *          notified.
  38  * @author Girish R (girish.ramachandran@sun.com)
  39  */
  40 
  41 public class GUIXEmbedListeners extends Frame {
  42 
  43     EmbeddedFrame frame;
  44     Button button1, button2;
  45     TextField textfield1;
  46     Choice colorchoice;
  47     Scrollbar scrollBar;
  48     Panel panel, panel1;
  49     volatile boolean act_event, adj_list, comp_hidden, comp_moved,
  50             comp_resized, comp_shown, comp_removed, comp_added, focus_gain,
  51             focus_lost, ancestor_moved, ancestor_resized, hierarchy_changed,
  52             item_changed, key_pressed, key_released, key_typed,
  53             mouse_clicked, mouse_entered, mouse_exited, mouse_pressed,
  54             mouse_released, mouse_moved, mouse_dragged, textvalue_changed;
  55 
  56     static String WINDOW_ID_FILE = "window_id";
  57     String windowID;
  58     Process process;
  59 
  60     public void destroyProcess() {
  61         if (process != null)
  62             process.destroy();
  63     }
  64 
  65     /**
  66      * Open SWT shell here.
  67      * Add all components and listeners
  68      */
  69     public GUIXEmbedListeners() throws Exception {
  70 
  71         String osName = System.getProperty("os.name");
  72         if (!osName.equals("Linux") && !osName.equals("SunOS"))
  73             return;
  74 
  75         //Run the native process and wait for it to get over
  76         process = Runtime.getRuntime().exec("./gtk-embedder");
  77 
  78         File file = new File(WINDOW_ID_FILE);
  79         int waitCount = 0;
  80         while (!file.exists() && waitCount < 20) {
  81             try { Thread.sleep(100); } catch (Exception e) {}
  82             waitCount++;
  83         }
  84         windowID = new BufferedReader(new FileReader(file)).readLine();
  85 
  86         frame = new XEmbeddedFrame(Long.decode(windowID).longValue());
  87 
  88         frame.setLayout(new FlowLayout());
  89         frame.setSize(500, 270);
  90 
  91         frame.setBackground(Color.red);
  92 
  93         button1 = new Button("TestButton");
  94         button2 = new Button("TestButton2");
  95         textfield1 = new TextField("Textfield");
  96         textfield1.setLocation(50, 50);
  97         textfield1.setSize(80, 80);
  98 
  99         colorchoice = new Choice();
 100         colorchoice.add("Green");
 101         colorchoice.add("Blue");
 102         colorchoice.add("Red");
 103         colorchoice.add("White");
 104 
 105         panel = new Panel();
 106         panel.setSize(100, 100);
 107         panel.setBackground(Color.white);
 108         panel1 = new Panel();
 109         panel1.setLocation(50, 50);
 110 
 111         panel1.setBackground(Color.blue);
 112         scrollBar = new Scrollbar();
 113 
 114         button1.addActionListener( e -> act_event = true );
 115 
 116         scrollBar.addAdjustmentListener( e -> adj_list = true );
 117 
 118         button1.addComponentListener( new ComponentListener () {
 119             public void componentHidden(ComponentEvent e) { comp_hidden = true; }
 120             public void componentMoved(ComponentEvent e) { comp_moved = true; }
 121             public void componentResized(ComponentEvent e) { comp_resized = true; }
 122             public void componentShown(ComponentEvent e) { comp_shown = true; }
 123         });
 124 
 125         frame.addContainerListener( new ContainerListener () {
 126             public void componentAdded(ContainerEvent e) { comp_added = true; }
 127             public void componentRemoved(ContainerEvent e) { comp_removed = true; }
 128         });
 129 
 130         button1.addFocusListener( new FocusListener () {
 131             public void focusGained(FocusEvent e) { focus_gain = true; }
 132             public void focusLost(FocusEvent e) { focus_lost = true; }
 133         });
 134 
 135         button1.addHierarchyBoundsListener( new HierarchyBoundsListener () {
 136             public void ancestorMoved(HierarchyEvent e) { ancestor_moved = true; }
 137             public void ancestorResized(HierarchyEvent e) { ancestor_resized = true; }
 138         });
 139 
 140         button1.addHierarchyListener( e -> hierarchy_changed = true );
 141 
 142         colorchoice.addItemListener( e -> item_changed = true);
 143 
 144         textfield1.addKeyListener( new KeyListener () {
 145             public void keyPressed(KeyEvent e) { key_pressed = true; }
 146             public void keyReleased(KeyEvent e) { key_released = true; }
 147             public void keyTyped(KeyEvent e) { key_typed = true; }
 148         });
 149         button1.addMouseListener( new MouseListener () {
 150             public void mouseClicked(MouseEvent e) { mouse_clicked = true; }
 151             public void mouseEntered(MouseEvent e) { mouse_entered = true; }
 152             public void mouseExited(MouseEvent e) { mouse_exited = true; }
 153             public void mousePressed(MouseEvent e) { mouse_pressed = true; }
 154             public void mouseReleased(MouseEvent e) { mouse_released = true; }
 155         });
 156 
 157         button1.addMouseMotionListener( new MouseMotionListener () {
 158             public void mouseDragged(MouseEvent e) { mouse_moved = true; }
 159             public void mouseMoved(MouseEvent e) { mouse_dragged = true; }
 160         });
 161 
 162         textfield1.addTextListener( e -> textvalue_changed = true);
 163     }
 164 }