| package org.eclipse.nebula.widgets.nattable.renderer.swt.graphics |
| |
| import java.util.HashMap |
| import java.util.Map |
| import java.util.Stack |
| import org.eclipse.nebula.widgets.nattable.core.graphics.Color |
| import org.eclipse.nebula.widgets.nattable.core.graphics.GraphicsContext |
| import org.eclipse.swt.SWT |
| import org.eclipse.swt.graphics.GC |
| |
| /** |
| * Proxy implementation of {@link IGraphicsContext} to provide the drawing |
| * capabilities by {@link GC} so the NatTable can be painted using this. |
| * |
| * @author Dirk Fauth |
| * |
| */ |
| class SWTGraphicsContext implements GraphicsContext { |
| |
| /** |
| * The SWT {@link GC} which is proxied by this implementation. |
| */ |
| val GC gc |
| val stateStack = new Stack<Map<String, Object>> |
| |
| var Map<String, Object> originalState = newHashMap |
| |
| /** |
| * Create a new proxy instance for the specified {@link GC}. |
| * @param gc The SWT {@link GC} which is proxied by this implementation. |
| */ |
| new(GC gc) { |
| if (gc == null) throw new IllegalArgumentException("gc can not be null"); //$NON-NLS-1$ |
| this.gc = gc |
| } |
| |
| override pushState() { |
| stateStack.push(originalState) |
| originalState = new HashMap<String, Object> |
| } |
| |
| override popState() { |
| for (property : originalState.keySet) { |
| switch (property) { |
| case "foregroundColor": gc.foreground = originalState.get(property) as org.eclipse.swt.graphics.Color |
| } |
| } |
| originalState = stateStack.pop |
| } |
| |
| override setForegroundColor(Color foregroundColor) { |
| originalState.put("foregroundColor", gc.foreground) |
| gc.foreground = new org.eclipse.swt.graphics.Color( |
| gc.device, |
| foregroundColor.red, |
| foregroundColor.green, |
| foregroundColor.blue |
| ) |
| } |
| |
| override drawLine(int x1, int y1, int x2, int y2) { |
| gc.drawLine(x1, y1, x2, y2) |
| } |
| |
| override drawText(String text, int x, int y) { |
| gc.drawText(text, x, y, SWT::DRAW_TRANSPARENT.bitwiseOr(SWT::DRAW_DELIMITER)) |
| |
| //TODO draw underline and strikethrough in here instead of the TextPainter itself |
| } |
| |
| override calculateTextWidth(String text) { |
| gc.textExtent(text).x |
| } |
| |
| } |