KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > debug > internal > ui > sourcelookup > SourceLookupPanel


1 /*******************************************************************************
2  * Copyright (c) 2003, 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  * QNX Software Systems - Mikhail Khodjaiants - Bug 89748
11  *******************************************************************************/

12 package org.eclipse.debug.internal.ui.sourcelookup;
13
14 import java.util.ArrayList JavaDoc;
15 import java.util.Iterator JavaDoc;
16 import java.util.List JavaDoc;
17
18 import org.eclipse.core.runtime.CoreException;
19 import org.eclipse.debug.core.ILaunchConfiguration;
20 import org.eclipse.debug.core.ILaunchConfigurationWorkingCopy;
21 import org.eclipse.debug.core.model.ISourceLocator;
22 import org.eclipse.debug.core.sourcelookup.AbstractSourceLookupDirector;
23 import org.eclipse.debug.core.sourcelookup.ISourceContainer;
24 import org.eclipse.debug.core.sourcelookup.ISourceLookupDirector;
25 import org.eclipse.debug.core.sourcelookup.containers.DefaultSourceContainer;
26 import org.eclipse.debug.internal.ui.DebugUIPlugin;
27 import org.eclipse.debug.ui.AbstractLaunchConfigurationTab;
28 import org.eclipse.debug.ui.sourcelookup.WorkingSetSourceContainer;
29 import org.eclipse.jface.dialogs.Dialog;
30 import org.eclipse.jface.dialogs.IDialogConstants;
31 import org.eclipse.jface.util.IPropertyChangeListener;
32 import org.eclipse.jface.util.PropertyChangeEvent;
33 import org.eclipse.swt.SWT;
34 import org.eclipse.swt.events.SelectionAdapter;
35 import org.eclipse.swt.events.SelectionEvent;
36 import org.eclipse.swt.graphics.Font;
37 import org.eclipse.swt.graphics.FontMetrics;
38 import org.eclipse.swt.graphics.GC;
39 import org.eclipse.swt.layout.GridData;
40 import org.eclipse.swt.layout.GridLayout;
41 import org.eclipse.swt.widgets.Button;
42 import org.eclipse.swt.widgets.Composite;
43 import org.eclipse.swt.widgets.Label;
44 import org.eclipse.ui.IWorkingSet;
45 import org.eclipse.ui.IWorkingSetManager;
46
47 /**
48  * The panel that contains the list of source containers.
49  *
50  * @since 3.0
51  */

