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

ResourceChooser

public class ResourceChooser extends org.eclipse.ui.dialogs.AbstractElementListSelectionDialog
A dialog to let the user select a resource based on a resource type.

Fields Summary
private Pattern
mProjectResourcePattern
private com.android.ide.eclipse.common.resources.ResourceType
mResourceType
private com.android.ide.eclipse.common.resources.IResourceRepository
mProjectResources
private static final boolean
SHOW_SYSTEM_RESOURCE
private Pattern
mSystemResourcePattern
private com.android.ide.eclipse.common.resources.IResourceRepository
mSystemResources
private org.eclipse.swt.widgets.Button
mProjectButton
private org.eclipse.swt.widgets.Button
mSystemButton
private String
mCurrentResource
private final org.eclipse.core.resources.IProject
mProject
Constructors Summary
public ResourceChooser(org.eclipse.core.resources.IProject project, com.android.ide.eclipse.common.resources.ResourceType type, com.android.ide.eclipse.common.resources.IResourceRepository projectResources, com.android.ide.eclipse.common.resources.IResourceRepository systemResources, org.eclipse.swt.widgets.Shell parent)
Creates a Resource Chooser dialog.

param
project Project being worked on
param
type The type of the resource to choose
param
projectResources The repository for the project
param
systemResources The System resource repository
param
parent the parent shell

    
                                               
        
             
             
              
        super(parent, new ResourceLabelProvider());
        mProject = project;

        mResourceType = type;
        mProjectResources = projectResources;
        
        mProjectResourcePattern = Pattern.compile(
                "@" + mResourceType.getName() + "/(.+)"); //$NON-NLS-1$ //$NON-NLS-2$

        if (SHOW_SYSTEM_RESOURCE) {
            mSystemResources = systemResources;
            mSystemResourcePattern = Pattern.compile(
                    "@android:" + mResourceType.getName() + "/(.+)"); //$NON-NLS-1$ //$NON-NLS-2$
        }

        setTitle("Resource Chooser");
        setMessage(String.format("Choose a %1$s resource",
                mResourceType.getDisplayName().toLowerCase()));
    
Methods Summary
protected voidcomputeResult()

        Object[] elements = getSelectedElements();
        if (elements.length == 1 && elements[0] instanceof ResourceItem) {
            ResourceItem item = (ResourceItem)elements[0];
            
            mCurrentResource = mResourceType.getXmlString(item,
                    SHOW_SYSTEM_RESOURCE && mSystemButton.getSelection()); 
        }
    
private voidcreateButtons(org.eclipse.swt.widgets.Composite top)
Creates the radio button to switch between project and system resources.

param
top the parent composite

        if (!SHOW_SYSTEM_RESOURCE) {
            return;
        }
        mProjectButton = new Button(top, SWT.RADIO);
        mProjectButton.setText("Project Resources");
        mProjectButton.addSelectionListener(new SelectionAdapter() {
            @Override
            public void widgetSelected(SelectionEvent e) {
                super.widgetSelected(e);
                if (mProjectButton.getSelection()) {
                    setListElements(mProjectResources.getResources(mResourceType));
                }
            }
        });
        mSystemButton = new Button(top, SWT.RADIO);
        mSystemButton.setText("System Resources");
        mSystemButton.addSelectionListener(new SelectionAdapter() {
            @Override
            public void widgetSelected(SelectionEvent e) {
                super.widgetSelected(e);
                if (mProjectButton.getSelection()) {
                    setListElements(mSystemResources.getResources(mResourceType));
                }
            }
        });
    
protected org.eclipse.swt.widgets.ControlcreateDialogArea(org.eclipse.swt.widgets.Composite parent)

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

        createMessageArea(top);

        createButtons(top);
        createFilterText(top);
        createFilteredList(top);
        
        // create the "New Resource" button
        createNewResButtons(top);

        setupResourceList();
        selectResourceString(mCurrentResource);
        
        return top;
    
private voidcreateNewResButtons(org.eclipse.swt.widgets.Composite top)
Creates the "New Resource" button.

param
top the parent composite

        
        Button newResButton = new Button(top, SWT.NONE);

        String title = String.format("New %1$s...", mResourceType.getDisplayName());
        newResButton.setText(title);

        // We only support adding new strings right now
        newResButton.setEnabled(mResourceType == ResourceType.STRING);

        newResButton.addSelectionListener(new SelectionAdapter() {
            @Override
            public void widgetSelected(SelectionEvent e) {
                super.widgetSelected(e);
                
                if (mResourceType == ResourceType.STRING) {
                    createNewString();
                }
            }
        });
    
private voidcreateNewString()

        ExtractStringRefactoring ref = new ExtractStringRefactoring(
                mProject, true /*enforceNew*/);
        RefactoringWizard wizard = new ExtractStringWizard(ref, mProject);
        RefactoringWizardOpenOperation op = new RefactoringWizardOpenOperation(wizard);
        try {
            IWorkbench w = PlatformUI.getWorkbench();
            if (op.run(w.getDisplay().getActiveShell(), wizard.getDefaultPageTitle()) ==
                    IDialogConstants.OK_ID) {

                // Recompute the "current resource" to select the new id
                setupResourceList();
                
                // select it if possible
                selectItemName(ref.getXmlStringId());
            }
        } catch (InterruptedException ex) {
            // Interrupted. Pass.
        }
    
private com.android.ide.eclipse.common.resources.IResourceRepositorygetCurrentRepository()

return
The repository currently selected.

        IResourceRepository repo = mProjectResources;
        
        if (SHOW_SYSTEM_RESOURCE && mSystemButton.getSelection()) {
            repo = mSystemResources;
        }
        return repo;
    
public java.lang.StringgetCurrentResource()

        return mCurrentResource;
    
private voidselectItemName(java.lang.String itemName)
Select an item by its name, if possible.

        if (itemName == null) {
            return;
        }

        IResourceRepository repo = getCurrentRepository();

        ResourceItem[] items = repo.getResources(mResourceType); 
        
        for (ResourceItem item : items) {
            if (itemName.equals(item.getName())) {
                setSelection(new Object[] { item });
                break;
            }
        }
    
private voidselectResourceString(java.lang.String resourceString)
Select an item by its full resource string. This also selects between project and system repository based on the resource string.

        boolean isSystem = false;
        String itemName = null;
        
        // Is this a system resource?
        // If not a system resource or if they are not available, this will be a project res.
        if (SHOW_SYSTEM_RESOURCE) {
            Matcher m = mSystemResourcePattern.matcher(resourceString);
            if (m.matches()) {
                itemName = m.group(1);
                isSystem = true;
            }
        }

        if (!isSystem && itemName == null) {
            // Try to match project resource name
            Matcher m = mProjectResourcePattern.matcher(resourceString);
            if (m.matches()) {
                itemName = m.group(1);
            }
        }
        
        // Update the repository selection
        if (SHOW_SYSTEM_RESOURCE) {
            mProjectButton.setSelection(!isSystem);
            mSystemButton.setSelection(isSystem);
        }

        // Update the list
        setupResourceList();
        
        // If we have a selection name, select it
        if (itemName != null) {
            selectItemName(itemName);
        }
    
public voidsetCurrentResource(java.lang.String resource)

        mCurrentResource = resource;
    
private voidsetupResourceList()
Setups the current list.

        IResourceRepository repo = getCurrentRepository();
        setListElements(repo.getResources(mResourceType));