00001
00002
00003
00004
00005
00006
00007 package org.swtchart.internal;
00008
00009 import org.eclipse.swt.SWT;
00010 import org.eclipse.swt.custom.StyleRange;
00011 import org.eclipse.swt.events.PaintEvent;
00012 import org.eclipse.swt.events.PaintListener;
00013 import org.eclipse.swt.graphics.Color;
00014 import org.eclipse.swt.graphics.Font;
00015 import org.eclipse.swt.graphics.GC;
00016 import org.eclipse.swt.graphics.Image;
00017 import org.eclipse.swt.graphics.Point;
00018 import org.eclipse.swt.graphics.Rectangle;
00019 import org.eclipse.swt.graphics.TextLayout;
00020 import org.eclipse.swt.graphics.Transform;
00021 import org.eclipse.swt.widgets.Display;
00022 import org.swtchart.Chart;
00023 import org.swtchart.Constants;
00024 import org.swtchart.ITitle;
00025
00029 public class Title implements ITitle, PaintListener {
00030
00032 protected Chart chart;
00033
00035 protected String text;
00036
00038 private Color foreground;
00039
00041 private Font font;
00042
00044 private StyleRange[] styleRanges;
00045
00047 private final TextLayout textLayout;
00048
00050 protected boolean isVisible;
00051
00053 private final Font defaultFont;
00054
00056 private Rectangle bounds;
00057
00059 private ChartLayoutData layoutData;
00060
00062 private static final int DEFAULT_FONT_SIZE = Constants.LARGE_FONT_SIZE;
00063
00065 private static final int DEFAULT_FOREGROUND = SWT.COLOR_BLUE;
00066
00068 private static final String DEFAULT_TEXT = "";
00069
00076 public Title(Chart parent) {
00077
00078 this.chart = parent;
00079 text = DEFAULT_TEXT;
00080 isVisible = true;
00081
00082 defaultFont = new Font(Display.getDefault(), "Tahoma",
00083 DEFAULT_FONT_SIZE, SWT.BOLD);
00084 textLayout = new TextLayout(Display.getDefault());
00085 bounds = new Rectangle(0, 0, 0, 0);
00086 font = defaultFont;
00087 setForeground(Display.getDefault().getSystemColor(DEFAULT_FOREGROUND));
00088
00089 parent.addPaintListener(this);
00090 }
00091
00092
00093
00094
00095 public void setText(String text) {
00096 String title;
00097 if (text == null) {
00098 title = getDefaultText();
00099 } else {
00100 title = text;
00101 }
00102
00103 textLayout.setText(title);
00104 this.text = title;
00105
00106 chart.updateLayout();
00107 }
00108
00114 protected String getDefaultText() {
00115 return DEFAULT_TEXT;
00116 }
00117
00118
00119
00120
00121 public String getText() {
00122 return text;
00123 }
00124
00131 public void setFont(Font font) {
00132 if (font == null) {
00133 this.font = defaultFont;
00134 } else if (font.isDisposed()) {
00135 throw new IllegalArgumentException("disposed font is given");
00136 } else {
00137 this.font = font;
00138 }
00139 chart.updateLayout();
00140 }
00141
00147 public Font getFont() {
00148 if (font.isDisposed()) {
00149 font = defaultFont;
00150 }
00151 return font;
00152 }
00153
00160 public void setForeground(Color color) {
00161 if (color == null) {
00162 foreground = Display.getDefault()
00163 .getSystemColor(DEFAULT_FOREGROUND);
00164 } else if (color.isDisposed()) {
00165 throw new IllegalArgumentException("disposed color is given");
00166 } else {
00167 foreground = color;
00168 }
00169 }
00170
00176 public Color getForeground() {
00177 return foreground;
00178 }
00179
00180
00181
00182
00183 public void setStyleRanges(StyleRange[] ranges) {
00184 styleRanges = ranges;
00185 if (styleRanges != null) {
00186 for (StyleRange range : styleRanges) {
00187 if (range != null) {
00188 textLayout.setStyle(range, range.start, range.start
00189 + range.length);
00190 }
00191 }
00192 }
00193 chart.updateLayout();
00194 }
00195
00196
00197
00198
00199 public StyleRange[] getStyleRanges() {
00200 return styleRanges;
00201 }
00202
00203
00204
00205
00206 public void setVisible(boolean isVisible) {
00207 if (this.isVisible == isVisible) {
00208 return;
00209 }
00210
00211 this.isVisible = isVisible;
00212 chart.updateLayout();
00213 }
00214
00215
00216
00217
00218 public boolean isVisible() {
00219 return isVisible;
00220 }
00221
00227 protected boolean isHorizontal() {
00228 return true;
00229 }
00230
00234 public void updateLayoutData() {
00235 int height;
00236 int width;
00237 if (isVisible() && !text.trim().equals("")) {
00238 if (styleRanges == null) {
00239 Point p = Util.getExtentInGC(getFont(), text);
00240 width = p.x;
00241 height = p.y;
00242 } else {
00243 Rectangle r = textLayout.getBounds();
00244 width = r.width;
00245 height = r.height;
00246 }
00247 } else {
00248 width = 0;
00249 height = 0;
00250 }
00251
00252 if (isHorizontal()) {
00253 setLayoutData(new ChartLayoutData(width, height));
00254 } else {
00255 setLayoutData(new ChartLayoutData(height, width));
00256 }
00257 }
00258
00265 public void setLayoutData(ChartLayoutData layoutData) {
00266 this.layoutData = layoutData;
00267 }
00268
00274 public ChartLayoutData getLayoutData() {
00275 return layoutData;
00276 }
00277
00281 public void dispose() {
00282 if (!defaultFont.isDisposed()) {
00283 defaultFont.dispose();
00284 }
00285 if (!textLayout.isDisposed()) {
00286 textLayout.dispose();
00287 }
00288 chart.removePaintListener(this);
00289 }
00290
00291
00292
00293
00294 public void paintControl(PaintEvent e) {
00295 if (text == null || text.equals("") || !isVisible) {
00296 return;
00297 }
00298
00299 Font oldFont = e.gc.getFont();
00300 Color oldForeground = getForeground();
00301 e.gc.setFont(getFont());
00302 e.gc.setForeground(getForeground());
00303
00304 if (isHorizontal()) {
00305 drawHorizontalTitle(e.gc);
00306 } else {
00307 drawVerticalTitle(e.gc);
00308 }
00309
00310 e.gc.setFont(oldFont);
00311 e.gc.setForeground(oldForeground);
00312 }
00313
00326 public void setBounds(int x, int y, int width, int height) {
00327 bounds = new Rectangle(x, y, width, height);
00328 }
00329
00335 public Rectangle getBounds() {
00336 return bounds;
00337 }
00338
00345 private void drawHorizontalTitle(GC gc) {
00346 boolean useStyleRanges = styleRanges != null;
00347
00348 int x = getBounds().x;
00349 int y = getBounds().y;
00350
00351 if (useStyleRanges) {
00352 textLayout.draw(gc, x, y);
00353 } else {
00354 gc.drawText(text, x, y, true);
00355 }
00356 }
00357
00364 private void drawVerticalTitle(GC gc) {
00365 boolean useStyleRanges = styleRanges != null;
00366
00367 int textWidth = getBounds().height;
00368 int textHeight = getBounds().width;
00369
00370
00371 int margin = textHeight / 10;
00372 textWidth += margin;
00373
00374
00375
00376
00377
00378
00379 Image image = new Image(Display.getCurrent(), textWidth, textHeight);
00380 GC tmpGc = new GC(image);
00381
00382 if (useStyleRanges) {
00383 textLayout.draw(tmpGc, 0, 0);
00384 } else {
00385 tmpGc.setBackground(chart.getBackground());
00386 tmpGc.setForeground(getForeground());
00387 tmpGc.setFont(getFont());
00388 tmpGc.fillRectangle(image.getBounds());
00389 tmpGc.drawText(text, 0, 0);
00390 }
00391
00392
00393 Transform oldTransform = new Transform(gc.getDevice());
00394 gc.getTransform(oldTransform);
00395 Transform transform = new Transform(gc.getDevice());
00396 transform.translate(0, textWidth);
00397 transform.rotate(270);
00398 gc.setTransform(transform);
00399
00400
00401 int x = getBounds().x;
00402 int y = getBounds().y;
00403
00404 gc.drawImage(image, -y, x);
00405
00406 gc.setTransform(oldTransform);
00407
00408
00409 tmpGc.dispose();
00410 transform.dispose();
00411 image.dispose();
00412 }
00413 }