java - Eclipse RCP : Generating views from form values

标签 java user-interface eclipse-rcp

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

sketch

当用户填写右侧的表单并单击“绘图!”时按钮,左侧会打开一个新的可关闭选项卡,其中包含图表。

我是 RCP 新手,一直在关注 this tutorial 。我能够打开带有从菜单项触发的图表的选项卡。我该怎么办:

  1. 使用表单创建选项卡( View ?)
  2. 当用户单击按钮时打开一个新的图表选项卡

编辑

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

带有表单的 View ;按钮的监听器调用命令。

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

表单中的按钮调用的命令。这将打开一个带有图表的新 View 。

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

带有图表的 View 。它查找表单 View 并从表单中的文本字段读取值(?!):

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 具有 View 部分( View )和编辑器。打开多个编辑器很容易。对于打开多个 View 来说, View 并不多。 所以我的建议是,您将称为“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);

希望这有帮助!

关于java - Eclipse RCP : Generating views from form values,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2519838/

相关文章:

java - 如何托管 android 应用程序。我正在使用 mysql 和 Tomcat 服务器

java - 在 JSF 的情况下请求的资源不可用

css - 如何更改 ionic3 中模态的位置?

javascript - jQuery 隐藏 div,显示包含新内容的 div

java - 如何自定义 Eclipse 关于对话框的关于文本?

java - 我可以让 OwnerDrawLabelProvider 在单元格外部进行绘制吗?

java - Java中对象指针的Hashmap或Map

Java - 当不需要并且不知道出了什么问题时,字段实例变量会覆盖预先存在的值

java - 构建具有安全性的 Java 桌面应用程序的最佳方式

java - 限制 Eclipse e4 中的堆栈部分宽度