Skip to main content
summaryrefslogtreecommitdiffstats
blob: a1be9ac32f7d0bd8de30fe629a388b2c1fe78f64 (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
package org.eclipse.fx.text.ui;

import org.eclipse.core.commands.ExecutionException;
import org.eclipse.fx.ui.controls.styledtext.StyledTextArea;
import org.eclipse.fx.ui.controls.styledtext.TextSelection;
import org.eclipse.jface.text.IDocument;
import org.eclipse.jface.text.Region;
import org.eclipse.text.undo.DocumentUndoEvent;
import org.eclipse.text.undo.DocumentUndoManagerRegistry;
import org.eclipse.text.undo.IDocumentUndoListener;
import org.eclipse.text.undo.IDocumentUndoManager;

import javafx.scene.input.KeyEvent;
import javafx.scene.input.MouseEvent;

public class DefaultUndoManager implements IUndoManager {

	private IDocument fDocument;
	private IDocumentUndoManager fDocumentUndoManager;
	private ITextViewer fTextViewer;

	private int fUndoLevel;

	public DefaultUndoManager(int undoLevel) {
		this.fUndoLevel = undoLevel;
	}

	private boolean isConnected() {
		return fTextViewer != null && fDocumentUndoManager != null;
	}

	private void onKeyPressed(KeyEvent e) {
		switch(e.getCode()) {
		case UP:
		case DOWN:
		case LEFT:
		case RIGHT:
			if (isConnected()) {
				fDocumentUndoManager.commit();
			}
			break;
		}
	}

	private void onMousePressed(MouseEvent e) {
		if (e.getClickCount() == 1) {
			if (isConnected()) {
				fDocumentUndoManager.commit();
			}
		}
	}

	private ITextInputListener fTextInputListener = new ITextInputListener() {
		@Override
		public void inputDocumentChanged(IDocument oldInput, IDocument newInput) {
			connectDocumentUndoManager(newInput);
		}

		@Override
		public void inputDocumentAboutToBeChanged(IDocument oldInput, IDocument newInput) {
			disconnectDocumentUndoManager();
		}
	};

	private IDocumentUndoListener fDocumentUndoListener = new IDocumentUndoListener() {

		@Override
		public void documentUndoNotification(DocumentUndoEvent event ){
			if (!isConnected()) return;

			int eventType= event.getEventType();
			if (((eventType & DocumentUndoEvent.ABOUT_TO_UNDO) != 0) || ((eventType & DocumentUndoEvent.ABOUT_TO_REDO) != 0))  {
				if (event.isCompound()) {
//					ITextViewerExtension extension= null;
//					if (fTextViewer instanceof ITextViewerExtension)
//						extension= (ITextViewerExtension) fTextViewer;
//
//					if (extension != null)
//						extension.setRedraw(false);
				}
				// TODO turn of auto edit strategies
//				fTextViewer.getTextWidget().getDisplay().syncExec(new Runnable() {
//					@Override
//					public void run() {
//						if (fTextViewer instanceof TextViewer)
//							((TextViewer)fTextViewer).ignoreAutoEditStrategies(true);
//					}
//			    });

			} else if (((eventType & DocumentUndoEvent.UNDONE) != 0) || ((eventType & DocumentUndoEvent.REDONE) != 0))  {
				// TODO turn on auto edit strategies
//				fTextViewer.getTextWidget().getDisplay().syncExec(new Runnable() {
//					@Override
//					public void run() {
//						if (fTextViewer instanceof TextViewer)
//							((TextViewer)fTextViewer).ignoreAutoEditStrategies(false);
//					}
//			    });
				if (event.isCompound()) {
//					ITextViewerExtension extension= null;
//					if (fTextViewer instanceof ITextViewerExtension)
//						extension= (ITextViewerExtension) fTextViewer;
//
//					if (extension != null)
//						extension.setRedraw(true);
				}

				// Reveal the change if this manager's viewer has the focus.
				if (fTextViewer != null) {
					StyledTextArea widget= fTextViewer.getTextWidget();
					// TODO select?
//					if (widget != null && !widget.isDisposed() && (widget.isFocusControl()))// || fTextViewer.getTextWidget() == control))

					selectAndReveal(event.getOffset(), event.getText() == null ? 0 : event.getText().length());
				}
			}
		}

	};

	/**
	 * Selects and reveals the specified range.
	 *
	 * @param offset the offset of the range
	 * @param length the length of the range
	 */
	private void selectAndReveal(int offset, int length) {
		if (fTextViewer instanceof ITextViewerExtension5) {
			ITextViewerExtension5 extension= (ITextViewerExtension5) fTextViewer;
			extension.exposeModelRange(new Region(offset, length));
		} else if (!fTextViewer.overlapsWithVisibleRegion(offset, length))
			fTextViewer.resetVisibleRegion();

		fTextViewer.getTextWidget().setSelection(new TextSelection(offset, length));
//		fTextViewer.revealRange(offset, length);
	}

	private void connectDocumentUndoManager(IDocument document) {
		disconnectDocumentUndoManager();
		if (document != null) {
			fDocument= document;
			DocumentUndoManagerRegistry.connect(fDocument);
			fDocumentUndoManager= DocumentUndoManagerRegistry.getDocumentUndoManager(fDocument);
			fDocumentUndoManager.connect(this);
			setMaximalUndoLevel(fUndoLevel);
			fDocumentUndoManager.addDocumentUndoListener(fDocumentUndoListener);
		}
	}

	private void disconnectDocumentUndoManager() {
		if (fDocumentUndoManager != null) {
			fDocumentUndoManager.disconnect(this);
			fDocumentUndoManager.removeDocumentUndoListener(fDocumentUndoListener);
			fDocumentUndoListener= null;
			fDocumentUndoManager= null;
		}
	}

	@Override
	public void connect(ITextViewer viewer) {
		this.fTextViewer = viewer;

		this.fTextViewer.getTextWidget().addEventHandler(MouseEvent.MOUSE_PRESSED, this::onMousePressed);
		this.fTextViewer.getTextWidget().addEventHandler(KeyEvent.KEY_PRESSED, this::onKeyPressed);

		this.fTextViewer.addTextInputListener(this.fTextInputListener);

		connectDocumentUndoManager(fTextViewer.getDocument());
	}

	@Override
	public void disconnect() {

		this.fTextViewer.getTextWidget().removeEventHandler(MouseEvent.MOUSE_PRESSED, this::onMousePressed);
		this.fTextViewer.getTextWidget().removeEventHandler(KeyEvent.KEY_PRESSED, this::onKeyPressed);

		this.fTextViewer.removeTextInputListener(this.fTextInputListener);

		this.fTextViewer = null;

		disconnectDocumentUndoManager();
	}

	@Override
	public void beginCompoundChange() {
		if (isConnected()) {
			fDocumentUndoManager.beginCompoundChange();
		}
	}

	@Override
	public void endCompoundChange() {
		if (isConnected()) {
			fDocumentUndoManager.endCompoundChange();
		}
	}

	@Override
	public void reset() {
		if (isConnected())
			fDocumentUndoManager.reset();
	}

	@Override
	public void setMaximalUndoLevel(int undoLevel) {
		fUndoLevel= Math.max(0, undoLevel);
		if (isConnected()) {
			fDocumentUndoManager.setMaximalUndoLevel(fUndoLevel);
		}
	}

	@Override
	public boolean undoable() {
		if (isConnected())
			return fDocumentUndoManager.undoable();
		return false;
	}

	@Override
	public boolean redoable() {
		if (isConnected())
			return fDocumentUndoManager.redoable();
		return false;
	}

	@Override
	public void undo() {
		if (isConnected()) {
			try {
				fDocumentUndoManager.undo();
			} catch (ExecutionException ex) {
				ex.printStackTrace();
//				openErrorDialog(JFaceTextMessages.getString("DefaultUndoManager.error.undoFailed.title"), ex); //$NON-NLS-1$
			}
		}
	}

	@Override
	public void redo() {
		if (isConnected()) {
			try {
				fDocumentUndoManager.redo();
			} catch (ExecutionException ex) {
				ex.printStackTrace();
//				openErrorDialog(JFaceTextMessages.getString("DefaultUndoManager.error.redoFailed.title"), ex); //$NON-NLS-1$
			}
		}
	}

}

Back to the top