AI 144487: ADT: Add "New String" refactoring to the other Resource Chooser.
BUG=1722971 Automated import of CL 144487
This commit is contained in:
committed by
The Android Open Source Project
parent
bc659bb756
commit
d7c946dcef
@@ -16,10 +16,16 @@
|
|||||||
|
|
||||||
package com.android.ide.eclipse.adt.ui;
|
package com.android.ide.eclipse.adt.ui;
|
||||||
|
|
||||||
|
import com.android.ide.eclipse.adt.refactorings.extractstring.ExtractStringRefactoring;
|
||||||
|
import com.android.ide.eclipse.adt.refactorings.extractstring.ExtractStringWizard;
|
||||||
import com.android.ide.eclipse.common.resources.IResourceRepository;
|
import com.android.ide.eclipse.common.resources.IResourceRepository;
|
||||||
import com.android.ide.eclipse.common.resources.ResourceItem;
|
import com.android.ide.eclipse.common.resources.ResourceItem;
|
||||||
import com.android.ide.eclipse.common.resources.ResourceType;
|
import com.android.ide.eclipse.common.resources.ResourceType;
|
||||||
|
|
||||||
|
import org.eclipse.core.resources.IProject;
|
||||||
|
import org.eclipse.jface.dialogs.IDialogConstants;
|
||||||
|
import org.eclipse.ltk.ui.refactoring.RefactoringWizard;
|
||||||
|
import org.eclipse.ltk.ui.refactoring.RefactoringWizardOpenOperation;
|
||||||
import org.eclipse.swt.SWT;
|
import org.eclipse.swt.SWT;
|
||||||
import org.eclipse.swt.events.SelectionAdapter;
|
import org.eclipse.swt.events.SelectionAdapter;
|
||||||
import org.eclipse.swt.events.SelectionEvent;
|
import org.eclipse.swt.events.SelectionEvent;
|
||||||
@@ -27,6 +33,8 @@ import org.eclipse.swt.widgets.Button;
|
|||||||
import org.eclipse.swt.widgets.Composite;
|
import org.eclipse.swt.widgets.Composite;
|
||||||
import org.eclipse.swt.widgets.Control;
|
import org.eclipse.swt.widgets.Control;
|
||||||
import org.eclipse.swt.widgets.Shell;
|
import org.eclipse.swt.widgets.Shell;
|
||||||
|
import org.eclipse.ui.IWorkbench;
|
||||||
|
import org.eclipse.ui.PlatformUI;
|
||||||
import org.eclipse.ui.dialogs.AbstractElementListSelectionDialog;
|
import org.eclipse.ui.dialogs.AbstractElementListSelectionDialog;
|
||||||
|
|
||||||
import java.util.regex.Matcher;
|
import java.util.regex.Matcher;
|
||||||
@@ -50,26 +58,32 @@ public class ResourceChooser extends AbstractElementListSelectionDialog {
|
|||||||
private Button mSystemButton;
|
private Button mSystemButton;
|
||||||
|
|
||||||
private String mCurrentResource;
|
private String mCurrentResource;
|
||||||
|
|
||||||
|
private final IProject mProject;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates a Resource Chooser dialog.
|
* Creates a Resource Chooser dialog.
|
||||||
|
* @param project Project being worked on
|
||||||
* @param type The type of the resource to choose
|
* @param type The type of the resource to choose
|
||||||
* @param project The repository for the project
|
* @param projectResources The repository for the project
|
||||||
* @param system The System resource repository
|
* @param systemResources The System resource repository
|
||||||
* @param parent the parent shell
|
* @param parent the parent shell
|
||||||
*/
|
*/
|
||||||
public ResourceChooser(ResourceType type, IResourceRepository project,
|
public ResourceChooser(IProject project, ResourceType type,
|
||||||
IResourceRepository system, Shell parent) {
|
IResourceRepository projectResources,
|
||||||
|
IResourceRepository systemResources,
|
||||||
|
Shell parent) {
|
||||||
super(parent, new ResourceLabelProvider());
|
super(parent, new ResourceLabelProvider());
|
||||||
|
mProject = project;
|
||||||
|
|
||||||
mResourceType = type;
|
mResourceType = type;
|
||||||
mProjectResources = project;
|
mProjectResources = projectResources;
|
||||||
|
|
||||||
mProjectResourcePattern = Pattern.compile(
|
mProjectResourcePattern = Pattern.compile(
|
||||||
"@" + mResourceType.getName() + "/(.+)"); //$NON-NLS-1$ //$NON-NLS-2$
|
"@" + mResourceType.getName() + "/(.+)"); //$NON-NLS-1$ //$NON-NLS-2$
|
||||||
|
|
||||||
if (SHOW_SYSTEM_RESOURCE) {
|
if (SHOW_SYSTEM_RESOURCE) {
|
||||||
mSystemResources = system;
|
mSystemResources = systemResources;
|
||||||
mSystemResourcePattern = Pattern.compile(
|
mSystemResourcePattern = Pattern.compile(
|
||||||
"@android:" + mResourceType.getName() + "/(.+)"); //$NON-NLS-1$ //$NON-NLS-2$
|
"@android:" + mResourceType.getName() + "/(.+)"); //$NON-NLS-1$ //$NON-NLS-2$
|
||||||
}
|
}
|
||||||
@@ -108,7 +122,11 @@ public class ResourceChooser extends AbstractElementListSelectionDialog {
|
|||||||
createFilterText(top);
|
createFilterText(top);
|
||||||
createFilteredList(top);
|
createFilteredList(top);
|
||||||
|
|
||||||
setupResourceListAndCurrent();
|
// create the "New Resource" button
|
||||||
|
createNewResButtons(top);
|
||||||
|
|
||||||
|
setupResourceList();
|
||||||
|
selectResourceString(mCurrentResource);
|
||||||
|
|
||||||
return top;
|
return top;
|
||||||
}
|
}
|
||||||
@@ -127,7 +145,9 @@ public class ResourceChooser extends AbstractElementListSelectionDialog {
|
|||||||
@Override
|
@Override
|
||||||
public void widgetSelected(SelectionEvent e) {
|
public void widgetSelected(SelectionEvent e) {
|
||||||
super.widgetSelected(e);
|
super.widgetSelected(e);
|
||||||
setListElements(mProjectResources.getResources(mResourceType));
|
if (mProjectButton.getSelection()) {
|
||||||
|
setListElements(mProjectResources.getResources(mResourceType));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
mSystemButton = new Button(top, SWT.RADIO);
|
mSystemButton = new Button(top, SWT.RADIO);
|
||||||
@@ -136,63 +156,138 @@ public class ResourceChooser extends AbstractElementListSelectionDialog {
|
|||||||
@Override
|
@Override
|
||||||
public void widgetSelected(SelectionEvent e) {
|
public void widgetSelected(SelectionEvent e) {
|
||||||
super.widgetSelected(e);
|
super.widgetSelected(e);
|
||||||
setListElements(mSystemResources.getResources(mResourceType));
|
if (mProjectButton.getSelection()) {
|
||||||
|
setListElements(mSystemResources.getResources(mResourceType));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates the "New Resource" button.
|
||||||
|
* @param top the parent composite
|
||||||
|
*/
|
||||||
|
private void createNewResButtons(Composite top) {
|
||||||
|
|
||||||
|
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 void createNewString() {
|
||||||
* Setups the current list based on the current resource.
|
ExtractStringRefactoring ref = new ExtractStringRefactoring(
|
||||||
*/
|
mProject, true /*enforceNew*/);
|
||||||
private void setupResourceListAndCurrent() {
|
RefactoringWizard wizard = new ExtractStringWizard(ref, mProject);
|
||||||
if (setupInitialSelection(mProjectResourcePattern, mProjectResources) == false) {
|
RefactoringWizardOpenOperation op = new RefactoringWizardOpenOperation(wizard);
|
||||||
// if we couldn't understand the current value, we default to the project resources
|
try {
|
||||||
ResourceItem[] items = mProjectResources.getResources(mResourceType);
|
IWorkbench w = PlatformUI.getWorkbench();
|
||||||
setListElements(items);
|
if (op.run(w.getDisplay().getActiveShell(), wizard.getDefaultPageTitle()) ==
|
||||||
}
|
IDialogConstants.OK_ID) {
|
||||||
|
|
||||||
if (SHOW_SYSTEM_RESOURCE) {
|
// Recompute the "current resource" to select the new id
|
||||||
if (setupInitialSelection(mProjectResourcePattern, mProjectResources) == false) {
|
setupResourceList();
|
||||||
if (setupInitialSelection(mSystemResourcePattern, mSystemResources) == false) {
|
|
||||||
// if we couldn't understand the current value,
|
// select it if possible
|
||||||
// we default to the project resources
|
selectItemName(ref.getXmlStringId());
|
||||||
ResourceItem[] items = mProjectResources.getResources(mResourceType);
|
|
||||||
setListElements(items);
|
|
||||||
mProjectButton.setSelection(true);
|
|
||||||
} else {
|
|
||||||
mSystemButton.setSelection(true);
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
mProjectButton.setSelection(true);
|
|
||||||
}
|
}
|
||||||
|
} catch (InterruptedException ex) {
|
||||||
|
// Interrupted. Pass.
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return The repository currently selected.
|
||||||
|
*/
|
||||||
|
private IResourceRepository getCurrentRepository() {
|
||||||
|
IResourceRepository repo = mProjectResources;
|
||||||
|
|
||||||
|
if (SHOW_SYSTEM_RESOURCE && mSystemButton.getSelection()) {
|
||||||
|
repo = mSystemResources;
|
||||||
|
}
|
||||||
|
return repo;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Setups the current list.
|
||||||
|
*/
|
||||||
|
private void setupResourceList() {
|
||||||
|
IResourceRepository repo = getCurrentRepository();
|
||||||
|
setListElements(repo.getResources(mResourceType));
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Attempts to setup the list of element from a repository if the current resource
|
* Select an item by its name, if possible.
|
||||||
* matches the provided pattern.
|
|
||||||
* @param pattern the pattern to test the current value
|
|
||||||
* @param repository the repository to use if the pattern matches.
|
|
||||||
* @return true if success.
|
|
||||||
*/
|
*/
|
||||||
private boolean setupInitialSelection(Pattern pattern, IResourceRepository repository) {
|
private void selectItemName(String itemName) {
|
||||||
Matcher m = pattern.matcher(mCurrentResource);
|
if (itemName == null) {
|
||||||
if (m.matches()) {
|
return;
|
||||||
// we have a project resource, let's setup the list
|
}
|
||||||
ResourceItem[] items = repository.getResources(mResourceType);
|
|
||||||
setListElements(items);
|
IResourceRepository repo = getCurrentRepository();
|
||||||
|
|
||||||
// and let's look for the item we found
|
ResourceItem[] items = repo.getResources(mResourceType);
|
||||||
String name = m.group(1);
|
|
||||||
|
for (ResourceItem item : items) {
|
||||||
for (ResourceItem item : items) {
|
if (itemName.equals(item.getName())) {
|
||||||
if (name.equals(item.getName())) {
|
setSelection(new Object[] { item });
|
||||||
setSelection(new Object[] { item });
|
break;
|
||||||
break;
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return true;
|
|
||||||
|
/**
|
||||||
|
* Select an item by its full resource string.
|
||||||
|
* This also selects between project and system repository based on the resource string.
|
||||||
|
*/
|
||||||
|
private void selectResourceString(String resourceString) {
|
||||||
|
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);
|
||||||
}
|
}
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -135,8 +135,11 @@ public class UiResourceAttributeNode extends UiTextAttributeNode {
|
|||||||
IResourceRepository systemRepository = data.getSystemResources();
|
IResourceRepository systemRepository = data.getSystemResources();
|
||||||
|
|
||||||
// open a resource chooser dialog for specified resource type.
|
// open a resource chooser dialog for specified resource type.
|
||||||
ResourceChooser dlg = new ResourceChooser(mType,
|
ResourceChooser dlg = new ResourceChooser(project,
|
||||||
projectRepository, systemRepository, shell);
|
mType,
|
||||||
|
projectRepository,
|
||||||
|
systemRepository,
|
||||||
|
shell);
|
||||||
|
|
||||||
dlg.setCurrentResource(currentValue);
|
dlg.setCurrentResource(currentValue);
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user