00001
00002
00003
00004
00005
00006
00007 package org.swtchart.internal.series;
00008
00009 import java.text.DecimalFormat;
00010
00011 import org.eclipse.swt.SWT;
00012 import org.eclipse.swt.graphics.Color;
00013 import org.eclipse.swt.graphics.Font;
00014 import org.eclipse.swt.graphics.GC;
00015 import org.eclipse.swt.graphics.Point;
00016 import org.eclipse.swt.widgets.Display;
00017 import org.swtchart.ISeriesLabel;
00018 import org.swtchart.internal.Util;
00019
00023 public class SeriesLabel implements ISeriesLabel {
00024
00026 private boolean isVisible;
00027
00029 protected Font font;
00030
00032 protected Color color;
00033
00035 private String format;
00036
00038 private String[] formats;
00039
00041 private static final int DEFAULT_COLOR = SWT.COLOR_BLACK;
00042
00044 private static final Font DEFAULT_FONT = Display.getDefault()
00045 .getSystemFont();
00046
00048 private static final String DEFAULT_FORMAT = "#.###########";
00049
00053 public SeriesLabel() {
00054 font = DEFAULT_FONT;
00055 color = Display.getDefault().getSystemColor(DEFAULT_COLOR);
00056 isVisible = false;
00057 format = DEFAULT_FORMAT;
00058 formats = new String[0];
00059 }
00060
00061
00062
00063
00064 public String getFormat() {
00065 return format;
00066 }
00067
00068
00069
00070
00071 public void setFormat(String format) {
00072 if (format == null) {
00073 this.format = DEFAULT_FORMAT;
00074 } else {
00075 this.format = format;
00076 }
00077 }
00078
00079
00080
00081
00082 public String[] getFormats() {
00083 String[] copiedFormats = new String[formats.length];
00084 System.arraycopy(formats, 0, copiedFormats, 0, formats.length);
00085
00086 return copiedFormats;
00087 }
00088
00089
00090
00091
00092 public void setFormats(String[] formats) {
00093 if (formats == null) {
00094 this.formats = new String[0];
00095 return;
00096 }
00097
00098 this.formats = new String[formats.length];
00099 System.arraycopy(formats, 0, this.formats, 0, formats.length);
00100 }
00101
00102
00103
00104
00105 public Color getForeground() {
00106 return color;
00107 }
00108
00109
00110
00111
00112 public void setForeground(Color color) {
00113 if (color != null && color.isDisposed()) {
00114 SWT.error(SWT.ERROR_INVALID_ARGUMENT);
00115 }
00116
00117 if (color == null) {
00118 this.color = Display.getDefault().getSystemColor(DEFAULT_COLOR);
00119 } else {
00120 this.color = color;
00121 }
00122 }
00123
00124
00125
00126
00127 public Font getFont() {
00128 if (font.isDisposed()) {
00129 font = DEFAULT_FONT;
00130 }
00131 return font;
00132 }
00133
00134
00135
00136
00137 public void setFont(Font font) {
00138 if (font != null && font.isDisposed()) {
00139 SWT.error(SWT.ERROR_INVALID_ARGUMENT);
00140 }
00141 if (font == null) {
00142 this.font = DEFAULT_FONT;
00143 } else {
00144 this.font = font;
00145 }
00146 }
00147
00148
00149
00150
00151 public boolean isVisible() {
00152 return isVisible;
00153 }
00154
00155
00156
00157
00158 public void setVisible(boolean visible) {
00159 this.isVisible = visible;
00160 }
00161
00178 protected void draw(GC gc, int h, int v, double ySeriesValue,
00179 int seriesIndex, int alignment) {
00180 if (!isVisible) {
00181 return;
00182 }
00183
00184 Color oldForeground = gc.getForeground();
00185 gc.setForeground(color);
00186 gc.setFont(getFont());
00187
00188
00189 String format1 = format;
00190 if (formats.length > seriesIndex) {
00191 format1 = formats[seriesIndex];
00192 }
00193 if (format1 == null || format1.equals("")) {
00194 return;
00195 }
00196
00197
00198 String text;
00199 if (isDecimalFormat(format1)) {
00200 text = new DecimalFormat(format1).format(ySeriesValue);
00201 } else {
00202 text = format1.replaceAll("'", "");
00203 }
00204
00205
00206 if (alignment == SWT.CENTER) {
00207 Point p = Util.getExtentInGC(font, text);
00208 gc.drawString(text, (int) (h - p.x / 2d), (int) (v - p.y / 2d),
00209 true);
00210 } else if (alignment == SWT.BOTTOM) {
00211 gc.drawString(text, h, v, true);
00212 }
00213
00214 gc.setForeground(oldForeground);
00215 }
00216
00224 private static boolean isDecimalFormat(String text) {
00225 StringBuilder nonEscapedPart = new StringBuilder();
00226 String[] elements = text.split("'");
00227 if (elements != null) {
00228 for (int i = 0; i < elements.length; i += 2) {
00229 nonEscapedPart.append(elements[i]);
00230 }
00231 }
00232
00233 if (nonEscapedPart.indexOf("#") == -1
00234 && nonEscapedPart.indexOf("0") == -1) {
00235 return false;
00236 }
00237 return true;
00238 }
00239 }