Skip to main content
summaryrefslogtreecommitdiffstats
blob: c83e35b1a81b93f0eabbf29b9bfc7e1cfd64d4de (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
/*******************************************************************************
 * 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.FocusEvent;
import org.eclipse.swt.events.FocusListener;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.ScrollBar;
import org.eclipse.swt.widgets.Table;


/**
 * A generic closer class used to monitor various
 * interface events in order to determine whether
 * a content assistant should be terminated and all
 * associated windows be closed.
 */
class PopupCloser implements FocusListener, SelectionListener {
	
	/** The content assistant to be monitored */
	private ContentAssistant fContentAssistant;
	/** The table of a selector popup opened by the content assistant */
	private Table fTable;
	/** The scrollbar of the table for the selector popup */
	private ScrollBar fScrollbar;
	/** Indicates whether the scrollbar thumb has been grabed */
	private boolean fScrollbarClicked= false;
	
	/**
	 * Installs this closer on the given table opened by the given content assistant.
	 * 
	 * @param contentAssistant the content assistant
	 * @param table the table to be tracked
	 */
	public void install(ContentAssistant contentAssistant, Table table) {
		fContentAssistant= contentAssistant;
		fTable= table;
		if (Helper.okToUse(fTable)) {
			fTable.addFocusListener(this);
			fScrollbar= fTable.getVerticalBar();
			if (fScrollbar != null)
				fScrollbar.addSelectionListener(this);
		}
	}
	
	/**
	 * Uninstalls this closer if previously installed.
	 */
	public void uninstall() {
		if (Helper.okToUse(fScrollbar))
			fScrollbar.removeSelectionListener(this);
		if (Helper.okToUse(fTable))
			fTable.removeFocusListener(this);
	}
	
	/*
	 * @see org.eclipse.swt.events.SelectionListener#widgetSelected(org.eclipse.swt.events.SelectionEvent)
	 */
	public void widgetSelected(SelectionEvent e) {
		fScrollbarClicked= true;
	}
	
	/*
	 * @see org.eclipse.swt.events.SelectionListener#widgetDefaultSelected(org.eclipse.swt.events.SelectionEvent)
	 */
	public void widgetDefaultSelected(SelectionEvent e) {
		fScrollbarClicked= true;
	}
	
	/*
	 * @see org.eclipse.swt.events.FocusListener#focusGained(org.eclipse.swt.events.FocusEvent)
	 */
	public void focusGained(FocusEvent e) {
	}
	
	/*
	 * @see org.eclipse.swt.events.FocusListener#focusLost(org.eclipse.swt.events.FocusEvent)
	 */
	public void focusLost(final FocusEvent e) {
		fScrollbarClicked= false;
		Display d= fTable.getDisplay();
		d.asyncExec(new Runnable() {
			public void run() {
				if (Helper.okToUse(fTable) && !fTable.isFocusControl() && !fScrollbarClicked)
					fContentAssistant.popupFocusLost(e);
			}
		});
	}
}

Back to the top