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 sun.awt.*; 26 import java.awt.event.*; 27 import java.lang.reflect.*; 28 import java.awt.dnd.*; 29 import java.awt.datatransfer.*; 30 31 public class JavaClient { 32 ClientContainer cont; main(String[] args)33 public static void main(String[] args) { 34 if (System.getProperty("os.name").toLowerCase().startsWith("win")) { 35 return; 36 } 37 38 // Enable testing extensions in XEmbed server 39 System.setProperty("sun.awt.xembed.testing", "true"); 40 41 boolean xtoolkit = "sun.awt.X11.XToolkit".equals(Toolkit.getDefaultToolkit().getClass().getName()); 42 final EmbeddedFrame ef = createEmbeddedFrame(xtoolkit, Long.parseLong(args[0])); 43 ef.setBackground(new Color(100, 100, 200)); 44 ef.setLayout(new BorderLayout()); 45 ef.add(new ClientContainer(ef), BorderLayout.CENTER); 46 ef.pack(); 47 ef.registerListeners(); 48 ef.setVisible(true); 49 } createEmbeddedFrame(boolean xtoolkit, long window)50 private static EmbeddedFrame createEmbeddedFrame(boolean xtoolkit, long window) { 51 try { 52 Class cl = (xtoolkit?Class.forName("sun.awt.X11.XEmbeddedFrame"):Class.forName("sun.awt.motif.MEmbeddedFrame")); 53 Constructor cons = cl.getConstructor(new Class[]{Long.TYPE, Boolean.TYPE}); 54 return (EmbeddedFrame)cons.newInstance(new Object[] {window, true}); 55 } catch (Exception e) { 56 e.printStackTrace(); 57 throw new RuntimeException("Can't create embedded frame"); 58 } 59 } 60 } 61 62 class ClientContainer extends Container { 63 Window parent; 64 int width, height; ClientContainer(Window w)65 public ClientContainer(Window w) { 66 parent = w; 67 width = 500; 68 height = 50; 69 final TextField tf = new TextField(30); 70 71 DragSource ds = new DragSource(); 72 final DragSourceListener dsl = new DragSourceAdapter() { 73 public void dragDropEnd(DragSourceDropEvent dsde) { 74 } 75 }; 76 final DragGestureListener dgl = new DragGestureListener() { 77 public void dragGestureRecognized(DragGestureEvent dge) { 78 dge.startDrag(null, new StringSelection(tf.getText()), dsl); 79 } 80 }; 81 ds.createDefaultDragGestureRecognizer(tf, DnDConstants.ACTION_COPY, dgl); 82 83 final DropTargetListener dtl = new DropTargetAdapter() { 84 public void drop(DropTargetDropEvent dtde) { 85 dtde.acceptDrop(DnDConstants.ACTION_COPY); 86 try { 87 tf.setText(tf.getText() + (String)dtde.getTransferable().getTransferData(DataFlavor.stringFlavor)); 88 } catch (Exception e) { 89 } 90 } 91 }; 92 final DropTarget dt = new DropTarget(tf, dtl); 93 94 setLayout(new FlowLayout()); 95 add(tf); 96 Button close = new Button("Close"); 97 close.addActionListener(new ActionListener() { 98 public void actionPerformed(ActionEvent e) { 99 parent.dispose(); 100 } 101 }); 102 Button inc = new Button("Increase size"); 103 inc.addActionListener(new ActionListener() { 104 public void actionPerformed(ActionEvent e) { 105 changeSize(10); 106 } 107 }); 108 Button dec = new Button("Decrease size"); 109 dec.addActionListener(new ActionListener() { 110 public void actionPerformed(ActionEvent e) { 111 changeSize(-10); 112 } 113 }); 114 add(close); 115 add(inc); 116 add(dec); 117 } changeSize(int step)118 void changeSize(int step) { 119 width += step; 120 height += step; 121 parent.pack(); 122 } getPreferredSize()123 public Dimension getPreferredSize() { 124 return new Dimension(width, height); 125 } 126 } 127