KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2000, 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.ant.internal.ui.launchConfigurations;
12
13
14 import java.io.File JavaDoc;
15 import java.lang.reflect.InvocationTargetException JavaDoc;
16 import java.util.ArrayList JavaDoc;
17 import java.util.Iterator JavaDoc;
18 import java.util.List JavaDoc;
19
20 import org.eclipse.ant.internal.ui.AntUIImages;
21 import org.eclipse.ant.internal.ui.AntUIPlugin;
22 import org.eclipse.ant.internal.ui.AntUtil;
23 import org.eclipse.ant.internal.ui.IAntUIConstants;
24 import org.eclipse.ant.internal.ui.IAntUIHelpContextIds;
25 import org.eclipse.ant.internal.ui.model.AntElementNode;
26 import org.eclipse.ant.internal.ui.model.AntModelContentProvider;
27 import org.eclipse.ant.internal.ui.model.AntProjectNode;
28 import org.eclipse.ant.internal.ui.model.AntTargetNode;
29 import org.eclipse.ant.internal.ui.model.InternalTargetFilter;
30 import org.eclipse.core.resources.ResourcesPlugin;
31 import org.eclipse.core.runtime.CoreException;
32 import org.eclipse.core.runtime.IProgressMonitor;
33 import org.eclipse.core.runtime.IStatus;
34 import org.eclipse.core.runtime.jobs.ISchedulingRule;
35 import org.eclipse.core.variables.IStringVariableManager;
36 import org.eclipse.core.variables.VariablesPlugin;
37 import org.eclipse.debug.core.ILaunchConfiguration;
38 import org.eclipse.debug.core.ILaunchConfigurationWorkingCopy;
39 import org.eclipse.debug.ui.AbstractLaunchConfigurationTab;
40 import org.eclipse.jface.dialogs.Dialog;
41 import org.eclipse.jface.dialogs.IDialogConstants;
42 import org.eclipse.jface.operation.IRunnableContext;
43 import org.eclipse.jface.operation.IRunnableWithProgress;
44 import org.eclipse.jface.viewers.CheckStateChangedEvent;
45 import org.eclipse.jface.viewers.CheckboxTableViewer;
46 import org.eclipse.jface.viewers.ColumnWeightData;
47 import org.eclipse.jface.viewers.DoubleClickEvent;
48 import org.eclipse.jface.viewers.ICheckStateListener;
49 import org.eclipse.jface.viewers.IDoubleClickListener;
50 import org.eclipse.jface.viewers.ISelection;
51 import org.eclipse.jface.viewers.IStructuredSelection;
52 import org.eclipse.jface.viewers.TableLayout;
53 import org.eclipse.jface.viewers.Viewer;
54 import org.eclipse.jface.viewers.ViewerComparator;
55 import org.eclipse.jface.viewers.ViewerFilter;
56 import org.eclipse.jface.window.Window;
57 import org.eclipse.swt.SWT;
58 import org.eclipse.swt.events.SelectionAdapter;
59 import org.eclipse.swt.events.SelectionEvent;
60 import org.eclipse.swt.events.ShellAdapter;
61 import org.eclipse.swt.events.ShellEvent;
62 import org.eclipse.swt.graphics.Font;
63 import org.eclipse.swt.graphics.Image;
64 import org.eclipse.swt.layout.GridData;
65 import org.eclipse.swt.layout.GridLayout;
66 import org.eclipse.swt.widgets.Button;
67 import org.eclipse.swt.widgets.Composite;
68 import org.eclipse.swt.widgets.Label;
69 import org.eclipse.swt.widgets.Table;
70 import org.eclipse.swt.widgets.TableColumn;
71 import org.eclipse.swt.widgets.Text;
72 import org.eclipse.ui.PlatformUI;
73 import org.eclipse.ui.externaltools.internal.model.IExternalToolConstants;
74
75 import com.ibm.icu.text.MessageFormat;
76
77 /**
78  * Launch configuration tab which allows the user to choose the targets
79  * from an Ant buildfile that will be executed when the configuration is
80  * launched.
81  */

