SWT component for selecting a file from the workspace only
I am working with SWT and Eclipse plugin. I only need to select a file from the workspace. I founded a component to select a directory in the workspace, a component to select a file in the file system, but I did not find a component to select a file from the workspace.
Now I am using org.eclipse.swt.widgets.FileDialog
and installing the filter setFilterPath(Platform.getLocation().toOSString())
. But the user can select another file not from the workspace. They should be able to install files only from the workspace.
source to share
Thanks for answers. I am creating my component and using it. I also add a filter to select files.
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;
}
}
and call:
ResourceFileSelectionDialog dialog = new ResourceFileSelectionDialog("Title", "Message", new String[] { "properties" });
dialog.open();
source to share
Try it. With this, you should be able to view your workspace.
You need to add plugins eclipse.ui
and resources
as dependencies.
ElementTreeSelectionDialog dialog = new ElementTreeSelectionDialog(
Display.getDefault().getActiveShell(),
new WorkbenchLabelProvider(),
new BaseWorkbenchContentProvider());
dialog.open();
source to share
I am not aware of any SWT component that gives you this kind of control over user experience.
So, I think the best solution is here:
You can create a window that reads the contents of a folder, displays it to the user, and doesn't give him any deployment options other than the subfolders of the root folder (the workspace folder in your case).
See examples: http://www.ibm.com/developerworks/opensource/library/os-ecgui1/ http://www.ibm.com/developerworks/library/os-ecgui2/
source to share