|
Added
Link Here
|
| 1 |
/******************************************************************************* |
| 2 |
* Copyright (c) 2010 IBM Corporation and others. |
| 3 |
* All rights reserved. This program and the accompanying materials |
| 4 |
* are made available under the terms of the Eclipse Public License v1.0 |
| 5 |
* which accompanies this distribution, and is available at |
| 6 |
* http://www.eclipse.org/legal/epl-v10.html |
| 7 |
* |
| 8 |
* Contributors: |
| 9 |
* Daoen Pan <pan.daoen@consideo.com> |
| 10 |
*******************************************************************************/ |
| 11 |
package org.eclipse.ui.internal.texteditor; |
| 12 |
|
| 13 |
import org.eclipse.swt.graphics.Cursor; |
| 14 |
import org.eclipse.swt.graphics.ImageData; |
| 15 |
import org.eclipse.swt.graphics.PaletteData; |
| 16 |
import org.eclipse.swt.graphics.RGB; |
| 17 |
import org.eclipse.swt.widgets.Display; |
| 18 |
|
| 19 |
import org.eclipse.ui.PlatformUI; |
| 20 |
|
| 21 |
/** |
| 22 |
* Helper class to create and dispose a mono-color I-Beam cursor with 16x16 dimension. |
| 23 |
* |
| 24 |
* The color of the cursor can be changed by the method <code>setRGB(RGB)</code>. The cursor is |
| 25 |
* returned with the method <code>getCursor()</code>. |
| 26 |
*/ |
| 27 |
public class MonoColorIBeamCursor { |
| 28 |
|
| 29 |
private static byte[] ibeamShape= { (byte)0, (byte)0, (byte)0, (byte)0, (byte)0, |
| 30 |
(byte)0, (byte)0, (byte)0, (byte)0, (byte)0, (byte)0, |
| 31 |
(byte)0, (byte)0, (byte)0, (byte)0, (byte)0, (byte)0, |
| 32 |
(byte)0, (byte)0, (byte)0, (byte)0, (byte)1, (byte)1, |
| 33 |
(byte)0, (byte)0, (byte)0, (byte)1, (byte)1, (byte)0, |
| 34 |
(byte)0, (byte)0, (byte)0, (byte)0, (byte)0, (byte)0, |
| 35 |
(byte)0, (byte)0, (byte)0, (byte)0, (byte)1, (byte)0, |
| 36 |
(byte)1, (byte)0, (byte)0, (byte)0, (byte)0, (byte)0, |
| 37 |
(byte)0, (byte)0, (byte)0, (byte)0, (byte)0, (byte)0, |
| 38 |
(byte)0, (byte)0, (byte)0, (byte)1, (byte)0, (byte)0, |
| 39 |
(byte)0, (byte)0, (byte)0, (byte)0, (byte)0, (byte)0, |
| 40 |
(byte)0, (byte)0, (byte)0, (byte)0, (byte)0, (byte)0, |
| 41 |
(byte)0, (byte)1, (byte)0, (byte)0, (byte)0, (byte)0, |
| 42 |
(byte)0, (byte)0, (byte)0, (byte)0, (byte)0, (byte)0, |
| 43 |
(byte)0, (byte)0, (byte)0, (byte)0, (byte)0, (byte)1, |
| 44 |
(byte)0, (byte)0, (byte)0, (byte)0, (byte)0, (byte)0, |
| 45 |
(byte)0, (byte)0, (byte)0, (byte)0, (byte)0, (byte)0, |
| 46 |
(byte)0, (byte)0, (byte)0, (byte)1, (byte)0, (byte)0, |
| 47 |
(byte)0, (byte)0, (byte)0, (byte)0, (byte)0, (byte)0, |
| 48 |
(byte)0, (byte)0, (byte)0, (byte)0, (byte)0, (byte)0, |
| 49 |
(byte)1, (byte)1, (byte)1, (byte)0, (byte)0, (byte)0, |
| 50 |
(byte)0, (byte)0, (byte)0, (byte)0, (byte)0, (byte)0, |
| 51 |
(byte)0, (byte)0, (byte)0, (byte)0, (byte)0, (byte)1, |
| 52 |
(byte)0, (byte)0, (byte)0, (byte)0, (byte)0, (byte)0, |
| 53 |
(byte)0, (byte)0, (byte)0, (byte)0, (byte)0, (byte)0, |
| 54 |
(byte)0, (byte)0, (byte)0, (byte)1, (byte)0, (byte)0, |
| 55 |
(byte)0, (byte)0, (byte)0, (byte)0, (byte)0, (byte)0, |
| 56 |
(byte)0, (byte)0, (byte)0, (byte)0, (byte)0, (byte)0, |
| 57 |
(byte)0, (byte)1, (byte)0, (byte)0, (byte)0, (byte)0, |
| 58 |
(byte)0, (byte)0, (byte)0, (byte)0, (byte)0, (byte)0, |
| 59 |
(byte)0, (byte)0, (byte)0, (byte)0, (byte)0, (byte)1, |
| 60 |
(byte)0, (byte)0, (byte)0, (byte)0, (byte)0, (byte)0, |
| 61 |
(byte)0, (byte)0, (byte)0, (byte)0, (byte)0, (byte)0, |
| 62 |
(byte)0, (byte)0, (byte)0, (byte)1, (byte)0, (byte)0, |
| 63 |
(byte)0, (byte)0, (byte)0, (byte)0, (byte)0, (byte)0, |
| 64 |
(byte)0, (byte)0, (byte)0, (byte)0, (byte)0, (byte)0, |
| 65 |
(byte)1, (byte)0, (byte)1, (byte)0, (byte)0, (byte)0, |
| 66 |
(byte)0, (byte)0, (byte)0, (byte)0, (byte)0, (byte)0, |
| 67 |
(byte)0, (byte)0, (byte)1, (byte)1, (byte)0, (byte)0, |
| 68 |
(byte)0, (byte)1, (byte)1, (byte)0, (byte)0, (byte)0, |
| 69 |
(byte)0, (byte)0, (byte)0, (byte)0, (byte)0, (byte)0, |
| 70 |
(byte)0, (byte)0, (byte)0, (byte)0, (byte)0, (byte)0, |
| 71 |
(byte)0, (byte)0, (byte)0, (byte)0, (byte)0 }; |
| 72 |
|
| 73 |
private Cursor cursor; |
| 74 |
|
| 75 |
/** |
| 76 |
* Constructs an instance of MonoColorIBeamCursor with given color. |
| 77 |
* |
| 78 |
* @param rgb cursor color |
| 79 |
*/ |
| 80 |
public MonoColorIBeamCursor(RGB rgb) { |
| 81 |
createCursor(rgb); |
| 82 |
} |
| 83 |
|
| 84 |
/** |
| 85 |
* Dispose the cursor if necessary |
| 86 |
*/ |
| 87 |
public void dispose() { |
| 88 |
if (cursor != null) { |
| 89 |
cursor.dispose(); |
| 90 |
cursor= null; |
| 91 |
} |
| 92 |
} |
| 93 |
|
| 94 |
/** |
| 95 |
* Return the colored I-Beam cursor or null. |
| 96 |
* |
| 97 |
* @return the cursor |
| 98 |
*/ |
| 99 |
public Cursor getCursor() { |
| 100 |
return cursor; |
| 101 |
} |
| 102 |
|
| 103 |
/** |
| 104 |
* Set the color of the I-Beam cursor |
| 105 |
* |
| 106 |
* @param rgb a new color |
| 107 |
*/ |
| 108 |
public void setRGB(RGB rgb) { |
| 109 |
dispose(); |
| 110 |
if (rgb != null) |
| 111 |
createCursor(rgb); |
| 112 |
} |
| 113 |
|
| 114 |
/** |
| 115 |
* Create a I-Beam cursor with given color |
| 116 |
* |
| 117 |
* @param rgb the cursor color |
| 118 |
*/ |
| 119 |
private void createCursor(RGB rgb) { |
| 120 |
Display display= PlatformUI.getWorkbench().getDisplay(); |
| 121 |
if (display != null && !display.isDisposed()) { |
| 122 |
RGB[] colors= { new RGB(0, 0, 0), new RGB(rgb.red, rgb.green, rgb.blue) }; |
| 123 |
PaletteData palette= new PaletteData(colors); |
| 124 |
ImageData cursorData= new ImageData(16, 16, 8, palette, 4, ibeamShape); |
| 125 |
cursorData.transparentPixel= 0; |
| 126 |
cursor= new Cursor(display, cursorData, 8, 8); |
| 127 |
} |
| 128 |
} |
| 129 |
} |