KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > debug > internal > ui > launchConfigurations > LaunchConfigurationView


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.internal.ui.launchConfigurations;
12
13
14 import org.eclipse.core.runtime.CoreException;
15 import org.eclipse.debug.core.DebugPlugin;
16 import org.eclipse.debug.core.ILaunchConfiguration;
17 import org.eclipse.debug.core.ILaunchConfigurationListener;
18 import org.eclipse.debug.core.ILaunchManager;
19 import org.eclipse.debug.internal.ui.DebugUIPlugin;
20 import org.eclipse.debug.internal.ui.IDebugHelpContextIds;
21 import org.eclipse.debug.internal.ui.SWTFactory;
22 import org.eclipse.debug.ui.AbstractDebugView;
23 import org.eclipse.debug.ui.IDebugUIConstants;
24 import org.eclipse.debug.ui.IDebugView;
25 import org.eclipse.help.HelpSystem;
26 import org.eclipse.help.IContext;
27 import org.eclipse.help.IContextProvider;
28 import org.eclipse.jface.action.IMenuManager;
29 import org.eclipse.jface.action.IToolBarManager;
30 import org.eclipse.jface.action.Separator;
31 import org.eclipse.jface.viewers.StructuredSelection;
32 import org.eclipse.jface.viewers.StructuredViewer;
33 import org.eclipse.jface.viewers.TreeViewer;
34 import org.eclipse.jface.viewers.Viewer;
35 import org.eclipse.jface.viewers.ViewerFilter;
36 import org.eclipse.swt.SWT;
37 import org.eclipse.swt.events.KeyAdapter;
38 import org.eclipse.swt.events.KeyEvent;
39 import org.eclipse.swt.widgets.Composite;
40 import org.eclipse.swt.widgets.Display;
41 import org.eclipse.swt.widgets.Label;
42 import org.eclipse.swt.widgets.Text;
43 import org.eclipse.ui.PlatformUI;
44 import org.eclipse.ui.dialogs.PatternFilter;
45
46 import com.ibm.icu.text.MessageFormat;
47
48 /**
49  * A tree view of launch configurations
50  */

