PageRenderTime 51ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/org.eclipse.core.databinding/src/org/eclipse/core/internal/databinding/observable/MapEntryObservableValue.d

https://bitbucket.org/torhus/dwt2
D | 115 lines | 75 code | 13 blank | 27 comment | 8 complexity | c33da2b47df7be43bdec225666f46e2d MD5 | raw file
  1. /*******************************************************************************
  2. * Copyright (c) 2008 Marko Topolnik 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. * Marko Topolnik - initial API and implementation (bug 184830)
  10. * Matthew Hall - bug 184830
  11. ******************************************************************************/
  12. module org.eclipse.core.internal.databinding.observable.MapEntryObservableValue;
  13. import java.lang.all;
  14. import org.eclipse.core.databinding.observable.Diffs;
  15. import org.eclipse.core.databinding.observable.IStaleListener;
  16. import org.eclipse.core.databinding.observable.ObservableTracker;
  17. import org.eclipse.core.databinding.observable.StaleEvent;
  18. import org.eclipse.core.databinding.observable.map.IMapChangeListener;
  19. import org.eclipse.core.databinding.observable.map.IObservableMap;
  20. import org.eclipse.core.databinding.observable.map.MapChangeEvent;
  21. import org.eclipse.core.databinding.observable.value.AbstractObservableValue;
  22. import org.eclipse.core.databinding.observable.value.IObservableValue;
  23. /**
  24. * An {@link IObservableValue} that tracks the value of an entry in an
  25. * {@link IObservableMap}, identified by the entry's key.
  26. *
  27. * @since 1.1
  28. */
  29. public class MapEntryObservableValue : AbstractObservableValue {
  30. private IObservableMap map;
  31. private Object key;
  32. private Object valueType;
  33. private IMapChangeListener changeListener;
  34. class ChangeListener : IMapChangeListener {
  35. public void handleMapChange(MapChangeEvent event) {
  36. if (event.diff.getAddedKeys().contains(key)) {
  37. final Object newValue = event.diff.getNewValue(key);
  38. if (newValue !is null) {
  39. fireValueChange(Diffs.createValueDiff(cast(Object)null, newValue));
  40. }
  41. } else if (event.diff.getChangedKeys().contains(key)) {
  42. fireValueChange(Diffs.createValueDiff(event.diff
  43. .getOldValue(key), event.diff.getNewValue(key)));
  44. } else if (event.diff.getRemovedKeys().contains(key)) {
  45. final Object oldValue = event.diff.getOldValue(key);
  46. if (oldValue !is null) {
  47. fireValueChange(Diffs.createValueDiff(oldValue, cast(Object)null));
  48. }
  49. }
  50. }
  51. };
  52. private IStaleListener staleListener;
  53. class StaleListener : IStaleListener {
  54. public void handleStale(StaleEvent staleEvent) {
  55. fireStale();
  56. }
  57. };
  58. /**
  59. * Creates a map entry observable.
  60. *
  61. * @param map
  62. * the observable map whose entry will be tracked
  63. * @param key
  64. * the key identifying the entry whose value will be tracked
  65. * @param valueType
  66. * the type of the value
  67. */
  68. public this(IObservableMap map, Object key,
  69. Object valueType) {
  70. changeListener = new ChangeListener();
  71. staleListener = new StaleListener();
  72. super(map.getRealm());
  73. this.map = map;
  74. this.key = key;
  75. this.valueType = valueType;
  76. map.addMapChangeListener(changeListener);
  77. map.addStaleListener(staleListener);
  78. }
  79. public Object getValueType() {
  80. return this.valueType;
  81. }
  82. public bool isStale() {
  83. ObservableTracker.getterCalled(this);
  84. return map.isStale();
  85. }
  86. public synchronized void dispose() {
  87. if (map !is null) {
  88. map.removeMapChangeListener(changeListener);
  89. map.removeStaleListener(staleListener);
  90. map = null;
  91. changeListener = null;
  92. staleListener = null;
  93. }
  94. super.dispose();
  95. }
  96. protected Object doGetValue() {
  97. return this.map.get(this.key);
  98. }
  99. protected void doSetValue(Object value) {
  100. this.map.put(this.key, value);
  101. }
  102. }