1 /*
   2  * Copyright (c) 2004, 2008, 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 java.awt.*;
  25 import java.awt.event.*;
  26 import javax.swing.*;
  27 import java.io.*;
  28 import java.util.logging.*;
  29 import sun.awt.WindowIDProvider;
  30 import java.awt.dnd.*;
  31 import java.awt.datatransfer.*;
  32 
  33 public abstract class TestXEmbedServer {
  34     private static final Logger log = Logger.getLogger("test.xembed");
  35     Frame f;
  36     Canvas client;
  37     Button toFocus;
  38     Button b_modal;
  39     JButton b_close;
  40     JDialog modal_d;
  41     JFrame dummy;
  42     Container clientCont;
  43     boolean passed;
  44 
  45     public boolean isPassed() {
  46         return passed;
  47     }
  48 
  49     public TestXEmbedServer(boolean manual) {
  50 
  51         // Enable testing extensions in XEmbed server
  52         System.setProperty("sun.awt.xembed.testing", "true");
  53 
  54         f = new Frame("Main frame");
  55         f.addWindowListener(new WindowAdapter() {
  56                 public void windowClosing(WindowEvent e) {
  57                     synchronized(TestXEmbedServer.this) {
  58                         TestXEmbedServer.this.notifyAll();
  59                     }
  60                     dummy.dispose();
  61                     f.dispose();
  62                 }
  63             });
  64 
  65         f.setLayout(new BorderLayout());
  66 
  67         Container bcont = new Container();
  68 
  69         toFocus = new Button("Click to focus server");
  70         final TextField tf = new TextField(20);
  71         tf.setName("0");
  72         DragSource ds = new DragSource();
  73         final DragSourceListener dsl = new DragSourceAdapter() {
  74                 public void dragDropEnd(DragSourceDropEvent dsde) {
  75                 }
  76             };
  77         final DragGestureListener dgl = new DragGestureListener() {
  78                 public void dragGestureRecognized(DragGestureEvent dge) {
  79                     dge.startDrag(null, new StringSelection(tf.getText()), dsl);
  80                 }
  81             };
  82         ds.createDefaultDragGestureRecognizer(tf, DnDConstants.ACTION_COPY, dgl);
  83 
  84         final DropTargetListener dtl = new DropTargetAdapter() {
  85                 public void drop(DropTargetDropEvent dtde) {
  86                     dtde.acceptDrop(DnDConstants.ACTION_COPY);
  87                     try {
  88                         tf.setText(tf.getText() + (String)dtde.getTransferable().getTransferData(DataFlavor.stringFlavor));
  89                     } catch (Exception e) {
  90                     }
  91                 }
  92             };
  93         final DropTarget dt = new DropTarget(tf, dtl);
  94 
  95         Button b_add = new Button("Add client");
  96         b_add.addActionListener(new ActionListener() {
  97                 public void actionPerformed(ActionEvent e) {
  98                     addClient();
  99                 }
 100             });
 101         Button b_remove = new Button("Remove client");
 102         b_remove.addActionListener(new ActionListener() {
 103                 public void actionPerformed(ActionEvent e) {
 104                     if (clientCont.getComponentCount() != 0) {
 105                         clientCont.remove(clientCont.getComponentCount()-1);
 106                     }
 107                 }
 108             });
 109         b_close = new JButton("Close modal dialog");
 110         b_close.addActionListener(new ActionListener() {
 111                 public void actionPerformed(ActionEvent e) {
 112                     modal_d.dispose();
 113                 }
 114             });
 115         b_modal = new Button("Show modal dialog");
 116         b_modal.addActionListener(new ActionListener() {
 117                 public void actionPerformed(ActionEvent e) {
 118                     modal_d = new JDialog(f, "Modal dialog", true);
 119                     modal_d.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
 120                     modal_d.setBounds(0, 100, 200, 50);
 121                     modal_d.getContentPane().add(b_close);
 122                     modal_d.validate();
 123                     modal_d.show();
 124                 }
 125             });
 126 
 127         bcont.add(tf);
 128         bcont.add(toFocus);
 129         bcont.add(b_add);
 130         bcont.add(b_remove);
 131         bcont.add(b_modal);
 132         if (manual) {
 133             Button pass = new Button("Pass");
 134             pass.addActionListener(new ActionListener() {
 135                     public void actionPerformed(ActionEvent e) {
 136                         passed = true;
 137                         synchronized(TestXEmbedServer.this) {
 138                             TestXEmbedServer.this.notifyAll();
 139                         }
 140                     }
 141                 });
 142             bcont.add(pass);
 143             Button fail = new Button("Fail");
 144             fail.addActionListener(new ActionListener() {
 145                     public void actionPerformed(ActionEvent e) {
 146                         passed = false;
 147                         synchronized(TestXEmbedServer.this) {
 148                             TestXEmbedServer.this.notifyAll();
 149                         }
 150                     }
 151                 });
 152             bcont.add(fail);
 153         }
 154         b_modal.setName("2");
 155         bcont.setLayout(new FlowLayout());
 156         f.add(bcont, BorderLayout.NORTH);
 157 
 158         clientCont = Box.createVerticalBox();
 159         f.add(clientCont, BorderLayout.CENTER);
 160 
 161         dummy = new JFrame("Dummy");
 162         dummy.getContentPane().add(new JButton("Button"));
 163         dummy.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
 164         dummy.setBounds(0, 0, 100, 100);
 165         dummy.setVisible(true);
 166 
 167         f.setBounds(300, 0, 800, 300);
 168         f.setVisible(true);
 169     }
 170 
 171     public abstract Process startClient(Rectangle bounds[], long window);
 172 
 173     public void addClient() {
 174         client = new Canvas() {
 175                 public void paint(Graphics g) {
 176                     super.paint(g);
 177                 }
 178             };
 179         client.setBackground(new Color(30, 220, 40));
 180         clientCont.add(client);
 181         clientCont.validate();
 182         WindowIDProvider pid = (WindowIDProvider)client.getPeer();
 183         log.fine("Added XEmbed server(Canvas) with X window ID " + pid.getWindow());
 184         Rectangle toFocusBounds = toFocus.getBounds();
 185         toFocusBounds.setLocation(toFocus.getLocationOnScreen());
 186         f.validate();
 187 
 188         // KDE doesn't accept clicks on title as activation - click below title
 189         Rectangle fbounds = f.getBounds();
 190         fbounds.y += f.getInsets().top;
 191         fbounds.height -= f.getInsets().top;
 192 
 193         Process proc = startClient(new Rectangle[] {fbounds, dummy.getBounds(), toFocusBounds,
 194                                                     new Rectangle(b_modal.getLocationOnScreen(), b_modal.getSize()),
 195                                                     new Rectangle(10, 130, 20, 20)}, pid.getWindow());
 196         new ClientWatcher(client, proc, clientCont).start();
 197     }
 198 
 199     public void dispose() {
 200         f.dispose();
 201         f = null;
 202         dummy.dispose();
 203         dummy = null;
 204         if (modal_d != null) {
 205             modal_d.dispose();
 206             modal_d = null;
 207         }
 208     }
 209 }
 210 
 211 class ClientWatcher extends Thread {
 212     private Process clientProcess;
 213     private Canvas client;
 214     private Container parent;
 215     public ClientWatcher(Canvas client, Process proc, Container parent) {
 216         this.client = client;
 217         this.clientProcess = proc;
 218         this.parent = parent;
 219     }
 220 
 221     public void run() {
 222         try {
 223             clientProcess.waitFor();
 224         } catch (InterruptedException ie) {
 225         }
 226         parent.remove(client);
 227     }
 228 }