52 public class SourceLookupPanel extends AbstractLaunchConfigurationTab implements IPropertyChangeListener {
53     //the configuration being edited
54
protected ILaunchConfiguration fConfig;
55     //the viewer displaying the containers
56
protected SourceContainerViewer fPathViewer;
57     //the duplicates checkbox
58
protected Button fDuplicatesButton;
59     //the source actions - up, down, add, remove, restore
60
protected List JavaDoc fActions = new ArrayList JavaDoc(6);
61     //the director that will be used by the tab to manage/store the containers
62
protected ISourceLookupDirector fLocator;
63     
64     protected AddContainerAction fAddAction;
65     protected EditContainerAction fEditAction;
66     protected RestoreDefaultAction fRestoreDefaultAction;
67     
68     /**
69      * Creates and returns the source lookup control.
70      *
71      * @param parent the parent widget of this control
72      */

73     public void createControl(Composite parent) {
74         Font font = parent.getFont();
75         
76         Composite comp = new Composite(parent, SWT.NONE);
77         GridLayout topLayout = new GridLayout();
78         topLayout.numColumns = 2;
79         comp.setLayout(topLayout);
80         GridData gd = new GridData(GridData.FILL_BOTH);
81         comp.setLayoutData(gd);
82         
83         Label viewerLabel = new Label(comp, SWT.LEFT);
84         viewerLabel.setText(
85                 SourceLookupUIMessages.sourceTab_lookupLabel);
86         gd = new GridData(GridData.HORIZONTAL_ALIGN_FILL);
87         gd.horizontalSpan = 2;
88         viewerLabel.setLayoutData(gd);
89         viewerLabel.setFont(font);
90         
91         fPathViewer = new SourceContainerViewer(comp, this);
92         
93         gd = new GridData(GridData.FILL_BOTH);
94         fPathViewer.getControl().setLayoutData(gd);
95         fPathViewer.getControl().setFont(font);
96         
97         IWorkingSetManager workingSetMgr =DebugUIPlugin.getDefault().getWorkbench().getWorkingSetManager();
98         //listen to changes user made to the working sets, if a working set is being removed
99
//check current list to validate working sets
100
workingSetMgr.addPropertyChangeListener(this);
101         
102         Composite pathButtonComp = new Composite(comp, SWT.NONE);
103         GridLayout pathButtonLayout = new GridLayout();
104         pathButtonLayout.marginHeight = 0;
105         pathButtonLayout.marginWidth = 0;
106         pathButtonComp.setLayout(pathButtonLayout);
107         gd =
108             new GridData(
109                     GridData.VERTICAL_ALIGN_BEGINNING
110                     | GridData.HORIZONTAL_ALIGN_FILL);
111         pathButtonComp.setLayoutData(gd);
112         pathButtonComp.setFont(font);
113         
114         createVerticalSpacer(comp, 2);
115         
116         fDuplicatesButton = new Button(comp, SWT.CHECK);
117         fDuplicatesButton.setText(
118                 SourceLookupUIMessages.sourceTab_searchDuplicateLabel);
119         gd = new GridData(GridData.HORIZONTAL_ALIGN_BEGINNING);
120         gd.horizontalSpan = 2;
121         fDuplicatesButton.setLayoutData(gd);
122         fDuplicatesButton.setFont(font);
123         fDuplicatesButton.addSelectionListener(new SelectionAdapter() {
124             public void widgetSelected(SelectionEvent evt) {
125                 setDirty(true);
126                 updateLaunchConfigurationDialog();
127             }
128         });
129         
130         GC gc = new GC(parent);
131         gc.setFont(parent.getFont());
132         FontMetrics fontMetrics = gc.getFontMetrics();
133         gc.dispose();
134         
135         fAddAction = new AddContainerAction();
136         Button button =
137             createPushButton(pathButtonComp, fAddAction.getText(), fontMetrics);
138         fAddAction.setButton(button);
139         addAction(fAddAction);
140         
141         fEditAction = new EditContainerAction();
142         button =
143             createPushButton(pathButtonComp, fEditAction.getText(), fontMetrics);
144         fEditAction.setButton(button);
145         addAction(fEditAction);
146         
147         SourceContainerAction action = new RemoveAction();
148         button =
149             createPushButton(pathButtonComp, action.getText(), fontMetrics);
150         action.setButton(button);
151         addAction(action);
152         
153         action = new UpAction();
154         button =
155             createPushButton(pathButtonComp, action.getText(), fontMetrics);
156         action.setButton(button);
157         addAction(action);
158         
159         action = new DownAction();
160         button =
161             createPushButton(pathButtonComp, action.getText(), fontMetrics);
162         action.setButton(button);
163         addAction(action);
164         
165         fRestoreDefaultAction = new RestoreDefaultAction();
166         button = createPushButton(pathButtonComp, fRestoreDefaultAction.getText(), fontMetrics);
167         fRestoreDefaultAction.setButton(button);
168         addAction(fRestoreDefaultAction);
169         
170         retargetActions(fPathViewer);
171         
172         Dialog.applyDialogFont(comp);
173         setControl(comp);
174     }
175     
176     /**
177      * Creates and returns a button
178      *
179      * @param parent parent widget
180      * @param label label
181      * @return Button
182      */

183     protected Button createPushButton(
184             Composite parent,
185             String JavaDoc label,
186             FontMetrics fontMetrics) {
187         Button button = new Button(parent, SWT.PUSH);
188         button.setFont(parent.getFont());
189         button.setText(label);
190         GridData gd = getButtonGridData(button, fontMetrics);
191         button.setLayoutData(gd);
192         return button;
193     }
194     
195     private GridData getButtonGridData(
196             Button button,
197             FontMetrics fontMetrics) {
198         GridData gd =
199             new GridData(
200                     GridData.FILL_HORIZONTAL | GridData.VERTICAL_ALIGN_BEGINNING);
201         
202         int widthHint =
203             Dialog.convertHorizontalDLUsToPixels(
204                     fontMetrics,
205                     IDialogConstants.BUTTON_WIDTH);
206         gd.widthHint =
207             Math.max(
208                     widthHint,
209                     button.computeSize(SWT.DEFAULT, SWT.DEFAULT, true).x);
210         
211         return gd;
212     }
213     
214     /**
215      * Adds the given action to the action collection in this tab
216      */

217     protected void addAction(SourceContainerAction action) {
218         fActions.add(action);
219     }
220     
221     /**
222      * Re-targets actions to the given viewer
223      */

224     protected void retargetActions(SourceContainerViewer viewer) {
225         Iterator JavaDoc actions = fActions.iterator();
226         while (actions.hasNext()) {
227             SourceContainerAction action = (SourceContainerAction) actions.next();
228             action.setViewer(viewer);
229         }
230     }
231     
232     /**
233      * Initializes this control based on the settings in the given
234      * launch configuration.
235      */

236     public void initializeFrom(ILaunchConfiguration configuration) {
237         if (fLocator != null) {
238             fLocator.dispose();
239             fLocator = null;
240         }
241         setErrorMessage(null);
242         setMessage(null);
243         String JavaDoc memento = null;
244         String JavaDoc type = null;
245         try{
246             memento = configuration.getAttribute(ILaunchConfiguration.ATTR_SOURCE_LOCATOR_MEMENTO, (String JavaDoc)null);
247             type = configuration.getAttribute(ILaunchConfiguration.ATTR_SOURCE_LOCATOR_ID, (String JavaDoc)null);
248             if (type == null) {
249                 type = configuration.getType().getSourceLocatorId();
250             }
251         } catch(CoreException e){
252             setErrorMessage(e.getMessage());
253             return;
254         }
255         
256         if(type == null) {
257             setErrorMessage(SourceLookupUIMessages.sourceLookupPanel_2);
258             return;
259         }
260         
261         boolean migration = false;
262         try {
263             ISourceLocator locator = getLaunchManager().newSourceLocator(type);
264             if(!(locator instanceof AbstractSourceLookupDirector)) {
265                 // migrate to the new source lookup infrastructure
266
memento = null; // don't use old memento
267
type = configuration.getType().getSourceLocatorId();
268                 if(type == null) {
269                     setErrorMessage(SourceLookupUIMessages.sourceLookupPanel_2);
270                     return;
271                 }
272                 locator = getLaunchManager().newSourceLocator(type);
273                 if (!(locator instanceof AbstractSourceLookupDirector)) {
274                     setErrorMessage(SourceLookupUIMessages.sourceLookupPanel_2);
275                     return;
276                 }
277                 migration = true;
278             }
279             fLocator = (AbstractSourceLookupDirector)locator;
280             if (memento == null) {
281                 fLocator.initializeDefaults(configuration);
282             } else {
283                 fLocator.initializeFromMemento(memento, configuration);
284             }
285         } catch (CoreException e) {
286             setErrorMessage(e.getMessage());
287             return;
288         }
289         initializeFrom(fLocator);
290         if (migration && configuration.isWorkingCopy()) {
291             // ensure perform apply actual updates the config
292
setDirty(true);
293             performApply((ILaunchConfigurationWorkingCopy)configuration);
294         }
295     }
296     
297     /**
298      * Initializes this control based on the settings in the given
299      * AbstractSourceLookupDirector
300      */

301     public void initializeFrom(ISourceLookupDirector locator) {
302         if(fConfig == null) {
303             fConfig = locator.getLaunchConfiguration();
304         }
305         fPathViewer.setEntries(locator.getSourceContainers());
306         fDuplicatesButton.setSelection(locator.isFindDuplicates());
307         fLocator = locator;
308         fAddAction.setSourceLookupDirector(locator);
309         fEditAction.setSourceLookupDirector(locator);
310         fRestoreDefaultAction.setSourceLookupDirector(locator);
311         setDirty(false);
312     }
313     
314     /**
315      * Saves the containers and duplicate policy into the given working copy of the configuration.
316      * Saving the configuration will result in a change event, which will be picked up by the director
317      * and used to refresh its internal list.
318      *
319      * @param workingCopy the working copy of the configuration that these values should be stored in, may be null.
320      * If null, will be written into a working copy of the configuration referenced by the director.
321      */

322     public void performApply(ILaunchConfigurationWorkingCopy configuration) {
323         if (isDirty()) {
324             if (fLocator == null) {
325                 configuration.setAttribute(ILaunchConfiguration.ATTR_SOURCE_LOCATOR_MEMENTO, (String JavaDoc)null);
326                 configuration.setAttribute(ILaunchConfiguration.ATTR_SOURCE_LOCATOR_ID, (String JavaDoc)null);
327                 return;
328             }
329             ILaunchConfigurationWorkingCopy workingCopy = configuration;
330             if(configuration == null) {
331                 try {
332                     ILaunchConfiguration config = fLocator.getLaunchConfiguration();
333                     if(config != null) {
334                         workingCopy = config.getWorkingCopy();
335                     }
336                 }
337                 catch(CoreException e) {
338                     DebugUIPlugin.log(e);
339                     setErrorMessage(SourceLookupUIMessages.sourceLookupPanel_1);
340                     return;
341                 }
342             }
343             if(workingCopy == null) {
344                 DebugUIPlugin.logErrorMessage("Error occurred - a working copy could not be acquired, therefore source lookup path changes will not be applied."); //$NON-NLS-1$
345
return;
346             }
347             //set new values in director so memento returned is correct
348
fLocator.setSourceContainers(fPathViewer.getEntries());
349             fLocator.setFindDuplicates(fDuplicatesButton.getSelection());
350                         
351             //writing to the file will cause a change event and the listeners will be updated
352
try {
353                 if (isDefault()) {
354                     workingCopy.setAttribute(ILaunchConfiguration.ATTR_SOURCE_LOCATOR_MEMENTO, (String JavaDoc)null);
355                     workingCopy.setAttribute(ILaunchConfiguration.ATTR_SOURCE_LOCATOR_ID, (String JavaDoc)null);
356                 } else {
357                     workingCopy.setAttribute(ILaunchConfiguration.ATTR_SOURCE_LOCATOR_MEMENTO, fLocator.getMemento());
358                     workingCopy.setAttribute(ILaunchConfiguration.ATTR_SOURCE_LOCATOR_ID, fLocator.getId());
359                 }
360             }
361             catch(CoreException e) {
362                 DebugUIPlugin.log(e);
363                 setErrorMessage(SourceLookupUIMessages.sourceLookupPanel_1);
364             }
365             
366         }
367     }
368     
369     /**
370      * determines of the current source lokoup path is the default path
371      * @param configuration
372      * @return
373      */

374     protected boolean isDefault() {
375         ISourceContainer[] current = getEntries();
376         return !fDuplicatesButton.getSelection() && current.length == 1 && current[0] instanceof DefaultSourceContainer;
377     }
378
379     /**
380      * Returns the entries visible in the viewer
381      */

382     public ISourceContainer[] getEntries() {
383         return fPathViewer.getEntries();
384     }
385     /**
386      * Marks the panel as dirty.
387      */

388     public void setDirty(boolean dirty) {
389         super.setDirty(dirty);
390         
391     }
392     
393     /* (non-Javadoc)
394      * @see org.eclipse.debug.ui.ILaunchConfigurationTab#getName()
395      */

396     public String JavaDoc getName() {
397         return SourceLookupUIMessages.sourceTab_tabTitle;
398     }
399     
400     /* (non-Javadoc)
401      * @see org.eclipse.debug.ui.ILaunchConfigurationTab#setDefaults(org.eclipse.debug.core.ILaunchConfigurationWorkingCopy)
402      */

403     public void setDefaults(ILaunchConfigurationWorkingCopy configuration) {
404         
405     }
406     
407     /* (non-Javadoc)
408      * @see org.eclipse.debug.ui.AbstractLaunchConfigurationTab#updateLaunchConfigurationDialog()
409      */

410     protected void updateLaunchConfigurationDialog() {
411         if (getLaunchConfigurationDialog() != null) {
412             super.updateLaunchConfigurationDialog();
413         }
414     }
415         
416     /**
417      * This is called whenever a working set has been removed. Loops through the original list
418      * of working sets the user stores in the configuration. If the one being removed is in the
419      * list, remove it from the list
420      * @param affectedWorkingSet - the working set being removed
421      */

422     private void validateWorkingSetSourceContainers(IWorkingSet affectedWorkingSet) {
423         List JavaDoc sourceContainers = (List JavaDoc) fPathViewer.getInput();
424         
425         if (sourceContainers != null) {
426             for (int i = 0; i < sourceContainers.size(); i++) {
427                 if (sourceContainers.get(i)
428                         instanceof WorkingSetSourceContainer) {
429                     WorkingSetSourceContainer wsSrcContainer =
430                         (WorkingSetSourceContainer) sourceContainers.get(i);
431                     if (wsSrcContainer
432                             .getName()
433                             .equals(affectedWorkingSet.getName())) {
434                         sourceContainers.remove(i);
435                     }
436                 }
437             }
438         }
439     }
440     
441     /**
442      * Listen to working set changes
443      * @param event
444      */

445     public void propertyChange(PropertyChangeEvent event) {
446         //if the PropertyChangeEvent has changeId CHANGE_WORKING_SET_REMOVE,
447
//validate the list to make sure all working sets are valid
448
if (event.getProperty().equals(IWorkingSetManager.CHANGE_WORKING_SET_REMOVE))
449             validateWorkingSetSourceContainers((IWorkingSet) event.getOldValue());
450         
451         //if the PropertyChangeEvent has changeId CHANGE_WORKING_SET_NAME_CHANGE,
452
//do nothing because the event only has newValue, since oldValue is not provided
453
//there is no way to identify which working set does the newValue corresponds to
454
}
455     
456     
457     /* (non-Javadoc)
458      * @see org.eclipse.debug.ui.ILaunchConfigurationTab#activated(org.eclipse.debug.core.ILaunchConfigurationWorkingCopy)
459      */

460     public void activated(ILaunchConfigurationWorkingCopy workingCopy) {
461         initializeFrom(workingCopy);
462     }
463     
464     /**
465      * Returns the source lookup director associated with this
466      * panel, or <code>null</code> if none.
467      *
468      * @return the source lookup director associated with this
469      * panel, or <code>null</code> if none
470      */

471     public ISourceLookupDirector getDirector() {
472         return fLocator;
473     }
474     
475     /* (non-Javadoc)
476      * @see org.eclipse.debug.ui.ILaunchConfigurationTab#dispose()
477      */

478     public void dispose() {
479         super.dispose();
480         IWorkingSetManager workingSetMgr =DebugUIPlugin.getDefault().getWorkbench().getWorkingSetManager();
481         //listen to changes user made to the working sets, if a working set is being removed
482
//check current list to validate working sets
483
workingSetMgr.removePropertyChangeListener(this);
484     }
485 }
486
Popular Tags