1 11 package org.eclipse.jface.viewers; 12 13 import org.eclipse.core.runtime.ListenerList; 14 import org.eclipse.core.runtime.Assert; 15 import org.eclipse.jface.util.SafeRunnable; 16 import org.eclipse.swt.events.HelpEvent; 17 import org.eclipse.swt.events.HelpListener; 18 import org.eclipse.swt.widgets.Control; 19 import org.eclipse.swt.widgets.Item; 20 21 46 public abstract class Viewer implements IInputSelectionProvider { 47 48 53 private ListenerList selectionChangedListeners = new ListenerList(); 54 55 61 private ListenerList helpListeners = new ListenerList(); 62 63 69 private String [] keys; 70 71 78 private Object [] values; 79 80 83 private boolean helpHooked = false; 84 85 88 private HelpListener helpListener = null; 89 90 94 protected static final String WIDGET_DATA_KEY = "org.eclipse.jface.viewers.WIDGET_DATA"; 96 99 protected Viewer() { 100 } 101 102 108 public void addHelpListener(HelpListener listener) { 109 helpListeners.add(listener); 110 if (!helpHooked) { 111 Control control = getControl(); 112 if (control != null && !control.isDisposed()) { 113 if (this.helpListener == null) { 114 this.helpListener = new HelpListener() { 115 public void helpRequested(HelpEvent event) { 116 handleHelpRequest(event); 117 } 118 }; 119 } 120 control.addHelpListener(this.helpListener); 121 helpHooked = true; 122 } 123 } 124 } 125 126 129 public void addSelectionChangedListener(ISelectionChangedListener listener) { 130 selectionChangedListeners.add(listener); 131 } 132 133 141 protected void fireHelpRequested(HelpEvent event) { 142 Object [] listeners = helpListeners.getListeners(); 143 for (int i = 0; i < listeners.length; ++i) { 144 ((HelpListener) listeners[i]).helpRequested(event); 145 } 146 } 147 148 156 protected void fireSelectionChanged(final SelectionChangedEvent event) { 157 Object [] listeners = selectionChangedListeners.getListeners(); 158 for (int i = 0; i < listeners.length; ++i) { 159 final ISelectionChangedListener l = (ISelectionChangedListener) listeners[i]; 160 SafeRunnable.run(new SafeRunnable() { 161 public void run() { 162 l.selectionChanged(event); 163 } 164 }); 165 } 166 } 167 168 173 public abstract Control getControl(); 174 175 190 public Object getData(String key) { 191 Assert.isNotNull(key); 192 if (keys == null) { 193 return null; 194 } 195 for (int i = 0; i < keys.length; i++) { 196 if (keys[i].equals(key)) { 197 return values[i]; 198 } 199 } 200 return null; 201 } 202 203 206 public abstract Object getInput(); 207 208 211 public abstract ISelection getSelection(); 212 213 220 protected void handleHelpRequest(HelpEvent event) { 221 Object oldData = event.data; 222 event.data = this; 223 fireHelpRequested(event); 224 event.data = oldData; 225 } 226 227 240 protected void inputChanged(Object input, Object oldInput) { 241 } 242 243 247 public abstract void refresh(); 248 249 255 public void removeHelpListener(HelpListener listener) { 256 helpListeners.remove(listener); 257 if (helpListeners.size() == 0) { 258 Control control = getControl(); 259 if (control != null && !control.isDisposed()) { 260 control.removeHelpListener(this.helpListener); 261 helpHooked = false; 262 } 263 } 264 } 265 266 269 public void removeSelectionChangedListener( 270 ISelectionChangedListener listener) { 271 selectionChangedListeners.remove(listener); 272 } 273 274 284 public Item scrollDown(int x, int y) { 285 return null; 286 } 287 288 298 public Item scrollUp(int x, int y) { 299 return null; 300 } 301 302 319 public void setData(String key, Object value) { 320 Assert.isNotNull(key); 321 322 if (value == null) { 323 if (keys == null) { 324 return; 325 } 326 int index = 0; 327 while (index < keys.length && !keys[index].equals(key)) { 328 index++; 329 } 330 if (index == keys.length) { 331 return; 332 } 333 if (keys.length == 1) { 334 keys = null; 335 values = null; 336 } else { 337 String [] newKeys = new String [keys.length - 1]; 338 Object [] newValues = new Object [values.length - 1]; 339 System.arraycopy(keys, 0, newKeys, 0, index); 340 System.arraycopy(keys, index + 1, newKeys, index, 341 newKeys.length - index); 342 System.arraycopy(values, 0, newValues, 0, index); 343 System.arraycopy(values, index + 1, newValues, index, 344 newValues.length - index); 345 keys = newKeys; 346 values = newValues; 347 } 348 return; 349 } 350 351 352 if (keys == null) { 353 keys = new String [] { key }; 354 values = new Object [] { value }; 355 return; 356 } 357 for (int i = 0; i < keys.length; i++) { 358 if (keys[i].equals(key)) { 359 values[i] = value; 360 return; 361 } 362 } 363 String [] newKeys = new String [keys.length + 1]; 364 Object [] newValues = new Object [values.length + 1]; 365 System.arraycopy(keys, 0, newKeys, 0, keys.length); 366 System.arraycopy(values, 0, newValues, 0, values.length); 367 newKeys[keys.length] = key; 368 newValues[values.length] = value; 369 keys = newKeys; 370 values = newValues; 371 } 372 373 378 public abstract void setInput(Object input); 379 380 391 public void setSelection(ISelection selection) { 392 setSelection(selection, false); 393 } 394 395 405 public abstract void setSelection(ISelection selection, boolean reveal); 406 } 407 | Popular Tags |