KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 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 import java.util.HashMap JavaDoc;
14 import java.util.Map JavaDoc;
15
16 import org.eclipse.ant.internal.ui.AntUIImages;
17 import org.eclipse.ant.internal.ui.AntUIPlugin;
18 import org.eclipse.ant.internal.ui.AntUtil;
19 import org.eclipse.ant.internal.ui.IAntUIConstants;
20 import org.eclipse.core.resources.IncrementalProjectBuilder;
21 import org.eclipse.core.runtime.CoreException;
22 import org.eclipse.debug.core.ILaunchConfiguration;
23 import org.eclipse.debug.core.ILaunchConfigurationWorkingCopy;
24 import org.eclipse.debug.ui.AbstractLaunchConfigurationTab;
25 import org.eclipse.jface.dialogs.IDialogConstants;
26 import org.eclipse.jface.window.Window;
27 import org.eclipse.swt.SWT;
28 import org.eclipse.swt.events.SelectionAdapter;
29 import org.eclipse.swt.events.SelectionEvent;
30 import org.eclipse.swt.events.SelectionListener;
31 import org.eclipse.swt.graphics.Image;
32 import org.eclipse.swt.layout.GridData;
33 import org.eclipse.swt.layout.GridLayout;
34 import org.eclipse.swt.widgets.Button;
35 import org.eclipse.swt.widgets.Composite;
36 import org.eclipse.swt.widgets.Label;
37 import org.eclipse.swt.widgets.Text;
38 import org.eclipse.ui.PlatformUI;
39 import org.eclipse.ui.externaltools.internal.model.BuilderUtils;
40 import org.eclipse.ui.externaltools.internal.model.IExternalToolConstants;
41 import org.eclipse.ui.externaltools.internal.model.IExternalToolsHelpContextIds;
42
43 public class AntBuilderTargetsTab extends AbstractLaunchConfigurationTab {
44     
45     private ILaunchConfiguration fConfiguration;
46     
47     private Button fAfterCleanTarget;
48     private Button fManualBuildTarget;
49     private Button fAutoBuildTarget;
50     private Button fDuringCleanTarget;
51     
52     private Text fAfterCleanTargetText;
53     private Text fManualBuildTargetText;
54     private Text fAutoBuildTargetText;
55     private Text fDuringCleanTargetText;
56     
57     private Map JavaDoc fAttributeToTargets= new HashMap JavaDoc();
58     
59     private static final String JavaDoc NOT_ENABLED= AntLaunchConfigurationMessages.AntBuilderTargetsTab_0;
60     private static final String JavaDoc DEFAULT_TARGET_SELECTED= AntLaunchConfigurationMessages.AntBuilderTargetsTab_10;
61     
62     private SelectionListener fSelectionListener= new SelectionAdapter() {
63         /* (non-Javadoc)
64          * @see org.eclipse.swt.events.SelectionListener#widgetSelected(org.eclipse.swt.events.SelectionEvent)
65          */

66         public void widgetSelected(SelectionEvent e) {
67             String JavaDoc attribute= null;
68             Object JavaDoc source = e.getSource();
69             Text text= null;
70             if (source == fAfterCleanTarget) {
71                 attribute= IAntLaunchConfigurationConstants.ATTR_ANT_AFTER_CLEAN_TARGETS;
72                 text= fAfterCleanTargetText;
73             } else if (source == fManualBuildTarget) {
74                 attribute= IAntLaunchConfigurationConstants.ATTR_ANT_MANUAL_TARGETS;
75                 text= fManualBuildTargetText;
76             } else if (source == fAutoBuildTarget) {
77                 attribute= IAntLaunchConfigurationConstants.ATTR_ANT_AUTO_TARGETS;
78                 text= fAutoBuildTargetText;
79             } else if (source == fDuringCleanTarget) {
80                 attribute= IAntLaunchConfigurationConstants.ATTR_ANT_CLEAN_TARGETS;
81                 text= fDuringCleanTargetText;
82             }
83             
84             setTargets(attribute, text);
85             updateLaunchConfigurationDialog();
86         }
87     };
88     
89     public AntBuilderTargetsTab() {
90         super();
91     }
92     
93     protected void createTargetsComponent(Composite parent) {
94         createLabel(AntLaunchConfigurationMessages.AntBuilderTargetsTab_1, parent);
95         fAfterCleanTargetText= createText(parent);
96         fAfterCleanTarget = createPushButton(parent, AntLaunchConfigurationMessages.AntBuilderTargetsTab_2, null);
97         GridData gd = new GridData(GridData.HORIZONTAL_ALIGN_END);
98         fAfterCleanTarget.setLayoutData(gd);
99         fAfterCleanTarget.addSelectionListener(fSelectionListener);
100         
101         createLabel(AntLaunchConfigurationMessages.AntBuilderTargetsTab_3, parent);
102         fManualBuildTargetText = createText(parent);
103         fManualBuildTarget = createPushButton(parent, AntLaunchConfigurationMessages.AntBuilderTargetsTab_4, null);
104         gd = new GridData(GridData.HORIZONTAL_ALIGN_END);
105         fManualBuildTarget.setLayoutData(gd);
106         fManualBuildTarget.addSelectionListener(fSelectionListener);
107         
108         createLabel(AntLaunchConfigurationMessages.AntBuilderTargetsTab_5, parent);
109         fAutoBuildTargetText = createText(parent);
110         fAutoBuildTarget = createPushButton(parent, AntLaunchConfigurationMessages.AntBuilderTargetsTab_6, null);
111         gd = new GridData(GridData.HORIZONTAL_ALIGN_END);
112         fAutoBuildTarget.setLayoutData(gd);
113         fAutoBuildTarget.addSelectionListener(fSelectionListener);
114         
115         createLabel(AntLaunchConfigurationMessages.AntBuilderTargetsTab_7, parent);
116         fDuringCleanTargetText = createText(parent);
117         fDuringCleanTarget = createPushButton(parent, AntLaunchConfigurationMessages.AntBuilderTargetsTab_8, null);
118         gd = new GridData(GridData.HORIZONTAL_ALIGN_END);
119         fDuringCleanTarget.setLayoutData(gd);
120         fDuringCleanTarget.addSelectionListener(fSelectionListener);
121     }
122
123     private Label createLabel(String JavaDoc text, Composite parent) {
124         Label newLabel= new Label(parent, SWT.NONE);
125         newLabel.setText(text);
126         GridData gd = new GridData(GridData.FILL_HORIZONTAL);
127         gd.horizontalSpan = 2;
128         newLabel.setLayoutData(gd);
129         newLabel.setFont(parent.getFont());
130         return newLabel;
131     }
132     private Text createText(Composite parent) {
133         GridData gd;
134         Text newText = new Text(parent, SWT.MULTI | SWT.WRAP | SWT.BORDER | SWT.V_SCROLL | SWT.READ_ONLY);
135         newText.setFont(parent.getFont());
136         gd = new GridData(GridData.FILL_HORIZONTAL);
137         gd.heightHint = 40;
138         gd.widthHint = IDialogConstants.ENTRY_FIELD_WIDTH;
139         newText.setLayoutData(gd);
140         return newText;
141     }
142     
143     protected void setTargets(String JavaDoc attribute, Text text) {
144         ILaunchConfigurationWorkingCopy copy= null;
145         try {
146             copy = fConfiguration.getWorkingCopy();
147         } catch (CoreException e) {
148            return;
149         }
150         copy.setAttribute(IAntLaunchConfigurationConstants.ATTR_ANT_TARGETS, (String JavaDoc)fAttributeToTargets.get(attribute));
151         SetTargetsDialog dialog= new SetTargetsDialog(getShell(), copy);
152         if (dialog.open() != Window.OK) {
153             return;
154         }
155         String JavaDoc targetsSelected= dialog.getTargetsSelected();
156        
157         if (targetsSelected == null) {//default
158
text.setEnabled(true);
159              fAttributeToTargets.remove(attribute);
160              setTargetsForUser(text, DEFAULT_TARGET_SELECTED, null);
161          } else if (targetsSelected.length() == 0) {
162              text.setEnabled(false);
163              fAttributeToTargets.remove(attribute);
164              text.setText(NOT_ENABLED);
165          } else {
166              text.setEnabled(true);
167              fAttributeToTargets.put(attribute, targetsSelected);
168              setTargetsForUser(text, targetsSelected, null);
169          }
170     }
171
172     private void setTargetsForUser(Text text, String JavaDoc targetsSelected, String JavaDoc configTargets) {
173         if (!text.isEnabled()) {
174             text.setText(NOT_ENABLED);
175             return;
176         }
177         if (targetsSelected == null) {
178             if (configTargets == null) {
179                 //build kind has been specified..see initializeBuildKinds
180
text.setText(DEFAULT_TARGET_SELECTED);
181                 return;
182             }
183             targetsSelected= configTargets;
184         }
185         String JavaDoc[] targets= AntUtil.parseRunTargets(targetsSelected);
186         StringBuffer JavaDoc result= new StringBuffer JavaDoc(targets[0]);
187         for (int i = 1; i < targets.length; i++) {
188              result.append(", "); //$NON-NLS-1$
189
result.append(targets[i]);
190         }
191         text.setText(result.toString());
192     }
193
194     public void createControl(Composite parent) {
195         Composite mainComposite = new Composite(parent, SWT.NONE);
196         setControl(mainComposite);
197         PlatformUI.getWorkbench().getHelpSystem().setHelp(getControl(), IExternalToolsHelpContextIds.EXTERNAL_TOOLS_LAUNCH_CONFIGURATION_DIALOG_BUILDER_TAB);
198         
199         GridLayout layout = new GridLayout();
200         GridData gridData = new GridData(GridData.FILL_HORIZONTAL);
201         gridData.horizontalSpan = 2;
202         layout.numColumns = 2;
203         layout.makeColumnsEqualWidth = false;
204         layout.horizontalSpacing=0;
205         layout.verticalSpacing=0;
206         mainComposite.setLayout(layout);
207         mainComposite.setLayoutData(gridData);
208         mainComposite.setFont(parent.getFont());
209         createTargetsComponent(mainComposite);
210     }
211
212     public void setDefaults(ILaunchConfigurationWorkingCopy configuration) {
213         configuration.setAttribute(IExternalToolConstants.ATTR_TRIGGERS_CONFIGURED, true);
214         configuration.setAttribute(IAntLaunchConfigurationConstants.ATTR_TARGETS_UPDATED, true);
215     }
216
217     public void initializeFrom(ILaunchConfiguration configuration) {
218         fConfiguration= configuration;
219         
220         fAfterCleanTargetText.setEnabled(false);
221         fManualBuildTargetText.setEnabled(false);
222         fAutoBuildTargetText.setEnabled(false);
223         fDuringCleanTargetText.setEnabled(false);
224
225         initializeBuildKinds(configuration);
226         intializeTargets(configuration);
227     }
228
229     private void intializeTargets(ILaunchConfiguration configuration) {
230         String JavaDoc configTargets= null;
231         String JavaDoc autoTargets= null;
232         String JavaDoc manualTargets= null;
233         String JavaDoc afterCleanTargets= null;
234         String JavaDoc duringCleanTargets= null;
235         try {
236             if (!configuration.getAttribute(IAntLaunchConfigurationConstants.ATTR_TARGETS_UPDATED, false)) {
237                 //not yet migrated to new format
238
configTargets= configuration.getAttribute(IAntLaunchConfigurationConstants.ATTR_ANT_TARGETS, (String JavaDoc)null);
239             }
240             
241             autoTargets= configuration.getAttribute(IAntLaunchConfigurationConstants.ATTR_ANT_AUTO_TARGETS, (String JavaDoc)null);
242             manualTargets= configuration.getAttribute(IAntLaunchConfigurationConstants.ATTR_ANT_MANUAL_TARGETS, (String JavaDoc)null);
243             afterCleanTargets= configuration.getAttribute(IAntLaunchConfigurationConstants.ATTR_ANT_AFTER_CLEAN_TARGETS, (String JavaDoc)null);
244             duringCleanTargets= configuration.getAttribute(IAntLaunchConfigurationConstants.ATTR_ANT_CLEAN_TARGETS, (String JavaDoc)null);
245             initializeAttributeToTargets(fAutoBuildTargetText, autoTargets, configTargets, IAntLaunchConfigurationConstants.ATTR_ANT_AUTO_TARGETS);
246             initializeAttributeToTargets(fManualBuildTargetText, manualTargets, configTargets, IAntLaunchConfigurationConstants.ATTR_ANT_MANUAL_TARGETS);
247             initializeAttributeToTargets(fDuringCleanTargetText, duringCleanTargets, configTargets, IAntLaunchConfigurationConstants.ATTR_ANT_CLEAN_TARGETS);
248             initializeAttributeToTargets(fAfterCleanTargetText, afterCleanTargets, configTargets, IAntLaunchConfigurationConstants.ATTR_ANT_AFTER_CLEAN_TARGETS);
249         } catch (CoreException ce) {
250             AntUIPlugin.log("Error reading configuration", ce); //$NON-NLS-1$
251
}
252         
253         setTargetsForUser(fManualBuildTargetText, manualTargets, configTargets);
254         setTargetsForUser(fAfterCleanTargetText, afterCleanTargets, configTargets);
255         setTargetsForUser(fDuringCleanTargetText, duringCleanTargets, configTargets);
256         setTargetsForUser(fAutoBuildTargetText, autoTargets, configTargets);
257     }
258     
259     private void initializeAttributeToTargets(Text textComponent, String JavaDoc specificTargets, String JavaDoc configTargets, String JavaDoc attribute) {
260         if (textComponent.isEnabled()) {
261             if (specificTargets == null && configTargets != null) {
262                 fAttributeToTargets.put(attribute, configTargets);
263             } else {
264                 fAttributeToTargets.put(attribute, specificTargets);
265             }
266         }
267     }
268
269     private void initializeBuildKinds(ILaunchConfiguration configuration) {
270         String JavaDoc buildKindString= null;
271         try {
272             buildKindString= configuration.getAttribute(IExternalToolConstants.ATTR_RUN_BUILD_KINDS, ""); //$NON-NLS-1$
273
} catch (CoreException e) {
274             AntUIPlugin.log("Error reading configuration", e); //$NON-NLS-1$
275
}
276         int buildTypes[]= BuilderUtils.buildTypesToArray(buildKindString);
277         for (int i = 0; i < buildTypes.length; i++) {
278             switch (buildTypes[i]) {
279                 case IncrementalProjectBuilder.FULL_BUILD:
280                     fAfterCleanTargetText.setEnabled(true);
281                     break;
282                 case IncrementalProjectBuilder.INCREMENTAL_BUILD:
283                     fManualBuildTargetText.setEnabled(true);
284                     break;
285                 case IncrementalProjectBuilder.AUTO_BUILD:
286                     fAutoBuildTargetText.setEnabled(true);
287                     break;
288                 case IncrementalProjectBuilder.CLEAN_BUILD:
289                     fDuringCleanTargetText.setEnabled(true);
290                     break;
291             }
292         }
293     }
294
295     public void performApply(ILaunchConfigurationWorkingCopy configuration) {
296         StringBuffer JavaDoc buffer= new StringBuffer JavaDoc();
297         if (!fAfterCleanTargetText.getText().equals(NOT_ENABLED)) {
298             buffer.append(IExternalToolConstants.BUILD_TYPE_FULL).append(',');
299         }
300         if (!fManualBuildTargetText.getText().equals(NOT_ENABLED)){
301             buffer.append(IExternalToolConstants.BUILD_TYPE_INCREMENTAL).append(',');
302         }
303         if (!fAutoBuildTargetText.getText().equals(NOT_ENABLED)) {
304             buffer.append(IExternalToolConstants.BUILD_TYPE_AUTO).append(',');
305         }
306         if (!fDuringCleanTargetText.getText().equals(NOT_ENABLED)) {
307             buffer.append(IExternalToolConstants.BUILD_TYPE_CLEAN);
308         }
309         configuration.setAttribute(IExternalToolConstants.ATTR_RUN_BUILD_KINDS, buffer.toString());
310         
311         String JavaDoc targets= (String JavaDoc) fAttributeToTargets.get(IAntLaunchConfigurationConstants.ATTR_ANT_AFTER_CLEAN_TARGETS);
312         configuration.setAttribute(IAntLaunchConfigurationConstants.ATTR_ANT_AFTER_CLEAN_TARGETS, targets);
313         targets= (String JavaDoc) fAttributeToTargets.get(IAntLaunchConfigurationConstants.ATTR_ANT_AUTO_TARGETS);
314         configuration.setAttribute(IAntLaunchConfigurationConstants.ATTR_ANT_AUTO_TARGETS, targets);
315         targets= (String JavaDoc) fAttributeToTargets.get(IAntLaunchConfigurationConstants.ATTR_ANT_MANUAL_TARGETS);
316         configuration.setAttribute(IAntLaunchConfigurationConstants.ATTR_ANT_MANUAL_TARGETS, targets);
317         targets= (String JavaDoc) fAttributeToTargets.get(IAntLaunchConfigurationConstants.ATTR_ANT_CLEAN_TARGETS);
318         configuration.setAttribute(IAntLaunchConfigurationConstants.ATTR_ANT_CLEAN_TARGETS, targets);
319         
320         configuration.setAttribute(IAntLaunchConfigurationConstants.ATTR_TARGETS_UPDATED, true);
321     }
322
323     /* (non-Javadoc)
324      * @see org.eclipse.debug.ui.ILaunchConfigurationTab#getName()
325      */

326     public String JavaDoc getName() {
327         return AntLaunchConfigurationMessages.AntTargetsTab_Tar_gets_14;
328     }
329         
330     /* (non-Javadoc)
331      * @see org.eclipse.debug.ui.ILaunchConfigurationTab#getImage()
332      */

333     public Image getImage() {
334         return AntUIImages.getImage(IAntUIConstants.IMG_TAB_ANT_TARGETS);
335     }
336 }
337
Popular Tags