com.gaborcselle.swt
Class Diagram

java.lang.Object
  extended by org.eclipse.swt.widgets.Widget
      extended by org.eclipse.swt.widgets.Control
          extended by org.eclipse.swt.widgets.Scrollable
              extended by org.eclipse.swt.widgets.Composite
                  extended by org.eclipse.swt.widgets.Canvas
                      extended by com.gaborcselle.swt.Diagram
All Implemented Interfaces:
org.eclipse.swt.graphics.Drawable

public class Diagram
extends org.eclipse.swt.widgets.Canvas

A simple diagramming widget for SWT. The diagram rescales automatically when its size is changed (e.g. by resizing the window containing it).

Displays two axes (x and y) with labels. In the diagram area, the user can draw lines, rectangles and text using drawX() methods (drawLine(), drawRectangle() and drawText()). Coordinates for range from (0.0, 0.0), which represents the lower left corner to (100.0, 100.0), which represents for the upper right corner. These coordinates are diagram coordinates: the actual physical coordinates change when diagram is rescaled to a different size. However, physical offsets can be given for all objects: The drawn objects are then shifted right and up by the given number of pixels.

This widget will not dispose any Color or Font objects passed to it. They must be disposed by the user.

An illustration of the diagram elements can be found at the authors' website.

Internals: All objects created with drawX() are stored in a list. When the PaintEvent handler is called, these object's positions and sizes are scaled according to the diagram area's current size and painted to the underlying Canvas using the internal paintX() functions.

Version:
1.0
Author:
Gabor Cselle and Erol Koç

Field Summary
static int VALIGN_CENTER
          Constant for drawText(): Indicates that text should be drawn so that given x / y coordinates are on left edge, in the middle of text.
static int VALIGN_NORMAL
          Constant for drawText(): Indicates that text should be drawn so that given x / y coordinates are on left edge, in the bottom of the text.
 
Fields inherited from class org.eclipse.swt.widgets.Control
handle
 
Constructor Summary
Diagram(org.eclipse.swt.widgets.Composite parent, int style)
          Constructs a new instance of this class given its parent and a style value describing its behavior and appearance.
 
Method Summary
 void clear()
          Clears all previously drawn objects in the diagram area.
 void drawLine(float x1, float y1, float x2, float y2, org.eclipse.swt.graphics.Color color, int width)
          Adds a line to the diagram.
 void drawLine(float x1, float y1, float x2, float y2, int xoffset, int yoffset, org.eclipse.swt.graphics.Color color, int width)
          Adds a line to the diagram and shifts it by the given offset.
 void drawRectangle(float x1, float y1, float x2, float y2, org.eclipse.swt.graphics.Color color)
          Adds a rectangle to the diagram.
 void drawRectangle(float x1, float y1, float x2, float y2, int xoffset, int yoffset, org.eclipse.swt.graphics.Color color)
          Adds a rectangle to the diagram and shifts it by the given offset.
 void drawText(java.lang.String text, float x, float y, org.eclipse.swt.graphics.Font font, int vAlign)
          Adds text to the diagram.
 void drawText(java.lang.String text, float x, float y, int xoffset, int yoffset, org.eclipse.swt.graphics.Font font, int vAlign)
          Adds text to the diagram and shifts it by the given offset.
 void setAxisDistance(int axisDistance)
          Set the distance between the diagram axes and the labels.
 void setAxisLabelFont(org.eclipse.swt.graphics.Font axisLabelFont)
          Set the font to be used to draw the axis labels.
 void setAxisLineWidth(int axisLineWidth)
          Set the line width for the axes.
 void setBottomMargin(int bottomMargin)
          The margin between the bottom edge of the diagram widget and the x axis labels.
 void setLeftMargin(int leftMargin)
          The margin between the left edge of the diagram widget and the y axis labels.
 void setRightMargin(int rightMargin)
          The margin between the right edge of the diagram widget and the right edge of the diagram area.
 void setTopMargin(int topMargin)
          The margin between the top edge of the diagram widget and the top edge of the diagram area.
 void setXAxisLabelMax(java.lang.String xAxisLabelMax)
          Set the label text next to the maximum marker on the x axis.
 void setXAxisLabelMin(java.lang.String xAxisLabelMin)
          Set the label text next to the minimum marker on the x axis.
 void setYAxisLabelMax(java.lang.String yAxisLabelMax)
          Set the label text next to the maximum marker on the y axis.
 void setYAxisLabelMin(java.lang.String yAxisLabelMin)
          Set the label text next to the minimum marker on the y axis.
 
