KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > debug > ui > launcher > SourceLookupBlock


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.jdt.internal.debug.ui.launcher;
12
13
14 import java.util.ArrayList JavaDoc;
15 import java.util.List JavaDoc;
16
17 import org.eclipse.core.runtime.CoreException;
18 import org.eclipse.debug.core.ILaunchConfiguration;
19 import org.eclipse.debug.core.ILaunchConfigurationWorkingCopy;
20 import org.eclipse.debug.ui.ILaunchConfigurationTab;
21 import org.eclipse.jdt.debug.ui.JavaUISourceLocator;
22 import org.eclipse.jdt.internal.debug.ui.JDIDebugUIPlugin;
23 import org.eclipse.jdt.internal.debug.ui.actions.AddAdvancedAction;
24 import org.eclipse.jdt.internal.debug.ui.actions.AddExternalFolderAction;
25 import org.eclipse.jdt.internal.debug.ui.actions.AddExternalJarAction;
26 import org.eclipse.jdt.internal.debug.ui.actions.AddFolderAction;
27 import org.eclipse.jdt.internal.debug.ui.actions.AddJarAction;
28 import org.eclipse.jdt.internal.debug.ui.actions.AddLibraryAction;
29 import org.eclipse.jdt.internal.debug.ui.actions.AddProjectAction;
30 import org.eclipse.jdt.internal.debug.ui.actions.AddVariableAction;
31 import org.eclipse.jdt.internal.debug.ui.actions.AttachSourceAction;
32 import org.eclipse.jdt.internal.debug.ui.actions.MoveDownAction;
33 import org.eclipse.jdt.internal.debug.ui.actions.MoveUpAction;
34 import org.eclipse.jdt.internal.debug.ui.actions.RemoveAction;
35 import org.eclipse.jdt.internal.debug.ui.actions.RuntimeClasspathAction;
36 import org.eclipse.jdt.launching.IJavaLaunchConfigurationConstants;
37 import org.eclipse.jdt.launching.IRuntimeClasspathEntry;
38 import org.eclipse.jdt.launching.JavaRuntime;
39 import org.eclipse.jface.action.IAction;
40 import org.eclipse.jface.dialogs.Dialog;
41 import org.eclipse.jface.dialogs.IDialogConstants;
42 import org.eclipse.swt.SWT;
43 import org.eclipse.swt.events.SelectionAdapter;
44 import org.eclipse.swt.events.SelectionEvent;
45 import org.eclipse.swt.graphics.Font;
46 import org.eclipse.swt.graphics.FontMetrics;
47 import org.eclipse.swt.graphics.GC;
48 import org.eclipse.swt.layout.GridData;
49 import org.eclipse.swt.layout.GridLayout;
50 import org.eclipse.swt.widgets.Button;
51 import org.eclipse.swt.widgets.Composite;
52 import org.eclipse.swt.widgets.Label;
53  
54 /**
55  * Control used to edit the source lookup path for a Java launch configuration.
56  */

