001: /*******************************************************************************
002: * Copyright (c) 2004, 2006 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.ui.internal;
011:
012: import org.eclipse.jface.resource.ImageDescriptor;
013: import org.eclipse.swt.SWT;
014: import org.eclipse.swt.graphics.Cursor;
015: import org.eclipse.swt.widgets.Display;
016:
017: /**
018: * Provides the set of cursors used for drag-and-drop.
019: */
020: public class DragCursors {
021: public static final int INVALID = 0;
022:
023: public static final int LEFT = 1;
024:
025: public static final int RIGHT = 2;
026:
027: public static final int TOP = 3;
028:
029: public static final int BOTTOM = 4;
030:
031: public static final int CENTER = 5;
032:
033: public static final int OFFSCREEN = 6;
034:
035: public static final int FASTVIEW = 7;
036:
037: private final static Cursor cursors[] = new Cursor[8];
038:
039: public static int positionToDragCursor(int swtPositionConstant) {
040: switch (swtPositionConstant) {
041: case SWT.LEFT:
042: return LEFT;
043: case SWT.RIGHT:
044: return RIGHT;
045: case SWT.TOP:
046: return TOP;
047: case SWT.BOTTOM:
048: return BOTTOM;
049: case SWT.CENTER:
050: return CENTER;
051: }
052:
053: return INVALID;
054: }
055:
056: /**
057: * Converts a drag cursor (LEFT, RIGHT, TOP, BOTTOM, CENTER) into an SWT constant
058: * (SWT.LEFT, SWT.RIGHT, SWT.TOP, SWT.BOTTOM, SWT.CENTER)
059: *
060: * @param dragCursorId
061: * @return an SWT.* constant
062: */
063: public static int dragCursorToSwtConstant(int dragCursorId) {
064: switch (dragCursorId) {
065: case LEFT:
066: return SWT.LEFT;
067: case RIGHT:
068: return SWT.RIGHT;
069: case TOP:
070: return SWT.TOP;
071: case BOTTOM:
072: return SWT.BOTTOM;
073: case CENTER:
074: return SWT.CENTER;
075: }
076:
077: return SWT.DEFAULT;
078: }
079:
080: /**
081: * Return the cursor for a drop scenario, as identified by code.
082: * Code must be one of INVALID, LEFT, RIGHT, TOP, etc.
083: * If the code is not found default to INVALID.
084: */
085: public static Cursor getCursor(int code) {
086: Display display = Display.getCurrent();
087: if (cursors[code] == null) {
088: ImageDescriptor source = null;
089: ImageDescriptor mask = null;
090: switch (code) {
091: case LEFT:
092: source = WorkbenchImages
093: .getImageDescriptor(IWorkbenchGraphicConstants.IMG_OBJS_DND_LEFT_SOURCE);
094: mask = WorkbenchImages
095: .getImageDescriptor(IWorkbenchGraphicConstants.IMG_OBJS_DND_LEFT_MASK);
096: cursors[LEFT] = new Cursor(display, source
097: .getImageData(), mask.getImageData(), 16, 16);
098: break;
099: case RIGHT:
100: source = WorkbenchImages
101: .getImageDescriptor(IWorkbenchGraphicConstants.IMG_OBJS_DND_RIGHT_SOURCE);
102: mask = WorkbenchImages
103: .getImageDescriptor(IWorkbenchGraphicConstants.IMG_OBJS_DND_RIGHT_MASK);
104: cursors[RIGHT] = new Cursor(display, source
105: .getImageData(), mask.getImageData(), 16, 16);
106: break;
107: case TOP:
108: source = WorkbenchImages
109: .getImageDescriptor(IWorkbenchGraphicConstants.IMG_OBJS_DND_TOP_SOURCE);
110: mask = WorkbenchImages
111: .getImageDescriptor(IWorkbenchGraphicConstants.IMG_OBJS_DND_TOP_MASK);
112: cursors[TOP] = new Cursor(display, source
113: .getImageData(), mask.getImageData(), 16, 16);
114: break;
115: case BOTTOM:
116: source = WorkbenchImages
117: .getImageDescriptor(IWorkbenchGraphicConstants.IMG_OBJS_DND_BOTTOM_SOURCE);
118: mask = WorkbenchImages
119: .getImageDescriptor(IWorkbenchGraphicConstants.IMG_OBJS_DND_BOTTOM_MASK);
120: cursors[BOTTOM] = new Cursor(display, source
121: .getImageData(), mask.getImageData(), 16, 16);
122: break;
123: case CENTER:
124: source = WorkbenchImages
125: .getImageDescriptor(IWorkbenchGraphicConstants.IMG_OBJS_DND_STACK_SOURCE);
126: mask = WorkbenchImages
127: .getImageDescriptor(IWorkbenchGraphicConstants.IMG_OBJS_DND_STACK_MASK);
128: cursors[CENTER] = new Cursor(display, source
129: .getImageData(), mask.getImageData(), 16, 16);
130: break;
131: case OFFSCREEN:
132: source = WorkbenchImages
133: .getImageDescriptor(IWorkbenchGraphicConstants.IMG_OBJS_DND_OFFSCREEN_SOURCE);
134: mask = WorkbenchImages
135: .getImageDescriptor(IWorkbenchGraphicConstants.IMG_OBJS_DND_OFFSCREEN_MASK);
136: cursors[OFFSCREEN] = new Cursor(display, source
137: .getImageData(), mask.getImageData(), 16, 16);
138: break;
139: case FASTVIEW:
140: source = WorkbenchImages
141: .getImageDescriptor(IWorkbenchGraphicConstants.IMG_OBJS_DND_TOFASTVIEW_SOURCE);
142: mask = WorkbenchImages
143: .getImageDescriptor(IWorkbenchGraphicConstants.IMG_OBJS_DND_TOFASTVIEW_MASK);
144: cursors[FASTVIEW] = new Cursor(Display.getCurrent(),
145: source.getImageData(), mask.getImageData(), 16,
146: 16);
147: default:
148: case INVALID:
149: source = WorkbenchImages
150: .getImageDescriptor(IWorkbenchGraphicConstants.IMG_OBJS_DND_INVALID_SOURCE);
151: mask = WorkbenchImages
152: .getImageDescriptor(IWorkbenchGraphicConstants.IMG_OBJS_DND_INVALID_MASK);
153: cursors[INVALID] = new Cursor(display, source
154: .getImageData(), mask.getImageData(), 16, 16);
155: break;
156: }
157: }
158: return cursors[code];
159: }
160:
161: /**
162: * Disposes all drag-and-drop cursors.
163: */
164: public static void dispose() {
165: for (int idx = 0; idx < cursors.length; idx++) {
166: cursors[idx].dispose();
167: cursors[idx] = null;
168: }
169: }
170: }
|