2015-01-15 12 views
0

워크 벤치 프로젝트 탐색기에 10 개의 서로 다른 ecore 모델 중 하나의 인스턴스가있는 많은 XML 파일이 있습니다. 각 ecore 모델에 대해 사용자가 해당 XML 파일을 표시하거나 숨길 수있게하려면 내비게이터 navigatorContent 확장 점에 commonFilter을 제공하고 싶습니다. 이들은 외부 도구 파일이므로 파일 이름이나 xml 확장명을 관찰하여 내용을 식별 할 수있는 방법이 없으며 이름 바꾸기가 불가능합니다. org.eclipse.jface.viewers.ViewerFilter에서 파생 된 클래스를 사용하여 XML 파일에 포함 된 ecore 모델을 식별하는 가장 좋은 방법은 무엇입니까? 나는 거기에 EMF 자원으로, 또는 EcoreUtil으로, 또는 어댑터로 이것을 할 수있는 간단한 방법이 있다고 가정하지만, 나는 성공적인 기술을 발견하지 못했다. 또는 확장 점의 filterExpression 또는 시청자의 viewerContentBinding에서 직접이 작업을 수행하는 것이 좋습니다. genmodel 파생 플러그인은 다양한 ecore 모델에서 사용할 수 있습니다.XML 파일 용 Eclipse Project Explorer 필터

package com.my.navigator; 
import org.eclipse.core.resources.IFile; 
import org.eclipse.jface.viewers.Viewer; 
import org.eclipse.jface.viewers.ViewerFilter; 


public class MyViewerFilter extends ViewerFilter { 

public MyViewerFilter() { 
} 

@Override 
public boolean select(Viewer viewer, Object parentElement, Object element) { 
     if (element instanceof IFile) { 
      IFile file = (IFile)element; 
      // check whether file is one of our ecore models... 
      // ... 
     } 

     return true; 
    } 

} 

답변

0

당신은 당신의 파일을위한 새로운 콘텐츠 형식을 정의 할 org.eclipse.core.contenttype.contentTypes를 사용할 수 있습니다.

콘텐츠 형식 정의의 describer 인수를 사용하여 XML 파일이 요구 사항을 충족하는지 확인할 수있는 '콘텐츠 설명자'클래스를 지정합니다. 이미 설명자의 기초로 사용할 수있는 XMLContentDescriber 클래스가 있습니다.

예를 들어이 개미의 build.xml 파일의 콘텐츠 형식 정의입니다 :

<extension 
    point="org.eclipse.core.contenttype.contentTypes"> 
    <content-type 
     id="antBuildFile" 
     name="%antBuildFileContentType.name" 
     base-type="org.eclipse.core.runtime.xml" 
     file-names="build.xml" 
     file-extensions="macrodef,ent,xml,ant" 
     priority="normal"> 
     <describer 
      class="org.eclipse.ant.internal.core.contentDescriber.AntBuildfileContentDescriber"> 
     </describer> 
    </content-type> 
</extension> 

이 당신의 거친 아이디어를 제공하기 위해 개미 콘텐츠 describer 무엇을 할 수 있습니다

public final class AntBuildfileContentDescriber extends XMLContentDescriber implements IExecutableExtension { 

    private int checkCriteria(InputSource contents) throws IOException { 
     AntHandler antHandler = new AntHandler(); 
     try { 
      if (!antHandler.parseContents(contents)) { 
       return INDETERMINATE; 
      } 
     } 
     catch (SAXException e) { 
      // we may be handed any kind of contents... it is normal we fail to parse 
      return INDETERMINATE; 
     } 
     catch (ParserConfigurationException e) { 
      // some bad thing happened - force this describer to be disabled 
      String message = "Internal Error: XML parser configuration error during content description for Ant buildfiles"; //$NON-NLS-1$ 
      throw new RuntimeException(message); 
     } 
     // Check to see if we matched our criteria. 
     if (antHandler.hasRootProjectElement()) { 
      if (antHandler.hasProjectDefaultAttribute() || antHandler.hasTargetElement() || antHandler.hasAntElement()) { 
       // project and default attribute or project and target element(s) 
       // or project and top level ant element(s) (classpath, import, macrodef, path, property, taskdef, typedef) 
       return VALID; 
      } 
      // only a top level project element...maybe an Ant buildfile 
      return INDETERMINATE; 
     } 

     return INDETERMINATE; 
    } 

    @Override 
    public int describe(InputStream contents, IContentDescription description) throws IOException { 
     // call the basic XML describer to do basic recognition 
     if (super.describe(contents, description) == INVALID) { 
      return INVALID; 
     } 
     // super.describe will have consumed some chars, need to rewind 
     contents.reset(); 
     // Check to see if we matched our criteria. 
     return checkCriteria(new InputSource(contents)); 
    } 

    @Override 
    public int describe(Reader contents, IContentDescription description) throws IOException { 
     // call the basic XML describer to do basic recognition 
     if (super.describe(contents, description) == INVALID) { 
      return INVALID; 
     } 
     // super.describe will have consumed some chars, need to rewind 
     contents.reset(); 
     // Check to see if we matched our criteria. 
     return checkCriteria(new InputSource(contents)); 
    } 

    @Override 
    public void setInitializationData(IConfigurationElement config, String propertyName, Object data) throws CoreException { 
     // do nothing 
    } 
}