001: /*******************************************************************************
002: * Copyright (c) 2000, 2006 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.ui.texteditor;
011:
012: import org.eclipse.swt.SWT;
013: import org.eclipse.swt.custom.StackLayout;
014: import org.eclipse.swt.layout.FillLayout;
015: import org.eclipse.swt.widgets.Composite;
016: import org.eclipse.swt.widgets.Control;
017:
018: import org.eclipse.core.runtime.CoreException;
019: import org.eclipse.core.runtime.IStatus;
020:
021: import org.eclipse.ui.IEditorInput;
022:
023: /**
024: * Capable of handling input elements that have an associated status with them.
025: * @since 2.0
026: */
027: public class StatusTextEditor extends AbstractTextEditor {
028:
029: /** The root composite of this editor */
030: private Composite fParent;
031: /** The layout used to manage the regular and the status page */
032: private StackLayout fStackLayout;
033: /** The root composite for the regular page */
034: private Composite fDefaultComposite;
035: /** The status page */
036: private Control fStatusControl;
037:
038: /*
039: * @see IWorkbenchPart#createPartControl(org.eclipse.swt.widgets.Composite)
040: */
041: public void createPartControl(Composite parent) {
042:
043: fParent = new Composite(parent, SWT.NONE);
044: fStackLayout = new StackLayout();
045: fParent.setLayout(fStackLayout);
046:
047: fDefaultComposite = new Composite(fParent, SWT.NONE);
048: fDefaultComposite.setLayout(new FillLayout());
049: super .createPartControl(fDefaultComposite);
050:
051: updatePartControl(getEditorInput());
052: }
053:
054: /**
055: * Checks if the status of the given input is OK. If not the
056: * status control is shown rather than the default control.
057: *
058: * @param input the input whose status is checked
059: */
060: public void updatePartControl(IEditorInput input) {
061:
062: if (fStatusControl != null) {
063: fStatusControl.dispose();
064: fStatusControl = null;
065: }
066:
067: Control front = null;
068: if (fParent != null && input != null) {
069: if (getDocumentProvider() instanceof IDocumentProviderExtension) {
070: IDocumentProviderExtension extension = (IDocumentProviderExtension) getDocumentProvider();
071: IStatus status = extension.getStatus(input);
072: if (!isErrorStatus(status)) {
073: front = fDefaultComposite;
074: } else {
075: fStatusControl = createStatusControl(fParent,
076: status);
077: front = fStatusControl;
078: }
079: }
080: }
081:
082: if (fStackLayout.topControl != front) {
083: fStackLayout.topControl = front;
084: fParent.layout();
085: updateStatusFields();
086: }
087: }
088:
089: /*
090: * @see org.eclipse.ui.texteditor.AbstractTextEditor#validateEditorInputState()
091: * @since 3.3
092: */
093: public boolean validateEditorInputState() {
094: if (!super .validateEditorInputState())
095: return false;
096:
097: if (getDocumentProvider() instanceof IDocumentProviderExtension) {
098: IDocumentProviderExtension extension = (IDocumentProviderExtension) getDocumentProvider();
099: IStatus status = extension.getStatus(getEditorInput());
100: return !isErrorStatus(status)
101: && status.getSeverity() != IStatus.CANCEL;
102: }
103:
104: return true;
105: }
106:
107: /**
108: * Returns whether the given status indicates an error. Subclasses may override.
109: *
110: * @param status the status to be checked
111: * @return <code>true</code> if the status indicates an error, <code>false</code> otherwise\
112: * @since 3.0
113: */
114: protected boolean isErrorStatus(IStatus status) {
115: return status != null && status.getSeverity() == IStatus.ERROR;
116: }
117:
118: /**
119: * Creates the status control for the given status.
120: * May be overridden by subclasses.
121: *
122: * @param parent the parent control
123: * @param status the status
124: * @return the new status control
125: */
126: protected Control createStatusControl(Composite parent,
127: IStatus status) {
128: InfoForm infoForm = new InfoForm(parent);
129: infoForm.setHeaderText(getStatusHeader(status));
130: infoForm.setBannerText(getStatusBanner(status));
131: infoForm.setInfo(getStatusMessage(status));
132: return infoForm.getControl();
133: }
134:
135: /**
136: * Returns a header for the given status
137: *
138: * @param status the status whose message is returned
139: * @return a header for the given status
140: */
141: protected String getStatusHeader(IStatus status) {
142: return ""; //$NON-NLS-1$
143: }
144:
145: /**
146: * Returns a banner for the given status.
147: *
148: * @param status the status whose message is returned
149: * @return a banner for the given status
150: */
151: protected String getStatusBanner(IStatus status) {
152: return ""; //$NON-NLS-1$
153: }
154:
155: /**
156: * Returns a message for the given status.
157: *
158: * @param status the status whose message is returned
159: * @return a message for the given status
160: */
161: protected String getStatusMessage(IStatus status) {
162: return status.getMessage();
163: }
164:
165: /*
166: * @see AbstractTextEditor#updateStatusField(String)
167: */
168: protected void updateStatusField(String category) {
169: IDocumentProvider provider = getDocumentProvider();
170: if (provider instanceof IDocumentProviderExtension) {
171: IDocumentProviderExtension extension = (IDocumentProviderExtension) provider;
172: IStatus status = extension.getStatus(getEditorInput());
173: if (isErrorStatus(status)) {
174: IStatusField field = getStatusField(category);
175: if (field != null) {
176: field.setText(fErrorLabel);
177: return;
178: }
179: }
180: }
181:
182: super .updateStatusField(category);
183: }
184:
185: /*
186: * @see AbstractTextEditor#doSetInput(IEditorInput)
187: */
188: protected void doSetInput(IEditorInput input) throws CoreException {
189: super .doSetInput(input);
190: if (fParent != null && !fParent.isDisposed())
191: updatePartControl(getEditorInput());
192: }
193:
194: /*
195: * @see ITextEditor#doRevertToSaved()
196: */
197: public void doRevertToSaved() {
198: // http://dev.eclipse.org/bugs/show_bug.cgi?id=19014
199: super .doRevertToSaved();
200: if (fParent != null && !fParent.isDisposed())
201: updatePartControl(getEditorInput());
202: }
203:
204: /*
205: * @see AbstractTextEditor#sanityCheckState(IEditorInput)
206: */
207: protected void sanityCheckState(IEditorInput input) {
208: // http://dev.eclipse.org/bugs/show_bug.cgi?id=19014
209: super .sanityCheckState(input);
210: if (fParent != null && !fParent.isDisposed())
211: updatePartControl(getEditorInput());
212: }
213:
214: /*
215: * @see org.eclipse.ui.texteditor.AbstractTextEditor#handleEditorInputChanged()
216: * @since 3.1
217: */
218: protected void handleEditorInputChanged() {
219: super .handleEditorInputChanged();
220: if (fParent != null && !fParent.isDisposed())
221: updatePartControl(getEditorInput());
222: }
223:
224: /*
225: * @see org.eclipse.ui.texteditor.AbstractTextEditor#handleElementContentReplaced()
226: * @since 3.1
227: */
228: protected void handleElementContentReplaced() {
229: super.handleElementContentReplaced();
230: if (fParent != null && !fParent.isDisposed())
231: updatePartControl(getEditorInput());
232: }
233: }
|