Skip to main content
aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorangelozerr2018-01-08 17:14:40 -0500
committerangelozerr2018-01-09 08:24:27 -0500
commite8186dc22f970dae14d07ccb5341906955989f29 (patch)
tree380f593d9480b7897632030cc243c6ffb252cc03 /org.eclipse.jface.text.examples
parentae1808bedf9b19c2eeb02600f58c48787041a1d3 (diff)
downloadeclipse.platform.text-e8186dc22f970dae14d07ccb5341906955989f29.tar.gz
eclipse.platform.text-e8186dc22f970dae14d07ccb5341906955989f29.tar.xz
eclipse.platform.text-e8186dc22f970dae14d07ccb5341906955989f29.zip
Bug 528418 - [CodeMining] Support for action CodeMiningI20180111-2000I20180111-0530I20180110-2000
Change-Id: I79cdc0ef39e6c29235913bcc077f2743232ec86c Signed-off-by: angelozerr <angelo.zerr@gmail.com>
Diffstat (limited to 'org.eclipse.jface.text.examples')
-rw-r--r--org.eclipse.jface.text.examples/src/org/eclipse/jface/text/examples/sources/inlined/ColorAnnotation.java50
1 files changed, 50 insertions, 0 deletions
diff --git a/org.eclipse.jface.text.examples/src/org/eclipse/jface/text/examples/sources/inlined/ColorAnnotation.java b/org.eclipse.jface.text.examples/src/org/eclipse/jface/text/examples/sources/inlined/ColorAnnotation.java
index 3a169613f..fbee0d35f 100644
--- a/org.eclipse.jface.text.examples/src/org/eclipse/jface/text/examples/sources/inlined/ColorAnnotation.java
+++ b/org.eclipse.jface.text.examples/src/org/eclipse/jface/text/examples/sources/inlined/ColorAnnotation.java
@@ -10,14 +10,24 @@
*/
package org.eclipse.jface.text.examples.sources.inlined;
+import java.util.function.Consumer;
+
+import org.eclipse.jface.text.BadLocationException;
+import org.eclipse.jface.text.IDocument;
+import org.eclipse.jface.text.IRegion;
import org.eclipse.jface.text.Position;
import org.eclipse.jface.text.source.ISourceViewer;
import org.eclipse.jface.text.source.inlined.LineContentAnnotation;
+import org.eclipse.jface.util.Geometry;
import org.eclipse.swt.custom.StyledText;
+import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.FontMetrics;
import org.eclipse.swt.graphics.GC;
+import org.eclipse.swt.graphics.RGB;
import org.eclipse.swt.graphics.Rectangle;
+import org.eclipse.swt.widgets.ColorDialog;
+import org.eclipse.swt.widgets.Shell;
/**
* Color annotation displays a colorized square before the rgb declaration.
@@ -25,6 +35,41 @@ import org.eclipse.swt.graphics.Rectangle;
public class ColorAnnotation extends LineContentAnnotation {
private Color color;
+
+ private Consumer<MouseEvent> action = e -> {
+ StyledText styledText = super.getTextWidget();
+ Shell shell = new Shell(styledText.getDisplay());
+ Rectangle location = Geometry.toDisplay(styledText, new Rectangle(e.x, e.y, 1, 1));
+ shell.setLocation(location.x, location.y);
+ // Open color dialog
+ ColorDialog dialog = new ColorDialog(shell);
+ // dialog.setRGB(annotation.getRGBA().rgb);
+ RGB color = dialog.open();
+ if (color != null) {
+ // Color was selected, update the viewer
+ try {
+ int offset = getPosition().getOffset();
+ IDocument document = getViewer().getDocument();
+ IRegion line = document.getLineInformation(document.getLineOfOffset(offset));
+ int length = line.getLength() - (offset - line.getOffset());
+ String rgb = formatToRGB(color);
+ document.replace(offset, length, rgb);
+ } catch (BadLocationException e1) {
+
+ }
+ }
+ };
+
+ /**
+ * Format the given rgb to hexa color.
+ *
+ * @param rgb
+ * @return the hexa color from the given rgb.
+ */
+ private static String formatToRGB(RGB rgb) {
+ return new StringBuilder("rgb(").append(rgb.red).append(",").append(rgb.green).append(",").append(rgb.blue)
+ .append(")").toString();
+ }
public ColorAnnotation(Position pos, ISourceViewer viewer) {
super(pos, viewer);
@@ -74,4 +119,9 @@ public class ColorAnnotation extends LineContentAnnotation {
int width = 2 * fontMetrics.getAverageCharWidth() + getSquareSize(fontMetrics);
return width;
}
+
+ @Override
+ public Consumer<MouseEvent> getAction(MouseEvent e) {
+ return action;
+ }
}

Back to the top