WSFileDialog.java
Go to the documentation of this file.
00001 package org.ros.model.ros.diagram;
00002 
00003 
00004 
00005 import java.util.ArrayList;
00006 import java.util.Iterator;
00007 import java.util.List;
00008 
00009 import org.eclipse.core.resources.IResource;
00010 import org.eclipse.core.resources.ResourcesPlugin;
00011 import org.eclipse.core.runtime.IAdaptable;
00012 import org.eclipse.jface.dialogs.Dialog;
00013 import org.eclipse.jface.viewers.DecoratingLabelProvider;
00014 import org.eclipse.jface.viewers.ISelection;
00015 import org.eclipse.jface.viewers.IStructuredSelection;
00016 import org.eclipse.jface.viewers.TreeViewer;
00017 import org.eclipse.jface.viewers.Viewer;
00018 import org.eclipse.jface.viewers.ViewerFilter;
00019 import org.eclipse.swt.SWT;
00020 import org.eclipse.swt.layout.GridData;
00021 import org.eclipse.swt.widgets.Composite;
00022 import org.eclipse.swt.widgets.Control;
00023 import org.eclipse.swt.widgets.Shell;
00024 import org.eclipse.ui.IWorkingSet;
00025 import org.eclipse.ui.ResourceWorkingSetFilter;
00026 import org.eclipse.ui.internal.ide.IDEWorkbenchPlugin;
00027 import org.eclipse.ui.model.WorkbenchContentProvider;
00028 import org.eclipse.ui.model.WorkbenchLabelProvider;
00029 import org.eclipse.ui.views.navigator.ResourceNavigator;
00030 
00031 
00039 public class WSFileDialog extends Dialog {
00040 
00041         private TreeViewer viewer;
00042         private IResource rootElement;
00043         private boolean expand;
00044         private String[] extensions;
00045     private FilePatternFilter patternFilter = new FilePatternFilter();
00046     private ResourceWorkingSetFilter workingSetFilter = new ResourceWorkingSetFilter();
00047         private IWorkingSet workingSet;
00048         private int selectionStyle;
00049         private IResource[] result;
00050         private String title;
00051 
00061         public WSFileDialog(Shell parentShell, int selectionStyle, String title, IResource rootElement, boolean expand, String[] extensions, IWorkingSet workingSet) {
00062                 super(parentShell);
00063                 setShellStyle(getShellStyle() | SWT.RESIZE);
00064                 this.title = title;
00065                 this.rootElement = rootElement;
00066                 this.expand = expand;
00067                 this.extensions = extensions;
00068                 this.workingSet = workingSet;
00069                 this.selectionStyle = selectionStyle;
00070         }
00071         
00079         public WSFileDialog(Shell parentShell, int selectionStyle, String title) {
00080                 this(
00081                                 parentShell,
00082                                 selectionStyle,
00083                                 title,
00084                                 ResourcesPlugin.getWorkspace().getRoot(),
00085                                 true,
00086                                 null,
00087                                 null);
00088         }
00089 
00094         public void setExtensions(String[] extensions) {
00095                 this.extensions = extensions;
00096         }
00097 
00098         /* (non-Javadoc)
00099          * @see org.eclipse.jface.dialogs.Dialog#createDialogArea(org.eclipse.swt.widgets.Composite)
00100          */
00101         @Override
00102         protected Control createDialogArea(Composite parent) {
00103                 Composite comp = (Composite) super.createDialogArea(parent);
00104                 getShell().setText(title);
00105                 TreeViewer viewer = createViewer(comp);
00106                 GridData data = new GridData(GridData.FILL_BOTH);
00107                 data.grabExcessHorizontalSpace=true;
00108                 data.grabExcessVerticalSpace=true;
00109                 data.heightHint = 400;
00110                 data.widthHint = 300;
00111                 viewer.getControl().setLayoutData(data);
00112                 this.viewer = viewer;
00113                 return comp;
00114         }
00115         
00116         protected TreeViewer createViewer(Composite parent) {
00117                 ResourceNavigator nav = new ResourceNavigator();
00118                 TreeViewer viewer =
00119                         new TreeViewer(parent, selectionStyle | SWT.H_SCROLL | SWT.V_SCROLL | SWT.BORDER);
00120                 viewer.setUseHashlookup(true);
00121                 initContentProvider(viewer);
00122                 initLabelProvider(viewer);
00123                 initFilters(viewer);
00124                 viewer.setInput(rootElement);
00125                 if (expand) {
00126                         viewer.expandToLevel(2);
00127                 }
00128                 return viewer;
00129         }
00130         
00135         protected void initFilters(TreeViewer viewer) {
00136                 viewer.addFilter(patternFilter);
00137                 if (workingSet != null) {
00138                         workingSetFilter.setWorkingSet(workingSet);
00139                         viewer.addFilter(workingSetFilter);
00140                 }
00141         }
00142 
00148         protected void initContentProvider(TreeViewer viewer) {
00149                 viewer.setContentProvider(new WorkbenchContentProvider());
00150         }
00151         
00152         protected void initLabelProvider(TreeViewer viewer) {
00153                 viewer.setLabelProvider(
00154                         new DecoratingLabelProvider(
00155                                 new WorkbenchLabelProvider(),
00156                                 IDEWorkbenchPlugin.getDefault().getWorkbench().getDecoratorManager().getLabelDecorator()));
00157         }
00158 
00164         @Override
00165         protected void okPressed() {
00166                 ISelection selection = viewer.getSelection();
00167                 List data = new ArrayList();
00168                 if (!selection.isEmpty()) {
00169                         if (selection instanceof IStructuredSelection) {
00170                                 IStructuredSelection sel = (IStructuredSelection)selection;
00171                                 for (Iterator i = sel.iterator();i.hasNext();) {
00172                                         Object next = i.next();
00173                                         IResource resource= null;                       
00174                                         if (next instanceof IResource)
00175                                                 resource= (IResource)next;
00176                                         else if (next instanceof IAdaptable) {
00177                                                 if (resource == null)
00178                                                         resource= (IResource)((IAdaptable)next).getAdapter(IResource.class);
00179                                         }
00180                                         if (resource != null) {
00181                                                 data.add(resource);
00182                                         }
00183                                 }
00184                         }
00185                 }
00186                 result = (IResource[])data.toArray(new IResource[]{});
00187                 super.okPressed();
00188         }
00189         
00195         public IResource getSingleResult() {
00196                 if (getReturnCode() == OK) {
00197                         return getMultiResult()[0];
00198                 } else return null;
00199         }
00200         
00205         public IResource[] getMultiResult() {
00206                 if (getReturnCode() == OK) {
00207                         return result;
00208                 } else return null;
00209         }
00210         
00215         private class FilePatternFilter extends ViewerFilter {
00216 
00220                 @Override
00221                 public boolean select(Viewer viewer, Object parentElement, Object element) {
00222                         if (extensions == null || extensions.length == 0) return true;
00223                 IResource resource = null;
00224                 if (element instanceof IResource) {
00225                     resource = (IResource) element;
00226                 } else if (element instanceof IAdaptable) {
00227                     IAdaptable adaptable = (IAdaptable) element;
00228                     resource = (IResource) adaptable.getAdapter(IResource.class);
00229                 }
00230                 if (resource != null && !resource.isDerived() && !resource.getName().equals("CVS")) {
00231                            if (resource.getType() != IResource.FILE) return true;
00232                     String extension = resource.getFileExtension();
00233                     if (extension == null) return true;
00234                     for (int i = 0; i < extensions.length;i++) {
00235                                 if (extension.equalsIgnoreCase(extensions[i])) return true;
00236                     }
00237                 }
00238                 return false;
00239                 }
00240         }
00241 }


bride_plugin_source
Author(s): Alexander Bubeck
autogenerated on Sun Oct 5 2014 22:38:34