KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ltk > ui > refactoring > TextStatusContextViewer


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.ltk.ui.refactoring;
12
13 import org.eclipse.core.runtime.IAdaptable;
14
15 import org.eclipse.swt.SWT;
16 import org.eclipse.swt.custom.CLabel;
17 import org.eclipse.swt.custom.ViewForm;
18 import org.eclipse.swt.events.DisposeEvent;
19 import org.eclipse.swt.events.DisposeListener;
20 import org.eclipse.swt.graphics.Image;
21 import org.eclipse.swt.widgets.Composite;
22 import org.eclipse.swt.widgets.Control;
23
24 import org.eclipse.jface.dialogs.Dialog;
25 import org.eclipse.jface.resource.ImageDescriptor;
26 import org.eclipse.jface.text.IDocument;
27 import org.eclipse.jface.text.IRegion;
28 import org.eclipse.jface.text.source.SourceViewer;
29
30 import org.eclipse.ui.model.IWorkbenchAdapter;
31
32 import org.eclipse.ltk.internal.ui.refactoring.RefactoringUIMessages;
33
34 /**
35  * An abstract base implementation of a status context viewer that presents
36  * textual context information.
37  * <p>
38  * Subclasses need to implement {@link #createSourceViewer(Composite)} to create
39  * the correct source viewer. They should use the method {@link #updateTitle(IAdaptable)}
40  * and {@link #setInput(IDocument, IRegion)} to set the title text and image and to
41  * populate the source viewer.
42  * </p>
43  *
44  * @since 3.0
45  */

46 public abstract class TextStatusContextViewer implements IStatusContextViewer {
47
48     private SourceViewer fSourceViewer;
49     private ViewForm fForm;
50     private CLabel fLabel;
51     private Image fPaneImage;
52
53     /**
54      * Returns the internal source viewer.
55      *
56      * @return the internal source viewer or <code>null</code> if the
57      * source viewer hasn't been created yet
58      */

59     protected SourceViewer getSourceViewer() {
60         return fSourceViewer;
61     }
62     
63     /**
64      * Hook to create the source viewer used to present the textual context
65      * information.
66      *
67      * @param parent the composite to be used as the source viewer's
68      * parent
69      * @return the source viewer to be used
70      */

71     protected abstract SourceViewer createSourceViewer(Composite parent);
72
73     //---- Helper methods to populate viewer -------------------------------
74

75     /**
76      * Updates the title image and text of the pane surrounding the source
77      * viewer. The image and text is determined by retrieving the <code>
78      * IWorkbenchAdapter</code> for the given element. If the element doen't
79      * provide a <code>IWorkbenchAdapter</code> or if the element is <code>
80      * null</code> the image is reseted and a default label is shown.
81      *
82      * @param element the element providing the image and label for the title.
83      * Can be <code>null</code> to reset the image and text
84      */

85     protected void updateTitle(IAdaptable element) {
86         String JavaDoc title= null;
87         ImageDescriptor imageDescriptor= null;
88         if (element != null) {
89             IWorkbenchAdapter adapter= (IWorkbenchAdapter)element.getAdapter(IWorkbenchAdapter.class);
90             if (adapter != null) {
91                 title= adapter.getLabel(element);
92                 imageDescriptor= adapter.getImageDescriptor(element);
93             }
94         }
95         if (title == null || title.length() == 0)
96             title= RefactoringUIMessages.RefactoringStatusViewer_Problem_context;
97         fLabel.setText(title);
98         if (fPaneImage != null) {
99             fPaneImage.dispose();
100             fPaneImage= null;
101         }
102         if (imageDescriptor != null) {
103             fPaneImage= imageDescriptor.createImage(getControl().getDisplay());
104         }
105         fLabel.setImage(fPaneImage);
106     }
107     
108     /**
109      * Sets the input of the source viewer to the given document and reveals the
110      * region determined by the given parameter region.
111      *
112      * @param document the document to present
113      * @param region the region to reveal.
114      */

115     protected void setInput(IDocument document, IRegion region) {
116         Control ctrl= getControl();
117         if (ctrl != null && ctrl.isDisposed())
118             ctrl= null;
119         try {
120             if (ctrl != null)
121                 ctrl.setRedraw(false);
122             fSourceViewer.setInput(document);
123             if (region != null && document != null) {
124                 int offset= region.getOffset();
125                 int length= region.getLength();
126                 if (offset >= 0 && length >= 0) {
127                     fSourceViewer.setSelectedRange(offset, length);
128                     fSourceViewer.revealRange(offset, length);
129                 }
130             }
131         } finally {
132             if (ctrl != null)
133                 ctrl.setRedraw(true);
134         }
135     }
136     
137     //---- Methods defined in IStatusContextViewer -------------------------------
138

139     /**
140      * {@inheritDoc}
141      */

142     public void createControl(Composite parent) {
143         fForm= new ViewForm(parent, SWT.BORDER | SWT.FLAT);
144         fForm.marginWidth= 0;
145         fForm.marginHeight= 0;
146         fLabel= new CLabel(fForm, SWT.NONE);
147         fForm.setTopLeft(fLabel);
148         fForm.addDisposeListener(new DisposeListener() {
149             public void widgetDisposed(DisposeEvent e) {
150                 if (fPaneImage != null)
151                     fPaneImage.dispose();
152             }
153         });
154
155         Dialog.applyDialogFont(parent);
156
157         fSourceViewer= createSourceViewer(fForm);
158         fSourceViewer.setEditable(false);
159         fForm.setContent(fSourceViewer.getControl());
160     }
161
162     /**
163      * {@inheritDoc}
164      */

165     public Control getControl() {
166         return fForm;
167     }
168 }
169
170
Popular Tags