1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
|
public class ChartRenderer implements PaintListener, ControlListener{
private Canvas canvas = null;
private Chart cm = null;
private boolean bIsPainting = false;
private Image buffer;
private static int X_OFFSET = 3;
private static int Y_OFFSET = 3;
private GeneratedChartState state;
public ChartRenderer() {
}
public ChartRenderer(Canvas canvas) {
this.canvas = canvas;
canvas.addPaintListener(this);
canvas.addControlListener(this);
}
public void renderModel(Chart cm) {
this.cm = cm;
if (canvas != null && !canvas.isDisposed()) {
updateBuffer();
canvas.redraw();
}
}
private void updateBuffer() {
if (bIsPainting) {
return;
}
Throwable paintError = null;
Rectangle re = canvas.getClientArea();
final Rectangle adjustedRe = new Rectangle(0, 0, re.width, re.height);
if (adjustedRe.width - 2 * X_OFFSET <= 0
|| adjustedRe.height - 2 * Y_OFFSET <= 0) {
if (buffer != null && !buffer.isDisposed()) {
buffer.dispose();
buffer = null;
}
return;
}
if (cm == null) {
buffer = null;
} else {
bIsPainting = true;
Image oldBuffer = null;
if (buffer == null) {
buffer = new Image(Display.getDefault(), adjustedRe);
} else {
Rectangle ore = buffer.getBounds();
oldBuffer = buffer;
if (!adjustedRe.equals(ore)) {
buffer = new Image(Display.getDefault(), adjustedRe);
}
}
GC gc = new GC(buffer);
// fill default background as white.
gc.setForeground(Display.getDefault().getSystemColor(
SWT.COLOR_WHITE));
gc.fillRectangle(buffer.getBounds());
final Bounds bo = BoundsImpl.create(X_OFFSET, Y_OFFSET,
adjustedRe.width - 2 * X_OFFSET, adjustedRe.height - 2
* Y_OFFSET);
IDeviceRenderer deviceRenderer = null;
try {
deviceRenderer = PluginSettings.instance().getDevice("dv.SWT");
deviceRenderer.setProperty(IDeviceRenderer.GRAPHICS_CONTEXT, gc);
bo.scale(72d / deviceRenderer.getDisplayServer().getDpiResolution());
// GENERATE AND RENDER THE CHART
final Generator gr = Generator.instance();
RunTimeContext rtc = new RunTimeContext();
state = gr.build(deviceRenderer
.getDisplayServer(), cm, bo, null, rtc, null);
gr.render(deviceRenderer, state);
} catch (Exception ex) {
paintError = ex;
} finally {
gc.dispose();
if (deviceRenderer != null) {
deviceRenderer.dispose();
}
}
if (paintError != null) {
buffer = oldBuffer;
}
if (oldBuffer != null && oldBuffer != buffer) {
oldBuffer.dispose();
}
bIsPainting = false;
}
}
public void paintControl(PaintEvent pe) {
GC gc = pe.gc;
if (buffer != null) {
gc.drawImage(buffer, 0, 0);
}
}
public void setCanvas(Canvas paintCanvas) {
this.canvas = paintCanvas;
}
public void controlMoved(ControlEvent e) {
}
public void controlResized(ControlEvent e) {
if (canvas != null && !canvas.isDisposed()) {
updateBuffer();
canvas.redraw();
}
}
} |
Partager