Eclipse RCP: Generating views from form values

I want to build a user interface similar to the sketch below:

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

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.

I am new to RCP and have been following this tutorial. I am able to bring up tabs with charts triggered from a menu item. How do I go about:

  • creating the tab (view?) with the form
  • open a new chart tab when the user clicks the button
  • Edit

    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.

    A view with the form; the button's listener invokes a command.

    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() {
        }
    }
    

    The command invoked by the button from the form. This opens a new view with a chart.

    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;
        }
    }
    

    The view with the chart. It looks up the form view and reads a value from a text field in the form (?!):

    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;
    
        }
    
    }
    

    The perspective that ties it all together:

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

    I would recommend a different aproach. Eclipse has viewparts (views) and editors. It is easy to open multiple editors. Views are not so much for open multiple ones. So my suggestion is, that you implement the part you call "FormView" as a StandAloneView and implement the "ChartView" as an editor.

    I would also recommend to use a different listener for the button, so also the code will be executed when using the keyboard to click the button.

    My proposal:

    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 needs to be changed to ChartEditor. See here http://www.vogella.de/articles/RichClientPlatform/article.html#editor_editorinput how that is done.
    ChartEditorInput hereby is a class you need to implement aside the editor class, which holds the data.

    In your perspective you call:

    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);
    

    Hope this helps!

    链接地址: http://www.djcxy.com/p/18678.html

    上一篇: 在Eclipse中显示但禁用视图

    下一篇: Eclipse RCP:从表单值生成视图