KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > debug > ui > CommonTab


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.debug.ui;
12
13  
14 import java.util.ArrayList JavaDoc;
15 import java.util.HashMap JavaDoc;
16 import java.util.Iterator JavaDoc;
17 import java.util.List JavaDoc;
18 import java.util.Map JavaDoc;
19
20 import org.eclipse.core.resources.IContainer;
21 import org.eclipse.core.resources.IFile;
22 import org.eclipse.core.resources.IProject;
23 import org.eclipse.core.resources.IResource;
24 import org.eclipse.core.resources.IWorkspaceRoot;
25 import org.eclipse.core.resources.ResourcesPlugin;
26 import org.eclipse.core.runtime.CoreException;
27 import org.eclipse.core.runtime.IPath;
28 import org.eclipse.core.runtime.Path;
29 import org.eclipse.core.variables.VariablesPlugin;
30 import org.eclipse.debug.core.DebugPlugin;
31 import org.eclipse.debug.core.ILaunchConfiguration;
32 import org.eclipse.debug.core.ILaunchConfigurationWorkingCopy;
33 import org.eclipse.debug.internal.ui.DebugUIPlugin;
34 import org.eclipse.debug.internal.ui.IDebugHelpContextIds;
35 import org.eclipse.debug.internal.ui.IInternalDebugUIConstants;
36 import org.eclipse.debug.internal.ui.SWTFactory;
37 import org.eclipse.debug.internal.ui.launchConfigurations.LaunchConfigurationManager;
38 import org.eclipse.debug.internal.ui.launchConfigurations.LaunchConfigurationsMessages;
39 import org.eclipse.debug.internal.ui.launchConfigurations.LaunchGroupExtension;
40 import org.eclipse.debug.internal.ui.launchConfigurations.LaunchHistory;
41 import org.eclipse.jface.dialogs.IDialogConstants;
42 import org.eclipse.jface.dialogs.IDialogSettings;
43 import org.eclipse.jface.resource.ImageDescriptor;
44 import org.eclipse.jface.viewers.CheckStateChangedEvent;
45 import org.eclipse.jface.viewers.CheckboxTableViewer;
46 import org.eclipse.jface.viewers.ICheckStateListener;
47 import org.eclipse.jface.viewers.ILabelProviderListener;
48 import org.eclipse.jface.viewers.IStructuredContentProvider;
49 import org.eclipse.jface.viewers.ITableLabelProvider;
50 import org.eclipse.jface.viewers.Viewer;
51 import org.eclipse.swt.SWT;
52 import org.eclipse.swt.events.ModifyEvent;
53 import org.eclipse.swt.events.ModifyListener;
54 import org.eclipse.swt.events.SelectionAdapter;
55 import org.eclipse.swt.events.SelectionEvent;
56 import org.eclipse.swt.events.SelectionListener;
57 import org.eclipse.swt.graphics.Image;
58 import org.eclipse.swt.layout.GridData;
59 import org.eclipse.swt.layout.GridLayout;
60 import org.eclipse.swt.widgets.Button;
61 import org.eclipse.swt.widgets.Combo;
62 import org.eclipse.swt.widgets.Composite;
63 import org.eclipse.swt.widgets.Control;
64 import org.eclipse.swt.widgets.FileDialog;
65 import org.eclipse.swt.widgets.Group;
66 import org.eclipse.swt.widgets.Shell;
67 import org.eclipse.swt.widgets.Text;
68 import org.eclipse.ui.PlatformUI;
69 import org.eclipse.ui.WorkbenchEncoding;
70 import org.eclipse.ui.dialogs.ContainerSelectionDialog;
71 import org.eclipse.ui.dialogs.ElementTreeSelectionDialog;
72 import org.eclipse.ui.ide.IDEEncoding;
73 import org.eclipse.ui.model.WorkbenchContentProvider;
74 import org.eclipse.ui.model.WorkbenchLabelProvider;
75 import org.eclipse.ui.views.navigator.ResourceComparator;
76
77 import com.ibm.icu.text.MessageFormat;
78
79 /**
80  * Launch configuration tab used to specify the location a launch configuration
81  * is stored in, whether it should appear in the favorites list, and perspective
82  * switching behavior for an associated launch.
83  * <p>
84  * Clients may instantiate this class. This class is not intended to be subclassed.
85  * </p>
86  * @since 2.0
87  */

