001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041: package org.netbeans.modules.sql.framework.ui.graph.view.impl;
042:
043: import java.awt.Cursor;
044: import java.awt.datatransfer.DataFlavor;
045: import java.awt.datatransfer.Transferable;
046: import java.awt.dnd.DnDConstants;
047: import java.awt.dnd.DragGestureEvent;
048: import java.awt.dnd.DragGestureListener;
049: import java.awt.dnd.DragGestureRecognizer;
050: import java.awt.dnd.DragSource;
051: import java.awt.dnd.DragSourceDragEvent;
052: import java.awt.dnd.DragSourceDropEvent;
053: import java.awt.dnd.DragSourceEvent;
054: import java.awt.dnd.DragSourceListener;
055: import java.awt.event.MouseMotionAdapter;
056: import java.util.Collections;
057: import java.util.HashMap;
058: import java.util.Map;
059:
060: import javax.swing.Icon;
061: import javax.swing.JButton;
062:
063: /**
064: * Base dragable menu item class
065: *
066: * @author Girish Patil
067: * @version $Revision$
068: */
069: public class BaseDragableMenuItem extends JButton implements
070: DragGestureListener, Transferable {
071:
072: /**
073: * The default data flavor of this class.
074: */
075: protected static final DataFlavor DEFAULT_FLAVORS[] = new DataFlavor[1];
076:
077: /**
078: * Is this label draggable.
079: */
080: protected boolean mIsDraggable = true;
081:
082: /**
083: * The cursor of this drag operation. Default to copy drop cursor.
084: */
085: protected Cursor mDragCursor = DragSource.DefaultCopyDrop;
086:
087: /**
088: * data flavor and transfer object storage
089: */
090: protected Map dataFlavorMap;
091:
092: /**
093: * The default transfer data. Default reference to this object instance.
094: */
095: protected Object mDefaultData;
096:
097: /**
098: * the gesture recognizer from default drag source.
099: */
100: protected DragGestureRecognizer mRecognizer;
101:
102: static {
103: try {
104: DEFAULT_FLAVORS[0] = new DataFlavor(
105: DataFlavor.javaJVMLocalObjectMimeType);
106: } catch (ClassNotFoundException ex) {
107: // Ignore
108: }
109: }
110:
111: /**
112: * Creates a DragableJLabel instance with no image and with an empty string for the
113: * title.
114: */
115: public BaseDragableMenuItem() {
116: super ();
117: init();
118: }
119:
120: /**
121: * Creates a BaseDragableMenuItem instance with the specified image.
122: *
123: * @param image the image of this label
124: */
125: public BaseDragableMenuItem(Icon image) {
126: super (image);
127: init();
128: }
129:
130: /**
131: * Creates a BaseDragableMenuItem instance with the specified image and horizontal
132: * alignment.
133: *
134: * @param image the image of this label
135: * @param horizontalAlignment the horizontal aligmenet
136: */
137: public BaseDragableMenuItem(Icon image, int horizontalAlignment) {
138: super (image);
139: this .setHorizontalAlignment(horizontalAlignment);
140: init();
141: }
142:
143: /**
144: * Creates a BaseDragableMenuItem instance with the specified text.
145: *
146: * @param text the text of this label.
147: */
148: public BaseDragableMenuItem(String text) {
149: super (text);
150: init();
151: }
152:
153: /**
154: * Creates a BaseDragableMenuItem instance with the specified text, image, and
155: * horizontal alignment.
156: *
157: * @param text the text of this label
158: * @param icon the icon of this label.
159: * @param horizontalAlignment the horizontal aligment
160: */
161: public BaseDragableMenuItem(String text, Icon icon,
162: int horizontalAlignment) {
163: super (text, icon);
164: this .setHorizontalAlignment(horizontalAlignment);
165: init();
166: }
167:
168: /**
169: * Creates a BaseDragableMenuItem instance with the specified text and horizontal
170: * alignment.
171: *
172: * @param text the text of this label
173: * @param horizontalAlignment the horizontal aligment
174: */
175: public BaseDragableMenuItem(String text, int horizontalAlignment) {
176: super (text);
177: this .setHorizontalAlignment(horizontalAlignment);
178: init();
179: }
180:
181: /**
182: * Return the default data flavor DataFlavor.javaJVMLocalObjectMimeType. Or null if
183: * the VM does not support DataFlavor.javaJVMLocalObjectMimeType.
184: *
185: * @return the default data flavor of this drag operation.
186: */
187: public DataFlavor getDefaultDataFlavor() {
188: return DEFAULT_FLAVORS[0];
189: }
190:
191: /**
192: * Retrun the drag action of this dragable.
193: *
194: * @return the drag action of this dragable.
195: */
196: public int getDragAction() {
197: return mRecognizer.getSourceActions();
198: }
199:
200: /**
201: * Retrun the cursor for this drag operation.
202: *
203: * @return the cursor to display during this drag operation.
204: */
205: public Cursor getDragCursor() {
206: return mDragCursor;
207: }
208:
209: /**
210: * Default getTransferData implementation for Transferable.
211: *
212: * @param flavor Description of the Parameter
213: * @return The transferData value
214: */
215: public Object getTransferData(DataFlavor flavor) {
216: return dataFlavorMap.get(flavor);
217: }
218:
219: /**
220: * Retrieves an array of valid data flavors.
221: *
222: * @return The transferDataFlavors value
223: */
224: public DataFlavor[] getTransferDataFlavors() {
225: return (DataFlavor[]) dataFlavorMap.keySet().toArray(
226: new DataFlavor[0]);
227: }
228:
229: /**
230: * Checks whether the data flavor is supported.
231: *
232: * @param flavor Description of the Parameter
233: * @return The dataFlavorSupported value
234: */
235: public boolean isDataFlavorSupported(DataFlavor flavor) {
236: DataFlavor dataFlavors[] = this .getTransferDataFlavors();
237:
238: if ((dataFlavors != null) && (dataFlavors.length > 0)) {
239: for (int i = 0; i < dataFlavors.length; i++) {
240: if (flavor.equals(dataFlavors[i])) {
241: return true;
242: }
243: }
244: }
245:
246: return false;
247: }
248:
249: /**
250: * Determines whether this button isDraggable
251: *
252: * @return The draggable value
253: */
254: public boolean isDraggable() {
255: return mIsDraggable;
256: }
257:
258: /**
259: * Set the default transfer data object. This object is returned when
260: * <code>getTransferData</code> has been passed the default data flavor as its
261: * paramter. If data is null, transfer data will be this object instance.
262: *
263: * @param data the defautl transfer data for the drag operation.
264: */
265: public void setDefaultTransferData(Object data) {
266: if (data == null) {
267: dataFlavorMap.put(getDefaultDataFlavor(), this );
268: }
269:
270: dataFlavorMap.put(getDefaultDataFlavor(), data);
271: }
272:
273: /**
274: * Set the drag action of this label.
275: *
276: * @param dragAction the drag action of this label.
277: */
278: public void setDragAction(int dragAction) {
279: mRecognizer.setSourceActions(dragAction);
280: }
281:
282: /**
283: * Set the cursor for this drag operation. Default to
284: * <code>DragSource.DefaultCopyDrop</code> if dragCursor is null.
285: *
286: * @param dragCursor the cursor to display during this drag operation.
287: */
288: public void setDragCursor(Cursor dragCursor) {
289: if (dragCursor == null) {
290: mDragCursor = DragSource.DefaultCopyDrop;
291: }
292:
293: mDragCursor = dragCursor;
294: }
295:
296: /**
297: * Sets the button's draggablity
298: *
299: * @param val The new draggable value
300: */
301: public void setDraggable(boolean val) {
302: mIsDraggable = val;
303: }
304:
305: /**
306: * Add a data flavor for this drag action.
307: *
308: * @param flavor the dataflavor of this drag action.
309: * @param transferData the object return by this dataflavor.
310: */
311: public void addDataFlavor(DataFlavor flavor, Object transferData) {
312: dataFlavorMap.put(flavor, transferData);
313: }
314:
315: /**
316: * Add a drag source listener for this drag operation.
317: *
318: * @param listener the listener to be added
319: */
320: public void addDragSourceListener(DragSourceListener listener) {
321: DragSource.getDefaultDragSource().addDragSourceListener(
322: listener);
323: }
324:
325: public void dragDropEnd(DragSourceDropEvent event) {
326: setCursor(Cursor.getDefaultCursor());
327: }
328:
329: public void dragEnter(DragSourceDragEvent event) {
330: }
331:
332: public void dragExit(DragSourceEvent event) {
333: BaseDragableMenuItem.this .doClick(0);
334: }
335:
336: protected Transferable getTransferable() {
337: return this ;
338: }
339:
340: public void dragGestureRecognized(DragGestureEvent event) {
341: if (this .isEnabled() && this .isVisible() && this .isDraggable()) {
342: DragSource.getDefaultDragSource().startDrag(event,
343: mDragCursor, getTransferable(), null);
344:
345: }
346: }
347:
348: public void dragOver(DragSourceDragEvent event) {
349: }
350:
351: public void dropActionChanged(DragSourceDragEvent event) {
352: }
353:
354: /**
355: * Remove a data flavor for this drag action.
356: *
357: * @param flavor the data flavor to be removed.
358: */
359: public void removeDataFlavor(DataFlavor flavor) {
360: dataFlavorMap.remove(flavor);
361: }
362:
363: public void removeDragSourceListener(DragSourceListener listener) {
364: DragSource.getDefaultDragSource().removeDragSourceListener(
365: listener);
366: }
367:
368: /**
369: * Initialize this Object with the following steps: 1. initialize a
370: * DragGestureRecognizer to start listener on the gesture. 2. set the default transfer
371: * data to this object instance.
372: */
373: private void init() {
374: mRecognizer = DragSource.getDefaultDragSource()
375: .createDefaultDragGestureRecognizer(this ,
376: DnDConstants.ACTION_COPY_OR_MOVE, this );
377: dataFlavorMap = Collections.synchronizedMap(new HashMap());
378: dataFlavorMap.put(DEFAULT_FLAVORS[0], this );
379: this .addMouseMotionListener(new MouseMotionAdapter() {
380: // public void mouseExit(MouseEvent event) {
381: // BaseDragableMenuItem.this.doClick(0);
382: // }
383: });
384: }
385: }
|