00001
00002
00003
00004
00005
00006
00007 package org.swtchart.internal;
00008
00009 import java.util.ArrayList;
00010 import java.util.List;
00011
00012 import org.eclipse.swt.SWT;
00013 import org.eclipse.swt.events.PaintEvent;
00014 import org.eclipse.swt.events.PaintListener;
00015 import org.eclipse.swt.graphics.Color;
00016 import org.eclipse.swt.graphics.GC;
00017 import org.eclipse.swt.graphics.Point;
00018 import org.eclipse.swt.widgets.Composite;
00019 import org.eclipse.swt.widgets.Display;
00020 import org.swtchart.Chart;
00021 import org.swtchart.IAxis;
00022 import org.swtchart.IBarSeries;
00023 import org.swtchart.ICustomPaintListener;
00024 import org.swtchart.ILineSeries;
00025 import org.swtchart.IPlotArea;
00026 import org.swtchart.ISeries;
00027 import org.swtchart.ISeriesSet;
00028 import org.swtchart.internal.series.Series;
00029 import org.swtchart.internal.series.SeriesSet;
00030
00034 public class PlotArea extends Composite implements PaintListener, IPlotArea {
00035
00037 protected Chart chart;
00038
00040 protected SeriesSet seriesSet;
00041
00043 List<ICustomPaintListener> paintListeners;
00044
00046 private static final int DEFAULT_BACKGROUND = SWT.COLOR_WHITE;
00047
00056 public PlotArea(Chart chart, int style) {
00057 super(chart, style | SWT.NO_BACKGROUND | SWT.DOUBLE_BUFFERED);
00058
00059 this.chart = chart;
00060
00061 seriesSet = new SeriesSet(chart);
00062 paintListeners = new ArrayList<ICustomPaintListener>();
00063
00064 setBackground(Display.getDefault().getSystemColor(DEFAULT_BACKGROUND));
00065 addPaintListener(this);
00066 }
00067
00073 public ISeriesSet getSeriesSet() {
00074 return seriesSet;
00075 }
00076
00077
00078
00079
00080 @Override
00081 public void setBounds(int x, int y, int width, int height) {
00082 super.setBounds(x, y, width, height);
00083 ((SeriesSet) getSeriesSet()).compressAllSeries();
00084 }
00085
00086
00087
00088
00089 @Override
00090 public void setBackground(Color color) {
00091 if (color == null) {
00092 super.setBackground(Display.getDefault().getSystemColor(
00093 DEFAULT_BACKGROUND));
00094 } else {
00095 super.setBackground(color);
00096 }
00097 }
00098
00099
00100
00101
00102 public void addCustomPaintListener(ICustomPaintListener listener) {
00103 paintListeners.add(listener);
00104 }
00105
00106
00107
00108
00109 public void removeCustomPaintListener(ICustomPaintListener listener) {
00110 paintListeners.remove(listener);
00111 }
00112
00113
00114
00115
00116 public void paintControl(PaintEvent e) {
00117 Point p = getSize();
00118 GC gc = e.gc;
00119
00120
00121 Color oldBackground = gc.getBackground();
00122 gc.setBackground(getBackground());
00123 gc.fillRectangle(0, 0, p.x, p.y);
00124
00125
00126 for (IAxis axis : chart.getAxisSet().getAxes()) {
00127 ((Grid) axis.getGrid()).draw(gc, p.x, p.y);
00128 }
00129
00130
00131 for (ICustomPaintListener listener : paintListeners) {
00132 if (listener.drawBehindSeries()) {
00133 listener.paintControl(e);
00134 }
00135 }
00136
00137
00138 for (ISeries series : chart.getSeriesSet().getSeries()) {
00139 if (series instanceof IBarSeries) {
00140 ((Series) series).draw(gc, p.x, p.y);
00141 }
00142 }
00143 for (ISeries series : chart.getSeriesSet().getSeries()) {
00144 if (series instanceof ILineSeries) {
00145 ((Series) series).draw(gc, p.x, p.y);
00146 }
00147 }
00148
00149
00150 for (ICustomPaintListener listener : paintListeners) {
00151 if (!listener.drawBehindSeries()) {
00152 listener.paintControl(e);
00153 }
00154 }
00155
00156 e.gc.setBackground(oldBackground);
00157 }
00158
00159
00160
00161
00162 @Override
00163 public void dispose() {
00164 super.dispose();
00165 seriesSet.dispose();
00166 }
00167 }