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