Automated import from //branches/donutburger/...@142276,142276

This commit is contained in:
Raphael Moll
2009-03-24 22:21:57 -07:00
committed by The Android Open Source Project
parent 71194de290
commit 0bd19e8b8b
6 changed files with 375 additions and 70 deletions

View File

@@ -501,7 +501,7 @@
class="com.android.ide.eclipse.adt.refactorings.extractstring.ExtractStringAction"
definitionId="com.android.ide.eclipse.adt.refactoring.extract.string"
id="com.android.ide.eclipse.adt.actions.ExtractString"
label="Extract Android String"
label="Extract Android String..."
menubarPath="org.eclipse.jdt.ui.refactoring.menu/codingGroup"
style="push"
tooltip="Extracts a string into Android resource string">

View File

@@ -22,9 +22,7 @@ import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.jdt.core.ICompilationUnit;
import org.eclipse.jdt.core.ITypeRoot;
import org.eclipse.jdt.core.JavaCore;
import org.eclipse.jdt.ui.JavaUI;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.text.ITextSelection;
import org.eclipse.jface.viewers.ISelection;
@@ -122,7 +120,7 @@ public class ExtractStringAction implements IWorkbenchWindowActionDelegate {
public void run(IAction action) {
if (mSelection != null && mFile != null) {
ExtractStringRefactoring ref = new ExtractStringRefactoring(mFile, mSelection);
RefactoringWizard wizard = new ExtractStringWizard(ref, "Extract Android String");
RefactoringWizard wizard = new ExtractStringWizard(ref, mFile.getProject());
RefactoringWizardOpenOperation op = new RefactoringWizardOpenOperation(wizard);
try {
op.run(mWindow.getShell(), wizard.getDefaultPageTitle());

View File

@@ -16,29 +16,74 @@
package com.android.ide.eclipse.adt.refactorings.extractstring;
import com.android.ide.eclipse.common.AndroidConstants;
import com.android.ide.eclipse.editors.resources.configurations.FolderConfiguration;
import com.android.ide.eclipse.editors.resources.manager.ResourceFolderType;
import com.android.ide.eclipse.editors.wizards.ConfigurationSelector;
import com.android.sdklib.SdkConstants;
import org.eclipse.core.resources.IFolder;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.jface.wizard.IWizardPage;
import org.eclipse.jface.wizard.WizardPage;
import org.eclipse.ltk.ui.refactoring.UserInputWizardPage;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;
import java.util.HashMap;
import java.util.TreeSet;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* @see ExtractStringRefactoring
*/
class ExtractStringInputPage extends UserInputWizardPage implements IWizardPage {
public ExtractStringInputPage() {
super("ExtractStringInputPage"); //$NON-NLS-1$
}
/** Last res file path used, shared across the session instances but specific to the
* current project. The default for unknown projects is {@link #DEFAULT_RES_FILE_PATH}. */
private static HashMap<String, String> sLastResFilePath = new HashMap<String, String>();
/** The project where the user selection happened. */
private final IProject mProject;
/** Field displaying the user-selected string to be replaced. */
private Label mStringLabel;
/** Test field where the user enters the new ID to be generated or replaced with. */
private Text mNewIdTextField;
private Label mFileLabel;
/** The configuration selector, to select the resource path of the XML file. */
private ConfigurationSelector mConfigSelector;
/** The combo to display the existing XML files or enter a new one. */
private Combo mResFileCombo;
/** Regex pattern to read a valid res XML file path. It checks that the are 2 folders and
* a leaf file name ending with .xml */
private static final Pattern RES_XML_FILE_REGEX = Pattern.compile(
"/res/[a-z][a-zA-Z0-9_-]+/[^.]+\\.xml"); //$NON-NLS-1$
/** Absolute destination folder root, e.g. "/res/" */
private static final String RES_FOLDER_ABS =
AndroidConstants.WS_RESOURCES + AndroidConstants.WS_SEP;
/** Relative destination folder root, e.g. "res/" */
private static final String RES_FOLDER_REL =
SdkConstants.FD_RESOURCES + AndroidConstants.WS_SEP;
private static final String DEFAULT_RES_FILE_PATH = "/res/values/strings.xml";
public ExtractStringInputPage(IProject project) {
super("ExtractStringInputPage"); //$NON-NLS-1$
mProject = project;
}
/**
* Create the UI for the refactoring wizard.
@@ -48,34 +93,57 @@ class ExtractStringInputPage extends UserInputWizardPage implements IWizardPage
*/
public void createControl(Composite parent) {
final ExtractStringRefactoring ref = getOurRefactoring();
Composite content = new Composite(parent, SWT.NONE);
GridLayout layout = new GridLayout();
layout.numColumns = 2;
layout.numColumns = 1;
content.setLayout(layout);
// line 1: String found in selection
Label label = new Label(content, SWT.NONE);
createStringReplacementGroup(content);
createResFileGroup(content);
validatePage();
setControl(content);
}
/**
* Creates the top group with the field to replace which string and by what
* and by which options.
*
* @param content A composite with a 1-column grid layout
*/
private void createStringReplacementGroup(Composite content) {
final ExtractStringRefactoring ref = getOurRefactoring();
Group group = new Group(content, SWT.NONE);
group.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
group.setText("String Replacement");
GridLayout layout = new GridLayout();
layout.numColumns = 2;
group.setLayout(layout);
// line: String found in selection
Label label = new Label(group, SWT.NONE);
label.setText("String:");
String selectedString = ref.getTokenString();
mStringLabel = new Label(content, SWT.NONE);
mStringLabel = new Label(group, SWT.NONE);
mStringLabel.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
mStringLabel.setText(selectedString != null ? selectedString : "");
// TODO provide an option to replace all occurences of this string instead of
// just the one.
// line 2 : Textfield for new ID
// line : Textfield for new ID
label = new Label(content, SWT.NONE);
label = new Label(group, SWT.NONE);
label.setText("Replace by R.string.");
mNewIdTextField = new Text(content, SWT.SINGLE | SWT.LEFT | SWT.BORDER);
mNewIdTextField = new Text(group, SWT.SINGLE | SWT.LEFT | SWT.BORDER);
mNewIdTextField.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
mNewIdTextField.setText(guessId(selectedString));
@@ -83,37 +151,65 @@ class ExtractStringInputPage extends UserInputWizardPage implements IWizardPage
mNewIdTextField.addModifyListener(new ModifyListener() {
public void modifyText(ModifyEvent e) {
if (validatePage(ref)) {
if (validatePage()) {
ref.setReplacementStringId(mNewIdTextField.getText().trim());
}
}
});
// line 3: selection of the output file
// TODO add a file field/chooser combo to let the user select the file to edit.
label = new Label(content, SWT.NONE);
label.setText("Resource file:");
mFileLabel = new Label(content, SWT.NONE);
mFileLabel.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
mFileLabel.setText("/res/values/strings.xml");
ref.setTargetFile(mFileLabel.getText());
// line 4: selection of the res config
// TODO add the Configuration Selector to decide with strings.xml to change
label = new Label(content, SWT.NONE);
label.setText("Configuration:");
label = new Label(content, SWT.NONE);
label.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
label.setText("default");
validatePage(ref);
setControl(content);
}
/**
* Creates the lower group with the fields to choose the resource confirmation and
* the target XML file.
*
* @param content A composite with a 1-column grid layout
*/
private void createResFileGroup(Composite content) {
Group group = new Group(content, SWT.NONE);
group.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
group.setText("XML resource to edit");
GridLayout layout = new GridLayout();
layout.numColumns = 2;
group.setLayout(layout);
// line: selection of the res config
Label label;
label = new Label(group, SWT.NONE);
label.setText("Configuration:");
mConfigSelector = new ConfigurationSelector(group);
GridData gd = new GridData(2, GridData.GRAB_HORIZONTAL | GridData.GRAB_VERTICAL);
gd.widthHint = ConfigurationSelector.WIDTH_HINT;
gd.heightHint = ConfigurationSelector.HEIGHT_HINT;
mConfigSelector.setLayoutData(gd);
OnConfigSelectorUpdated onConfigSelectorUpdated = new OnConfigSelectorUpdated();
mConfigSelector.setOnChangeListener(onConfigSelectorUpdated);
// line: selection of the output file
label = new Label(group, SWT.NONE);
label.setText("Resource file:");
mResFileCombo = new Combo(group, SWT.DROP_DOWN);
mResFileCombo.select(0);
mResFileCombo.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
mResFileCombo.addModifyListener(onConfigSelectorUpdated);
// set output file name to the last one used
String projPath = mProject.getFullPath().toPortableString();
String filePath = sLastResFilePath.get(projPath);
mResFileCombo.setText(filePath != null ? filePath : DEFAULT_RES_FILE_PATH);
onConfigSelectorUpdated.run();
}
/**
* Utility method to guess a suitable new XML ID based on the selected string.
*/
private String guessId(String text) {
// make lower case
text = text.toLowerCase();
@@ -128,16 +224,25 @@ class ExtractStringInputPage extends UserInputWizardPage implements IWizardPage
return text;
}
/**
* Returns the {@link ExtractStringRefactoring} instance used by this wizard page.
*/
private ExtractStringRefactoring getOurRefactoring() {
return (ExtractStringRefactoring) getRefactoring();
}
private boolean validatePage(ExtractStringRefactoring ref) {
String text = mNewIdTextField.getText().trim();
/**
* Validates fields of the wizard input page. Displays errors as appropriate and
* enable the "Next" button (or not) by calling {@link #setPageComplete(boolean)}.
*
* @return True if the page has been positively validated. It may still have warnings.
*/
private boolean validatePage() {
boolean success = true;
// Analyze fatal errors.
String text = mNewIdTextField.getText().trim();
if (text == null || text.length() < 1) {
setErrorMessage("Please provide a resource ID to replace with.");
success = false;
@@ -157,21 +262,216 @@ class ExtractStringInputPage extends UserInputWizardPage implements IWizardPage
}
}
String resFile = mResFileCombo.getText();
if (success) {
if (resFile == null || resFile.length() == 0) {
setErrorMessage("A resource file name is required.");
success = false;
} else if (!RES_XML_FILE_REGEX.matcher(resFile).matches()) {
setErrorMessage("The XML file name is not valid.");
success = false;
}
}
// Analyze info & warnings.
if (success) {
if (ref.isResIdDuplicate(mFileLabel.getText(), text)) {
setErrorMessage(null);
setErrorMessage(null);
ExtractStringRefactoring ref = getOurRefactoring();
ref.setTargetFile(resFile);
sLastResFilePath.put(mProject.getFullPath().toPortableString(), resFile);
if (ref.isResIdDuplicate(resFile, text)) {
setMessage(
String.format("Warning: There's already a string item called '%1$s' in %2$s.",
text, mFileLabel.getText()));
String.format("There's already a string item called '%1$s' in %2$s.",
text, resFile),
WizardPage.WARNING);
} else if (mProject.findMember(resFile) == null) {
setMessage(
String.format("File %2$s does not exist and will be created.",
text, resFile),
WizardPage.INFORMATION);
} else {
setMessage(null);
setErrorMessage(null);
}
}
setPageComplete(success);
return success;
}
public class OnConfigSelectorUpdated implements Runnable, ModifyListener {
/** Regex pattern to parse a valid res path: it reads (/res/folder-name/)+(filename). */
private final Pattern mPathRegex = Pattern.compile(
"(/res/[a-z][a-zA-Z0-9_-]+/)(.+)"); //$NON-NLS-1$
/** Temporary config object used to retrieve the Config Selector value. */
private FolderConfiguration mTempConfig = new FolderConfiguration();
private HashMap<String, TreeSet<String>> mFolderCache =
new HashMap<String, TreeSet<String>>();
private String mLastFolderUsedInCombo = null;
private boolean mInternalConfigChange;
private boolean mInternalFileComboChange;
/**
* Callback invoked when the {@link ConfigurationSelector} has been changed.
* <p/>
* The callback does the following:
* <ul>
* <li> Examine the current file name to retrieve the XML filename, if any.
* <li> Recompute the path based on the configuration selector (e.g. /res/values-fr/).
* <li> Examine the path to retrieve all the files in it. Keep those in a local cache.
* <li> If the XML filename from step 1 is not in the file list, it's a custom file name.
* Insert it and sort it.
* <li> Re-populate the file combo with all the choices.
* <li> Select the original XML file.
*/
public void run() {
if (mInternalConfigChange) {
return;
}
// get current leafname, if any
String leafName = "";
String currPath = mResFileCombo.getText();
Matcher m = mPathRegex.matcher(currPath);
if (m.matches()) {
// Note: groups 1 and 2 cannot be null.
leafName = m.group(2);
currPath = m.group(1);
} else {
// There was a path but it was invalid. Ignore it.
currPath = "";
}
// recreate the res path from the current configuration
mConfigSelector.getConfiguration(mTempConfig);
StringBuffer sb = new StringBuffer(RES_FOLDER_ABS);
sb.append(mTempConfig.getFolderName(ResourceFolderType.VALUES));
sb.append('/');
String newPath = sb.toString();
if (newPath.equals(currPath) && newPath.equals(mLastFolderUsedInCombo)) {
// Path has not changed. No need to reload.
return;
}
// Get all the files at the new path
TreeSet<String> filePaths = mFolderCache.get(newPath);
if (filePaths == null) {
filePaths = new TreeSet<String>();
IFolder folder = mProject.getFolder(newPath);
if (folder != null && folder.exists()) {
try {
for (IResource res : folder.members()) {
String name = res.getName();
if (res.getType() == IResource.FILE && name.endsWith(".xml")) {
filePaths.add(newPath + name);
}
}
} catch (CoreException e) {
// Ignore.
}
}
mFolderCache.put(newPath, filePaths);
}
currPath = newPath + leafName;
if (leafName.length() > 0 && !filePaths.contains(currPath)) {
filePaths.add(currPath);
}
// Fill the combo
try {
mInternalFileComboChange = true;
mResFileCombo.removeAll();
for (String filePath : filePaths) {
mResFileCombo.add(filePath);
}
int index = -1;
if (leafName.length() > 0) {
index = mResFileCombo.indexOf(currPath);
if (index >= 0) {
mResFileCombo.select(index);
}
}
if (index == -1) {
mResFileCombo.setText(currPath);
}
mLastFolderUsedInCombo = newPath;
} finally {
mInternalFileComboChange = false;
}
// finally validate the whole page
validatePage();
}
/**
* Callback invoked when {@link ExtractStringInputPage#mResFileCombo} has been
* modified.
*/
public void modifyText(ModifyEvent e) {
if (mInternalFileComboChange) {
return;
}
String wsFolderPath = mResFileCombo.getText();
// This is a custom path, we need to sanitize it.
// First it should start with "/res/". Then we need to make sure there are no
// relative paths, things like "../" or "./" or even "//".
wsFolderPath = wsFolderPath.replaceAll("/+\\.\\./+|/+\\./+|//+|\\\\+|^/+", "/"); //$NON-NLS-1$ //$NON-NLS-2$
wsFolderPath = wsFolderPath.replaceAll("^\\.\\./+|^\\./+", ""); //$NON-NLS-1$ //$NON-NLS-2$
wsFolderPath = wsFolderPath.replaceAll("/+\\.\\.$|/+\\.$|/+$", ""); //$NON-NLS-1$ //$NON-NLS-2$
// We get "res/foo" from selections relative to the project when we want a "/res/foo" path.
if (wsFolderPath.startsWith(RES_FOLDER_REL)) {
wsFolderPath = RES_FOLDER_ABS + wsFolderPath.substring(RES_FOLDER_REL.length());
mInternalFileComboChange = true;
mResFileCombo.setText(wsFolderPath);
mInternalFileComboChange = false;
}
if (wsFolderPath.startsWith(RES_FOLDER_ABS)) {
wsFolderPath = wsFolderPath.substring(RES_FOLDER_ABS.length());
int pos = wsFolderPath.indexOf(AndroidConstants.WS_SEP_CHAR);
if (pos >= 0) {
wsFolderPath = wsFolderPath.substring(0, pos);
}
String[] folderSegments = wsFolderPath.split(FolderConfiguration.QUALIFIER_SEP);
if (folderSegments.length > 0) {
String folderName = folderSegments[0];
if (folderName != null && !folderName.equals(wsFolderPath)) {
// update config selector
mInternalConfigChange = true;
mConfigSelector.setConfiguration(folderSegments);
mInternalConfigChange = false;
}
}
}
validatePage();
}
}
}

View File

@@ -177,7 +177,7 @@ class ExtractStringRefactoring extends Refactoring {
public ExtractStringRefactoring(IFile file, ITextSelection selection) {
mFile = file;
mSelectionStart = selection.getOffset();
mSelectionEnd = mSelectionStart + selection.getLength();
mSelectionEnd = mSelectionStart + Math.max(0, selection.getLength() - 1);
}
/**
@@ -906,9 +906,8 @@ class ExtractStringRefactoring extends Refactoring {
* compilation unit. The resource may not exist.
*/
private IResource getTargetXmlResource(String xmlFileWsPath) {
IProject proj = mUnit.getPrimary().getResource().getProject();
Path path = new Path(xmlFileWsPath);
IResource resource = proj.findMember(path);
IProject proj = mFile.getProject();
IResource resource = proj.getFile(xmlFileWsPath);
return resource;
}

View File

@@ -16,6 +16,7 @@
package com.android.ide.eclipse.adt.refactorings.extractstring;
import org.eclipse.core.resources.IProject;
import org.eclipse.ltk.ui.refactoring.RefactoringWizard;
/**
@@ -26,17 +27,24 @@ import org.eclipse.ltk.ui.refactoring.RefactoringWizard;
*/
class ExtractStringWizard extends RefactoringWizard {
private final IProject mProject;
/**
* Create a wizard for ExtractString based on a simple dialog with one page.
*
* @param ref The instance of {@link ExtractStringRefactoring} to associate to the wizard.
* @param project The project where the wizard was invoked from (e.g. where the user selection
* happened, so that we can retrieve project resources.)
*/
public ExtractStringWizard(ExtractStringRefactoring ref, String title) {
public ExtractStringWizard(ExtractStringRefactoring ref, IProject project) {
super(ref, DIALOG_BASED_USER_INTERFACE | PREVIEW_EXPAND_FIRST_NODE);
setDefaultPageTitle(title);
mProject = project;
setDefaultPageTitle(ref.getName());
}
@Override
protected void addUserInputPages() {
addPage(new ExtractStringInputPage());
addPage(new ExtractStringInputPage(mProject));
}
}

View File

@@ -271,9 +271,9 @@ class NewXmlFileCreationPage extends WizardPage {
final static int NUM_COL = 4;
/** Absolute destination folder root, e.g. "/res/" */
private static String sResFolderAbs = AndroidConstants.WS_RESOURCES + AndroidConstants.WS_SEP;
private static final String RES_FOLDER_ABS = AndroidConstants.WS_RESOURCES + AndroidConstants.WS_SEP;
/** Relative destination folder root, e.g. "res/" */
private static String sResFolderRel = SdkConstants.FD_RESOURCES + AndroidConstants.WS_SEP;
private static final String RES_FOLDER_REL = SdkConstants.FD_RESOURCES + AndroidConstants.WS_SEP;
private IProject mProject;
private Text mProjectTextField;
@@ -856,16 +856,16 @@ class NewXmlFileCreationPage extends WizardPage {
ArrayList<TypeInfo> matches = new ArrayList<TypeInfo>();
// We get "res/foo" from selections relative to the project when we want a "/res/foo" path.
if (wsFolderPath.startsWith(sResFolderRel)) {
wsFolderPath = sResFolderAbs + wsFolderPath.substring(sResFolderRel.length());
if (wsFolderPath.startsWith(RES_FOLDER_REL)) {
wsFolderPath = RES_FOLDER_ABS + wsFolderPath.substring(RES_FOLDER_REL.length());
mInternalWsFolderPathUpdate = true;
mWsFolderPathTextField.setText(wsFolderPath);
mInternalWsFolderPathUpdate = false;
}
if (wsFolderPath.startsWith(sResFolderAbs)) {
wsFolderPath = wsFolderPath.substring(sResFolderAbs.length());
if (wsFolderPath.startsWith(RES_FOLDER_ABS)) {
wsFolderPath = wsFolderPath.substring(RES_FOLDER_ABS.length());
int pos = wsFolderPath.indexOf(AndroidConstants.WS_SEP_CHAR);
if (pos >= 0) {
@@ -952,16 +952,16 @@ class NewXmlFileCreationPage extends WizardPage {
// The configuration is valid. Reformat the folder path using the canonical
// value from the configuration.
newPath = sResFolderAbs + mTempConfig.getFolderName(type.getResFolderType());
newPath = RES_FOLDER_ABS + mTempConfig.getFolderName(type.getResFolderType());
} else {
// The configuration is invalid. We still update the path but this time
// do it manually on the string.
if (wsFolderPath.startsWith(sResFolderAbs)) {
if (wsFolderPath.startsWith(RES_FOLDER_ABS)) {
wsFolderPath.replaceFirst(
"^(" + sResFolderAbs +")[^-]*(.*)", //$NON-NLS-1$ //$NON-NLS-2$
"^(" + RES_FOLDER_ABS +")[^-]*(.*)", //$NON-NLS-1$ //$NON-NLS-2$
"\\1" + type.getResFolderName() + "\\2"); //$NON-NLS-1$ //$NON-NLS-2$
} else {
newPath = sResFolderAbs + mTempConfig.getFolderName(type.getResFolderType());
newPath = RES_FOLDER_ABS + mTempConfig.getFolderName(type.getResFolderType());
}
}
@@ -1018,7 +1018,7 @@ class NewXmlFileCreationPage extends WizardPage {
if (type != null) {
mConfigSelector.getConfiguration(mTempConfig);
StringBuffer sb = new StringBuffer(sResFolderAbs);
StringBuffer sb = new StringBuffer(RES_FOLDER_ABS);
sb.append(mTempConfig.getFolderName(type.getResFolderType()));
mInternalWsFolderPathUpdate = true;
@@ -1131,8 +1131,8 @@ class NewXmlFileCreationPage extends WizardPage {
// -- validate generated path
if (error == null) {
String wsFolderPath = getWsFolderPath();
if (!wsFolderPath.startsWith(sResFolderAbs)) {
error = String.format("Target folder must start with %1$s.", sResFolderAbs);
if (!wsFolderPath.startsWith(RES_FOLDER_ABS)) {
error = String.format("Target folder must start with %1$s.", RES_FOLDER_ABS);
}
}