001 /*
002 GRANITE DATA SERVICES
003 Copyright (C) 2012 GRANITE DATA SERVICES S.A.S.
004
005 This file is part of Granite Data Services.
006
007 Granite Data Services is free software; you can redistribute it and/or modify
008 it under the terms of the GNU Library General Public License as published by
009 the Free Software Foundation; either version 2 of the License, or (at your
010 option) any later version.
011
012 Granite Data Services is distributed in the hope that it will be useful, but
013 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
014 FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License
015 for more details.
016
017 You should have received a copy of the GNU Library General Public License
018 along with this library; if not, see <http://www.gnu.org/licenses/>.
019 */
020
021 package org.granite.client.tide.collections.javafx;
022
023 import java.util.Collection;
024 import java.util.Map;
025 import java.util.Set;
026
027 import javafx.beans.InvalidationListener;
028 import javafx.collections.MapChangeListener;
029 import javafx.collections.ObservableMap;
030
031 import org.granite.client.persistence.LazyableCollection;
032 import org.granite.client.tide.collections.ManagedPersistentMap;
033 import org.granite.client.tide.data.Identifiable;
034
035
036 /**
037 * Internal implementation of persistent collection handling automatic lazy loading.<br/>
038 * Used for wrapping persistent collections received from the server.<br/>
039 * Should not be used directly.
040 *
041 * @author William DRAI
042 */
043 public class JavaFXManagedPersistentMap<K, V> extends AbstractJavaFXManagedPersistentAssociation implements ObservableMap<K, V>, ManagedPersistentMap<K, V> {
044
045 private final ObservableMap<K, V> map;
046
047
048 @SuppressWarnings("unchecked")
049 public JavaFXManagedPersistentMap(Identifiable entity, String propertyName, LazyableCollection map) {
050 super(entity, propertyName);
051 this.map = (ObservableMap<K, V>)map;
052 }
053
054 public Object getObject() {
055 return map;
056 }
057
058 @Override
059 public LazyableCollection getCollection() {
060 return (LazyableCollection)map;
061 }
062
063 public int size() {
064 if (checkForRead())
065 return map.size();
066 return 0;
067 }
068
069 public boolean isEmpty() {
070 if (checkForRead())
071 return map.isEmpty();
072 return true;
073 }
074
075 @Override
076 public void clear() {
077 boolean wasInitialized = checkForRead(false);
078 map.clear();
079 if (!wasInitialized)
080 initialize();
081 }
082
083 public V get(Object key) {
084 if (checkForRead())
085 return map.get(key);
086 return null;
087 }
088
089 @Override
090 public boolean containsKey(Object item) {
091 if (checkForRead())
092 return map.containsKey(item);
093 return false;
094 }
095
096 @Override
097 public boolean containsValue(Object item) {
098 if (checkForRead())
099 return map.containsValue(item);
100 return false;
101 }
102
103 @Override
104 public Set<K> keySet() {
105 if (checkForRead())
106 return map.keySet();
107 return null;
108 }
109
110 @Override
111 public Collection<V> values() {
112 if (checkForRead())
113 return map.values();
114 return null;
115 }
116
117 @Override
118 public Set<Entry<K, V>> entrySet() {
119 if (checkForRead())
120 return map.entrySet();
121 return null;
122 }
123
124 @Override
125 public V put(K key, V value) {
126 checkForWrite();
127 return map.put(key, value);
128 }
129
130 public void putAll(Map<? extends K, ? extends V> m) {
131 checkForWrite();
132 map.putAll(m);
133 }
134
135 public V remove(Object key) {
136 checkForWrite();
137 return map.remove(key);
138 }
139
140 @Override
141 public void addListener(InvalidationListener listener) {
142 map.addListener(listener);
143 }
144
145 @Override
146 public void addListener(MapChangeListener<? super K, ? super V> listener) {
147 map.addListener(listener);
148 }
149
150 @Override
151 public void removeListener(InvalidationListener listener) {
152 map.removeListener(listener);
153 }
154
155 @Override
156 public void removeListener(MapChangeListener<? super K, ? super V> listener) {
157 map.removeListener(listener);
158 }
159
160 @Override
161 public LazyableCollection clone(boolean uninitialize) {
162 return new JavaFXManagedPersistentMap<K, V>(getOwner(), getPropertyName(), ((LazyableCollection)map).clone(uninitialize));
163 }
164 }