1 19 20 21 package org.netbeans.core.windows.model; 22 23 24 import org.netbeans.core.windows.WindowManagerImpl; 25 import org.openide.windows.TopComponent; 26 27 import java.util.ArrayList ; 28 import java.util.Iterator ; 29 import java.util.List ; 30 import org.netbeans.core.windows.Constants; 31 32 40 final class TopComponentSubModel { 41 42 43 private final List <TopComponent> openedTopComponents = new ArrayList <TopComponent>(10); 44 45 private final List <String > tcIDs = new ArrayList <String >(10); 46 47 private final int kind; 48 49 private String selectedTopComponentID; 50 51 private String previousSelectedTopComponentID; 52 53 public TopComponentSubModel(int kind) { 54 this.kind = kind; 55 } 56 57 public List <TopComponent> getTopComponents() { 58 List <TopComponent> l = new ArrayList <TopComponent>(openedTopComponents); 59 60 List <String > ids = new ArrayList <String >(tcIDs); 61 List <TopComponent> ll = new ArrayList <TopComponent>(ids.size()); 62 for(Iterator <String > it = ids.iterator(); it.hasNext(); ) { 63 String tcID = it.next(); 64 TopComponent tc = getTopComponent(tcID); 65 if(tc != null) { 66 ll.add(tc); 67 } else { 68 it.remove(); 70 } 71 } 72 ll.removeAll(openedTopComponents); 73 l.addAll(ll); 74 75 return l; 76 } 77 78 public List <TopComponent> getOpenedTopComponents() { 79 return new ArrayList <TopComponent>(openedTopComponents); 80 } 81 82 public boolean addOpenedTopComponent(TopComponent tc) { 83 if(openedTopComponents.contains(tc)) { 84 return false; 85 } 86 87 String tcID = getID(tc); 88 int index = tcIDs.indexOf(tcID); 89 90 int position = openedTopComponents.size(); 91 if( index >= 0 ) { 92 for( TopComponent otc : openedTopComponents ) { 93 String otcID = getID(otc); 94 int openedIndex = tcIDs.indexOf( otcID ); 95 if( openedIndex >= index ) { 96 position = openedTopComponents.indexOf( otc ); 97 break; 98 } 99 } 100 } 101 TopComponent persTC = getTopComponent(tcID); 104 if (persTC != tc) { 105 String message = "Model in inconsistent state, generated TC ID=" + tcID + " for " + tc.getClass() + ":" + tc.hashCode() + " but" + 106 " that ID is reserved for TC=" + persTC.getClass() + ":" + persTC.hashCode(); 107 assert false : message; 108 } 109 openedTopComponents.add(position, tc); 111 if(!tcIDs.contains(tcID)) { 112 tcIDs.add(tcID); 113 } 114 115 if(selectedTopComponentID == null && !isNullSelectionAllowed()) { 116 selectedTopComponentID = tcID; 117 } 118 119 if (kind == Constants.MODE_KIND_SLIDING) { 121 setSlidingProperty(tc); 122 } else { 123 clearSlidingProperty(tc); 124 } 125 126 return true; 127 } 128 129 public boolean insertOpenedTopComponent(TopComponent tc, int index) { 130 if(index >= 0 131 && !openedTopComponents.isEmpty() 132 && openedTopComponents.size() > index 133 && openedTopComponents.get(index) == tc) { 134 return false; 135 } 136 137 openedTopComponents.remove(tc); 139 140 int position = index; 141 if(position < 0) { 142 position = 0; 143 } else if(position > openedTopComponents.size()) { 144 position = openedTopComponents.size(); 145 } 146 147 String tcID = getID(tc); 148 tcIDs.remove(tcID); 149 openedTopComponents.add(position, tc); 150 if(position == 0) { 151 tcIDs.add(0, tcID); 152 } else { 153 TopComponent previous = (TopComponent)openedTopComponents.get(position - 1); 154 int previousIndex = tcIDs.indexOf(getID(previous)); 155 tcIDs.add(previousIndex + 1, tcID); 156 } 157 158 if(selectedTopComponentID == null && !isNullSelectionAllowed()) { 159 selectedTopComponentID = getID(tc); 160 } 161 162 if (kind == Constants.MODE_KIND_SLIDING) { 164 setSlidingProperty(tc); 165 } else { 166 clearSlidingProperty(tc); 167 } 168 169 return true; 170 } 171 172 public boolean addClosedTopComponent(TopComponent tc) { 173 int index = openedTopComponents.indexOf(tc); 174 String tcID = getID(tc); 175 if (!tcIDs.contains(tcID)) { 176 tcIDs.add(tcID); 177 } 178 if(index != -1) { 179 openedTopComponents.remove(tc); 180 if (selectedTopComponentID != null && selectedTopComponentID.equals(getID(tc))) { 181 adjustSelectedTopComponent(index); 182 } 183 } 184 185 186 if (kind == Constants.MODE_KIND_SLIDING) { 188 setSlidingProperty(tc); 189 } else { 190 clearSlidingProperty(tc); 191 } 192 193 return true; 194 } 195 196 public boolean addUnloadedTopComponent(String tcID) { 197 if(!tcIDs.contains(tcID)) { 198 tcIDs.add(tcID); 199 } 200 201 return true; 202 } 203 204 public boolean removeTopComponent(TopComponent tc) { 205 boolean res; 206 String tcID = getID(tc); 207 if(openedTopComponents.contains(tc)) { 208 if(selectedTopComponentID != null && selectedTopComponentID.equals(tcID)) { 209 int index = openedTopComponents.indexOf(getTopComponent(selectedTopComponentID)); 210 openedTopComponents.remove(tc); 211 adjustSelectedTopComponent(index); 212 } else { 213 openedTopComponents.remove(tc); 214 } 215 tcIDs.remove(tcID); 216 217 res = true; 218 } else if(tcIDs.contains(tcID)) { 219 tcIDs.remove(tcID); 220 res = true; 221 } else { 222 res = false; 223 } 224 225 clearSlidingProperty(tc); 227 228 return res; 229 } 230 231 public boolean containsTopComponent(TopComponent tc) { 232 return openedTopComponents.contains(tc) || tcIDs.contains(getID(tc)); 233 } 234 235 public boolean isEmpty() { 236 return tcIDs.isEmpty(); 237 } 238 239 private void adjustSelectedTopComponent(int index) { 240 if(openedTopComponents.isEmpty() || isNullSelectionAllowed()) { 241 selectedTopComponentID = null; 242 return; 243 } 244 245 if(index > openedTopComponents.size() - 1) { 246 index = openedTopComponents.size() - 1; 247 } 248 249 selectedTopComponentID = getID((TopComponent)openedTopComponents.get(index)); 250 } 251 252 255 private boolean isNullSelectionAllowed() { 256 return kind == Constants.MODE_KIND_SLIDING; 257 } 258 259 262 public void setSelectedTopComponent(TopComponent tc) { 263 if(tc != null && !openedTopComponents.contains(tc)) { 264 return; 265 } 266 267 if (tc == null && isNullSelectionAllowed()) { 268 selectedTopComponentID = null; 269 } else { 270 selectedTopComponentID = getID(tc); 271 } 272 } 273 274 public void setPreviousSelectedTopComponent(TopComponent tc) { 275 if(tc != null ) 276 previousSelectedTopComponentID = getID(tc); 277 else 278 previousSelectedTopComponentID = null; 279 } 280 281 public void setUnloadedSelectedTopComponent(String tcID) { 282 if(tcID != null && !getOpenedTopComponentsIDs().contains(tcID)) { 283 return; 284 } 285 286 selectedTopComponentID = tcID; 287 } 288 289 public void setUnloadedPreviousSelectedTopComponent(String tcID) { 290 previousSelectedTopComponentID = tcID; 291 } 292 293 public List <String > getOpenedTopComponentsIDs() { 294 List <String > l = new ArrayList <String >(openedTopComponents.size()); 295 for(Iterator <TopComponent> it = openedTopComponents.iterator(); it.hasNext(); ) { 296 l.add(getID(it.next())); 297 } 298 return l; 299 } 300 301 public List <String > getClosedTopComponentsIDs() { 303 List <String > closedIDs = new ArrayList <String >(tcIDs); 304 closedIDs.removeAll(getOpenedTopComponentsIDs()); 305 return closedIDs; 306 } 307 308 public List <String > getTopComponentsIDs() { 310 return new ArrayList <String >(tcIDs); 311 } 312 313 public void removeClosedTopComponentID(String tcID) { 315 tcIDs.remove(tcID); 316 } 317 318 public TopComponent getSelectedTopComponent() { 319 return getTopComponent(selectedTopComponentID); 320 } 321 322 public TopComponent getPreviousSelectedTopComponent() { 323 if( null != previousSelectedTopComponentID ) 324 return getTopComponent(previousSelectedTopComponentID); 325 return null; 326 } 327 328 private static TopComponent getTopComponent(String tcID) { 329 return WindowManagerImpl.getInstance().getTopComponentForID(tcID); 330 } 331 332 private static String getID(TopComponent tc) { 333 return WindowManagerImpl.getInstance().findTopComponentID(tc); 334 } 335 336 337 private static final String IS_SLIDING = "isSliding"; 339 340 private void setSlidingProperty(TopComponent tc) { 342 tc.putClientProperty(IS_SLIDING, Boolean.TRUE); 343 } 344 345 private void clearSlidingProperty(TopComponent tc) { 347 tc.putClientProperty(IS_SLIDING, null); 348 } 349 350 } 351 | Popular Tags |