DnDSource.java revision 8729:0242fce0f717
1254885Sdumbbell/*
2254885Sdumbbell * Copyright (c) 2009, Oracle and/or its affiliates. All rights reserved.
3254885Sdumbbell * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4254885Sdumbbell *
5254885Sdumbbell * This code is free software; you can redistribute it and/or modify it
6254885Sdumbbell * under the terms of the GNU General Public License version 2 only, as
7254885Sdumbbell * published by the Free Software Foundation.
8254885Sdumbbell *
9254885Sdumbbell * This code is distributed in the hope that it will be useful, but WITHOUT
10254885Sdumbbell * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11254885Sdumbbell * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12254885Sdumbbell * version 2 for more details (a copy is included in the LICENSE file that
13254885Sdumbbell * accompanied this code).
14254885Sdumbbell *
15254885Sdumbbell * You should have received a copy of the GNU General Public License version
16254885Sdumbbell * 2 along with this work; if not, write to the Free Software Foundation,
17254885Sdumbbell * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18254885Sdumbbell *
19254885Sdumbbell * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20254885Sdumbbell * or visit www.oracle.com if you need additional information or have any
21254885Sdumbbell * questions.
22254885Sdumbbell */
23254885Sdumbbell
24254885Sdumbbell/*
25254885Sdumbbell*  AWT Button is a DragSource and also a transferable object
26254885Sdumbbell*/
27254885Sdumbbell
28254885Sdumbbellimport java.awt.*;
29254885Sdumbbellimport java.awt.datatransfer.*;
30254885Sdumbbellimport java.awt.dnd.*;
31254885Sdumbbellimport java.io.*;
32254885Sdumbbell
33254885Sdumbbellclass DnDSource extends Button implements Transferable,
34254885Sdumbbell        DragGestureListener,
35254885Sdumbbell        DragSourceListener {
36254885Sdumbbell    private DataFlavor df;
37254885Sdumbbell    private transient int dropAction;
38254885Sdumbbell    private final int dragOperation = DnDConstants.ACTION_COPY | DnDConstants.ACTION_MOVE | DnDConstants.ACTION_LINK;
39254885Sdumbbell    DragSource dragSource = new DragSource();
40254885Sdumbbell
41254885Sdumbbell    DnDSource(String label) {
42254885Sdumbbell        super(label);
43254885Sdumbbell        setBackground(Color.yellow);
44254885Sdumbbell        setForeground(Color.blue);
45254885Sdumbbell        df = new DataFlavor(DnDSource.class, "DnDSource");
46254885Sdumbbell
47254885Sdumbbell        dragSource.createDefaultDragGestureRecognizer(
48254885Sdumbbell                this,
49254885Sdumbbell                dragOperation,
50254885Sdumbbell                this
51254885Sdumbbell        );
52254885Sdumbbell        dragSource.addDragSourceListener(this);
53254885Sdumbbell    }
54254885Sdumbbell
55254885Sdumbbell    public void changeCursor(
56254885Sdumbbell            DragSourceContext dsc,
57254885Sdumbbell            int ra
58254885Sdumbbell    ) {
59254885Sdumbbell        java.awt.Cursor c = null;
60254885Sdumbbell        if ((ra & DnDConstants.ACTION_LINK) == DnDConstants.ACTION_LINK)
61254885Sdumbbell            c = DragSource.DefaultLinkDrop;
62254885Sdumbbell        else if ((ra & DnDConstants.ACTION_MOVE) == DnDConstants.ACTION_MOVE)
63254885Sdumbbell            c = MyCursor.MOVE;//DragSource.DefaultMoveDrop;
64254885Sdumbbell        else if ((ra & DnDConstants.ACTION_COPY) == DnDConstants.ACTION_COPY)
65254885Sdumbbell            c = MyCursor.COPY;
66254885Sdumbbell        else
67254885Sdumbbell            c = MyCursor.NO_DROP;
68254885Sdumbbell        dsc.setCursor(c);
69254885Sdumbbell    }
70254885Sdumbbell
71254885Sdumbbell    /**
72254885Sdumbbell     * a Drag gesture has been recognized
73254885Sdumbbell     */
74254885Sdumbbell
75254885Sdumbbell    public void dragGestureRecognized(DragGestureEvent dge) {
76254885Sdumbbell        System.out.println("starting Drag");
77254885Sdumbbell        try {
78254885Sdumbbell            if (DragSource.isDragImageSupported()) {
79254885Sdumbbell                System.out.println("starting Imaged Drag");
80254885Sdumbbell                dge.startDrag(
81254885Sdumbbell                        null,
82254885Sdumbbell                        new ImageGenerator(50, 100, new Color(0xff, 0xff, 0xff, 0x00) ) {
83254885Sdumbbell                                @Override public void paint(Graphics gr) {
84254885Sdumbbell                                    gr.translate(width/2, height/2);
85254885Sdumbbell                                    ((Graphics2D)gr).setStroke(new BasicStroke(3));
86254885Sdumbbell                                    int R = width/4+5;
87254885Sdumbbell                                    gr.setColor(new Color(0x00, 0x00, 0xff, 0x7F));
88254885Sdumbbell                                    gr.fillRect(-R, -R, 2*R, 2*R);
89254885Sdumbbell                                    gr.setColor(new Color(0x00, 0x00, 0xff, 0xff));
90254885Sdumbbell                                    gr.drawRect(-R, -R, 2*R, 2*R);
91254885Sdumbbell
92254885Sdumbbell
93254885Sdumbbell                                    gr.translate(10, -10);
94254885Sdumbbell                                    R -= 5;
95254885Sdumbbell                                    gr.setColor(Color.RED);
96254885Sdumbbell                                    gr.fillOval(-R, -R, 2*R, 2*R);
97254885Sdumbbell                                    gr.setColor(Color.MAGENTA);
98254885Sdumbbell                                    gr.drawOval(-R, -R, 2*R, 2*R);
99254885Sdumbbell                                }
100254885Sdumbbell                        }.getImage(),
101254885Sdumbbell                        new Point(15, 40),
102254885Sdumbbell                        this,
103254885Sdumbbell                        this);
104254885Sdumbbell            } else {
105254885Sdumbbell                dge.startDrag(
106254885Sdumbbell                        null,
107254885Sdumbbell                        this,
108254885Sdumbbell                        this);
109254885Sdumbbell            }
110254885Sdumbbell        } catch (InvalidDnDOperationException e) {
111254885Sdumbbell            e.printStackTrace();
112254885Sdumbbell        }
113254885Sdumbbell    }
114254885Sdumbbell
115254885Sdumbbell    /**
116254885Sdumbbell     * as the hotspot enters a platform dependent drop site
117254885Sdumbbell     */
118254885Sdumbbell
119254885Sdumbbell    public void dragEnter(DragSourceDragEvent dsde) {
120254885Sdumbbell        System.out.println("[Source] dragEnter");
121254885Sdumbbell        changeCursor(
122254885Sdumbbell            dsde.getDragSourceContext(),
123254885Sdumbbell            dsde.getUserAction() & dsde.getDropAction()
124254885Sdumbbell        );
125254885Sdumbbell    }
126254885Sdumbbell
127254885Sdumbbell    /**
128254885Sdumbbell     * as the hotspot moves over a platform dependent drop site
129254885Sdumbbell     */
130254885Sdumbbell    public void dragOver(DragSourceDragEvent dsde) {
131254885Sdumbbell        System.out.println("[Source] dragOver");
132254885Sdumbbell        changeCursor(
133254885Sdumbbell            dsde.getDragSourceContext(),
134254885Sdumbbell            dsde.getUserAction() & dsde.getDropAction()
135254885Sdumbbell        );
136254885Sdumbbell        dropAction = dsde.getUserAction() & dsde.getDropAction();
137254885Sdumbbell        System.out.println("dropAction = " + dropAction);
138254885Sdumbbell    }
139254885Sdumbbell
140254885Sdumbbell    /**
141254885Sdumbbell     * as the hotspot exits a platform dependent drop site
142254885Sdumbbell     */
143254885Sdumbbell    public void dragExit(DragSourceEvent dse) {
144254885Sdumbbell        System.out.println("[Source] dragExit");
145254885Sdumbbell        changeCursor(
146254885Sdumbbell                dse.getDragSourceContext(),
147254885Sdumbbell                DnDConstants.ACTION_NONE
148254885Sdumbbell        );
149254885Sdumbbell    }
150254885Sdumbbell
151254885Sdumbbell    /**
152254885Sdumbbell     * as the operation changes
153254885Sdumbbell     */
154254885Sdumbbell    public void dragGestureChanged(DragSourceDragEvent dsde) {
155254885Sdumbbell        System.out.println("[Source] dragGestureChanged");
156254885Sdumbbell        changeCursor(
157254885Sdumbbell            dsde.getDragSourceContext(),
158254885Sdumbbell            dsde.getUserAction() & dsde.getDropAction()
159254885Sdumbbell        );
160254885Sdumbbell        dropAction = dsde.getUserAction() & dsde.getDropAction();
161254885Sdumbbell        System.out.println("dropAction = " + dropAction);
162254885Sdumbbell    }
163254885Sdumbbell
164254885Sdumbbell
165254885Sdumbbell    /**
166254885Sdumbbell     * as the operation completes
167254885Sdumbbell     */
168254885Sdumbbell    public void dragDropEnd(DragSourceDropEvent dsde) {
169254885Sdumbbell        System.out.println("[Source] dragDropEnd");
170254885Sdumbbell    }
171254885Sdumbbell
172254885Sdumbbell    public void dropActionChanged(DragSourceDragEvent dsde) {
173254885Sdumbbell        System.out.println("[Source] dropActionChanged");
174254885Sdumbbell        dropAction = dsde.getUserAction() & dsde.getDropAction();
175254885Sdumbbell        System.out.println("dropAction = " + dropAction);
176254885Sdumbbell    }
177254885Sdumbbell
178254885Sdumbbell    public DataFlavor[] getTransferDataFlavors() {
179254885Sdumbbell        return new DataFlavor[]{df};
180254885Sdumbbell    }
181254885Sdumbbell
182254885Sdumbbell    public boolean isDataFlavorSupported(DataFlavor sdf) {
183254885Sdumbbell        return df.equals(sdf);
184254885Sdumbbell    }
185254885Sdumbbell
186254885Sdumbbell    public Object getTransferData(DataFlavor tdf) throws UnsupportedFlavorException, IOException {
187254885Sdumbbell        Object copy = null;
188254885Sdumbbell        if( !df.equals(tdf) ){
189254885Sdumbbell            throw new UnsupportedFlavorException(tdf);
190254885Sdumbbell        }
191254885Sdumbbell        Container parent = getParent();
192254885Sdumbbell        switch (dropAction) {
193254885Sdumbbell            case DnDConstants.ACTION_COPY:
194254885Sdumbbell                try {
195254885Sdumbbell                    copy = this.clone();
196254885Sdumbbell                } catch (CloneNotSupportedException e) {
197254885Sdumbbell                    ByteArrayOutputStream baos = new ByteArrayOutputStream();
198254885Sdumbbell                    ObjectOutputStream oos = new ObjectOutputStream(baos);
199254885Sdumbbell
200254885Sdumbbell                    oos.writeObject(this);
201254885Sdumbbell                    ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
202254885Sdumbbell                    ObjectInputStream ois = new ObjectInputStream(bais);
203254885Sdumbbell                    try {
204254885Sdumbbell                        copy = ois.readObject();
205254885Sdumbbell                    } catch (ClassNotFoundException cnfe) {
206254885Sdumbbell                        // do nothing
207254885Sdumbbell                    }
208254885Sdumbbell                }
209254885Sdumbbell                parent.add(this);
210254885Sdumbbell                return copy;
211254885Sdumbbell
212254885Sdumbbell            case DnDConstants.ACTION_MOVE:
213254885Sdumbbell                synchronized (this) {
214254885Sdumbbell                    if (parent != null) {
215254885Sdumbbell                        parent.remove(this);
216254885Sdumbbell                        Label label = new Label("[empty]");
217254885Sdumbbell                        label.setBackground(Color.cyan);
218254885Sdumbbell                        label.setBounds(this.getBounds());
219254885Sdumbbell                        parent.add(label);
220254885Sdumbbell                    }
221254885Sdumbbell                }
222254885Sdumbbell                return this;
223254885Sdumbbell
224254885Sdumbbell            case DnDConstants.ACTION_LINK:
225254885Sdumbbell                return this;
226254885Sdumbbell
227254885Sdumbbell            default:
228254885Sdumbbell                return null;
229254885Sdumbbell        }
230254885Sdumbbell
231254885Sdumbbell    }
232254885Sdumbbell}
233254885Sdumbbell
234254885Sdumbbell