Skip to main content
summaryrefslogtreecommitdiffstats
blob: 6e9c9dbf6311e9d31eb41b22f4f95f35900e84c4 (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
/*******************************************************************************
 * Copyright (c) 2000, 2007 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.fx.text.ui.contentassist;

import static com.sun.javafx.PlatformUtil.isMac;

import java.util.Collections;
import java.util.List;
import java.util.function.Function;

import javafx.application.Platform;
import javafx.collections.FXCollections;
import javafx.geometry.Point2D;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.control.ListView;
import javafx.scene.control.PopupControl;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyEvent;
import javafx.scene.layout.BorderPane;
import javafx.stage.PopupWindow;
import javafx.stage.Stage;
import javafx.stage.StageStyle;

import org.eclipse.fx.text.ui.ITextViewer;
import org.eclipse.fx.ui.controls.list.SimpleListCell;
import org.eclipse.jface.text.BadLocationException;
import org.eclipse.jface.text.IDocument;

public class ContentProposalPopup {
	private ITextViewer viewer;
	private PopupWindow stage;
	private ListView<ICompletionProposal> proposalList;
	private String prefix;
	private int offset;
	private Function<Integer, List<ICompletionProposal>> proposalComputer;

	public ContentProposalPopup(ITextViewer viewer, Function<Integer, List<ICompletionProposal>> proposalComputer) {
		this.viewer = viewer;
		this.proposalComputer = proposalComputer;
	}

	public void displayProposals(List<ICompletionProposal> proposalList, int offset, Point2D position) {
		setup();
		this.prefix = "";
		this.offset = offset;
		this.proposalList.setItems(FXCollections.observableArrayList(proposalList));
		this.proposalList.getSelectionModel().select(0);
		this.stage.setX(position.getX());
		this.stage.setY(position.getY());
		this.stage.setWidth(300);
		this.stage.setHeight(200);
		this.stage.show(viewer.getTextWidget().getScene().getWindow());
		this.stage.requestFocus();
	}

	private void handleKeyTyped(KeyEvent event) {
		if( event.getCharacter().length() == 0 ) {
			return;
		}

		String character = event.getCharacter();
		if( character.length() == 0 ) {
			return;
		}

		if (event.isControlDown() || event.isAltDown() || (isMac() && event.isMetaDown())) {
			if (!((event.isControlDown() || isMac()) && event.isAltDown())) {
				return;
			}
		}

		if (character.charAt(0) > 0x1F
	            && character.charAt(0) != 0x7F ) {
//			try {
				this.prefix = this.prefix+character;
//				viewer.getDocument().replace(offset, 0, character);
				this.offset += event.getCharacter().length();
//				viewer.getTextWidget().setCaretOffset(offset);
				updateProposals();
//			} catch (BadLocationException e) {
//				// TODO Auto-generated catch block
//				e.printStackTrace();
//			}
		}
	}

	private void updateProposals() {
		List<ICompletionProposal> list = proposalComputer.apply(offset);
		if( ! list.isEmpty() ) {
			proposalList.setItems(FXCollections.observableArrayList(list));
			proposalList.scrollTo(0);
			proposalList.getSelectionModel().select(0);
		} else {
			stage.hide();
		}
	}

	private void handleKeyPressed(KeyEvent event) {
		if( event.getCode() == KeyCode.ESCAPE ) {
			event.consume();
			stage.hide();
		} else if( event.getCode() == KeyCode.BACK_SPACE ) {
			event.consume();
			this.offset -= 1;
			try {
				this.viewer.getDocument().replace(offset, 1, "");
				viewer.getTextWidget().setCaretOffset(offset);
				updateProposals();
			} catch (BadLocationException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		} else if( event.getCode() == KeyCode.ENTER ) {
			event.consume();
			applySelection();
		} else if( event.getCode() == KeyCode.LEFT ) {
			event.consume();
			this.offset -= 1;
			this.offset = Math.max(0, this.offset);
			viewer.getTextWidget().setCaretOffset(offset);
			updateProposals();
		} else if( event.getCode() == KeyCode.RIGHT ) {
			event.consume();
			this.offset += 1;
			this.offset = Math.min(viewer.getDocument().getLength()-1, this.offset);
			viewer.getTextWidget().setCaretOffset(offset);
			updateProposals();
		}
	}

	private void applySelection() {
		ICompletionProposal selectedItem = proposalList.getSelectionModel().getSelectedItem();
		if( selectedItem != null ) {
			IDocument document = viewer.getDocument();
			selectedItem.apply(document);
			viewer.getTextWidget().setSelection(selectedItem.getSelection(document));
			stage.hide();
		}
	}

	private void setup() {
		if( stage == null ) {
			stage = new PopupWindow() {

			};
			stage.setAutoFix(false);
			stage.setWidth(300);
			stage.setHeight(200);
			BorderPane p = new BorderPane();
			p.setPrefHeight(200);
			p.setPrefWidth(400);
			stage.getScene().addEventFilter(KeyEvent.KEY_TYPED, this::handleKeyTyped);
			stage.getScene().addEventFilter(KeyEvent.KEY_PRESSED, this::handleKeyPressed);
			stage.getScene().getStylesheets().addAll(viewer.getTextWidget().getScene().getStylesheets());
			proposalList = new ListView<>();
			proposalList.setOnMouseClicked((e) -> {
				if(e.getClickCount() == 1) {
					applySelection();
				}
			});

			Function<ICompletionProposal, CharSequence> label = (c) -> c.getLabel();
			Function<ICompletionProposal, Node> graphic = (c) -> c.getGraphic();
			Function<ICompletionProposal, List<String>> css = (c) -> Collections.emptyList();


			proposalList.setCellFactory((v) -> new SimpleListCell<ICompletionProposal>(label,graphic,css));
			p.setCenter(proposalList);
			stage.getScene().setRoot(p);
			stage.focusedProperty().addListener((o) -> {
				if( stage != null && ! stage.isFocused() ) {
					Platform.runLater(stage::hide);
				}
			});
			// Fix CSS warnings
			stage.setOnHidden((o) -> {
				stage = null;
			});
		}
	}
}

Back to the top