001: /*******************************************************************************
002: * Copyright (c) 2007 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: ******************************************************************************/package org.eclipse.ui.internal.menus;
011:
012: import java.util.HashMap;
013: import java.util.Iterator;
014: import java.util.Map;
015:
016: import org.eclipse.swt.events.DisposeEvent;
017: import org.eclipse.swt.events.DisposeListener;
018: import org.eclipse.swt.events.FocusEvent;
019: import org.eclipse.swt.events.FocusListener;
020: import org.eclipse.swt.widgets.Control;
021: import org.eclipse.swt.widgets.Widget;
022: import org.eclipse.ui.AbstractSourceProvider;
023: import org.eclipse.ui.ISources;
024: import org.eclipse.ui.swt.IFocusService;
025:
026: /**
027: * @since 3.3
028: *
029: */
030: public class FocusControlSourceProvider extends AbstractSourceProvider
031: implements IFocusService {
032:
033: /**
034: * The names of the sources supported by this source provider.
035: */
036: private static final String[] PROVIDED_SOURCE_NAMES = new String[] {
037: ISources.ACTIVE_FOCUS_CONTROL_ID_NAME,
038: ISources.ACTIVE_FOCUS_CONTROL_NAME };
039:
040: Map controlToId = new HashMap();
041: private FocusListener focusListener;
042:
043: private String currentId;
044:
045: private Control currentControl;
046:
047: private DisposeListener disposeListener;
048:
049: /*
050: * (non-Javadoc)
051: *
052: * @see org.eclipse.ui.menus.IFocusService#addTrackerFor(org.eclipse.swt.widgets.Control,
053: * java.lang.String)
054: */
055: public void addFocusTracker(Control control, String id) {
056: if (control.isDisposed()) {
057: return;
058: }
059: controlToId.put(control, id);
060: control.addFocusListener(getFocusListener());
061: control.addDisposeListener(getDisposeListener());
062: }
063:
064: /**
065: * @return
066: */
067: private DisposeListener getDisposeListener() {
068: if (disposeListener == null) {
069: disposeListener = new DisposeListener() {
070: public void widgetDisposed(DisposeEvent e) {
071: controlToId.remove(e.widget);
072: }
073: };
074: }
075: return disposeListener;
076: }
077:
078: /**
079: * @return
080: */
081: private FocusListener getFocusListener() {
082: if (focusListener == null) {
083: focusListener = new FocusListener() {
084: public void focusGained(FocusEvent e) {
085: focusIn(e.widget);
086: }
087:
088: public void focusLost(FocusEvent e) {
089: focusIn(null);
090: }
091: };
092: }
093: return focusListener;
094: }
095:
096: /**
097: * @param widget
098: */
099: private void focusIn(Widget widget) {
100: String id = (String) controlToId.get(widget);
101: if (currentId != id) {
102: if (id == null) {
103: currentId = null;
104: currentControl = null;
105: } else {
106: currentId = id;
107: currentControl = (Control) widget;
108: }
109: Map m = new HashMap();
110: m.put(ISources.ACTIVE_FOCUS_CONTROL_ID_NAME, currentId);
111: m.put(ISources.ACTIVE_FOCUS_CONTROL_NAME, currentControl);
112: fireSourceChanged(ISources.ACTIVE_MENU, m);
113: }
114: }
115:
116: /*
117: * (non-Javadoc)
118: *
119: * @see org.eclipse.ui.menus.IFocusService#removeTrackerFor(org.eclipse.swt.widgets.Control)
120: */
121: public void removeFocusTracker(Control control) {
122: controlToId.remove(control);
123: if (control.isDisposed()) {
124: return;
125: }
126: control.removeFocusListener(getFocusListener());
127: control.removeDisposeListener(getDisposeListener());
128: }
129:
130: /*
131: * (non-Javadoc)
132: *
133: * @see org.eclipse.ui.ISourceProvider#dispose()
134: */
135: public void dispose() {
136: Iterator i = controlToId.keySet().iterator();
137: while (i.hasNext()) {
138: Control c = (Control) i.next();
139: if (!c.isDisposed()) {
140: c.removeFocusListener(getFocusListener());
141: c.removeDisposeListener(getDisposeListener());
142: }
143: }
144: controlToId.clear();
145: controlToId = null;
146: focusListener = null;
147: disposeListener = null;
148: }
149:
150: /*
151: * (non-Javadoc)
152: *
153: * @see org.eclipse.ui.ISourceProvider#getCurrentState()
154: */
155: public Map getCurrentState() {
156: Map m = new HashMap();
157: m.put(ISources.ACTIVE_FOCUS_CONTROL_ID_NAME, currentId);
158: m.put(ISources.ACTIVE_FOCUS_CONTROL_NAME, currentControl);
159: return m;
160: }
161:
162: /*
163: * (non-Javadoc)
164: *
165: * @see org.eclipse.ui.ISourceProvider#getProvidedSourceNames()
166: */
167: public String[] getProvidedSourceNames() {
168: return PROVIDED_SOURCE_NAMES;
169: }
170: }
|