繁体   English   中英

开始聆听Eclipse启动时的用户选择

[英]Start listening to user selections on Eclipse start-up

我正在开发一个Eclipse插件,用于在两个程序上执行等效检查。 一旦用户在项目浏览器中选择了两个程序,就启用“最初变灰”图标,并且用户可以单击它以进行等效性检查。

问题是,当我在Eclipse中安装插件时,它不会监听用户的选择。 以某种方式,当Eclipse启动时,我必须“激活”我的选择类。 为此,我使用一个实现IStartup的类。

这是我的选择类,如果需要的话;

import java.util.ArrayList;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.ISelectionListener;
import org.eclipse.ui.IWorkbenchPart;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.part.ViewPart;

public class SelectionView extends ViewPart{

static ArrayList<String> paths=new ArrayList<>(2);

private ISelectionListener listener = new ISelectionListener() {
    public void selectionChanged(IWorkbenchPart sourcepart, ISelection selection) {
        if (sourcepart != SelectionView.this) {
            showSelection(sourcepart, selection);
        }
    }
};

public void showSelection(IWorkbenchPart sourcepart, ISelection selection) {
    if (selection instanceof IStructuredSelection) {        
        IStructuredSelection ss = (IStructuredSelection) selection;
        Object[] ob=ss.toArray();
        String path=getPath().substring(0,getPath().lastIndexOf('/'));
        String file=null;
        for(int i=0;i<ob.length;i++){
            if((file=ob[i].toString()).endsWith(".c")){
                paths.add(0,path+"/"+file);
                if(paths.size()>=2){
                    Handler.return_val=true;
                    Handler.arg1=paths.get(0);
                    Handler.arg2=paths.get(1);
                }
            }
        }

    }

}
public String getPath(){
    IWorkbenchPart workbenchPart = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().getActivePart(); 
    IFile file = (IFile) workbenchPart.getSite().getPage().getActiveEditor().getEditorInput().getAdapter(IFile.class);
    String path=ResourcesPlugin.getWorkspace().getRoot().getLocation().toString()+file.getFullPath();
    return path;
    }   
public void createPartControl(Composite parent) {   
    getSite().getWorkbenchWindow().getSelectionService().addSelectionListener(listener);
    }

public void dispose() {
    getSite().getWorkbenchWindow().getSelectionService().removeSelectionListener(listener);
    super.dispose();
    }
@Override
public void setFocus() {}

}

有人可以帮助我在Eclipse启动时“激活”此类吗? 也就是说,在Eclipse启动后,要在IStartup接口的earlyStartup()方法中编写哪些代码以开始侦听用户的选择?

谢谢!

您拥有自己的激活器,可以将工作空间选择服务挂接到激活器启动方法上。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM