FileDocCategorySizeDatePackage
ResourceExplorerView.javaAPI DocAndroid 1.5 API13812Wed May 06 22:41:10 BST 2009com.android.ide.eclipse.editors.resources.explorer

ResourceExplorerView

public class ResourceExplorerView extends org.eclipse.ui.part.ViewPart implements com.android.ide.eclipse.editors.resources.manager.ResourceMonitor.IResourceEventListener, org.eclipse.ui.ISelectionListener
Resource Explorer View.

This contains a basic Tree view, and uses a TreeViewer to handle the data.

The view listener to change in selection in the workbench, and update to show the resource of the project of the current selected item (either item in the package explorer, or of the current editor).

see
ResourceContentProvider

Fields Summary
private static final String
PREFS_COLUMN_RES
private static final String
PREFS_COLUMN_2
private org.eclipse.swt.widgets.Tree
mTree
private org.eclipse.jface.viewers.TreeViewer
mTreeViewer
private org.eclipse.core.resources.IProject
mCurrentProject
Constructors Summary
public ResourceExplorerView()


      
    
Methods Summary
public voidcreatePartControl(org.eclipse.swt.widgets.Composite parent)

        mTree = new Tree(parent, SWT.SINGLE | SWT.VIRTUAL);
        mTree.setLayoutData(new GridData(GridData.FILL_BOTH));
        mTree.setHeaderVisible(true);
        mTree.setLinesVisible(true);

        final IPreferenceStore store = AdtPlugin.getDefault().getPreferenceStore();

        // create 2 columns. The main one with the resources, and an "info" column.
        createTreeColumn(mTree, "Resources", SWT.LEFT,
                "abcdefghijklmnopqrstuvwxz", -1, PREFS_COLUMN_RES, store); //$NON-NLS-1$
        createTreeColumn(mTree, "Info", SWT.LEFT,
                "0123456789", -1, PREFS_COLUMN_2, store); //$NON-NLS-1$

        // create the jface wrapper
        mTreeViewer = new TreeViewer(mTree);
        
        mTreeViewer.setContentProvider(new ResourceContentProvider(true /* fullLevels */));
        mTreeViewer.setLabelProvider(new ResourceLabelProvider());
        
        // listen to selection change in the workbench.
        IWorkbenchPage page = getSite().getPage();
        
        page.addSelectionListener(this);
        
        // init with current selection
        selectionChanged(getSite().getPart(), page.getSelection());
        
        // add support for double click.
        mTreeViewer.addDoubleClickListener(new IDoubleClickListener() {
            public void doubleClick(DoubleClickEvent event) {
                ISelection sel = event.getSelection();

                if (sel instanceof IStructuredSelection) {
                    IStructuredSelection selection = (IStructuredSelection) sel;

                    if (selection.size() == 1) {
                        Object element = selection.getFirstElement();
                        
                        // if it's a resourceFile, we directly open it.
                        if (element instanceof ResourceFile) {
                            try {
                                IDE.openEditor(getSite().getWorkbenchWindow().getActivePage(),
                                        ((ResourceFile)element).getFile().getIFile());
                            } catch (PartInitException e) {
                            }
                        } else if (element instanceof ProjectResourceItem) {
                            // if it's a ResourceItem, we open the first file, but only if
                            // there's no alternate files.
                            ProjectResourceItem item = (ProjectResourceItem)element;
                            
                            if (item.isEditableDirectly()) {
                                ResourceFile[] files = item.getSourceFileArray();
                                if (files[0] != null) {
                                    try {
                                        IDE.openEditor(
                                                getSite().getWorkbenchWindow().getActivePage(),
                                                files[0].getFile().getIFile());
                                    } catch (PartInitException e) {
                                    }
                                }
                            }
                        }
                    }
                }
            }
        });
        
        // set up the resource manager to send us resource change notification
        AdtPlugin.getDefault().getResourceMonitor().addResourceEventListener(this);
    
public voidcreateTreeColumn(org.eclipse.swt.widgets.Tree parent, java.lang.String header, int style, java.lang.String sample_text, int fixedSize, java.lang.String pref_name, org.eclipse.jface.preference.IPreferenceStore prefs)
Create a TreeColumn with the specified parameters. If a PreferenceStore object and a preference entry name String object are provided then the column will listen to change in its width and update the preference store accordingly.

