KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > ui > infoviews > AbstractInfoView


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 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.ui.infoviews;
12
13 import org.eclipse.core.runtime.IAdaptable;
14
15 import org.eclipse.swt.SWT;
16 import org.eclipse.swt.graphics.Color;
17 import org.eclipse.swt.graphics.RGB;
18 import org.eclipse.swt.widgets.Composite;
19 import org.eclipse.swt.widgets.Control;
20 import org.eclipse.swt.widgets.Display;
21 import org.eclipse.swt.widgets.Menu;
22 import org.eclipse.swt.widgets.Shell;
23
24 import org.eclipse.jface.action.IAction;
25 import org.eclipse.jface.action.IMenuListener;
26 import org.eclipse.jface.action.IMenuManager;
27 import org.eclipse.jface.action.IToolBarManager;
28 import org.eclipse.jface.action.MenuManager;
29 import org.eclipse.jface.action.Separator;
30 import org.eclipse.jface.resource.ColorRegistry;
31 import org.eclipse.jface.resource.JFaceResources;
32 import org.eclipse.jface.util.IPropertyChangeListener;
33 import org.eclipse.jface.util.PropertyChangeEvent;
34 import org.eclipse.jface.viewers.ISelection;
35 import org.eclipse.jface.viewers.ISelectionProvider;
36 import org.eclipse.jface.viewers.IStructuredSelection;
37
38 import org.eclipse.jface.text.ITextSelection;
39
40 import org.eclipse.ui.IActionBars;
41 import org.eclipse.ui.IPartListener2;
42 import org.eclipse.ui.ISelectionListener;
43 import org.eclipse.ui.IWorkbenchPart;
44 import org.eclipse.ui.IWorkbenchPartReference;
45 import org.eclipse.ui.PlatformUI;
46 import org.eclipse.ui.actions.ActionFactory;
47 import org.eclipse.ui.part.ViewPart;
48 import org.eclipse.ui.texteditor.ITextEditorActionConstants;
49
50 import org.eclipse.jdt.core.ICompilationUnit;
51 import org.eclipse.jdt.core.IJavaElement;
52 import org.eclipse.jdt.core.ILocalVariable;
53 import org.eclipse.jdt.core.IType;
54 import org.eclipse.jdt.core.JavaModelException;
55
56 import org.eclipse.jdt.ui.IContextMenuConstants;
57 import org.eclipse.jdt.ui.JavaElementLabels;
58 import org.eclipse.jdt.ui.actions.SelectionDispatchAction;
59
60 import org.eclipse.jdt.internal.ui.javaeditor.JavaEditor;
61 import org.eclipse.jdt.internal.ui.util.SelectionUtil;
62
63 /**
64  * Abstract class for views which show information for a given element.
65  *
66  * @since 3.0
67  */