88 public class CommonTab extends AbstractLaunchConfigurationTab {
89     
90     /**
91      * Provides a persistable dialog for selecting the shared project location
92      * @since 3.2
93      */

94     class SharedLocationSelectionDialog extends ContainerSelectionDialog {
95         private final String JavaDoc SETTINGS_ID = IDebugUIConstants.PLUGIN_ID + ".SHARED_LAUNCH_CONFIGURATON_DIALOG"; //$NON-NLS-1$
96

97         public SharedLocationSelectionDialog(Shell parentShell, IContainer initialRoot, boolean allowNewContainerName, String JavaDoc message) {
98             super(parentShell, initialRoot, allowNewContainerName, message);
99         }
100
101         protected IDialogSettings getDialogBoundsSettings() {
102             IDialogSettings settings = DebugUIPlugin.getDefault().getDialogSettings();
103             IDialogSettings section = settings.getSection(SETTINGS_ID);
104             if (section == null) {
105                 section = settings.addNewSection(SETTINGS_ID);
106             }
107             return section;
108         }
109     }
110     
111     private static final String JavaDoc EMPTY_STRING = ""; //$NON-NLS-1$
112

113     /**
114      * This attribute exists solely for the purpose of making sure that invalid shared locations
115      * can be revertable. This attirbute is not saveable and will never appear in a saved
116      * launch configuration.
117      * @since 3.3
118      */

119     private static final String JavaDoc BAD_CONTAINER = "bad_container_name"; //$NON-NLS-1$
120

121     // Local/shared UI widgets
122
private Button fLocalRadioButton;
123     private Button fSharedRadioButton;
124     private Text fSharedLocationText;
125     private Button fSharedLocationButton;
126     private Button fLaunchInBackgroundButton;
127     private Button fDefaultEncodingButton;
128     private Button fAltEncodingButton;
129     private Combo fEncodingCombo;
130     private Button fConsoleOutput;
131     private Button fFileOutput;
132     private Button fFileBrowse;
133     private Text fFileText;
134     private Button fVariables;
135     private Button fAppend;
136     private Button fWorkspaceBrowse;
137     
138     /**
139      * Check box list for specifying favorites
140      */

141     private CheckboxTableViewer fFavoritesTable;
142             
143     /**
144      * Modify listener that simply updates the owning launch configuration dialog.
145      */

146     private ModifyListener fBasicModifyListener = new ModifyListener() {
147         public void modifyText(ModifyEvent evt) {
148             updateLaunchConfigurationDialog();
149         }
150     };
151     
152     /* (non-Javadoc)
153      * @see org.eclipse.debug.ui.ILaunchConfigurationTab#createControl(org.eclipse.swt.widgets.Composite)
154      */

155     public void createControl(Composite parent) {
156         Composite comp = new Composite(parent, SWT.NONE);
157         setControl(comp);
158         PlatformUI.getWorkbench().getHelpSystem().setHelp(getControl(), IDebugHelpContextIds.LAUNCH_CONFIGURATION_DIALOG_COMMON_TAB);
159         comp.setLayout(new GridLayout(2, true));
160         comp.setFont(parent.getFont());
161         
162         createSharedConfigComponent(comp);
163         createFavoritesComponent(comp);
164         createEncodingComponent(comp);
165         createOutputCaptureComponent(comp);
166         createLaunchInBackgroundComponent(comp);
167     }
168     
169     /**
170      * Creates the favorites control
171      * @param parent the parent composite to add this one to
172      * @since 3.2
173      */

174     private void createFavoritesComponent(Composite parent) {
175         Group favComp = SWTFactory.createGroup(parent, LaunchConfigurationsMessages.CommonTab_Display_in_favorites_menu__10, 1, 1, GridData.FILL_BOTH);
176         fFavoritesTable = CheckboxTableViewer.newCheckList(favComp, SWT.CHECK | SWT.BORDER | SWT.MULTI | SWT.FULL_SELECTION);
177         Control table = fFavoritesTable.getControl();
178         GridData gd = new GridData(GridData.FILL_BOTH);
179         table.setLayoutData(gd);
180         table.setFont(parent.getFont());
181         fFavoritesTable.setContentProvider(new FavoritesContentProvider());
182         fFavoritesTable.setLabelProvider(new FavoritesLabelProvider());
183         fFavoritesTable.addCheckStateListener(new ICheckStateListener() {
184                 public void checkStateChanged(CheckStateChangedEvent event) {
185                     updateLaunchConfigurationDialog();
186                 }
187             });
188     }
189     
190     /**
191      * Creates the shared config component
192      * @param parent the parent composite to add this component to
193      * @since 3.2
194      */

195     private void createSharedConfigComponent(Composite parent) {
196         Group group = SWTFactory.createGroup(parent, LaunchConfigurationsMessages.CommonTab_0, 3, 2, GridData.FILL_HORIZONTAL);
197         Composite comp = SWTFactory.createComposite(group, parent.getFont(), 3, 3, GridData.FILL_BOTH, 0, 0);
198         fLocalRadioButton = createRadioButton(comp, LaunchConfigurationsMessages.CommonTab_L_ocal_3);
199         GridData gd = new GridData();
200         gd.horizontalSpan = 3;
201         fLocalRadioButton.setLayoutData(gd);
202         fSharedRadioButton = createRadioButton(comp, LaunchConfigurationsMessages.CommonTab_S_hared_4);
203         fSharedRadioButton.addSelectionListener(new SelectionAdapter() {
204             public void widgetSelected(SelectionEvent evt) {
205                 handleSharedRadioButtonSelected();
206             }
207         });
208         fSharedLocationText = SWTFactory.createSingleText(comp, 1);
209         fSharedLocationText.addModifyListener(fBasicModifyListener);
210         fSharedLocationButton = createPushButton(comp, LaunchConfigurationsMessages.CommonTab__Browse_6, null);
211         fSharedLocationButton.addSelectionListener(new SelectionAdapter() {
212             public void widgetSelected(SelectionEvent evt) {
213                 handleSharedLocationButtonSelected();
214             }
215         });
216
217         fLocalRadioButton.setSelection(true);
218         setSharedEnabled(false);
219     }
220     
221     /**
222      * Creates the component set for the capture output composite
223      * @param parent the parent to add this component to
224      */

225     private void createOutputCaptureComponent(Composite parent) {
226         Group group = SWTFactory.createGroup(parent, LaunchConfigurationsMessages.CommonTab_4, 5, 2, GridData.FILL_HORIZONTAL);
227         Composite comp = SWTFactory.createComposite(group, 5, 5, GridData.FILL_BOTH);
228         GridLayout ld = (GridLayout)comp.getLayout();
229         ld.marginWidth = 1;
230         ld.marginHeight = 1;
231         fConsoleOutput = createCheckButton(comp, LaunchConfigurationsMessages.CommonTab_5);
232         GridData gd = new GridData(SWT.BEGINNING, SWT.NORMAL, true, false);
233         gd.horizontalSpan = 5;
234         fConsoleOutput.setLayoutData(gd);
235         fConsoleOutput.addSelectionListener(new SelectionAdapter() {
236             public void widgetSelected(SelectionEvent e) {
237                 updateLaunchConfigurationDialog();
238             }
239         });
240         
241         fFileOutput = createCheckButton(comp, LaunchConfigurationsMessages.CommonTab_6);
242         fFileOutput.setLayoutData(new GridData(SWT.BEGINNING, SWT.NORMAL, false, false));
243         fFileOutput.addSelectionListener(new SelectionAdapter() {
244             public void widgetSelected(SelectionEvent e) {
245                 enableOuputCaptureWidgets(fFileOutput.getSelection());
246                 updateLaunchConfigurationDialog();
247             }
248         });
249         fFileText = SWTFactory.createSingleText(comp, 4);
250         fFileText.addModifyListener(fBasicModifyListener);
251         
252         Composite bcomp = SWTFactory.createComposite(comp, 3, 5, GridData.HORIZONTAL_ALIGN_END);
253         ld = (GridLayout)bcomp.getLayout();
254         ld.marginHeight = 1;
255         ld.marginWidth = 0;
256         fWorkspaceBrowse = createPushButton(bcomp, LaunchConfigurationsMessages.CommonTab_12, null);
257         fWorkspaceBrowse.addSelectionListener(new SelectionAdapter() {
258             public void widgetSelected(SelectionEvent e) {
259                 ElementTreeSelectionDialog dialog = new ElementTreeSelectionDialog(getShell(), new WorkbenchLabelProvider(), new WorkbenchContentProvider());
260                 dialog.setTitle(LaunchConfigurationsMessages.CommonTab_13);
261                 dialog.setMessage(LaunchConfigurationsMessages.CommonTab_14);
262                 dialog.setInput(ResourcesPlugin.getWorkspace().getRoot());
263                 dialog.setComparator(new ResourceComparator(ResourceComparator.NAME));
264                 if (dialog.open() == IDialogConstants.OK_ID) {
265                     IResource resource = (IResource) dialog.getFirstResult();
266                     String JavaDoc arg = resource.getFullPath().toString();
267                     String JavaDoc fileLoc = VariablesPlugin.getDefault().getStringVariableManager().generateVariableExpression("workspace_loc", arg); //$NON-NLS-1$
268
fFileText.setText(fileLoc);
269                 }
270             }
271         });
272         fFileBrowse = createPushButton(bcomp, LaunchConfigurationsMessages.CommonTab_7, null);
273         fFileBrowse.addSelectionListener(new SelectionAdapter() {
274             public void widgetSelected(SelectionEvent e) {
275                 String JavaDoc filePath = fFileText.getText();
276                 FileDialog dialog = new FileDialog(getShell(), SWT.SAVE);
277                 filePath = dialog.open();
278                 if (filePath != null) {
279                     fFileText.setText(filePath);
280                 }
281             }
282         });
283         fVariables = createPushButton(bcomp, LaunchConfigurationsMessages.CommonTab_9, null);
284         fVariables.addSelectionListener(new SelectionListener() {
285             public void widgetSelected(SelectionEvent e) {
286                 StringVariableSelectionDialog dialog = new StringVariableSelectionDialog(getShell());
287                 dialog.open();
288                 String JavaDoc variable = dialog.getVariableExpression();
289                 if (variable != null) {
290                     fFileText.insert(variable);
291                 }
292             }
293             public void widgetDefaultSelected(SelectionEvent e) {}
294         });
295         fAppend = createCheckButton(comp, LaunchConfigurationsMessages.CommonTab_11);
296         gd = new GridData(SWT.LEFT, SWT.TOP, true, false);
297         gd.horizontalSpan = 4;
298         fAppend.setLayoutData(gd);
299         fAppend.addSelectionListener(new SelectionAdapter() {
300             public void widgetSelected(SelectionEvent e) {
301                 updateLaunchConfigurationDialog();
302             }
303         });
304     }
305
306     /**
307      * Enables or disables the output capture widgets based on the the specified enablement
308      * @param enable if the output capture widgets should be enabled or not
309      * @since 3.2
310      */

311     private void enableOuputCaptureWidgets(boolean enable) {
312         fFileText.setEnabled(enable);
313         fFileBrowse.setEnabled(enable);
314         fWorkspaceBrowse.setEnabled(enable);
315         fVariables.setEnabled(enable);
316         fAppend.setEnabled(enable);
317     }
318     
319     /**
320      * Creates the encoding component
321      * @param parent the parent to add this composite to
322      */

323     private void createEncodingComponent(Composite parent) {
324         List JavaDoc allEncodings = IDEEncoding.getIDEEncodings();
325         String JavaDoc defaultEncoding = WorkbenchEncoding.getWorkbenchDefaultEncoding();
326         Group group = SWTFactory.createGroup(parent, LaunchConfigurationsMessages.CommonTab_1, 2, 1, GridData.FILL_BOTH);
327         
328         fDefaultEncodingButton = createRadioButton(group, MessageFormat.format(LaunchConfigurationsMessages.CommonTab_2, new String JavaDoc[]{defaultEncoding}));
329         GridData gd = new GridData(SWT.BEGINNING, SWT.NORMAL, true, false);
330         gd.horizontalSpan = 2;
331         fDefaultEncodingButton.setLayoutData(gd);
332         
333         fAltEncodingButton = createRadioButton(group, LaunchConfigurationsMessages.CommonTab_3);
334         fAltEncodingButton.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_BEGINNING));
335         
336         fEncodingCombo = new Combo(group, SWT.READ_ONLY);
337         fEncodingCombo.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
338         fEncodingCombo.setFont(parent.getFont());
339         String JavaDoc[] encodingArray = (String JavaDoc[]) allEncodings.toArray(new String JavaDoc[0]);
340         fEncodingCombo.setItems(encodingArray);
341         if (encodingArray.length > 0) {
342             fEncodingCombo.select(0);
343         }
344         SelectionListener listener = new SelectionAdapter() {
345             public void widgetSelected(SelectionEvent e) {
346                 updateLaunchConfigurationDialog();
347                 fEncodingCombo.setEnabled(fAltEncodingButton.getSelection() == true);
348             }
349         };
350         fAltEncodingButton.addSelectionListener(listener);
351         fDefaultEncodingButton.addSelectionListener(listener);
352         fEncodingCombo.addSelectionListener(listener);
353     }
354
355     /**
356      * Creates the controls needed to edit the launch in background
357      * attribute of an external tool
358      *
359      * @param parent the composite to create the controls in
360      */

