更改文件目录后更新JFace树视图

时间:2017-04-24 08:36:30

标签: java eclipse

大家好日子。目前正在开发一个RCP项目,该项目涉及使用treeviewer(org.eclipse.jface.viewers.TreeViewer)在Eclipse中显示目录及其中的文件。从本质上讲,树是用private String folderPath = "C:\\Default";这样的目录实例化的,它可以很好地工作。它在运行应用程序后显示初始目录及其内容。运行RCP后查看treeviewer的图像:

see image of treeviewer after running the RCP

问题是当使用DirectoryDialog对象更新文件夹路径时,树形视图保持不变。

我发现在运行应用程序时使用新文件夹路径值重新初始化树很难。

此处附有代码..

treeviewer class

        public class SampleTreeView extends ViewPart {

     private String folderPath = "C:\\Default";



     private TreeViewer tv;


     public SampleTreeView() {
      super();
     }

     public String getFolderPath() {


      return folderPath;
     }

     public void setFolderPath(String projectPath) {
      this.folderPath = projectPath;


     }

     public void createPartControl(Composite parent) {
      Composite composite = new Composite(parent, SWT.NONE);
      composite.setLayout(new GridLayout(1, false));

      // Add a check box to toggle whether the labels preserve case
      Button preserveCase = new Button(composite, SWT.CHECK);
      preserveCase.setText("&Preserve case");

      Button refreshBtn = new Button(composite, SWT.PUSH);
      refreshBtn.setText("Refresh");

      //register listener for the selection event
      refreshBtn.addSelectionListener(new SelectionAdapter() {
       @Override
       public void widgetSelected(SelectionEvent e) {

        tv.refresh();
       }
      });


      tv = new TreeViewer(composite);
      getSite().setSelectionProvider(tv);
      tv.getTree().setLayoutData(new GridData(GridData.FILL_BOTH));
      tv.setContentProvider(new FileTreeContentProvider());
      tv.setLabelProvider(new FileTreeLabelProvider());
      tv.setInput("root");

      preserveCase.addSelectionListener(new SelectionAdapter() {
       public void widgetSelected(SelectionEvent event) {
        boolean preserveCase = ((Button) event.widget).getSelection();
        FileTreeLabelProvider ftlp = (FileTreeLabelProvider) tv
         .getLabelProvider();
        ftlp.setPreserveCase(preserveCase);
       }
      });

     }

     public void setFocus() {
      tv.getControl().setFocus();
     }


     class FileTreeContentProvider implements ITreeContentProvider {


      SampleTreeView stv = new SampleTreeView();

      File source = new File(stv.getFolderPath());

      public Object[] getChildren(Object arg0) {


       return ((File) arg0).listFiles();
      }


      public Object getParent(Object arg0) {

       return ((File) arg0).getParentFile();
      }


      public boolean hasChildren(Object arg0) {

       Object[] obj = getChildren(arg0);


       return obj == null ? false : obj.length > 0;
      }


      public Object[] getElements(Object arg0) {

       return source.listFiles();
      }


      public void dispose() {

      }


      public void inputChanged(Viewer arg0, Object arg1, Object arg2) {

      }
     }


     class FileTreeLabelProvider implements ILabelProvider {

      private List listeners;
      boolean preserveCase;



      public FileTreeLabelProvider() {

       listeners = new ArrayList();

      }


      public void setPreserveCase(boolean preserveCase) {
       this.preserveCase = preserveCase;


       LabelProviderChangedEvent event = new LabelProviderChangedEvent(this);
       for (int i = 0, n = listeners.size(); i < n; i++) {
        ILabelProviderListener ilpl = (ILabelProviderListener) listeners
         .get(i);
        ilpl.labelProviderChanged(event);
       }
      }


      public Image getImage(Object obj) {
       String imageKey = ISharedImages.IMG_OBJ_FILE;
       if (((File) obj).isDirectory())
        imageKey = ISharedImages.IMG_OBJ_FOLDER;
       return PlatformUI.getWorkbench().getSharedImages().getImage(imageKey);
      }

      public String getText(Object arg0) {

       String text = ((File) arg0).getName();


       if (text.length() == 0) {
        text = ((File) arg0).getPath();
       }


       return preserveCase ? text : text.toUpperCase();
      }


      public void addListener(ILabelProviderListener arg0) {
       listeners.add(arg0);
      }

      public void dispose() {
       // Dispose the images
       if (dir != null)
        dir.dispose();
       if (file != null)
        file.dispose();
      }

      public boolean isLabelProperty(Object arg0, String arg1) {
       return false;
      }

      public void removeListener(ILabelProviderListener arg0) {
       listeners.remove(arg0);
      }
     } //end of label provider
    }

0 个答案:

没有答案