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:
042: // This file is currently unused. Rip it out if I don't plan to
043: // get text drag & drop handling working within the designer
044: // in flow mode.
045: package org.netbeans.modules.visualweb.designer;
046:
047: import java.awt.Toolkit;
048: import java.awt.dnd.DnDConstants;
049: import java.awt.event.InputEvent;
050: import java.awt.event.MouseEvent;
051: import java.awt.event.MouseListener;
052: import java.awt.event.MouseMotionListener;
053:
054: import javax.swing.JComponent;
055: import javax.swing.TransferHandler;
056:
057: /**
058: * COPIED from javax.swing.plaf.basic.BasicDragGestureRecognizer
059: * because that copy is package private.
060: *
061: *
062: * <p>
063: * Default drag gesture recognition for drag operations performed by classses
064: * that have a <code>dragEnabled</code> property. The gesture for a drag in
065: * this package is a mouse press over a selection followed by some movement
066: * by enough pixels to keep it from being treated as a click.
067: *
068: * @author Timothy Prinzing
069: * @version 1.5 01/23/03
070: */
071: public class BasicDragGestureRecognizer implements MouseListener,
072: MouseMotionListener {
073:
074: private MouseEvent dndArmedEvent = null;
075:
076: private static int motionThreshold;
077:
078: private static boolean checkedMotionThreshold = false;
079:
080: static int getMotionThreshold() {
081: if (checkedMotionThreshold) {
082: return motionThreshold;
083: } else {
084: checkedMotionThreshold = true;
085: try {
086: motionThreshold = ((Integer) Toolkit
087: .getDefaultToolkit().getDesktopProperty(
088: "DnD.gestureMotionThreshold"))
089: .intValue();
090: } catch (Exception e) {
091: motionThreshold = 5;
092: }
093: }
094: return motionThreshold;
095: }
096:
097: protected int mapDragOperationFromModifiers(MouseEvent e) {
098: int mods = e.getModifiersEx();
099:
100: if ((mods & InputEvent.BUTTON1_DOWN_MASK) != InputEvent.BUTTON1_DOWN_MASK) {
101: return TransferHandler.NONE;
102: }
103:
104: JComponent c = getComponent(e);
105: TransferHandler th = c.getTransferHandler();
106: // return SunDragSourceContextPeer.convertModifiersToDropAction(mods, th.getSourceActions(c));
107: return convertModifiersToDropAction(mods, th
108: .getSourceActions(c));
109: }
110:
111: // XXX Moved from DesignerUtils.
112: /** XXX Copied from SunDragSourceContextPeer, to avoid dependency on sun jdk. */
113: private static int convertModifiersToDropAction(
114: final int modifiers, final int supportedActions) {
115: int dropAction = DnDConstants.ACTION_NONE;
116:
117: /*
118: * Fix for 4285634.
119: * Calculate the drop action to match Motif DnD behavior.
120: * If the user selects an operation (by pressing a modifier key),
121: * return the selected operation or ACTION_NONE if the selected
122: * operation is not supported by the drag source.
123: * If the user doesn't select an operation search the set of operations
124: * supported by the drag source for ACTION_MOVE, then for
125: * ACTION_COPY, then for ACTION_LINK and return the first operation
126: * found.
127: */
128: switch (modifiers
129: & (InputEvent.SHIFT_DOWN_MASK | InputEvent.CTRL_DOWN_MASK)) {
130: case InputEvent.SHIFT_DOWN_MASK | InputEvent.CTRL_DOWN_MASK:
131: dropAction = DnDConstants.ACTION_LINK;
132: break;
133: case InputEvent.CTRL_DOWN_MASK:
134: dropAction = DnDConstants.ACTION_COPY;
135: break;
136: case InputEvent.SHIFT_DOWN_MASK:
137: dropAction = DnDConstants.ACTION_MOVE;
138: break;
139: default:
140: if ((supportedActions & DnDConstants.ACTION_MOVE) != 0) {
141: dropAction = DnDConstants.ACTION_MOVE;
142: } else if ((supportedActions & DnDConstants.ACTION_COPY) != 0) {
143: dropAction = DnDConstants.ACTION_COPY;
144: } else if ((supportedActions & DnDConstants.ACTION_LINK) != 0) {
145: dropAction = DnDConstants.ACTION_LINK;
146: }
147: }
148:
149: return dropAction & supportedActions;
150: }
151:
152: public void mouseClicked(MouseEvent e) {
153: }
154:
155: public void mousePressed(MouseEvent e) {
156: dndArmedEvent = null;
157: if (isDragPossible(e)
158: && mapDragOperationFromModifiers(e) != TransferHandler.NONE) {
159: dndArmedEvent = e;
160: e.consume();
161: }
162: }
163:
164: public void mouseReleased(MouseEvent e) {
165: dndArmedEvent = null;
166: }
167:
168: public void mouseEntered(MouseEvent e) {
169: //dndArmedEvent = null;
170: }
171:
172: public void mouseExited(MouseEvent e) {
173: //if (dndArmedEvent != null && mapDragOperationFromModifiers(e) == TransferHandler.NONE) {
174: // dndArmedEvent = null;
175: //}
176: }
177:
178: public void mouseDragged(MouseEvent e) {
179: if (dndArmedEvent != null) {
180: e.consume();
181:
182: int action = mapDragOperationFromModifiers(e);
183: if (action == TransferHandler.NONE) {
184: return;
185: }
186:
187: int dx = Math.abs(e.getX() - dndArmedEvent.getX());
188: int dy = Math.abs(e.getY() - dndArmedEvent.getY());
189: if ((dx > getMotionThreshold())
190: || (dy > getMotionThreshold())) {
191: // start transfer... shouldn't be a click at this point
192: JComponent c = getComponent(e);
193: TransferHandler th = c.getTransferHandler();
194: th.exportAsDrag(c, dndArmedEvent, action);
195: dndArmedEvent = null;
196: }
197: }
198: }
199:
200: public void mouseMoved(MouseEvent e) {
201: }
202:
203: /**
204: * Determines if the following are true:
205: * <ul>
206: * <li>the press event is located over a selection
207: * <li>the dragEnabled property is true
208: * <li>A TranferHandler is installed
209: * </ul>
210: * <p>
211: * This is implemented to check for a TransferHandler.
212: * Subclasses should perform the remaining conditions.
213: */
214: protected boolean isDragPossible(MouseEvent e) {
215: JComponent c = getComponent(e);
216: return (c == null) ? true : (c.getTransferHandler() != null);
217: }
218:
219: protected JComponent getComponent(MouseEvent e) {
220: Object src = e.getSource();
221: if (src instanceof JComponent) {
222: JComponent c = (JComponent) src;
223: return c;
224: }
225: return null;
226: }
227:
228: }
|