KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jface > internal > text > link > contentassist > PopupCloser2


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

32 class PopupCloser2 extends ShellAdapter implements FocusListener, SelectionListener {
33
34     /** The content assistant to be monitored */
35     private ContentAssistant2 fContentAssistant;
36     /** The table of a selector popup opened by the content assistant */
37     private Table fTable;
38     /** The scrollbar of the table for the selector popup */
39     private ScrollBar fScrollbar;
40     /** Indicates whether the scrollbar thumb has been grabbed. */
41     private boolean fScrollbarClicked= false;
42     /** The shell on which some listeners are registered. */
43     private Shell fShell;
44
45
46     /**
47      * Installs this closer on the given table opened by the given content assistant.
48      *
49      * @param contentAssistant the content assistant
50      * @param table the table to be tracked
51      */

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

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

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

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

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

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

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

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