001: /*******************************************************************************
002: * Copyright (c) 2000, 2004 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.jsp.launching;
011:
012: import java.io.File;
013:
014: import org.eclipse.core.resources.IProject;
015: import org.eclipse.core.resources.IResource;
016: import org.eclipse.core.resources.ResourcesPlugin;
017: import org.eclipse.core.runtime.CoreException;
018: import org.eclipse.core.runtime.IPath;
019: import org.eclipse.core.runtime.Path;
020: import org.eclipse.core.variables.VariablesPlugin;
021: import org.eclipse.debug.core.DebugPlugin;
022: import org.eclipse.debug.core.ILaunchConfiguration;
023: import org.eclipse.debug.core.ILaunchConfigurationWorkingCopy;
024: import org.eclipse.debug.ui.AbstractLaunchConfigurationTab;
025: import org.eclipse.jdt.launching.IJavaLaunchConfigurationConstants;
026: import org.eclipse.jface.viewers.ILabelProvider;
027: import org.eclipse.jface.window.Window;
028: import org.eclipse.jsp.JspPluginImages;
029: import org.eclipse.swt.SWT;
030: import org.eclipse.swt.events.ModifyEvent;
031: import org.eclipse.swt.events.ModifyListener;
032: import org.eclipse.swt.events.SelectionAdapter;
033: import org.eclipse.swt.events.SelectionEvent;
034: import org.eclipse.swt.graphics.Font;
035: import org.eclipse.swt.graphics.Image;
036: import org.eclipse.swt.layout.GridData;
037: import org.eclipse.swt.layout.GridLayout;
038: import org.eclipse.swt.widgets.Button;
039: import org.eclipse.swt.widgets.Composite;
040: import org.eclipse.swt.widgets.DirectoryDialog;
041: import org.eclipse.swt.widgets.Label;
042: import org.eclipse.swt.widgets.Text;
043: import org.eclipse.ui.dialogs.ElementListSelectionDialog;
044: import org.eclipse.ui.model.WorkbenchLabelProvider;
045:
046: /**
047: * Specifies the install location of Tomcat.
048: */
049: public class TomcatTab extends AbstractLaunchConfigurationTab {
050:
051: // Tomcat location
052: private Button fBrowseButton;
053: private Text fTomcatDir;
054:
055: // WebApp location
056: private Button fProjectButton;
057: private Text fProjectText;
058:
059: /**
060: * Constructs a new Tomcat tab
061: */
062: public TomcatTab() {
063: super ();
064: }
065:
066: /**
067: * @see org.eclipse.debug.ui.ILaunchConfigurationTab#createControl(Composite)
068: */
069: public void createControl(Composite parent) {
070:
071: Font font = parent.getFont();
072:
073: Composite composite = new Composite(parent, SWT.NONE);
074: GridLayout workingDirLayout = new GridLayout();
075: workingDirLayout.numColumns = 3;
076: workingDirLayout.marginHeight = 0;
077: workingDirLayout.marginWidth = 0;
078: composite.setLayout(workingDirLayout);
079: GridData gd = new GridData(GridData.FILL_HORIZONTAL);
080: composite.setLayoutData(gd);
081: composite.setFont(font);
082: setControl(composite);
083:
084: createVerticalSpacer(composite, 3);
085:
086: Label label = new Label(composite, SWT.NONE);
087: label.setText(LaunchingMessages.TomcatTab_3);
088: gd = new GridData(GridData.HORIZONTAL_ALIGN_BEGINNING);
089: gd.horizontalSpan = 3;
090: label.setLayoutData(gd);
091: label.setFont(font);
092:
093: fTomcatDir = new Text(composite, SWT.SINGLE | SWT.BORDER);
094: gd = new GridData(GridData.FILL_HORIZONTAL);
095: gd.horizontalSpan = 2;
096: fTomcatDir.setLayoutData(gd);
097: fTomcatDir.setFont(font);
098: fTomcatDir.addModifyListener(new ModifyListener() {
099: public void modifyText(ModifyEvent evt) {
100: updateLaunchConfigurationDialog();
101: }
102: });
103:
104: fBrowseButton = createPushButton(composite,
105: LaunchingMessages.TomcatTab_21, null);
106: fBrowseButton.addSelectionListener(new SelectionAdapter() {
107: public void widgetSelected(SelectionEvent evt) {
108: handleTomcatBrowseButtonSelected();
109: }
110: });
111:
112: createVerticalSpacer(composite, 3);
113:
114: label = new Label(composite, SWT.NONE);
115: label.setText(LaunchingMessages.TomcatTab_22);
116: gd = new GridData(GridData.HORIZONTAL_ALIGN_BEGINNING);
117: gd.horizontalSpan = 3;
118: label.setLayoutData(gd);
119: label.setFont(font);
120:
121: fProjectText = new Text(composite, SWT.SINGLE | SWT.BORDER);
122: gd = new GridData(GridData.FILL_HORIZONTAL);
123: gd.horizontalSpan = 2;
124: fProjectText.setLayoutData(gd);
125: fProjectText.setFont(font);
126: fProjectText.addModifyListener(new ModifyListener() {
127: public void modifyText(ModifyEvent evt) {
128: updateLaunchConfigurationDialog();
129: }
130: });
131:
132: fProjectButton = createPushButton(composite,
133: LaunchingMessages.TomcatTab_23, null);
134: fProjectButton.addSelectionListener(new SelectionAdapter() {
135: public void widgetSelected(SelectionEvent evt) {
136: handleProjectBrowseButtonSelected();
137: }
138: });
139: }
140:
141: /**
142: * Show a dialog that lets the user select a project
143: * from the workspace
144: */
145: protected void handleProjectBrowseButtonSelected() {
146: ILabelProvider lp = new WorkbenchLabelProvider();
147: ElementListSelectionDialog dialog = new ElementListSelectionDialog(
148: getShell(), lp);
149: dialog.setElements(ResourcesPlugin.getWorkspace().getRoot()
150: .getProjects());
151: dialog.setMultipleSelection(false);
152: dialog.setTitle(LaunchingMessages.TomcatTab_28);
153: dialog.setMessage(LaunchingMessages.TomcatTab_29);
154: if (dialog.open() == Window.OK) {
155: Object[] elements = dialog.getResult();
156: if (elements != null && elements.length == 1) {
157: fProjectText.setText(((IResource) elements[0])
158: .getName());
159: }
160: }
161:
162: }
163:
164: /**
165: * Show a dialog that lets the user select a tomcat install directory
166: */
167: protected void handleTomcatBrowseButtonSelected() {
168: DirectoryDialog dialog = new DirectoryDialog(getShell());
169: dialog.setMessage(LaunchingMessages.TomcatTab_4);
170: String currentWorkingDir = fTomcatDir.getText();
171: if (!currentWorkingDir.trim().equals("")) { //$NON-NLS-1$
172: File path = new File(currentWorkingDir);
173: if (path.exists()) {
174: dialog.setFilterPath(currentWorkingDir);
175: }
176: }
177:
178: String selectedDirectory = dialog.open();
179: if (selectedDirectory != null) {
180: fTomcatDir.setText(selectedDirectory);
181: }
182: }
183:
184: /**
185: * @see org.eclipse.debug.ui.ILaunchConfigurationTab#dispose()
186: */
187: public void dispose() {
188: // empty implementation
189: }
190:
191: /**
192: * @see org.eclipse.debug.ui.ILaunchConfigurationTab#isValid(ILaunchConfiguration)
193: */
194: public boolean isValid(ILaunchConfiguration config) {
195: setErrorMessage(null);
196: setMessage(null);
197:
198: String workingDirPath = fTomcatDir.getText().trim();
199: // resolve variables (if any)
200: String expansion;
201: try {
202: expansion = VariablesPlugin.getDefault()
203: .getStringVariableManager()
204: .performStringSubstitution(workingDirPath);
205: } catch (CoreException e) {
206: setErrorMessage(e.getMessage());
207: return false;
208: }
209: if (workingDirPath.length() > 0) {
210: File dir = new File(expansion);
211: if (!dir.exists()) {
212: setErrorMessage(LaunchingMessages.TomcatTab_5);
213: return false;
214: }
215: if (!dir.isDirectory()) {
216: setErrorMessage(LaunchingMessages.TomcatTab_6);
217: return false;
218: }
219: }
220:
221: String projectName = fProjectText.getText().trim();
222: if (projectName.length() > 0) {
223: IProject project = ResourcesPlugin.getWorkspace().getRoot()
224: .getProject(projectName);
225: if (!project.exists()) {
226: setErrorMessage(LaunchingMessages.TomcatTab_30);
227: return false;
228: }
229: }
230:
231: return true;
232: }
233:
234: /**
235: * @see org.eclipse.debug.ui.ILaunchConfigurationTab#setDefaults(ILaunchConfigurationWorkingCopy)
236: */
237: public void setDefaults(ILaunchConfigurationWorkingCopy config) {
238: config.setAttribute(TomcatLaunchDelegate.ATTR_CATALINA_HOME,
239: "${catalina_home}"); //$NON-NLS-1$
240: config
241: .setAttribute(
242: IJavaLaunchConfigurationConstants.ATTR_CLASSPATH_PROVIDER,
243: TomcatLaunchDelegate.ID_TOMCAT_CLASSPATH_PROVIDER);
244: config.setAttribute(
245: IJavaLaunchConfigurationConstants.ATTR_MAIN_TYPE_NAME,
246: "org.apache.catalina.startup.Bootstrap"); //$NON-NLS-1$
247: }
248:
249: /**
250: * @see org.eclipse.debug.ui.ILaunchConfigurationTab#initializeFrom(ILaunchConfiguration)
251: */
252: public void initializeFrom(ILaunchConfiguration configuration) {
253: try {
254: fTomcatDir.setText(configuration.getAttribute(
255: TomcatLaunchDelegate.ATTR_CATALINA_HOME, "")); //$NON-NLS-1$
256: fProjectText
257: .setText(configuration
258: .getAttribute(
259: IJavaLaunchConfigurationConstants.ATTR_PROJECT_NAME,
260: "")); //$NON-NLS-1$
261: if (configuration.isWorkingCopy()) {
262: // set VM args
263: ILaunchConfigurationWorkingCopy workingCopy = (ILaunchConfigurationWorkingCopy) configuration;
264: String home = TomcatLaunchDelegate.getCatalinaHome();
265: IPath endorsed = new Path(home)
266: .append("common").append("endorsed"); //$NON-NLS-1$//$NON-NLS-2$
267: IPath temp = new Path(home).append("temp"); //$NON-NLS-1$
268: StringBuffer args = new StringBuffer();
269: args.append("-Djava.endorsed.dirs=\""); //$NON-NLS-1$
270: args.append(endorsed.toOSString());
271: args.append("\" "); //$NON-NLS-1$
272: args.append("-Dcatalina.base=\""); //$NON-NLS-1$
273: args.append(home);
274: args.append("\" "); //$NON-NLS-1$
275: args.append("-Dcatalina.home=\""); //$NON-NLS-1$
276: args.append(home);
277: args.append("\" "); //$NON-NLS-1$
278: args.append("-Djava.io.tmpdir=\""); //$NON-NLS-1$
279: args.append(temp.toOSString());
280: args.append("\""); //$NON-NLS-1$
281: workingCopy
282: .setAttribute(
283: IJavaLaunchConfigurationConstants.ATTR_VM_ARGUMENTS,
284: args.toString());
285: workingCopy
286: .setAttribute(
287: IJavaLaunchConfigurationConstants.ATTR_PROGRAM_ARGUMENTS,
288: "start"); //$NON-NLS-1$
289: }
290: } catch (CoreException e) {
291: setErrorMessage(e.getMessage());
292: DebugPlugin.log(e);
293: }
294: }
295:
296: /**
297: * @see org.eclipse.debug.ui.ILaunchConfigurationTab#performApply(ILaunchConfigurationWorkingCopy)
298: */
299: public void performApply(
300: ILaunchConfigurationWorkingCopy configuration) {
301: configuration.setAttribute(
302: TomcatLaunchDelegate.ATTR_CATALINA_HOME,
303: getAttributeValueFrom(fTomcatDir));
304: String projectName = getAttributeValueFrom(fProjectText);
305: configuration.setAttribute(
306: IJavaLaunchConfigurationConstants.ATTR_PROJECT_NAME,
307: projectName);
308: }
309:
310: /**
311: * Retuns the string in the text widget, or <code>null</code> if empty.
312: *
313: * @return text or <code>null</code>
314: */
315: protected String getAttributeValueFrom(Text text) {
316: String content = text.getText().trim();
317: if (content.length() > 0) {
318: return content;
319: }
320: return null;
321: }
322:
323: /**
324: * @see org.eclipse.debug.ui.ILaunchConfigurationTab#getName()
325: */
326: public String getName() {
327: return LaunchingMessages.TomcatTab_7;
328: }
329:
330: /**
331: * @see org.eclipse.debug.ui.ILaunchConfigurationTab#getImage()
332: */
333: public Image getImage() {
334: return JspPluginImages.getImage(JspPluginImages.IMG_OBJ_TOMCAT);
335: }
336:
337: }
|