SWT组件仅用于从工作区选择文件

时间:2013-02-07 12:05:55

标签: java eclipse-plugin swt

我使用SWT和Eclipse的插件。我只需要从工作区选择文件。我创建了用于在工作区中选择目录的组件,用于在文件系统中选择文件的组件,但是我没有找到用于从工作区中选择文件的组件。

现在我正在使用org.eclipse.swt.widgets.FileDialog并设置过滤器setFilterPath(Platform.getLocation().toOSString())。但是用户可以选择不是来自工作区的其他文件。他们应该只能在工作区内设置文件。

3 个答案:

答案 0 :(得分:5)

感谢您的回答。我创建自己的组件并使用它。我也为选择文件添加了过滤器。

import java.util.ArrayList;
import java.util.List;

import org.eclipse.core.resources.IContainer;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.jface.viewers.ILabelProvider;
import org.eclipse.jface.viewers.ITreeContentProvider;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.dialogs.ElementTreeSelectionDialog;
import org.eclipse.ui.dialogs.ISelectionStatusValidator;
import org.eclipse.ui.model.WorkbenchLabelProvider;

/**
 * @author Alexey Prybytkouski
 */
public class ResourceFileSelectionDialog extends ElementTreeSelectionDialog {

    private String[] extensions;

    private static ITreeContentProvider contentProvider = new ITreeContentProvider() {
        public Object[] getChildren(Object element) {
            if (element instanceof IContainer) {
                try {
                    return ((IContainer) element).members();
                }
                catch (CoreException e) {
                }
            }
            return null;
        }

        public Object getParent(Object element) {
            return ((IResource) element).getParent();
        }

        public boolean hasChildren(Object element) {
            return element instanceof IContainer;
        }

        public Object[] getElements(Object input) {
            return (Object[]) input;
        }

        public void dispose() {
        }

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

    private static final IStatus OK = new Status(IStatus.OK, PLUGIN_ID, 0, "", null);
    private static final IStatus ERROR = new Status(IStatus.ERROR, PLUGIN_ID, 0, "", null);

    /*
     * Validator
     */
    private ISelectionStatusValidator validator = new ISelectionStatusValidator() {
        public IStatus validate(Object[] selection) {
            return selection.length == 1 && selection[0] instanceof IFile
                    && checkExtension(((IFile) selection[0]).getFileExtension()) ? OK : ERROR;
        }
    };

    public ResourceFileSelectionDialog(String title, String message, String[] type) {
        this(Display.getDefault().getActiveShell(), WorkbenchLabelProvider.getDecoratingWorkbenchLabelProvider(),
                contentProvider);
        this.extensions = type;

        setTitle(title);
        setMessage(message);

        setInput(computeInput());
        setValidator(validator);
    }

    public ResourceFileSelectionDialog(Shell parent, ILabelProvider labelProvider, ITreeContentProvider contentProvider) {
        super(parent, labelProvider, contentProvider);
    }

    /*
     * Show projects
     */
    private Object[] computeInput() {
        /*
         * Refresh projects tree.
         */
        IProject[] projects = ResourcesPlugin.getWorkspace().getRoot().getProjects();
        for (int i = 0; i < projects.length; i++) {
            try {
                projects[i].refreshLocal(IResource.DEPTH_INFINITE, null);
            } catch (CoreException e) {
                e.printStackTrace();
            }
        }

        try {
            ResourcesPlugin.getWorkspace().getRoot().refreshLocal(IResource.DEPTH_ONE, null);
        } catch (CoreException e) {
        }
        List<IProject> openProjects = new ArrayList<IProject>(projects.length);
        for (int i = 0; i < projects.length; i++) {
            if (projects[i].isOpen()) {
                openProjects.add(projects[i]);
            }
        }
        return openProjects.toArray();
    }

    /*
     * Check file extension
     */
    private boolean checkExtension(String name) {
        if (name.equals("*")) {
            return true;
        }

        for (int i = 0; i < extensions.length; i++) {
            if (extensions[i].equals(name)) {
                return true;
            }
        }
        return false;
    }
}

并致电:

ResourceFileSelectionDialog dialog = new ResourceFileSelectionDialog("Title", "Message", new String[] { "properties" });
dialog.open();

expample1

example2

答案 1 :(得分:3)

试试这个。有了这个,您应该能够浏览工作区。

您需要添加eclipse.uiresources个插件作为依赖项。

ElementTreeSelectionDialog dialog = new ElementTreeSelectionDialog(
    Display.getDefault().getActiveShell(), 
    new WorkbenchLabelProvider(), 
    new BaseWorkbenchContentProvider());

dialog.open();

答案 2 :(得分:1)

我不知道任何SWT组件可以为您提供对用户交互的控制。

所以,我认为这里最好的解决方案是:

您可以开发一个窗口,该窗口读取文件夹的内容,将其显示给用户,并且除了根文件夹的子文件夹(在您的情况下为工作区文件夹)之外,不会给他任何其他可能性。

见这个例子: http://www.ibm.com/developerworks/opensource/library/os-ecgui1/ http://www.ibm.com/developerworks/library/os-ecgui2/