KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > texteditor > StatusTextEditor


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
12 package org.eclipse.ui.texteditor;
13
14
15 import org.eclipse.swt.SWT;
16 import org.eclipse.swt.custom.StackLayout;
17 import org.eclipse.swt.layout.FillLayout;
18 import org.eclipse.swt.widgets.Composite;
19 import org.eclipse.swt.widgets.Control;
20
21 import org.eclipse.core.runtime.CoreException;
22 import org.eclipse.core.runtime.IStatus;
23
24 import org.eclipse.ui.IEditorInput;
25
26
27 /**
28  * Capable of handling input elements that have an associated status with them.
29  * @since 2.0
30  */

31 public class StatusTextEditor extends AbstractTextEditor {
32
33     /** The root composite of this editor */
34     private Composite fParent;
35     /** The layout used to manage the regular and the status page */
36     private StackLayout fStackLayout;
37     /** The root composite for the regular page */
38     private Composite fDefaultComposite;
39     /** The status page */
40     private Control fStatusControl;
41
42     /*
43      * @see IWorkbenchPart#createPartControl(org.eclipse.swt.widgets.Composite)
44      */

45     public void createPartControl(Composite parent) {
46
47         fParent= new Composite(parent, SWT.NONE);
48         fStackLayout= new StackLayout();
49         fParent.setLayout(fStackLayout);
50
51         fDefaultComposite= new Composite(fParent, SWT.NONE);
52         fDefaultComposite.setLayout(new FillLayout());
53         super.createPartControl(fDefaultComposite);
54
55         updatePartControl(getEditorInput());
56     }
57
58     /**
59      * Checks if the status of the given input is OK. If not the
60      * status control is shown rather than the default control.
61      *
62      * @param input the input whose status is checked
63      */

64     public void updatePartControl(IEditorInput input) {
65
66         if (fStatusControl != null) {
67             fStatusControl.dispose();
68             fStatusControl= null;
69         }
70
71         Control front= null;
72         if (fParent != null && input != null) {
73             if (getDocumentProvider() instanceof IDocumentProviderExtension) {
74                 IDocumentProviderExtension extension= (IDocumentProviderExtension) getDocumentProvider();
75                 IStatus status= extension.getStatus(input);
76                 if (!isErrorStatus(status)) {
77                     front= fDefaultComposite;
78                 } else {
79                     fStatusControl= createStatusControl(fParent, status);
80                     front= fStatusControl;
81                 }
82             }
83         }
84
85         if (fStackLayout.topControl != front) {
86             fStackLayout.topControl= front;
87             fParent.layout();
88             updateStatusFields();
89         }
90     }
91     
92     /*
93      * @see org.eclipse.ui.texteditor.AbstractTextEditor#validateEditorInputState()
94      * @since 3.3
95      */

96     public boolean validateEditorInputState() {
97         if (!super.validateEditorInputState())
98             return false;
99
100         if (getDocumentProvider() instanceof IDocumentProviderExtension) {
101             IDocumentProviderExtension extension= (IDocumentProviderExtension)getDocumentProvider();
102             IStatus status= extension.getStatus(getEditorInput());
103             return !isErrorStatus(status) && status.getSeverity() != IStatus.CANCEL;
104         }
105
106         return true;
107     }
108
109     /**
110      * Returns whether the given status indicates an error. Subclasses may override.
111      *
112      * @param status the status to be checked
113      * @return <code>true</code> if the status indicates an error, <code>false</code> otherwise\
114      * @since 3.0
115      */

116     protected boolean isErrorStatus(IStatus status) {
117         return status != null && status.getSeverity() == IStatus.ERROR;
118     }
119
120     /**
121      * Creates the status control for the given status.
122      * May be overridden by subclasses.
123      *
124      * @param parent the parent control
125      * @param status the status
126      * @return the new status control
127      */

128     protected Control createStatusControl(Composite parent, IStatus status) {
129         InfoForm infoForm= new InfoForm(parent);
130         infoForm.setHeaderText(getStatusHeader(status));
131         infoForm.setBannerText(getStatusBanner(status));
132         infoForm.setInfo(getStatusMessage(status));
133         return infoForm.getControl();
134     }
135
136     /**
137      * Returns a header for the given status
138      *
139      * @param status the status whose message is returned
140      * @return a header for the given status
141      */

142     protected String JavaDoc getStatusHeader(IStatus status) {
143         return ""; //$NON-NLS-1$
144
}
145
146     /**
147      * Returns a banner for the given status.
148      *
149      * @param status the status whose message is returned
150      * @return a banner for the given status
151      */

152     protected String JavaDoc getStatusBanner(IStatus status) {
153         return ""; //$NON-NLS-1$
154
}
155
156     /**
157      * Returns a message for the given status.
158      *
159      * @param status the status whose message is returned
160      * @return a message for the given status
161      */

162     protected String JavaDoc getStatusMessage(IStatus status) {
163         return status.getMessage();
164     }
165
166     /*
167      * @see AbstractTextEditor#updateStatusField(String)
168      */

169     protected void updateStatusField(String JavaDoc category) {
170         IDocumentProvider provider= getDocumentProvider();
171         if (provider instanceof IDocumentProviderExtension) {
172             IDocumentProviderExtension extension= (IDocumentProviderExtension) provider;
173             IStatus status= extension.getStatus(getEditorInput());
174             if (isErrorStatus(status)) {
175                 IStatusField field= getStatusField(category);
176                 if (field != null) {
177                     field.setText(fErrorLabel);
178                     return;
179                 }
180             }
181         }
182
183         super.updateStatusField(category);
184     }
185
186     /*
187      * @see AbstractTextEditor#doSetInput(IEditorInput)
188      */

189     protected void doSetInput(IEditorInput input) throws CoreException {
190         super.doSetInput(input);
191         if (fParent != null && !fParent.isDisposed())
192             updatePartControl(getEditorInput());
193     }
194
195     /*
196      * @see ITextEditor#doRevertToSaved()
197      */

198     public void doRevertToSaved() {
199         // http://dev.eclipse.org/bugs/show_bug.cgi?id=19014
200
super.doRevertToSaved();
201         if (fParent != null && !fParent.isDisposed())
202             updatePartControl(getEditorInput());
203     }
204
205     /*
206      * @see AbstractTextEditor#sanityCheckState(IEditorInput)
207      */

208     protected void sanityCheckState(IEditorInput input) {
209         // http://dev.eclipse.org/bugs/show_bug.cgi?id=19014
210
super.sanityCheckState(input);
211         if (fParent != null && !fParent.isDisposed())
212             updatePartControl(getEditorInput());
213     }
214
215     /*
216      * @see org.eclipse.ui.texteditor.AbstractTextEditor#handleEditorInputChanged()
217      * @since 3.1
218      */

219     protected void handleEditorInputChanged() {
220         super.handleEditorInputChanged();
221         if (fParent != null && !fParent.isDisposed())
222             updatePartControl(getEditorInput());
223     }
224
225     /*
226      * @see org.eclipse.ui.texteditor.AbstractTextEditor#handleElementContentReplaced()
227      * @since 3.1
228      */

229     protected void handleElementContentReplaced() {
230         super.handleElementContentReplaced();
231         if (fParent != null && !fParent.isDisposed())
232             updatePartControl(getEditorInput());
233     }
234 }
235
Popular Tags