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