TestXEmbedServer.java revision 3326:9deace8396f9
154773Simp/*
254773Simp * Copyright (c) 2004, 2008, Oracle and/or its affiliates. All rights reserved.
354773Simp * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
454773Simp *
554773Simp * This code is free software; you can redistribute it and/or modify it
654773Simp * under the terms of the GNU General Public License version 2 only, as
754773Simp * published by the Free Software Foundation.
854773Simp *
954773Simp * This code is distributed in the hope that it will be useful, but WITHOUT
1054773Simp * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
1154773Simp * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
1254773Simp * version 2 for more details (a copy is included in the LICENSE file that
1354773Simp * accompanied this code).
1454773Simp *
1554773Simp * You should have received a copy of the GNU General Public License version
1654773Simp * 2 along with this work; if not, write to the Free Software Foundation,
1754773Simp * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
1854773Simp *
1954773Simp * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2054773Simp * or visit www.oracle.com if you need additional information or have any
2154773Simp * questions.
2254773Simp */
2354773Simp
2454773Simpimport java.awt.*;
2554773Simpimport java.awt.event.*;
2654773Simpimport javax.swing.*;
2754773Simpimport java.io.*;
2854773Simpimport java.util.logging.*;
2954773Simpimport sun.awt.WindowIDProvider;
3054773Simpimport java.awt.dnd.*;
3154773Simpimport java.awt.datatransfer.*;
3254773Simp
3354773Simppublic abstract class TestXEmbedServer {
3454773Simp    // vertical position of server AND client windows
3554773Simp    private static final int VERTICAL_POSITION = 200;
3654773Simp
3754773Simp    private static final Logger log = Logger.getLogger("test.xembed");
3854773Simp    Frame f;
3954773Simp    Canvas client;
4054773Simp    Button toFocus;
4154773Simp    Button b_modal;
4254773Simp    JButton b_close;
4354773Simp    JDialog modal_d;
4454773Simp    JFrame dummy;
4554773Simp    Container clientCont;
4654773Simp    boolean passed;
4754773Simp
4854773Simp    public boolean isPassed() {
4954773Simp        return passed;
5054773Simp    }
5154773Simp
5254773Simp    public TestXEmbedServer(boolean manual) {
5354773Simp
5454773Simp        // Enable testing extensions in XEmbed server
5554773Simp        System.setProperty("sun.awt.xembed.testing", "true");
5654773Simp
5754773Simp        f = new Frame("Main frame");
5854773Simp        f.addWindowListener(new WindowAdapter() {
5954773Simp                public void windowClosing(WindowEvent e) {
6054773Simp                    synchronized(TestXEmbedServer.this) {
6154773Simp                        TestXEmbedServer.this.notifyAll();
6254773Simp                    }
6354773Simp                    dummy.dispose();
6454773Simp                    f.dispose();
6554773Simp                }
6654773Simp            });
6754773Simp
6854773Simp        f.setLayout(new BorderLayout());
6954773Simp
7054773Simp        Container bcont = new Container();
7154773Simp
7254773Simp        toFocus = new Button("Click to focus server");
7354773Simp        final TextField tf = new TextField(20);
7454773Simp        tf.setName("0");
7554773Simp        DragSource ds = new DragSource();
7654773Simp        final DragSourceListener dsl = new DragSourceAdapter() {
7754773Simp                public void dragDropEnd(DragSourceDropEvent dsde) {
7854773Simp                }
7954773Simp            };
8054773Simp        final DragGestureListener dgl = new DragGestureListener() {
8154773Simp                public void dragGestureRecognized(DragGestureEvent dge) {
8254773Simp                    dge.startDrag(null, new StringSelection(tf.getText()), dsl);
8354773Simp                }
8454773Simp            };
8554773Simp        ds.createDefaultDragGestureRecognizer(tf, DnDConstants.ACTION_COPY, dgl);
8654773Simp
8754773Simp        final DropTargetListener dtl = new DropTargetAdapter() {
8854773Simp                public void drop(DropTargetDropEvent dtde) {
8954773Simp                    dtde.acceptDrop(DnDConstants.ACTION_COPY);
9054773Simp                    try {
9154773Simp                        tf.setText(tf.getText() + (String)dtde.getTransferable().getTransferData(DataFlavor.stringFlavor));
9254773Simp                    } catch (Exception e) {
9354773Simp                    }
9454994Simp                }
9554994Simp            };
9654994Simp        final DropTarget dt = new DropTarget(tf, dtl);
9754994Simp
9854994Simp        Button b_add = new Button("Add client");
9954994Simp        b_add.addActionListener(new ActionListener() {
10054994Simp                public void actionPerformed(ActionEvent e) {
10154773Simp                    addClient();
10254773Simp                }
10354773Simp            });
10454773Simp        Button b_remove = new Button("Remove client");
10554773Simp        b_remove.addActionListener(new ActionListener() {
10654773Simp                public void actionPerformed(ActionEvent e) {
10754773Simp                    if (clientCont.getComponentCount() != 0) {
10854773Simp                        clientCont.remove(clientCont.getComponentCount()-1);
10954773Simp                    }
11054773Simp                }
11154773Simp            });
11254773Simp        b_close = new JButton("Close modal dialog");
11354773Simp        b_close.addActionListener(new ActionListener() {
11454773Simp                public void actionPerformed(ActionEvent e) {
11554773Simp                    modal_d.dispose();
11654773Simp                }
11754773Simp            });
11854773Simp        b_modal = new Button("Show modal dialog");
11954773Simp        b_modal.addActionListener(new ActionListener() {
12054773Simp                public void actionPerformed(ActionEvent e) {
12154773Simp                    modal_d = new JDialog(f, "Modal dialog", true);
12254773Simp                    modal_d.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
12354773Simp                    modal_d.setBounds(0, 100, 200, 50);
12454773Simp                    modal_d.getContentPane().add(b_close);
12554773Simp                    modal_d.validate();
12654994Simp                    modal_d.show();
12754773Simp                }
12854994Simp            });
12954994Simp
13054773Simp        bcont.add(tf);
13154994Simp        bcont.add(toFocus);
13254773Simp        bcont.add(b_add);
13354994Simp        bcont.add(b_remove);
13454773Simp        bcont.add(b_modal);
13554994Simp        if (manual) {
13654994Simp            Button pass = new Button("Pass");
13754994Simp            pass.addActionListener(new ActionListener() {
13854994Simp                    public void actionPerformed(ActionEvent e) {
13954994Simp                        passed = true;
14054994Simp                        synchronized(TestXEmbedServer.this) {
14154773Simp                            TestXEmbedServer.this.notifyAll();
14254773Simp                        }
14354773Simp                    }
14454773Simp                });
14554773Simp            bcont.add(pass);
14654773Simp            Button fail = new Button("Fail");
14754773Simp            fail.addActionListener(new ActionListener() {
14854773Simp                    public void actionPerformed(ActionEvent e) {
14954773Simp                        passed = false;
15054773Simp                        synchronized(TestXEmbedServer.this) {
15154994Simp                            TestXEmbedServer.this.notifyAll();
15254994Simp                        }
15354773Simp                    }
15454994Simp                });
15554994Simp            bcont.add(fail);
15654994Simp        }
15754994Simp        b_modal.setName("2");
15854994Simp        bcont.setLayout(new FlowLayout());
15954994Simp        f.add(bcont, BorderLayout.NORTH);
16054994Simp
16154994Simp        clientCont = Box.createVerticalBox();
16254994Simp        f.add(clientCont, BorderLayout.CENTER);
16354773Simp
16454773Simp        dummy = new JFrame("Dummy");
16554773Simp        dummy.getContentPane().add(new JButton("Button"));
16654994Simp        dummy.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
16754773Simp        dummy.setBounds(0, VERTICAL_POSITION, 100, 100);
16854994Simp        dummy.setVisible(true);
16954773Simp
17054773Simp        f.setBounds(300, VERTICAL_POSITION, 800, 300);
17154773Simp        f.setVisible(true);
17254773Simp    }
17354773Simp
17454773Simp    public abstract Process startClient(Rectangle bounds[], long window);
17554773Simp
17654773Simp    public void addClient() {
17754994Simp        client = new Canvas() {
17854773Simp                public void paint(Graphics g) {
17954994Simp                    super.paint(g);
18054773Simp                }
18154994Simp            };
18254773Simp        client.setBackground(new Color(30, 220, 40));
18354773Simp        clientCont.add(client);
18454994Simp        clientCont.validate();
18554773Simp        WindowIDProvider pid = (WindowIDProvider)client.getPeer();
18654773Simp        log.fine("Added XEmbed server(Canvas) with X window ID " + pid.getWindow());
18754773Simp        Rectangle toFocusBounds = toFocus.getBounds();
18854773Simp        toFocusBounds.setLocation(toFocus.getLocationOnScreen());
18954773Simp        f.validate();
19054773Simp
19154773Simp        // KDE doesn't accept clicks on title as activation - click below title
19254773Simp        Rectangle fbounds = f.getBounds();
19354773Simp        fbounds.y += f.getInsets().top;
19454773Simp        fbounds.height -= f.getInsets().top;
19554994Simp
19654994Simp        Process proc = startClient(new Rectangle[] {fbounds, dummy.getBounds(), toFocusBounds,
19754994Simp                                                    new Rectangle(b_modal.getLocationOnScreen(), b_modal.getSize()),
19854994Simp                                                    new Rectangle(10, 130, 20, 20)}, pid.getWindow());
19954994Simp        new ClientWatcher(client, proc, clientCont).start();
20054773Simp    }
20154773Simp
20254773Simp    public void dispose() {
20354994Simp        f.dispose();
20454994Simp        f = null;
20554773Simp        dummy.dispose();
20654773Simp        dummy = null;
20754773Simp        if (modal_d != null) {
20854773Simp            modal_d.dispose();
20954773Simp            modal_d = null;
21054773Simp        }
21154773Simp    }
21254773Simp}
21354773Simp
21454773Simpclass ClientWatcher extends Thread {
21554773Simp    private Process clientProcess;
21654773Simp    private Canvas client;
21754773Simp    private Container parent;
21854773Simp    public ClientWatcher(Canvas client, Process proc, Container parent) {
21954773Simp        this.client = client;
22054773Simp        this.clientProcess = proc;
22154773Simp        this.parent = parent;
22254994Simp    }
22354773Simp
22454773Simp    public void run() {
22554773Simp        try {
22654773Simp            clientProcess.waitFor();
22754773Simp        } catch (InterruptedException ie) {
22854773Simp        }
22954773Simp        parent.remove(client);
23054773Simp    }
23155161Simp}
23254773Simp