KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > debug > internal > ui > launchConfigurations > DeleteLaunchConfigurationStatusHandler


1 /*******************************************************************************
2  * Copyright (c) 2004, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.debug.internal.ui.launchConfigurations;
12
13 import java.util.ArrayList JavaDoc;
14 import java.util.HashMap JavaDoc;
15
16 import org.eclipse.core.resources.IProject;
17 import org.eclipse.core.resources.IResource;
18 import org.eclipse.core.runtime.CoreException;
19 import org.eclipse.core.runtime.IStatus;
20 import org.eclipse.debug.core.DebugPlugin;
21 import org.eclipse.debug.core.ILaunchConfiguration;
22 import org.eclipse.debug.core.IStatusHandler;
23 import org.eclipse.debug.internal.ui.DebugUIPlugin;
24 import org.eclipse.debug.internal.ui.IInternalDebugUIConstants;
25 import org.eclipse.jface.dialogs.IDialogConstants;
26 import org.eclipse.jface.dialogs.MessageDialogWithToggle;
27 import org.eclipse.ui.model.AdaptableList;
28
29 /**
30  * Provides a status handler to prompt the user to delete any launch configurations
31  * for the associated project that is about to be deleted, for more information
32  * see <code>LaunchManager#preDelete(IProject)</code>.
33  *
34  * @since 3.2
35  *
36  */

37 public class DeleteLaunchConfigurationStatusHandler implements IStatusHandler {
38     
39     /* (non-Javadoc)
40      * @see org.eclipse.debug.core.IStatusHandler#handleStatus(org.eclipse.core.runtime.IStatus, java.lang.Object)
41      */

42     public Object JavaDoc handleStatus(IStatus status, Object JavaDoc source) throws CoreException {
43         String JavaDoc pref = DebugUIPlugin.getDefault().getPreferenceStore().getString(IInternalDebugUIConstants.PREF_DELETE_CONFIGS_ON_PROJECT_DELETE);
44         if(source instanceof IProject[]) {
45             IProject[] projects = (IProject[])source;
46             ArrayList JavaDoc configs = new ArrayList JavaDoc();
47             ArrayList JavaDoc elements = null;
48             HashMap JavaDoc map = new HashMap JavaDoc();
49             for (int i = 0; i < projects.length; i++) {
50                 elements = collectAssociatedLaunches(projects[i]);
51                 if(!elements.isEmpty()) {
52                     map.put(projects[i], elements);
53                     configs.addAll(elements);
54                 }
55             }
56             if(configs.size() > 0) {
57                 if(pref.equals(MessageDialogWithToggle.PROMPT)) {
58                     DeleteAssociatedLaunchConfigurationsDialog lsd = new DeleteAssociatedLaunchConfigurationsDialog(DebugUIPlugin.getShell(),
59                             new AdaptableList(configs),
60                             LaunchConfigurationsMessages.DeleteLaunchConfigurations_0,
61                             map);
62                     lsd.setInitialSelections(configs.toArray());
63                     lsd.setTitle(LaunchConfigurationsMessages.DeleteLaunchConfigurations_1);
64                     if(lsd.open() == IDialogConstants.OK_ID) {
65                         doDelete(lsd.getResult());
66                     }
67                 }
68                 else if(pref.equals(MessageDialogWithToggle.ALWAYS)){
69                     doDelete(configs.toArray());
70                 }
71             }
72         }
73         return null;
74     }
75
76     /**
77      * Gets the launch configuration associated with the specified project.
78      * This method relies on the resource mapping existing, if no such mapping
79      * exists the launch configuration is ignored.
80      *
81      * @param project the project to collect launch configurations for
82      * @return the list of associated launch configurations
83      */

84     private ArrayList JavaDoc collectAssociatedLaunches(IProject project) {
85         ArrayList JavaDoc list = new ArrayList JavaDoc();
86         try {
87             ILaunchConfiguration[] configs = DebugPlugin.getDefault().getLaunchManager().getLaunchConfigurations();
88             IResource[] resources = null;
89             for(int i = 0; i < configs.length; i++) {
90                 if(configs[i].isLocal()) {
91                     resources = configs[i].getMappedResources();
92                     if(resources != null) {
93                         for(int j = 0; j < resources.length; j++){
94                             if(resources[j].equals(project)) {
95                                 list.add(configs[i]);
96                             }
97                         }
98                     }
99                 }
100             }
101         }
102         catch (CoreException e) {
103             DebugUIPlugin.log(e);
104         }
105         return list;
106     }
107     
108     /**
109      * Actually performs the deletion of the launch configurations.
110      * @param launches the launch configurations to delete
111      */

112     private void doDelete(Object JavaDoc[] launches) {
113         try {
114             for(int i = 0; i < launches.length; i++) {
115                 ((ILaunchConfiguration)launches[i]).delete();
116             }
117         }
118         catch (CoreException e) {
119             DebugUIPlugin.log(e);
120         }
121     }
122     
123 }
124
Popular Tags