82 public class AntTargetsTab extends AbstractLaunchConfigurationTab {
83     
84     private AntTargetNode fDefaultTarget = null;
85     private AntTargetNode[] fAllTargets= null;
86     private List JavaDoc fOrderedTargets = null;
87     
88     private CheckboxTableViewer fTableViewer = null;
89     private Label fSelectionCountLabel = null;
90     private Text fTargetOrderText = null;
91     private Button fOrderButton = null;
92     private Button fFilterInternalTargets;
93     private InternalTargetFilter fInternalTargetFilter= null;
94     private Button fSortButton;
95     
96     private ILaunchConfiguration fLaunchConfiguration;
97     private int fSortDirection= 0;
98     private boolean fInitializing= false;
99     
100     /**
101      * Sort direction constants.
102      */

103     public final static int SORT_NONE= 0;
104     public final static int SORT_NAME= 1;
105     public final static int SORT_NAME_REVERSE= -1;
106     public final static int SORT_DESCRIPTION= 2;
107     public final static int SORT_DESCRIPTION_REVERSE= -2;
108     
109     /**
110      * A comparator which can sort targets by name or description, in
111      * forward or reverse order.
112      */

113     private class AntTargetsComparator extends ViewerComparator {
114         /* (non-Javadoc)
115          * @see org.eclipse.jface.viewers.ViewerComparator#compare(org.eclipse.jface.viewers.Viewer, java.lang.Object, java.lang.Object)
116          */

117         public int compare(Viewer viewer, Object JavaDoc e1, Object JavaDoc e2) {
118             if (!(e1 instanceof AntTargetNode && e2 instanceof AntTargetNode)) {
119                 return super.compare(viewer, e1, e2);
120             }
121             if (fSortDirection == SORT_NONE) {
122                 return 0;
123             }
124             String JavaDoc string1, string2;
125             int result= 0;
126             if (fSortDirection == SORT_NAME || fSortDirection == SORT_NAME_REVERSE) {
127                 string1= ((AntTargetNode) e1).getLabel();
128                 string2= ((AntTargetNode) e2).getLabel();
129             } else {
130                 string1= ((AntTargetNode) e1).getTarget().getDescription();
131                 string2= ((AntTargetNode) e2).getTarget().getDescription();
132             }
133             if (string1 != null && string2 != null) {
134                 result= getComparator().compare(string1, string2);
135             } else if (string1 == null) {
136                 result= 1;
137             } else if (string2 == null) {
138                 result= -1;
139             }
140             if (fSortDirection < 0) { // reverse sort
141
if (result == 0) {
142                     result= -1;
143                 } else {
144                     result= -result;
145                 }
146             }
147             return result;
148         }
149     }
150     
151     /* (non-Javadoc)
152      * @see org.eclipse.debug.ui.ILaunchConfigurationTab#createControl(org.eclipse.swt.widgets.Composite)
153      */

154     public void createControl(Composite parent) {
155         Font font = parent.getFont();
156         
157         Composite comp = new Composite(parent, SWT.NONE);
158         setControl(comp);
159         PlatformUI.getWorkbench().getHelpSystem().setHelp(getControl(), IAntUIHelpContextIds.ANT_TARGETS_TAB);
160         GridLayout topLayout = new GridLayout();
161         comp.setLayout(topLayout);
162         GridData gd = new GridData(GridData.FILL_BOTH);
163         comp.setLayoutData(gd);
164         comp.setFont(font);
165         
166         createTargetsTable(comp);
167         createSelectionCount(comp);
168         
169         Composite buttonComposite= new Composite(comp, SWT.NONE);
170         GridLayout layout= new GridLayout();
171         layout.verticalSpacing = 0;
172         layout.marginHeight = 0;
173         layout.marginWidth = 0;
174         buttonComposite.setLayout(layout);
175         buttonComposite.setFont(font);
176         
177         createSortTargets(buttonComposite);
178         createFilterInternalTargets(buttonComposite);
179         
180         createVerticalSpacer(comp, 1);
181         createTargetOrder(comp);
182         Dialog.applyDialogFont(parent);
183     }
184     
185     /**
186      * Creates the selection count widget
187      * @param parent the parent composite
188      */

189     private void createSelectionCount(Composite parent) {
190         fSelectionCountLabel = new Label(parent, SWT.NONE);
191         fSelectionCountLabel.setFont(parent.getFont());
192         fSelectionCountLabel.setText(AntLaunchConfigurationMessages.AntTargetsTab_0_out_of_0_selected_2);
193         GridData gd = new GridData(GridData.FILL_HORIZONTAL);
194         fSelectionCountLabel.setLayoutData(gd);
195     }
196
197     /**
198      * Creates the widgets that display the target order
199      * @param parent the parent composite
200      */

201     private void createTargetOrder(Composite parent) {
202         Font font= parent.getFont();
203         
204         Label label = new Label(parent, SWT.NONE);
205         label.setText(AntLaunchConfigurationMessages.AntTargetsTab_Target_execution_order__3);
206         label.setFont(font);
207         
208         Composite orderComposite = new Composite(parent, SWT.NONE);
209         GridData gd = new GridData(GridData.FILL_HORIZONTAL);
210         orderComposite.setLayoutData(gd);
211         GridLayout layout = new GridLayout(2, false);
212         layout.marginHeight = 0;
213         layout.marginWidth = 0;
214         orderComposite.setLayout(layout);
215         orderComposite.setFont(font);
216                 
217         fTargetOrderText = new Text(orderComposite, SWT.MULTI | SWT.WRAP | SWT.BORDER | SWT.V_SCROLL | SWT.READ_ONLY);
218         fTargetOrderText.setFont(font);
219         gd = new GridData(GridData.FILL_HORIZONTAL);
220         gd.heightHint = 40;
221         gd.widthHint = IDialogConstants.ENTRY_FIELD_WIDTH;
222         fTargetOrderText.setLayoutData(gd);
223
224         fOrderButton = createPushButton(orderComposite, AntLaunchConfigurationMessages.AntTargetsTab__Order____4, null);
225         gd = (GridData)fOrderButton.getLayoutData();
226         gd.verticalAlignment = GridData.BEGINNING;
227         fOrderButton.setFont(font);
228         fOrderButton.addSelectionListener(new SelectionAdapter() {
229             public void widgetSelected(SelectionEvent e) {
230                 handleOrderPressed();
231             }
232         });
233     }
234
235     /**
236      * Creates the toggle to filter internal targets from the table
237      * @param parent the parent composite
238      */

239     private void createFilterInternalTargets(Composite parent) {
240         fFilterInternalTargets= createCheckButton(parent, AntLaunchConfigurationMessages.AntTargetsTab_12);
241         fFilterInternalTargets.addSelectionListener(new SelectionAdapter() {
242             public void widgetSelected(SelectionEvent e) {
243                 handleFilterTargetsSelected();
244             }
245         });
246     }
247     
248     /**
249      * Creates the toggle to sort targets in the table
250      * @param parent the parent composite
251      */

252     private void createSortTargets(Composite parent) {
253         fSortButton= createCheckButton(parent, AntLaunchConfigurationMessages.AntTargetsTab_14);
254         fSortButton.addSelectionListener(new SelectionAdapter() {
255             public void widgetSelected(SelectionEvent e) {
256                 handleSortTargetsSelected();
257             }
258         });
259     }
260     
261     /**
262      * The filter targets button has been toggled. If it's been
263      * turned on, filter out internal targets. Else, restore internal
264      * targets to the table.
265      */

266     private void handleFilterTargetsSelected() {
267         boolean filter= fFilterInternalTargets.getSelection();
268         if (filter) {
269             fTableViewer.addFilter(getInternalTargetsFilter());
270         } else {
271             fTableViewer.removeFilter(getInternalTargetsFilter());
272         }
273         
274         // Must refresh before updating selection count because the selection
275
// count's "hidden" reporting needs the content provider to be queried
276
// first to count how many targets are hidden.
277
updateSelectionCount();
278         if (!fInitializing) {
279             updateLaunchConfigurationDialog();
280         }
281     }
282     
283     private ViewerFilter getInternalTargetsFilter() {
284         if (fInternalTargetFilter == null) {
285             fInternalTargetFilter= new InternalTargetFilter();
286         }
287         return fInternalTargetFilter;
288     }
289     
290     /**
291      * The button to sort targets has been toggled.
292      * Set the tab's sorting as appropriate.
293      */

294     private void handleSortTargetsSelected() {
295         setSort(fSortButton.getSelection() ? SORT_NAME : SORT_NONE);
296     }
297     
298     /**
299      * Sets the sorting of targets in this tab. See the sort constants defined
300      * above.
301      *
302      * @param column the column which should be sorted on
303      */

304     private void setSort(int column) {
305         fSortDirection= column;
306         fTableViewer.refresh();
307         if (!fInitializing) {
308             updateLaunchConfigurationDialog();
309         }
310     }
311
312     /**
313      * The target order button has been pressed. Prompt the
314      * user to reorder the selected targets.
315      */

316     private void handleOrderPressed() {
317         TargetOrderDialog dialog = new TargetOrderDialog(getShell(), fOrderedTargets.toArray());
318         int ok = dialog.open();
319         if (ok == Window.OK) {
320             fOrderedTargets.clear();
321             Object JavaDoc[] targets = dialog.getTargets();
322             for (int i = 0; i < targets.length; i++) {
323                 fOrderedTargets.add(targets[i]);
324                 updateSelectionCount();
325                 updateLaunchConfigurationDialog();
326             }
327         }
328     }
329     
330     /**
331      * Creates the table which displays the available targets
332      * @param parent the parent composite
333      */

334     private void createTargetsTable(Composite parent) {
335         Font font= parent.getFont();
336         Label label = new Label(parent, SWT.NONE);
337         label.setFont(font);
338         label.setText(AntLaunchConfigurationMessages.AntTargetsTab_Check_targets_to_e_xecute__1);
339                 
340         final Table table= new Table(parent, SWT.CHECK | SWT.BORDER | SWT.FULL_SELECTION | SWT.RESIZE);
341         
342         GridData data= new GridData(GridData.FILL_BOTH);
343         int availableRows= availableRows(parent);
344         data.heightHint = table.getItemHeight() * (availableRows / 20);
345         data.widthHint= 250;
346         table.setLayoutData(data);
347         table.setFont(font);
348                 
349         table.setHeaderVisible(true);
350         table.setLinesVisible(true);
351
352         TableLayout tableLayout= new TableLayout();
353         ColumnWeightData weightData = new ColumnWeightData(30, true);
354         tableLayout.addColumnData(weightData);
355         weightData = new ColumnWeightData(70, true);
356         tableLayout.addColumnData(weightData);
357         table.setLayout(tableLayout);
358
359         final TableColumn column1= new TableColumn(table, SWT.NULL);
360         column1.setText(AntLaunchConfigurationMessages.AntTargetsTab_Name_5);
361             
362         final TableColumn column2= new TableColumn(table, SWT.NULL);
363         column2.setText(AntLaunchConfigurationMessages.AntTargetsTab_Description_6);
364         
365
366         //TableLayout only sizes columns once. If showing the targets
367
//tab as the initial tab, the dialog isn't open when the layout
368
//occurs and the column size isn't computed correctly. Need to
369
//recompute the size of the columns once all the parent controls
370
//have been created/sized.
371
//HACK Bug 139190
372
getShell().addShellListener(new ShellAdapter() {
373             public void shellActivated(ShellEvent e) {
374                 if(!table.isDisposed()) {
375                     int tableWidth = table.getSize().x;
376                     if (tableWidth > 0) {
377                         int c1 = tableWidth / 3;
378                         column1.setWidth(c1);
379                         column2.setWidth(tableWidth - c1);
380                     }
381                     getShell().removeShellListener(this);
382                 }
383             }
384         });
385         
386         fTableViewer = new CheckboxTableViewer(table);
387         fTableViewer.setLabelProvider(new TargetTableLabelProvider());
388         fTableViewer.setContentProvider(new AntModelContentProvider());
389         fTableViewer.setComparator(new AntTargetsComparator());
390         
391         fTableViewer.addDoubleClickListener(new IDoubleClickListener() {
392             public void doubleClick(DoubleClickEvent event) {
393                 ISelection selection= event.getSelection();
394                 if (!selection.isEmpty() && selection instanceof IStructuredSelection) {
395                     IStructuredSelection ss= (IStructuredSelection)selection;
396                     Object JavaDoc element= ss.getFirstElement();
397                     boolean checked= !fTableViewer.getChecked(element);
398                     fTableViewer.setChecked(element, checked);
399                     updateOrderedTargets(element, checked);
400                 }
401             }
402         });
403         
404         fTableViewer.addCheckStateListener(new ICheckStateListener() {
405             public void checkStateChanged(CheckStateChangedEvent event) {
406                 updateOrderedTargets(event.getElement(), event.getChecked());
407             }
408         });
409         
410         TableColumn[] columns= fTableViewer.getTable().getColumns();
411         for (int i = 0; i < columns.length; i++) {
412             final int index= i;
413             columns[index].addSelectionListener(new SelectionAdapter() {
414                 public void widgetSelected(SelectionEvent e) {
415                     if (fSortButton.getSelection()) {
416                         // index 0 => sort_name (1)
417
// index 1 => sort_description (2)
418
int column= index + 1;
419                         if (column == fSortDirection) {
420                             column= -column; // invert the sort when the same column is selected twice in a row
421
}
422                         setSort(column);
423                     }
424                 }
425             });
426         }
427     }
428     
429     /**
430      * Return the number of rows available in the current display using the
431      * current font.
432      * @param parent The Composite whose Font will be queried.
433      * @return int The result of the display size divided by the font size.
434      */

435     private int availableRows(Composite parent) {
436
437         int fontHeight = (parent.getFont().getFontData())[0].getHeight();
438         int displayHeight = parent.getDisplay().getClientArea().height;
439
440         return displayHeight / fontHeight;
441     }
442     
443     /**
444      * Updates the ordered targets list in response to an element being checked
445      * or unchecked. When the element is checked, it's added to the list. When
446      * unchecked, it's removed.
447      *
448      * @param element the element in question
449      * @param checked whether the element has been checked or unchecked
450      */

451     private void updateOrderedTargets(Object JavaDoc element , boolean checked) {
452         if (checked) {
453              fOrderedTargets.add(element);
454         } else {
455             fOrderedTargets.remove(element);
456         }
457         updateSelectionCount();
458         updateLaunchConfigurationDialog();
459     }
460     
461     /**
462      * Updates the selection count widget to display how many targets are
463      * selected (example, "1 out of 6 selected") and filtered.
464      */

465     private void updateSelectionCount() {
466         Object JavaDoc[] checked = fTableViewer.getCheckedElements();
467         String JavaDoc numSelected = Integer.toString(checked.length);
468     
469         int all= fAllTargets == null ? 0 : fAllTargets.length;
470         int visible= fTableViewer.getTable().getItemCount();
471         String JavaDoc total = Integer.toString(visible);
472         int numHidden= all - visible;
473         if (numHidden > 0) {
474             fSelectionCountLabel.setText(MessageFormat.format(AntLaunchConfigurationMessages.AntTargetsTab_13, new String JavaDoc[]{numSelected, String.valueOf(all), String.valueOf(numHidden)}));
475         } else {
476             fSelectionCountLabel.setText(MessageFormat.format(AntLaunchConfigurationMessages.AntTargetsTab__0__out_of__1__selected_7, new String JavaDoc[]{numSelected, total}));
477         }
478         
479         fOrderButton.setEnabled(checked.length > 1);
480         
481         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
482         Iterator JavaDoc iter = fOrderedTargets.iterator();
483         while (iter.hasNext()) {
484             buffer.append(((AntTargetNode)iter.next()).getTargetName());
485             buffer.append(", "); //$NON-NLS-1$
486
}
487         if (buffer.length() > 2) {
488             // remove trailing comma
489
buffer.setLength(buffer.length() - 2);
490         }
491         fTargetOrderText.setText(buffer.toString());
492     }
493     
494     /**
495      * Returns all targets in the buildfile.
496      * @return all targets in the buildfile
497      */

498     private AntTargetNode[] getTargets() {
499         if (fAllTargets == null || isDirty()) {
500             fAllTargets= null;
501             fDefaultTarget= null;
502             setDirty(false);
503             setErrorMessage(null);
504             setMessage(null);
505             
506             final String JavaDoc expandedLocation= validateLocation();
507             if (expandedLocation == null) {
508                 return fAllTargets;
509             }
510             final CoreException[] exceptions= new CoreException[1];
511             try {
512                 IRunnableWithProgress operation= new IRunnableWithProgress() {
513                     /* (non-Javadoc)
514                      * @see org.eclipse.jface.operation.IRunnableWithProgress#run(org.eclipse.core.runtime.IProgressMonitor)
515                      */

516                     public void run(IProgressMonitor monitor) {
517                         try {
518                             fAllTargets = AntUtil.getTargets(expandedLocation, fLaunchConfiguration);
519                         } catch (CoreException ce) {
520                             exceptions[0]= ce;
521                         }
522                     }
523                 };
524                 
525                 IRunnableContext context= PlatformUI.getWorkbench().getActiveWorkbenchWindow();
526                 if (context == null) {
527                     context= getLaunchConfigurationDialog();
528                 }
529
530                 ISchedulingRule rule= null;
531                 if (!ResourcesPlugin.getWorkspace().isTreeLocked()) {
532                     //only set a scheduling rule if not in a resource change callback
533
rule= AntUtil.getFileForLocation(expandedLocation, null);
534                 }
535                 PlatformUI.getWorkbench().getProgressService().runInUI(context, operation, rule);
536             } catch (InvocationTargetException JavaDoc e) {
537                 AntUIPlugin.log("Internal error occurred retrieving targets", e.getTargetException()); //$NON-NLS-1$
538
setErrorMessage(AntLaunchConfigurationMessages.AntTargetsTab_1);
539                 fAllTargets= null;
540                 return null;
541             } catch (InterruptedException JavaDoc e) {
542                 AntUIPlugin.log("Internal error occurred retrieving targets", e); //$NON-NLS-1$
543
setErrorMessage(AntLaunchConfigurationMessages.AntTargetsTab_1);
544                 fAllTargets= null;
545                 return null;
546             }
547             
548             if (exceptions[0] != null) {
549                 IStatus exceptionStatus= exceptions[0].getStatus();
550                 IStatus[] children= exceptionStatus.getChildren();
551                 StringBuffer JavaDoc message= new StringBuffer JavaDoc(exceptions[0].getMessage());
552                 for (int i = 0; i < children.length; i++) {
553                     message.append(' ');
554                     IStatus childStatus = children[i];
555                     message.append(childStatus.getMessage());
556                 }
557                 setErrorMessage(message.toString());
558                 fAllTargets= null;
559                 return fAllTargets;
560             }
561             
562             if (fAllTargets == null) {
563                 //if an error was not thrown during parsing then having no targets is valid (Ant 1.6.*)
564
return fAllTargets;
565             }
566             
567             AntTargetNode target= fAllTargets[0];
568             AntProjectNode projectNode= target.getProjectNode();
569             setErrorMessageFromNode(projectNode);
570             for (int i=0; i < fAllTargets.length; i++) {
571                 target= fAllTargets[i];
572                 if (target.isDefaultTarget()) {
573                     fDefaultTarget= target;
574                 }
575                 setErrorMessageFromNode(target);
576             }
577         }
578         
579         return fAllTargets;
580     }
581     
582     private void setErrorMessageFromNode(AntElementNode node) {
583         if (getErrorMessage() != null) {
584             return;
585         }
586         if (node.isErrorNode() || node.isWarningNode()) {
587             String JavaDoc message= node.getProblemMessage();
588             if (message != null) {
589                 setErrorMessage(message);
590             } else {
591                 setErrorMessage(AntLaunchConfigurationMessages.AntTargetsTab_0);
592             }
593         }
594     }
595
596     /* (non-Javadoc)
597      * @see org.eclipse.debug.ui.ILaunchConfigurationTab#setDefaults(org.eclipse.debug.core.ILaunchConfigurationWorkingCopy)
598      */

599     public void setDefaults(ILaunchConfigurationWorkingCopy configuration) {
600     }
601
602     /* (non-Javadoc)
603      * @see org.eclipse.debug.ui.ILaunchConfigurationTab#initializeFrom(org.eclipse.debug.core.ILaunchConfiguration)
604      */

605     public void initializeFrom(ILaunchConfiguration configuration) {
606         fInitializing= true;
607         fLaunchConfiguration= configuration;
608         fOrderedTargets = new ArrayList JavaDoc();
609         setErrorMessage(null);
610         setMessage(null);
611         setDirty(true);
612         boolean hideInternal= false;
613         try {
614             hideInternal = fLaunchConfiguration.getAttribute(IAntLaunchConfigurationConstants.ATTR_HIDE_INTERNAL_TARGETS, false);
615         } catch (CoreException e) {
616             AntUIPlugin.log(e);
617         }
618         fFilterInternalTargets.setSelection(hideInternal);
619         handleFilterTargetsSelected();
620         int sort= SORT_NONE;
621         try {
622             sort = fLaunchConfiguration.getAttribute(IAntLaunchConfigurationConstants.ATTR_SORT_TARGETS, sort);
623         } catch (CoreException e) {
624             AntUIPlugin.log(e);
625         }
626         fSortButton.setSelection(sort != SORT_NONE);
627         setSort(sort);
628         String JavaDoc configTargets= null;
629         String JavaDoc newLocation= null;
630         
631         try {
632             configTargets= configuration.getAttribute(IAntLaunchConfigurationConstants.ATTR_ANT_TARGETS, (String JavaDoc)null);
633             newLocation= configuration.getAttribute(IExternalToolConstants.ATTR_LOCATION, (String JavaDoc)null);
634         } catch (CoreException ce) {
635             AntUIPlugin.log(AntLaunchConfigurationMessages.AntTargetsTab_Error_reading_configuration_12, ce);
636         }
637         
638         if (newLocation == null) {
639             fAllTargets= null;
640             initializeForNoTargets();
641             return;
642         }
643         
644         AntTargetNode[] allTargetNodes= getTargets();
645         if (allTargetNodes == null) {
646             initializeForNoTargets();
647             return;
648         }
649         
650         String JavaDoc[] targetNames= AntUtil.parseRunTargets(configTargets);
651         if (targetNames.length == 0) {
652             fTableViewer.setAllChecked(false);
653             setExecuteInput(allTargetNodes);
654             if (fDefaultTarget != null) {
655                 fOrderedTargets.add(fDefaultTarget);
656                 fTableViewer.setChecked(fDefaultTarget, true);
657                 updateSelectionCount();
658                 updateLaunchConfigurationDialog();
659             }
660             fInitializing= false;
661             return;
662         }
663         
664         setExecuteInput(allTargetNodes);
665         fTableViewer.setAllChecked(false);
666         for (int i = 0; i < targetNames.length; i++) {
667             for (int j = 0; j < fAllTargets.length; j++) {
668                 if (targetNames[i].equals(fAllTargets[j].getTargetName())) {
669                     fOrderedTargets.add(fAllTargets[j]);
670                     fTableViewer.setChecked(fAllTargets[j], true);
671                 }
672             }
673         }
674         updateSelectionCount();
675         fInitializing= false;
676     }
677     
678     private void initializeForNoTargets() {
679         setExecuteInput(new AntTargetNode[0]);
680         fTableViewer.setInput(new AntTargetNode[0]);
681         fInitializing= false;
682     }
683
684     /**
685      * Sets the execute table's input to the given input.
686      */

687     private void setExecuteInput(Object JavaDoc input) {
688         fTableViewer.setInput(input);
689         updateSelectionCount();
690     }
691
692     /* (non-Javadoc)
693      * @see org.eclipse.debug.ui.ILaunchConfigurationTab#performApply(org.eclipse.debug.core.ILaunchConfigurationWorkingCopy)
694      */

695     public void performApply(ILaunchConfigurationWorkingCopy configuration) {
696         // attribute added in 3.0, so null must be used instead of false for backwards compatibility
697
if (fFilterInternalTargets.getSelection()) {
698             configuration.setAttribute(IAntLaunchConfigurationConstants.ATTR_HIDE_INTERNAL_TARGETS, true);
699         } else {
700             configuration.setAttribute(IAntLaunchConfigurationConstants.ATTR_HIDE_INTERNAL_TARGETS, (String JavaDoc)null);
701         }
702         //attribute added in 3.0, so null must be used instead of 0 for backwards compatibility
703
if (fSortDirection != SORT_NONE) {
704             configuration.setAttribute(IAntLaunchConfigurationConstants.ATTR_SORT_TARGETS, fSortDirection);
705         } else {
706             configuration.setAttribute(IAntLaunchConfigurationConstants.ATTR_SORT_TARGETS, (String JavaDoc)null);
707         }
708         
709         if (fOrderedTargets.size() == 1) {
710             AntTargetNode item = (AntTargetNode)fOrderedTargets.get(0);
711             if (item.isDefaultTarget()) {
712                 configuration.setAttribute(IAntLaunchConfigurationConstants.ATTR_ANT_TARGETS, (String JavaDoc)null);
713                 return;
714             }
715         } else if (fOrderedTargets.size() == 0) {
716             configuration.setAttribute(IAntLaunchConfigurationConstants.ATTR_ANT_TARGETS, (String JavaDoc)null);
717             return;
718         }
719         
720         StringBuffer JavaDoc buff= new StringBuffer JavaDoc();
721         Iterator JavaDoc iter = fOrderedTargets.iterator();
722         String JavaDoc targets = null;
723         while (iter.hasNext()) {
724             AntTargetNode item = (AntTargetNode)iter.next();
725             buff.append(item.getTargetName());
726             buff.append(',');
727         }
728         if (buff.length() > 0) {
729             targets= buff.toString();
730         }
731
732         configuration.setAttribute(IAntLaunchConfigurationConstants.ATTR_ANT_TARGETS, targets);
733     }
734
735     /* (non-Javadoc)
736      * @see org.eclipse.debug.ui.ILaunchConfigurationTab#getName()
737      */

738     public String JavaDoc getName() {
739         return AntLaunchConfigurationMessages.AntTargetsTab_Tar_gets_14;
740     }
741         
742     /* (non-Javadoc)
743      * @see org.eclipse.debug.ui.ILaunchConfigurationTab#getImage()
744      */

745     public Image getImage() {
746         return AntUIImages.getImage(IAntUIConstants.IMG_TAB_ANT_TARGETS);
747     }
748
749     /* (non-Javadoc)
750      * @see org.eclipse.debug.ui.ILaunchConfigurationTab#isValid(org.eclipse.debug.core.ILaunchConfiguration)
751      */

752     public boolean isValid(ILaunchConfiguration launchConfig) {
753         if (fAllTargets == null || isDirty()) {
754             if (getErrorMessage() != null && !isDirty()) {
755                 //error in parsing;
756
return false;
757             }
758             //targets not up to date and no error message...we have not parsed recently
759
initializeFrom(launchConfig);
760             if (getErrorMessage() != null) {
761                 //error in parsing;
762
return false;
763             }
764         }
765         
766         setErrorMessage(null);
767         return super.isValid(launchConfig);
768     }
769     
770     /* (non-Javadoc)
771      * @see org.eclipse.debug.ui.AbstractLaunchConfigurationTab#setDirty(boolean)
772      */

773     protected void setDirty(boolean dirty) {
774         //provide package visibility
775
super.setDirty(dirty);
776     }
777     
778     /* (non-Javadoc)
779      * @see org.eclipse.debug.ui.ILaunchConfigurationTab#activated(org.eclipse.debug.core.ILaunchConfigurationWorkingCopy)
780      */

781     public void activated(ILaunchConfigurationWorkingCopy workingCopy) {
782         if (isDirty()) {
783             super.activated(workingCopy);
784         }
785     }
786     
787     /* (non-Javadoc)
788      * @see org.eclipse.debug.ui.ILaunchConfigurationTab#deactivated(org.eclipse.debug.core.ILaunchConfigurationWorkingCopy)
789      */

790     public void deactivated(ILaunchConfigurationWorkingCopy workingCopy) {
791         if (fOrderedTargets.size() == 0) {
792             //set the dirty flag so that the state will be reinitialized on activation
793
setDirty(true);
794         }
795     }
796     
797     private String JavaDoc validateLocation() {
798         String JavaDoc expandedLocation= null;
799         String JavaDoc location= null;
800         IStringVariableManager manager = VariablesPlugin.getDefault().getStringVariableManager();
801         try {
802             location= fLaunchConfiguration.getAttribute(IExternalToolConstants.ATTR_LOCATION, (String JavaDoc)null);
803             if (location == null) {
804                 return null;
805             }
806             
807             expandedLocation= manager.performStringSubstitution(location);
808             if (expandedLocation == null) {
809                 return null;
810             }
811             File JavaDoc file = new File JavaDoc(expandedLocation);
812             if (!file.exists()) {
813                 setErrorMessage(AntLaunchConfigurationMessages.AntTargetsTab_15);
814                 return null;
815             }
816             if (!file.isFile()) {
817                 setErrorMessage(AntLaunchConfigurationMessages.AntTargetsTab_16);
818                 return null;
819             }
820             
821             return expandedLocation;
822             
823         } catch (CoreException e1) {
824             if (location != null) {
825                 try {
826                     manager.validateStringVariables(location);
827                     setMessage(AntLaunchConfigurationMessages.AntTargetsTab_17);
828                     return null;
829                 } catch (CoreException e2) {//invalid variable
830
setErrorMessage(e2.getStatus().getMessage());
831                     return null;
832                 }
833             }
834             
835             setErrorMessage(e1.getStatus().getMessage());
836             return null;
837         }
838     }
839     
840     protected boolean isTargetSelected() {
841         return !fOrderedTargets.isEmpty();
842     }
843 }
844
Popular Tags