361     protected void createLaunchInBackgroundComponent(Composite parent) {
362         fLaunchInBackgroundButton = createCheckButton(parent, LaunchConfigurationsMessages.CommonTab_10);
363         GridData data = new GridData(GridData.HORIZONTAL_ALIGN_FILL);
364         data.horizontalSpan = 2;
365         fLaunchInBackgroundButton.setLayoutData(data);
366         fLaunchInBackgroundButton.setFont(parent.getFont());
367         fLaunchInBackgroundButton.addSelectionListener(new SelectionAdapter() {
368             public void widgetSelected(SelectionEvent e) {
369                 updateLaunchConfigurationDialog();
370             }
371         });
372     }
373
374     /**
375      * handles the shared radio button being selected
376      */

377     private void handleSharedRadioButtonSelected() {
378         setSharedEnabled(isShared());
379         updateLaunchConfigurationDialog();
380     }
381     
382     /**
383      * Sets the widgets for specifying that a launch configuration is to be shared to the enable value
384      * @param enable the enabled value for
385      */

386     private void setSharedEnabled(boolean enable) {
387         fSharedLocationText.setEnabled(enable);
388         fSharedLocationButton.setEnabled(enable);
389     }
390     
391     private String JavaDoc getDefaultSharedConfigLocation(ILaunchConfiguration config) {
392         String JavaDoc path = EMPTY_STRING;
393         try {
394             IResource[] res = config.getMappedResources();
395             if(res != null) {
396                 IProject proj;
397                 for (int i = 0; i < res.length; i++) {
398                     proj = res[i].getProject();
399                     if(proj.isAccessible()) {
400                         return proj.getFullPath().toOSString();
401                     }
402                 }
403             }
404         }
405         catch (CoreException e) {DebugUIPlugin.log(e);}
406         return path;
407     }
408     
409     /**
410      * if the shared radio button is selected, indicating that the launch configuration is to be shared
411      * @return true if the radio button is selected, false otherwise
412      */

