001: /*
002: * The contents of this file are subject to the terms of the Common Development
003: * and Distribution License (the License). You may not use this file except in
004: * compliance with the License.
005: *
006: * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
007: * or http://www.netbeans.org/cddl.txt.
008: *
009: * When distributing Covered Code, include this CDDL Header Notice in each file
010: * and include the License file at http://www.netbeans.org/cddl.txt.
011: * If applicable, add the following below the CDDL Header, with the fields
012: * enclosed by brackets [] replaced by your own identifying information:
013: * "Portions Copyrighted [year] [name of copyright owner]"
014: *
015: * The Original Software is NetBeans. The Initial Developer of the Original
016: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
017: * Microsystems, Inc. All Rights Reserved.
018: */
019: package org.netbeans.modules.soa.mappercore.dnd;
020:
021: import java.awt.datatransfer.Transferable;
022: import java.awt.dnd.DragGestureEvent;
023: import java.awt.dnd.DragGestureListener;
024: import java.awt.dnd.DragGestureRecognizer;
025: import java.awt.dnd.DragSource;
026: import java.awt.dnd.DragSourceDragEvent;
027: import java.awt.dnd.DragSourceDropEvent;
028: import java.awt.dnd.DragSourceEvent;
029: import java.awt.dnd.DragSourceListener;
030: import java.awt.dnd.DropTarget;
031: import java.awt.dnd.DropTargetDragEvent;
032: import java.awt.dnd.DropTargetDropEvent;
033: import java.awt.dnd.DropTargetEvent;
034: import java.awt.dnd.DropTargetListener;
035: import java.awt.event.MouseEvent;
036: import javax.swing.JComponent;
037:
038: /**
039: *
040: * @author anjeleevich
041: */
042: public class DnDSupport implements DnDConstants {
043:
044: private DnDHandler dndHandler;
045: private JComponent component;
046:
047: private DragSource dragSource;
048: private DropTarget dropTarget;
049:
050: private DnDRecognizer recognizer;
051:
052: private Transferable cachedTransferable;
053:
054: private DragHandler dragHandler;
055: private DropHandler dropHandler;
056:
057: public DnDSupport(JComponent component, DnDHandler dndHandler) {
058: this .component = component;
059: this .dndHandler = dndHandler;
060:
061: dropHandler = new DropHandler();
062: dropTarget = new DropTarget(component, dropHandler);
063:
064: }
065:
066: private DragHandler getDragHandler() {
067: if (dragHandler == null) {
068: dragHandler = new DragHandler();
069: }
070: return dragHandler;
071: }
072:
073: private DragSource getDragSource() {
074: if (dragSource == null) {
075: dragSource = new DragSource();
076: }
077: return dragSource;
078: }
079:
080: private DnDRecognizer getDnDRecognizer() {
081: if (recognizer == null) {
082: recognizer = new DnDRecognizer(getDragSource(),
083: getDragHandler());
084: }
085: return recognizer;
086: }
087:
088: public void startDrag(MouseEvent event, Transferable transferable,
089: int action) {
090: if (action != NONE) {
091: cachedTransferable = transferable;
092: getDnDRecognizer().startDrag(component, event,
093: COPY | MOVE | LINK, action);
094: }
095: }
096:
097: private class DropHandler implements DropTargetListener {
098: public void dragEnter(DropTargetDragEvent dtde) {
099: dndHandler.drag(component, dtde);
100: }
101:
102: public void dragOver(DropTargetDragEvent dtde) {
103: dndHandler.drag(component, dtde);
104: }
105:
106: public void dropActionChanged(DropTargetDragEvent dtde) {
107: }
108:
109: public void dragExit(DropTargetEvent dte) {
110: dndHandler.dragExit(component);
111: }
112:
113: public void drop(DropTargetDropEvent dtde) {
114: dndHandler.drop(component, dtde);
115: }
116: }
117:
118: private class DragHandler implements DragSourceListener,
119: DragGestureListener {
120:
121: public void dragGestureRecognized(DragGestureEvent dge) {
122: if (cachedTransferable != null) {
123: try {
124: dge.startDrag(null, cachedTransferable, this );
125: } finally {
126: cachedTransferable = null;
127: }
128: }
129: }
130:
131: public void dragEnter(DragSourceDragEvent dsde) {
132: }
133:
134: public void dragOver(DragSourceDragEvent dsde) {
135: }
136:
137: public void dropActionChanged(DragSourceDragEvent dsde) {
138: }
139:
140: public void dragExit(DragSourceEvent dse) {
141: }
142:
143: public void dragDropEnd(DragSourceDropEvent dsde) {
144: dndHandler.dragDone(component, dsde);
145: }
146: }
147:
148: private static class DnDRecognizer extends DragGestureRecognizer {
149:
150: DnDRecognizer(DragSource dragSource, DragGestureListener dgl) {
151: super (dragSource, null, NONE, dgl);
152: }
153:
154: void startDrag(JComponent component, MouseEvent event,
155: int srcActions, int action) {
156: setComponent(component);
157: setSourceActions(srcActions);
158: appendEvent(event);
159: fireDragGestureRecognized(action, event.getPoint());
160: }
161:
162: protected void registerListeners() {
163: }
164:
165: protected void unregisterListeners() {
166: }
167: }
168: }
|