001: package org.swingml.registry;
002:
003: import java.awt.*;
004: import java.util.*;
005: import java.util.List;
006:
007: import org.swingml.*;
008: import org.swingml.system.*;
009:
010: /**
011: * This registry is used to register AbstractSwingMLModels to the Containers
012: * they represent.
013: *
014: * @author CrossLogic
015: */
016: public class SwingMLModelToContainerRegistry {
017:
018: private static Map guidRegistry = new HashMap();
019: private static Map modelKeyToGuidMap = new HashMap();
020: private static long nextGuid = Long.MIN_VALUE;
021:
022: /**
023: * generate a key from the given model
024: *
025: * @param model
026: * @return
027: */
028: private static String generateKey(AbstractSwingMLModel model) {
029: String result = null;
030: if (model != null) {
031: result = generateKey(model.getName(), model.getId());
032: }
033: return result;
034: }
035:
036: private static String generateKey(String name, String id) {
037: String result = null;
038: if (name != null) {
039: result = name;
040: if (id != null) {
041: result += "::" + id;
042: }
043: }
044: return result;
045: }
046:
047: /**
048: * Return the registered Container for the given name.
049: *
050: * @param model
051: * @return
052: */
053: public static Container getContainer(String name) {
054: return getContainer(name, null);
055: }
056:
057: /**
058: * Return the registered Container for the given name.
059: *
060: * @param model
061: * @return
062: */
063: public static Container getContainer(String name, String id) {
064: Container result = null;
065: synchronized (getModelKeyToGuidMap()) {
066: String modelKey = generateKey(name, id);
067: if (modelKey != null) {
068: List guidList = (List) getModelKeyToGuidMap().get(
069: modelKey);
070: if (guidList != null && !guidList.isEmpty()) {
071: Long guid = (Long) guidList
072: .get(guidList.size() - 1);
073: AbstractSwingMLModel model = (AbstractSwingMLModel) getGuidRegistry()
074: .get(guid);
075: if (model != null) {
076: result = model.getContainer();
077: }
078: }
079: }
080: }
081: return result;
082: }
083:
084: /**
085: * Return the guid for the given value. Null, otherwise.
086: *
087: * @param value
088: * @return
089: */
090: private static Long getGuid(Container container) {
091: Long result = null;
092: synchronized (getGuidRegistry()) {
093: try {
094: Iterator schmiterator = getGuidRegistry().keySet()
095: .iterator();
096: while (schmiterator.hasNext()) {
097: Long guid = (Long) schmiterator.next();
098: AbstractSwingMLModel model = (AbstractSwingMLModel) getGuidRegistry()
099: .get(guid);
100: if (model.getContainer() != null
101: && model.getContainer().equals(container)) {
102: result = guid;
103: break;
104: }
105: }
106: } catch (Exception e) {
107: e.printStackTrace();
108: }
109: }
110: return result;
111: }
112:
113: private static Map getGuidRegistry() {
114: return guidRegistry;
115: }
116:
117: /**
118: * Return the registered AbstractSwingMLModel for the given container.
119: *
120: * @param container
121: * @return
122: */
123: public static AbstractSwingMLModel getModel(Container container) {
124: AbstractSwingMLModel result = null;
125: synchronized (getGuidRegistry()) {
126: Long guid = getGuid(container);
127: if (guid != null) {
128: try {
129: result = (AbstractSwingMLModel) getGuidRegistry()
130: .get(guid);
131: } catch (Exception e) {
132: SwingMLLogger
133: .getInstance()
134: .log(ILogCapable.ERROR,
135: "Invalid SwingMLModel registered with a container.");
136: SwingMLLogger.getInstance().log(ILogCapable.ERROR,
137: e);
138: }
139: }
140: }
141: return result;
142: }
143:
144: /**
145: * Return the registered AbstractSwingMLModel for the given container.
146: *
147: * @param container
148: * @return
149: */
150: public static AbstractSwingMLModel getModel(String containerName) {
151: return getModel(containerName, null);
152: }
153:
154: /**
155: * Return the registered AbstractSwingMLModel for the given container.
156: *
157: * @param container
158: * @return
159: */
160: public static AbstractSwingMLModel getModel(String containerName,
161: String id) {
162: AbstractSwingMLModel result = null;
163: Container container = getContainer(generateKey(containerName,
164: id));
165: if (container != null) {
166: result = getModel(container);
167: }
168: return result;
169: }
170:
171: public static Map getModelKeyToGuidMap() {
172: return modelKeyToGuidMap;
173: }
174:
175: private static Long getNextGuid() {
176: if (nextGuid == Long.MAX_VALUE) {
177: nextGuid = Long.MIN_VALUE;
178: }
179: return new Long(nextGuid++);
180: }
181:
182: public static long getRegistryCount() {
183: long result = -1;
184: synchronized (getGuidRegistry()) {
185: result = getGuidRegistry().size();
186: }
187: return result;
188: }
189:
190: /**
191: * Register the given model with the given Container.
192: *
193: * @param model
194: * @param container
195: */
196: public static void register(AbstractSwingMLModel model) {
197: synchronized (getGuidRegistry()) {
198: if (model.getName() != null && model.getName().length() > 0) {
199: Long guid = model.getGuid();
200: if (guid == null) {
201: guid = getNextGuid();
202: model.setGuid(guid);
203: }
204: if (!getGuidRegistry().containsKey(guid)) {
205: getGuidRegistry().put(guid, model);
206: String modelKey = generateKey(model);
207: if (modelKey != null) {
208: List guidList = (List) getModelKeyToGuidMap()
209: .get(modelKey);
210: if (guidList == null) {
211: guidList = new ArrayList();
212: }
213: guidList.add(guid);
214: getModelKeyToGuidMap().put(modelKey, guidList);
215: }
216: }
217: }
218: }
219: }
220:
221: /**
222: * Remove the registration of the given AbstractSwingMLModel from the
223: * registry.
224: *
225: * @param model
226: */
227: public static void unregister(AbstractSwingMLModel model) {
228: synchronized (getGuidRegistry()) {
229: Long guid = model.getGuid();
230: if (guid != null) {
231:
232: getGuidRegistry().remove(guid);
233: String modelKey = generateKey(model);
234: List guidList = (List) getModelKeyToGuidMap().get(
235: modelKey);
236: guidList.remove(guid);
237: if (guidList.isEmpty()) {
238: getModelKeyToGuidMap().remove(modelKey);
239: }
240: }
241: }
242: }
243:
244: /**
245: * Remove the registration of the given Container from the registry.
246: *
247: * @param model
248: */
249: public static void unregister(Container container) {
250: AbstractSwingMLModel model = getModel(container);
251: if (model != null) {
252: unregister(model);
253: }
254: }
255: }
|