413     private boolean isShared() {
414         return fSharedRadioButton.getSelection();
415     }
416     
417     /**
418      * Handles the shared location button being selected
419      */

420     private void handleSharedLocationButtonSelected() {
421         String JavaDoc currentContainerString = fSharedLocationText.getText();
422         IContainer currentContainer = getContainer(currentContainerString);
423         SharedLocationSelectionDialog dialog = new SharedLocationSelectionDialog(getShell(),
424                    currentContainer,
425                    false,
426                    LaunchConfigurationsMessages.CommonTab_Select_a_location_for_the_launch_configuration_13);
427         dialog.showClosedProjects(false);
428         dialog.open();
429         Object JavaDoc[] results = dialog.getResult();
430         if ((results != null) && (results.length > 0) && (results[0] instanceof IPath)) {
431             IPath path = (IPath)results[0];
432             String JavaDoc containerName = path.toOSString();
433             fSharedLocationText.setText(containerName);
434         }
435     }
436     
437     /**
438      * gets the container form the specified path
439      * @param path the path to get the container from
440      * @return the container for the specified path or null if one cannot be determined
441      */

442     private IContainer getContainer(String JavaDoc path) {
443         Path containerPath = new Path(path);
444         return (IContainer) getWorkspaceRoot().findMember(containerPath);
445     }
446     
447     /* (non-Javadoc)
448      * @see org.eclipse.debug.ui.ILaunchConfigurationTab#initializeFrom(org.eclipse.debug.core.ILaunchConfiguration)
449      */

