Package com.positive.charts.text

Source Code of com.positive.charts.text.TextBox

/* ========================================================================
* JCommon : a free general purpose class library for the Java(tm) platform
* ========================================================================
*
* (C) Copyright 2000-2008, by Object Refinery Limited and Contributors.
*
* Project Info:  http://www.jfree.org/jcommon/index.html
*
* This library is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 2.1 of the License, or
* (at your option) any later version.
*
* This library is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
* License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301,
* USA. 
*
* [Java is a trademark or registered trademark of Sun Microsystems, Inc.
* in the United States and other countries.]
*
* ------------
* TextBox.java
* ------------
* (C) Copyright 2004, 2008, by Object Refinery Limited and Contributors.
*
* Original Author:  David Gilbert (for Object Refinery Limited);
* Contributor(s):   -;
*
* $Id: TextBox.java,v 1.13 2008/02/13 22:03:47 mungady Exp $
*
* Changes
* -------
* 09-Mar-2004 : Version 1 (DG);
* 22-Mar-2004 : Added equals() method and implemented Serializable (DG);
* 09-Nov-2004 : Renamed getAdjustedHeight() --> calculateExtendedHeight() in
*               Spacer class (DG);
* 22-Feb-2005 : Replaced Spacer with RectangleInsets (DG);
* 14-Feb-2008 : Fixed alignment of text content with respect to insets (DG);
*
*/

package com.positive.charts.text;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;

import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.widgets.Display;

import com.positive.charts.block.RectangleInsets;
import com.positive.charts.data.util.ObjectUtilities;
import com.positive.charts.data.util.RectangleAnchor;
import com.positive.charts.plot.PiePlot;
import com.positive.charts.util.Size2D;
import com.positive.charts.util.Stroke;
import com.positive.charts.util.TextBlock;
import com.positive.charts.util.TextBlockAnchor;
import com.positive.colorchecker.StaticColorChecker;

/**
* A box containing a text block.
*
* @author David Gilbert
*/
public class TextBox implements Serializable {

  /** For serialization. */
  private static final long serialVersionUID = 3360220213180203706L;

  /** The outline paint. */
  private transient Color outlinePaint;

  /** The outline stroke. */
  private transient Stroke outlineStroke;

  /** The interior space. */
  private RectangleInsets interiorGap;

  /** The background paint. */
  private transient Color backgroundPaint;

  /** The shadow paint. */
  private transient Color shadowPaint;

  /** The shadow x-offset. */
  private double shadowXOffset = 2.0;

  /** The shadow y-offset. */
  private double shadowYOffset = 2.0;

  /** The text block. */
  private TextBlock textBlock;

  private static Color color;

  /**
   * Creates an empty text box.
   */
  public TextBox() {
    this((TextBlock) null);
  }

  /**
   * Creates a text box.
   *
   * @param text
   *            the text.
   */
  public TextBox(final String text) {
    this((TextBlock) null);
    if (text != null) {
      this.textBlock = new TextBlock();

      this.textBlock.addLine(text, PiePlot.DEFAULT_LABEL_FONT,
          PiePlot.DEFAULT_LABEL_PAINT);
    }
  }

  /**
   * Creates a new text box.
   *
   * @param block
   *            the text block.
   */
  public TextBox(final TextBlock block) {
    this.outlinePaint = PiePlot.DEFAULT_LABEL_PAINT;
    this.outlineStroke = new Stroke(1);
    this.interiorGap = new RectangleInsets(1.0, 3.0, 1.0, 3.0);
    if (color == null) {
      color = new Color(Display.getCurrent(), 255, 255, 192);
    }
    this.backgroundPaint = color;
    this.shadowPaint =  StaticColorChecker.dublicateColor(SWT.COLOR_GRAY);
    //Display.getCurrent().getSystemColor(SWT.COLOR_GRAY);
    this.shadowXOffset = 2.0;
    this.shadowYOffset = 2.0;
    this.textBlock = block;
  }

