【问题标题】:Eclipse RCP: Generating views from form valuesEclipse RCP:从表单值生成视图
【发布时间】:2019-07-06 21:06:56
【问题描述】:

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

当用户填写右侧的表格并单击“绘图!”时按钮,一个新的可关闭标签会在左侧打开一个图表。

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

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

编辑

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

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

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 视图,还是所有选项卡和表单都在一个 Eclipse 视图中?如果它们在一个视图中,那么您当前使用什么类来创建左侧的选项卡?
  • 我希望所有选项卡都是单独的视图。通过使用 RCP,我希望摆脱编写代码来实现人们期望在一个体面的桌面应用程序中实现的功能——比如停靠选项卡、能够并排平铺它们等。

标签: java user-interface eclipse-rcp


【解决方案1】:

我会推荐一种不同的方法。 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);

希望这会有所帮助!

【讨论】:

  • 谢谢@Raven,我已经用目前的代码编辑了这个问题。
  • 不客气。我稍微改变了我的帖子,但情况正如我预期的那样。您应该将 ChartView 更改为编辑器。你不需要实现所有的保存,另存为的东西,但你会“免费”获得许多窗口处理的东西。
  • 如果您喜欢我的回答,请评价并查看。谢谢:-))
  • 当您可能需要多个相同类型的选项卡式视图时,我还建议您使用编辑器。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-07-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-04-08
  • 1970-01-01
相关资源
最近更新 更多