450     public void initializeFrom(ILaunchConfiguration configuration) {
451         boolean isShared = !configuration.isLocal();
452         fSharedRadioButton.setSelection(isShared);
453         fLocalRadioButton.setSelection(!isShared);
454         setSharedEnabled(isShared);
455         fSharedLocationText.setText(getDefaultSharedConfigLocation(configuration));
456         if(isShared) {
457             String JavaDoc containerName = EMPTY_STRING;
458             IFile file = configuration.getFile();
459             if (file != null) {
460                 IContainer parent = file.getParent();
461                 if (parent != null) {
462                     containerName = parent.getFullPath().toOSString();
463                 }
464             }
465             fSharedLocationText.setText(containerName);
466         }
467         updateFavoritesFromConfig(configuration);
468         updateLaunchInBackground(configuration);
469         updateEncoding(configuration);
470         updateConsoleOutput(configuration);
471     }
472     
473     /**
474      * Updates the console output form the local configuration
475      * @param configuration the local configuration
476      */

477     private void updateConsoleOutput(ILaunchConfiguration configuration) {
478         boolean outputToConsole = true;
479         String JavaDoc outputFile = null;
480         boolean append = false;
481         
482         try {
483             outputToConsole = configuration.getAttribute(IDebugUIConstants.ATTR_CAPTURE_IN_CONSOLE, true);
484             outputFile = configuration.getAttribute(IDebugUIConstants.ATTR_CAPTURE_IN_FILE, (String JavaDoc)null);
485             append = configuration.getAttribute(IDebugUIConstants.ATTR_APPEND_TO_FILE, false);
486         } catch (CoreException e) {
487         }
488         
489         fConsoleOutput.setSelection(outputToConsole);
490         fAppend.setSelection(append);
491         boolean haveOutputFile= outputFile != null;
492         if (haveOutputFile) {
493             fFileText.setText(outputFile);
494         }
495         fFileOutput.setSelection(haveOutputFile);
496         enableOuputCaptureWidgets(haveOutputFile);
497     }
498
499     /**
500      * Updates the launch on background check button
501      * @param configuration the local launch configuration
502      */

503     protected void updateLaunchInBackground(ILaunchConfiguration configuration) {
504         fLaunchInBackgroundButton.setSelection(isLaunchInBackground(configuration));
505     }
506     
507     /**
508      * Updates the encoding
509      * @param configuration the local configuration
510      */

511     private void updateEncoding(ILaunchConfiguration configuration) {
512         String JavaDoc encoding = null;
513         try {
514             encoding = configuration.getAttribute(DebugPlugin.ATTR_CONSOLE_ENCODING, (String JavaDoc)null);
515         } catch (CoreException e) {
516         }
517         
518         if (encoding != null) {
519             fAltEncodingButton.setSelection(true);
520             fDefaultEncodingButton.setSelection(false);
521             fEncodingCombo.setText(encoding);
522             fEncodingCombo.setEnabled(true);
523         } else {
524             fDefaultEncodingButton.setSelection(true);
525             fAltEncodingButton.setSelection(false);
526             fEncodingCombo.setEnabled(false);
527         }
528     }
529     
530     /**
531      * Returns whether the given configuration should be launched in the background.
532      *
533      * @param configuration the configuration
534      * @return whether the configuration is configured to launch in the background
535      */