51 public class LaunchConfigurationView extends AbstractDebugView implements ILaunchConfigurationListener {
52     
53     /**
54      * the filtering tree viewer
55      *
56      * @since 3.2
57      */

58     private LaunchConfigurationFilteredTree fTree;
59     
60     /**
61      * a handle to the launch manager
62      *
63      * @since 3.2
64      */

65     private ILaunchManager fLaunchManager = DebugPlugin.getDefault().getLaunchManager();
66     
67     /**
68      * The launch group to display
69      */

70     private LaunchGroupExtension fLaunchGroup;
71     
72     /**
73      * Actions
74      */

75     private CreateLaunchConfigurationAction fCreateAction;
76     private DeleteLaunchConfigurationAction fDeleteAction;
77     private DuplicateLaunchConfigurationAction fDuplicateAction;
78     private CollapseAllLaunchConfigurationAction fCollapseAllAction;
79     
80     /**
81      * Action for providing filtering to the Launch Configuration Dialog
82      * @since 3.2
83      */

84     private FilterLaunchConfigurationAction fFilterAction;
85     
86     /**
87      * This label is used to notify users that items (possibly) have been filtered from the
88      * launch configuration view
89      * @since 3.3
90      */

91     private Label fFilteredNotice = null;
92     
93     /**
94      * Whether to automatically select configs that are added
95      */

96     private boolean fAutoSelect = true;
97     
98     /**
99      * the group of additional filters to be added to the viewer
100      * @since 3.2
101      */

102     private ViewerFilter[] fFilters = null;
103     
104     /**
105      * Constructs a launch configuration view for the given launch group
106      */

107     public LaunchConfigurationView(LaunchGroupExtension launchGroup) {
108         super();
109         fLaunchGroup = launchGroup;
110     }
111     
112     /**
113      * Constructor
114      * @param launchGroup
115      * @param filters
116      */

117     public LaunchConfigurationView(LaunchGroupExtension launchGroup, ViewerFilter[] filters) {
118         super();
119         fLaunchGroup = launchGroup;
120         fFilters = filters;
121     }
122     
123     /**
124      * Returns the launch group this view is displaying.
125      *
126      * @return the launch group this view is displaying
127      */

128     protected LaunchGroupExtension getLaunchGroup() {
129         return fLaunchGroup;
130     }
131
132     /**
133      * @see org.eclipse.debug.ui.AbstractDebugView#createViewer(org.eclipse.swt.widgets.Composite)
134      */

135     protected Viewer createViewer(Composite parent) {
136         fTree = new LaunchConfigurationFilteredTree(parent, SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL, new PatternFilter(), fLaunchGroup, fFilters);
137         fTree.createViewControl();
138         getLaunchManager().addLaunchConfigurationListener(this);
139         LaunchConfigurationViewer viewer = fTree.getLaunchConfigurationViewer();
140         viewer.setLaunchConfigurationView(this);
141         return viewer;
142     }
143     
144     /**
145      * @see org.eclipse.debug.ui.AbstractDebugView#getAdapter(java.lang.Class)
146      */

147     public Object JavaDoc getAdapter(Class JavaDoc key) {
148         if (key == IContextProvider.class) {
149             return new IContextProvider () {
150                 public int getContextChangeMask() {
151                     return SELECTION;
152                 }
153
154                 public IContext getContext(Object JavaDoc target) {
155                     String JavaDoc id = fTree.computeContextId();
156                     if (id!=null)
157                         return HelpSystem.getContext(id);
158                     return null;
159                 }
160
161                 public String JavaDoc getSearchExpression(Object JavaDoc target) {
162                     return null;
163                 }
164             };
165         }
166         return super.getAdapter(key);
167     }
168     
169     /**
170      * gets the filtering text control from the viewer
171      * @return the filtering text control
172      *
173      * @since 3.2
174      */

175     public Text getFilteringTextControl() {
176         return fTree.getFilterControl();
177     }
178
179     /**
180      * @see org.eclipse.debug.ui.AbstractDebugView#createActions()
181      */

182     protected void createActions() {
183         fCreateAction = new CreateLaunchConfigurationAction(getViewer(), getLaunchGroup().getMode());
184         setAction(CreateLaunchConfigurationAction.ID_CREATE_ACTION, fCreateAction);
185         
186         fDeleteAction = new DeleteLaunchConfigurationAction(getViewer(), getLaunchGroup().getMode());
187         setAction(DeleteLaunchConfigurationAction.ID_DELETE_ACTION, fDeleteAction);
188         setAction(IDebugView.REMOVE_ACTION, fDeleteAction);
189         
190         fDuplicateAction = new DuplicateLaunchConfigurationAction(getViewer(), getLaunchGroup().getMode());
191         setAction(DuplicateLaunchConfigurationAction.ID_DUPLICATE_ACTION, fDuplicateAction);
192         
193         fCollapseAllAction = new CollapseAllLaunchConfigurationAction((TreeViewer)getViewer());
194         setAction(CollapseAllLaunchConfigurationAction.ID_COLLAPSEALL_ACTION, fCollapseAllAction);
195         
196         fFilterAction = new FilterLaunchConfigurationAction();
197         setAction(FilterLaunchConfigurationAction.ID_FILTER_ACTION, fFilterAction);
198         
199     }
200
201     /**
202      * @see org.eclipse.debug.ui.AbstractDebugView#getHelpContextId()
203      */

204     protected String JavaDoc getHelpContextId() {
205         return IDebugHelpContextIds.LAUNCH_CONFIGURATION_VIEW;
206     }
207
208     /**
209      * @see org.eclipse.debug.ui.AbstractDebugView#fillContextMenu(org.eclipse.jface.action.IMenuManager)
210      */

211     protected void fillContextMenu(IMenuManager menu) {
212         menu.add(fCreateAction);
213         menu.add(fDuplicateAction);
214         menu.add(fDeleteAction);
215         menu.add(new Separator());
216     }
217
218     /**
219      * @see org.eclipse.debug.ui.AbstractDebugView#configureToolBar(org.eclipse.jface.action.IToolBarManager)
220      */

221     protected void configureToolBar(IToolBarManager tbm) {}
222     
223     /**
224      * Returns this view's tree viewer
225      *
226      * @return this view's tree viewer
227      */

228     protected TreeViewer getTreeViewer() {
229         return fTree.getLaunchConfigurationViewer();
230     }
231
232     /**
233      * @see org.eclipse.ui.IWorkbenchPart#dispose()
234      */

235     public void dispose() {
236         fCreateAction.dispose();
237         fDeleteAction.dispose();
238         fDuplicateAction.dispose();
239         fFilterAction = null;
240         fCollapseAllAction = null;
241         getLaunchManager().removeLaunchConfigurationListener(this);
242     }
243
244     /**
245      * @see org.eclipse.debug.core.ILaunchConfigurationListener#launchConfigurationAdded(org.eclipse.debug.core.ILaunchConfiguration)
246      */

247     public void launchConfigurationAdded(final ILaunchConfiguration configuration) {
248         try {
249             if (configuration.getAttribute(IDebugUIConstants.ATTR_PRIVATE, false)) {
250                 return;
251             }
252         } catch (CoreException e) {
253             DebugUIPlugin.log(e);
254             return;
255         }
256         Display display = DebugUIPlugin.getStandardDisplay();
257         if (display.getThread() == Thread.currentThread()) {
258             // If we're already in the UI thread (user pressing New in the
259
// dialog), update the tree immediately.
260
handleConfigurationAdded(configuration);
261         } else {
262             display.asyncExec(new Runnable JavaDoc() {
263                 public void run() {
264                     handleConfigurationAdded(configuration);
265                 }
266             });
267         }
268     }
269
270     /**
271      * The given launch configuration has been added. Add it to the tree.
272      * @param configuration the added configuration
273      */

274     private void handleConfigurationAdded(final ILaunchConfiguration configuration) {
275         TreeViewer viewer = getTreeViewer();
276         if (viewer != null) {
277             try {
278                 viewer.add(configuration.getType(), configuration);
279                 // if moved, remove original now
280
ILaunchConfiguration from = getLaunchManager().getMovedFrom(configuration);
281                 if (from != null) {
282                     viewer.remove(from);
283                 }
284                 if (isAutoSelect()) {
285                     viewer.setSelection(new StructuredSelection(configuration), true);
286                 }
287                 updateFilterLabel();
288             }
289             catch (CoreException e) {}
290         }
291     }
292
293     /**
294      * @see org.eclipse.debug.core.ILaunchConfigurationListener#launchConfigurationChanged(org.eclipse.debug.core.ILaunchConfiguration)
295      */

296     public void launchConfigurationChanged(ILaunchConfiguration configuration) {}
297
298     /**
299      * @see org.eclipse.debug.core.ILaunchConfigurationListener#launchConfigurationRemoved(org.eclipse.debug.core.ILaunchConfiguration)
300      */

301     public void launchConfigurationRemoved(final ILaunchConfiguration configuration) {
302         // if moved, ignore
303
ILaunchConfiguration to = getLaunchManager().getMovedTo(configuration);
304         if (to != null) {
305             return;
306         }
307         Display display = DebugUIPlugin.getStandardDisplay();
308         if (display.getThread() == Thread.currentThread()) {
309             // If we're already in the UI thread (user pressing Delete in the
310
// dialog), update the tree immediately.
311
handleConfigurationRemoved(configuration);
312         } else {
313             display.asyncExec(new Runnable JavaDoc() {
314                 public void run() {
315                     handleConfigurationRemoved(configuration);
316                 }
317             });
318         }
319     }
320
321     /**
322      * The given launch configuration has been removed. Remove it from the tree.
323      * @param configuration the deleted configuration
324      */

325     private void handleConfigurationRemoved(ILaunchConfiguration configuration) {
326         getTreeViewer().remove(configuration);
327         updateFilterLabel();
328     }
329
330     /**
331      * This is similar to IWorkbenchPart#createPartControl(Composite), but it is
332      * called by the launch dialog when creating the launch config tree view.
333      * Since this view is not contained in the workbench, we cannot do all the
334      * usual initialization (toolbars, etc).
335      */

336     public void createLaunchDialogControl(Composite parent) {
337         createViewer(parent);
338         createActions();
339         createContextMenu(getViewer().getControl());
340         PlatformUI.getWorkbench().getHelpSystem().setHelp(parent, getHelpContextId());
341         getViewer().getControl().addKeyListener(new KeyAdapter() {
342             public void keyPressed(KeyEvent e) {
343                 handleKeyPressed(e);
344             }
345         });
346         if (getViewer() instanceof StructuredViewer) {
347             ((StructuredViewer)getViewer()).addDoubleClickListener(this);
348         }
349         fFilteredNotice = SWTFactory.createLabel(parent, "", 1); //$NON-NLS-1$
350
fFilteredNotice.setBackground(parent.getBackground());
351     }
352     
353     /**
354      * @see org.eclipse.debug.ui.IDebugView#getViewer()
355      */

356     public Viewer getViewer() {
357         return fTree.getLaunchConfigurationViewer();
358     }
359     
360     /**
361      * Updates the filter notification label
362      * @since 3.3
363      */

364     public void updateFilterLabel() {
365         LaunchConfigurationViewer viewer = (LaunchConfigurationViewer) getViewer();
366         fFilteredNotice.setText(MessageFormat.format(LaunchConfigurationsMessages.LaunchConfigurationView_0, new String JavaDoc[] {Integer.toString(viewer.getNonFilteredChildCount()), Integer.toString(viewer.getTotalChildCount())}));
367     }
368     
369     /**
370      * returns the launch manager
371      * @return
372      */

373     protected ILaunchManager getLaunchManager() {
374         return fLaunchManager;
375     }
376     
377     /**
378      * Sets whether to automatically select configs that are
379      * added into the view (newly created).
380      *
381      * @param select whether to automatically select configs that are
382      * added into the view (newly created)
383      */

384     public void setAutoSelect(boolean select) {
385         fAutoSelect = select;
386     }
387     
388     /**
389      * Returns whether this view is currently configured to
390      * automatically select newly created configs that are
391      * added into the view.
392      *
393      * @return whether this view is currently configured to
394      * automatically select newly created configs
395      */

396     protected boolean isAutoSelect() {
397         return fAutoSelect;
398     }
399 }
400
Popular Tags