Embedding an SWT chart within Eclipse workbench viewers

When embedding a SWT chart inside an existing Eclipse view part, the SWTGraphicViewer class takes in a parent component. You can provide a ViewPart extension and embed an SWT chart within the view.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
33
34
35
36
/**
* This sample class demonstrates how to plug-in a new
* workbench view. The view shows data obtained from the
* model.
*/
public class ChartView extends ViewPart {

/**
* The constructor.
*/
public ChartView() {
}

/**
* This is a callback that will allow us
* to create the viewer and initialize it.
*/
public void createPartControl(Composite parent) {
   try {
      FileInputStream inputStream = new FileInputStream(new File("chart.xml"));
      IChart chart = SerializerImpl.instance().read(inputStream);
      // create a new SWT Chart viewer
      IRenderer renderer = new SWTGraphicViewer(parent, SWT.DIALOG_TRIM|SWT.RESIZE );
      Generator.instance().run(renderer, chart);
   } catch (FileNotFoundException e) {
      e.printStackTrace();
   } catch (IOException e) {
      e.printStackTrace();
   } catch (RendererException e) {
      e.printStackTrace();
   }
}

   public void setFocus() {
   }
}

Line 20 - In this line an input stream from an XML file is created. The XML file is formatted based on the input schema and contains the raw data and configuration data for the chart.

Line 21 - The SerializerImpl class provides a read method that will allow one to take the XML stream and construct an internal representation of the chart object model. It should be noted that the serializer interface can also consume a XML document object model instead of an input stream. Therefore, a developer can construct the XML document object model in memory and pass in this DOM to the serializer.

Line 23 - Construct a SWT renderer. The SWT renderer takes a parent SWT widget. We also pass in some SWT component flags that will configure the swt shell for the viewer. We create a dialog shell that can be automaitcally resized.

Line 24 - In this line, the contents of the viewer are set.

Related reference
Programmer's Guide