1 19 20 21 package org.netbeans.core.windows; 22 23 24 25 import java.awt.Dimension ; 26 import java.awt.Frame ; 27 import java.awt.Rectangle ; 28 import java.awt.Toolkit ; 29 import java.io.IOException ; 30 import java.util.*; 31 import java.util.logging.Level ; 32 import java.util.logging.Logger ; 33 import org.netbeans.core.windows.persistence.*; 34 import org.openide.awt.ToolbarPool; 35 import org.openide.filesystems.FileObject; 36 import org.openide.filesystems.FileSystem; 37 import org.openide.filesystems.Repository; 38 import org.openide.util.Exceptions; 39 import org.openide.util.Utilities; 40 import org.openide.windows.*; 41 42 43 48 final public class PersistenceHandler implements PersistenceObserver { 49 50 52 private final Map<String , ModeImpl> name2mode = new WeakHashMap<String , ModeImpl>(10); 53 54 private final Map<String , TopComponentGroupImpl> name2group = new WeakHashMap<String , TopComponentGroupImpl>(10); 55 56 private static PersistenceHandler defaultInstance; 57 58 59 private static final boolean DEBUG = Debug.isLoggable(PersistenceHandler.class); 60 61 62 63 private PersistenceHandler() { 64 } 65 66 69 public void clear() { 70 name2mode.clear(); 71 name2group.clear(); 72 } 73 74 public static synchronized PersistenceHandler getDefault() { 75 if(defaultInstance == null) { 76 defaultInstance = new PersistenceHandler(); 77 } 78 79 return defaultInstance; 80 } 81 82 public boolean isTopComponentPersistentWhenClosed(TopComponent tc) { 84 int persistenceType = tc.getPersistenceType(); 85 if (persistenceType == TopComponent.PERSISTENCE_ALWAYS) { 86 return true; 87 } else { 88 return false; 89 } 90 } 91 92 public void load() { 93 if(DEBUG) { 94 debugLog("## PersistenceHandler.load"); } 96 97 WindowManagerConfig wmc = null; 98 try { 99 wmc = PersistenceManager.getDefault().loadWindowSystem(); 100 } catch (IOException exc) { 101 Exceptions.attachLocalizedMessage(exc, "Cannot load window system persistent data, user directory content is broken. Resetting to default layout..."); Logger.getLogger(PersistenceHandler.class.getName()).log(Level.WARNING, null, exc); FileSystem fs = Repository.getDefault().getDefaultFileSystem(); 106 FileObject rootFolder = fs.getRoot().getFileObject(PersistenceManager.ROOT_LOCAL_FOLDER); 107 if (null != rootFolder) { 108 try { 109 rootFolder.delete(); 110 wmc = PersistenceManager.getDefault().loadWindowSystem(); 111 } catch (IOException ioE) { 112 Exceptions.attachLocalizedMessage(ioE, "Cannot load even default layout, using internally predefined configuration."); Logger.getLogger(PersistenceHandler.class.getName()).log(Level.WARNING, null, ioE); 114 wmc = ConfigFactory.createDefaultConfig(); 115 } 116 } else { 117 Logger.getLogger(PersistenceHandler.class.getName()).log(Level.WARNING, "Cannot even get access to local winsys configuration, using internally predefined configuration."); wmc = ConfigFactory.createDefaultConfig(); 119 } 120 } 121 122 ToolbarPool.getDefault().setPreferredIconSize(wmc.preferredToolbarIconSize); 123 124 WindowManagerImpl wm = WindowManagerImpl.getInstance(); 125 if (wmc.tcIdViewList.length > 0) { 126 List<TopComponent> tcList = new ArrayList<TopComponent>(wmc.tcIdViewList.length); 127 for (int i = 0; i < wmc.tcIdViewList.length; i++) { 128 TopComponent tc = getTopComponentForID(wmc.tcIdViewList[i]); 129 if (tc != null) { 130 tcList.add(tc); 131 } 132 } 133 TopComponent [] tcs = tcList.toArray(new TopComponent[tcList.size()]); 134 wm.setRecentViewList(tcs); 135 } else { 136 List<TopComponent> tcList = new ArrayList<TopComponent>(); 138 for (int i = 0; i < wmc.modes.length; i++) { 139 ModeConfig mc = wmc.modes[i]; 140 for (int j = 0; j < mc.tcRefConfigs.length; j++) { 141 if (mc.tcRefConfigs[j].opened) { 143 TopComponent tc = getTopComponentForID(mc.tcRefConfigs[j].tc_id); 144 if (tc != null) { 145 tcList.add(tc); 146 } 147 } 148 } 149 } 150 TopComponent [] tcs = tcList.toArray(new TopComponent[tcList.size()]); 151 wm.setRecentViewList(tcs); 152 } 153 154 wm.setEditorAreaConstraints(wmc.editorAreaConstraints); 155 wm.setEditorAreaState(wmc.editorAreaState); 156 157 ModeImpl activeMode = null; 158 ModeImpl editorMaximizedMode = null; 159 ModeImpl viewMaximizedMode = null; 160 161 Map<ModeImpl, ModeConfig> mode2config = new HashMap<ModeImpl, ModeConfig>(); 163 Set slidingModes = new HashSet(); 164 165 for (int i = 0; i < wmc.modes.length; i++) { 166 ModeConfig mc = (ModeConfig) wmc.modes[i]; 167 ModeImpl mode = getModeFromConfig(mc); 168 169 mode2config.put(mode, mc); 170 171 if(mc.name.equals(wmc.activeModeName)) { 172 activeMode = mode; 173 } 174 if(mc.name.equals(wmc.editorMaximizedModeName)) { 175 editorMaximizedMode = mode; 176 } else if(mc.name.equals(wmc.viewMaximizedModeName)) { 177 viewMaximizedMode = mode; 178 } 179 } 180 181 for(Iterator it = mode2config.keySet().iterator(); it.hasNext(); ) { 183 ModeImpl mode = (ModeImpl)it.next(); 184 ModeConfig mc = (ModeConfig)mode2config.get(mode); 185 initModeFromConfig(mode, mc); 186 initPreviousModes(mode, mc, mode2config); 187 188 if(mc.selectedTopComponentID != null) { 190 mode.setUnloadedSelectedTopComponent(mc.selectedTopComponentID); 191 } 192 if(mc.previousSelectedTopComponentID != null) { 193 mode.setUnloadedPreviousSelectedTopComponent(mc.previousSelectedTopComponentID); 194 } 195 } 196 197 for (int i = 0; i < wmc.groups.length; i++) { 199 GroupConfig groupCfg = wmc.groups[i]; 200 createTopComponentGroupFromConfig(groupCfg); 201 } 202 203 if(activeMode != null) { 206 TopComponent active = activeMode.getSelectedTopComponent(); 207 if(active != null) { 208 WindowManagerImpl.getInstance().specialPersistenceCompShow(active); 209 } 210 } 211 wm.setActiveMode(activeMode); 215 wm.setEditorMaximizedMode(editorMaximizedMode); 216 wm.setViewMaximizedMode(viewMaximizedMode); 217 218 Rectangle joinedBounds = computeBounds( 219 wmc.centeredHorizontallyJoined, 220 wmc.centeredVerticallyJoined, 221 wmc.xJoined, 222 wmc.yJoined, 223 wmc.widthJoined, 224 wmc.heightJoined, 225 wmc.relativeXJoined, 226 wmc.relativeYJoined, 227 wmc.relativeWidthJoined, 228 wmc.relativeHeightJoined); 229 if(joinedBounds != null) { 230 wm.setMainWindowBoundsJoined(joinedBounds); 231 } 232 234 Rectangle separatedBounds = computeBounds( 235 wmc.centeredHorizontallySeparated, 236 wmc.centeredVerticallySeparated, 237 wmc.xSeparated, 238 wmc.ySeparated, 239 wmc.widthSeparated, 240 wmc.heightSeparated, 241 wmc.relativeXSeparated, 242 wmc.relativeYSeparated, 243 wmc.relativeWidthSeparated, 244 wmc.relativeHeightSeparated); 245 if(separatedBounds != null) { 246 wm.setMainWindowBoundsSeparated(separatedBounds); 247 } 248 250 wm.setMainWindowFrameStateJoined(wmc.mainWindowFrameStateJoined); 251 wm.setMainWindowFrameStateSeparated(wmc.mainWindowFrameStateSeparated); 252 253 Rectangle absBounds = wmc.editorAreaBounds == null ? new Rectangle () : wmc.editorAreaBounds; 254 Rectangle relBounds = wmc.editorAreaRelativeBounds == null ? new Rectangle () : wmc.editorAreaRelativeBounds; 255 Rectangle bounds = computeBounds(false, false, 256 absBounds.x, 257 absBounds.y, 258 absBounds.width, 259 absBounds.height, 260 relBounds.x / 100.0F, 261 relBounds.y / 100.0F, 262 relBounds.width / 100.0F, 263 relBounds.height / 100.0F); 264 wm.setEditorAreaBounds(bounds); 265 wm.setEditorAreaFrameState(wmc.editorAreaFrameState); 266 wm.setToolbarConfigName(wmc.toolbarConfiguration); 267 } 268 269 270 271 public synchronized void save() { 272 if(DEBUG) { 273 debugLog("## PersistenceHandler.save"); } 275 276 WindowManagerConfig wmc = getConfig(); 277 PersistenceManager.getDefault().saveWindowSystem(wmc); 278 } 279 280 private ModeImpl getModeFromConfig(ModeConfig mc) { 281 if(DEBUG) { 282 debugLog("Getting mode name=" + mc.name); 283 } 284 285 ModeImpl mode = (ModeImpl)WindowManagerImpl.getInstance().findMode(mc.name); 286 if(mode == null) { 287 mode = createModeFromConfig(mc); 288 } 289 return mode; 290 } 291 292 private ModeImpl createModeFromConfig(ModeConfig mc) { 293 if(DEBUG) { 294 debugLog(""); debugLog("Creating mode name=\"" + mc.name + "\""); } 297 298 ModeImpl mode; 299 if (mc.kind == Constants.MODE_KIND_SLIDING) { 300 mode = WindowManagerImpl.getInstance().createSlidingMode( 301 mc.name, mc.permanent, mc.side, mc.slideInSizes); 302 } else { 303 mode = WindowManagerImpl.getInstance().createMode( 304 mc.name, mc.kind, mc.state, mc.permanent, mc.constraints); 305 } 306 name2mode.put(mc.name, mode); 307 308 return mode; 309 } 310 311 314 private void initPreviousModes(ModeImpl mode, ModeConfig mc, Map modes) { 315 for (int j = 0; j < mc.tcRefConfigs.length; j++) { 316 TCRefConfig tcRefConfig = (TCRefConfig) mc.tcRefConfigs[j]; 317 if(DEBUG) { 318 debugLog("\tTopComponent[" + j + "] id=\"" + tcRefConfig.tc_id + "\", \topened=" + tcRefConfig.opened); } 321 if (tcRefConfig.previousMode != null) { 322 Iterator it = modes.keySet().iterator(); 323 ModeImpl previous = null; 324 while (it.hasNext()) { 325 ModeImpl md = (ModeImpl)it.next(); 326 327 if (tcRefConfig.previousMode.equals(md.getName())) { 328 previous = md; 329 break; 330 } 331 } 332 if (previous != null) { 333 WindowManagerImpl.getInstance().setPreviousModeForTopComponent(tcRefConfig.tc_id, mode, previous, tcRefConfig.previousIndex); 334 } else { 335 Logger.getLogger(PersistenceHandler.class.getName()).log(Level.WARNING, null, 336 new java.lang.NullPointerException ("Cannot find previous mode named \'" + 337 tcRefConfig.previousMode + 338 "\'")); 339 340 } 341 } 342 } 343 } 344 345 private ModeImpl initModeFromConfig(ModeImpl mode, ModeConfig mc) { 346 WindowManagerImpl wm = WindowManagerImpl.getInstance(); 347 for (int j = 0; j < mc.tcRefConfigs.length; j++) { 348 TCRefConfig tcRefConfig = (TCRefConfig) mc.tcRefConfigs[j]; 349 if(DEBUG) { 350 debugLog("\tTopComponent[" + j + "] id=\"" + tcRefConfig.tc_id + "\", \topened=" + tcRefConfig.opened); } 353 354 if (tcRefConfig.opened) { 356 TopComponent tc = getTopComponentForID(tcRefConfig.tc_id); 357 if(tc != null) { 358 mode.addOpenedTopComponent(tc); 359 } 360 } else { 361 mode.addUnloadedTopComponent(tcRefConfig.tc_id); 362 } 363 wm.setTopComponentDockedInMaximizedMode( tcRefConfig.tc_id, tcRefConfig.dockedInMaximizedMode ); 364 wm.setTopComponentSlidedInDefaultMode( tcRefConfig.tc_id, !tcRefConfig.dockedInDefaultMode ); 365 wm.setTopComponentMaximizedWhenSlidedIn( tcRefConfig.tc_id, tcRefConfig.slidedInMaximized ); 366 } 367 368 Rectangle absBounds = mc.bounds == null ? new Rectangle () : mc.bounds; 370 Rectangle relBounds = mc.relativeBounds == null ? new Rectangle () : mc.relativeBounds; 371 Rectangle bounds = computeBounds(false, false, 372 absBounds.x, 373 absBounds.y, 374 absBounds.width, 375 absBounds.height, 376 relBounds.x / 100.0F, 377 relBounds.y / 100.0F, 378 relBounds.width / 100.0F, 379 relBounds.height / 100.0F); 380 mode.setBounds(bounds); 381 mode.setFrameState(mc.frameState); 382 383 return mode; 384 } 385 386 TopComponent getTopComponentForID(String tc_id) { 387 if(tc_id == null || "".equals(tc_id)) { 388 return null; 389 } 390 391 TopComponent tc = PersistenceManager.getDefault().getTopComponentForID(tc_id); 393 397 return tc; 398 } 399 400 private TopComponentGroupImpl createTopComponentGroupFromConfig(GroupConfig groupCfg) { 401 if(DEBUG) { 402 debugLog(""); debugLog("Creating group name=\"" + groupCfg.name + "\" \t[opened=" + groupCfg.opened + "]"); } 405 406 TopComponentGroupImpl tcGroup = new TopComponentGroupImpl(groupCfg.name, groupCfg.opened); 407 408 name2group.put(groupCfg.name, tcGroup); 409 410 for (int j = 0; j < groupCfg.tcGroupConfigs.length; j++) { 411 TCGroupConfig tcGroupCfg = groupCfg.tcGroupConfigs[j]; 412 if(DEBUG) { 413 debugLog("\tTopComponent[" + j + "] id=\"" + tcGroupCfg.tc_id + "\", \topen=" + tcGroupCfg.open + ", \tclose=" + tcGroupCfg.close 415 + ", \twasOpened=" + tcGroupCfg.wasOpened); } 417 418 tcGroup.addUnloadedTopComponent(tcGroupCfg.tc_id); 419 420 if (tcGroupCfg.open) { 421 tcGroup.addUnloadedOpeningTopComponent(tcGroupCfg.tc_id); 422 } 423 if (tcGroupCfg.close) { 424 tcGroup.addUnloadedClosingTopComponent(tcGroupCfg.tc_id); 425 } 426 427 if(groupCfg.opened && tcGroupCfg.wasOpened) { 429 tcGroup.addGroupUnloadedOpenedTopComponent(tcGroupCfg.tc_id); 430 } 431 } 432 433 WindowManagerImpl.getInstance().addTopComponentGroup(tcGroup); 434 435 return tcGroup; 436 } 437 438 439 440 private WindowManagerConfig getConfig() { 441 WindowManagerConfig wmc = new WindowManagerConfig(); 442 443 wmc.preferredToolbarIconSize = ToolbarPool.getDefault().getPreferredIconSize(); 444 445 WindowManagerImpl wmi = WindowManagerImpl.getInstance(); 446 447 Rectangle joinedBounds = wmi.getMainWindowBoundsJoined(); 448 if(DEBUG) { 449 debugLog("joinedBouds=" + joinedBounds); } 451 wmc.xJoined = joinedBounds.x; 452 wmc.yJoined = joinedBounds.y; 453 wmc.widthJoined = joinedBounds.width; 454 wmc.heightJoined = joinedBounds.height; 455 Rectangle separatedBounds = wmi.getMainWindowBoundsSeparated(); 456 if(DEBUG) { 457 debugLog("separatedBounds=" + separatedBounds); } 459 wmc.xSeparated = separatedBounds.x; 460 wmc.ySeparated = separatedBounds.y; 461 wmc.widthSeparated = separatedBounds.width; 462 wmc.heightSeparated = separatedBounds.height; 463 464 wmc.mainWindowFrameStateJoined = wmi.getMainWindowFrameStateJoined(); 465 if (wmc.mainWindowFrameStateJoined == Frame.ICONIFIED) { 466 wmc.mainWindowFrameStateJoined = Frame.NORMAL; 469 } 470 if(DEBUG) { 471 debugLog("mainWindowFrameStateJoined=" + wmc.mainWindowFrameStateJoined); } 473 wmc.mainWindowFrameStateSeparated = wmi.getMainWindowFrameStateSeparated(); 474 if (wmc.mainWindowFrameStateSeparated == Frame.ICONIFIED) { 475 wmc.mainWindowFrameStateSeparated = Frame.NORMAL; 477 } 478 if(DEBUG) { 479 debugLog("mainWindowFrameStateSeparated=" + wmc.mainWindowFrameStateSeparated); } 481 482 wmc.editorAreaState = wmi.getEditorAreaState(); 483 if(DEBUG) { 484 debugLog("editorAreaState=" + wmc.editorAreaState); } 486 wmc.editorAreaBounds = wmi.getEditorAreaBounds(); 487 if(DEBUG) { 488 debugLog("editorAreaBounds=" + wmc.editorAreaBounds); } 490 wmc.editorAreaConstraints = wmi.getEditorAreaConstraints(); 491 if(DEBUG) { 492 debugLog("editorAreaConstraints=" + Arrays.toString(wmc.editorAreaConstraints)); } 494 wmc.editorAreaFrameState = wmi.getEditorAreaFrameState(); 495 if(DEBUG) { 496 debugLog("editorAreaFrameState=" + wmc.editorAreaFrameState); } 498 wmc.screenSize = Toolkit.getDefaultToolkit().getScreenSize(); 499 500 ModeImpl mo = wmi.getActiveMode(); 501 if(DEBUG) { 502 debugLog("active mode=" + mo); } 504 if (mo != null) { 505 wmc.activeModeName = mo.getName(); 506 } 507 508 mo = wmi.getEditorMaximizedMode(); 509 if(DEBUG) { 510 debugLog("editor maximized mode=" + mo); } 512 if (mo != null) { 513 wmc.editorMaximizedModeName = mo.getName(); 514 } 515 516 mo = wmi.getViewMaximizedMode(); 517 if(DEBUG) { 518 debugLog("view maximized mode=" + mo); } 520 if (mo != null) { 521 wmc.viewMaximizedModeName = mo.getName(); 522 } 523 524 wmc.toolbarConfiguration = wmi.getToolbarConfigName(); 525 if(DEBUG) { 526 debugLog("toolbarConfiguration=" + wmc.toolbarConfiguration); } 528 529 Set<? extends Mode> modeSet = wmi.getModes(); 531 List<ModeConfig> modeConfigs = new ArrayList<ModeConfig>(modeSet.size()); 532 for (Iterator<? extends Mode> it = modeSet.iterator(); it.hasNext(); ) { 533 ModeImpl modeImpl = (ModeImpl)it.next(); 534 modeConfigs.add(getConfigFromMode(modeImpl)); 535 } 536 wmc.modes = modeConfigs.toArray(new ModeConfig[0]); 537 538 Set<TopComponentGroupImpl> tcGroups = wmi.getTopComponentGroups(); 540 List<GroupConfig> groupConfigs = new ArrayList<GroupConfig>(tcGroups.size()); 541 for (Iterator<TopComponentGroupImpl> it = tcGroups.iterator(); it.hasNext(); ) { 542 groupConfigs.add(getConfigFromGroup(it.next())); 543 } 544 wmc.groups = groupConfigs.toArray(new GroupConfig[0]); 545 546 PersistenceManager pm = PersistenceManager.getDefault(); 547 TopComponent [] tcs = wmi.getRecentViewList(); 549 List<String > tcIdList = new ArrayList<String >(tcs.length); 550 for (int i = 0; i < tcs.length; i++) { 551 if (pm.isTopComponentPersistent(tcs[i])) { 552 String tc_id = WindowManager.getDefault().findTopComponentID(tcs[i]); 553 tc_id = PersistenceManager.escapeTcId4XmlContent(tc_id); 554 tcIdList.add(tc_id); 555 } 556 } 557 wmc.tcIdViewList = tcIdList.toArray(new String [tcIdList.size()]); 558 559 return wmc; 560 } 561 562 private ModeConfig getConfigFromMode(ModeImpl mode) { 563 PersistenceManager pm = PersistenceManager.getDefault(); 564 WindowManagerImpl wm = WindowManagerImpl.getInstance(); 565 ModeConfig modeCfg = new ModeConfig(); 566 modeCfg.name = mode.getName(); 567 if(DEBUG) { 568 debugLog(""); debugLog("mode name=" + modeCfg.name); } 571 modeCfg.state = mode.getState(); 572 if(DEBUG) { 573 debugLog("mode state=" + modeCfg.state); } 575 576 modeCfg.kind = mode.getKind(); 577 if(DEBUG) { 578 debugLog("mode kind=" + modeCfg.kind); } 580 if (wm instanceof WindowManagerImpl) { 581 modeCfg.side = wm.getCentral().getModeSide(mode); 582 if( null != modeCfg.side ) { 583 modeCfg.slideInSizes = wm.getCentral().getSlideInSizes( modeCfg.side ); 584 } 585 } 586 if(DEBUG) { 587 debugLog("mode side=" + modeCfg.side); } 589 590 modeCfg.constraints = mode.getConstraints(); 591 if(DEBUG) { 592 debugLog("mode constraints=" + Arrays.toString(modeCfg.constraints)); } 594 Rectangle relBounds = null; 597 if (relBounds != null) { 598 modeCfg.relativeBounds = relBounds; 599 } else { 600 modeCfg.bounds = mode.getBounds(); 601 if(DEBUG) { 602 debugLog("mode bounds=" + modeCfg.bounds); } 604 } 605 modeCfg.frameState = mode.getFrameState(); 606 if(DEBUG) { 607 debugLog("mode frame state=" + modeCfg.frameState); } 609 610 TopComponent selectedTC = mode.getSelectedTopComponent(); 611 if(selectedTC != null) { 612 if (pm.isTopComponentPersistent(selectedTC)) { 613 String tc_id = wm.findTopComponentID(selectedTC); 614 if(DEBUG) { 615 debugLog("selected tc=" + selectedTC.getName()); } 617 modeCfg.selectedTopComponentID = tc_id; 618 } 619 } 620 modeCfg.permanent = mode.isPermanent(); 621 if(DEBUG) { 622 debugLog("mode permanent=" + modeCfg.permanent); } 624 625 TopComponent prevSelectedTC = mode.getPreviousSelectedTopComponent(); 626 if(prevSelectedTC != null) { 627 if (pm.isTopComponentPersistent(prevSelectedTC)) { 628 String tc_id = wm.findTopComponentID(prevSelectedTC); 629 if(DEBUG) { 630 debugLog("previous selected tc=" + selectedTC.getName()); } 632 modeCfg.previousSelectedTopComponentID = tc_id; 633 } 634 } 635 636 List<TCRefConfig> tcRefCfgList = new ArrayList<TCRefConfig>(); 638 List<String > openedTcIDs = mode.getOpenedTopComponentsIDs(); 639 for(Iterator it = mode.getTopComponentsIDs().iterator(); it.hasNext(); ) { 640 String tcID = (String )it.next(); 641 642 boolean opened = openedTcIDs.contains(tcID); 643 TopComponent tc = wm.findTopComponent(tcID); 644 if(opened) { 645 if(tc == null || !pm.isTopComponentPersistent(tc)) { 646 continue; 647 } 648 } 649 650 String modeName = null; 652 int prevIndex = -1; 653 if (mode.getKind() == Constants.MODE_KIND_SLIDING || mode.getState() == Constants.MODE_STATE_SEPARATED) { 654 ModeImpl prev = wm.getPreviousModeForTopComponent(tcID, mode); 655 if (prev != null) { 656 modeName = prev.getName(); 657 prevIndex = wm.getPreviousIndexForTopComponent(tcID, mode); 658 } 659 } 660 661 if(DEBUG) { 662 debugLog("tc ID=" + tcID + " opened=" + opened); } 664 TCRefConfig tcRefCfg = new TCRefConfig(); 665 tcRefCfg.tc_id = tcID; 666 tcRefCfg.opened = opened; 667 tcRefCfg.previousMode = modeName; 668 tcRefCfg.previousIndex = prevIndex; 669 tcRefCfg.dockedInMaximizedMode = wm.isTopComponentDockedInMaximizedMode( tcID ); 670 tcRefCfg.dockedInDefaultMode = !wm.isTopComponentSlidedInDefaultMode( tcID ); 671 tcRefCfg.slidedInMaximized = wm.isTopComponentMaximizedWhenSlidedIn( tcID ); 672 tcRefCfgList.add(tcRefCfg); 673 } 674 675 modeCfg.tcRefConfigs = tcRefCfgList.toArray(new TCRefConfig[tcRefCfgList.size()]); 676 return modeCfg; 677 } 678 679 private GroupConfig getConfigFromGroup(TopComponentGroupImpl tcGroup) { 680 GroupConfig groupCfg = new GroupConfig(); 681 groupCfg.name = tcGroup.getName(); 682 groupCfg.opened = tcGroup.isOpened(); 683 if(DEBUG) { 684 debugLog(""); debugLog("group name=" + groupCfg.name); } 687 Set<String > openSet = tcGroup.getOpeningSetIDs(); 688 Set<String > closeSet = tcGroup.getClosingSetIDs(); 689 Set<String > wasOpenedSet = tcGroup.getGroupOpenedTopComponentsIDs(); 690 691 Map<String , TCGroupConfig> tcGroupCfgMap = new HashMap<String , TCGroupConfig>(); 692 693 for (String tcID: tcGroup.getTopComponentsIDs()) { 694 695 TCGroupConfig tcGroupCfg; 696 if (tcGroupCfgMap.containsKey(tcID)) { 697 tcGroupCfg = tcGroupCfgMap.get(tcID); 698 } else { 699 tcGroupCfg = new TCGroupConfig(); 700 tcGroupCfg.tc_id = tcID; 701 tcGroupCfgMap.put(tcID, tcGroupCfg); 702 } 703 704 tcGroupCfg.open = openSet.contains(tcID); 705 tcGroupCfg.close = closeSet.contains(tcID); 706 if(groupCfg.opened) { 707 tcGroupCfg.wasOpened = wasOpenedSet.contains(tcID); 708 } 709 if(DEBUG) { 710 debugLog("tc id=" + tcGroupCfg.tc_id + ", open=" + tcGroupCfg.open + ", close=" + tcGroupCfg.close + ", wasOpened=" + tcGroupCfg.wasOpened); } 715 } 716 717 groupCfg.tcGroupConfigs = tcGroupCfgMap.values().toArray(new TCGroupConfig[0]); 718 return groupCfg; 719 } 720 721 722 725 public synchronized void modeConfigAdded(ModeConfig modeConfig) { 726 if(DEBUG) { 727 debugLog("WMI.modeConfigAdded mo:" + modeConfig.name); } 729 ModeImpl mode = getModeFromConfig(modeConfig); 730 initModeFromConfig(mode, modeConfig); 731 } 732 733 736 public synchronized void modeConfigRemoved(String modeName) { 737 if(DEBUG) { 738 debugLog("WMI.modeConfigRemoved mo:" + modeName); } 740 ModeImpl mode = (ModeImpl)name2mode.remove(modeName); 741 if(mode != null) { 742 WindowManagerImpl.getInstance().removeMode(mode); 743 } else { 744 Logger.getLogger(PersistenceHandler.class.getName()).log(Level.WARNING, null, 745 new java.lang.NullPointerException ("Mode for name=" + 746 modeName + 747 " was not created")); } 749 } 750 751 757 public synchronized void topComponentRefConfigAdded 758 (String modeName, TCRefConfig tcRefConfig, String [] tcRefNames) { 759 if(DEBUG) { 760 debugLog("WMI.topComponentRefConfigAdded mo:" + modeName + " tcRef:" + tcRefConfig.tc_id); } 762 763 WindowManagerImpl wm = WindowManagerImpl.getInstance(); 764 wm.setTopComponentDockedInMaximizedMode( tcRefConfig.tc_id, tcRefConfig.dockedInMaximizedMode ); 765 wm.setTopComponentSlidedInDefaultMode( tcRefConfig.tc_id, !tcRefConfig.dockedInDefaultMode ); 766 wm.setTopComponentMaximizedWhenSlidedIn( tcRefConfig.tc_id, tcRefConfig.slidedInMaximized ); 767 768 TopComponent tc = getTopComponentForID(tcRefConfig.tc_id); 769 if(tc != null) { 770 ModeImpl mode = (ModeImpl)name2mode.get(modeName); 771 if(mode != null) { 772 if(tcRefConfig.opened) { 773 mode.addOpenedTopComponent(tc); 774 } else { 775 mode.addClosedTopComponent(tc); 776 } 777 } 778 } 779 } 780 781 784 public synchronized void topComponentRefConfigRemoved(String tc_id) { 785 if(DEBUG) { 786 debugLog("WMI.topComponentRefConfigRemoved tcRef:" + tc_id); } 788 789 WindowManagerImpl wm = WindowManagerImpl.getInstance(); 790 ModeImpl mode = wm.findModeForOpenedID(tc_id); 791 if(mode != null) { 792 TopComponent tc = getTopComponentForID(tc_id); 793 if(tc != null) { 794 mode.removeTopComponent(tc); 795 } 796 } else { 797 mode = wm.findModeForClosedID(tc_id); 798 if(mode != null) { 799 mode.removeClosedTopComponentID(tc_id); 800 } 801 } 802 } 803 804 807 public synchronized void groupConfigAdded(GroupConfig groupConfig) { 808 if(DEBUG) { 809 debugLog("WMI.groupConfigAdded group:" + groupConfig.name); } 811 createTopComponentGroupFromConfig(groupConfig); 812 } 813 814 817 public synchronized void groupConfigRemoved(String groupName) { 818 if(DEBUG) { 819 debugLog("WMI.groupConfigRemoved group:" + groupName); } 821 TopComponentGroupImpl group = (TopComponentGroupImpl)name2group.remove(groupName); 822 if(group != null) { 823 WindowManagerImpl.getInstance().removeTopComponentGroup(group); 824 } else { 825 Logger.getLogger(PersistenceHandler.class.getName()).log(Level.WARNING, null, 826 new java.lang.NullPointerException ("Null group for name=" + 827 groupName)); 828 } 829 } 830 831 835 public synchronized void topComponentGroupConfigAdded(String groupName, TCGroupConfig tcGroupConfig) { 836 if(DEBUG) { 837 debugLog("WMI.topComponentGroupConfigAdded group:" + groupName + " tcGroup:" + tcGroupConfig.tc_id); } 839 840 TopComponentGroupImpl group = (TopComponentGroupImpl)name2group.get(groupName); 841 if(group != null) { 842 group.addUnloadedTopComponent(tcGroupConfig.tc_id); 843 if(tcGroupConfig.open) { 844 group.addUnloadedOpeningTopComponent(tcGroupConfig.tc_id); 845 } 846 847 if(tcGroupConfig.close) { 848 group.addUnloadedClosingTopComponent(tcGroupConfig.tc_id); 849 } 850 } 851 } 852 853 857 public synchronized void topComponentGroupConfigRemoved(String groupName, String tc_id) { 858 if(DEBUG) { 859 debugLog("WMI.topComponentGroupConfigRemoved group:" + groupName + " tcGroup:" + tc_id); } 861 862 TopComponentGroupImpl group = (TopComponentGroupImpl)name2group.get(groupName); 863 if(group != null) { 864 group.removeUnloadedTopComponent(tc_id); 865 } 866 } 867 868 869 private static String dumpConfig (WindowManagerConfig wmc) { 870 StringBuffer buffer = new StringBuffer (); 871 buffer.append("\n-- wmc: [" + Integer.toHexString(System.identityHashCode(wmc)) + "]"); 872 buffer.append("\n-- JOINED --"); 873 buffer.append("\n-- x: " + wmc.xJoined); 874 buffer.append("\n-- y: " + wmc.yJoined); 875 buffer.append("\n-- width: " + wmc.widthJoined); 876 buffer.append("\n-- height: " + wmc.heightJoined); 877 buffer.append("\n-- relativeX: " + wmc.relativeXJoined); 878 buffer.append("\n-- relativeY: " + wmc.relativeYJoined); 879 buffer.append("\n-- relativeWidth: " + wmc.relativeWidthJoined); 880 buffer.append("\n-- relativeHeight: " + wmc.relativeHeightJoined); 881 buffer.append("\n-- centeredHorizontally: " + wmc.centeredHorizontallyJoined); 882 buffer.append("\n-- centeredVertically: " + wmc.centeredVerticallyJoined); 883 buffer.append("\n-- maximizeIfWidthBelowJoined: " + wmc.maximizeIfWidthBelowJoined); 884 buffer.append("\n-- maximizeIfHeightBelowJoined: " + wmc.maximizeIfHeightBelowJoined); 885 886 buffer.append("\n-- SEPARATED --"); 887 buffer.append("\n-- x: " + wmc.xSeparated); 888 buffer.append("\n-- y: " + wmc.ySeparated); 889 buffer.append("\n-- width: " + wmc.widthSeparated); 890 buffer.append("\n-- height: " + wmc.heightSeparated); 891 buffer.append("\n-- relativeX: " + wmc.relativeXSeparated); 892 buffer.append("\n-- relativeY: " + wmc.relativeYSeparated); 893 buffer.append("\n-- relativeWidth: " + wmc.relativeWidthSeparated); 894 buffer.append("\n-- relativeHeight: " + wmc.relativeHeightSeparated); 895 buffer.append("\n-- centeredHorizontally: " + wmc.centeredHorizontallySeparated); 896 buffer.append("\n-- centeredVertically: " + wmc.centeredVerticallySeparated); 897 898 buffer.append("\n-- editorAreaState: " + wmc.editorAreaState); 899 if(wmc.editorAreaConstraints != null) { 900 for (int i = 0; i < wmc.editorAreaConstraints.length; i++) { 901 buffer.append("\n-- co[" + i + "]: " + wmc.editorAreaConstraints[i]); 902 } 903 } 904 buffer.append("\n-- editorAreaBounds: " + wmc.editorAreaBounds); 905 buffer.append("\n-- editorAreaRelativeBounds: " + wmc.editorAreaRelativeBounds); 906 907 buffer.append("\n-- screenSize: " + wmc.screenSize); 908 buffer.append("\n-- activeModeName: " + wmc.activeModeName); 909 buffer.append("\n-- editorMaximizedModeName: " + wmc.editorMaximizedModeName); 910 buffer.append("\n-- viewMaximizedModeName: " + wmc.viewMaximizedModeName); 911 buffer.append("\n-- toolbarconfig: " + wmc.toolbarConfiguration); 912 buffer.append("\n-- modes: " + Arrays.toString(wmc.modes) + " size " + (wmc.modes == null ? -1 : wmc.modes.length)); 913 for (int i = 0; i < wmc.modes.length; i++) { 914 ModeConfig mc = wmc.modes[i]; 915 buffer.append("\n-- --"); 916 buffer.append("\n-- -- mode[" + i + "]: " + mc.name); 917 buffer.append("\n-- -- state: " + mc.state + " " 918 + ((mc.state == Constants.MODE_STATE_JOINED) ? "joined" : "separated")); 919 if (mc.constraints != null) { 920 for (int j = 0; j < mc.constraints.length; j++) { 921 buffer.append("\n-- -- co[" + j + "]: " + mc.constraints[j]); 922 } 923 } 924 buffer.append("\n-- -- kind: " + mc.kind + " " 925 + ((mc.kind == Constants.MODE_KIND_EDITOR) ? "editor" : "view")); 926 buffer.append("\n-- -- bounds: " + mc.bounds); 927 buffer.append("\n-- -- relativeBounds: " + mc.relativeBounds); 928 buffer.append("\n-- -- state: " + mc.frameState); 929 buffer.append("\n-- -- active-tc: " + mc.selectedTopComponentID); 930 buffer.append("\n-- -- permanent: " + mc.permanent); 931 if (mc.tcRefConfigs != null) { 932 for (int k = 0; k < mc.tcRefConfigs.length; k++) { 933 TCRefConfig tcRefCfg = mc.tcRefConfigs[k]; 934 buffer.append("\n++ ++ ++ tcRef[" + k + "]: " + tcRefCfg.tc_id); 935 buffer.append("\n++ ++ ++ opened: " + tcRefCfg.opened); 936 } 937 } 938 } 939 buffer.append("\n-- groups: " + Arrays.toString(wmc.groups) + " size " + (wmc.groups == null ? -1 : wmc.groups.length)); 940 for (int i = 0; i < wmc.groups.length; i++) { 941 GroupConfig sc = wmc.groups[i]; 942 buffer.append("\n-- --"); 943 buffer.append("\n-- -- group[" + i + "]: " + sc.name); 944 if (sc.tcGroupConfigs != null) { 945 for (int k = 0; k < sc.tcGroupConfigs.length; k++) { 946 TCGroupConfig tcGroupCfg = sc.tcGroupConfigs[k]; 947 buffer.append("\n++ ++ ++ tcGroup[" + k + "]: " + tcGroupCfg.tc_id); 948 buffer.append("\n++ ++ ++ open: " + tcGroupCfg.open); 949 buffer.append("\n++ ++ ++ close: " + tcGroupCfg.close); 950 } 951 } 952 } 953 954 return buffer.toString(); 955 } 956 957 private static void debugLog(String message) { 958 Debug.log(PersistenceHandler.class, message); 959 } 960 961 private static Rectangle computeBounds(boolean centeredHorizontaly, boolean centeredVerticaly, 962 int x, int y, int width, int height, float relativeX, float relativeY, float relativeWidth, float relativeHeight) { 963 Rectangle bounds; 964 if(width > 0 && height > 0) { 965 bounds = new Rectangle (x, y, width, height); 967 Rectangle screen = Utilities.getUsableScreenBounds(); 969 int xlimit = screen.x + screen.width - 20; int ylimit = screen.y + screen.height - 20; while (bounds.x > xlimit) { 973 bounds.x = Math.max(bounds.x - screen.width, screen.x); 974 } 975 while (bounds.y > ylimit) { 976 bounds.y = Math.max(bounds.y - ylimit, screen.y); 977 } 978 } else if(relativeWidth > 0F && relativeHeight > 0F) { 980 Rectangle screen = Utilities.getUsableScreenBounds(); 982 bounds = new Rectangle ((int)(screen.width * relativeX), (int)(screen.height * relativeY), 983 (int)(screen.width * relativeWidth), (int)(screen.height * relativeHeight)); 984 } else { 985 return null; 986 } 987 if(centeredHorizontaly || centeredVerticaly) { 989 Rectangle centered = Utilities.findCenterBounds(new Dimension (bounds.width, bounds.height)); 990 if(centeredHorizontaly) { 991 bounds.x = centered.x; 992 } 993 if(centeredVerticaly) { 994 bounds.y = centered.y; 995 } 996 } 997 998 return bounds; 999 } 1000 1001} 1002 | Popular Tags |