KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 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 org.eclipse.ant.internal.ui.AntUIPlugin;
14 import org.eclipse.ant.internal.ui.AntUtil;
15 import org.eclipse.ant.internal.ui.IAntUIConstants;
16 import org.eclipse.ant.internal.ui.IAntUIHelpContextIds;
17 import org.eclipse.core.resources.IFile;
18 import org.eclipse.core.resources.IResource;
19 import org.eclipse.core.resources.ResourcesPlugin;
20 import org.eclipse.core.runtime.CoreException;
21 import org.eclipse.core.variables.IStringVariableManager;
22 import org.eclipse.core.variables.VariablesPlugin;
23 import org.eclipse.debug.core.ILaunchConfiguration;
24 import org.eclipse.debug.core.ILaunchConfigurationWorkingCopy;
25 import org.eclipse.debug.ui.ILaunchConfigurationTab;
26 import org.eclipse.jdt.launching.IJavaLaunchConfigurationConstants;
27 import org.eclipse.jface.dialogs.Dialog;
28 import org.eclipse.jface.viewers.IStructuredSelection;
29 import org.eclipse.swt.SWT;
30 import org.eclipse.swt.events.SelectionAdapter;
31 import org.eclipse.swt.events.SelectionEvent;
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.ui.PlatformUI;
37 import org.eclipse.ui.externaltools.internal.launchConfigurations.ExternalToolsMainTab;
38 import org.eclipse.ui.externaltools.internal.model.IExternalToolConstants;
39 import org.eclipse.ui.externaltools.internal.ui.FileSelectionDialog;
40
41 public class AntMainTab extends ExternalToolsMainTab {
42
43     private String JavaDoc fCurrentLocation= null;
44     private Button fSetInputHandlerButton;
45     private IFile fNewFile;
46
47     /* (non-Javadoc)
48      * @see org.eclipse.debug.ui.ILaunchConfigurationTab#initializeFrom(org.eclipse.debug.core.ILaunchConfiguration)
49      */

50     public void initializeFrom(ILaunchConfiguration configuration) {
51         super.initializeFrom(configuration);
52         try {
53             fCurrentLocation= configuration.getAttribute(IExternalToolConstants.ATTR_LOCATION, (String JavaDoc)null);
54         } catch (CoreException e) {
55         }
56         updateCheckButtons(configuration);
57     }
58     
59     /* (non-Javadoc)
60      * @see org.eclipse.debug.ui.ILaunchConfigurationTab#performApply(org.eclipse.debug.core.ILaunchConfigurationWorkingCopy)
61      */

62     public void performApply(ILaunchConfigurationWorkingCopy configuration) {
63         super.performApply(configuration);
64         try {
65             //has the location changed
66
String JavaDoc newLocation= configuration.getAttribute(IExternalToolConstants.ATTR_LOCATION, (String JavaDoc)null);
67             if (newLocation != null) {
68                 if (!newLocation.equals(fCurrentLocation)) {
69                     updateTargetsTab();
70                     fCurrentLocation= newLocation;
71                     updateProjectName(configuration);
72                 }
73             } else if (fCurrentLocation != null){
74                 updateTargetsTab();
75                 fCurrentLocation= newLocation;
76                 updateProjectName(configuration);
77             }
78         } catch (CoreException e) {
79         }
80        
81         setMappedResources(configuration);
82         setAttribute(IAntUIConstants.SET_INPUTHANDLER, configuration, fSetInputHandlerButton.getSelection(), true);
83     }
84
85     private void setMappedResources(ILaunchConfigurationWorkingCopy configuration) {
86         IFile file= getIFile(configuration);
87         configuration.setMappedResources(new IResource[] {file});
88     }
89
90     private void updateProjectName(ILaunchConfigurationWorkingCopy configuration) {
91         IFile file = getIFile(configuration);
92         String JavaDoc projectName= ""; //$NON-NLS-1$
93
if (file != null) {
94             projectName= file.getProject().getName();
95         }
96         configuration.setAttribute(IJavaLaunchConfigurationConstants.ATTR_PROJECT_NAME, projectName);
97     }
98
99     private IFile getIFile(ILaunchConfigurationWorkingCopy configuration) {
100         IFile file= null;
101         if (fNewFile != null) {
102             file= fNewFile;
103             fNewFile= null;
104         } else {
105             IStringVariableManager manager = VariablesPlugin.getDefault().getStringVariableManager();
106             try {
107                 String JavaDoc location= configuration.getAttribute(IExternalToolConstants.ATTR_LOCATION, (String JavaDoc)null);
108                 if (location != null) {
109                     String JavaDoc expandedLocation= manager.performStringSubstitution(location);
110                     if (expandedLocation != null) {
111                         file= AntUtil.getFileForLocation(expandedLocation, null);
112                     }
113                 }
114             } catch (CoreException e) {
115             }
116         }
117         return file;
118     }
119
120     /* (non-Javadoc)
121      * @see org.eclipse.debug.ui.ILaunchConfigurationTab#createControl(org.eclipse.swt.widgets.Composite)
122      */

123     public void createControl(Composite parent) {
124         Composite mainComposite = new Composite(parent, SWT.NONE);
125         setControl(mainComposite);
126         PlatformUI.getWorkbench().getHelpSystem().setHelp(mainComposite, IAntUIHelpContextIds.ANT_MAIN_TAB);
127         GridLayout layout = new GridLayout();
128         layout.numColumns = 1;
129         GridData gridData = new GridData(GridData.FILL_HORIZONTAL);
130         mainComposite.setLayout(layout);
131         mainComposite.setLayoutData(gridData);
132         mainComposite.setFont(parent.getFont());
133         createLocationComponent(mainComposite);
134         createWorkDirectoryComponent(mainComposite);
135         createArgumentComponent(mainComposite);
136         createVerticalSpacer(mainComposite, 2);
137         createSetInputHandlerComponent(mainComposite);
138         Dialog.applyDialogFont(parent);
139     }
140     
141     /**
142      * Creates the controls needed to edit the set input handler attribute of an
143      * Ant build
144      *
145      * @param parent the composite to create the controls in
146      */

147     private void createSetInputHandlerComponent(Composite parent) {
148         fSetInputHandlerButton = createCheckButton(parent, AntLaunchConfigurationMessages.AntMainTab_0);
149         GridData data = new GridData(GridData.HORIZONTAL_ALIGN_FILL);
150         data.horizontalSpan = 2;
151         fSetInputHandlerButton.setLayoutData(data);
152         fSetInputHandlerButton.addSelectionListener(new SelectionAdapter() {
153             public void widgetSelected(SelectionEvent e) {
154                 updateLaunchConfigurationDialog();
155             }
156         });
157     }
158     
159     private void updateCheckButtons(ILaunchConfiguration configuration) {
160         boolean setInputHandler= true;
161         try {
162             setInputHandler= configuration.getAttribute(IAntUIConstants.SET_INPUTHANDLER, true);
163         } catch (CoreException ce) {
164             AntUIPlugin.log(AntLaunchConfigurationMessages.AntMainTab_1, ce);
165         }
166         fSetInputHandlerButton.setSelection(setInputHandler);
167     }
168
169     /* (non-Javadoc)
170      * @see org.eclipse.ui.externaltools.internal.launchConfigurations.ExternalToolsMainTab#handleWorkspaceLocationButtonSelected()
171      */

172     protected void handleWorkspaceLocationButtonSelected() {
173         FileSelectionDialog dialog;
174         dialog = new FileSelectionDialog(getShell(), ResourcesPlugin.getWorkspace().getRoot(), AntLaunchConfigurationMessages.AntMainTab__Select_a_build_file__1);
175         dialog.open();
176         IStructuredSelection result = dialog.getResult();
177         if (result == null) {
178             return;
179         }
180         Object JavaDoc file= result.getFirstElement();
181         if (file instanceof IFile) {
182             fNewFile= (IFile)file;
183             locationField.setText(VariablesPlugin.getDefault().getStringVariableManager().generateVariableExpression("workspace_loc", fNewFile.getFullPath().toString())); //$NON-NLS-1$
184
}
185     }
186
187     /* (non-Javadoc)
188      * @see org.eclipse.ui.externaltools.internal.launchConfigurations.ExternalToolsMainTab#getWorkingDirectoryLabel()
189      */

190     protected String JavaDoc getWorkingDirectoryLabel() {
191         return AntLaunchConfigurationMessages.AntMainTab_3;
192     }
193     
194     private void updateTargetsTab() {
195         //the location has changed...set the targets tab to
196
//need to be recomputed
197
ILaunchConfigurationTab[] tabs= getLaunchConfigurationDialog().getTabs();
198         for (int i = 0; i < tabs.length; i++) {
199             ILaunchConfigurationTab tab = tabs[i];
200             if (tab instanceof AntTargetsTab) {
201                 ((AntTargetsTab)tab).setDirty(true);
202                 break;
203             }
204         }
205     }
206     
207     /* (non-Javadoc)
208      * @see org.eclipse.ui.externaltools.internal.launchConfigurations.ExternalToolsMainTab#getLocationLabel()
209      */

210     protected String JavaDoc getLocationLabel() {
211         return AntLaunchConfigurationMessages.AntMainTab_6;
212     }
213 }
214
Popular Tags