001: package net.refractions.udig.project.ui.internal.tool.display;
002:
003: import net.refractions.udig.project.ui.tool.ModalTool;
004:
005: import org.eclipse.core.runtime.IConfigurationElement;
006: import org.eclipse.jface.resource.ImageDescriptor;
007: import org.eclipse.swt.SWT;
008: import org.eclipse.swt.graphics.Cursor;
009: import org.eclipse.swt.widgets.Display;
010: import org.eclipse.ui.PlatformUI;
011: import org.eclipse.ui.plugin.AbstractUIPlugin;
012:
013: /**
014: * The cursor proxy allows for tool cursor images to be loaded lazily. It acts as a proxy
015: * for cursor image of the tool until the image is really needed to be displayed.
016: *
017: * @author Vitalus
018: * @since UDIG 1.1
019: *
020: */
021: public class CursorProxy {
022:
023: private volatile Cursor cursor;
024: private String imagePath;
025: private String hotspotX;
026: private String hotspotY;
027: private String cursorID;
028: private String pluginID;
029:
030: /**
031: * The constructor to create custom cursor proxy from extention.
032: *
033: * @param configuration
034: */
035: public CursorProxy(IConfigurationElement configuration) {
036: if (configuration != null) {
037: imagePath = configuration.getAttribute("image"); //$NON-NLS-1$
038: hotspotX = configuration.getAttribute("hotspotX"); //$NON-NLS-1$
039: hotspotY = configuration.getAttribute("hotspotY"); //$NON-NLS-1$
040: cursorID = configuration.getAttribute("id"); //$NON-NLS-1$
041: pluginID = configuration.getNamespace();
042: } else {
043: cursorID = ModalTool.DEFAULT_CURSOR;
044: }
045: }
046:
047: /**
048: * Returns cursor ID declared in extention point.
049: * ID is unique in extention registry.
050: *
051: * @return
052: */
053: public String getID() {
054: return cursorID;
055: }
056:
057: /**
058: * @return Returns the SWT cursor object.
059: */
060: public Cursor getCursor() {
061: if (cursor == null) {
062: synchronized (this ) {
063: if (cursor == null) {
064: if (imagePath == null) {
065: cursor = getSystemCursor(cursorID);
066: } else {
067: ImageDescriptor imageDescriptor = AbstractUIPlugin
068: .imageDescriptorFromPlugin(pluginID,
069: imagePath);
070: int x;
071: try {
072: x = Integer.parseInt(hotspotX);
073: } catch (Exception e) {
074: x = 0;
075: }
076: int y;
077: try {
078: y = Integer.parseInt(hotspotY);
079: } catch (Exception e) {
080: y = 0;
081: }
082: if (imageDescriptor == null
083: || imageDescriptor.getImageData() == null)
084: cursor = getSystemCursor(cursorID);
085: else
086: cursor = new Cursor(Display.getDefault(),
087: imageDescriptor.getImageData(), x,
088: y);
089: }
090: }
091: }
092: }
093:
094: return cursor;
095: }
096:
097: /**
098: * Returns system cursor object based on constants from <code>ModalTool</code>
099: * interface. These constants are mapped to SWT cursor constants.
100: *
101: * @param systemCursorID
102: * @return
103: */
104: static Cursor getSystemCursor(String systemCursorID) {
105: Display display = PlatformUI.getWorkbench().getDisplay();
106: if (systemCursorID == null)
107: return display.getSystemCursor(SWT.CURSOR_ARROW);
108:
109: if (systemCursorID.equals(ModalTool.CROSSHAIR_CURSOR))
110: return display.getSystemCursor(SWT.CURSOR_CROSS);
111: if (systemCursorID.equals(ModalTool.E_RESIZE_CURSOR))
112: return display.getSystemCursor(SWT.CURSOR_SIZEE);
113: if (systemCursorID.equals(ModalTool.HAND_CURSOR))
114: return display.getSystemCursor(SWT.CURSOR_HAND);
115: if (systemCursorID.equals(ModalTool.MOVE_CURSOR))
116: return display.getSystemCursor(SWT.CURSOR_SIZEALL);
117: if (systemCursorID.equals(ModalTool.N_RESIZE_CURSOR))
118: return display.getSystemCursor(SWT.CURSOR_SIZEN);
119: if (systemCursorID.equals(ModalTool.NE_RESIZE_CURSOR))
120: return display.getSystemCursor(SWT.CURSOR_SIZENE);
121: if (systemCursorID.equals(ModalTool.NW_RESIZE_CURSOR))
122: return display.getSystemCursor(SWT.CURSOR_SIZENW);
123: if (systemCursorID.equals(ModalTool.S_RESIZE_CURSOR))
124: return display.getSystemCursor(SWT.CURSOR_SIZES);
125: if (systemCursorID.equals(ModalTool.SE_RESIZE_CURSOR))
126: return display.getSystemCursor(SWT.CURSOR_SIZESE);
127: if (systemCursorID.equals(ModalTool.SW_RESIZE_CURSOR))
128: return display.getSystemCursor(SWT.CURSOR_SIZESW);
129: if (systemCursorID.equals(ModalTool.TEXT_CURSOR))
130: return display.getSystemCursor(SWT.CURSOR_IBEAM);
131: if (systemCursorID.equals(ModalTool.W_RESIZE_CURSOR))
132: return display.getSystemCursor(SWT.CURSOR_SIZESW);
133: if (systemCursorID.equals(ModalTool.WAIT_CURSOR))
134: return display.getSystemCursor(SWT.CURSOR_WAIT);
135:
136: if (systemCursorID.equals(ModalTool.NO_CURSOR))
137: return display.getSystemCursor(SWT.CURSOR_NO);
138:
139: return display.getSystemCursor(SWT.CURSOR_ARROW);
140: }
141:
142: /**
143: * Dispose the cursor.
144: */
145: public void dispose() {
146: if (cursor != null)
147: cursor.dispose();
148: cursor = null;
149: }
150: }
|