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: e78d4678a85e7ec5de5954a78e5b863378064c69 (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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
/*******************************************************************************
 * 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;

import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.core.runtime.jobs.Job;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.StyledText;
import org.eclipse.swt.events.KeyEvent;
import org.eclipse.swt.events.KeyListener;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.MouseListener;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.wst.sse.core.internal.util.Debug;
import org.eclipse.wst.sse.core.internal.util.Utilities;
import org.eclipse.wst.sse.ui.internal.view.events.CaretEvent;
import org.eclipse.wst.sse.ui.internal.view.events.ICaretListener;

/**
 * Has the responsibility of listening for key events, and mouse events,
 * deciding if the caret has moved (without a text change), and if so, will
 * notify CaretListeners that the caret has moved. Objects which are
 * interested in ALL caret postion changes will also have to listen for
 * textChanged events.
 * 
 * @deprecated - use base selection notification
 */
public class CaretMediator implements Listener {

	class CaretMediatorListener implements KeyListener, MouseListener {
		public void keyPressed(KeyEvent e) {
			internalKeyPressed(e);
		}

		public void keyReleased(KeyEvent e) {
			internalKeyReleased(e);
		}

		public void mouseDoubleClick(MouseEvent e) {
		}

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

		public void mouseUp(MouseEvent e) {
			internalMouseUp(e);
		}
	}

	class RefreshDelayJob extends Job {
		private int fDelay = 0;
		RefreshDelayJob(int delay) {
			super(SSEUIMessages.caret_update); //$NON-NLS-1$
			setSystem(true);
			fDelay = delay;
		}

		/**
		 * Setup a delayed CaretEvent firing
		 */
		void touch() {
			cancel();
			schedule(fDelay);
		}

		protected IStatus run(IProgressMonitor monitor) {
			handleEvent(null);
			return Status.OK_STATUS;
		}
	}
	
	RefreshDelayJob fDelayer = null;
	private static final int DELAY = 300;

	/** used just for debug print outs */
	private long endTime;
	private long startTime;

	protected ICaretListener[] fCaretListeners;
	protected CaretMediatorListener internalListener;
	protected StyledText textWidget;

	/**
	 * CaretMediator constructor comment.
	 */
	public CaretMediator() {
		super();
	}

	/**
	 * CaretMediator constructor comment. Must always provide the widget its
	 * supposed to listen to.
	 */
	public CaretMediator(StyledText styledTextWidget) {
		this();
		setTextWidget(styledTextWidget);
	}

	public synchronized void addCaretListener(ICaretListener listener) {
		if (Debug.debugStructuredDocument) {
			System.out.println("CaretMediator::addCaretListener. Request to add an instance of " + listener.getClass() + " as a listener on caretlistner.");//$NON-NLS-2$//$NON-NLS-1$
		}
		// make sure listener is not already in listening array
		// (and if it is, print a warning to aid debugging, if needed)

		if (Utilities.contains(fCaretListeners, listener)) {
			if (Debug.displayWarnings) {
				System.out.println("CaretMediator::addCaretListener. listener " + listener + " was added more than once. ");//$NON-NLS-2$//$NON-NLS-1$
			}
		} else {
			if (Debug.debugStructuredDocument) {
				System.out.println("CaretMediator::addCaretListener. Adding an instance of " + listener.getClass() + " as a listener on caret mediator.");//$NON-NLS-2$//$NON-NLS-1$
			}
			int oldSize = 0;
			if (fCaretListeners != null) {
				// normally won't be null, but we need to be sure, for first
				// time through
				oldSize = fCaretListeners.length;
			}
			int newSize = oldSize + 1;
			ICaretListener[] newListeners = new ICaretListener[newSize];
			if (fCaretListeners != null) {
				System.arraycopy(fCaretListeners, 0, newListeners, 0, oldSize);
			}
			// add listener to last position
			newListeners[newSize - 1] = listener;
			//
			// now switch new for old
			fCaretListeners = newListeners;

		}
	}

	protected void fireCaretEvent(CaretEvent event) {
		if (fCaretListeners != null) {
			// we must assign listeners to local variable to be thread safe,
			// since the add and remove listner methods
			// can change this object's actual instance of the listener array
			// from another thread
			// (and since object assignment is atomic, we don't need to
			// synchronize
			ICaretListener[] holdListeners = fCaretListeners;
			//
			for (int i = 0; i < holdListeners.length; i++) {
				holdListeners[i].caretMoved(event);
			}
		}
	}

	public void handleEvent(Event e) {
		Display display = null;

		if (Debug.debugCaretMediator) {
			endTime = System.currentTimeMillis();
			System.out.println("Timer fired: " + (endTime - startTime)); //$NON-NLS-1$
		}

		// check if 'okToUse'
		if (textWidget != null && !textWidget.isDisposed()) {
			display = textWidget.getDisplay();
			if ((display != null) && (!display.isDisposed())) {
				display.asyncExec(new Runnable() {
					public void run() {
						if (textWidget != null && !textWidget.isDisposed()) {
							fireCaretEvent(new CaretEvent(textWidget, textWidget.getCaretOffset()));
						}
					}
				});
			}
		}
	}

	protected void internalKeyPressed(KeyEvent e) {
		fDelayer.cancel();
	}

	protected void internalKeyReleased(KeyEvent e) {
		switch (e.keyCode) {
			case SWT.ARROW_DOWN :
			case SWT.ARROW_UP :
			case SWT.ARROW_LEFT :
			case SWT.ARROW_RIGHT :
			case SWT.HOME :
			case SWT.END :
			case SWT.PAGE_DOWN :
			case SWT.PAGE_UP : {
				fDelayer.touch();
				break;
			}
			default : {
				// always update cursor postion, even during normal typing
				// (the logic may look funny, since we always to the same
				// thing, but we haven't always done the same thing, so I
				// wanted to leave that fact documented via code.)
				fDelayer.touch();
			}
		}
	}

	protected void internalMouseDown(MouseEvent e) {
		fDelayer.cancel();
	}

	protected void internalMouseUp(MouseEvent e) {
		// Note, even during a swipe select, when the mouse button goes up,
		// and the widget is
		// queried for the current caret postion, it always returns the
		// beginning of the selection,
		// which is desirable (at least for the known use of this feature,
		// which is to signal
		// that the property sheet can update itself.
		fDelayer.touch();
	}

	public void release() {
		fDelayer.cancel();
		if (textWidget != null && !textWidget.isDisposed()) {
			textWidget.removeKeyListener(internalListener);
			textWidget.removeMouseListener(internalListener);
			textWidget = null;
		}
	}

	public synchronized void removeCaretListener(ICaretListener listener) {
		if ((fCaretListeners != null) && (listener != null)) {
			// if its not in the listeners, we'll ignore the request
			if (Utilities.contains(fCaretListeners, listener)) {
				int oldSize = fCaretListeners.length;
				int newSize = oldSize - 1;
				ICaretListener[] newListeners = new ICaretListener[newSize];
				int index = 0;
				for (int i = 0; i < oldSize; i++) {
					if (fCaretListeners[i] == listener) { // ignore
					} else {
						// copy old to new if its not the one we are removing
						newListeners[index++] = fCaretListeners[i];
					}
				}
				// now that we have a new array, let's switch it for the old
				// one
				fCaretListeners = newListeners;
			}
		}
	}

	public void setTextWidget(StyledText newTextWidget) {
		if(fDelayer == null) {
			fDelayer = new RefreshDelayJob(DELAY);
		}

		// unhook from previous, if any
		if (this.textWidget != null) {
			fDelayer.cancel();
			this.textWidget.removeKeyListener(internalListener);
			this.textWidget.removeMouseListener(internalListener);
		}

		this.textWidget = newTextWidget;

		if (internalListener == null) {
			internalListener = new CaretMediatorListener();
		}

		if (this.textWidget != null) {
			this.textWidget.addKeyListener(internalListener);
			this.textWidget.addMouseListener(internalListener);
		}
	}
}

Back to the top