1 11 12 package org.eclipse.ui.internal.menus; 13 14 import java.util.HashMap ; 15 import java.util.Iterator ; 16 import java.util.Map ; 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 32 public class FocusControlSourceProvider extends AbstractSourceProvider 33 implements IFocusService { 34 35 38 private static final String [] PROVIDED_SOURCE_NAMES = new String [] { 39 ISources.ACTIVE_FOCUS_CONTROL_ID_NAME, 40 ISources.ACTIVE_FOCUS_CONTROL_NAME }; 41 42 Map controlToId = new HashMap (); 43 private FocusListener focusListener; 44 45 private String currentId; 46 47 private Control currentControl; 48 49 private DisposeListener disposeListener; 50 51 57 public void addFocusTracker(Control control, String id) { 58 if (control.isDisposed()) { 59 return; 60 } 61 controlToId.put(control, id); 62 control.addFocusListener(getFocusListener()); 63 control.addDisposeListener(getDisposeListener()); 64 } 65 66 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 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 101 private void focusIn(Widget widget) { 102 String id = (String ) 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 m = new HashMap (); 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 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 137 public void dispose() { 138 Iterator 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 157 public Map getCurrentState() { 158 Map m = new HashMap (); 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 169 public String [] getProvidedSourceNames() { 170 return PROVIDED_SOURCE_NAMES; 171 } 172 } 173 | Popular Tags |