KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ant > internal > ui > launchConfigurations > AntBuildTab


1 /*******************************************************************************
2  * Copyright (c) 2000, 2005 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.ant.internal.ui.launchConfigurations;
12
13
14 import java.util.ArrayList JavaDoc;
15 import java.util.Iterator JavaDoc;
16 import java.util.List JavaDoc;
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 /**
48  * A launch configuration tab which allows the user to specify
49  * which resources should be built before the Ant build (a build scope)
50  * <p>
51  * This class may be instantiated; this class is not intended
52  * to be subclassed.
53  * </p>
54  * @since 3.0
55  */

56 public class AntBuildTab extends AbstractLaunchConfigurationTab {
57
58     /**
59      * String attribute identifying the build scope for this launch configuration.
60      * <code>null</code> indicates the default workspace build.
61      */

62     public static final String JavaDoc ATTR_BUILD_SCOPE = AntUIPlugin.getUniqueIdentifier() + ".ATTR_BUILD_SCOPE"; //$NON-NLS-1$
63

64     /**
65      * Attribute identifier specifying whether referenced projects should be
66      * considered when computing the projects to build. Default value is
67      * <code>true</code>.
68      */

69     public static final String JavaDoc ATTR_INCLUDE_REFERENCED_PROJECTS = AntUIPlugin.getUniqueIdentifier() + ".ATTR_INCLUDE_REFERENCED_PROJECTS"; //$NON-NLS-1$
70

71     // Check Buttons
72
private Button fBuildButton;
73     
74     // Group box
75
private Group fGroup;
76     
77     // Radio Buttons
78
private Button fProjectButton;
79     private Button fSpecificProjectsButton;
80     private Button fWorkspaceButton;
81     
82     // Push Button
83
private Button fSelectButton;
84     
85     // whether to include referenced projects
86
private Button fReferencedProjects;
87     
88     // projects to build (empty if none)
89
private List JavaDoc fProjects = new ArrayList JavaDoc();
90     
91     class ProjectsContentProvider implements IStructuredContentProvider {
92
93         /* (non-Javadoc)
94          * @see org.eclipse.jface.viewers.IStructuredContentProvider#getElements(java.lang.Object)
95          */

96         public Object JavaDoc[] getElements(Object JavaDoc inputElement) {
97             return ((IWorkspace)inputElement).getRoot().getProjects();
98         }
99
100         /* (non-Javadoc)
101          * @see org.eclipse.jface.viewers.IContentProvider#dispose()
102          */

103         public void dispose() {
104         }
105
106         /* (non-Javadoc)
107          * @see org.eclipse.jface.viewers.IContentProvider#inputChanged(org.eclipse.jface.viewers.Viewer, java.lang.Object, java.lang.Object)
108          */

109         public void inputChanged(Viewer viewer, Object JavaDoc oldInput, Object JavaDoc newInput) {
110         }
111         
112     }
113     /**
114      * @see org.eclipse.debug.ui.ILaunchConfigurationTab#createControl(org.eclipse.swt.widgets.Composite)
115      */

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     /**
186      * Prompts the user to select the projects to build.
187      */

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 JavaDoc[] res = dialog.getResult();
195         fProjects = new ArrayList JavaDoc(res.length);
196         for (int i = 0; i < res.length; i++) {
197             fProjects.add(res[i]);
198         }
199         updateLaunchConfigurationDialog();
200     }
201     
202     /**
203      * @see org.eclipse.debug.ui.ILaunchConfigurationTab#setDefaults(org.eclipse.debug.core.ILaunchConfigurationWorkingCopy)
204      */

205     public void setDefaults(ILaunchConfigurationWorkingCopy configuration) {
206     }
207
208     /**
209      * @see org.eclipse.debug.ui.ILaunchConfigurationTab#initializeFrom(org.eclipse.debug.core.ILaunchConfiguration)
210      */

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)); //$NON-NLS-1$
223
}
224         fReferencedProjects.setSelection(ref);
225     }
226
227     /**
228      * Updates the tab to display the build scope specified by the launch config
229      */

230     private void updateScope(ILaunchConfiguration configuration) {
231         String JavaDoc scope = null;
232         try {
233             scope= configuration.getAttribute(ATTR_BUILD_SCOPE, (String JavaDoc)null);
234         } catch (CoreException ce) {
235             AntUIPlugin.log(AntUIPlugin.newErrorStatus("Exception reading launch configuration", ce)); //$NON-NLS-1$
236
}
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             // select the workspace by default
244
fBuildButton.setSelection(true);
245             fWorkspaceButton.setSelection(true);
246         } else {
247             if (scope.equals("${none}")) { //$NON-NLS-1$
248
fBuildButton.setSelection(false);
249             } else if (scope.equals("${project}")) { //$NON-NLS-1$
250
fProjectButton.setSelection(true);
251             } else if (scope.startsWith("${projects:")) { //$NON-NLS-1$
252
fSpecificProjectsButton.setSelection(true);
253                 IProject[] projects = getBuildProjects(scope);
254                 fProjects = new ArrayList JavaDoc(projects.length);
255                 for (int i = 0; i < projects.length; i++) {
256                     fProjects.add(projects[i]);
257                 }
258             }
259         }
260     }
261     /**
262      * @see org.eclipse.debug.ui.ILaunchConfigurationTab#performApply(org.eclipse.debug.core.ILaunchConfigurationWorkingCopy)
263      */