  /**
   * Draws the text box.
   *
   * @param g2
   *            the graphics device.
   * @param x
   *            the x-coordinate.
   * @param y
   *            the y-coordinate.
   * @param anchor
   *            the anchor point.
   */
  public void draw(final GC g2, final float x, final float y,
      final RectangleAnchor anchor) {
    final Size2D d1 = this.textBlock.calculateDimensions(g2);
    final double w = this.interiorGap.extendWidth((int) d1.width);
    final double h = this.interiorGap.extendHeight((int) d1.height);
    final Size2D d2 = new Size2D(w, h);
    final Rectangle bounds = RectangleAnchor.createRectangle(new Rectangle(
        0, 0, (int) d2.width, (int) d2.height), x, y, anchor);
    final double xx = bounds.x;
    final double yy = bounds.y;

    // if (this.shadowPaint != null) {
    // final Rectangle shadow = new Rectangle.Double(xx
    // + this.shadowXOffset, yy + this.shadowYOffset, bounds
    // .getWidth(), bounds.getHeight());
    // g2.setPaint(this.shadowPaint);
    // g2.fill(shadow);
    // }
    if (this.backgroundPaint != null) {
      g2.setBackground(this.backgroundPaint);
      g2.fillRectangle(bounds);
    }

    if ((this.outlinePaint != null) && (this.outlineStroke != null)) {
      g2.setForeground(this.outlinePaint);
      // g2.setPaint(this.outlinePaint);
      this.outlineStroke.set(g2);
      g2.drawRectangle(bounds);
    }

    this.textBlock.draw(g2, (float) (xx + this.interiorGap
        .calculateLeftInset((int) w)), (float) (yy + this.interiorGap
        .calculateTopInset((int) h)), TextBlockAnchor.TOP_LEFT);

  }

  /**
   * Tests this object for equality with an arbitrary object.
   *
   * @param obj
   *            the object to test against (<code>null</code> permitted).
   *
   * @return A boolean.
   */
  public boolean equals(final Object obj) {
    if (obj == this) {
      return true;
    }
    if (!(obj instanceof TextBox)) {
      return false;
    }
    final TextBox that = (TextBox) obj;
    // if (!ObjectUtilities.equal(this.outlinePaint, that.outlinePaint)) {
    // return false;
    // }
    // if (!ObjectUtilities.equal(this.outlineStroke, that.outlineStroke)) {
    // return false;
    // }
    // if (!ObjectUtilities.equal(this.interiorGap, that.interiorGap)) {
    // return false;
    // }
    // if (!ObjectUtilities.equal(this.backgroundPaint,
    // that.backgroundPaint)) {
    // return false;
    // }
    // if (!ObjectUtilities.equal(this.shadowPaint, that.shadowPaint)) {
    // return false;
    // }
    if (this.shadowXOffset != that.shadowXOffset) {
      return false;
    }
    if (this.shadowYOffset != that.shadowYOffset) {
      return false;
    }
    if (!ObjectUtilities.equal(this.textBlock, that.textBlock)) {
      return false;
    }

    return true;
  }

  /**
   * Returns the background paint.
   *
   * @return The background paint.
   */
  public Color getBackgroundPaint() {
    return this.backgroundPaint;
  }

  /**
   * Returns the height of the text box.
   *
   * @param g2
   *            the graphics device.
   *
   * @return The height (in Java2D units).
   */
  public double getHeight(final GC g2) {
    final Size2D d = this.textBlock.calculateDimensions(g2);
    return this.interiorGap.extendHeight((int) d.getHeight());
  }

  /**
   * Returns the interior gap.
   *
   * @return The interior gap.
   */
  public RectangleInsets getInteriorGap() {
    return this.interiorGap;
  }

  /**
   * Returns the outline paint.
   *
   * @return The outline paint.
   */
  public Color getOutlinePaint() {
    return this.outlinePaint;
  }

  /**
   * Returns the outline stroke.
   *
   * @return The outline stroke.
   */
  public Stroke getOutlineStroke() {
    return this.outlineStroke;
  }

  /**
   * Returns the shadow paint.
   *
   * @return The shadow paint.
   */
  public Color getShadowPaint() {
    return this.shadowPaint;
  }

  /**
   * Returns the x-offset for the shadow effect.
   *
   * @return The offset.
   */
  public double getShadowXOffset() {
    return this.shadowXOffset;
  }

  /**
   * Returns the y-offset for the shadow effect.
   *
   * @return The offset.
   */
  public double getShadowYOffset() {
    return this.shadowYOffset;
  }

