在启动后修改Eclipse Project Explorer导致问题

时间:2018-01-15 16:13:37

标签: eclipse eclipse-plugin eclipse-rcp project-explorer

我有一个RCP项目。我目前想修改Project Explorer。我写了一个额外的ContentProvider(实现ICommonContentProvider)和一个额外的LabelProvider(实现ICommonLabelProvider)。在我的plugin.xml中,我添加了以下内容:

<extension
   id="navigator-viewbinding"
   point="org.eclipse.ui.navigator.viewer">
   <viewerContentBinding
         viewerId="org.eclipse.ui.navigator.ProjectExplorer">
      <includes>
         <contentExtension
               isRoot="true"          
               pattern="de.myapp.application.EditorResourceContent">
         </contentExtension>             
      </includes>
   </viewerContentBinding>      
</extension>

并且:

<extension
     id="navigator-content"
     point="org.eclipse.ui.navigator.navigatorContent">
  <navigatorContent
        activeByDefault="true"

       contentProvider="de.myapp.application.ProjectExplorerContentProvider"
        icon="icon.gif"
        id="de.myapp.application.EditorResourceContent"
        labelProvider="de.myapp.application.ProjectExplorerLabelProvider"
        name="Editor Decoration"
        priority="highest">
     <triggerPoints>
        <or>
          <instanceof
                value="org.eclipse.core.resources.IFile">
          </instanceof>
          <instanceof
                value="org.eclipse.core.resources.IFolder">
          </instanceof>
          <instanceof
                value="org.eclipse.jdt.internal.core.PackageFragment">
          </instanceof>
          <instanceof 
                value="org.eclipse.core.resources.IWorkspaceRoot" /> 
          <instanceof
                value="org.eclipse.core.resources.IProject" />  
        </or>
     </triggerPoints>
     <possibleChildren>
       <or>         
          <instanceof 
                value="org.eclipse.core.resources.IWorkspaceRoot" /> 
          <instanceof
                value="org.eclipse.core.resources.IProject" /> 
          <instanceof 
                 value="org.eclipse.core.resources.IResource" /> 
          <instanceof  
                 value="org.eclipse.core.resources.IFolder" />
          <instanceof 
                 value="org.eclipse.core.resources.IFile" /> 
          <instanceof 
                 value="org.eclipse.jdt.internal.core.PackageFragment" /> 
     </or>
 </possibleChildren>

  </navigatorContent>
 </extension>

当我启动编辑器时,Project Explorer将不会显示任何项目,直到我单击鼠标右键。然后加载所有项目。当我打开树时,我看到修改后的图标上没有错误标记,但显示了自己的图标。我也看到了包装上的错误标记(未修改)。我甚至看到了egit装饰器,但没有看到错误的红色标记。

我也提供了两个提供商,也许它会帮助我解决我的两个问题。

的ContentProvider:

public class ProjectExplorerContentProvider implements 
ICommonContentProvider 
{

private static final Object[] NO_CHILDREN = {};

@Override
public Object[] getElements(Object inputElement) {

    return getChildren(inputElement);
}

@Override
public Object[] getChildren(Object parentElement) {

    Object[] children = null;
    if(IWorkspaceRoot.class.isInstance(parentElement))
    {
        IProject[] projects = ((IWorkspaceRoot)parentElement).getProjects();
        children = createParents(projects);
    }
    else
    {
        children = NO_CHILDREN;
    }

    return children;
}

private Object[] createParents(IProject[] projects)
{
    Object[] result;

    List<Object> list = new ArrayList<Object>();
    for (int i = 0; i < projects.length; i++) {
        try {
            if(projects[i].hasNature("org.eclipse.xtext.ui.shared.xtextNature"))
                list.add(projects[i]);

        } catch (CoreException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    result = new Object[list.size()];
    list.toArray(result);

    return result;
}


@Override
public Object getParent(Object element) {

    Object parent = null;

    if(IProject.class.isInstance(element))
    {
        parent = ((IProject)element).getWorkspace().getRoot();
    }
    return parent;
}

@Override
public boolean hasChildren(Object element) {

    boolean hasChildren = false;
    if(IWorkspaceRoot.class.isInstance(element))
    {
        hasChildren = ((IWorkspaceRoot)element).getProjects().length > 0;
    }

    return hasChildren;
}

@Override
public void restoreState(IMemento aMemento) {

}

@Override
public void saveState(IMemento aMemento) {

}

@Override
public void init(ICommonContentExtensionSite aConfig) {

}

}

这里是LabelProvider:

public class ProjectExplorerLabelProvider implements ICommonLabelProvider{

/****/
@Override
public void addListener(ILabelProviderListener listener) {
    // TODO Auto-generated method stub

}

@Override
public void dispose() {
    // TODO Auto-generated method stub

}

@Override
public boolean isLabelProperty(Object element, String property) {
    // TODO Auto-generated method stub
    return false;
}

@Override
public void removeListener(ILabelProviderListener listener) {
    // TODO Auto-generated method stub

}

@Override
public Image getImage(Object anElement) {
    if (anElement instanceof File) {
        File fi = (File) anElement;
        if (fi.getFileExtension().equalsIgnoreCase("mydsl")) {
            return Activator.getImage("icons/img1.png");
        } else {
            try {
                InputStream inputStream = fi.getContents();
                String content = 
ContentFactory.getInstance().toStringInputStream(inputStream);
                inputStream.close();
                if (content.contains("some string")) {
                    return Activator.getImage("icons/img2.png");
                } else if (content.contains("some other string")) {
                    return Activator.getImage("icons/img3.png");
                } else if (content.contains("some other string")) {
                    return Activator.getImage("icons/img4.png");
                } 
            } catch (CoreException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    } else if (anElement instanceof Folder) {
        Folder fo = (Folder) anElement;
    }
    else if (anElement instanceof PackageFragment) {
        PackageFragment pf = (PackageFragment) anElement;
        }
    }

    return null;
}

@Override
public String getText(Object element) {
    // TODO Auto-generated method stub
    return null;
}

@Override
public void restoreState(IMemento aMemento) {
    // TODO Auto-generated method stub

}

@Override
public void saveState(IMemento aMemento) {
    // TODO Auto-generated method stub

}

@Override
public String getDescription(Object anElement) {
    // TODO Auto-generated method stub
    return null;
}

@Override
public void init(ICommonContentExtensionSite aConfig) {

}

}

1 个答案:

答案 0 :(得分:0)

  

当我启动编辑器时,Project Explorer不会显示任何项目,   直到我点击鼠标右键。然后加载所有项目。

对于这个问题,你可以试试这个:

  

更新2015-08-24:如果在最终申请中是ProjectExplorer   内容本身不可见(但只有在你强迫之后才能看到   更新,例如,通过右键单击打开上下文菜单,尝试   this:覆盖的getDefaultPageInput()方法   ApplicationWorkbenchAdvisor并添加以下行:

return ResourcesPlugin.getWorkspace().getRoot();

来源:

https://dirksmetric.wordpress.com/2012/08/01/tutorial-eclipse-rcp-e4-with-3-x-views-like-project-explorer-properties-etc/

  

当我打开树时,我看到修改后的图标上没有错误标记,但是   图标本身就会显示出来。

我认为你用装饰覆盖图标?很明显,它上面没有任何装饰。