param
parent The Table parent object
param
header The header string
param
style The column style
param
sample_text A sample text to figure out column width if preference value is missing
param
fixedSize a fixed size. If != -1 the column is non resizable
param
pref_name The preference entry name for column width
param
prefs The preference store


        // create the column
        TreeColumn col = new TreeColumn(parent, style);
        
        if (fixedSize != -1) {
            col.setWidth(fixedSize);
            col.setResizable(false);
        } else {
            // if there is no pref store or the entry is missing, we use the sample
            // text and pack the column.
            // Otherwise we just read the width from the prefs and apply it.
            if (prefs == null || prefs.contains(pref_name) == false) {
                col.setText(sample_text);
                col.pack();
    
                // init the prefs store with the current value
                if (prefs != null) {
                    prefs.setValue(pref_name, col.getWidth());
                }
            } else {
                col.setWidth(prefs.getInt(pref_name));
            }
    
            // if there is a pref store and a pref entry name, then we setup a
            // listener to catch column resize to put the new width value into the store.
            if (prefs != null && pref_name != null) {
                col.addControlListener(new ControlListener() {
                    public void controlMoved(ControlEvent e) {
                    }
    
                    public void controlResized(ControlEvent e) {
                        // get the new width
                        int w = ((TreeColumn)e.widget).getWidth();
    
                        // store in pref store
                        prefs.setValue(pref_name, w);
                    }
                });
            }
        }

        // set the header
        col.setText(header);
    
public voiddispose()

        AdtPlugin.getDefault().getResourceMonitor().removeResourceEventListener(this);

        super.dispose();
    
private booleanhandleProjectSelection(org.eclipse.core.resources.IProject project)
Handles a project selection.

param
project the new selected project
return
true if the project could be processed.

        try {
            // if it's an android project, then we get its resources, and feed them
            // to the tree viewer.
            if (project.hasNature(AndroidConstants.NATURE)) {
                if (mCurrentProject != project) {
                    ProjectResources projRes = ResourceManager.getInstance().getProjectResources(
                            project);
                    if (projRes != null) {
                        mTreeViewer.setInput(projRes);
                        mCurrentProject = project;
                        return true;
                    }
                }
            }
        } catch (CoreException e) {
        }
        
        return false;
    
public voidresourceChangeEventEnd()
Processes the end of a resource change event.

        try {
            mTree.getDisplay().asyncExec(new Runnable() {
                public void run() {
                    if (mTree.isDisposed() == false) {
                        mTreeViewer.refresh();
                    }
                }
            });
        } catch (SWTException e) {
            // display is disposed. nothing to do.
        }
    
public voidresourceChangeEventStart()
Processes a start in a resource event change.

        // pass
    
public voidselectionChanged(org.eclipse.ui.IWorkbenchPart part, org.eclipse.jface.viewers.ISelection selection)
Processes a new selection.

        // first we test if the part is an editor.
        if (part instanceof IEditorPart) {
            // if it is, we check if it's a file editor.
            IEditorInput input = ((IEditorPart)part).getEditorInput();
            
            if (input instanceof IFileEditorInput) {
                // from the file editor we can get the IFile object, and from it, the IProject.
                IFile file = ((IFileEditorInput)input).getFile();
                
                // get the file project
                IProject project = file.getProject();
                
                handleProjectSelection(project);
            }
        } else if (selection instanceof IStructuredSelection) {
            // if it's not an editor, we look for structured selection.
            for (Iterator<?> it = ((IStructuredSelection) selection).iterator();
                    it.hasNext();) {
                Object element = it.next();
                IProject project = null;
                
                // if we are in the navigator or package explorer, the selection could contain a
                // IResource object.
                if (element instanceof IResource) {
                    project = ((IResource) element).getProject();
                } else if (element instanceof IJavaElement) {
                    // if we are in the package explorer on a java element, we handle that too.
                    IJavaElement javaElement = (IJavaElement)element;
                    IJavaProject javaProject = javaElement.getJavaProject();
                    if (javaProject != null) {
                        project = javaProject.getProject();
                    }
                } else if (element instanceof IAdaptable) {
                    // finally we try to get a project object from IAdaptable.
                    project = (IProject) ((IAdaptable) element)
                            .getAdapter(IProject.class);
                }

                // if we found a project, handle it, and return.
                if (project != null) {
                    if (handleProjectSelection(project)) {
                        return;
                    }
                }
            }
        }
    
public voidsetFocus()

        mTree.setFocus();