一個popupMenus Extensions,
objectContribution:objectClass*: org.eclipse.core.resources.IFile
在action的Class代碼中:
public void selectionChanged(IAction action, ISelection selection) {
StructuredSelection ss = (StructuredSelection) selection;
this.selectedFile==(IFile)ss.getFirstElement(); //此處拋出異常
}
上述代碼的異常非常奇怪:
根據(jù)的的跟蹤,ss.getFirstElement()返回值是File,該類實現(xiàn)了IFile接口,
而且我用 ss.getFirstElement().getClass().isAssignableFrom(IFile.class)返回是false;
真是奇怪!---有人知道為什么嗎?
另外在實踐eclipse plugin開發(fā)過程中也有幾個心得:(肯定能用,但未必最佳)
1、如果開發(fā)plugin,所有的依賴庫都要包含到 Plug-in Dependencies 中;而不能只是引入到工程中。
2、如何輸出到console:
MessageConsole mc=new MessageConsole("****",null);
IConsole[] cs=new IConsole[1];
cs[0]=mc;
ConsolePlugin.getDefault().getConsoleManager().addConsoles(cs);
mc.activate();
PrintStream out=new PrintStream( mc.newOutputStream());
out.println("*******.");
3、如何獲取依賴工程的輸出路徑:
selectedProject:當(dāng)前工程---由用戶選擇
String[] ps= selectedProject.getRequiredProjectNames();
IWorkspace w= selectedProject.getProject().getWorkspace();
for(int i=0;i<ps.length;i++){
IResource r=w.getRoot().findMember(ps[i]);
try{
IJavaProject jp=new JavaProject((IProject)r,null);
File source=new File(jp.getProject().getLocation().append(jp.getOutputLocation().removeFirstSegments(1)).toOSString());
//作你的事情.....
}catch(Exception e){
//不是javaProject
e.printStackTrace();
}
4、如何使用進度Dialog:
Shell shell = new Shell();
ProgressMonitorDialog dialog = new ProgressMonitorDialog(shell);
IRunnableWithProgress thread = new SomeRunner(shell);
dialog.run(true, false, thread);
//=============================
private class SomeRunner implements IRunnableWithProgress {
public void run(IProgressMonitor monitor)throws InvocationTargetException, InterruptedException {
monitor.beginTask("一些信息", 數(shù)值-總工作量);
for(;;){
// 一些工作
monitor.worked(數(shù)值-已完成工作量); //實際中,我得情況不太相符,不明白,但差不多 :(
monitor.setTaskName("一些信息");
// 一些工作
}
monitor.done();
}
}