Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: c1f3afeae14da55b2398a24027a119dc9ef06459 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
/*******************************************************************************
 * Copyright (c) 2000, 2008 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/

package org.eclipse.ui.texteditor;

import org.eclipse.swt.graphics.Image;

import org.eclipse.core.runtime.Assert;

import org.eclipse.jface.action.IStatusLineManager;
import org.eclipse.jface.viewers.ISelectionChangedListener;
import org.eclipse.jface.viewers.ISelectionProvider;
import org.eclipse.jface.viewers.SelectionChangedEvent;

/**
 * An editor status line.
 * The selection provider of the editor triggers the status line to be cleared.
 * @since 2.1
 */
class EditorStatusLine implements IEditorStatusLine {

	/**
	 * Clears the status line on selection changed.
	 */
	private class StatusLineClearer implements ISelectionChangedListener {
		@Override
		public void selectionChanged(SelectionChangedEvent event) {
			fStatusLineManager.setErrorMessage(null, null);
			fStatusLineManager.setMessage(null, null);

			Assert.isTrue(this == fStatusLineClearer);
			uninstallStatusLineClearer();
		}
	}

	/** The status line manager. */
	private final IStatusLineManager fStatusLineManager;

	/** The selection provider. */
	private final ISelectionProvider fSelectionProvider;

	/** The status line clearer, <code>null</code> if not installed. */
	private StatusLineClearer fStatusLineClearer;

	/**
	 * Constructor for EditorStatusLine.
	 *
	 * @param statusLineManager the status line manager
	 * @param selectionProvider the selection provider
	 */
	public EditorStatusLine(IStatusLineManager statusLineManager, ISelectionProvider selectionProvider) {

		Assert.isNotNull(statusLineManager);
		Assert.isNotNull(selectionProvider);

		fStatusLineManager= statusLineManager;
		fSelectionProvider= selectionProvider;
	}

	/**
	 * Returns the status line manager.
	 *
	 * @return the status line manager
	 */
	public IStatusLineManager getStatusLineManager() {
		return fStatusLineManager;
	}

	/**
	 * Returns the selection provider.
	 *
	 * @return the selection provider
	 */
	public ISelectionProvider getSelectionProvider() {
		return fSelectionProvider;
	}

	@Override
	public void setMessage(boolean error, String message, Image image) {

		if (error)
			fStatusLineManager.setErrorMessage(image, message);
		else {
			// Clear error message
			fStatusLineManager.setErrorMessage(null, null);

			fStatusLineManager.setMessage(image, message);
		}

		if (isMessageEmpty(message))
			uninstallStatusLineClearer();
		else
			installStatusLineClearer();
	}

	/**
	 * Returns whether this given string is empty.
	 *
	 * @param message a string
	 * @return <code>true</code> if the string is <code>null</code>, has 0 length or only white space characters.
	 */
	private static boolean isMessageEmpty(String message) {
		return message == null || message.trim().length() == 0;
	}

	/**
	 * Uninstalls the status line clearer.
	 */
	private void uninstallStatusLineClearer() {
		if (fStatusLineClearer == null)
			return;

		fSelectionProvider.removeSelectionChangedListener(fStatusLineClearer);
		fStatusLineClearer= null;
	}

	/**
	 * Installs the status line clearer.
	 */
	private void installStatusLineClearer() {
		if (fStatusLineClearer != null)
			return;

		StatusLineClearer statusLineClearer= new StatusLineClearer();
		fSelectionProvider.addSelectionChangedListener(statusLineClearer);
		fStatusLineClearer= statusLineClearer;
	}
}

Back to the top