从void main调用ViewPart(SWT)

时间:2013-08-08 05:13:30

标签: eclipse-plugin swt

我是eclipse插件和SWT的新手。我正在设计一个具有一些功能的视图部分。 但唯一的问题是我必须将它作为eclipse插件启动才能检查。 有没有办法可以从main(String [] args)方法中调用它?

我发布了我的示例视图代码

public class View extends ViewPart {
public static Display display=new Display();
public static final String ID = "Test.View"; //$NON-NLS-1$
private final FormToolkit toolkit = new FormToolkit(Display.getCurrent());

public View() {
}

/**
 * Create contents of the view part.
 * @param parent
 */
@Override
public void createPartControl(Composite parent) {
    Composite container = toolkit.createComposite(parent, SWT.NONE);
    toolkit.paintBordersFor(container);
    {
        Label lblNewLabel = new Label(container, SWT.NONE);
        lblNewLabel.setBounds(25, 46, 49, 13);
        toolkit.adapt(lblNewLabel, true, true);
        lblNewLabel.setText("New Label");
    }

    Spinner spinner = new Spinner(container, SWT.BORDER);
    spinner.setBounds(88, 105, 47, 21);
    toolkit.adapt(spinner);
    toolkit.paintBordersFor(spinner);

    DragSource dragSource = new DragSource(container, DND.DROP_MOVE);

    DropTarget dropTarget = new DropTarget(container, DND.DROP_MOVE);

    Canvas canvas = new Canvas(container, SWT.NONE);
    canvas.setBounds(94, 161, 174, 148);
    toolkit.adapt(canvas);
    toolkit.paintBordersFor(canvas);

    createActions();
    initializeToolBar();
    initializeMenu();
}

public void dispose() {
    toolkit.dispose();
    super.dispose();
}

/**
 * Create the actions.
 */
private void createActions() {
    // Create the actions
}

/**
 * Initialize the toolbar.
 */
private void initializeToolBar() {
    IToolBarManager tbm = getViewSite().getActionBars().getToolBarManager();
}

/**
 * Initialize the menu.
 */
private void initializeMenu() {
    IMenuManager manager = getViewSite().getActionBars().getMenuManager();
}

@Override
public void setFocus() {
    // Set the focus
} 

我可以写一些像

这样的东西吗?
public static void main(String[] args){
    View v=new View();

    Shell shell=new Shell(display);
    v.createPartControl(shell);
}

1 个答案:

答案 0 :(得分:0)

  1. 实际上,Eclipse视图是RCP的一部分,依赖于其基础结构。您可以创建一个仅显示视图的小型RCP应用程序。

  2. 在您的视图中使用public static Display display=new Display();是完全错误的!当代码运行时肯定有Display,可以通过Display.getDefault()(在任何线程上)或Display.getCurrent()(在GUI线程上)访问。

相关问题