FileDocCategorySizeDatePackage
ReferenceChooserDialog.javaAPI DocAndroid 1.5 API13395Wed May 06 22:41:10 BST 2009com.android.ide.eclipse.adt.ui

ReferenceChooserDialog

public class ReferenceChooserDialog extends org.eclipse.ui.dialogs.SelectionStatusDialog
A dialog to let the user choose a reference to a resource.

Fields Summary
private static Pattern
sResourcePattern
private static Pattern
sInlineIdResourcePattern
private static org.eclipse.jface.dialogs.IDialogSettings
sDialogSettings
private com.android.ide.eclipse.common.resources.IResourceRepository
mResources
private String
mCurrentResource
private org.eclipse.ui.dialogs.FilteredTree
mFilteredTree
private org.eclipse.swt.widgets.Button
mNewResButton
private final org.eclipse.core.resources.IProject
mProject
private org.eclipse.jface.viewers.TreeViewer
mTreeViewer
Constructors Summary
public ReferenceChooserDialog(org.eclipse.core.resources.IProject project, com.android.ide.eclipse.common.resources.IResourceRepository resources, org.eclipse.swt.widgets.Shell parent)

param
project
param
parent


              
           
        super(parent);
        mProject = project;
        mResources = resources;

        int shellStyle = getShellStyle();
        setShellStyle(shellStyle | SWT.MAX | SWT.RESIZE);

        setTitle("Reference Chooser");
        setMessage(String.format("Choose a resource"));
        
        setDialogBoundsSettings(sDialogSettings, getDialogBoundsStrategy());
    
Methods Summary
protected voidcomputeResult()

        // get the selection
        TreePath treeSelection = getSelection();
        if (treeSelection != null) {
            if (treeSelection.getSegmentCount() == 2) {
                // get the resource type and the resource item
                ResourceType resourceType = (ResourceType)treeSelection.getFirstSegment();
                ResourceItem resourceItem = (ResourceItem)treeSelection.getLastSegment();
                
                mCurrentResource = resourceType.getXmlString(resourceItem, false /* system */); 
            }
        }
    
protected org.eclipse.swt.widgets.ControlcreateDialogArea(org.eclipse.swt.widgets.Composite parent)

        Composite top = (Composite)super.createDialogArea(parent);

        // create the standard message area
        createMessageArea(top);

        // create the filtered tree
        createFilteredTree(top);

        // setup the initial selection
        setupInitialSelection();
        
        // create the "New Resource" button
        createNewResButtons(top);
        
        return top;
    
private voidcreateFilteredTree(org.eclipse.swt.widgets.Composite parent)

        mFilteredTree = new FilteredTree(parent, SWT.BORDER | SWT.SINGLE | SWT.FULL_SELECTION,
                new PatternFilter());
        
        GridData data = new GridData();
        data.widthHint = convertWidthInCharsToPixels(60);
        data.heightHint = convertHeightInCharsToPixels(18);
        data.grabExcessVerticalSpace = true;
        data.grabExcessHorizontalSpace = true;
        data.horizontalAlignment = GridData.FILL;
        data.verticalAlignment = GridData.FILL;
        mFilteredTree.setLayoutData(data);
        mFilteredTree.setFont(parent.getFont());
        
        mTreeViewer = mFilteredTree.getViewer();
        Tree tree = mTreeViewer.getTree();
        
        tree.addSelectionListener(new SelectionListener() {
            public void widgetDefaultSelected(SelectionEvent e) {
                handleDoubleClick();
            }

            public void widgetSelected(SelectionEvent e) {
                handleSelection();
            }
        });
        
        mTreeViewer.setLabelProvider(new ResourceLabelProvider());
        mTreeViewer.setContentProvider(new ResourceContentProvider(false /* fullLevels */));
        mTreeViewer.setInput(mResources);
    
private voidcreateNewResButtons(org.eclipse.swt.widgets.Composite top)
Creates the "New Resource" button.

param
top the parent composite

        mNewResButton = new Button(top, SWT.NONE);
        mNewResButton.addSelectionListener(new OnNewResButtonSelected());
        updateNewResButton();
    
public java.lang.StringgetCurrentResource()

        return mCurrentResource;
    
