Skip to main content
summaryrefslogtreecommitdiffstats
blob: febd74f149d6f583354db370966f81b81fc1ea78 (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
/*******************************************************************************
 * Copyright (c) 2000, 2003 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials 
 * are made available under the terms of the Common Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/cpl-v10.html
 * 
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/

package org.eclipse.jface.text.contentassist;


import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableItem;

import org.eclipse.jface.text.AbstractInformationControlManager;
import org.eclipse.jface.text.Assert;
import org.eclipse.jface.text.IInformationControl;
import org.eclipse.jface.text.IInformationControlCreator;
import org.eclipse.jface.text.IInformationControlExtension3;



/**
 * Displays the additional information available for a completion proposal.
 * 
 * @since 2.0
 */
class AdditionalInfoController extends AbstractInformationControlManager implements Runnable {
	
	/**
	 * Internal table selection listener.
	 */
	private class TableSelectionListener implements SelectionListener {
		
		/*
		 * @see SelectionListener#widgetSelected(SelectionEvent)
		 */
		public void widgetSelected(SelectionEvent e) {
			handleTableSelectionChanged();
		}
		
		/*
		 * @see SelectionListener#widgetDefaultSelected(SelectionEvent)
		 */
		public void widgetDefaultSelected(SelectionEvent e) {
		}
	}
	
	
	/** The proposal table */
	private Table fProposalTable;
	/** The thread controlling the delayed display of the additional info */
	private Thread fThread;
	/** Indicates whether the display delay has been reset */
	private boolean fIsReset= false;
	/** Object to synchronize display thread and table selection changes */
	private final Object fMutex= new Object();
	/**
	 * Thread access lock.
	 * since 3.0
	 */
	private final Object fThreadAccess= new Object();
	/** Object to synchronize initial display of additonal info */
	private Object fStartSignal;
	/** The table selection listener */
	private SelectionListener fSelectionListener= new TableSelectionListener();
	/** The delay after which additional information is displayed */
	private int fDelay;
	
	
	/**
	 * Creates a new additional information controller.
	 * 
	 * @param creator the information control creator to be used by this controller
	 * @param delay time in milliseconds after which additional info should be displayed
	 */
	AdditionalInfoController(IInformationControlCreator creator, int delay) {
		super(creator);
		fDelay= delay;
		setAnchor(ANCHOR_RIGHT);
		setFallbackAnchors(new Anchor[] { ANCHOR_LEFT, ANCHOR_BOTTOM, ANCHOR_RIGHT });
	}
	
	/*
	 * @see AbstractInformationControlManager#install(Control)
	 */
	public void install(Control control) {

		if (fProposalTable == control) {
			// already installed
			return;
		}
		
		super.install(control);
		
		Assert.isTrue(control instanceof Table);
		fProposalTable= (Table) control;
		fProposalTable.addSelectionListener(fSelectionListener);
		synchronized (fThreadAccess) {
	 		if (fThread != null)
	 			fThread.interrupt();
			fThread= new Thread(this, JFaceTextMessages.getString("InfoPopup.info_delay_timer_name")); //$NON-NLS-1$
		
			fStartSignal= new Object();
			synchronized (fStartSignal) {
				fThread.start();
				try {
					// wait until thread is ready
					fStartSignal.wait();
				} catch (InterruptedException x) {
				}
			}
		}
	}
	
	/*
	 * @see AbstractInformationControlManager#disposeInformationControl()
	 */	
	public void disposeInformationControl() {
		
		synchronized (fThreadAccess) {
			if (fThread != null) {
				fThread.interrupt();
				fThread= null;
			}
		}
		
		if (fProposalTable != null && !fProposalTable.isDisposed()) {
			fProposalTable.removeSelectionListener(fSelectionListener);
			fProposalTable= null;
		}
		
		super.disposeInformationControl();
	}
	
	/*
	 * @see java.lang.Runnable#run()
	 */
	public void run() {
		try {
			while (true) {
				
				synchronized (fMutex) {
					
					if (fStartSignal != null) {
						synchronized (fStartSignal) {
							fStartSignal.notifyAll();
							fStartSignal= null;
						}
					}
					
					// Wait for a selection event to occur.
					fMutex.wait();
					
					while (true) {
						fIsReset= false;
						// Delay before showing the popup.
						fMutex.wait(fDelay);
						if (!fIsReset)
							break;
					}
				}
				
				if (fProposalTable != null && !fProposalTable.isDisposed()) {
					fProposalTable.getDisplay().asyncExec(new Runnable() {
						public void run() {
							if (!fIsReset)
								showInformation();
						}
					});
				}
				
			}
		} catch (InterruptedException e) {
		}
		
		synchronized (fThreadAccess) {
			// only null fThread if it is us!
			if (Thread.currentThread() == fThread)
				fThread= null;
		}
	}
	
	/**
	 *Handles a change of the line selected in the associated selector.
	 */
	public void handleTableSelectionChanged() {
		
		if (fProposalTable != null && !fProposalTable.isDisposed() && fProposalTable.isVisible()) {
			synchronized (fMutex) {
				fIsReset= true;
				fMutex.notifyAll();
			}
		}
	}
			
	/*
	 * @see AbstractInformationControlManager#computeInformation()
	 */
	protected void computeInformation() {
		
		if (fProposalTable == null || fProposalTable.isDisposed())
			return;
			
		TableItem[] selection= fProposalTable.getSelection();
		if (selection != null && selection.length > 0) {
			
			TableItem item= selection[0];
			
			// compute information
			String information= null;
			Object d= item.getData();
			
			if (d instanceof ICompletionProposal) {
				ICompletionProposal p= (ICompletionProposal) d;
				information= p.getAdditionalProposalInfo();
			}
			
			if (d instanceof ICompletionProposalExtension3)
				setCustomInformationControlCreator(((ICompletionProposalExtension3) d).getInformationControlCreator());
			else
				setCustomInformationControlCreator(null);
			
			// compute subject area
			setMargins(4, -2);
			Rectangle area= fProposalTable.getBounds();
			area.x= 0; // subject area is the whole subject control
			area.y= 0;
			
			// set information & subject area
			setInformation(information, area);
		}
	}
	
	/*
	 * @see org.eclipse.jface.text.AbstractInformationControlManager#computeSizeConstraints(Control, IInformationControl)
	 */
	protected Point computeSizeConstraints(Control subjectControl, IInformationControl informationControl) {
		Point sizeConstraint= super.computeSizeConstraints(subjectControl, informationControl);
		Point size= subjectControl.getSize();

		Rectangle otherTrim= subjectControl.getShell().computeTrim(0, 0, 0, 0);
		size.x += otherTrim.width;
		size.y += otherTrim.height;

		if (informationControl instanceof IInformationControlExtension3) {
			Rectangle thisTrim= ((IInformationControlExtension3)informationControl).computeTrim();
			size.x -= thisTrim.width;
			size.y -= thisTrim.height;
		}
			
		if (sizeConstraint.x < size.x)
			sizeConstraint.x= size.x;
		if (sizeConstraint.y < size.y)
			sizeConstraint.y= size.y;
		return sizeConstraint;
	}
}


Back to the top