KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2006, 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.jdt.internal.debug.ui.launcher;
12
13 import java.util.Arrays JavaDoc;
14 import java.util.Comparator JavaDoc;
15 import java.util.HashMap JavaDoc;
16
17 import org.eclipse.core.runtime.CoreException;
18 import org.eclipse.core.runtime.IAdaptable;
19 import org.eclipse.core.runtime.IProgressMonitor;
20 import org.eclipse.core.runtime.IStatus;
21 import org.eclipse.core.runtime.Status;
22 import org.eclipse.jdt.core.IJavaElement;
23 import org.eclipse.jdt.core.IJavaProject;
24 import org.eclipse.jdt.core.IType;
25 import org.eclipse.jdt.core.JavaCore;
26 import org.eclipse.jdt.core.JavaModelException;
27 import org.eclipse.jdt.internal.debug.ui.IJavaDebugHelpContextIds;
28 import org.eclipse.jdt.internal.debug.ui.JDIDebugUIPlugin;
29 import org.eclipse.jface.dialogs.IDialogSettings;
30 import org.eclipse.jface.resource.ImageDescriptor;
31 import org.eclipse.jface.viewers.ILabelProvider;
32 import org.eclipse.jface.viewers.ILabelProviderListener;
33 import org.eclipse.swt.graphics.Image;
34 import org.eclipse.swt.widgets.Composite;
35 import org.eclipse.swt.widgets.Control;
36 import org.eclipse.swt.widgets.Shell;
37 import org.eclipse.ui.IMemento;
38 import org.eclipse.ui.PlatformUI;
39 import org.eclipse.ui.dialogs.FilteredItemsSelectionDialog;
40 import org.eclipse.ui.model.IWorkbenchAdapter;
41
42 /**
43  * This is a specialization of <code>FilteredItemsSelectionDialog</code> used to present
44  * users with a listing of <code>IType</code>s that contain main methods
45  *
46  * @since 3.3
47  *
48  */