private com.android.ide.eclipse.common.resources.ResourceTypegetSelectedResourceType()
Returns the {@link ResourceType} of the selected element, if any. Returns null if nothing suitable is selected.

        ResourceType type = null;

        TreePath selection = getSelection();
        if (selection != null && selection.getSegmentCount() > 0) {
            Object first = selection.getFirstSegment();
            if (first instanceof ResourceType) {
                type = (ResourceType) first;
            }
        }
        return type;
    
private org.eclipse.jface.viewers.TreePathgetSelection()
Returns the selected item in the tree as a {@link TreePath} object.

return
the TreePath object or null if there was no selection.

        ISelection selection = mFilteredTree.getViewer().getSelection();
        if (selection instanceof TreeSelection) {
            TreeSelection treeSelection = (TreeSelection)selection;
            TreePath[] treePaths = treeSelection.getPaths();
            
            // the selection mode is SWT.SINGLE, so we just get the first one.
            if (treePaths.length > 0) {
                return treePaths[0];
            }
        }
        
        return null;
    
protected voidhandleDoubleClick()

        if (validateCurrentSelection()) {
            buttonPressed(IDialogConstants.OK_ID);
        }
    
protected voidhandleSelection()

        validateCurrentSelection();
        updateNewResButton();
    
public voidsetCurrentResource(java.lang.String resource)

        mCurrentResource = resource;
    
private voidsetupInitialSelection()
Sets up the initial selection.

This parses {@link #mCurrentResource} to find out the resource type and the resource name.

        // checks the inline id pattern first as it's more restrictive than the other one.
        Matcher m = sInlineIdResourcePattern.matcher(mCurrentResource);
        if (m.matches()) {
            // get the matching name
            String resourceName = m.group(1);

            // setup initial selection
            setupInitialSelection(ResourceType.ID, resourceName);
        } else {
            // attempts the inline id pattern
            m = sResourcePattern.matcher(mCurrentResource);
            if (m.matches()) {
                // get the resource type.
                ResourceType resourceType = ResourceType.getEnum(m.group(1));
                if (resourceType != null) {
                    // get the matching name
                    String resourceName = m.group(2);
                    
                    // setup initial selection
                    setupInitialSelection(resourceType, resourceName);
                }
            }
        }
    
private voidsetupInitialSelection(com.android.ide.eclipse.common.resources.ResourceType resourceType, java.lang.String resourceName)
Sets up the initial selection based on a {@link ResourceType} and a resource name.

param
resourceType the resource type.
param
resourceName the resource name.

        // get all the resources of this type
        ResourceItem[] resourceItems = mResources.getResources(resourceType);
        
        for (ResourceItem resourceItem : resourceItems) {
            if (resourceName.equals(resourceItem.getName())) {
                // name of the resource match, we select it,
                TreePath treePath = new TreePath(new Object[] { resourceType, resourceItem });
                mFilteredTree.getViewer().setSelection(
                        new TreeSelection(treePath),
                        true /*reveal*/);
                
                // and we're done.
                return;
            }
        }
        
        // if we get here, the resource type is valid, but the resource is missing.
        // we select and expand the resource type element.
        TreePath treePath = new TreePath(new Object[] { resourceType });
        mFilteredTree.getViewer().setSelection(
                new TreeSelection(treePath),
                true /*reveal*/);
        mFilteredTree.getViewer().setExpandedState(resourceType, true /* expanded */);
    
private voidupdateNewResButton()
Updates the new res button when the list selection changes. The name of the button changes depending on the resource.

        ResourceType type = getSelectedResourceType();
        
        // We only support adding new strings right now
        mNewResButton.setEnabled(type == ResourceType.STRING);
        
        String title = String.format("New %1$s...",
                type == null ? "Resource" : type.getDisplayName());
        mNewResButton.setText(title);
        mNewResButton.pack();
    
private booleanvalidateCurrentSelection()

        TreePath treeSelection = getSelection();
        
        IStatus status;
        if (treeSelection != null) {
            if (treeSelection.getSegmentCount() == 2) {
                status = new Status(IStatus.OK, AdtPlugin.PLUGIN_ID,
                        IStatus.OK, "", //$NON-NLS-1$
                        null);
            } else {
                status = new Status(IStatus.ERROR, AdtPlugin.PLUGIN_ID,
                        IStatus.ERROR, "You must select a Resource Item",
                        null);
            }
        } else {
            status = new Status(IStatus.ERROR, AdtPlugin.PLUGIN_ID,
                    IStatus.ERROR, "", //$NON-NLS-1$
                    null);
        }
        
        updateStatus(status);

        return status.isOK();