|
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.core.runtime.Assert; |
| 20 |
|
| 21 |
import org.eclipse.ui.PlatformUI; |
| 22 |
|
| 23 |
/** |
| 24 |
* Helper class to create and dispose a mono-color I-Beam cursor with 16x16 dimension. |
| 25 |
* |
| 26 |
* The color of the cursor can be changed by the method <code>setRGB(RGB)</code>. The cursor is |
| 27 |
* returned with the method <code>getCursor()</code>. |
| 28 |
*/ |
| 29 |
public class MonoColorIBeamCursor { |
| 30 |
|
| 31 |
private static byte[] ibeamShape= { (byte)0, (byte)0, (byte)0, (byte)0, (byte)0, |
| 32 |
(byte)0, (byte)0, (byte)0, (byte)0, (byte)0, (byte)0, |
| 33 |
(byte)0, (byte)0, (byte)0, (byte)0, (byte)0, (byte)0, |
| 34 |
(byte)0, (byte)0, (byte)0, (byte)0, (byte)1, (byte)1, |
| 35 |
(byte)0, (byte)0, (byte)0, (byte)1, (byte)1, (byte)0, |
| 36 |
(byte)0, (byte)0, (byte)0, (byte)0, (byte)0, (byte)0, |
| 37 |
(byte)0, (byte)0, (byte)0, (byte)0, (byte)1, (byte)0, |
| 38 |
(byte)1, (byte)0, (byte)0, (byte)0, (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)1, (byte)0, (byte)0, |
| 41 |
(byte)0, (byte)0, (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)1, (byte)0, (byte)0, (byte)0, (byte)0, |
| 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)1, |
| 46 |
(byte)0, (byte)0, (byte)0, (byte)0, (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)1, (byte)0, (byte)0, |
| 49 |
(byte)0, (byte)0, (byte)0, (byte)0, (byte)0, (byte)0, |
| 50 |
(byte)0, (byte)0, (byte)0, (byte)0, (byte)0, (byte)0, |
| 51 |
(byte)1, (byte)1, (byte)1, (byte)0, (byte)0, (byte)0, |
| 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)1, |
| 54 |
(byte)0, (byte)0, (byte)0, (byte)0, (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)1, (byte)0, (byte)0, |
| 57 |
(byte)0, (byte)0, (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)1, (byte)0, (byte)0, (byte)0, (byte)0, |
| 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)1, |
| 62 |
(byte)0, (byte)0, (byte)0, (byte)0, (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)1, (byte)0, (byte)0, |
| 65 |
(byte)0, (byte)0, (byte)0, (byte)0, (byte)0, (byte)0, |
| 66 |
(byte)0, (byte)0, (byte)0, (byte)0, (byte)0, (byte)0, |
| 67 |
(byte)1, (byte)0, (byte)1, (byte)0, (byte)0, (byte)0, |
| 68 |
(byte)0, (byte)0, (byte)0, (byte)0, (byte)0, (byte)0, |
| 69 |
(byte)0, (byte)0, (byte)1, (byte)1, (byte)0, (byte)0, |
| 70 |
(byte)0, (byte)1, (byte)1, (byte)0, (byte)0, (byte)0, |
| 71 |
(byte)0, (byte)0, (byte)0, (byte)0, (byte)0, (byte)0, |
| 72 |
(byte)0, (byte)0, (byte)0, (byte)0, (byte)0, (byte)0, |
| 73 |
(byte)0, (byte)0, (byte)0, (byte)0, (byte)0 }; |
| 74 |
|
| 75 |
/** |
| 76 |
* The customized cursor |
| 77 |
*/ |
| 78 |
private Cursor cursor; |
| 79 |
|
| 80 |
/** |
| 81 |
* The RGB value of the cursor |
| 82 |
*/ |
| 83 |
private RGB rgb; |
| 84 |
|
| 85 |
/** |
| 86 |
* Constructs an instance of MonoColorIBeamCursor with given color. |
| 87 |
* |
| 88 |
* @param rgb cursor color, rgb can not be null |
| 89 |
*/ |
| 90 |
public MonoColorIBeamCursor(RGB rgb) { |
| 91 |
Assert.isTrue(rgb != null); |
| 92 |
this.rgb= rgb; |
| 93 |
createCursor(); |
| 94 |
} |
| 95 |
|
| 96 |
/** |
| 97 |
* Dispose the cursor if necessary |
| 98 |
*/ |
| 99 |
public void dispose() { |
| 100 |
if (cursor != null) { |
| 101 |
cursor.dispose(); |
| 102 |
cursor= null; |
| 103 |
} |
| 104 |
} |
| 105 |
|
| 106 |
/** |
| 107 |
* Return the colored I-Beam cursor or null. |
| 108 |
* |
| 109 |
* @return the cursor |
| 110 |
*/ |
| 111 |
public Cursor getCursor() { |
| 112 |
return cursor; |
| 113 |
} |
| 114 |
|
| 115 |
/** |
| 116 |
* Set the color of the I-Beam cursor |
| 117 |
* |
| 118 |
* @param rgb a new color, rgb can not be null |
| 119 |
*/ |
| 120 |
public void setRGB(RGB rgb) { |
| 121 |
Assert.isTrue(rgb != null); |
| 122 |
if (!this.rgb.equals(rgb)) { |
| 123 |
this.rgb= rgb; |
| 124 |
dispose(); |
| 125 |
createCursor(); |
| 126 |
} |
| 127 |
} |
| 128 |
|
| 129 |
/** |
| 130 |
* Create a I-Beam cursor with given color |
| 131 |
*/ |
| 132 |
private void createCursor() { |
| 133 |
Display display= PlatformUI.getWorkbench().getDisplay(); |
| 134 |
if (display != null && !display.isDisposed()) { |
| 135 |
RGB[] colors= { new RGB(0, 0, 0), new RGB(rgb.red, rgb.green, rgb.blue) }; |
| 136 |
PaletteData palette= new PaletteData(colors); |
| 137 |
ImageData cursorData= new ImageData(16, 16, 8, palette, 4, ibeamShape); |
| 138 |
cursorData.transparentPixel= 0; |
| 139 |
cursor= new Cursor(display, cursorData, 8, 8); |
| 140 |
} |
| 141 |
} |
| 142 |
} |