2014-11-11 4 views
1

Eclipse 플러그인을 개발하려고합니다. 나는이 물건의 기초에 대해 알고있다. 샘플 플러그인 템플릿Eclipse 플러그인 개발 : Eclipse 편집기에서 이벤트 수신 방법

우리는 메뉴 항목 (또는 버튼 식 아이콘 아래 화상이 경우) 식의 시험 예에서, 방법 sampleHandler.java으로 실행되는 실행 및 팝업을 클릭하면 아래 그림과 같은 화면이 나타납니다. 내가 어떤 키를 누를 때마다 메소드를 호출 '실행'할

enter image description here

코드 편집기 대신의 메뉴 항목 (또는 버튼)을 클릭에 (백 스페이스를 말할 수 있습니다).

SampleHandler.java

public class SampleHandler extends AbstractHandler { 

    public SampleHandler() { 
    } 

    /** 
    * the command has been executed, so extract extract the needed information 
    * from the application context. 
    */ 
    public Object execute(ExecutionEvent event) throws ExecutionException { 
     IWorkbenchWindow window = HandlerUtil.getActiveWorkbenchWindowChecked(event); 
     MessageDialog.openInformation(
       window.getShell(), 
       "Sdfsdfsadf", 
       "Hello, Eclipse world"); 
     return null; 
    } 
    } 

나는 other posts에 주어진 제안을 시도했지만 내가 원하는 기능을 수행 할 수 없습니다입니다.

위 라인에 언급 된 포스트에서 내 이해 당로서, 나는이 코드를 아래에 시도 -

java.lang.NullPointerException 
at eventlisten.handlers.SampleHandler.execute(SampleHandler.java:45) 
at org.eclipse.ui.internal.handlers.HandlerProxy.execute(HandlerProxy.java:290) 
at org.eclipse.core.commands.Command.executeWithChecks(Command.java:499) 
at org.eclipse.core.commands.ParameterizedCommand.executeWithChecks(ParameterizedCommand.java:508) 
at org.eclipse.ui.internal.handlers.HandlerService.executeCommand(HandlerService.java:169) 
at org.eclipse.ui.internal.handlers.SlaveHandlerService.executeCommand(SlaveHandlerService.java:241) 
at org.eclipse.ui.menus.CommandContributionItem.handleWidgetSelection(CommandContributionItem.java:829) 
at org.eclipse.ui.menus.CommandContributionItem.access$19(CommandContributionItem.java:815) 
at org.eclipse.ui.menus.CommandContributionItem$5.handleEvent(CommandContributionItem.java:805) 
at org.eclipse.swt.widgets.EventTable.sendEvent(EventTable.java:84) 
at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:1276) 
at org.eclipse.swt.widgets.Display.runDeferredEvents(Display.java:3562) 
at org.eclipse.swt.widgets.Display.readAndDispatch(Display.java:3186) 
at org.eclipse.ui.internal.Workbench.runEventLoop(Workbench.java:2701) 
at org.eclipse.ui.internal.Workbench.runUI(Workbench.java:2665) 
at org.eclipse.ui.internal.Workbench.access$4(Workbench.java:2499) 
at org.eclipse.ui.internal.Workbench$7.run(Workbench.java:679) 
at org.eclipse.core.databinding.observable.Realm.runWithDefault(Realm.java:332) 
at org.eclipse.ui.internal.Workbench.createAndRunWorkbench(Workbench.java:668) 
at org.eclipse.ui.PlatformUI.createAndRunWorkbench(PlatformUI.java:149) 
at org.eclipse.ui.internal.ide.application.IDEApplication.start(IDEApplication.java:124) 
at org.eclipse.equinox.internal.app.EclipseAppHandle.run(EclipseAppHandle.java:196) 
at org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.runApplication(EclipseAppLauncher.java:110) 
at org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.start(EclipseAppLauncher.java:79) 
at org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:353) 
at org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:180) 
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) 
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
at java.lang.reflect.Method.invoke(Method.java:606) 
at org.eclipse.equinox.launcher.Main.invokeFramework(Main.java:629) 
at org.eclipse.equinox.launcher.Main.basicRun(Main.java:584) 
at org.eclipse.equinox.launcher.Main.run(Main.java:1438) 
at org.eclipse.equinox.launcher.Main.main(Main.java:1414) 

수있는 사람 가이드 -

package eventlisten.handlers; 

import org.eclipse.core.commands.AbstractHandler; 
import org.eclipse.core.commands.ExecutionEvent; 
import org.eclipse.core.commands.ExecutionException; 
import org.eclipse.ui.IEditorInput; 
import org.eclipse.ui.IEditorPart; 
import org.eclipse.ui.IWorkbenchPage; 
import org.eclipse.ui.IWorkbenchWindow; 
import org.eclipse.ui.PlatformUI; 


import org.eclipse.ui.handlers.HandlerUtil; 
import org.eclipse.ui.texteditor.ITextEditor; 
import org.eclipse.jface.dialogs.MessageDialog; 
import org.eclipse.jface.text.DocumentEvent; 
import org.eclipse.jface.text.IDocument; 
import org.eclipse.jface.text.IDocumentListener; 

/** 
* Our sample handler extends AbstractHandler, an IHandler base class. 
* @see org.eclipse.core.commands.IHandler 
* @see org.eclipse.core.commands.AbstractHandler 
*/ 
public class SampleHandler extends AbstractHandler { 
    /** 
    * The constructor. 
    */ 
    public SampleHandler() { 
    } 

    /** 
    * the command has been executed, so extract extract the needed information 
    * from the application context. 
    */ 
    public Object execute(ExecutionEvent event) throws ExecutionException { 

     IWorkbenchWindow window = HandlerUtil.getActiveWorkbenchWindowChecked(event); 
     MessageDialog.openInformation(window.getShell(),"EventListen","Trying event listen"); 
     IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage(); 
     IEditorPart editor = page.getActiveEditor(); 
     IEditorInput input = editor.getEditorInput(); 
     IDocument document=(((ITextEditor)editor).getDocumentProvider()).getDocument(IDocument.class); 

     document.addDocumentListener(new IDocumentListener() //**this is line 45** 
     { 
      @Override 
      public void documentAboutToBeChanged(DocumentEvent event) { 
       // TODO Auto-generated method stub 
       System.out.println("Hello"); 
      } 

      @Override 
      public void documentChanged(DocumentEvent event) { 
       // TODO Auto-generated method stub 
       System.out.println("Hello second"); 

      } 
     }); 

     return null; 
    } 
} 

을하지만, 팝업을 보여주는 후에는 예외를 발생 그 과정을 통해 나? 추가 정보가 필요한 경우 알려 주시기 바랍니다.

+0

다른 질문과 답변을 참조하십시오. 그러나 구체적으로 시도한 결과와 달성 한 결과는 무엇입니까? –

+0

게시물을 업데이트했습니다. 그 코드의 코드를 어떻게 사용하는지조차 알지 못합니다. – User42

+0

사용자가 키를 누르거나 내용이 변경 될 때마다 (또는 파일 저장시 충분할 때마다) 항상 실행을 실행 하시겠습니까? 'document == null'입니까? 라인 39에서 중단 점을 설정하고, 디버그 모드에서 플러그인을 시작하고 43 행을 실행 한 다음 (툴바에서 F6 또는 단계를 건너 뛰기) '문서'위로 마우스를 가져 가서 테스트 할 수 있습니다. 아니면 어떤 인스턴스가'null '인지 구체적으로 말할 수 있습니까? – bugybunny

답변

관련 문제