Skip to content

Decorators

greipadmin edited this page Jan 16, 2019 · 16 revisions

Intoduction

Decorators are graphical components to visualize data or images. Decorators are used by Greip widgets (e.g. Picture, Tile). You can also use all decorators for your own widgets.

IDecorator

IDecorator is the interface that is used by controls to paint decorators. All decorators must implement this interface. The base implementation is AbstractDecorator.

Methods

  • void doPaint(GC gc, int x, int y), paints the decorator to the specified GC.
  • Point getSize(), returns the size of the decorator

AbstractDecorator

AbstractDecorator is the base implementation of the IDecorator interface. All decorator implementations are subclasses of this abstract class.

Constructors

  • AbstractDecorator(Control parent)
    • parent the parent control to paint the decorator

Methods

  • void dispose(), is called when the parent control is disposed
  • Display getDisplay(), returns the display of the parent control
  • Control getParent(), returns the parent control
  • void redraw(), force decorator paintig by redrawing the parent control

ImageDecorator

Paint an image to the parent control. Supports all image formats supported by ImageLoaderplus animated GIFs.

Methods

  • void loadImage(InputStream stream), loads an image from input stream
  • void loadImage(String filename), loads an image from the file
  • void setImage(Image image), sets an image
  • void scaleTo(final Point scaleTo), scales the image to the defined size

Example

public static void main(final String[] args) {
  final Display display = new Display();
  final Shell shell = new Shell(display);

  // creates the decorator and sets the image
  final ImageDecorator decorator = new ImageDecorator(shell);
  decorator.setImage(display.getSystemImage(SWT.ICON_INFORMATION));

  // adds a listener to paint the decorator
  shell.addListener(SWT.Paint, e -> decorator.doPaint(e.gc, 100, 10));

  shell.setText("Greip - ImageDecorator Example");
  shell.setSize(300, 100);
  shell.open();

  while (!shell.isDisposed()) {
    if (!display.readAndDispatch()) display.sleep();
  }
  display.dispose();
}

PercentageDecorator

Displays an percentage value as animated graphic.

Methods

  • void setCircleBackground(Color color), defines the circle background color
  • void setCircleForeground(Color color), defines the circle foreground color
  • void setCircleType(CircleType type), defines the type of the circle
  • void setFont(final Font font), sets the text font
  • void setForeground(final Color color), sets the foreground (text) color
  • void setInnerDiameter(int innerDiameter), sets the inner diameter in pixels
  • void setMaxValue(int maxValue), sets the maximum number (equal to 100 percent)
  • void setNumberFormat(NumberFormat format), sets the format for number formatting
  • void setOuterDiameter(int outerDiameter), sets the outer diameter in pixels
  • void setShowAnimation(boolean animate), enables or disables the animation
  • void setTreshholdColors(Map<Integer, Color> treshholdMap), defines color treshholds
  • void setUnit(String text), sets the unit text
  • void setUnitAlignment(int alignment), sets the alignment of the unit
  • void setUnitFont(Font font), sets the font to paint the unit text
  • void setValue(int value), sets the displayed value

You can also use all related getters.

Example

public static void main(final String[] args) {
  final Display display = new Display();
  final Shell shell = new Shell(display);

  // creates the decorator and set value
  final PercentageDecorator decorator = new PercentageDecorator(shell);
  decorator.setMaxValue(200);
  decorator.setValue(73);

  // configure colors
  final Map<Double, Color> colors = new HashMap<>();
  colors.put(new Double(0), display.getSystemColor(SWT.COLOR_GREEN));
  colors.put(new Double(75), display.getSystemColor(SWT.COLOR_YELLOW));
  colors.put(new Double(150), display.getSystemColor(SWT.COLOR_RED));
  decorator.setTreshholdColors(colors);

  // adds a listener to paint the decorator
  shell.addListener(SWT.Paint, e -> decorator.doPaint(e.gc, 100, 10));

  shell.setText("Greip - PercentageDecorator Example");
  shell.setSize(300, 150);
  shell.open();

  while (!shell.isDisposed()) {
    if (!display.readAndDispatch()) display.sleep();
  }
  display.dispose();
}

NumberDecorator

Displays an absolute number value as animated graphic.

Methods

  • void setCircleColor(Color color), defines the circle color
  • void setFont(final Font font), sets the text font
  • void setForeground(final Color color), sets the foreground (text) color
  • void setInnerDiameter(int innerDiameter), sets the inner diameter in pixels
  • void setNumberFormat(NumberFormat format), sets the format for number formatting
  • void setOuterDiameter(int outerDiameter), sets the outer diameter in pixels
  • void setShowAnimation(boolean animate), enables or disables the animation
  • void setTreshholdColors(Map<Integer, Color> treshholdMap), defines color treshholds
  • void setUnit(String text), sets the unit text
  • void setUnitAlignment(int alignment), sets the alignment of the unit
  • void setUnitFont(Font font), sets the font to paint the unit text
  • void setValue(int value), sets the displayed value

You can also use all related getters.

Example

public static void main(final String[] args) {
  final Display display = new Display();
  final Shell shell = new Shell(display);

  // creates the decorator and set value
  final NumberDecorator decorator = new NumberDecorator(shell);
  decorator.setValue(73);
  decorator.setUnit("kg");
  decorator.setForeground(display.getSystemColor(SWT.COLOR_BLUE));
  decorator.setCircleColor(display.getSystemColor(SWT.COLOR_DARK_GRAY));

  // adds a listener to paint the decorator
  shell.addListener(SWT.Paint, e -> decorator.doPaint(e.gc, 100, 10));

  shell.setText("Greip - NumberDecorator Example");
  shell.setSize(300, 150);
  shell.open();

  while (!shell.isDisposed()) {
    if (!display.readAndDispatch()) display.sleep();
  }
  display.dispose();
}
Clone this wiki locally