1 11 package org.eclipse.debug.internal.ui.actions; 12 13 14 import com.ibm.icu.text.MessageFormat; 15 import java.util.ArrayList ; 16 import java.util.List ; 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 37 public class AddToFavoritesAction extends SelectionListenerAction { 38 39 private ILaunchConfiguration fConfiguration = null; 40 private String fMode =null; 41 private ILaunchGroup fGroup = null; 42 43 46 public AddToFavoritesAction() { 47 super(""); setEnabled(false); 49 PlatformUI.getWorkbench().getHelpSystem().setHelp(this, IDebugHelpContextIds.EDIT_LAUNCH_CONFIGURATION_ACTION); 50 } 51 52 55 protected boolean updateSelection(IStructuredSelection selection) { 56 setLaunchConfiguration(null); 57 setMode(null); 58 setGroup(null); 59 if (selection.size() == 1) { 60 Object 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 []{DebugUIPlugin.removeAccelerators(getGroup().getLabel())})); 85 } 86 } 87 } 88 89 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 groups = config.getAttribute(IDebugUIConstants.ATTR_FAVORITE_GROUPS, (List )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 117 protected void setLaunchConfiguration(ILaunchConfiguration configuration) { 118 fConfiguration = configuration; 119 } 120 121 125 protected ILaunchConfiguration getLaunchConfiguration() { 126 return fConfiguration; 127 } 128 129 133 protected void setMode(String mode) { 134 fMode = mode; 135 } 136 137 141 protected String getMode() { 142 return fMode; 143 } 144 145 149 protected void setGroup(ILaunchGroup group) { 150 fGroup = group; 151 } 152 153 157 protected ILaunchGroup getGroup() { 158 return fGroup; 159 } 160 161 164 public void run() { 165 final CoreException[] ex = new CoreException[1]; 166 BusyIndicator.showWhile(DebugUIPlugin.getStandardDisplay(), new Runnable () { 167 public void run() { 168 try { 169 List list = getLaunchConfiguration().getAttribute(IDebugUIConstants.ATTR_FAVORITE_GROUPS, (List )null); 170 if (list == null) { 171 list = new ArrayList (); 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()); } 186 } 187 188 } 189 | Popular Tags |