1 11 package org.eclipse.ant.internal.ui.launchConfigurations; 12 13 14 import java.util.ArrayList ; 15 import java.util.Iterator ; 16 import java.util.List ; 17 import org.eclipse.ant.internal.ui.AntUIImages; 18 import org.eclipse.ant.internal.ui.AntUIPlugin; 19 import org.eclipse.ant.internal.ui.IAntUIConstants; 20 import org.eclipse.ant.internal.ui.IAntUIHelpContextIds; 21 import org.eclipse.core.resources.IProject; 22 import org.eclipse.core.resources.IResource; 23 import org.eclipse.core.resources.IWorkspace; 24 import org.eclipse.core.resources.IWorkspaceRoot; 25 import org.eclipse.core.resources.ResourcesPlugin; 26 import org.eclipse.core.runtime.CoreException; 27 import org.eclipse.debug.core.ILaunchConfiguration; 28 import org.eclipse.debug.core.ILaunchConfigurationWorkingCopy; 29 import org.eclipse.debug.ui.AbstractLaunchConfigurationTab; 30 import org.eclipse.debug.ui.DebugUITools; 31 import org.eclipse.jface.viewers.IStructuredContentProvider; 32 import org.eclipse.jface.viewers.Viewer; 33 import org.eclipse.jface.window.Window; 34 import org.eclipse.swt.SWT; 35 import org.eclipse.swt.events.SelectionAdapter; 36 import org.eclipse.swt.events.SelectionEvent; 37 import org.eclipse.swt.graphics.Image; 38 import org.eclipse.swt.layout.GridData; 39 import org.eclipse.swt.layout.GridLayout; 40 import org.eclipse.swt.widgets.Button; 41 import org.eclipse.swt.widgets.Composite; 42 import org.eclipse.swt.widgets.Group; 43 import org.eclipse.ui.PlatformUI; 44 import org.eclipse.ui.dialogs.ListSelectionDialog; 45 import org.eclipse.ui.model.WorkbenchLabelProvider; 46 47 56 public class AntBuildTab extends AbstractLaunchConfigurationTab { 57 58 62 public static final String ATTR_BUILD_SCOPE = AntUIPlugin.getUniqueIdentifier() + ".ATTR_BUILD_SCOPE"; 64 69 public static final String ATTR_INCLUDE_REFERENCED_PROJECTS = AntUIPlugin.getUniqueIdentifier() + ".ATTR_INCLUDE_REFERENCED_PROJECTS"; 71 private Button fBuildButton; 73 74 private Group fGroup; 76 77 private Button fProjectButton; 79 private Button fSpecificProjectsButton; 80 private Button fWorkspaceButton; 81 82 private Button fSelectButton; 84 85 private Button fReferencedProjects; 87 88 private List fProjects = new ArrayList (); 90 91 class ProjectsContentProvider implements IStructuredContentProvider { 92 93 96 public Object [] getElements(Object inputElement) { 97 return ((IWorkspace)inputElement).getRoot().getProjects(); 98 } 99 100 103 public void dispose() { 104 } 105 106 109 public void inputChanged(Viewer viewer, Object oldInput, Object newInput) { 110 } 111 112 } 113 116 public void createControl(Composite parent) { 117 Composite mainComposite = new Composite(parent, SWT.NONE); 118 setControl(mainComposite); 119 PlatformUI.getWorkbench().getHelpSystem().setHelp(getControl(), IAntUIHelpContextIds.ANT_BUILD_TAB); 120 121 GridLayout layout = new GridLayout(); 122 GridData gd = new GridData(GridData.FILL_HORIZONTAL); 123 mainComposite.setLayout(layout); 124 mainComposite.setLayoutData(gd); 125 mainComposite.setFont(parent.getFont()); 126 127 fBuildButton = createCheckButton(mainComposite, AntLaunchConfigurationMessages.AntBuildTab_1); 128 fBuildButton.addSelectionListener(new SelectionAdapter() { 129 public void widgetSelected(SelectionEvent e) { 130 updateEnabledState(); 131 updateLaunchConfigurationDialog(); 132 } 133 }); 134 135 fGroup = new Group(mainComposite, SWT.NONE); 136 fGroup.setFont(mainComposite.getFont()); 137 layout = new GridLayout(); 138 layout.numColumns = 2; 139 layout.makeColumnsEqualWidth = false; 140 fGroup.setLayout(layout); 141 gd = new GridData(GridData.FILL_HORIZONTAL); 142 gd.horizontalSpan = 2; 143 fGroup.setLayoutData(gd); 144 145 SelectionAdapter adapter = new SelectionAdapter() { 146 public void widgetSelected(SelectionEvent e) { 147 if (((Button)e.getSource()).getSelection()) { 148 updateEnabledState(); 149 updateLaunchConfigurationDialog(); 150 } 151 } 152 }; 153 154 fWorkspaceButton = createRadioButton(fGroup, AntLaunchConfigurationMessages.AntBuildTab_2); 155 gd = new GridData(GridData.FILL_HORIZONTAL); 156 gd.horizontalSpan = 2; 157 fWorkspaceButton.setLayoutData(gd); 158 fWorkspaceButton.addSelectionListener(adapter); 159 160 fProjectButton = createRadioButton(fGroup, AntLaunchConfigurationMessages.AntBuildTab_3); 161 gd = new GridData(GridData.FILL_HORIZONTAL); 162 gd.horizontalSpan = 2; 163 fProjectButton.setLayoutData(gd); 164 fProjectButton.addSelectionListener(adapter); 165 166 fSpecificProjectsButton = createRadioButton(fGroup, AntLaunchConfigurationMessages.AntBuildTab_4); 167 gd = new GridData(GridData.FILL_HORIZONTAL); 168 gd.horizontalSpan = 1; 169 fSpecificProjectsButton.setLayoutData(gd); 170 fSpecificProjectsButton.addSelectionListener(adapter); 171 172 fSelectButton = createPushButton(fGroup, AntLaunchConfigurationMessages.AntBuildTab_5, null); 173 gd = (GridData)fSelectButton.getLayoutData(); 174 gd.horizontalAlignment = GridData.HORIZONTAL_ALIGN_END; 175 fSelectButton.addSelectionListener(new SelectionAdapter() { 176 public void widgetSelected(SelectionEvent e) { 177 selectResources(); 178 } 179 }); 180 181 createVerticalSpacer(mainComposite, 1); 182 fReferencedProjects = createCheckButton(mainComposite, AntLaunchConfigurationMessages.AntBuildTab_6); 183 } 184 185 188 private void selectResources() { 189 ListSelectionDialog dialog = new ListSelectionDialog(getShell(), ResourcesPlugin.getWorkspace(), new ProjectsContentProvider(), new WorkbenchLabelProvider(), AntLaunchConfigurationMessages.AntBuildTab_7); 190 dialog.setInitialElementSelections(fProjects); 191 if (dialog.open() == Window.CANCEL) { 192 return; 193 } 194 Object [] res = dialog.getResult(); 195 fProjects = new ArrayList (res.length); 196 for (int i = 0; i < res.length; i++) { 197 fProjects.add(res[i]); 198 } 199 updateLaunchConfigurationDialog(); 200 } 201 202 205 public void setDefaults(ILaunchConfigurationWorkingCopy configuration) { 206 } 207 208 211 public void initializeFrom(ILaunchConfiguration configuration) { 212 updateScope(configuration); 213 updateReferencedProjects(configuration); 214 updateEnabledState(); 215 } 216 217 private void updateReferencedProjects(ILaunchConfiguration configuration) { 218 boolean ref = false; 219 try { 220 ref = configuration.getAttribute(ATTR_INCLUDE_REFERENCED_PROJECTS, true); 221 } catch (CoreException e) { 222 AntUIPlugin.log(AntUIPlugin.newErrorStatus("Exception reading launch configuration", e)); } 224 fReferencedProjects.setSelection(ref); 225 } 226 227 230 private void updateScope(ILaunchConfiguration configuration) { 231 String scope = null; 232 try { 233 scope= configuration.getAttribute(ATTR_BUILD_SCOPE, (String )null); 234 } catch (CoreException ce) { 235 AntUIPlugin.log(AntUIPlugin.newErrorStatus("Exception reading launch configuration", ce)); } 237 fBuildButton.setSelection(scope != null); 238 fWorkspaceButton.setSelection(false); 239 fProjectButton.setSelection(false); 240 fSpecificProjectsButton.setSelection(false); 241 fProjects.clear(); 242 if (scope == null) { 243 fBuildButton.setSelection(true); 245 fWorkspaceButton.setSelection(true); 246 } else { 247 if (scope.equals("${none}")) { fBuildButton.setSelection(false); 249 } else if (scope.equals("${project}")) { fProjectButton.setSelection(true); 251 } else if (scope.startsWith("${projects:")) { fSpecificProjectsButton.setSelection(true); 253 IProject[] projects = getBuildProjects(scope); 254 fProjects = new ArrayList (projects.length); 255 for (int i = 0; i < projects.length; i++) { 256 fProjects.add(projects[i]); 257 } 258 } 259 } 260 } 261 264 public void performApply(ILaunchConfigurationWorkingCopy configuration) { 265 String scope = generateScopeMemento(); 266 configuration.setAttribute(ATTR_BUILD_SCOPE, scope); 267 if (fReferencedProjects.getSelection()) { 268 configuration.setAttribute(ATTR_INCLUDE_REFERENCED_PROJECTS, (String )null); 270 } else { 271 configuration.setAttribute(ATTR_INCLUDE_REFERENCED_PROJECTS, false); 272 } 273 } 274 275 278 private String generateScopeMemento() { 279 if (fBuildButton.getSelection()) { 280 if (fWorkspaceButton.getSelection()) { 281 return null; 282 } 283 if (fProjectButton.getSelection()) { 284 return "${project}"; } 286 if (fSpecificProjectsButton.getSelection()) { 287 return getBuildScopeAttribute(fProjects); 288 } 289 return null; 290 291 } 292 return "${none}"; } 294 295 298 public String getName() { 299 return AntLaunchConfigurationMessages.AntBuildTab_8; 300 } 301 302 305 private void updateEnabledState() { 306 boolean enabled= fBuildButton.getSelection(); 307 fGroup.setEnabled(enabled); 308 fWorkspaceButton.setEnabled(enabled); 309 fProjectButton.setEnabled(enabled); 310 fSpecificProjectsButton.setEnabled(enabled); 311 fSelectButton.setEnabled(enabled && fSpecificProjectsButton.getSelection()); 312 if (!enabled) { 313 super.setErrorMessage(null); 314 } 315 if (enabled) { 316 if (!fWorkspaceButton.getSelection() && !fProjectButton.getSelection() && 317 !fSpecificProjectsButton.getSelection()) { 318 fWorkspaceButton.setSelection(true); 319 } 320 } 321 fReferencedProjects.setEnabled(fBuildButton.getSelection() && (fProjectButton.getSelection() || fSpecificProjectsButton.getSelection())); 322 } 323 324 327 public Image getImage() { 328 return AntUIImages.getImage(IAntUIConstants.IMG_ANT_BUILD_TAB); 329 } 330 331 public boolean isValid(ILaunchConfiguration launchConfig) { 332 setErrorMessage(null); 333 setMessage(null); 334 if (fBuildButton.getSelection() && fSpecificProjectsButton.getSelection() && fProjects.isEmpty()) { 335 setErrorMessage(AntLaunchConfigurationMessages.AntBuildTab_9); 336 return false; 337 } 338 return true; 339 } 340 341 347 public static IProject[] getBuildProjects(String scope) { 348 if (scope.startsWith("${projects:")) { String pathString = scope.substring(11, scope.length() - 1); 350 if (pathString.length() > 1) { 351 String [] names = pathString.split(","); IProject[] projects = new IProject[names.length]; 353 IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot(); 354 for (int i = 0; i < names.length; i++) { 355 projects[i] = root.getProject(names[i]); 356 } 357 return projects; 358 } 359 } else if (scope.equals("${project}")) { IResource resource = DebugUITools.getSelectedResource(); 361 if (resource != null) { 362 return new IProject[]{resource.getProject()}; 363 } 364 } 365 return new IProject[0]; 366 } 367 368 376 public static String getBuildScope(ILaunchConfiguration configuration) throws CoreException { 377 return configuration.getAttribute(ATTR_BUILD_SCOPE, (String ) null); 378 } 379 380 388 public static boolean isIncludeReferencedProjects(ILaunchConfiguration configuration) throws CoreException { 389 return configuration.getAttribute(ATTR_INCLUDE_REFERENCED_PROJECTS, true); 390 } 391 392 399 public static String getBuildScopeAttribute(List projects) { 400 StringBuffer buf = new StringBuffer (); 401 buf.append("${projects:"); Iterator iterator = projects.iterator(); 403 while (iterator.hasNext()) { 404 IProject project = (IProject) iterator.next(); 405 buf.append(project.getName()); 406 if (iterator.hasNext()) { 407 buf.append(","); } 409 } 410 buf.append("}"); return buf.toString(); 412 } 413 414 417 public void activated(ILaunchConfigurationWorkingCopy workingCopy) { 418 } 420 421 424 public void deactivated(ILaunchConfigurationWorkingCopy workingCopy) { 425 } 427 } 428 | Popular Tags |