FileDocCategorySizeDatePackage
FixLaunchConfig.javaAPI DocAndroid 1.5 API5855Wed May 06 22:41:10 BST 2009com.android.ide.eclipse.adt.project

FixLaunchConfig

public class FixLaunchConfig extends Thread
Class to fix the launch configuration of a project if the java package defined in the manifest has been changed.
This fix can be done synchronously, or asynchronously.
start() will start a thread that will do the fix.
run() will do the fix in the current thread.

By default, the fix first display a dialog to the user asking if he/she wants to do the fix. This can be overriden by calling setDisplayPrompt(false).

Fields Summary
private org.eclipse.core.resources.IProject
mProject
private String
mOldPackage
private String
mNewPackage
private boolean
mDisplayPrompt
Constructors Summary
public FixLaunchConfig(org.eclipse.core.resources.IProject project, String oldPackage, String newPackage)


           
        super();

        mProject = project;
        mOldPackage = oldPackage;
        mNewPackage = newPackage;
    
Methods Summary
private static org.eclipse.debug.core.ILaunchConfiguration[]findConfigs(java.lang.String projectName)
Looks for and returns all existing Launch Configuration object for a specified project.

param
projectName The name of the project
return
all the ILaunchConfiguration object. If none are present, an empty array is returned.

        // get the launch manager
        ILaunchManager manager = DebugPlugin.getDefault().getLaunchManager();

        // now get the config type for our particular android type.
        ILaunchConfigurationType configType = manager.
                getLaunchConfigurationType(LaunchConfigDelegate.ANDROID_LAUNCH_TYPE_ID);

        // create a temp list to hold all the valid configs
        ArrayList<ILaunchConfiguration> list = new ArrayList<ILaunchConfiguration>();

        try {
            ILaunchConfiguration[] configs = manager.getLaunchConfigurations(configType);

            for (ILaunchConfiguration config : configs) {
                if (config.getAttribute(
                        IJavaLaunchConfigurationConstants.ATTR_PROJECT_NAME,
                        "").equals(projectName)) {  //$NON-NLS-1$
                    list.add(config);
                }
            }
        } catch (CoreException e) {
        }

        return list.toArray(new ILaunchConfiguration[list.size()]);

    
public voidrun()
Fix the Launch configurations.


        if (mDisplayPrompt) {
            // ask the user if he really wants to fix the launch config
            boolean res = AdtPlugin.displayPrompt(
                    "Launch Configuration Update",
                    "The package definition in the manifest changed.\nDo you want to update your Launch Configuration(s)?");

            if (res == false) {
                return;
            }
        }

        // get the list of config for the project
        String projectName = mProject.getName();
        ILaunchConfiguration[] configs = findConfigs(mProject.getName());

        // loop through all the config and update the package
        for (ILaunchConfiguration config : configs) {
            try {
                // get the working copy so that we can make changes.
                ILaunchConfigurationWorkingCopy copy = config.getWorkingCopy();

                // get the attributes for the activity
                String activity = config.getAttribute(LaunchConfigDelegate.ATTR_ACTIVITY,
                        ""); //$NON-NLS-1$

                // manifests can define activities that are not in the defined package,
                // so we need to make sure the activity is inside the old package.
                if (activity.startsWith(mOldPackage)) {
                    // create the new activity
                    activity = mNewPackage + activity.substring(mOldPackage.length());

                    // put it in the copy
                    copy.setAttribute(LaunchConfigDelegate.ATTR_ACTIVITY, activity);

                    // save the config
                    copy.doSave();
                }
            } catch (CoreException e) {
                // couldn't get the working copy. we output the error in the console
                String msg = String.format("Failed to modify %1$s: %2$s", projectName,
                        e.getMessage());
                AdtPlugin.printErrorToConsole(mProject, msg);
            }
        }

    
public voidsetDisplayPrompt(boolean displayPrompt)
Set the display prompt. If true run()/start() first ask the user if he/she wants to fix the Launch Config

param
displayPrompt

        mDisplayPrompt = displayPrompt;