KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > menus > FocusControlSourceProvider


1 /*******************************************************************************
2  * Copyright (c) 2007 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
12 package org.eclipse.ui.internal.menus;
13
14 import java.util.HashMap JavaDoc;
15 import java.util.Iterator JavaDoc;
16 import java.util.Map JavaDoc;
17
18 import org.eclipse.swt.events.DisposeEvent;
19 import org.eclipse.swt.events.DisposeListener;
20 import org.eclipse.swt.events.FocusEvent;
21 import org.eclipse.swt.events.FocusListener;
22 import org.eclipse.swt.widgets.Control;
23 import org.eclipse.swt.widgets.Widget;
24 import org.eclipse.ui.AbstractSourceProvider;
25 import org.eclipse.ui.ISources;
26 import org.eclipse.ui.swt.IFocusService;
27
28 /**
29  * @since 3.3
30  *
31  */

32 public class FocusControlSourceProvider extends AbstractSourceProvider
33         implements IFocusService {
34
35     /**
36      * The names of the sources supported by this source provider.
37      */

38     private static final String JavaDoc[] PROVIDED_SOURCE_NAMES = new String JavaDoc[] {
39             ISources.ACTIVE_FOCUS_CONTROL_ID_NAME,
40             ISources.ACTIVE_FOCUS_CONTROL_NAME };
41
42     Map JavaDoc controlToId = new HashMap JavaDoc();
43     private FocusListener focusListener;
44
45     private String JavaDoc currentId;
46
47     private Control currentControl;
48
49     private DisposeListener disposeListener;
50
51     /*
52      * (non-Javadoc)
53      *
54      * @see org.eclipse.ui.menus.IFocusService#addTrackerFor(org.eclipse.swt.widgets.Control,
55      * java.lang.String)
56      */

57     public void addFocusTracker(Control control, String JavaDoc id) {
58         if (control.isDisposed()) {
59             return;
60         }
61         controlToId.put(control, id);
62         control.addFocusListener(getFocusListener());
63         control.addDisposeListener(getDisposeListener());
64     }
65
66     /**
67      * @return
68      */

69     private DisposeListener getDisposeListener() {
70         if (disposeListener == null) {
71             disposeListener = new DisposeListener() {
72                 public void widgetDisposed(DisposeEvent e) {
73                     controlToId.remove(e.widget);
74                 }
75             };
76         }
77         return disposeListener;
78     }
79
80     /**
81      * @return
82      */

83     private FocusListener getFocusListener() {
84         if (focusListener == null) {
85             focusListener = new FocusListener() {
86                 public void focusGained(FocusEvent e) {
87                     focusIn(e.widget);
88                 }
89
90                 public void focusLost(FocusEvent e) {
91                     focusIn(null);
92                 }
93             };
94         }
95         return focusListener;
96     }
97
98     /**
99      * @param widget
100      */

101     private void focusIn(Widget widget) {
102         String JavaDoc id = (String JavaDoc) controlToId.get(widget);
103         if (currentId != id) {
104             if (id == null) {
105                 currentId = null;
106                 currentControl = null;
107             } else {
108                 currentId = id;
109                 currentControl = (Control) widget;
110             }
111             Map JavaDoc m = new HashMap JavaDoc();
112             m.put(ISources.ACTIVE_FOCUS_CONTROL_ID_NAME, currentId);
113             m.put(ISources.ACTIVE_FOCUS_CONTROL_NAME, currentControl);
114             fireSourceChanged(ISources.ACTIVE_MENU, m);
115         }
116     }
117
118     /*
119      * (non-Javadoc)
120      *
121      * @see org.eclipse.ui.menus.IFocusService#removeTrackerFor(org.eclipse.swt.widgets.Control)
122      */

123     public void removeFocusTracker(Control control) {
124         controlToId.remove(control);
125         if (control.isDisposed()) {
126             return;
127         }
128         control.removeFocusListener(getFocusListener());
129         control.removeDisposeListener(getDisposeListener());
130     }
131
132     /*
133      * (non-Javadoc)
134      *
135      * @see org.eclipse.ui.ISourceProvider#dispose()
136      */

137     public void dispose() {
138         Iterator JavaDoc i = controlToId.keySet().iterator();
139         while (i.hasNext()) {
140             Control c = (Control) i.next();
141             if (!c.isDisposed()) {
142                 c.removeFocusListener(getFocusListener());
143                 c.removeDisposeListener(getDisposeListener());
144             }
145         }
146         controlToId.clear();
147         controlToId = null;
148         focusListener = null;
149         disposeListener = null;
150     }
151
152     /*
153      * (non-Javadoc)
154      *
155      * @see org.eclipse.ui.ISourceProvider#getCurrentState()
156      */

157     public Map JavaDoc getCurrentState() {
158         Map JavaDoc m = new HashMap JavaDoc();
159         m.put(ISources.ACTIVE_FOCUS_CONTROL_ID_NAME, currentId);
160         m.put(ISources.ACTIVE_FOCUS_CONTROL_NAME, currentControl);
161         return m;
162     }
163
164     /*
165      * (non-Javadoc)
166      *
167      * @see org.eclipse.ui.ISourceProvider#getProvidedSourceNames()
168      */

169     public String JavaDoc[] getProvidedSourceNames() {
170         return PROVIDED_SOURCE_NAMES;
171     }
172 }
173
Popular Tags