Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions org.eclipse.gef/src/org/eclipse/gef/SharedCursors.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import org.eclipse.draw2d.Cursors;

import org.eclipse.gef.internal.InternalGEFPlugin;
import org.eclipse.gef.internal.InternalImages;

/**
Expand Down Expand Up @@ -45,15 +46,15 @@ public class SharedCursors extends Cursors {
public static final Cursor CURSOR_TREE_MOVE;

static {
CURSOR_PLUG = createCursor("icons/plug-cursor.png"); //$NON-NLS-1$
CURSOR_PLUG_NOT = createCursor("icons/plugnot-cursor.png"); //$NON-NLS-1$
CURSOR_TREE_ADD = createCursor("icons/tree_add-cursor.png"); //$NON-NLS-1$
CURSOR_TREE_MOVE = createCursor("icons/tree_move-cursor.png"); //$NON-NLS-1$
CURSOR_PLUG = createCursor("icons/plug-cursor.svg"); //$NON-NLS-1$
CURSOR_PLUG_NOT = createCursor("icons/plugnot-cursor.svg"); //$NON-NLS-1$
CURSOR_TREE_ADD = createCursor("icons/tree_add-cursor.svg"); //$NON-NLS-1$
CURSOR_TREE_MOVE = createCursor("icons/tree_move-cursor.svg"); //$NON-NLS-1$
}

private static Cursor createCursor(String sourceName) {
ImageDescriptor src = InternalImages.createDescriptor(sourceName);
return new Cursor(null, src.getImageData(100), 0, 0);
return InternalGEFPlugin.createCursor(src, 0, 0);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,16 @@
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.nio.charset.StandardCharsets;

import org.eclipse.swt.SWTException;
import org.eclipse.swt.graphics.Cursor;
import org.eclipse.swt.graphics.Device;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.ImageData;
import org.eclipse.swt.graphics.ImageDataProvider;
import org.eclipse.swt.graphics.ImageLoader;

import org.eclipse.core.runtime.Platform;
Expand Down Expand Up @@ -146,4 +151,23 @@ public static boolean isSvgSupported() {
}
return isSvgSupported;
}

/**
* This method attempts to create the cursor using a constructor introduced in
* SWT 3.131.0 that takes an {@link ImageDataProvider}. If this constructor is
* not available (SWT versions prior to 3.131.0), it falls back to using the
* older constructor that accepts {@link ImageData}.
*/
public static Cursor createCursor(ImageDescriptor source, int hotspotX, int hotspotY) {
try {
Constructor<Cursor> ctor = Cursor.class.getConstructor(Device.class, ImageDataProvider.class, int.class,
int.class);
return ctor.newInstance(null, (ImageDataProvider) source::getImageData, hotspotX, hotspotY);
} catch (NoSuchMethodException e) {
// SWT version < 3.131.0 (no ImageDataProvider-based constructor)
return new Cursor(null, source.getImageData(100), hotspotX, hotspotY); // older constructor
} catch (InstantiationException | IllegalAccessException | InvocationTargetException e) {
throw new RuntimeException("Failed to instantiate Cursor", e); //$NON-NLS-1$
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@
import org.eclipse.swt.graphics.Cursor;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.ImageData;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.widgets.Canvas;
Expand Down Expand Up @@ -1608,13 +1607,10 @@ public static Cursor getCursor(int code) {
*/
private static Cursor createCursor(String sourceName) {
ImageDescriptor source = PlatformUI.getWorkbench().getSharedImages().getImageDescriptor(sourceName);
int deviceZoom = InternalGEFPlugin.getOrDefaultDeviceZoom();
// Scales the image if the display is using neither 100% nor 200% zoom
ImageData sourceData = InternalGEFPlugin.scaledImageData(source, deviceZoom);
// Hotspot should be the center of the image. e.g. (16, 16) on 100% zoom
int hotspotX = sourceData.width / 2;
int hotspotY = sourceData.height / 2;
return new Cursor(null, sourceData, hotspotX, hotspotY);
int hotspotX = source.getImageData(100).width / 2;
int hotspotY = source.getImageData(100).height / 2;
return InternalGEFPlugin.createCursor(source, hotspotX, hotspotY);
}
}
}
Loading