Eclipse RCP:从表单值生成视图
我想建立一个类似于下面的草图的用户界面:
草图http://i39.tinypic.com/288uvz8.png
当用户填写右侧的表单并点击“Plot!” 按钮,一个新的可关闭的选项卡在左侧打开并显示一个图表。
我是RCP的新手,并且一直遵循本教程。 我可以调出带有菜单项触发图表的选项卡。 我如何去做:
编辑
这是我目前的代码。 它满足了这个问题中列出的基本要求,但我不确定这是否是最好的方法。 如果这里有人能指引我朝着正确的方向,我会很高兴。
与形式的观点; 该按钮的侦听器调用一个命令。
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() {
    }
}
由窗体中的按钮调用的命令。 这使用图表打开了一个新视图。
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;
    }
}
与图表的视图。 它查找窗体视图并从窗体(?!)的文本字段中读取值:
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 < 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;
    }
}
把它们联系在一起的观点:
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 + ":*");
    }
}
我会建议一个不同的方法。 Eclipse具有视图部分(视图)和编辑器。 打开多个编辑器很容易。 意见不是多开放多个。 所以我的建议是,您将您称为“FormView”的部分作为StandAloneView实现,并将“ChartView”实现为编辑器。
我还建议为按钮使用不同的侦听器,因此在使用键盘单击按钮时也会执行代码。
我的建议:
public class FormView extends ViewPart { 
...
button.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
    // this below can also be called by a command but you need to take care about the data, which the user put into the fields in  different way.
    Shell shell = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell();
    IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
    IWorkbenchPage page = window.getActivePage();
    ChartEditorInput input = new ChartEditorInput(text.getText(),...<other data you need to pass>);
    try {
     page.openEditor(input, ChartEditor.ID);
    } catch (PartInitException e) { 
       e.printStackTrace();
    }
}
});
  ChartView需要更改为ChartEditor。  看到这里http://www.vogella.de/articles/RichClientPlatform/article.html#editor_editorinput这是如何完成的。 
  ChartEditorInput是一个你需要实现的类,它包含数据的编辑器类。 
在你看来,你可以打电话给
public void createInitialLayout(IPageLayout layout) {
   String editorArea = layout.getEditorArea();
   layout.setFixed(false);
   layout.setEditorAreaVisible(true);
   layout.addStandaloneView("your.domain.and.FormView", true,IPageLayout.RIGHT, 0.15f, editorArea);
希望这可以帮助!
链接地址: http://www.djcxy.com/p/18677.html