Methods inherited from class org.eclipse.swt.widgets.Canvas
getCaret, scroll, setCaret, setFont
 
Methods inherited from class org.eclipse.swt.widgets.Composite
changed, computeSize, getChildren, getLayout, getLayoutDeferred, getTabList, isLayoutDeferred, layout, layout, layout, layout, setFocus, setLayout, setLayoutDeferred, setTabList
 
Methods inherited from class org.eclipse.swt.widgets.Scrollable
computeTrim, getClientArea, getHorizontalBar, getVerticalBar
 
Methods inherited from class org.eclipse.swt.widgets.Control
addControlListener, addFocusListener, addHelpListener, addKeyListener, addMouseListener, addMouseMoveListener, addMouseTrackListener, addPaintListener, addTraverseListener, computeSize, forceFocus, getAccessible, getBackground, getBorderWidth, getBounds, getEnabled, getFont, getForeground, getLayoutData, getLocation, getMenu, getMonitor, getParent, getShell, getSize, getToolTipText, getVisible, internal_dispose_GC, internal_new_GC, isEnabled, isFocusControl, isReparentable, isVisible, moveAbove, moveBelow, pack, pack, redraw, redraw, removeControlListener, removeFocusListener, removeHelpListener, removeKeyListener, removeMouseListener, removeMouseMoveListener, removeMouseTrackListener, removePaintListener, removeTraverseListener, setBackground, setBounds, setBounds, setCapture, setCursor, setEnabled, setForeground, setLayoutData, setLocation, setLocation, setMenu, setParent, setRedraw, setSize, setSize, setToolTipText, setVisible, toControl, toControl, toDisplay, toDisplay, traverse, update
 
Methods inherited from class org.eclipse.swt.widgets.Widget
addDisposeListener, addListener, dispose, getData, getData, getDisplay, getStyle, isDisposed, isListening, notifyListeners, removeDisposeListener, removeListener, setData, setData, toString
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

VALIGN_CENTER

public static final int VALIGN_CENTER
Constant for drawText(): Indicates that text should be drawn so that given x / y coordinates are on left edge, in the middle of text.

See Also:
Constant Field Values

VALIGN_NORMAL

public static final int VALIGN_NORMAL
Constant for drawText(): Indicates that text should be drawn so that given x / y coordinates are on left edge, in the bottom of the text.

See Also:
Constant Field Values
Constructor Detail

Diagram

public Diagram(org.eclipse.swt.widgets.Composite parent,
               int style)
Constructs a new instance of this class given its parent and a style value describing its behavior and appearance. See the org.eclipse.swt.widgets.Canvas class for the meaning of the parameters.

Method Detail

drawText

public void drawText(java.lang.String text,
                     float x,
                     float y,
                     org.eclipse.swt.graphics.Font font,
                     int vAlign)
Adds text to the diagram.

See Also:
drawText(String, float, float, int, int, Font, int)

drawText

public void drawText(java.lang.String text,
                     float x,
                     float y,
                     int xoffset,
                     int yoffset,
                     org.eclipse.swt.graphics.Font font,
                     int vAlign)
Adds text to the diagram and shifts it by the given offset. Will be painted by paintText() when this diagram is painted.

Parameters:
text - The text to print on the diagram.
x - x axis diagram coordinate of where text should be printed
y - y axis diagram coordinate of where text should be printed
xoffset - a physical x offset for where the text should be painted. Moves text to the right.
yoffset - a physical y offset for where the text should be painted. Moves text up.
font - The font with which the text should be drawn. Must be disposed by the user.
vAlign - vertical alignment of the text to be drawn. Choices are VALIGN_CENTER and VALIGN_NORMAL.

drawLine

public void drawLine(float x1,
                     float y1,
                     float x2,
                     float y2,
                     org.eclipse.swt.graphics.Color color,
                     int width)
