220N/A/*
3909N/A * Copyright (c) 2004, 2010, Oracle and/or its affiliates. All rights reserved.
220N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
220N/A *
220N/A * This code is free software; you can redistribute it and/or modify it
220N/A * under the terms of the GNU General Public License version 2 only, as
220N/A * published by the Free Software Foundation.
220N/A *
220N/A * This code is distributed in the hope that it will be useful, but WITHOUT
220N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
220N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
220N/A * version 2 for more details (a copy is included in the LICENSE file that
220N/A * accompanied this code).
220N/A *
220N/A * You should have received a copy of the GNU General Public License version
220N/A * 2 along with this work; if not, write to the Free Software Foundation,
220N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
220N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
220N/A */
220N/A
220N/Aimport java.awt.*;
220N/Aimport java.awt.event.*;
220N/Aimport javax.swing.*;
220N/Aimport java.io.*;
220N/Aimport java.util.logging.*;
220N/Aimport sun.awt.WindowIDProvider;
220N/Aimport java.awt.dnd.*;
220N/Aimport java.awt.datatransfer.*;
220N/A
220N/Apublic abstract class TestXEmbedServer {
3326N/A // vertical position of server AND client windows
3326N/A private static final int VERTICAL_POSITION = 200;
3326N/A
220N/A private static final Logger log = Logger.getLogger("test.xembed");
220N/A Frame f;
220N/A Canvas client;
220N/A Button toFocus;
220N/A Button b_modal;
220N/A JButton b_close;
220N/A JDialog modal_d;
220N/A JFrame dummy;
220N/A Container clientCont;
220N/A boolean passed;
220N/A
220N/A public boolean isPassed() {
220N/A return passed;
220N/A }
220N/A
220N/A public TestXEmbedServer(boolean manual) {
220N/A
220N/A // Enable testing extensions in XEmbed server
220N/A System.setProperty("sun.awt.xembed.testing", "true");
220N/A
220N/A f = new Frame("Main frame");
220N/A f.addWindowListener(new WindowAdapter() {
220N/A public void windowClosing(WindowEvent e) {
220N/A synchronized(TestXEmbedServer.this) {
220N/A TestXEmbedServer.this.notifyAll();
220N/A }
220N/A dummy.dispose();
220N/A f.dispose();
220N/A }
220N/A });
220N/A
220N/A f.setLayout(new BorderLayout());
220N/A
220N/A Container bcont = new Container();
220N/A
220N/A toFocus = new Button("Click to focus server");
220N/A final TextField tf = new TextField(20);
220N/A tf.setName("0");
220N/A DragSource ds = new DragSource();
220N/A final DragSourceListener dsl = new DragSourceAdapter() {
220N/A public void dragDropEnd(DragSourceDropEvent dsde) {
220N/A }
220N/A };
220N/A final DragGestureListener dgl = new DragGestureListener() {
220N/A public void dragGestureRecognized(DragGestureEvent dge) {
220N/A dge.startDrag(null, new StringSelection(tf.getText()), dsl);
220N/A }
220N/A };
220N/A ds.createDefaultDragGestureRecognizer(tf, DnDConstants.ACTION_COPY, dgl);
220N/A
220N/A final DropTargetListener dtl = new DropTargetAdapter() {
220N/A public void drop(DropTargetDropEvent dtde) {
220N/A dtde.acceptDrop(DnDConstants.ACTION_COPY);
220N/A try {
220N/A tf.setText(tf.getText() + (String)dtde.getTransferable().getTransferData(DataFlavor.stringFlavor));
220N/A } catch (Exception e) {
220N/A }
220N/A }
220N/A };
220N/A final DropTarget dt = new DropTarget(tf, dtl);
220N/A
220N/A Button b_add = new Button("Add client");
220N/A b_add.addActionListener(new ActionListener() {
220N/A public void actionPerformed(ActionEvent e) {
220N/A addClient();
220N/A }
220N/A });
220N/A Button b_remove = new Button("Remove client");
220N/A b_remove.addActionListener(new ActionListener() {
220N/A public void actionPerformed(ActionEvent e) {
220N/A if (clientCont.getComponentCount() != 0) {
220N/A clientCont.remove(clientCont.getComponentCount()-1);
220N/A }
220N/A }
220N/A });
220N/A b_close = new JButton("Close modal dialog");
220N/A b_close.addActionListener(new ActionListener() {
220N/A public void actionPerformed(ActionEvent e) {
220N/A modal_d.dispose();
220N/A }
220N/A });
220N/A b_modal = new Button("Show modal dialog");
220N/A b_modal.addActionListener(new ActionListener() {
220N/A public void actionPerformed(ActionEvent e) {
220N/A modal_d = new JDialog(f, "Modal dialog", true);
220N/A modal_d.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
220N/A modal_d.setBounds(0, 100, 200, 50);
220N/A modal_d.getContentPane().add(b_close);
220N/A modal_d.validate();
220N/A modal_d.show();
220N/A }
220N/A });
220N/A
220N/A bcont.add(tf);
220N/A bcont.add(toFocus);
220N/A bcont.add(b_add);
220N/A bcont.add(b_remove);
220N/A bcont.add(b_modal);
220N/A if (manual) {
220N/A Button pass = new Button("Pass");
220N/A pass.addActionListener(new ActionListener() {
220N/A public void actionPerformed(ActionEvent e) {
220N/A passed = true;
220N/A synchronized(TestXEmbedServer.this) {
220N/A TestXEmbedServer.this.notifyAll();
220N/A }
220N/A }
220N/A });
220N/A bcont.add(pass);
220N/A Button fail = new Button("Fail");
220N/A fail.addActionListener(new ActionListener() {
220N/A public void actionPerformed(ActionEvent e) {
220N/A passed = false;
220N/A synchronized(TestXEmbedServer.this) {
220N/A TestXEmbedServer.this.notifyAll();
220N/A }
220N/A }
220N/A });
220N/A bcont.add(fail);
220N/A }
220N/A b_modal.setName("2");
220N/A bcont.setLayout(new FlowLayout());
220N/A f.add(bcont, BorderLayout.NORTH);
220N/A
220N/A clientCont = Box.createVerticalBox();
220N/A f.add(clientCont, BorderLayout.CENTER);
220N/A
220N/A dummy = new JFrame("Dummy");
220N/A dummy.getContentPane().add(new JButton("Button"));
220N/A dummy.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
3326N/A dummy.setBounds(0, VERTICAL_POSITION, 100, 100);
220N/A dummy.setVisible(true);
220N/A
3326N/A f.setBounds(300, VERTICAL_POSITION, 800, 300);
220N/A f.setVisible(true);
220N/A }
220N/A
220N/A public abstract Process startClient(Rectangle bounds[], long window);
220N/A
220N/A public void addClient() {
220N/A client = new Canvas() {
220N/A public void paint(Graphics g) {
220N/A super.paint(g);
220N/A }
220N/A };
220N/A client.setBackground(new Color(30, 220, 40));
220N/A clientCont.add(client);
220N/A clientCont.validate();
220N/A WindowIDProvider pid = (WindowIDProvider)client.getPeer();
220N/A log.fine("Added XEmbed server(Canvas) with X window ID " + pid.getWindow());
220N/A Rectangle toFocusBounds = toFocus.getBounds();
220N/A toFocusBounds.setLocation(toFocus.getLocationOnScreen());
220N/A f.validate();
220N/A
220N/A // KDE doesn't accept clicks on title as activation - click below title
220N/A Rectangle fbounds = f.getBounds();
220N/A fbounds.y += f.getInsets().top;
220N/A fbounds.height -= f.getInsets().top;
220N/A
220N/A Process proc = startClient(new Rectangle[] {fbounds, dummy.getBounds(), toFocusBounds,
220N/A new Rectangle(b_modal.getLocationOnScreen(), b_modal.getSize()),
220N/A new Rectangle(10, 130, 20, 20)}, pid.getWindow());
220N/A new ClientWatcher(client, proc, clientCont).start();
220N/A }
220N/A
220N/A public void dispose() {
220N/A f.dispose();
220N/A f = null;
220N/A dummy.dispose();
220N/A dummy = null;
220N/A if (modal_d != null) {
220N/A modal_d.dispose();
220N/A modal_d = null;
220N/A }
220N/A }
220N/A}
220N/A
220N/Aclass ClientWatcher extends Thread {
220N/A private Process clientProcess;
220N/A private Canvas client;
220N/A private Container parent;
220N/A public ClientWatcher(Canvas client, Process proc, Container parent) {
220N/A this.client = client;
220N/A this.clientProcess = proc;
220N/A this.parent = parent;
220N/A }
220N/A
220N/A public void run() {
220N/A try {
220N/A clientProcess.waitFor();
220N/A } catch (InterruptedException ie) {
220N/A }
220N/A parent.remove(client);
220N/A }
220N/A}