264     public void performApply(ILaunchConfigurationWorkingCopy configuration) {
265         String JavaDoc scope = generateScopeMemento();
266         configuration.setAttribute(ATTR_BUILD_SCOPE, scope);
267         if (fReferencedProjects.getSelection()) {
268             // default is true
269
configuration.setAttribute(ATTR_INCLUDE_REFERENCED_PROJECTS, (String JavaDoc)null);
270         } else {
271             configuration.setAttribute(ATTR_INCLUDE_REFERENCED_PROJECTS, false);
272         }
273     }
274
275     /**
276      * Generates a memento for the build scope.
277      */

278     private String JavaDoc generateScopeMemento() {
279         if (fBuildButton.getSelection()) {
280             if (fWorkspaceButton.getSelection()) {
281                 return null;
282             }
283             if (fProjectButton.getSelection()) {
284                 return "${project}"; //$NON-NLS-1$
285
}
286             if (fSpecificProjectsButton.getSelection()) {
287                 return getBuildScopeAttribute(fProjects);
288             }
289             return null;
290             
291         }
292         return "${none}"; //$NON-NLS-1$
293
}
294
295     /**
296      * @see org.eclipse.debug.ui.ILaunchConfigurationTab#getName()
297      */

298     public String JavaDoc getName() {
299         return AntLaunchConfigurationMessages.AntBuildTab_8;
300     }
301     
302     /**
303      * Updates the enablement state of the fields.
304      */

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     /**
325      * @see org.eclipse.debug.ui.ILaunchConfigurationTab#getImage()
326      */

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     /**
342      * Returns a collection of projects referenced by a build scope attribute.
343      *
344      * @param scope build scope attribute (<code>ATTR_BUILD_SCOPE</code>)
345      * @return collection of porjects referred to by the scope attribute
346      */

347     public static IProject[] getBuildProjects(String JavaDoc scope) {
348         if (scope.startsWith("${projects:")) { //$NON-NLS-1$
349
String JavaDoc pathString = scope.substring(11, scope.length() - 1);
350             if (pathString.length() > 1) {
351                 String JavaDoc[] names = pathString.split(","); //$NON-NLS-1$
352
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}")) { //$NON-NLS-1$
360
IResource resource = DebugUITools.getSelectedResource();
361             if (resource != null) {
362                 return new IProject[]{resource.getProject()};
363             }
364         }
365         return new IProject[0];
366     }
367     
368     /**
369      * Returns the build scope attribute specified by the given launch configuration
370      * or <code>null</code> if none.
371      *
372      * @param configuration launch configuration
373      * @return build scope attribute (<code>ATTR_BUILD_SCOPE</code>)
374      * @throws CoreException if unable to access the associated attribute
375      */

376     public static String JavaDoc getBuildScope(ILaunchConfiguration configuration) throws CoreException {
377         return configuration.getAttribute(ATTR_BUILD_SCOPE, (String JavaDoc) null);
378     }
379     
380     /**
381      * Whether referenced projects should be considered when building. Only valid
382      * when a set of projects is to be built.
383      *
384      * @param configuration
385      * @return whether referenced projects should be considerd when building
386      * @throws CoreException if unable to access the associated attribute
387      */

388     public static boolean isIncludeReferencedProjects(ILaunchConfiguration configuration) throws CoreException {
389         return configuration.getAttribute(ATTR_INCLUDE_REFERENCED_PROJECTS, true);
390     }
391     
392     /**
393      * Creates and returns a memento for the given project set, to be used as a
394      * build scope attribute.
395      *
396      * @param projects list of projects
397      * @return an equivalent refresh attribute
398      */

399     public static String JavaDoc getBuildScopeAttribute(List JavaDoc projects) {
400         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
401         buf.append("${projects:"); //$NON-NLS-1$
402
Iterator JavaDoc 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(","); //$NON-NLS-1$
408
}
409         }
410         buf.append("}"); //$NON-NLS-1$
411
return buf.toString();
412     }
413     
414     /* (non-Javadoc)
415      * @see org.eclipse.debug.ui.ILaunchConfigurationTab#activated(org.eclipse.debug.core.ILaunchConfigurationWorkingCopy)
416      */

417     public void activated(ILaunchConfigurationWorkingCopy workingCopy) {
418         // do nothing on activation
419
}
420
421     /* (non-Javadoc)
422      * @see org.eclipse.debug.ui.ILaunchConfigurationTab#deactivated(org.eclipse.debug.core.ILaunchConfigurationWorkingCopy)
423      */

424     public void deactivated(ILaunchConfigurationWorkingCopy workingCopy) {
425         // do nothing on deactivation
426
}
427 }
428
Popular Tags