Adds a line to the diagram.

See Also:
drawLine(float, float, float, float, int, int, Color, int)

drawLine

public void drawLine(float x1,
                     float y1,
                     float x2,
                     float y2,
                     int xoffset,
                     int yoffset,
                     org.eclipse.swt.graphics.Color color,
                     int width)
Adds a line to the diagram and shifts it by the given offset. Will be painted by paintLine() when this diagram is painted.

Parameters:
x1 - x axis diagram coordinate of first line point
y1 - y axis diagram coordinate of first line point
x2 - x axis diagram coordinate of second line point
y2 - y axis diagram coordinate of second line point
xoffset - a physical x offset for where the line should be painted. Moves line to the right.
yoffset - a physical y offset for where the rectangle should be painted. Moves line up.
color - the color with which the line should be drawn. Color needs to be disposed by the user.
width - the line width with which the line should be drawn.

drawRectangle

public void drawRectangle(float x1,
                          float y1,
                          float x2,
                          float y2,
                          org.eclipse.swt.graphics.Color color)
Adds a rectangle to the diagram.

See Also:
drawRectangle(float, float, float, float, int, int, Color)

drawRectangle

public void drawRectangle(float x1,
                          float y1,
                          float x2,
                          float y2,
                          int xoffset,
                          int yoffset,
                          org.eclipse.swt.graphics.Color color)
Adds a rectangle to the diagram and shifts it by the given offset. Will be painted by paintRectangle() when this diagram is painted.

Parameters:
x1 - x axis diagram coordinate of lower left corner
y1 - y axis diagram coordinate of lower left corner
x2 - x axis diagram coordinate of upper right corner
y2 - y axis diagram coordinate of upper right corner
xoffset - a physical x offset for where the rectangle should be painted. Moves rectangle to the right.
yoffset - a physical y offset for where the rectangle should be painted. Moves rectangle up.
color -

clear

public void clear()
Clears all previously drawn objects in the diagram area.


setAxisDistance

public void setAxisDistance(int axisDistance)
Set the distance between the diagram axes and the labels.

Parameters:
axisDistance - The axisDistance to set.

setAxisLabelFont

public void setAxisLabelFont(org.eclipse.swt.graphics.Font axisLabelFont)
Set the font to be used to draw the axis labels.

Parameters:
axisLabelFont - The axisLabelFont to set.

setBottomMargin

public void setBottomMargin(int bottomMargin)
The margin between the bottom edge of the diagram widget and the x axis labels.

Parameters:
bottomMargin - The bottomMargin to set.

setLeftMargin

public void setLeftMargin(int leftMargin)
The margin between the left edge of the diagram widget and the y axis labels.

Parameters:
leftMargin - The leftMargin to set.

setRightMargin

public void setRightMargin(int rightMargin)
The margin between the right edge of the diagram widget and the right edge of the diagram area.

Parameters:
rightMargin - The rightMargin to set.

setTopMargin

public void setTopMargin(int topMargin)
The margin between the top edge of the diagram widget and the top edge of the diagram area.

Parameters:
topMargin - The topMargin to set.

setXAxisLabelMax

public void setXAxisLabelMax(java.lang.String xAxisLabelMax)
Set the label text next to the maximum marker on the x axis.

Parameters:
xAxisLabelMax - The xAxisLabelMax to set.

setXAxisLabelMin

public void setXAxisLabelMin(java.lang.String xAxisLabelMin)
Set the label text next to the minimum marker on the x axis.

Parameters:
xAxisLabelMin - The xAxisLabelMin to set.

setYAxisLabelMax

public void setYAxisLabelMax(java.lang.String yAxisLabelMax)
Set the label text next to the maximum marker on the y axis.

Parameters:
yAxisLabelMax - The yAxisLabelMax to set.

setYAxisLabelMin

public void setYAxisLabelMin(java.lang.String yAxisLabelMin)
Set the label text next to the minimum marker on the y axis.

Parameters:
yAxisLabelMin - The yAxisLabelMin to set.

setAxisLineWidth

public void setAxisLineWidth(int axisLineWidth)
Set the line width for the axes.

Parameters:
axisLineWidth - The axisLineWidth to set.