在eclipse插件项目中清除和重绘视图

时间:2015-09-30 12:33:41

标签: java view eclipse-plugin swt jface

我在Eclipse应用程序中使用this教程创建了一个自定义视图,该教程负责可视化图形(由不同点定义,存储在数组中)。

此视图还包含一个下拉菜单,用户可以在其中选择不同的图名。

这是我的代码:

private List<Action> actionList = new ArrayList<Action>();

HashMap<String, int[]> graphCoordinates = new HashMap<String, int[]>();

@Override
public void createPartControl(Composite parent) {
    declareMaps();
    createActions();
    createMenu();

    Canvas canvas = new Canvas(parent, SWT.NONE);
    canvas.addPaintListener(new PaintListener() {
        public void paintControl(PaintEvent e) {
            if (graphCoordinates.isEmpty()) {
                declareGraphCoordinates(e.width, e.height);
            }
        }
    });
}

private void createActions() {
    for (String column : graphNames) {
        Action action = new Action(column) {
            public void run() {
                int[] polylinePoints = graphCoordinates.get(getId());
                // TODO: Draw!
            }
        };
        action.setId(column);
        actionList.add(action);
    }
}

private void createMenu() {
    IMenuManager mgr = getViewSite().getActionBars().getMenuManager();
    for (int i = 0; i < graphNames.size(); i++) {
        mgr.add(actionList.get(i));
    }
}

declareMaps()方法非常广泛。它声明了必须绘制的包含图形坐标的地图。主要是将图表添加到HashMap graphCoordinates,其中键是图名称,值为带有折线点的int[]。但这不是问题所在。

createActions()中,我为每个图形定义Action个对象,并将它们存储在ArrayList中。 在createMenu()我将Actions添加到菜单中。

此实现允许用户在我的视图菜单中选择图形名称,并在单击其中一个之后,调用相应run()的{​​{1}}方法。在Action方法中,我想清除视图并绘制新图,具体取决于用户的决定:

run()

阅读用户决策也不是问题,但如何重置视图(如有必要,删除旧图)并绘制新图?绘图发生在e.gc.drawPolyline(graphCoordinates.get(userDecision)); 方法内。我是否必须再次调用它或者实现它的最佳方法是什么?

1 个答案:

答案 0 :(得分:1)

只需在画布控件上调用redraw()即可告诉它您需要重绘它。这将导致再次调用绘图侦听器。在PaintListener绘制所选择的任何内容。

相关问题