536     public static boolean isLaunchInBackground(ILaunchConfiguration configuration) {
537         boolean launchInBackground= true;
538         try {
539             launchInBackground= configuration.getAttribute(IDebugUIConstants.ATTR_LAUNCH_IN_BACKGROUND, true);
540         } catch (CoreException ce) {
541             DebugUIPlugin.log(ce);
542         }
543         return launchInBackground;
544     }
545
546     /**
547      * Updates the favorites selections from the local configuration
548      * @param config the local configuration
549      */

550     private void updateFavoritesFromConfig(ILaunchConfiguration config) {
551         fFavoritesTable.setInput(config);
552         fFavoritesTable.setCheckedElements(new Object JavaDoc[]{});
553         try {
554             List JavaDoc groups = config.getAttribute(IDebugUIConstants.ATTR_FAVORITE_GROUPS, new ArrayList JavaDoc());
555             if (groups.isEmpty()) {
556                 // check old attributes for backwards compatible
557
if (config.getAttribute(IDebugUIConstants.ATTR_DEBUG_FAVORITE, false)) {
558                     groups.add(IDebugUIConstants.ID_DEBUG_LAUNCH_GROUP);
559                 }
560                 if (config.getAttribute(IDebugUIConstants.ATTR_RUN_FAVORITE, false)) {
561                     groups.add(IDebugUIConstants.ID_RUN_LAUNCH_GROUP);
562                 }
563             }
564             if (!groups.isEmpty()) {
565                 List JavaDoc list = new ArrayList JavaDoc();
566                 Iterator JavaDoc iterator = groups.iterator();
567                 while (iterator.hasNext()) {
568                     String JavaDoc id = (String JavaDoc)iterator.next();
569                     LaunchGroupExtension extension = getLaunchConfigurationManager().getLaunchGroup(id);
570                     list.add(extension);
571                 }
572                 fFavoritesTable.setCheckedElements(list.toArray());
573             }
574         } catch (CoreException e) {
575             DebugUIPlugin.log(e);
576         }
577     }
578
579     /**
580      * Updates the configuration form the local shared config working copy
581      * @param config the local shared config working copy
582      */

583     private void updateConfigFromLocalShared(ILaunchConfigurationWorkingCopy config) {
584         if (isShared()) {
585             String JavaDoc containerPathString = fSharedLocationText.getText();
586             IContainer container = getContainer(containerPathString);
587             if(container == null) {
588                 //we need to force an attribute to allow the invalid container path to be revertable
589
config.setAttribute(BAD_CONTAINER, containerPathString);
590             }
591             else {
592                 config.setContainer(container);
593             }
594         } else {
595             config.setContainer(null);
596         }
597     }
598     
599     /**
600      * Convenience accessor
601      */

602     protected LaunchConfigurationManager getLaunchConfigurationManager() {
603         return DebugUIPlugin.getDefault().getLaunchConfigurationManager();
604     }
605     
606     /**
607      * Update the favorite settings.
608      *
609      * NOTE: set to <code>null</code> instead of <code>false</code> for backwards compatibility
610      * when comparing if content is equal, since 'false' is default
611      * and will be missing for older configurations.
612      */

613     private void updateConfigFromFavorites(ILaunchConfigurationWorkingCopy config) {
614         try {
615             Object JavaDoc[] checked = fFavoritesTable.getCheckedElements();
616             boolean debug = config.getAttribute(IDebugUIConstants.ATTR_DEBUG_FAVORITE, false);
617             boolean run = config.getAttribute(IDebugUIConstants.ATTR_RUN_FAVORITE, false);
618             if (debug || run) {
619                 // old attributes
620
List JavaDoc groups = new ArrayList JavaDoc();
621                 int num = 0;
622                 if (debug) {
623                     groups.add(getLaunchConfigurationManager().getLaunchGroup(IDebugUIConstants.ID_DEBUG_LAUNCH_GROUP));
624                     num++;
625                 }
626                 if (run) {
627                     num++;
628                     groups.add(getLaunchConfigurationManager().getLaunchGroup(IDebugUIConstants.ID_DEBUG_LAUNCH_GROUP));
629                 }
630                 // see if there are any changes
631
if (num == checked.length) {
632                     boolean different = false;
633                     for (int i = 0; i < checked.length; i++) {
634                         if (!groups.contains(checked[i])) {
635                             different = true;
636                             break;
637                         }
638                     }
639                     if (!different) {
640                         return;
641                     }
642                 }
643             }
644             config.setAttribute(IDebugUIConstants.ATTR_DEBUG_FAVORITE, (String JavaDoc)null);
645             config.setAttribute(IDebugUIConstants.ATTR_RUN_FAVORITE, (String JavaDoc)null);
646             List JavaDoc groups = null;
647             for (int i = 0; i < checked.length; i++) {
648                 LaunchGroupExtension group = (LaunchGroupExtension)checked[i];
649                 if (groups == null) {
650                     groups = new ArrayList JavaDoc();
651                 }
652                 groups.add(group.getIdentifier());
653             }
654             config.setAttribute(IDebugUIConstants.ATTR_FAVORITE_GROUPS, groups);
655         } catch (CoreException e) {
656             DebugUIPlugin.log(e);
657         }
658     }
659     
660     /**
661      * Convenience method for getting the workspace root.
662      */

