Skip to main content

This CGIT instance is deprecated, and repositories have been moved to Gitlab or Github. See the repository descriptions for specific locations.

summaryrefslogtreecommitdiffstats
blob: 83344e421d49e257cbf203a9a8b584c516520bdc (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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
/*******************************************************************************
 * Copyright (c) 2001, 2004 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
 *     Jens Lukowski/Innoopract - initial renaming/restructuring
 *     
 *******************************************************************************/
package org.eclipse.wst.sse.ui.internal.util;

import org.eclipse.swt.custom.StyledText;
import org.eclipse.swt.custom.VerifyKeyListener;
import org.eclipse.swt.events.FocusAdapter;
import org.eclipse.swt.events.FocusEvent;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.MouseListener;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.events.VerifyEvent;
import org.eclipse.swt.widgets.Control;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.PlatformUI;


/**
 * Utility to display (and/or clear) messages on the status line.
 * 
 * @author pavery
 */
public class PlatformStatusLineUtil {

	private static class ClearErrorMessage implements Runnable {
		public void run() {
			displayMessage(null);
		}
	}

	/**
	 * Used to clear message on focus loss, change of selection, key type,
	 * etc...
	 */
	private static class OneTimeListener extends FocusAdapter implements VerifyKeyListener, SelectionListener, MouseListener {

		private Runnable fRunner = null;
		private StyledText fStyledText;

		public OneTimeListener(StyledText target, Runnable r) {
			fStyledText = target;
			fRunner = r;
			fStyledText.addVerifyKeyListener(this);
			fStyledText.addFocusListener(this);
			fStyledText.addSelectionListener(this);
			fStyledText.addMouseListener(this);
		}

		public void focusLost(FocusEvent e) {
			unhookAndRun();
		}

		public void mouseDoubleClick(MouseEvent e) {
			unhookAndRun();
		}

		public void mouseDown(MouseEvent e) {
			unhookAndRun();
		}

		public void mouseUp(MouseEvent e) {
			//
		}

		private void unhookAndRun() {
			fStyledText.removeVerifyKeyListener(this);
			fStyledText.removeFocusListener(this);
			fStyledText.removeSelectionListener(this);
			fStyledText.removeMouseListener(this);
			fStyledText.getDisplay().asyncExec(fRunner);
		}

		public void verifyKey(VerifyEvent event) {
			unhookAndRun();
		}

		public void widgetDefaultSelected(SelectionEvent e) {
			unhookAndRun();
		}

		public void widgetSelected(SelectionEvent e) {
			unhookAndRun();
		}
	}

	/**
	 * Status line will be cleared w/ key type, or selection change
	 * 
	 * @param widget
	 */
	public static void addOneTimeClearListener() {
		IEditorPart editor = getActiveEditor();
		boolean added = false;
		if (editor != null) {
			Control control = (Control) editor.getAdapter(Control.class);
			if (control instanceof StyledText) {
				addOneTimeClearListener(((StyledText) control));
				added = true;
			}
		}
		if (!added) {
			// clear the error message immediately
			displayMessage(null);
		}
	}

	private static void addOneTimeClearListener(StyledText widget) {
		new OneTimeListener(widget, new ClearErrorMessage());
	}

	/**
	 * Clears the status line immediately
	 */
	public static void clearStatusLine() {
		displayMessage(null);
	}

	/**
	 * Display a message on the status line (with a beep)
	 * 
	 * @param msg
	 */
	public static void displayErrorMessage(String msg) {
		displayMessage(msg);
		PlatformUI.getWorkbench().getDisplay().beep();
	}

	/**
	 * Display a message on the status line (no beep)
	 * 
	 * @param msg
	 */
	public static void displayMessage(String msg) {
		IEditorPart editor = getActiveEditor();
		if (editor != null) {
			editor.getEditorSite().getActionBars().getStatusLineManager().setErrorMessage(msg);
		}

	}

	private static IEditorPart getActiveEditor() {
		IEditorPart editor = null;
		IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
		if (window == null) {
			window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
		}
		if (window != null) {
			IWorkbenchPage page = window.getActivePage();
			if (page != null)
				editor = page.getActiveEditor();
		}
		return editor;
	}

	private PlatformStatusLineUtil() {
		// force use of singleton
	}
}

Back to the top