KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > debug > internal > ui > actions > AddToFavoritesAction


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 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.actions;
12
13
14 import com.ibm.icu.text.MessageFormat;
15 import java.util.ArrayList JavaDoc;
16 import java.util.List JavaDoc;
17 import org.eclipse.core.runtime.CoreException;
18 import org.eclipse.core.runtime.IAdaptable;
19 import org.eclipse.debug.core.ILaunch;
20 import org.eclipse.debug.core.ILaunchConfiguration;
21 import org.eclipse.debug.core.ILaunchConfigurationWorkingCopy;
22 import org.eclipse.debug.core.model.IDebugElement;
23 import org.eclipse.debug.core.model.IProcess;
24 import org.eclipse.debug.internal.ui.DebugUIPlugin;
25 import org.eclipse.debug.internal.ui.IDebugHelpContextIds;
26 import org.eclipse.debug.ui.DebugUITools;
27 import org.eclipse.debug.ui.IDebugUIConstants;
28 import org.eclipse.debug.ui.ILaunchGroup;
29 import org.eclipse.jface.viewers.IStructuredSelection;
30 import org.eclipse.swt.custom.BusyIndicator;
31 import org.eclipse.ui.PlatformUI;
32 import org.eclipse.ui.actions.SelectionListenerAction;
33
34 /**
35  * Adds the selected launch configuration to the launch favorites.
36  */

37 public class AddToFavoritesAction extends SelectionListenerAction {
38     
39     private ILaunchConfiguration fConfiguration = null;
40     private String JavaDoc fMode =null;
41     private ILaunchGroup fGroup = null;
42
43     /**
44      * Constructs a new action.
45      */

46     public AddToFavoritesAction() {
47         super(""); //$NON-NLS-1$
48
setEnabled(false);
49         PlatformUI.getWorkbench().getHelpSystem().setHelp(this, IDebugHelpContextIds.EDIT_LAUNCH_CONFIGURATION_ACTION);
50     }
51
52     /**
53      * @see org.eclipse.ui.actions.SelectionListenerAction#updateSelection(org.eclipse.jface.viewers.IStructuredSelection)
54      */

55     protected boolean updateSelection(IStructuredSelection selection) {
56         setLaunchConfiguration(null);
57         setMode(null);
58         setGroup(null);
59         if (selection.size() == 1) {
60             Object JavaDoc object = selection.getFirstElement();
61             ILaunch launch = null;
62             if (object instanceof IAdaptable) {
63                 launch = (ILaunch)((IAdaptable)object).getAdapter(ILaunch.class);
64             }
65             if (launch == null) {
66                 if (object instanceof ILaunch) {
67                     launch = (ILaunch)object;
68                 } else if (object instanceof IDebugElement) {
69                     launch = ((IDebugElement)object).getLaunch();
70                 } else if (object instanceof IProcess) {
71                     launch = ((IProcess)object).getLaunch();
72                 }
73             }
74             if (launch != null) {
75                 ILaunchConfiguration configuration = launch.getLaunchConfiguration();
76                 if (configuration != null) {
77                     ILaunchGroup group= DebugUITools.getLaunchGroup(configuration, getMode());
78                     if (group == null) {
79                         return false;
80                     }
81                     setGroup(group);
82                     setLaunchConfiguration(configuration);
83                     setMode(launch.getLaunchMode());
84                     setText(MessageFormat.format(ActionMessages.AddToFavoritesAction_1, new String JavaDoc[]{DebugUIPlugin.removeAccelerators(getGroup().getLabel())}));
85                 }
86             }
87         }
88         
89         // Disable the action if the launch config is private
90
ILaunchConfiguration config = getLaunchConfiguration();
91         if (config == null) {
92             return false;
93         }
94         if (DebugUITools.isPrivate(config)) {
95                 return false;
96         }
97         
98         if (getGroup() != null) {
99             try {
100                 List JavaDoc groups = config.getAttribute(IDebugUIConstants.ATTR_FAVORITE_GROUPS, (List JavaDoc)null);
101                 if (groups != null) {
102                     return !groups.contains(getGroup().getIdentifier());
103                 }
104                 return true;
105             } catch (CoreException e) {
106             }
107             
108         }
109         
110         return false;
111     }
112
113     /**
114      * Allows the underlying <code>ILaunchConfiguration</code> to be set
115      * @param configuration the new configuration to set
116      */

117     protected void setLaunchConfiguration(ILaunchConfiguration configuration) {
118         fConfiguration = configuration;
119     }
120     
121     /**
122      * Returns the underlying <code>ILaunchConfiguration</code>
123      * @return the underlying <code>ILaunchConfiguration</code>
124      */

125     protected ILaunchConfiguration getLaunchConfiguration() {
126         return fConfiguration;
127     }
128     
129     /**
130      * Sets the mode this action applies to
131      * @param mode the modes to set
132      */

133     protected void setMode(String JavaDoc mode) {
134         fMode = mode;
135     }
136     
137     /**
138      * Returns the mode this action applies to
139      * @return
140      */

141     protected String JavaDoc getMode() {
142         return fMode;
143     }
144     
145     /**
146      * Sets the <code>ILaunchGroup</code> this action applies to
147      * @param group the new <code>ILaunchGroup</code>
148      */

149     protected void setGroup(ILaunchGroup group) {
150         fGroup = group;
151     }
152     
153     /**
154      * Returns the underlying <code>ILaunchGroup</code>
155      * @return the underlying <code>ILaunchGroup</code>
156      */

157     protected ILaunchGroup getGroup() {
158         return fGroup;
159     }
160     
161     /**
162      * @see org.eclipse.jface.action.IAction#run()
163      */

164     public void run() {
165         final CoreException[] ex = new CoreException[1];
166         BusyIndicator.showWhile(DebugUIPlugin.getStandardDisplay(), new Runnable JavaDoc() {
167             public void run() {
168                 try {
169                     List JavaDoc list = getLaunchConfiguration().getAttribute(IDebugUIConstants.ATTR_FAVORITE_GROUPS, (List JavaDoc)null);
170                     if (list == null) {
171                         list = new ArrayList JavaDoc();
172                     }
173                     list.add(getGroup().getIdentifier());
174                     ILaunchConfigurationWorkingCopy copy = getLaunchConfiguration().getWorkingCopy();
175                     copy.setAttribute(IDebugUIConstants.ATTR_FAVORITE_GROUPS, list);
176                     copy.doSave();
177                     setEnabled(false);
178                 } catch (CoreException e) {
179                     ex[0] = e;
180                 }
181             }
182         });
183         if (ex[0] != null) {
184             DebugUIPlugin.errorDialog(DebugUIPlugin.getShell(), ActionMessages.AddToFavoritesAction_2, ActionMessages.AddToFavoritesAction_3, ex[0].getStatus()); //
185
}
186     }
187
188 }
189
Popular Tags