gpt4 book ai didi

java - eclipse RCP : Generating views from form values

转载 作者:行者123 更新时间:2023-12-04 06:56:19 25 4
gpt4 key购买 nike

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

sketch

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

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

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

  • 编辑

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

    带有表单的 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/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com