title: Eclipse RCP: Generating views from form values
body: <p>I want to build a user interface similar to the sketch below:</p>
  
  <p><a href="https://i.stack.imgur.com/0V23D.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/0V23D.png" alt="sketch"></a>
  </p>
  
  <p>When the user fills out the form on the right and clicks the 'Plot!' button, a new closeable tab opens on the left with a chart.</p>
  
  <p>I am new to RCP and have been following <a href="https://web.archive.org/web/20100616024256/http://www.vogella.de:80/articles/RichClientPlatform/article.html" rel="nofollow noreferrer">this tutorial</a>. I am able to bring up tabs with charts triggered from a menu item. How do I go about:</p>
  
  <ol>
  <li>creating the tab (view?) with the form</li>
  <li>open a new chart tab when the user clicks the button</li>
  </ol>
  
  <p><strong>Edit</strong></p>
  
  <p>Here is my current code. It satisfies the basic requirements outlined in this question, but  I am not sure if that is the best approach. I would be delighted if someone here can guide me in the right direction.</p>
  
  <p>A view with the form; the button's listener invokes a command.</p>
  
  <pre><code>public class FormView extends ViewPart {
      public static final String ID = 
          FormView.class.getPackage().getName() + ".Form";
  
      private FormToolkit toolkit;
      private Form form;
      public Text text;
  
      @Override
      public void createPartControl(Composite parent) {
          toolkit = new FormToolkit(parent.getDisplay());
          form = toolkit.createForm(parent);
          form.setText("Pie Chucker");
          GridLayout layout = new GridLayout();
          form.getBody().setLayout(layout);
          layout.numColumns = 2;
          GridData gd = new GridData();
          gd.horizontalSpan = 2;
          Label label = new Label(form.getBody(), SWT.NULL);
          label.setText("Chart Title:");
          text = new Text(form.getBody(), SWT.BORDER);
          text.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
          Button button = new Button(form.getBody(), SWT.PUSH);
          button.setText("Plot");
          gd = new GridData();
          gd.horizontalSpan = 2;
          button.setLayoutData(gd);
          button.addMouseListener(new MouseAdapter() {
              @Override
              public void mouseDown(MouseEvent e) {
                  IHandlerService handlerService = (IHandlerService) getSite()
                      .getService(IHandlerService.class);
                  try {
                      handlerService.executeCommand(ShowChartHandler.ID, null);
                  } catch (Exception ex) {
                      throw new RuntimeException(ShowChartHandler.ID + 
                          " not found");
                  }
              }
          });
  
      }
  
      @Override
      public void setFocus() {
      }
  }
  </code></pre>
  
  <p>The command invoked by the button from the form. This opens a new view with a chart.</p>
  
  <pre><code>public class ShowChartHandler extends AbstractHandler implements IHandler {
      public static final String ID = 
          ShowChartHandler.class.getPackage().getName() + ".ShowChart";
      private int count = 0;
  
      @Override
      public Object execute(ExecutionEvent event) throws ExecutionException {
          IWorkbenchWindow window = HandlerUtil.getActiveWorkbenchWindow(event);
          try {
              window.getActivePage().showView(ChartView.ID, 
                  String.valueOf(++count), IWorkbenchPage.VIEW_ACTIVATE);
          } catch (PartInitException e) {
              e.printStackTrace();
          }
          return null;
      }
  }
  </code></pre>
  
  <p>The view with the chart. It looks up the form view and reads a value from a text field in the form (?!):</p>
  
  <pre><code>public class ChartView extends ViewPart {
      public static final String ID = 
          ChartView.class.getPackage().getName() + ".Chart";
  
      private static final Random random = new Random();
  
      public ChartView() {
          // TODO Auto-generated constructor stub
      }
  
      @Override
      public void createPartControl(Composite parent) {
          FormView form = 
              (FormView) Workbench.getInstance()
                                  .getActiveWorkbenchWindow()
                                  .getActivePage()
                                  .findView(FormView.ID);
          String title = form == null? null : form.text.getText();
          if (title == null || title.trim().length() == 0) {
              title = "Pie Chart";
          }
          setPartName(title);
          JFreeChart chart = createChart(createDataset(), title);
          new ChartComposite(parent, SWT.NONE, chart, true);
      }
  
      @Override
      public void setFocus() {
          // TODO Auto-generated method stub
      }
  
      /**
       * Creates the Dataset for the Pie chart
       */
      private PieDataset createDataset() {
          Double[] nums = getRandomNumbers();
          DefaultPieDataset dataset = new DefaultPieDataset();
          dataset.setValue("One", nums[0]);
          dataset.setValue("Two", nums[1]);
          dataset.setValue("Three", nums[2]);
          dataset.setValue("Four", nums[3]);
          dataset.setValue("Five", nums[4]);
          dataset.setValue("Six", nums[5]);
          return dataset;
      }
  
      private Double[] getRandomNumbers() {
          Double[] nums = new Double[6];
          int sum = 0;
          for (int i = 0; i &lt; 5; i++) {
              int r = random.nextInt(20);
              nums[i] = new Double(r);
              sum += r;
          }
          nums[5] = new Double(100 - sum);
          return nums;
      }
  
      /**
       * Creates the Chart based on a dataset
       */
      private JFreeChart createChart(PieDataset dataset, String title) {
  
          JFreeChart chart = ChartFactory.createPieChart(title, // chart title
                  dataset, // data
                  true, // include legend
                  true, false);
  
          PiePlot plot = (PiePlot) chart.getPlot();
          plot.setSectionOutlinesVisible(false);
          plot.setLabelFont(new Font("SansSerif", Font.PLAIN, 12));
          plot.setNoDataMessage("No data available");
          plot.setCircular(false);
          plot.setLabelGap(0.02);
          return chart;
  
      }
  
  }
  </code></pre>
  
  <p>The perspective that ties it all together:</p>
  
  <pre><code>public class Perspective implements IPerspectiveFactory {
  
      public void createInitialLayout(IPageLayout layout) {
          layout.setEditorAreaVisible(false);
          layout.addStandaloneView(FormView.ID, false, 
                  IPageLayout.RIGHT, 0.3f, 
                  IPageLayout.ID_EDITOR_AREA);
          IFolderLayout charts = layout.createFolder("Charts", 
                  IPageLayout.LEFT, 0.7f, FormView.ID);
          charts.addPlaceholder(ChartView.ID + ":*");
      }
  }
  </code></pre>