Created
June 18, 2013 18:34
-
-
Save tanmatra/5808021 to your computer and use it in GitHub Desktop.
Workaround for https://bugs.eclipse.org/bugs/show_bug.cgi?id=253670
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.lang.reflect.Field; | |
import org.eclipse.jface.layout.GridDataFactory; | |
import org.eclipse.swt.SWT; | |
import org.eclipse.swt.events.KeyAdapter; | |
import org.eclipse.swt.events.KeyEvent; | |
import org.eclipse.swt.events.PaintEvent; | |
import org.eclipse.swt.events.PaintListener; | |
import org.eclipse.swt.events.SelectionAdapter; | |
import org.eclipse.swt.events.SelectionEvent; | |
import org.eclipse.swt.graphics.Color; | |
import org.eclipse.swt.graphics.GC; | |
import org.eclipse.swt.graphics.Transform; | |
import org.eclipse.swt.layout.FillLayout; | |
import org.eclipse.swt.layout.GridLayout; | |
import org.eclipse.swt.layout.RowLayout; | |
import org.eclipse.swt.widgets.Button; | |
import org.eclipse.swt.widgets.Canvas; | |
import org.eclipse.swt.widgets.Composite; | |
import org.eclipse.swt.widgets.Display; | |
import org.eclipse.swt.widgets.Group; | |
import org.eclipse.swt.widgets.Label; | |
import org.eclipse.swt.widgets.Shell; | |
/** | |
* Workaround for https://bugs.eclipse.org/bugs/show_bug.cgi?id=253670 | |
*/ | |
public class RotateTest | |
{ | |
// Windows: (1 << 14) | |
// Linux: (1 << 9) | |
private static final int GC_DATA_DRAW_OFFSET; | |
static { | |
int intValue; | |
try { | |
final Field field = GC.class.getDeclaredField("DRAW_OFFSET"); | |
field.setAccessible(true); | |
try { | |
final Object value = field.get(null); | |
if (value instanceof Integer) { | |
intValue = ((Integer) value).intValue(); | |
} else { | |
intValue = 0; | |
} | |
} catch (IllegalArgumentException | IllegalAccessException ex) { | |
intValue = 0; | |
} | |
} catch (NoSuchFieldException | SecurityException ex) { | |
intValue = 0; | |
} | |
GC_DATA_DRAW_OFFSET = intValue; | |
} | |
private static final int SHIFT_Y = 100; | |
private static final int SHIFT_X = 100; | |
private static final int BOX_HEIGHT = 50; | |
private static final int BOX_WIDTH = 150; | |
private static int rotation = 0; | |
private static int intScale = 100; | |
private static float scale = 1.0f; | |
private static Canvas badCanvas; | |
private static Canvas goodCanvas; | |
private static Label statusLabel; | |
private static Display display; | |
private static Color foreground; | |
// private static final LineAttributes lineAttributes = new LineAttributes(1, SWT.CAP_FLAT, SWT.JOIN_MITER); | |
// static { | |
// lineAttributes.style = SWT.LINE_DASH; | |
// } | |
private static class CanvasPaintListener implements PaintListener | |
{ | |
private final Color background; | |
private CanvasPaintListener(Color background) { | |
this.background = background; | |
} | |
@Override | |
public void paintControl(PaintEvent e) { | |
final GC gc = e.gc; | |
gc.setAntialias(SWT.ON); | |
// gc.setLineAttributes(lineAttributes); // XXX | |
gc.setForeground(foreground); | |
gc.setBackground(background); | |
final Transform transform = new Transform(Display.getCurrent()); | |
try { | |
transform.translate(SHIFT_X, SHIFT_Y); | |
transform.scale(scale, scale); | |
transform.rotate(rotation); | |
gc.setTransform(transform); | |
fixTransform(gc); | |
gc.fillRectangle(0, 0, BOX_WIDTH, BOX_HEIGHT); | |
gc.drawRectangle(0, 0, BOX_WIDTH, BOX_HEIGHT); | |
} finally { | |
transform.dispose(); | |
} | |
} | |
protected void fixTransform(GC gc) { | |
// do nothing for now | |
} | |
} | |
public static void main(String[] args) { | |
display = new Display(); | |
final Color badCanvasBackground = display.getSystemColor(SWT.COLOR_RED); | |
final Color goodCanvasBackground = display.getSystemColor(SWT.COLOR_GREEN); | |
foreground = display.getSystemColor(SWT.COLOR_BLACK); | |
final Shell shell = new Shell(display); | |
// shell.setSize(400, 400); | |
shell.setLayout(new GridLayout(2, true)); | |
shell.addKeyListener(new KeyAdapter() { | |
@Override | |
public void keyPressed(KeyEvent e) { | |
switch (e.keyCode) { | |
case SWT.ARROW_LEFT: | |
rotateLeft(); | |
break; | |
case SWT.ARROW_RIGHT: | |
rotateRight(); | |
break; | |
case SWT.ARROW_UP: | |
scaleUp(); | |
break; | |
case SWT.ARROW_DOWN: | |
scaleDown(); | |
break; | |
} | |
} | |
}); | |
final Composite topPanel = new Composite(shell, SWT.NONE); | |
topPanel.setLayout(new RowLayout(SWT.HORIZONTAL)); | |
GridDataFactory.swtDefaults().span(2, 1).applyTo(topPanel); | |
final Button leftButton = new Button(topPanel, SWT.ARROW | SWT.LEFT); | |
leftButton.addSelectionListener(new SelectionAdapter() { | |
@Override | |
public void widgetSelected(SelectionEvent e) { | |
rotateLeft(); | |
} | |
}); | |
final Button rightButton = new Button(topPanel, SWT.ARROW | SWT.RIGHT); | |
rightButton.addSelectionListener(new SelectionAdapter() { | |
@Override | |
public void widgetSelected(SelectionEvent e) { | |
rotateRight(); | |
} | |
}); | |
final Group badGroup = new Group(shell, SWT.NONE); | |
badGroup.setText("Bad"); | |
GridDataFactory.fillDefaults().grab(true, true).hint(400, 400).applyTo(badGroup); | |
badGroup.setLayout(new FillLayout()); | |
badCanvas = new Canvas(badGroup, SWT.DOUBLE_BUFFERED); | |
badCanvas.addPaintListener(new CanvasPaintListener(badCanvasBackground)); | |
final Group goodGroup = new Group(shell, SWT.NONE); | |
goodGroup.setText("Good"); | |
GridDataFactory.fillDefaults().grab(true, true).hint(400, 400).applyTo(goodGroup); | |
goodGroup.setLayout(new FillLayout()); | |
goodCanvas = new Canvas(goodGroup, SWT.DOUBLE_BUFFERED); | |
goodCanvas.addPaintListener(new CanvasPaintListener(goodCanvasBackground) { | |
@Override | |
protected void fixTransform(GC gc) { | |
super.fixTransform(gc); | |
if (GC_DATA_DRAW_OFFSET != 0) { | |
gc.getGCData().state |= GC_DATA_DRAW_OFFSET; | |
} | |
} | |
}); | |
statusLabel = new Label(shell, SWT.NONE); | |
GridDataFactory.swtDefaults().span(2, 1).applyTo(statusLabel); | |
updateStatus(); | |
shell.open(); | |
shell.pack(); | |
while (!shell.isDisposed()) { | |
if (!display.readAndDispatch()) { | |
display.sleep(); | |
} | |
} | |
display.dispose(); | |
} | |
protected static void updateStatus() { | |
statusLabel.setText(String.format("Rotation: %d°\tScale: %.2f", rotation, scale)); | |
} | |
private static void redrawAll() { | |
badCanvas.redraw(); | |
goodCanvas.redraw(); | |
updateStatus(); | |
} | |
private static void rotateLeft() { | |
rotation -= 5; | |
if (rotation < 0) { | |
rotation += 360; | |
} | |
if (rotation >= 360) { | |
rotation -= 360; | |
} | |
redrawAll(); | |
} | |
private static void rotateRight() { | |
rotation += 5; | |
if (rotation < 0) { | |
rotation += 360; | |
} | |
if (rotation >= 360) { | |
rotation -= 360; | |
} | |
redrawAll(); | |
} | |
private static void scaleUp() { | |
intScale += 5; | |
scale = intScale / 100.0f; | |
redrawAll(); | |
} | |
private static void scaleDown() { | |
intScale -= 5; | |
scale = intScale / 100.0f; | |
redrawAll(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment