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:
020: package org.netbeans.modules.soa.mapper.basicmapper.util;
021:
022: import java.awt.Cursor;
023: import java.awt.datatransfer.DataFlavor;
024: import java.awt.datatransfer.Transferable;
025: import java.awt.dnd.DnDConstants;
026: import java.awt.dnd.DragGestureEvent;
027: import java.awt.dnd.DragGestureListener;
028: import java.awt.dnd.DragGestureRecognizer;
029: import java.awt.dnd.DragSource;
030: import java.awt.dnd.DragSourceDragEvent;
031: import java.awt.dnd.DragSourceDropEvent;
032: import java.awt.dnd.DragSourceEvent;
033: import java.awt.dnd.DragSourceListener;
034: import java.awt.event.MouseEvent;
035: import java.awt.event.MouseMotionAdapter;
036: import java.util.Collections;
037: import java.util.HashMap;
038: import java.util.Map;
039:
040: import javax.swing.Icon;
041: import javax.swing.JButton;
042:
043: /**
044: * <p>
045: *
046: * Title: </p> DragableLabel <p>
047: *
048: * Description: </p> DragableLabel provide a JLabel with a drag guesture
049: * listener. <p>
050: *
051: * @author Un Seng Leong
052: * @created December 4, 2002
053: */
054: public class DragableLabel extends JButton implements
055: DragGestureListener, Transferable {
056:
057: /**
058: * The default data flavor of this class.
059: */
060: protected static final DataFlavor DEFAULT_FLAVORS[] = new DataFlavor[1];
061:
062: /**
063: * Is this label draggable.
064: */
065: protected boolean mIsDraggable = true;
066:
067: /**
068: * The cursor of this drag operation. Default to copy drop cursor.
069: */
070: protected Cursor mDragCursor = DragSource.DefaultCopyDrop;
071:
072: /**
073: * data flavor and transfer object storage
074: */
075: protected Map dataFlavorMap;
076:
077: /**
078: * The default transfer data. Default reference to this object instance.
079: */
080: protected Object mDefaultData;
081:
082: /**
083: * the gesture recognizer from default drag source.
084: */
085: protected DragGestureRecognizer mRecognizer;
086:
087: static {
088: try {
089: DEFAULT_FLAVORS[0] = new DataFlavor(
090: DataFlavor.javaJVMLocalObjectMimeType);
091: } catch (ClassNotFoundException ex) {
092: // ignore
093: }
094: }
095:
096: /**
097: * Creates a DragableJLabel instance with no image and with an empty string
098: * for the title.
099: */
100: public DragableLabel() {
101: super ();
102: init();
103: }
104:
105: /**
106: * Creates a DragableJLabel instance with the specified image.
107: *
108: * @param image the image of this label
109: */
110: public DragableLabel(Icon image) {
111: super (image);
112: init();
113: }
114:
115: /**
116: * Creates a DragableJLabel instance with the specified image and horizontal
117: * alignment.
118: *
119: * @param image the image of this label
120: * @param horizontalAlignment the horizontal aligmenet
121: */
122: public DragableLabel(Icon image, int horizontalAlignment) {
123: super (image);
124: this .setHorizontalAlignment(horizontalAlignment);
125: init();
126: }
127:
128: /**
129: * Creates a DragableJLabel instance with the specified text.
130: *
131: * @param text the text of this label.
132: */
133: public DragableLabel(String text) {
134: super (text);
135: init();
136: }
137:
138: /**
139: * Creates a DragableJLabel instance with the specified text, image, and
140: * horizontal alignment.
141: *
142: * @param text the text of this label
143: * @param icon the icon of this label.
144: * @param horizontalAlignment the horizontal aligment
145: */
146: public DragableLabel(String text, Icon icon, int horizontalAlignment) {
147: super (text, icon);
148: this .setHorizontalAlignment(horizontalAlignment);
149: init();
150: }
151:
152: /**
153: * Creates a DragableJLabel instance with the specified text and horizontal
154: * alignment.
155: *
156: * @param text the text of this label
157: * @param horizontalAlignment the horizontal aligment
158: */
159: public DragableLabel(String text, int horizontalAlignment) {
160: super (text);
161: this .setHorizontalAlignment(horizontalAlignment);
162: init();
163: }
164:
165: /**
166: * Return the default data flavor DataFlavor.javaJVMLocalObjectMimeType. Or
167: * null if the VM does not support DataFlavor.javaJVMLocalObjectMimeType.
168: *
169: * @return the default data flavor of this drag operation.
170: */
171: public DataFlavor getDefaultDataFlavor() {
172: return DEFAULT_FLAVORS[0];
173: }
174:
175: /**
176: * Retrun the drag action of this dragable.
177: *
178: * @return the drag action of this dragable.
179: */
180: public int getDragAction() {
181: return mRecognizer.getSourceActions();
182: }
183:
184: /**
185: * Retrun the cursor for this drag operation.
186: *
187: * @return the cursor to display during this drag operation.
188: */
189: public Cursor getDragCursor() {
190: return mDragCursor;
191: }
192:
193: /**
194: * Default getTransferData implementation for Transferable.
195: *
196: * @param flavor Description of the Parameter
197: * @return The transferData value
198: */
199: public Object getTransferData(DataFlavor flavor) {
200: return dataFlavorMap.get(flavor);
201: }
202:
203: /**
204: * Retrieves an array of valid data flavors.
205: *
206: * @return The transferDataFlavors value
207: */
208: public DataFlavor[] getTransferDataFlavors() {
209: return (DataFlavor[]) dataFlavorMap.keySet().toArray(
210: new DataFlavor[0]);
211: }
212:
213: /**
214: * Checks whether the data flavor is supported.
215: *
216: * @param flavor Description of the Parameter
217: * @return The dataFlavorSupported value
218: */
219: public boolean isDataFlavorSupported(DataFlavor flavor) {
220: DataFlavor dataFlavors[] = this .getTransferDataFlavors();
221:
222: if ((dataFlavors != null) && (dataFlavors.length > 0)) {
223: for (int i = 0; i < dataFlavors.length; i++) {
224: if (flavor.equals(dataFlavors[i])) {
225: return true;
226: }
227: }
228: }
229:
230: return false;
231: }
232:
233: /**
234: * Determines whether this button isDraggable
235: *
236: * @return The draggable value
237: */
238: public boolean isDraggable() {
239: return mIsDraggable;
240: }
241:
242: /**
243: * Set the default transfer data object. This object is returned when <code>getTransferData</code>
244: * has been passed the default data flavor as its paramter. If data is null,
245: * transfer data will be this object instance.
246: *
247: * @param data the defautl transfer data for the drag operation.
248: */
249: public void setDefaultTransferData(Object data) {
250: if (data == null) {
251: dataFlavorMap.put(getDefaultDataFlavor(), this );
252: }
253:
254: dataFlavorMap.put(getDefaultDataFlavor(), data);
255: }
256:
257: /**
258: * Set the drag action of this label.
259: *
260: * @param dragAction the drag action of this label.
261: */
262: public void setDragAction(int dragAction) {
263: mRecognizer.setSourceActions(dragAction);
264: }
265:
266: /**
267: * Set the cursor for this drag operation. Default to <code>DragSource.DefaultCopyDrop</code>
268: * if dragCursor is null.
269: *
270: * @param dragCursor the cursor to display during this drag operation.
271: */
272: public void setDragCursor(Cursor dragCursor) {
273: if (dragCursor == null) {
274: mDragCursor = DragSource.DefaultCopyDrop;
275: }
276:
277: mDragCursor = dragCursor;
278: }
279:
280: /**
281: * Sets the button's draggablity
282: *
283: * @param val The new draggable value
284: */
285: public void setDraggable(boolean val) {
286: mIsDraggable = val;
287: }
288:
289: /**
290: * Add a data flavor for this drag action.
291: *
292: * @param flavor the dataflavor of this drag action.
293: * @param transferData the object return by this dataflavor.
294: */
295: public void addDataFlavor(DataFlavor flavor, Object transferData) {
296: dataFlavorMap.put(flavor, transferData);
297: }
298:
299: /**
300: * Add a drag source listener for this drag operation.
301: *
302: * @param listener the listener to be added
303: */
304: public void addDragSourceListener(DragSourceListener listener) {
305: DragSource.getDefaultDragSource().addDragSourceListener(
306: listener);
307: }
308:
309: /**
310: * DOCUMENT ME!
311: *
312: * @param event Description of the Parameter
313: */
314: public void dragDropEnd(DragSourceDropEvent event) {
315: setCursor(Cursor.getDefaultCursor());
316: }
317:
318: /**
319: * DOCUMENT ME!
320: *
321: * @param event Description of the Parameter
322: */
323: public void dragEnter(DragSourceDragEvent event) {
324: }
325:
326: /**
327: * DOCUMENT ME!
328: *
329: * @param event Description of the Parameter
330: */
331: public void dragExit(DragSourceEvent event) {
332: DragableLabel.this .doClick(0);
333:
334: }
335:
336: /**
337: * Default drag and drop interface implementation
338: *
339: * @param event Description of the Parameter
340: */
341: public void dragGestureRecognized(DragGestureEvent event) {
342: if (this .isEnabled() && this .isVisible() && this .isDraggable()) {
343: DragSource.getDefaultDragSource().startDrag(event,
344: mDragCursor, this , null);
345:
346: }
347: }
348:
349: /**
350: * DOCUMENT ME!
351: *
352: * @param event Description of the Parameter
353: */
354: public void dragOver(DragSourceDragEvent event) {
355: }
356:
357: /**
358: * DOCUMENT ME!
359: *
360: * @param event Description of the Parameter
361: */
362: public void dropActionChanged(DragSourceDragEvent event) {
363: }
364:
365: /**
366: * Remove a data flavor for this drag action.
367: *
368: * @param flavor the data flavor to be removed.
369: */
370: public void removeDataFlavor(DataFlavor flavor) {
371: dataFlavorMap.remove(flavor);
372: }
373:
374: /**
375: * DOCUMENT ME!
376: *
377: * @param listener DOCUMENT ME!
378: */
379: public void removeDragSourceListener(DragSourceListener listener) {
380: DragSource.getDefaultDragSource().removeDragSourceListener(
381: listener);
382: }
383:
384: /**
385: * Initialize this Object with the following steps: 1. initialize a
386: * DragGestureRecognizer to start listener on the gesture. 2. set the
387: * default transfer data to this object instance.
388: */
389: private void init() {
390: mRecognizer = DragSource.getDefaultDragSource()
391: .createDefaultDragGestureRecognizer(this ,
392: DnDConstants.ACTION_COPY_OR_MOVE, this );
393: dataFlavorMap = Collections.synchronizedMap(new HashMap());
394: dataFlavorMap.put(DEFAULT_FLAVORS[0], this );
395: this .addMouseMotionListener(new MouseMotionAdapter() {
396: public void mouseExit(MouseEvent event) {
397: DragableLabel.this .doClick(0);
398: }
399: });
400: }
401: }
|