1 package org.sapia.gumby; 2 3 import java.util.ArrayList ; 4 import java.util.HashMap ; 5 import java.util.List ; 6 import java.util.Map ; 7 8 23 public class DefaultGuiEnv implements GuiEnv { 24 25 private List _scopeList = new ArrayList (); 26 private Map _scopeMap = new HashMap (); 27 28 public GuiEnv _parent; 29 30 public DefaultGuiEnv() { 31 } 32 33 public DefaultGuiEnv(GuiEnv parent) { 34 _parent = parent; 35 } 36 37 40 public synchronized Object get(String name) { 41 for(int i = 0; i < _scopeList.size(); i++) { 42 Scope scope; 43 Object toReturn = null; 44 scope = (Scope) _scopeList.get(i); 45 if((toReturn = scope.get(name)) != null) { 46 return toReturn; 47 } 48 } 49 if(_parent != null) { 50 return _parent.get(name); 51 } 52 return null; 53 } 54 55 58 public synchronized Object get(String name, String scopeName) { 59 Scope scope = (Scope) _scopeMap.get(scopeName); 60 if(scope == null) { 61 if(_parent != null) { 62 return _parent.get(name, scopeName); 63 } 64 return null; 65 } 66 return scope.get(name); 67 } 68 69 73 public synchronized void put(String name, Object toBind, String scopeName) { 74 Scope scope = (Scope) _scopeMap.get(scopeName); 75 if(scope == null) { 76 if(_parent != null) { 77 if(_parent.hasScope(scopeName)) { 78 _parent.put(name, toBind, scopeName); 79 return; 80 } 81 } 82 scope = new MapScope(); 83 _scopeList.add(scope); 84 _scopeMap.put(scopeName, scope); 85 } 86 scope.put(name, toBind); 87 } 88 89 92 public synchronized Object acquire(String name, String scope) 93 throws IllegalArgumentException { 94 Object toReturn = get(name, scope); 95 if(toReturn == null) { 96 throw new IllegalStateException ("Could not acquire object for: " + name); 97 } 98 return toReturn; 99 } 100 101 104 public synchronized Object acquire(String name) 105 throws IllegalArgumentException { 106 Object toReturn = get(name); 107 if(toReturn == null) { 108 throw new IllegalStateException ("Could not acquire object for: " + name); 109 } 110 return toReturn; 111 } 112 113 116 public synchronized boolean hasScope(String name) { 117 if(!_scopeMap.containsKey(name)) { 118 if(_parent != null) { 119 return _parent.hasScope(name); 120 } 121 return false; 122 } 123 return true; 124 } 125 126 130 public synchronized void addScope(String scopeName, Scope scope) { 131 _scopeList.add(scope); 132 _scopeMap.put(scopeName, scope); 133 } 134 135 138 public synchronized void removeScope(String name) { 139 Scope scope = (Scope) _scopeMap.remove(name); 140 _scopeList.remove(scope); 141 } 142 143 146 public synchronized Scope getScope(String name) { 147 Scope scope = (Scope) _scopeMap.get(name); 148 if(scope == null && _parent != null) { 149 scope = _parent.getScope(name); 150 } 151 return scope; 152 } 153 154 public String toString() { 155 return _scopeMap.toString(); 156 } 157 158 } 159 | Popular Tags |