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.graphics.Image;
013:
014: import org.eclipse.core.runtime.Assert;
015:
016: import org.eclipse.jface.action.IStatusLineManager;
017: import org.eclipse.jface.viewers.ISelectionChangedListener;
018: import org.eclipse.jface.viewers.ISelectionProvider;
019: import org.eclipse.jface.viewers.SelectionChangedEvent;
020:
021: /**
022: * An editor status line.
023: * The selection provider of the editor triggers the status line to be cleared.
024: * @since 2.1
025: */
026: class EditorStatusLine implements IEditorStatusLine {
027:
028: /**
029: * Clears the status line on selection changed.
030: */
031: private class StatusLineClearer implements
032: ISelectionChangedListener {
033: /*
034: * @see ISelectionChangedListener#selectionChanged(org.eclipse.jface.viewers.SelectionChangedEvent)
035: */
036: public void selectionChanged(SelectionChangedEvent event) {
037: fStatusLineManager.setErrorMessage(null, null);
038: fStatusLineManager.setMessage(null, null);
039:
040: Assert.isTrue(this == fStatusLineClearer);
041: uninstallStatusLineClearer();
042: }
043: }
044:
045: /** The status line manager. */
046: private final IStatusLineManager fStatusLineManager;
047:
048: /** The selection provider. */
049: private final ISelectionProvider fSelectionProvider;
050:
051: /** The status line clearer, <code>null</code> if not installed. */
052: private StatusLineClearer fStatusLineClearer;
053:
054: /**
055: * Constructor for EditorStatusLine.
056: *
057: * @param statusLineManager the status line manager
058: * @param selectionProvider the selection provider
059: */
060: public EditorStatusLine(IStatusLineManager statusLineManager,
061: ISelectionProvider selectionProvider) {
062:
063: Assert.isNotNull(statusLineManager);
064: Assert.isNotNull(selectionProvider);
065:
066: fStatusLineManager = statusLineManager;
067: fSelectionProvider = selectionProvider;
068: }
069:
070: /**
071: * Returns the status line manager.
072: *
073: * @return the status line manager
074: */
075: public IStatusLineManager getStatusLineManager() {
076: return fStatusLineManager;
077: }
078:
079: /**
080: * Returns the selection provider.
081: *
082: * @return the selection provider
083: */
084: public ISelectionProvider getSelectionProvider() {
085: return fSelectionProvider;
086: }
087:
088: /*
089: * @see org.eclipse.ui.texteditor.IStatusLine#setMessage(boolean, String, Image)
090: */
091: public void setMessage(boolean error, String message, Image image) {
092:
093: if (error)
094: fStatusLineManager.setErrorMessage(image, message);
095: else {
096: // Clear error message
097: fStatusLineManager.setErrorMessage(null, null);
098:
099: fStatusLineManager.setMessage(image, message);
100: }
101:
102: if (isMessageEmpty(message))
103: uninstallStatusLineClearer();
104: else
105: installStatusLineClearer();
106: }
107:
108: /**
109: * Returns whether this given string is empty.
110: *
111: * @param message a string
112: * @return <code>true</code> if the string is <code>null</code>, has 0 length or only white space characters.
113: */
114: private static boolean isMessageEmpty(String message) {
115: return message == null || message.trim().length() == 0;
116: }
117:
118: /**
119: * Uninstalls the status line clearer.
120: */
121: private void uninstallStatusLineClearer() {
122: if (fStatusLineClearer == null)
123: return;
124:
125: fSelectionProvider
126: .removeSelectionChangedListener(fStatusLineClearer);
127: fStatusLineClearer = null;
128: }
129:
130: /**
131: * Installs the status line clearer.
132: */
133: private void installStatusLineClearer() {
134: if (fStatusLineClearer != null)
135: return;
136:
137: StatusLineClearer statusLineClearer = new StatusLineClearer();
138: fSelectionProvider
139: .addSelectionChangedListener(statusLineClearer);
140: fStatusLineClearer = statusLineClearer;
141: }
142: }
|