57 public class SourceLookupBlock extends AbstractJavaClasspathTab implements ILaunchConfigurationTab {
58     
59     protected ILaunchConfiguration fConfig;
60     
61     protected RuntimeClasspathViewer fPathViewer;
62     protected Button fDefaultButton;
63     protected Button fDuplicatesButton;
64     
65     protected static final String JavaDoc DIALOG_SETTINGS_PREFIX = "SourceLookupBlock"; //$NON-NLS-1$
66

67     /**
68      * Creates and returns the source lookup control.
69      *
70      * @param parent the parent widget of this control
71      */

72     public void createControl(Composite parent) {
73         Font font = parent.getFont();
74         
75         Composite comp = new Composite(parent, SWT.NONE);
76         GridLayout topLayout = new GridLayout();
77         topLayout.numColumns = 2;
78         comp.setLayout(topLayout);
79         GridData gd = new GridData(GridData.FILL_BOTH);
80         comp.setLayoutData(gd);
81         
82         Label viewerLabel= new Label(comp, SWT.LEFT);
83         viewerLabel.setText(LauncherMessages.SourceLookupBlock__Source_Lookup_Path__1);
84         gd= new GridData(GridData.HORIZONTAL_ALIGN_FILL);
85         gd.horizontalSpan= 2;
86         viewerLabel.setLayoutData(gd);
87         viewerLabel.setFont(font);
88         
89         fPathViewer = new RuntimeClasspathViewer(comp);
90         fPathViewer.addEntriesChangedListener(this);
91         gd = new GridData(GridData.FILL_BOTH);
92         fPathViewer.getControl().setLayoutData(gd);
93         fPathViewer.getControl().setFont(font);
94
95         Composite pathButtonComp = new Composite(comp, SWT.NONE);
96         GridLayout pathButtonLayout = new GridLayout();
97         pathButtonLayout.marginHeight = 0;
98         pathButtonLayout.marginWidth = 0;
99         pathButtonComp.setLayout(pathButtonLayout);
100         gd = new GridData(GridData.VERTICAL_ALIGN_BEGINNING | GridData.HORIZONTAL_ALIGN_FILL);
101         pathButtonComp.setLayoutData(gd);
102         pathButtonComp.setFont(font);
103         
104         createVerticalSpacer(comp, 2);
105                         
106         fDefaultButton = new Button(comp, SWT.CHECK);
107         fDefaultButton.setText(LauncherMessages.SourceLookupBlock_Use_defau_lt_source_lookup_path_1);
108         gd = new GridData(GridData.HORIZONTAL_ALIGN_BEGINNING);
109         gd.horizontalSpan = 2;
110         fDefaultButton.setLayoutData(gd);
111         fDefaultButton.setFont(font);
112         fDefaultButton.addSelectionListener(new SelectionAdapter() {
113             public void widgetSelected(SelectionEvent evt) {
114                 handleDefaultButtonSelected();
115             }
116         });
117         
118         fDuplicatesButton = new Button(comp, SWT.CHECK);
119         fDuplicatesButton.setText(LauncherMessages.SourceLookupBlock__Search_for_duplicate_source_files_on_path_1);
120         gd = new GridData(GridData.HORIZONTAL_ALIGN_BEGINNING);
121         gd.horizontalSpan = 2;
122         fDuplicatesButton.setLayoutData(gd);
123         fDuplicatesButton.setFont(font);
124         fDuplicatesButton.addSelectionListener(new SelectionAdapter() {
125             public void widgetSelected(SelectionEvent evt) {
126                 setDirty(true);
127                 updateLaunchConfigurationDialog();
128             }
129         });
130         
131         List JavaDoc advancedActions = new ArrayList JavaDoc(5);
132         
133         GC gc = new GC(parent);
134         gc.setFont(parent.getFont());
135         FontMetrics fontMetrics= gc.getFontMetrics();
136         gc.dispose();
137                 
138         RuntimeClasspathAction action = new MoveUpAction(fPathViewer);
139         Button button = createPushButton(pathButtonComp, action.getText(), fontMetrics);
140         action.setButton(button);
141         
142         action = new MoveDownAction(fPathViewer);
143         button = createPushButton(pathButtonComp, action.getText(), fontMetrics);
144         action.setButton(button);
145
146         action = new RemoveAction(fPathViewer);
147         button = createPushButton(pathButtonComp, action.getText(), fontMetrics);
148         action.setButton(button);
149         
150         action = new AddProjectAction(fPathViewer);
151         button = createPushButton(pathButtonComp, action.getText(), fontMetrics);
152         action.setButton(button);
153
154         action = new AddJarAction(fPathViewer);
155         button = createPushButton(pathButtonComp, action.getText(), fontMetrics);
156         action.setButton(button);
157
158         action = new AddExternalJarAction(fPathViewer, DIALOG_SETTINGS_PREFIX);
159         button = createPushButton(pathButtonComp, action.getText(), fontMetrics);
160         action.setButton(button);
161
162         action = new AddFolderAction(fPathViewer);
163         advancedActions.add(action);
164
165         action = new AddExternalFolderAction(fPathViewer, DIALOG_SETTINGS_PREFIX);
166         advancedActions.add(action);
167
168         action = new AddVariableAction(fPathViewer);
169         advancedActions.add(action);
170         
171         action = new AddLibraryAction(null);
172         advancedActions.add(action);
173         
174         action = new AttachSourceAction(fPathViewer, SWT.RADIO);
175         advancedActions.add(action);
176                                     
177         IAction[] adv = (IAction[])advancedActions.toArray(new IAction[advancedActions.size()]);
178         action = new AddAdvancedAction(fPathViewer, adv);
179         button = createPushButton(pathButtonComp, action.getText(), fontMetrics);
180         action.setButton(button);
181                 
182         setControl(comp);
183     }
184
185     /**
186      * The "default" button has been toggled
187      */

188     protected void handleDefaultButtonSelected() {
189         setDirty(true);
190         boolean def = fDefaultButton.getSelection();
191         if (def) {
192             try {
193                 ILaunchConfiguration config = getLaunchConfiguration();
194                 ILaunchConfigurationWorkingCopy wc = null;
195                 if (config.isWorkingCopy()) {
196                     wc= (ILaunchConfigurationWorkingCopy)config;
197                 } else {
198                     wc = config.getWorkingCopy();
199                 }
200                 performApply(wc);
201                 IRuntimeClasspathEntry[] defs = JavaRuntime.computeUnresolvedSourceLookupPath(wc);
202                 fPathViewer.setEntries(defs);
203             } catch (CoreException e) {
204                 JDIDebugUIPlugin.log(e);
205             }
206         }
207         fPathViewer.setEnabled(!def);
208         updateLaunchConfigurationDialog();
209     }
210     
211     /**
212      * Creates and returns a button
213      *
214      * @param parent parent widget
215      * @param label label
216      * @return Button
217      */

218     protected Button createPushButton(Composite parent, String JavaDoc label, FontMetrics fontMetrics) {
219         Button button = new Button(parent, SWT.PUSH);
220         button.setFont(parent.getFont());
221         button.setText(label);
222         GridData gd= getButtonGridData(button, fontMetrics);
223         button.setLayoutData(gd);
224         return button;
225     }
226     
227     private GridData getButtonGridData(Button button, FontMetrics fontMetrics) {
228         GridData gd= new GridData(GridData.FILL_HORIZONTAL | GridData.VERTICAL_ALIGN_BEGINNING);
229
230         int widthHint= Dialog.convertHorizontalDLUsToPixels(fontMetrics, IDialogConstants.BUTTON_WIDTH);
231         gd.widthHint= Math.max(widthHint, button.computeSize(SWT.DEFAULT, SWT.DEFAULT, true).x);
232     
233         return gd;
234     }
235
236     /* (non-Javadoc)
237      * @see org.eclipse.debug.ui.ILaunchConfigurationTab#initializeFrom(org.eclipse.debug.core.ILaunchConfiguration)
238      */

239     public void initializeFrom(ILaunchConfiguration config) {
240         boolean useDefault = true;
241         setErrorMessage(null);
242         try {
243             useDefault = config.getAttribute(IJavaLaunchConfigurationConstants.ATTR_DEFAULT_SOURCE_PATH, true);
244         } catch (CoreException e) {
245             JDIDebugUIPlugin.log(e);
246         }
247         if (config == getLaunchConfiguration()) {
248             // same as previously viewed launch config
249
if (!useDefault && !fDefaultButton.getSelection()) {
250                 // If an explicit classpath is being used, it must be the same as before.
251
// No need to refresh
252
setDirty(false);
253                 return;
254             }
255         }
256         setLaunchConfiguration(config);
257         fDefaultButton.setSelection(useDefault);
258         try {
259             IRuntimeClasspathEntry[] entries = JavaRuntime.computeUnresolvedSourceLookupPath(config);
260             fPathViewer.setEntries(entries);
261         } catch (CoreException e) {
262             setErrorMessage(e.getMessage());
263         }
264         fPathViewer.setEnabled(!useDefault);
265         fPathViewer.setLaunchConfiguration(config);
266         try {
267             fDuplicatesButton.setSelection(config.getAttribute(JavaUISourceLocator.ATTR_FIND_ALL_SOURCE_ELEMENTS, false));
268         } catch (CoreException e) {
269             JDIDebugUIPlugin.log(e);
270         }
271         setDirty(false);
272     }
273     
274     /* (non-Javadoc)
275      * @see org.eclipse.debug.ui.ILaunchConfigurationTab#performApply(org.eclipse.debug.core.ILaunchConfigurationWorkingCopy)
276      */

277     public void performApply(ILaunchConfigurationWorkingCopy configuration) {
278         if (isDirty()) {
279             boolean def = fDefaultButton.getSelection();
280             if (def) {
281                 configuration.setAttribute(IJavaLaunchConfigurationConstants.ATTR_DEFAULT_SOURCE_PATH, (String JavaDoc)null);
282                 configuration.setAttribute(IJavaLaunchConfigurationConstants.ATTR_SOURCE_PATH, (List JavaDoc)null);
283             } else {
284                 configuration.setAttribute(IJavaLaunchConfigurationConstants.ATTR_DEFAULT_SOURCE_PATH, def);
285                 try {
286                     IRuntimeClasspathEntry[] entries = fPathViewer.getEntries();
287                     List JavaDoc mementos = new ArrayList JavaDoc(entries.length);
288                     for (int i = 0; i < entries.length; i++) {
289                         mementos.add(entries[i].getMemento());
290                     }
291                     configuration.setAttribute(IJavaLaunchConfigurationConstants.ATTR_SOURCE_PATH, mementos);
292                 } catch (CoreException e) {
293                     JDIDebugUIPlugin.statusDialog(LauncherMessages.SourceLookupBlock_Unable_to_save_source_lookup_path_1, e.getStatus());
294                 }
295             }
296             boolean dup = fDuplicatesButton.getSelection();
297             if (dup) {
298                 configuration.setAttribute(JavaUISourceLocator.ATTR_FIND_ALL_SOURCE_ELEMENTS, true);
299             } else {
300                 configuration.setAttribute(JavaUISourceLocator.ATTR_FIND_ALL_SOURCE_ELEMENTS, (String JavaDoc)null);
301             }
302         }
303     }
304     
305     /**
306      * Returns the entries visible in the viewer
307      */

308     public IRuntimeClasspathEntry[] getEntries() {
309         return fPathViewer.getEntries();
310     }
311     
312     /**
313      * Sets the configuration associated with this source lookup
314      * block.
315      *
316      * @param configuration launch configuration
317      */

318     private void setLaunchConfiguration(ILaunchConfiguration configuration) {
319         fConfig = configuration;
320     }
321     
322     /**
323      * Sets the configuration associated with this source lookup
324      * block.
325      *
326      * @param configuration launch configuration
327      */

328     protected ILaunchConfiguration getLaunchConfiguration() {
329         return fConfig;
330     }
331
332     /* (non-Javadoc)
333      * @see org.eclipse.debug.ui.ILaunchConfigurationTab#getName()
334      */

335     public String JavaDoc getName() {
336         return LauncherMessages.SourceLookupBlock_Source_1;
337     }
338
339     /* (non-Javadoc)
340      * @see org.eclipse.debug.ui.ILaunchConfigurationTab#setDefaults(org.eclipse.debug.core.ILaunchConfigurationWorkingCopy)
341      */

342     public void setDefaults(ILaunchConfigurationWorkingCopy configuration) {
343         configuration.setAttribute(IJavaLaunchConfigurationConstants.ATTR_DEFAULT_SOURCE_PATH, (String JavaDoc)null);
344         configuration.setAttribute(IJavaLaunchConfigurationConstants.ATTR_SOURCE_PATH, (List JavaDoc)null);
345         configuration.setAttribute(JavaUISourceLocator.ATTR_FIND_ALL_SOURCE_ELEMENTS, (String JavaDoc)null);
346     }
347
348     /* (non-Javadoc)
349      * @see org.eclipse.debug.ui.AbstractLaunchConfigurationTab#updateLaunchConfigurationDialog()
350      */

351     protected void updateLaunchConfigurationDialog() {
352         if (getLaunchConfigurationDialog() != null) {
353             super.updateLaunchConfigurationDialog();
354         }
355     }
356
357     /* (non-Javadoc)
358      * @see org.eclipse.debug.ui.ILaunchConfigurationTab#dispose()
359      */

360     public void dispose() {
361         fPathViewer.removeEntriesChangedListener(this);
362         super.dispose();
363     }
364 }
365
Popular Tags