00001
00002
00003
00004
00005
00006
00007 package org.swtchart.internal;
00008
00009 import java.util.ArrayList;
00010
00011 import org.eclipse.swt.SWT;
00012 import org.eclipse.swt.graphics.Color;
00013 import org.eclipse.swt.graphics.GC;
00014 import org.eclipse.swt.widgets.Display;
00015 import org.swtchart.IGrid;
00016 import org.swtchart.LineStyle;
00017 import org.swtchart.internal.axis.Axis;
00018
00022 public class Grid implements IGrid {
00023
00025 private Axis axis;
00026
00028 private Color color;
00029
00031 private boolean isVisible;
00032
00034 private LineStyle lineStyle;
00035
00037 private final static int LINE_WIDTH = 1;
00038
00040 private final static LineStyle DEFAULT_STYLE = LineStyle.DOT;
00041
00043 private final static int DEFAULT_FOREGROUND = SWT.COLOR_GRAY;
00044
00051 public Grid(Axis axis) {
00052 this.axis = axis;
00053
00054 color = Display.getDefault().getSystemColor(DEFAULT_FOREGROUND);
00055 lineStyle = DEFAULT_STYLE;
00056 isVisible = true;
00057 }
00058
00059
00060
00061
00062 public Color getForeground() {
00063 if (color.isDisposed()) {
00064 color = Display.getDefault().getSystemColor(DEFAULT_FOREGROUND);
00065 }
00066 return color;
00067 }
00068
00069
00070
00071
00072 public void setForeground(Color color) {
00073 if (color != null && color.isDisposed()) {
00074 SWT.error(SWT.ERROR_INVALID_ARGUMENT);
00075 }
00076
00077 if (color == null) {
00078 this.color = Display.getDefault()
00079 .getSystemColor(DEFAULT_FOREGROUND);
00080 } else {
00081 this.color = color;
00082 }
00083 }
00084
00085
00086
00087
00088 public LineStyle getStyle() {
00089 return lineStyle;
00090 }
00091
00092
00093
00094
00095 public void setStyle(LineStyle style) {
00096 if (style == null) {
00097 this.lineStyle = DEFAULT_STYLE;
00098 } else {
00099 this.lineStyle = style;
00100 }
00101 }
00102
00113 protected void draw(GC gc, int width, int height) {
00114 if (!isVisible || lineStyle.equals(LineStyle.NONE)) {
00115 return;
00116 }
00117
00118 int xWidth;
00119 if (axis.isHorizontalAxis()) {
00120 xWidth = width;
00121 } else {
00122 xWidth = height;
00123 }
00124
00125 Color oldForeground = gc.getForeground();
00126 gc.setForeground(getForeground());
00127 ArrayList<Integer> tickLabelPosition = axis.getTick()
00128 .getAxisTickLabels().getTickLabelPositions();
00129
00130 gc.setLineStyle(Util.getIndexDefinedInSWT(lineStyle));
00131 if (axis.isValidCategoryAxis()) {
00132 int step = 0;
00133 if (tickLabelPosition.size() > 1) {
00134 step = tickLabelPosition.get(1).intValue()
00135 - tickLabelPosition.get(0).intValue();
00136 } else {
00137 step = xWidth;
00138 }
00139 int x = (int) (tickLabelPosition.get(0).intValue() - step / 2d);
00140
00141 for (int i = 0; i < tickLabelPosition.size() + 1; i++) {
00142 x += step;
00143 if (x >= xWidth) {
00144 continue;
00145 }
00146
00147 if (axis.isHorizontalAxis()) {
00148 gc.drawLine(x, LINE_WIDTH, x, height - LINE_WIDTH);
00149 } else {
00150 gc.drawLine(LINE_WIDTH, x, width - LINE_WIDTH, x);
00151 }
00152 }
00153 } else {
00154 for (int i = 0; i < tickLabelPosition.size(); i++) {
00155 int x = tickLabelPosition.get(i).intValue();
00156 if (x >= xWidth) {
00157 continue;
00158 }
00159
00160 if (axis.isHorizontalAxis()) {
00161 gc.drawLine(x, LINE_WIDTH, x, height - LINE_WIDTH);
00162 } else {
00163 gc.drawLine(LINE_WIDTH, height - 1 - x, width - LINE_WIDTH,
00164 height - 1 - x);
00165 }
00166 }
00167 }
00168 gc.setForeground(oldForeground);
00169 }
00170 }