}
/**
+ * Creates a Java class name out of the given string, if possible. For
+ * example, "My Project" becomes "MyProject", "hello" becomes "Hello",
+ * "Java's" becomes "Java", and so on.
+ *
+ * @param string the string to be massaged into a Java class
+ * @return the string as a Java class, or null if a class name could not be
+ * extracted
+ */
+ public static String extractClassName(String string) {
+ StringBuilder sb = new StringBuilder(string.length());
+ int n = string.length();
+
+ int i = 0;
+ for (; i < n; i++) {
+ char c = Character.toUpperCase(string.charAt(i));
+ if (Character.isJavaIdentifierStart(c)) {
+ sb.append(c);
+ i++;
+ break;
+ }
+ }
+ if (sb.length() > 0) {
+ for (; i < n; i++) {
+ char c = string.charAt(i);
+ if (Character.isJavaIdentifierPart(c)) {
+ sb.append(c);
+ }
+ }
+
+ return sb.toString();
+ }
+
+ return null;
+ }
+
+ /**
* Strips off the last file extension from the given filename, e.g.
* "foo.backup.diff" will be turned into "foo.backup".
* <p>
package com.android.ide.eclipse.adt.internal.wizards.newproject;
import static com.android.ide.eclipse.adt.AdtUtils.capitalize;
+import static com.android.ide.eclipse.adt.AdtUtils.extractClassName;
import static com.android.ide.eclipse.adt.AdtUtils.stripWhitespace;
import com.android.ide.eclipse.adt.AdtConstants;
}
-
/**
* Structure that collects all externally visible information from this page.
* This is used by the calling wizard to actually do the work or by other pages.
// This will invoke the selection listener on the selector defined above.
if (targets != null && targets.length == 1) {
mSdkTargetSelector.setSelection(targets[0]);
+ } else if (targets != null) {
+ // Pick the highest available platform by default (see issue #17505
+ // for related discussion.)
+ IAndroidTarget initialTarget = null;
+ for (IAndroidTarget target : targets) {
+ if (target.isPlatform()
+ && !target.getVersion().isPreview()
+ && (initialTarget == null ||
+ target.getVersion().getApiLevel() >
+ initialTarget.getVersion().getApiLevel())) {
+ initialTarget = target;
+ }
+ }
+ if (initialTarget != null) {
+ mSdkTargetSelector.setSelection(initialTarget);
+ }
}
}
if (!mInternalApplicationNameUpdate) {
mApplicationNameModifiedByUser = true;
if (!mActivityNameModifiedByUser) {
- String name = capitalize(mApplicationNameField.getText());
- try {
- mInternalActivityNameUpdate = true;
- mActivityNameField.setText(stripWhitespace(name) + ACTIVITY_NAME_SUFFIX);
- } finally {
- mInternalActivityNameUpdate = false;
+ String name = extractClassName(mApplicationNameField.getText());
+ if (name != null) {
+ try {
+ mInternalActivityNameUpdate = true;
+ mActivityNameField.setText(stripWhitespace(name) + ACTIVITY_NAME_SUFFIX);
+ } finally {
+ mInternalActivityNameUpdate = false;
+ }
}
}
}
return setStatus("Enter a valid activity name", MSG_ERROR);
}
+ if (activityFieldContents.contains("..")) { //$NON-NLS-1$
+ return setStatus("Package segments in activity name cannot be empty (..)", MSG_ERROR);
+ }
+
// The activity field can actually contain part of a sub-package name
// or it can start with a dot "." to indicates it comes from the parent package name.
String packageName = ""; //$NON-NLS-1$
// the activity field can contain a simple java identifier, or a
// package name or one that starts with a dot. So if it starts with a dot,
// ignore this dot -- the rest must look like a package name.
- if (activityFieldContents.charAt(0) == '.') {
+ if (activityFieldContents.length() > 0 && activityFieldContents.charAt(0) == '.') {
activityFieldContents = activityFieldContents.substring(1);
}
import com.android.ide.eclipse.adt.internal.preferences.AdtPrefs;
import com.android.ide.eclipse.adt.internal.project.AndroidNature;
import com.android.ide.eclipse.adt.internal.project.ProjectHelper;
+import com.android.ide.eclipse.adt.internal.refactorings.extractstring.ExtractStringRefactoring;
import com.android.ide.eclipse.adt.internal.sdk.Sdk;
import com.android.ide.eclipse.adt.internal.wizards.newproject.NewProjectCreationPage.IMainInfo;
import com.android.ide.eclipse.adt.internal.wizards.newproject.NewTestProjectCreationPage.TestInfo;
import java.net.MalformedURLException;
import java.util.HashMap;
import java.util.Map;
-import java.util.Set;
import java.util.Map.Entry;
+import java.util.Set;
/**
* A "New Android Project" Wizard.
// get the value from the key
String value = strings.get(key);
+ // Escape values if necessary
+ value = ExtractStringRefactoring.escapeString(value);
+
// place them in the template
String stringDef = stringTemplate.replace(PARAM_STRING_NAME, key);
stringDef = stringDef.replace(PARAM_STRING_CONTENT, value);
assertEquals("foobar", AdtUtils.stripWhitespace(" foo bar \n\t"));
}
+ public void testExtractClassName() {
+ assertEquals("Foo", AdtUtils.extractClassName("foo"));
+ assertEquals("Foobar", AdtUtils.extractClassName("foo bar"));
+ assertEquals("JavasTypeSystem", AdtUtils.extractClassName("Java's Type System"));
+ assertEquals("Foo", AdtUtils.extractClassName("1foo "));
+ }
+
public void testStripAllExtensions() {
assertEquals("", AdtUtils.stripAllExtensions(""));
assertEquals("foobar", AdtUtils.stripAllExtensions("foobar"));