/* * Copyright (c) 2004, 2014, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions. */ import sun.awt.EmbeddedFrame; import sun.awt.X11.XEmbeddedFrame; import java.awt.*; import java.awt.event.*; import java.io.BufferedReader; import java.io.File; import java.io.FileReader; /* * @summary This class creates the SWT shell with AWT Frame embedded in it. * The embedded AWT Frame has components added to it, listeners are * added and events are triggered to check if the listeners are * notified. * @author Girish R (girish.ramachandran@sun.com) */ public class GUIXEmbedListeners extends Frame { EmbeddedFrame frame; Button button1, button2; TextField textfield1; Choice colorchoice; Scrollbar scrollBar; Panel panel, panel1; volatile boolean act_event, adj_list, comp_hidden, comp_moved, comp_resized, comp_shown, comp_removed, comp_added, focus_gain, focus_lost, ancestor_moved, ancestor_resized, hierarchy_changed, item_changed, key_pressed, key_released, key_typed, mouse_clicked, mouse_entered, mouse_exited, mouse_pressed, mouse_released, mouse_moved, mouse_dragged, textvalue_changed; static String WINDOW_ID_FILE = "window_id"; String windowID; Process process; public void destroyProcess() { if (process != null) process.destroy(); } /** * Open SWT shell here. * Add all components and listeners */ public GUIXEmbedListeners() throws Exception { String osName = System.getProperty("os.name"); if (!osName.equals("Linux") && !osName.equals("SunOS")) return; //Run the native process and wait for it to get over process = Runtime.getRuntime().exec("./gtk-embedder"); File file = new File(WINDOW_ID_FILE); int waitCount = 0; while (!file.exists() && waitCount < 20) { try { Thread.sleep(100); } catch (Exception e) {} waitCount++; } windowID = new BufferedReader(new FileReader(file)).readLine(); frame = new XEmbeddedFrame(Long.decode(windowID).longValue()); frame.setLayout(new FlowLayout()); frame.setSize(500, 270); frame.setBackground(Color.red); button1 = new Button("TestButton"); button2 = new Button("TestButton2"); textfield1 = new TextField("Textfield"); textfield1.setLocation(50, 50); textfield1.setSize(80, 80); colorchoice = new Choice(); colorchoice.add("Green"); colorchoice.add("Blue"); colorchoice.add("Red"); colorchoice.add("White"); panel = new Panel(); panel.setSize(100, 100); panel.setBackground(Color.white); panel1 = new Panel(); panel1.setLocation(50, 50); panel1.setBackground(Color.blue); scrollBar = new Scrollbar(); button1.addActionListener( e -> act_event = true ); scrollBar.addAdjustmentListener( e -> adj_list = true ); button1.addComponentListener( new ComponentListener () { public void componentHidden(ComponentEvent e) { comp_hidden = true; } public void componentMoved(ComponentEvent e) { comp_moved = true; } public void componentResized(ComponentEvent e) { comp_resized = true; } public void componentShown(ComponentEvent e) { comp_shown = true; } }); frame.addContainerListener( new ContainerListener () { public void componentAdded(ContainerEvent e) { comp_added = true; } public void componentRemoved(ContainerEvent e) { comp_removed = true; } }); button1.addFocusListener( new FocusListener () { public void focusGained(FocusEvent e) { focus_gain = true; } public void focusLost(FocusEvent e) { focus_lost = true; } }); button1.addHierarchyBoundsListener( new HierarchyBoundsListener () { public void ancestorMoved(HierarchyEvent e) { ancestor_moved = true; } public void ancestorResized(HierarchyEvent e) { ancestor_resized = true; } }); button1.addHierarchyListener( e -> hierarchy_changed = true ); colorchoice.addItemListener( e -> item_changed = true); textfield1.addKeyListener( new KeyListener () { public void keyPressed(KeyEvent e) { key_pressed = true; } public void keyReleased(KeyEvent e) { key_released = true; } public void keyTyped(KeyEvent e) { key_typed = true; } }); button1.addMouseListener( new MouseListener () { public void mouseClicked(MouseEvent e) { mouse_clicked = true; } public void mouseEntered(MouseEvent e) { mouse_entered = true; } public void mouseExited(MouseEvent e) { mouse_exited = true; } public void mousePressed(MouseEvent e) { mouse_pressed = true; } public void mouseReleased(MouseEvent e) { mouse_released = true; } }); button1.addMouseMotionListener( new MouseMotionListener () { public void mouseDragged(MouseEvent e) { mouse_moved = true; } public void mouseMoved(MouseEvent e) { mouse_dragged = true; } }); textfield1.addTextListener( e -> textvalue_changed = true); } }