rcp 本机磁盘文件浏览

package com.xxx.dev;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import org.eclipse.jface.action.IMenuManager;
import org.eclipse.jface.action.IToolBarManager;
import org.eclipse.jface.viewers.ILabelProvider;
import org.eclipse.jface.viewers.ILabelProviderListener;
import org.eclipse.jface.viewers.ITreeContentProvider;
import org.eclipse.jface.viewers.LabelProviderChangedEvent;
import org.eclipse.jface.viewers.TreeViewer;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.ISharedImages;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.part.ViewPart;

public class FileViewer extends ViewPart {

    private TreeViewer tv;
    public static final String ID = "com.xxx.dev.FileViewer"; //$NON-NLS-1$

    /**
     * Create contents of the view part
     * 
     * @param parent
     */
    @Override
    public void createPartControl(Composite parent) {
        Composite container = new Composite(parent, SWT.NONE);
        container.setLayout(new FillLayout());

        tv = new TreeViewer(container, SWT.BORDER);
        tv.setContentProvider(new FileTreeContentProvider());
        // 添加标签管理器
        tv.setLabelProvider(new FileTreeLabelProvider());
        // 设置treeviewer的输入
        tv.setInput("root");
        //
        createActions();
        initializeToolBar();
        initializeMenu();
    }

    /**
     * Create the actions
     */
    private void createActions() {
        // Create the actions
    }

    /**
     * Initialize the toolbar
     */
    private void initializeToolBar() {
        IToolBarManager toolbarManager = getViewSite().getActionBars()
                .getToolBarManager();
    }

    /**
     * Initialize the menu
     */
    private void initializeMenu() {
        IMenuManager menuManager = getViewSite().getActionBars()
                .getMenuManager();
    }

    @Override
    public void setFocus() {
        // Set the focus
    }
}

class FileTreeContentProvider implements ITreeContentProvider {
    public Object[] getChildren(Object arg0) {
        // 返回树的下一级节点
        return ((File) arg0).listFiles();
    }

    public Object getParent(Object arg0) {
        // 返回树的上一级节点
        return ((File) arg0).getParentFile();
    }

    public boolean hasChildren(Object arg0) {
        Object[] bj = getChildren(arg0);
        // 判断树是否有下一级节点,true为在节点显示"+"信息
        return bj == null ? false : bj.length > 0;
    }

    public Object[] getElements(Object arg0) {
        // 打印出树的输入信息,通常用户可以通过输入信息构建树
        System.out.println(arg0);
        // File.listRoots()作为树的根节点
        return File.listRoots();
    }

    @Override
    public void dispose() {
    }

    @Override
    public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
    }
}

class FileTreeLabelProvider implements ILabelProvider {
    private List listeners;

    private String file;

    private String dir;

    boolean preserveCase;

    public FileTreeLabelProvider() {
        listeners = new ArrayList();

        // 添加文件和目录的图标
        file = ISharedImages.IMG_OBJ_ELEMENT;
        dir = ISharedImages.IMG_OBJ_FOLDER;

    }

    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 arg0) {
        // 返回目录或文件的图标
        return ((File) arg0).isDirectory() ? PlatformUI.getWorkbench().getSharedImages().getImage(dir): PlatformUI.getWorkbench().getSharedImages().getImage(file);
    }

    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() {
    }

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

    public void removeListener(ILabelProviderListener arg0) {
        // 删除监听器
        listeners.remove(arg0);
    }
}

 

你可能感兴趣的:(eclipse,UI,浏览器)