  /**
   * Returns the text block.
   *
   * @return The text block.
   */
  public TextBlock getTextBlock() {
    return this.textBlock;
  }

  /**
   * Returns a hash code for this object.
   *
   * @return A hash code.
   */
  public int hashCode() {
    int result = 1;
    long temp;
    // result = (this.outlinePaint != null ? this.outlinePaint.hashCode() :
    // 0);
    // result = 29 * result + (this.outlineStroke != null
    // ? this.outlineStroke.hashCode() : 0);
    // result = 29 * result + (this.interiorGap != null
    // ? this.interiorGap.hashCode() : 0);
    // result = 29 * result + (this.backgroundPaint != null
    // ? this.backgroundPaint.hashCode() : 0);
    // result = 29 * result + (this.shadowPaint != null
    // ? this.shadowPaint.hashCode() : 0);
    // temp = this.shadowXOffset != +0.0d
    // ? Double.doubleToLongBits(this.shadowXOffset) : 0L;
    // result = 29 * result + (int) (temp ^ (temp >>> 32));
    temp = this.shadowYOffset != +0.0d ? Double
        .doubleToLongBits(this.shadowYOffset) : 0L;
    result = 29 * result + (int) (temp ^ (temp >>> 32));
    result = 29 * result
        + (this.textBlock != null ? this.textBlock.hashCode() : 0);
    return result;
  }

  /**
   * Provides serialization support.
   *
   * @param stream
   *            the input stream.
   *
   * @throws IOException
   *             if there is an I/O error.
   * @throws ClassNotFoundException
   *             if there is a classpath problem.
   */
  private void readObject(final ObjectInputStream stream) throws IOException,
      ClassNotFoundException {
    stream.defaultReadObject();
    // this.outlinePaint = SerialUtilities.readPaint(stream);
    // this.outlineStroke = SerialUtilities.readStroke(stream);
    // this.backgroundPaint = SerialUtilities.readPaint(stream);
    // this.shadowPaint = SerialUtilities.readPaint(stream);
  }

  /**
   * Sets the background paint.
   *
   * @param paint
   *            the paint.
   */
  public void setBackgroundPaint(final Color paint) {
    this.backgroundPaint = paint;
  }

  /**
   * Sets the interior gap.
   *
   * @param gap
   *            the gap.
   */
  public void setInteriorGap(final RectangleInsets gap) {
    this.interiorGap = gap;
  }

  /**
   * Sets the outline paint.
   *
   * @param paint
   *            the paint.
   */
  public void setOutlinePaint(final Color paint) {
    this.outlinePaint = paint;
  }

  /**
   * Sets the outline stroke.
   *
   * @param stroke
   *            the stroke.
   */
  public void setOutlineStroke(final Stroke stroke) {
    this.outlineStroke = stroke;
  }

  /**
   * Sets the shadow paint.
   *
   * @param paint
   *            the paint.
   */
  public void setShadowPaint(final Color paint) {
    this.shadowPaint = paint;
  }

  /**
   * Sets the x-offset for the shadow effect.
   *
   * @param offset
   *            the offset (in Java2D units).
   */
  public void setShadowXOffset(final double offset) {
    this.shadowXOffset = offset;
  }

  /**
   * Sets the y-offset for the shadow effect.
   *
   * @param offset
   *            the offset (in Java2D units).
   */
  public void setShadowYOffset(final double offset) {
    this.shadowYOffset = offset;
  }

  /**
   * Sets the text block.
   *
   * @param block
   *            the block.
   */
  public void setTextBlock(final TextBlock block) {
    this.textBlock = block;
  }

  /**
   * Provides serialization support.
   *
   * @param stream
   *            the output stream.
   *
   * @throws IOException
   *             if there is an I/O error.
   */
  private void writeObject(final ObjectOutputStream stream)
      throws IOException {
    stream.defaultWriteObject();
    // SerialUtilities.writePaint(this.outlinePaint, stream);
    // SerialUtilities.writeStroke(this.outlineStroke, stream);
    // SerialUtilities.writePaint(this.backgroundPaint, stream);
    // SerialUtilities.writePaint(this.shadowPaint, stream);
  }

}
TOP

Related Classes of com.positive.charts.text.TextBox

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.