KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jface > text > contentassist > PopupCloser


1 /*******************************************************************************
2  * Copyright (c) 2000, 2005 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.jface.text.contentassist;
12
13 import org.eclipse.swt.events.FocusEvent;
14 import org.eclipse.swt.events.FocusListener;
15 import org.eclipse.swt.events.SelectionEvent;
16 import org.eclipse.swt.events.SelectionListener;
17 import org.eclipse.swt.events.ShellAdapter;
18 import org.eclipse.swt.events.ShellEvent;
19 import org.eclipse.swt.widgets.Display;
20 import org.eclipse.swt.widgets.ScrollBar;
21 import org.eclipse.swt.widgets.Shell;
22 import org.eclipse.swt.widgets.Table;
23
24
25 /**
26  * A generic closer class used to monitor various
27  * interface events in order to determine whether
28  * a content assistant should be terminated and all
29  * associated windows be closed.
30  */

31 class PopupCloser extends ShellAdapter implements FocusListener, SelectionListener {
32
33     /** The content assistant to be monitored. */
34     private ContentAssistant fContentAssistant;
35     /** The table of a selector popup opened by the content assistant. */
36     private Table fTable;
37     /** The scroll bar of the table for the selector popup. */
38     private ScrollBar fScrollbar;
39     /** Indicates whether the scroll bar thumb has been grabbed. */
40     private boolean fScrollbarClicked= false;
41     /**
42      * The shell on which some listeners are registered.
43      * @since 3.1
44      */

45     private Shell fShell;
46
47     /**
48      * Installs this closer on the given table opened by the given content assistant.
49      *
50      * @param contentAssistant the content assistant
51      * @param table the table to be tracked
52      */

53     public void install(ContentAssistant contentAssistant, Table table) {
54         fContentAssistant= contentAssistant;
55         fTable= table;
56         if (Helper.okToUse(fTable)) {
57             Shell shell= fTable.getShell();
58             if (Helper.okToUse(shell)) {
59                 fShell= shell;
60                 fShell.addShellListener(this);
61             }
62
63             fTable.addFocusListener(this);
64             fScrollbar= fTable.getVerticalBar();
65             if (fScrollbar != null)
66                 fScrollbar.addSelectionListener(this);
67         }
68     }
69
70     /**
71      * Uninstalls this closer if previously installed.
72      */

73     public void uninstall() {
74         fContentAssistant= null;
75         if (Helper.okToUse(fShell))
76             fShell.removeShellListener(this);
77         fShell= null;
78         if (Helper.okToUse(fScrollbar))
79             fScrollbar.removeSelectionListener(this);
80         if (Helper.okToUse(fTable))
81             fTable.removeFocusListener(this);
82     }
83
84     /*
85      * @see org.eclipse.swt.events.SelectionListener#widgetSelected(org.eclipse.swt.events.SelectionEvent)
86      */

87     public void widgetSelected(SelectionEvent e) {
88         fScrollbarClicked= true;
89     }
90
91     /*
92      * @see org.eclipse.swt.events.SelectionListener#widgetDefaultSelected(org.eclipse.swt.events.SelectionEvent)
93      */

94     public void widgetDefaultSelected(SelectionEvent e) {
95         fScrollbarClicked= true;
96     }
97
98     /*
99      * @see org.eclipse.swt.events.FocusListener#focusGained(org.eclipse.swt.events.FocusEvent)
100      */

101     public void focusGained(FocusEvent e) {
102     }
103
104     /*
105      * @see org.eclipse.swt.events.FocusListener#focusLost(org.eclipse.swt.events.FocusEvent)
106      */

107     public void focusLost(final FocusEvent e) {
108         fScrollbarClicked= false;
109         Display d= fTable.getDisplay();
110         d.asyncExec(new Runnable JavaDoc() {
111             public void run() {
112                 if (Helper.okToUse(fTable) && !fTable.isFocusControl() && !fScrollbarClicked && fContentAssistant != null)
113                     fContentAssistant.popupFocusLost(e);
114             }
115         });
116     }
117
118     /*
119      * @see org.eclipse.swt.events.ShellAdapter#shellDeactivated(org.eclipse.swt.events.ShellEvent)
120      * @since 3.1
121      */

122     public void shellDeactivated(ShellEvent e) {
123         if (fContentAssistant != null)
124             fContentAssistant.hide();
125     }
126
127
128     /*
129      * @see org.eclipse.swt.events.ShellAdapter#shellClosed(org.eclipse.swt.events.ShellEvent)
130      * @since 3.1
131      */

132     public void shellClosed(ShellEvent e) {
133         if (fContentAssistant != null)
134             fContentAssistant.hide();
135     }
136 }
137
Popular Tags