1 19 20 package org.netbeans.core.windows.model; 21 22 23 import org.netbeans.core.windows.WindowManagerImpl; 24 import org.openide.windows.TopComponent; 25 26 import java.util.Collection ; 27 import java.util.HashSet ; 28 import java.util.Iterator ; 29 import java.util.Set ; 30 31 32 36 final class DefaultTopComponentGroupModel implements TopComponentGroupModel { 37 38 39 private final String name; 40 41 42 private boolean opened; 43 44 45 private final Set <String > topComponents = new HashSet <String >(3); 46 49 private final Set <String > openedTopComponents = new HashSet <String >(3); 50 51 54 private final Set <String > openedBeforeTopComponents = new HashSet <String >(3); 55 56 57 private final Set <String > openingTopComponents = new HashSet <String >(3); 58 59 private final Set <String > closingTopComponents = new HashSet <String >(3); 60 61 private final Object LOCK_OPENED = new Object (); 62 63 private final Object LOCK_TOPCOMPONENTS = new Object (); 64 65 66 public DefaultTopComponentGroupModel(String name, boolean opened) { 67 this.name = name; 68 this.opened = opened; 69 } 70 71 72 public String getName() { 73 return name; 74 } 75 76 public void open( 77 Collection <TopComponent> openedTopComponents, 78 Collection <TopComponent> openedBeforeTopComponents) { 79 synchronized(LOCK_OPENED) { 80 this.opened = true; 81 this.openedTopComponents.clear(); 82 for(TopComponent tc: openedTopComponents) { 83 String tcID = getID(tc); 84 if(tcID != null) { 85 this.openedTopComponents.add(tcID); 86 } 87 } 88 this.openedBeforeTopComponents.clear(); 89 for(TopComponent tc: openedBeforeTopComponents) { 90 String tcID = getID(tc); 91 if(tcID != null) { 92 this.openedBeforeTopComponents.add(tcID); 93 } 94 } 95 } 96 } 97 98 public void close() { 99 synchronized(LOCK_OPENED) { 100 this.opened = false; 101 this.openedTopComponents.clear(); 102 this.openedBeforeTopComponents.clear(); 103 } 104 } 105 106 public boolean isOpened() { 107 synchronized(LOCK_OPENED) { 108 return this.opened; 109 } 110 } 111 112 public Set <TopComponent> getTopComponents() { 113 Set <String > s; 114 synchronized(LOCK_TOPCOMPONENTS) { 115 s = new HashSet <String >(topComponents); 116 } 117 118 Set <TopComponent> result = new HashSet <TopComponent>(s.size()); 119 for(String tcId: s) { 120 TopComponent tc = getTopComponent(tcId); 121 if(tc != null) { 122 result.add(tc); 123 } 124 } 125 126 return result; 127 } 128 129 public Set <TopComponent> getOpenedTopComponents() { 130 Set <String > s; 131 synchronized(LOCK_OPENED) { 132 s = new HashSet <String >(openedTopComponents); 133 } 134 135 Set <TopComponent> result = new HashSet <TopComponent>(s.size()); 136 for(String tcId: s) { 137 TopComponent tc = getTopComponent(tcId); 138 if(tc != null) { 139 result.add(tc); 140 } 141 } 142 143 return result; 144 } 145 146 public Set <TopComponent> getOpenedBeforeTopComponents() { 147 Set <String > s; 148 synchronized(LOCK_OPENED) { 149 s = new HashSet <String >(openedBeforeTopComponents); 150 } 151 152 Set <TopComponent> result = new HashSet <TopComponent>(s.size()); 153 for(String tcId: s) { 154 TopComponent tc = getTopComponent(tcId); 155 if(tc != null) { 156 result.add(tc); 157 } 158 } 159 160 return result; 161 } 162 163 public Set <TopComponent> getOpeningTopComponents() { 164 Set <String > s; 165 synchronized(LOCK_TOPCOMPONENTS) { 166 s = new HashSet <String >(openingTopComponents); 167 } 168 169 Set <TopComponent> result = new HashSet <TopComponent>(s.size()); 170 for(String tcId: s) { 171 TopComponent tc = getTopComponent(tcId); 172 if(tc != null) { 173 result.add(tc); 174 } 175 } 176 177 return result; 178 } 179 180 public Set <TopComponent> getClosingTopComponents() { 181 Set <String > s; 182 synchronized(LOCK_TOPCOMPONENTS) { 183 s = new HashSet <String >(closingTopComponents); 184 } 185 186 Set <TopComponent> result = new HashSet <TopComponent>(s.size()); 187 for(String tcId: s) { 188 TopComponent tc = getTopComponent(tcId); 189 if(tc != null) { 190 result.add(tc); 191 } 192 } 193 194 return result; 195 } 196 197 public boolean addUnloadedTopComponent(String tcID) { 198 synchronized(LOCK_TOPCOMPONENTS) { 199 return topComponents.add(tcID); 200 } 201 } 202 203 public boolean removeUnloadedTopComponent(String tcID) { 204 synchronized(LOCK_TOPCOMPONENTS) { 205 if(openingTopComponents.contains(tcID)) { 206 openingTopComponents.remove(tcID); 207 } 208 if(closingTopComponents.contains(tcID)) { 209 closingTopComponents.remove(tcID); 210 } 211 return topComponents.remove(tcID); 212 } 213 } 214 215 public boolean addOpeningTopComponent(TopComponent tc) { 216 return addUnloadedOpeningTopComponent(getID(tc)); 217 } 218 219 public boolean addUnloadedOpeningTopComponent(String tcID) { 220 synchronized(LOCK_TOPCOMPONENTS) { 221 if(!topComponents.contains(tcID)) { 222 topComponents.add(tcID); 223 } 224 return openingTopComponents.add(tcID); 225 } 226 } 227 228 public boolean removeOpeningTopComponent(TopComponent tc) { 229 return removeUnloadedOpeningTopComponent(getID(tc)); 230 } 231 232 public boolean removeUnloadedOpeningTopComponent(String tcID) { 233 synchronized(LOCK_TOPCOMPONENTS) { 234 return openingTopComponents.remove(tcID); 235 } 236 } 237 238 public boolean addUnloadedClosingTopComponent(String tcID) { 239 synchronized(LOCK_TOPCOMPONENTS) { 240 if(!topComponents.contains(tcID)) { 241 topComponents.add(tcID); 242 } 243 return closingTopComponents.add(tcID); 244 } 245 } 246 247 public boolean removeUnloadedClosingTopComponent(String tcID) { 248 synchronized(LOCK_TOPCOMPONENTS) { 249 return closingTopComponents.remove(tcID); 250 } 251 } 252 253 public boolean addUnloadedOpenedTopComponent(String tcID) { 255 synchronized(LOCK_OPENED) { 256 if(!this.opened) { 257 return false; 258 } 259 this.openedTopComponents.add(tcID); 260 } 261 return true; 262 } 263 264 private static TopComponent getTopComponent(String tcID) { 265 return WindowManagerImpl.getInstance().getTopComponentForID(tcID); 266 } 267 268 private static String getID(TopComponent tc) { 269 return WindowManagerImpl.getInstance().findTopComponentID(tc); 270 } 271 272 273 public Set <String > getTopComponentsIDs() { 275 synchronized(LOCK_TOPCOMPONENTS) { 276 return new HashSet <String >(topComponents); 277 } 278 } 279 280 public Set <String > getOpeningSetIDs() { 281 synchronized(LOCK_TOPCOMPONENTS) { 282 return new HashSet <String >(openingTopComponents); 283 } 284 } 285 public Set <String > getClosingSetIDs() { 286 synchronized(LOCK_TOPCOMPONENTS) { 287 return new HashSet <String >(closingTopComponents); 288 } 289 } 290 public Set <String > getOpenedTopComponentsIDs() { 291 synchronized(LOCK_TOPCOMPONENTS) { 292 return new HashSet <String >(openedTopComponents); 293 } 294 } 295 297 } 298 299 | Popular Tags |