49 public class DebugTypeSelectionDialog extends FilteredItemsSelectionDialog {
50     
51     /**
52      * Main list label provider
53      */

54     public class DebugTypeLabelProvider implements ILabelProvider {
55         HashMap JavaDoc fImageMap = new HashMap JavaDoc();
56
57         public Image getImage(Object JavaDoc element) {
58             if(element instanceof IAdaptable) {
59                 IWorkbenchAdapter adapter = (IWorkbenchAdapter) ((IAdaptable)element).getAdapter(IWorkbenchAdapter.class);
60                 if(adapter != null) {
61                     ImageDescriptor descriptor = adapter.getImageDescriptor(element);
62                     Image image = (Image) fImageMap.get(descriptor);
63                     if(image == null) {
64                         image = descriptor.createImage();
65                         fImageMap.put(descriptor, image);
66                     }
67                     return image;
68                 }
69             }
70             return null;
71         }
72         public String JavaDoc getText(Object JavaDoc element) {
73             if(element instanceof IType) {
74                 IType type = (IType) element;
75                 String JavaDoc label = type.getElementName();
76                 String JavaDoc container = getDeclaringContainerName(type);
77                 if(container != null && !"".equals(container)) { //$NON-NLS-1$
78
label += " - "+container; //$NON-NLS-1$
79
}
80                 return label;
81             }
82             return null;
83         }
84         
85         /**
86          * Returns the name of the declaring container name
87          * @param type the type to find the container name for
88          * @return the container name for the specified type
89          */

90         protected String JavaDoc getDeclaringContainerName(IType type) {
91             IType outer = type.getDeclaringType();
92             if(outer != null) {
93                 return outer.getFullyQualifiedName('.');
94             }
95             else {
96                 String JavaDoc name = type.getPackageFragment().getElementName();
97                 if("".equals(name)) { //$NON-NLS-1$
98
name = LauncherMessages.MainMethodLabelProvider_0;
99                 }
100                 return name;
101             }
102         }
103         
104         /**
105          * Returns the narrowest enclosing <code>IJavaElement</code> which is either
106          * an <code>IType</code> (enclosing) or an <code>IPackageFragment</code> (contained in)
107          * @param type the type to find the enclosing <code>IJavaElement</code> for.
108          * @return the enclosing element or <code>null</code> if none
109          */

110         protected IJavaElement getDeclaringContainer(IType type) {
111             IJavaElement outer = type.getDeclaringType();
112             if(outer == null) {
113                 outer = type.getPackageFragment();
114             }
115             return outer;
116         }
117
118         public void dispose() {
119             fImageMap.clear();
120             fImageMap = null;
121         }
122         public void addListener(ILabelProviderListener listener) {}
123         public boolean isLabelProperty(Object JavaDoc element, String JavaDoc property) {return false;}
124         public void removeListener(ILabelProviderListener listener) {}
125     }
126     
127     /**
128      * Provides a label and image for the details area of the dialog
129      */

130     class DebugTypeDetailsLabelProvider extends DebugTypeLabelProvider {
131         public String JavaDoc getText(Object JavaDoc element) {
132             if(element instanceof IType) {
133                 IType type = (IType) element;
134                 String JavaDoc name = getDeclaringContainerName(type);
135                 if(name != null) {
136                     if(name.equals(LauncherMessages.MainMethodLabelProvider_0)) {
137                         IJavaProject project = type.getJavaProject();
138                         if(project != null) {
139                             try {
140                                 return project.getOutputLocation().toOSString().substring(1)+" - "+name; //$NON-NLS-1$
141
}
142                             catch (JavaModelException e) {JDIDebugUIPlugin.log(e);}
143                         }
144                     }
145                     else {
146                         return name;
147                     }
148                 }
149             }
150             return null;
151         }
152         public Image getImage(Object JavaDoc element) {
153             if(element instanceof IType) {
154                 return super.getImage(getDeclaringContainer(((IType) element)));
155             }
156             return super.getImage(element);
157         }
158     }
159     
160     /**
161      * Simple items filter
162      */

163     class DebugTypeItemsFilter extends ItemsFilter {
164         public boolean isConsistentItem(Object JavaDoc item) {
165             return item instanceof IType;
166         }
167         public boolean matchItem(Object JavaDoc item) {
168             if(!(item instanceof IType) || !Arrays.asList(fTypes).contains(item)) {
169                 return false;
170             }
171             return matches(((IType)item).getElementName());
172         }
173     }
174     
175     /**
176      * The selection history for the dialog
177      */

178     class DebugTypeSelectionHistory extends SelectionHistory {
179         protected Object JavaDoc restoreItemFromMemento(IMemento memento) {
180             IJavaElement element = JavaCore.create(memento.getTextData());
181             return (element instanceof IType ? element : null);
182         }
183         protected void storeItemToMemento(Object JavaDoc item, IMemento memento) {
184             if(item instanceof IType) {
185                 memento.putTextData(((IType) item).getHandleIdentifier());
186             }
187         }
188     }
189     
190     private static final String JavaDoc SETTINGS_ID = JDIDebugUIPlugin.getUniqueIdentifier() + ".MAIN_METHOD_SELECTION_DIALOG"; //$NON-NLS-1$
191
private IType[] fTypes = null;
192
193     /**
194      * Constructor
195      * @param elements the types to display in the dialog
196      */

197     public DebugTypeSelectionDialog(Shell shell, IType[] elements, String JavaDoc title) {
198         super(shell, false);
199         setTitle(title);
200         fTypes = elements;
201         setMessage(LauncherMessages.JavaMainTab_Choose_a_main__type_to_launch__12);
202         setInitialPattern("**"); //$NON-NLS-1$
203
setListLabelProvider(new DebugTypeLabelProvider());
204         setDetailsLabelProvider(new DebugTypeDetailsLabelProvider());
205         setSelectionHistory(new DebugTypeSelectionHistory());
206     }
207
208     /* (non-Javadoc)
209      * @see org.eclipse.ui.dialogs.FilteredItemsSelectionDialog#createDialogArea(org.eclipse.swt.widgets.Composite)
210      */

211     protected Control createDialogArea(Composite parent) {
212         Control ctrl = super.createDialogArea(parent);
213         PlatformUI.getWorkbench().getHelpSystem().setHelp(ctrl, IJavaDebugHelpContextIds.SELECT_MAIN_METHOD_DIALOG);
214         return ctrl;
215     }
216
217     /**
218      * @see org.eclipse.ui.dialogs.FilteredItemsSelectionDialog#getDialogSettings()
219      */

220     protected IDialogSettings getDialogSettings() {
221         IDialogSettings settings = JDIDebugUIPlugin.getDefault().getDialogSettings();
222         IDialogSettings section = settings.getSection(SETTINGS_ID);
223         if (section == null) {
224             section = settings.addNewSection(SETTINGS_ID);
225         }
226         return section;
227     }
228
229     /**
230      * @see org.eclipse.ui.dialogs.FilteredItemsSelectionDialog#getItemsComparator()
231      */

232     protected Comparator JavaDoc getItemsComparator() {
233         Comparator JavaDoc comp = new Comparator JavaDoc() {
234             public int compare(Object JavaDoc o1, Object JavaDoc o2) {
235                 if(o1 instanceof IType && o2 instanceof IType) {
236                     return ((IType)o1).getElementName().compareTo(((IType)o2).getElementName());
237                 }
238                 return -1;
239             }
240         };
241         return comp;
242     }
243     
244     /**
245      * @see org.eclipse.ui.dialogs.FilteredItemsSelectionDialog#validateItem(java.lang.Object)
246      */

247     protected IStatus validateItem(Object JavaDoc item) {
248         return Status.OK_STATUS;
249     }
250     
251     /**
252      * @see org.eclipse.ui.dialogs.FilteredItemsSelectionDialog#createExtendedContentArea(org.eclipse.swt.widgets.Composite)
253      */

254     protected Control createExtendedContentArea(Composite parent) {
255         return null;
256     }
257
258     /**
259      * @see org.eclipse.ui.dialogs.FilteredItemsSelectionDialog#createFilter()
260      */

261     protected ItemsFilter createFilter() {
262         return new DebugTypeItemsFilter();
263     }
264
265     /**
266      * @see org.eclipse.ui.dialogs.FilteredItemsSelectionDialog#fillContentProvider(org.eclipse.ui.dialogs.FilteredItemsSelectionDialog.AbstractContentProvider, org.eclipse.ui.dialogs.FilteredItemsSelectionDialog.ItemsFilter, org.eclipse.core.runtime.IProgressMonitor)
267      */

268     protected void fillContentProvider(AbstractContentProvider contentProvider, ItemsFilter itemsFilter, IProgressMonitor progressMonitor) throws CoreException {
269         if(fTypes != null && fTypes.length > 0) {
270             for(int i = 0; i < fTypes.length; i++) {
271                 if(itemsFilter.isConsistentItem(fTypes[i])) {
272                     contentProvider.add(fTypes[i], itemsFilter);
273                 }
274             }
275         }
276     }
277     
278     /**
279      * @see org.eclipse.ui.dialogs.FilteredItemsSelectionDialog#getElementName(java.lang.Object)
280      */

281     public String JavaDoc getElementName(Object JavaDoc item) {
282         if(item instanceof IType) {
283             return ((IType)item).getElementName();
284         }
285         return null;
286     }
287 }
288
Popular Tags