68 public abstract class AbstractInfoView extends ViewPart implements ISelectionListener, IMenuListener, IPropertyChangeListener {
69
70
71     /** JavaElementLabels flags used for the title */
72     private final long TITLE_FLAGS= JavaElementLabels.ALL_FULLY_QUALIFIED
73         | JavaElementLabels.M_PRE_RETURNTYPE | JavaElementLabels.M_PARAMETER_TYPES | JavaElementLabels.M_PARAMETER_NAMES | JavaElementLabels.M_EXCEPTIONS
74         | JavaElementLabels.F_PRE_TYPE_SIGNATURE | JavaElementLabels.M_PRE_TYPE_PARAMETERS | JavaElementLabels.T_TYPE_PARAMETERS
75         | JavaElementLabels.USE_RESOLVED;
76     private final long LOCAL_VARIABLE_TITLE_FLAGS= TITLE_FLAGS & ~JavaElementLabels.F_FULLY_QUALIFIED | JavaElementLabels.F_POST_QUALIFIED;
77     
78     /** JavaElementLabels flags used for the tool tip text */
79     private static final long TOOLTIP_LABEL_FLAGS= JavaElementLabels.DEFAULT_QUALIFIED | JavaElementLabels.ROOT_POST_QUALIFIED | JavaElementLabels.APPEND_ROOT_PATH |
80             JavaElementLabels.M_PARAMETER_TYPES | JavaElementLabels.M_PARAMETER_NAMES | JavaElementLabels.M_APP_RETURNTYPE | JavaElementLabels.M_EXCEPTIONS |
81             JavaElementLabels.F_APP_TYPE_SIGNATURE | JavaElementLabels.T_TYPE_PARAMETERS;
82
83
84     /*
85      * @see IPartListener2
86      */

87     private IPartListener2 fPartListener= new IPartListener2() {
88         public void partVisible(IWorkbenchPartReference ref) {
89             if (ref.getId().equals(getSite().getId())) {
90                 IWorkbenchPart activePart= ref.getPage().getActivePart();
91                 if (activePart != null)
92                     selectionChanged(activePart, ref.getPage().getSelection());
93                 startListeningForSelectionChanges();
94             }
95         }
96         public void partHidden(IWorkbenchPartReference ref) {
97             if (ref.getId().equals(getSite().getId()))
98                 stopListeningForSelectionChanges();
99         }
100         public void partInputChanged(IWorkbenchPartReference ref) {
101             if (!ref.getId().equals(getSite().getId()))
102                 computeAndSetInput(ref.getPart(false));
103         }
104         public void partActivated(IWorkbenchPartReference ref) {
105         }
106         public void partBroughtToTop(IWorkbenchPartReference ref) {
107         }
108         public void partClosed(IWorkbenchPartReference ref) {
109         }
110         public void partDeactivated(IWorkbenchPartReference ref) {
111         }
112         public void partOpened(IWorkbenchPartReference ref) {
113         }
114     };
115
116
117     /** The current input. */
118     protected IJavaElement fCurrentViewInput;
119     /** The copy to clipboard action. */
120     private SelectionDispatchAction fCopyToClipboardAction;
121     /** The goto input action. */
122     private GotoInputAction fGotoInputAction;
123     /** Counts the number of background computation requests. */
124     private volatile int fComputeCount;
125     
126     /**
127      * Background color.
128      * @since 3.2
129      */

130     private Color fBackgroundColor;
131     private RGB fBackgroundColorRGB;
132     
133
134     /**
135      * Set the input of this view.
136      *
137      * @param input the input object
138      */

139     abstract protected void setInput(Object JavaDoc input);
140
141     /**
142      * Computes the input for this view based on the given element.
143      *
144      * @param element the element from which to compute the input
145      * @return the input or <code>null</code> if the input was not computed successfully
146      */

147     abstract protected Object JavaDoc computeInput(Object JavaDoc element);
148
149     /**
150      * Create the part control.
151      *
152      * @param parent the parent control
153      * @see IWorkbenchPart#createPartControl(org.eclipse.swt.widgets.Composite)
154      */

155     abstract protected void internalCreatePartControl(Composite parent);
156
157     /**
158      * Set the view's foreground color.
159      *
160      * @param color the SWT color
161      */

162     abstract protected void setForeground(Color color);
163
164     /**
165      * Set the view's background color.
166      *
167      * @param color the SWT color
168      */

169     abstract protected void setBackground(Color color);
170
171     /**
172      * Returns the view's primary control.
173      *
174      * @return the primary control
175      */

176     abstract Control getControl();
177
178     /**
179      * Returns the context ID for the Help system
180      *
181      * @return the string used as ID for the Help context
182      * @since 3.1
183      */

184     abstract protected String JavaDoc getHelpContextId();
185
186     /*
187      * @see IWorkbenchPart#createPartControl(org.eclipse.swt.widgets.Composite)
188      */

189     public final void createPartControl(Composite parent) {
190         internalCreatePartControl(parent);
191         inititalizeColors();
192         getSite().getWorkbenchWindow().getPartService().addPartListener(fPartListener);
193         createContextMenu();
194         createActions();
195         fillActionBars(getViewSite().getActionBars());
196         PlatformUI.getWorkbench().getHelpSystem().setHelp(getControl(), getHelpContextId());
197     }
198
199     /**
200      * Creates the actions and action groups for this view.
201      */

202     protected void createActions() {
203         fGotoInputAction= new GotoInputAction(this);
204         fGotoInputAction.setEnabled(false);
205         fCopyToClipboardAction= new CopyToClipboardAction(getViewSite());
206
207         ISelectionProvider provider= getSelectionProvider();
208         if (provider != null)
209             provider.addSelectionChangedListener(fCopyToClipboardAction);
210     }
211
212     /**
213      * Creates the context menu for this view.
214      */

215     protected void createContextMenu() {
216         MenuManager menuManager= new MenuManager("#PopupMenu"); //$NON-NLS-1$
217
menuManager.setRemoveAllWhenShown(true);
218         menuManager.addMenuListener(this);
219         Menu contextMenu= menuManager.createContextMenu(getControl());
220         getControl().setMenu(contextMenu);
221         getSite().registerContextMenu(menuManager, getSelectionProvider());
222     }
223
224     /*
225      * @see IMenuListener#menuAboutToShow(org.eclipse.jface.action.IMenuManager)
226      */

227     public void menuAboutToShow(IMenuManager menu) {
228         menu.add(new Separator(IContextMenuConstants.GROUP_OPEN));
229         menu.add(new Separator(ITextEditorActionConstants.GROUP_EDIT));
230         menu.add(new Separator(IContextMenuConstants.GROUP_ADDITIONS));
231
232         IAction action;
233
234         action= getCopyToClipboardAction();
235         if (action != null)
236             menu.appendToGroup(ITextEditorActionConstants.GROUP_EDIT, action);
237
238         action= getSelectAllAction();
239         if (action != null)
240             menu.appendToGroup(ITextEditorActionConstants.GROUP_EDIT, action);
241
242         menu.appendToGroup(IContextMenuConstants.GROUP_OPEN, fGotoInputAction);
243     }
244
245     protected IAction getSelectAllAction() {
246         return null;
247     }
248
249     protected IAction getCopyToClipboardAction() {
250         return fCopyToClipboardAction;
251     }
252
253     /**
254      * Returns the input of this view.
255      *
256      * @return input the input object or <code>null</code> if not input is set
257      */

258     protected IJavaElement getInput() {
259         return fCurrentViewInput;
260     }
261
262     // Helper method
263
ISelectionProvider getSelectionProvider() {
264         return getViewSite().getSelectionProvider();
265     }
266
267     /**
268      * Fills the actions bars.
269      * <p>
270      * Subclasses may extend.
271      *
272      * @param actionBars the action bars
273      */

274     protected void fillActionBars(IActionBars actionBars) {
275         IToolBarManager toolBar= actionBars.getToolBarManager();
276         fillToolBar(toolBar);
277
278         IAction action;
279
280         action= getCopyToClipboardAction();
281         if (action != null)
282             actionBars.setGlobalActionHandler(ActionFactory.COPY.getId(), action);
283
284         action= getSelectAllAction();
285         if (action != null)
286             actionBars.setGlobalActionHandler(ActionFactory.SELECT_ALL.getId(), action);
287     }
288
289     /**
290      * Fills the tool bar.
291      * <p>
292      * Default is to do nothing.</p>
293      *
294      * @param tbm the tool bar manager
295      */

296     protected void fillToolBar(IToolBarManager tbm) {
297         tbm.add(fGotoInputAction);
298     }
299
300     /*
301      * @see org.eclipse.jdt.internal.ui.infoviews.AbstractInfoView#inititalizeColors()
302      * @since 3.2
303      */

304     private void inititalizeColors() {
305         if (getSite().getShell().isDisposed())
306             return;
307
308         Display display= getSite().getShell().getDisplay();
309         if (display == null || display.isDisposed())
310             return;
311
312         setForeground(display.getSystemColor(SWT.COLOR_INFO_FOREGROUND));
313         
314         ColorRegistry registry= JFaceResources.getColorRegistry();
315         registry.addListener(this);
316         
317         fBackgroundColorRGB= registry.getRGB(getBackgroundColorKey());
318         Color bgColor;
319         if (fBackgroundColorRGB == null) {
320             bgColor= display.getSystemColor(SWT.COLOR_INFO_BACKGROUND);
321             fBackgroundColorRGB= bgColor.getRGB();
322         } else {
323             bgColor= new Color(display, fBackgroundColorRGB);
324             fBackgroundColor= bgColor;
325         }
326         
327         setBackground(bgColor);
328     }
329     
330     /**
331      * The preference key for the background color.
332      *
333      * @return the background color key
334      * @since 3.2
335      */

336     abstract protected String JavaDoc getBackgroundColorKey();
337     
338     public void propertyChange(PropertyChangeEvent event) {
339         if (getBackgroundColorKey().equals(event.getProperty()))
340             inititalizeColors();
341     }
342
343     /**
344      * Start to listen for selection changes.
345      */

346     protected void startListeningForSelectionChanges() {
347         getSite().getPage().addPostSelectionListener(this);
348     }
349
350     /**
351      * Stop to listen for selection changes.
352      */

353     protected void stopListeningForSelectionChanges() {
354         getSite().getPage().removePostSelectionListener(this);
355     }
356
357     /*
358      * @see ISelectionListener#selectionChanged(org.eclipse.ui.IWorkbenchPart, org.eclipse.jface.viewers.ISelection)
359      */

360     public void selectionChanged(IWorkbenchPart part, ISelection selection) {
361         if (part.equals(this))
362             return;
363
364         computeAndSetInput(part);
365     }
366
367     /**
368      * Tells whether the new input should be ignored
369      * if the current input is the same.
370      *
371      * @param je the new input
372      * @param selection the current selection from the part that provides the input
373      * @return <code>true</code> if the new input should be ignored
374      */

375     protected boolean isIgnoringNewInput(IJavaElement je, IWorkbenchPart part, ISelection selection) {
376         return fCurrentViewInput != null && fCurrentViewInput.equals(je) && je != null;
377     }
378
379     /**
380      * Finds and returns the Java element selected in the given part.
381      *
382      * @param part the workbench part for which to find the selected Java element
383      * @param selection the selection
384      * @return the selected Java element
385      */

386     protected IJavaElement findSelectedJavaElement(IWorkbenchPart part, ISelection selection) {
387         Object JavaDoc element;
388         try {
389             if (part instanceof JavaEditor && selection instanceof ITextSelection) {
390                 IJavaElement[] elements= TextSelectionConverter.codeResolve((JavaEditor)part, (ITextSelection)selection);
391                 if (elements != null && elements.length > 0)
392                     return elements[0];
393                 else
394                     return null;
395             } else if (selection instanceof IStructuredSelection) {
396                 element= SelectionUtil.getSingleElement(selection);
397             } else {
398                 return null;
399             }
400         } catch (JavaModelException e) {
401             return null;
402         }
403
404         return findJavaElement(element);
405     }
406
407     /**
408      * Tries to get a Java element out of the given element.
409      *
410      * @param element an object
411      * @return the Java element represented by the given element or <code>null</code>
412      */

413     private IJavaElement findJavaElement(Object JavaDoc element) {
414
415         if (element == null)
416             return null;
417
418         IJavaElement je= null;
419         if (element instanceof IAdaptable)
420             je= (IJavaElement)((IAdaptable)element).getAdapter(IJavaElement.class);
421
422         return je;
423     }
424
425     /**
426      * Finds and returns the type for the given CU.
427      *
428      * @param cu the compilation unit
429      * @return the type with same name as the given CU or the first type in the CU
430      */

431     protected IType getTypeForCU(ICompilationUnit cu) {
432
433         if (cu == null || !cu.exists())
434             return null;
435
436         // Use primary type if possible
437
IType primaryType= cu.findPrimaryType();
438         if (primaryType != null)
439             return primaryType;
440
441         // Use first top-level type
442
try {
443             IType[] types= cu.getTypes();
444             if (types.length > 0)
445                 return types[0];
446             else
447                 return null;
448         } catch (JavaModelException ex) {
449             return null;
450         }
451     }
452
453     /*
454      * @see IWorkbenchPart#dispose()
455      */

456     final public void dispose() {
457         // cancel possible running computation
458
fComputeCount++;
459
460         getSite().getWorkbenchWindow().getPartService().removePartListener(fPartListener);
461
462         ISelectionProvider provider= getSelectionProvider();
463         if (provider != null)
464             provider.removeSelectionChangedListener(fCopyToClipboardAction);
465
466         JFaceResources.getColorRegistry().removeListener(this);
467         fBackgroundColorRGB= null;
468         if (fBackgroundColor != null) {
469             fBackgroundColor.dispose();
470             fBackgroundColor= null;
471         }
472         
473         internalDispose();
474
475     }
476
477     /*
478      * @see IWorkbenchPart#dispose()
479      */

480     abstract protected void internalDispose();
481
482     /**
483      * Determines all necessary details and delegates the computation into
484      * a background thread.
485      *
486      * @param part the workbench part
487      */

488     private void computeAndSetInput(final IWorkbenchPart part) {
489
490         final int currentCount= ++fComputeCount;
491
492         ISelectionProvider provider= part.getSite().getSelectionProvider();
493         if (provider == null)
494             return;
495
496         final ISelection selection= provider.getSelection();
497         if (selection == null || selection.isEmpty())
498             return;
499
500         Thread JavaDoc thread= new Thread JavaDoc("Info view input computer") { //$NON-NLS-1$
501
public void run() {
502                 if (currentCount != fComputeCount)
503                     return;
504
505                 final IJavaElement je= findSelectedJavaElement(part, selection);
506
507                 if (isIgnoringNewInput(je, part, selection))
508                     return;
509
510                 // The actual computation
511
final Object JavaDoc input= computeInput(je);
512                 if (input == null)
513                     return;
514
515                 Shell shell= getSite().getShell();
516                 if (shell.isDisposed())
517                     return;
518
519                 Display display= shell.getDisplay();
520                 if (display.isDisposed())
521                     return;
522
523                 display.asyncExec(new Runnable JavaDoc() {
524                     /*
525                      * @see java.lang.Runnable#run()
526                      */

527                     public void run() {
528
529                         if (fComputeCount != currentCount || getViewSite().getShell().isDisposed())
530                             return;
531
532                         fCurrentViewInput= je;
533                         doSetInput(input);
534                     }
535                 });
536             }
537         };
538
539         thread.setDaemon(true);
540         thread.setPriority(Thread.MIN_PRIORITY);
541         thread.start();
542     }
543
544     private void doSetInput(Object JavaDoc input) {
545         setInput(input);
546
547         fGotoInputAction.setEnabled(true);
548
549         IJavaElement inputElement= getInput();
550         
551         long flags;
552         if (inputElement instanceof ILocalVariable)
553             flags= LOCAL_VARIABLE_TITLE_FLAGS;
554         else
555             flags= TITLE_FLAGS;
556         
557         setContentDescription(JavaElementLabels.getElementLabel(inputElement, flags));
558         setTitleToolTip(JavaElementLabels.getElementLabel(inputElement, TOOLTIP_LABEL_FLAGS));
559     }
560 }
561
Popular Tags