2012-10-29 4 views
2

jsdt javaScriptNature가 필요한 프로젝트 속성을 제공하는 Eclipse 플러그인이 있습니다. 이제 플러그인의 프로젝트에 포함 된 javascript 라이브러리를 프로그래밍 방식으로 추가하고 싶습니다. 내가 할 수있는 방법이 있니?프로그래밍 방식으로 IncludePathEntry를 JavaScriptProject에 추가하십시오.

JsGlobalScopeContainer 및 JsGlobalScopeContainerInitializer에 대한 정보를 읽고 시도했지만 매우 혼란스러워 보입니다. 난 그냥 내 플러그인에서 몇 가지 .js 파일을 포함하는 라이브러리를 추가하고 싶습니다. 나는이 개념에 대해 머리를 맞을 수가 없다.

이 내가 지금까지 해낸 것입니다 :

IJavaScriptProject jsProj = JavaScriptCore.create(p); 
Path pa = new Path("/src/de/otris/eclipse/portalscripting/psLibrary/library.js"); 
IIncludePathEntry entry = JavaScriptCore.newProjectEntry(pa);    
IIncludePathEntry[] ipaths = jsProj.getRawIncludepath(); 
IIncludePathEntry[] newpaths = new IIncludePathEntry[ipaths.length +1]; 
System.arraycopy(ipaths, 0, newpaths, 0, ipaths.length); 
newpaths[ipaths.length] = entry; 
jsProj.setRawIncludepath(newpaths, null); 

답변

1

마지막으로 내 라이브러리에서 내 라이브러리를 직접 추가하는 방법을 찾았습니다. 유진의 대답은 잘못이 아니었지만, 설명이 부족했습니다. 어떻게하는지 보여 주려고 노력할 것입니다.

당신은 당신이 이런 식으로 할 수있는 여러 파일이 포함 된 라이브러리를 추가하려면 :

  1. 이 확장 점 org.eclipse.wst의 확장 기부 JsGlobalScopeContainerInitializer
  2. 를 확장하는 클래스 만들기를 .jsdt.core.JsGlobalScopeContainerInitializer
  3. IIncludePathEntry을 추가하고 해당 ID를 사용하여 JsGlobalScopeContainer를 가리키는 프로젝트에 y OU는 JsGlobalScopeContainerInitializer

    를 확장하는 클래스를 작성

1에서 라이브러리를 사용하려면

몇 가지 매우 혼란 튜토리얼 밖에 (일식 위키에 포함) 처음에는 어렵게 만든 것이있다 이것을 이해하기. 나는 다음과 같은 내놓았다 :

[... package and imports ommited ...] 
public class LibInitializer extends JsGlobalScopeContainerInitializer { 

    private static final String LIBRARY_ID = "com.testvendor.testplugin.library"; 

    public IPath getPath() {  
     return new Path(LIBRARY_ID); 
    } 

    @Override 
    public LibraryLocation getLibraryLocation() { 
     return null; 
    } 

    @Override 
    public String getDescription() { 
     return "Test Library"; 
    } 

    @Override 
    public String getDescription(IPath containerPath, IJavaScriptProject project) { 
     return getDescription(); 
    } 

    @Override 
    public IIncludePathEntry[] getIncludepathEntries() { 

     try { 
      //get the Bundle object of the plugin 
      Bundle bundle = Platform.getBundle("com.testvendor.testplugin"); 
      //get the java.io.File object corresponding to the root of the bundles installation directory 
      File bundleFile = FileLocator.getBundleFile(bundle); 
      //add the location pointing to the library relative to that bundle root 
      File libraryLocation = new File(bundleFile, "bin/com/testvendor/testplugin/library/");    
      //create a Path object from it 
      IPath pa = new Path(libraryLocation.getAbsolutePath()); 

      /* create an IIncludePathEntry of the type "library" from this path 
      my library only contains one folder (for now) so this is it */ 
      IIncludePathEntry entry = JavaScriptCore.newLibraryEntry(pa, pa, pa); 
      //put the entry (or entries if you had more) into an array and return 
      IIncludePathEntry[] entries = {entry}; 
      return entries; 

     } catch (IOException e) { 
      e.printStackTrace(); 
     }  
     return null; 
    } 
} 

가장 흥미로운 부분은 실제 항목이 컨테이너에서 검색하는 메소드 getIncludepathEntries()이다. IIncludePathEntry는 "file : //"가상 프로토콜의 URL에서 작동하지 않으므로 Eugene에서 제안한 "toFileURL"메서드는 여기서 작동하지 않습니다.

2

이 프로젝트는 ID com.testvendor.testplugin.library 가장 쉬운 방법과 컨테이너 항목을 포함 말할 JSGlobalScope ... 확장 점의 확장을 기부하면에 기여하는 것입니다 확장 점 * org.eclipse.wst.jsdt.core.JsGlobalScopeContainerInitializer **이 같은 과정의 수업 단계에서 JsGlobalScopeContainerInitializer을 의미

<extension point="org.eclipse.wst.jsdt.core.JsGlobalScopeContainerInitializer">    
    <JsGlobalScopeContainerInitializer           
    id="com.testvendor.testplugin.library"               
    class="com.testvendor.testplugin.library.LibInitializer"/>       
</extension> 

1.

3.당신은 지금 당신이 ContainerIntitializer에 추가 한 라이브러리 폴더에 포함 된 모든 자바 스크립트 객체와 클래스가 포함 된 자바 스크립트 자원의 라이브러리 항목을해야 운이 좋다면는 프로젝트

IJavaScriptProject jsProj = ... get your project object from somewhere ... 
//create an instance of the container from step 1. 
JsGlobalScopeContainerInitializer container = new LibInitializer(); 
//create an includepath entry refering to the container   
IIncludePathEntry entry = JavaScriptCore.newContainerEntry(container.getPath()); 

IIncludePathEntry[] ipaths = jsProj.getRawIncludepath();  
IIncludePathEntry[] newpaths = new IIncludePathEntry[ipaths.length +1]; 

System.arraycopy(ipaths, 0, newpaths, 0, ipaths.length); 
//add the new entry 
newPaths[ipaths.length] = enty; 
// set the new includepath to the project 
jsProj.setRawIncludepath(newpaths, null); 

에 IIncludePathEntry를 추가. 그리고이 모든 객체와 클래스는 코드 완성 제안에서 사용할 수 있습니다.

Finally a library!

는 그게 정말 그것보다 더 간단 할 수있는 주제에 대한 좌절의 시간을 보내는에서 다른 사람을 방지 바랍니다.

-1

프로젝트는 IJavaScriptProject::setRawIncludepath 방법 중 하나를 사용합니다 경로를 포함 설정합니다. IIncludePathEntries는 항목 종류에 해당하는 JavaScriptCore::new*Entry 메서드를 호출하여 만들어집니다.

+0

감사합니다. 그건 내가 뭘 찾았지만, 문제는 내가 newProjectEntry (IPath 경로)를 사용하려면 예를 들어있다. 내 플러그인 안에있는 일부 .js 파일을 가리키는 IPath 객체를 어떻게 얻을 수 있습니까? – Chris

+0

FileLocator.toFileURL을 사용하십시오 - platform.doc.isv % 2Freference % 2Fapi % 2Forg % 2Feclipse % 2Fcore % 2Fruntime % 2FFileLocator.html – Eugene

+0

제공된 정보로 지금까지 작성한 코드를 추가했지만 작동하지 않습니다. 오류는 없지만 프로젝트의 "JavaScript Resources"노드에는 해당 항목이 없습니다. – Chris

관련 문제