663     private IWorkspaceRoot getWorkspaceRoot() {
664         return ResourcesPlugin.getWorkspace().getRoot();
665     }
666     
667     /* (non-Javadoc)
668      * @see org.eclipse.debug.ui.ILaunchConfigurationTab#isValid(org.eclipse.debug.core.ILaunchConfiguration)
669      */

670     public boolean isValid(ILaunchConfiguration config) {
671         setMessage(null);
672         setErrorMessage(null);
673         
674         return validateLocalShared() && validateRedirectFile() && validateEncoding();
675     }
676     
677     /**
678      * validates the encoding selection
679      * @return true if the validate encoding is allowable, false otherwise
680      */

681     private boolean validateEncoding() {
682         if (fAltEncodingButton.getSelection()) {
683             if (fEncodingCombo.getSelectionIndex() == -1) {
684                 setErrorMessage(LaunchConfigurationsMessages.CommonTab_No_Encoding_Selected);
685                 return false;
686             }
687         }
688         return true;
689     }
690
691     /**
692      * Validates if the redirect file is valid
693      * @return true if the filename is not zero, false otherwise
694      */

695     private boolean validateRedirectFile() {
696         if(fFileOutput.getSelection()) {
697             int len = fFileText.getText().trim().length();
698             if (len == 0) {
699                 setErrorMessage(LaunchConfigurationsMessages.CommonTab_8);
700                 return false;
701             }
702         }
703         return true;
704     }
705
706     /**
707      * validates the local shared config file location
708      * @return true if the local shared file exists, false otherwise
709      */

710     private boolean validateLocalShared() {
711         if (isShared()) {
712             String JavaDoc path = fSharedLocationText.getText().trim();
713             IContainer container = getContainer(path);
714             if (container == null || container.equals(ResourcesPlugin.getWorkspace().getRoot())) {
715                 setErrorMessage(LaunchConfigurationsMessages.CommonTab_Invalid_shared_configuration_location_14);
716                 return false;
717             } else if (!container.getProject().isOpen()) {
718                 setErrorMessage(LaunchConfigurationsMessages.CommonTab_Cannot_save_launch_configuration_in_a_closed_project__1);
719                 return false;
720             }
721         }
722         return true;
723     }
724
725     /* (non-Javadoc)
726      * @see org.eclipse.debug.ui.ILaunchConfigurationTab#setDefaults(org.eclipse.debug.core.ILaunchConfigurationWorkingCopy)
727      */

728     public void setDefaults(ILaunchConfigurationWorkingCopy config) {
729         config.setContainer(null);
730         setAttribute(IDebugUIConstants.ATTR_LAUNCH_IN_BACKGROUND, config, true, true);
731     }
732
733     /* (non-Javadoc)
734      * @see org.eclipse.debug.ui.ILaunchConfigurationTab#performApply(org.eclipse.debug.core.ILaunchConfigurationWorkingCopy)
735      */

736     public void performApply(ILaunchConfigurationWorkingCopy configuration) {
737         updateConfigFromLocalShared(configuration);
738         updateConfigFromFavorites(configuration);
739         setAttribute(IDebugUIConstants.ATTR_LAUNCH_IN_BACKGROUND, configuration, fLaunchInBackgroundButton.getSelection(), true);
740         String JavaDoc encoding = null;
741         if(fAltEncodingButton.getSelection()) {
742             encoding = fEncodingCombo.getText();
743         }
744         configuration.setAttribute(DebugPlugin.ATTR_CONSOLE_ENCODING, encoding);
745         boolean captureOutput = false;
746         if (fConsoleOutput.getSelection()) {
747             captureOutput = true;
748             configuration.setAttribute(IDebugUIConstants.ATTR_CAPTURE_IN_CONSOLE, (String JavaDoc)null);
749         } else {
750             configuration.setAttribute(IDebugUIConstants.ATTR_CAPTURE_IN_CONSOLE, false);
751         }
752         if (fFileOutput.getSelection()) {
753             captureOutput = true;
754             String JavaDoc file = fFileText.getText();
755             configuration.setAttribute(IDebugUIConstants.ATTR_CAPTURE_IN_FILE, file);
756             if(fAppend.getSelection()) {
757                 configuration.setAttribute(IDebugUIConstants.ATTR_APPEND_TO_FILE, true);
758             } else {
759                 configuration.setAttribute(IDebugUIConstants.ATTR_APPEND_TO_FILE, (String JavaDoc)null);
760             }
761         } else {
762             configuration.setAttribute(IDebugUIConstants.ATTR_CAPTURE_IN_FILE, (String JavaDoc)null);
763         }
764         
765         if (!captureOutput) {
766             configuration.setAttribute(DebugPlugin.ATTR_CAPTURE_OUTPUT, false);
767         } else {
768             configuration.setAttribute(DebugPlugin.ATTR_CAPTURE_OUTPUT, (String JavaDoc)null);
769         }
770     }
771
772     /* (non-Javadoc)
773      * @see org.eclipse.debug.ui.ILaunchConfigurationTab#getName()
774      */

