KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.util.ArrayList JavaDoc;
15 import java.util.List JavaDoc;
16 import org.eclipse.core.resources.ResourcesPlugin;
17 import org.eclipse.core.runtime.CoreException;
18 import org.eclipse.debug.core.DebugPlugin;
19 import org.eclipse.debug.core.ILaunchConfiguration;
20 import org.eclipse.debug.core.ILaunchConfigurationType;
21 import org.eclipse.debug.core.ILaunchManager;
22 import org.eclipse.debug.internal.ui.DebugUIPlugin;
23 import org.eclipse.jface.viewers.ITreeContentProvider;
24 import org.eclipse.jface.viewers.Viewer;
25 import org.eclipse.swt.widgets.Shell;
26 import org.eclipse.ui.activities.WorkbenchActivityHelper;
27
28 /**
29  * Content provider for representing launch configuration types & launch configurations in a tree.
30  *
31  * @since 2.1
32  */

33 public class LaunchConfigurationTreeContentProvider implements ITreeContentProvider {
34
35     /**
36      * Empty Object array
37      */

38     private static final Object JavaDoc[] EMPTY_ARRAY = new Object JavaDoc[0];
39     
40     /**
41      * The mode in which the tree is being shown, one of <code>RUN_MODE</code>
42      * or <code>DEBUG_MODE</code> defined in <code>ILaunchManager</code>.
43      * If this is <code>null</code>, then it means both modes are being shown.
44      */

45     private String JavaDoc fMode;
46     
47     /**
48      * The Shell context
49      */

50     private Shell fShell;
51     
52     /**
53      * Constructor
54      * @param mode the mode
55      * @param shell the parent shell
56      */

57     public LaunchConfigurationTreeContentProvider(String JavaDoc mode, Shell shell) {
58         setMode(mode);
59         setShell(shell);
60     }
61
62     /**
63      * Actual launch configurations have no children. Launch configuration types have
64      * all configurations of that type as children, minus any configurations that are
65      * marked as private.
66      * <p>
67      * In 2.1, the <code>category</code> attribute was added to launch config
68      * types. The debug UI only displays those configs that do not specify a
69      * category.
70      * </p>
71      *
72      * @see org.eclipse.jface.viewers.ITreeContentProvider#getChildren(java.lang.Object)
73      */

74     public Object JavaDoc[] getChildren(Object JavaDoc parentElement) {
75         if (parentElement instanceof ILaunchConfiguration) {
76             return EMPTY_ARRAY;
77         } else if (parentElement instanceof ILaunchConfigurationType) {
78             try {
79                 ILaunchConfigurationType type = (ILaunchConfigurationType)parentElement;
80                 return getLaunchManager().getLaunchConfigurations(type);
81             } catch (CoreException e) {
82                 DebugUIPlugin.errorDialog(getShell(), LaunchConfigurationsMessages.LaunchConfigurationDialog_Error_19, LaunchConfigurationsMessages.LaunchConfigurationDialog_An_exception_occurred_while_retrieving_launch_configurations_20, e); //
83
}
84         } else {
85             return getLaunchManager().getLaunchConfigurationTypes();
86         }
87         return EMPTY_ARRAY;
88     }
89
90     /* (non-Javadoc)
91      * @see org.eclipse.jface.viewers.ITreeContentProvider#getParent(java.lang.Object)
92      */

93     public Object JavaDoc getParent(Object JavaDoc element) {
94         if (element instanceof ILaunchConfiguration) {
95             if (!((ILaunchConfiguration)element).exists()) {
96                 return null;
97             }
98             try {
99                 return ((ILaunchConfiguration)element).getType();
100             } catch (CoreException e) {
101                 DebugUIPlugin.errorDialog(getShell(), LaunchConfigurationsMessages.LaunchConfigurationDialog_Error_19, LaunchConfigurationsMessages.LaunchConfigurationDialog_An_exception_occurred_while_retrieving_launch_configurations_20, e); //
102
}
103         } else if (element instanceof ILaunchConfigurationType) {
104             return ResourcesPlugin.getWorkspace().getRoot();
105         }
106         return null;
107     }
108
109     /* (non-Javadoc)
110      * @see org.eclipse.jface.viewers.ITreeContentProvider#hasChildren(java.lang.Object)
111      */

112     public boolean hasChildren(Object JavaDoc element) {
113         if (element instanceof ILaunchConfiguration) {
114             return false;
115         }
116         return getChildren(element).length > 0;
117     }
118
119     /**
120      * Return only the launch configuration types that support the current mode AND
121      * are marked as 'public'.
122      *
123      * @see org.eclipse.jface.viewers.IStructuredContentProvider#getElements(java.lang.Object)
124      */

125     public Object JavaDoc[] getElements(Object JavaDoc inputElement) {
126         ILaunchConfigurationType[] allTypes = getLaunchManager().getLaunchConfigurationTypes();
127         return filterTypes(allTypes).toArray();
128     }
129
130     /**
131      * Returns a list containing the given types minus any types that
132      * should not be visible. A type should not be visible if it doesn't match
133      * the current mode or if it matches a disabled activity.
134      *
135      * @param allTypes the types
136      * @return the given types minus any types that should not be visible.
137      */

138     private List JavaDoc filterTypes(ILaunchConfigurationType[] allTypes) {
139         List JavaDoc filteredTypes= new ArrayList JavaDoc();
140         String JavaDoc mode = getMode();
141         LaunchConfigurationTypeContribution contribution;
142         for (int i = 0; i < allTypes.length; i++) {
143             ILaunchConfigurationType type = allTypes[i];
144             contribution= new LaunchConfigurationTypeContribution(type);
145             if (isVisible(type, mode) && !WorkbenchActivityHelper.filterItem(contribution)) {
146                 filteredTypes.add(type);
147             }
148         }
149         return filteredTypes;
150     }
151
152     /* (non-Javadoc)
153      * @see org.eclipse.jface.viewers.IContentProvider#dispose()
154      */

155     public void dispose() {
156     }
157
158     /* (non-Javadoc)
159      * @see org.eclipse.jface.viewers.IContentProvider#inputChanged(org.eclipse.jface.viewers.Viewer, java.lang.Object, java.lang.Object)
160      */

161     public void inputChanged(Viewer viewer, Object JavaDoc oldInput, Object JavaDoc newInput) {
162     }
163     
164     /**
165      * Return <code>true</code> if the specified launch configuration type should
166      * be visible in the specified mode, <code>false</code> otherwise.
167      */

168     private boolean isVisible(ILaunchConfigurationType configType, String JavaDoc mode) {
169         if (!configType.isPublic()) {
170             return false;
171         }
172         if (mode == null) {
173             return true;
174         }
175         return configType.supportsMode(mode);
176     }
177
178     /**
179      * Convenience method to get the singleton launch manager.
180      */

181     private ILaunchManager getLaunchManager() {
182         return DebugPlugin.getDefault().getLaunchManager();
183     }
184
185     /**
186      * Write accessor for the mode value
187      */

188     private void setMode(String JavaDoc mode) {
189         fMode = mode;
190     }
191     
192     /**
193      * Read accessor for the mode value
194      */

195     private String JavaDoc getMode() {
196         return fMode;
197     }
198
199     /**
200      * Write accessor for the shell value
201      */

202     private void setShell(Shell shell) {
203         fShell = shell;
204     }
205     
206     /**
207      * Read accessor for the shell value
208      */

209     private Shell getShell() {
210         return fShell;
211     }
212 }
213
Popular Tags