Eclipse RCP:从表单值生成视图

时间:2010-03-25 22:20:16

标签: java user-interface eclipse-rcp

我想构建一个类似于下面草图的用户界面:

sketch http://i39.tinypic.com/288uvz8.png

当用户填写右侧的表单并点击“Plot!”时按钮,左侧打开一个新的可关闭选项卡,带有图表。

我是RCP新手并且一直关注this tutorial。我能够显示从菜单项触发的图表标签。我该怎么做:

  1. 使用表单
  2. 创建标签(视图?)
  3. 在用户点击按钮
  4. 时打开新的图表标签

    修改

    这是我目前的代码。它满足了这个问题中概述的基本要求,但我不确定这是否是最好的方法。如果有人能指引我朝着正确的方向前进,我会很高兴。

    带有表格的视图;按钮的侦听器调用命令。

    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 + ":*");
        }
    }
    

1 个答案:

答案 0 :(得分:3)

我会推荐一种不同的方法。 Eclipse有viewparts(视图)和编辑器。打开多个编辑器很容易。对于开放的多个观点而言,观点并非如此。 所以我的建议是,你将名为“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);

希望这有帮助!