775     public String JavaDoc getName() {
776         return LaunchConfigurationsMessages.CommonTab__Common_15;
777     }
778     
779     /**
780      * @see org.eclipse.debug.ui.AbstractLaunchConfigurationTab#getId()
781      *
782      * @since 3.3
783      */

784     public String JavaDoc getId() {
785         return "org.eclipse.debug.ui.commonTab"; //$NON-NLS-1$
786
}
787     
788     /* (non-Javadoc)
789      * @see org.eclipse.debug.ui.ILaunchConfigurationTab#canSave()
790      */

791     public boolean canSave() {
792         return validateLocalShared();
793     }
794
795     /* (non-Javadoc)
796      * @see org.eclipse.debug.ui.ILaunchConfigurationTab#getImage()
797      */

798     public Image getImage() {
799         return DebugUITools.getImage(IInternalDebugUIConstants.IMG_OBJS_COMMON_TAB);
800     }
801     
802     /* (non-Javadoc)
803      * @see org.eclipse.debug.ui.ILaunchConfigurationTab#activated(org.eclipse.debug.core.ILaunchConfigurationWorkingCopy)
804      */

805     public void activated(ILaunchConfigurationWorkingCopy workingCopy) {}
806
807     /* (non-Javadoc)
808      * @see org.eclipse.debug.ui.ILaunchConfigurationTab#deactivated(org.eclipse.debug.core.ILaunchConfigurationWorkingCopy)
809      */

810     public void deactivated(ILaunchConfigurationWorkingCopy workingCopy) {}
811
812     /**
813      * Content provider for the favorites table
814      */

815     class FavoritesContentProvider implements IStructuredContentProvider {
816
817         public Object JavaDoc[] getElements(Object JavaDoc inputElement) {
818             ILaunchGroup[] groups = DebugUITools.getLaunchGroups();
819             List JavaDoc possibleGroups = new ArrayList JavaDoc();
820             ILaunchConfiguration configuration = (ILaunchConfiguration)inputElement;
821             for (int i = 0; i < groups.length; i++) {
822                 ILaunchGroup extension = groups[i];
823                 LaunchHistory history = getLaunchConfigurationManager().getLaunchHistory(extension.getIdentifier());
824                 if (history != null && history.accepts(configuration)) {
825                     possibleGroups.add(extension);
826                 }
827             }
828             return possibleGroups.toArray();
829         }
830
831         public void dispose() {}
832
833         public void inputChanged(Viewer viewer, Object JavaDoc oldInput, Object JavaDoc newInput) {}
834
835     }
836     
837     /**
838      * Provides the labels for the favorites table
839      *
840      */

841     class FavoritesLabelProvider implements ITableLabelProvider {
842         
843         private Map JavaDoc fImages = new HashMap JavaDoc();
844
845         public Image getColumnImage(Object JavaDoc element, int columnIndex) {
846             Image image = (Image)fImages.get(element);
847             if (image == null) {
848                 ImageDescriptor descriptor = ((LaunchGroupExtension)element).getImageDescriptor();
849                 if (descriptor != null) {
850                     image = descriptor.createImage();
851                     fImages.put(element, image);
852                 }
853             }
854             return image;
855         }
856
857         public String JavaDoc getColumnText(Object JavaDoc element, int columnIndex) {
858             String JavaDoc label = ((LaunchGroupExtension)element).getLabel();
859             return DebugUIPlugin.removeAccelerators(label);
860         }
861
862         public void addListener(ILabelProviderListener listener) {}
863
864         public void dispose() {
865             Iterator JavaDoc images = fImages.values().iterator();
866             while (images.hasNext()) {
867                 Image image = (Image)images.next();
868                 image.dispose();
869             }
870         }
871
872         public boolean isLabelProperty(Object JavaDoc element, String JavaDoc property) {return false;}
873
874         public void removeListener(ILabelProviderListener listener) {}
875     }
876
877 }
878
Popular Tags