KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > core > windows > PersistenceHandler


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20
21 package org.netbeans.core.windows;
22
23
24
25 import java.awt.Dimension JavaDoc;
26 import java.awt.Frame JavaDoc;
27 import java.awt.Rectangle JavaDoc;
28 import java.awt.Toolkit JavaDoc;
29 import java.io.IOException JavaDoc;
30 import java.util.*;
31 import java.util.logging.Level JavaDoc;
32 import java.util.logging.Logger JavaDoc;
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 /**
44  * Class which handles loading, saving of window system and persistence events.
45  *
46  * @author Peter Zavadsky, Marek Slama
47  */

48 final public class PersistenceHandler implements PersistenceObserver {
49
50     // Persistence data
51
/** Maps mode config name to mode instance. */
52     private final Map<String JavaDoc, ModeImpl> name2mode = new WeakHashMap<String JavaDoc, ModeImpl>(10);
53     /** Maps group config name to group instance. */
54     private final Map<String JavaDoc, TopComponentGroupImpl> name2group = new WeakHashMap<String JavaDoc, TopComponentGroupImpl>(10);
55
56     private static PersistenceHandler defaultInstance;
57
58     /** Debugging flag. */
59     private static final boolean DEBUG = Debug.isLoggable(PersistenceHandler.class);
60     
61     
62     /** Creates a new instance of PersistenceHanlder */
63     private PersistenceHandler() {
64     }
65
66     /**
67      * Clears cached data.
68      */

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     // XXX helper method
83
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"); // NOI18N
95
}
96         
97         WindowManagerConfig wmc = null;
98         try {
99             wmc = PersistenceManager.getDefault().loadWindowSystem();
100         } catch (IOException JavaDoc exc) {
101             // Serious persistence problem -> try to reset
102
Exceptions.attachLocalizedMessage(exc, "Cannot load window system persistent data, user directory content is broken. Resetting to default layout..."); //NOI18N
103
Logger.getLogger(PersistenceHandler.class.getName()).log(Level.WARNING, null, exc); // NOI18N
104
// try to delete local winsys data and try once more
105
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 JavaDoc ioE) {
112                     Exceptions.attachLocalizedMessage(ioE, "Cannot load even default layout, using internally predefined configuration."); //NOI18N
113
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."); // NOI18N
118
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             //No recent view list is saved, fill it by opened TopComponents
137
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                     //Only opened
142
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         // First create empty modes.
162
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         // Then fill them with TopComponents.
182
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             // Set selected TopComponent.
189
if(mc.selectedTopComponentID != null) {
190                 mode.setUnloadedSelectedTopComponent(mc.selectedTopComponentID);
191             }
192             if(mc.previousSelectedTopComponentID != null) {
193                 mode.setUnloadedPreviousSelectedTopComponent(mc.previousSelectedTopComponentID);
194             }
195         }
196         
197         //Initialize top component groups
198
for (int i = 0; i < wmc.groups.length; i++) {
199             GroupConfig groupCfg = wmc.groups[i];
200             createTopComponentGroupFromConfig(groupCfg);
201         }
202
203         // XXX #37188, 40237 Hot fix for the problem with initing active
204
// component (even it is not on screen yet).
205
if(activeMode != null) {
206             TopComponent active = activeMode.getSelectedTopComponent();
207             if(active != null) {
208                 WindowManagerImpl.getInstance().specialPersistenceCompShow(active);
209             }
210         }
211         // active mode can be null, Active mode info is stored in winsys config (system layer) and modes in
212
// project layer, that can cause out of synch state when switching projects...
213
// setting null is however considered a valid state.
214
wm.setActiveMode(activeMode);
215         wm.setEditorMaximizedMode(editorMaximizedMode);
216         wm.setViewMaximizedMode(viewMaximizedMode);
217
218         Rectangle JavaDoc 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         // PENDING else { ...some default value?
233

234         Rectangle JavaDoc 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         // PENDING else { ...some default value?
249

250         wm.setMainWindowFrameStateJoined(wmc.mainWindowFrameStateJoined);
251         wm.setMainWindowFrameStateSeparated(wmc.mainWindowFrameStateSeparated);
252
253         Rectangle JavaDoc absBounds = wmc.editorAreaBounds == null ? new Rectangle JavaDoc() : wmc.editorAreaBounds;
254         Rectangle JavaDoc relBounds = wmc.editorAreaRelativeBounds == null ? new Rectangle JavaDoc() : wmc.editorAreaRelativeBounds;
255         Rectangle JavaDoc 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     /** Implements <code>NbTopManager.WindowSystem</code> interface method. */
271     public synchronized void save() {
272         if(DEBUG) {
273             debugLog("## PersistenceHandler.save"); // NOI18N
274
}
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(""); // NOI18N
295
debugLog("Creating mode name=\"" + mc.name + "\""); // NOI8N
296
}
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     /**
312      * find the the previous mode for tc if exists and set it in the model..
313      */

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=\"" // NOI18N
319
+ tcRefConfig.tc_id + "\", \topened=" + tcRefConfig.opened); // NOI18N
320
}
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 JavaDoc("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=\"" // NOI18N
351
+ tcRefConfig.tc_id + "\", \topened=" + tcRefConfig.opened); // NOI18N
352
}
353
354             // PENDING
355
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         // PENDING Refine the unneded computing.
369
Rectangle JavaDoc absBounds = mc.bounds == null ? new Rectangle JavaDoc() : mc.bounds;
370         Rectangle JavaDoc relBounds = mc.relativeBounds == null ? new Rectangle JavaDoc() : mc.relativeBounds;
371         Rectangle JavaDoc 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 JavaDoc tc_id) {
387         if(tc_id == null || "".equals(tc_id)) {
388             return null;
389         }
390         
391 // long start = System.currentTimeMillis();
392
TopComponent tc = PersistenceManager.getDefault().getTopComponentForID(tc_id);
393 // if(DEBUG) {
394
// debugLog("***Getting TopComponent for ID=" + tc_id + " in " + (System.currentTimeMillis() - start) + " ms"); // NOI18N
395
// }
396

397         return tc;
398     }
399     
400     private TopComponentGroupImpl createTopComponentGroupFromConfig(GroupConfig groupCfg) {
401         if(DEBUG) {
402             debugLog(""); // NOI18N
403
debugLog("Creating group name=\"" + groupCfg.name + "\" \t[opened=" + groupCfg.opened + "]"); // NOI18N
404
}
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=\"" // NOI18N
414
+ tcGroupCfg.tc_id + "\", \topen=" + tcGroupCfg.open + ", \tclose=" + tcGroupCfg.close
415                     + ", \twasOpened=" + tcGroupCfg.wasOpened); // NOI18N
416
}
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             // Handle also wasOpened flag.
428
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 JavaDoc joinedBounds = wmi.getMainWindowBoundsJoined();
448         if(DEBUG) {
449             debugLog("joinedBouds=" + joinedBounds); // NOI18N
450
}
451         wmc.xJoined = joinedBounds.x;
452         wmc.yJoined = joinedBounds.y;
453         wmc.widthJoined = joinedBounds.width;
454         wmc.heightJoined = joinedBounds.height;
455         Rectangle JavaDoc separatedBounds = wmi.getMainWindowBoundsSeparated();
456         if(DEBUG) {
457             debugLog("separatedBounds=" + separatedBounds); // NOI18N
458
}
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             // #46646 - don't save iconified state
467
//mkleint - actually shoudn't we ignore the maximized states as well?
468
wmc.mainWindowFrameStateJoined = Frame.NORMAL;
469         }
470         if(DEBUG) {
471             debugLog("mainWindowFrameStateJoined=" + wmc.mainWindowFrameStateJoined); // NOI18N
472
}
473         wmc.mainWindowFrameStateSeparated = wmi.getMainWindowFrameStateSeparated();
474         if (wmc.mainWindowFrameStateSeparated == Frame.ICONIFIED) {
475             // #46646 - don't save iconified state
476
wmc.mainWindowFrameStateSeparated = Frame.NORMAL;
477         }
478         if(DEBUG) {
479             debugLog("mainWindowFrameStateSeparated=" + wmc.mainWindowFrameStateSeparated); // NOI18N
480
}
481
482         wmc.editorAreaState = wmi.getEditorAreaState();
483         if(DEBUG) {
484             debugLog("editorAreaState=" + wmc.editorAreaState); // NOI18N
485
}
486         wmc.editorAreaBounds = wmi.getEditorAreaBounds();
487         if(DEBUG) {
488             debugLog("editorAreaBounds=" + wmc.editorAreaBounds); // NOI18N
489
}
490         wmc.editorAreaConstraints = wmi.getEditorAreaConstraints();
491         if(DEBUG) {
492             debugLog("editorAreaConstraints=" + Arrays.toString(wmc.editorAreaConstraints)); // NOI18N
493
}
494         wmc.editorAreaFrameState = wmi.getEditorAreaFrameState();
495         if(DEBUG) {
496             debugLog("editorAreaFrameState=" + wmc.editorAreaFrameState); // NOI18N
497
}
498         wmc.screenSize = Toolkit.getDefaultToolkit().getScreenSize();
499         
500         ModeImpl mo = wmi.getActiveMode();
501         if(DEBUG) {
502             debugLog("active mode=" + mo); // NOI18N
503
}
504         if (mo != null) {
505             wmc.activeModeName = mo.getName();
506         }
507         
508         mo = wmi.getEditorMaximizedMode();
509         if(DEBUG) {
510             debugLog("editor maximized mode=" + mo); // NOI18N
511
}
512         if (mo != null) {
513             wmc.editorMaximizedModeName = mo.getName();
514         }
515         
516         mo = wmi.getViewMaximizedMode();
517         if(DEBUG) {
518             debugLog("view maximized mode=" + mo); // NOI18N
519
}
520         if (mo != null) {
521             wmc.viewMaximizedModeName = mo.getName();
522         }
523         
524         wmc.toolbarConfiguration = wmi.getToolbarConfigName();
525         if(DEBUG) {
526             debugLog("toolbarConfiguration=" + wmc.toolbarConfiguration); // NOI18N
527
}
528         
529         // Modes.
530
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         // TopComponent groups.
539
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         //RecentViewList
548
TopComponent [] tcs = wmi.getRecentViewList();
549         List<String JavaDoc> tcIdList = new ArrayList<String JavaDoc>(tcs.length);
550         for (int i = 0; i < tcs.length; i++) {
551             if (pm.isTopComponentPersistent(tcs[i])) {
552                 String JavaDoc 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 JavaDoc [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(""); // NOI18N
569
debugLog("mode name=" + modeCfg.name); // NOI18N
570
}
571         modeCfg.state = mode.getState();
572         if(DEBUG) {
573             debugLog("mode state=" + modeCfg.state); // NOI18N
574
}
575         
576         modeCfg.kind = mode.getKind();
577         if(DEBUG) {
578             debugLog("mode kind=" + modeCfg.kind); // NOI18N
579
}
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); // NOI18N
588
}
589         
590         modeCfg.constraints = mode.getConstraints();
591         if(DEBUG) {
592             debugLog("mode constraints=" + Arrays.toString(modeCfg.constraints)); // NOI18N
593
}
594         // PENDING Whether to save relative or absolute bounds.
595
// In case of relative, they would need to be computed.
596
Rectangle JavaDoc 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); // NOI18N
603
}
604         }
605         modeCfg.frameState = mode.getFrameState();
606         if(DEBUG) {
607             debugLog("mode frame state=" + modeCfg.frameState); // NOI18N
608
}
609         
610         TopComponent selectedTC = mode.getSelectedTopComponent();
611         if(selectedTC != null) {
612             if (pm.isTopComponentPersistent(selectedTC)) {
613                 String JavaDoc tc_id = wm.findTopComponentID(selectedTC);
614                 if(DEBUG) {
615                     debugLog("selected tc=" + selectedTC.getName()); // NOI18N
616
}
617                 modeCfg.selectedTopComponentID = tc_id;
618             }
619         }
620         modeCfg.permanent = mode.isPermanent();
621         if(DEBUG) {
622             debugLog("mode permanent=" + modeCfg.permanent); // NOI18N
623
}
624         
625         TopComponent prevSelectedTC = mode.getPreviousSelectedTopComponent();
626         if(prevSelectedTC != null) {
627             if (pm.isTopComponentPersistent(prevSelectedTC)) {
628                 String JavaDoc tc_id = wm.findTopComponentID(prevSelectedTC);
629                 if(DEBUG) {
630                     debugLog("previous selected tc=" + selectedTC.getName()); // NOI18N
631
}
632                 modeCfg.previousSelectedTopComponentID = tc_id;
633             }
634         }
635         
636         // TopComponents:
637
List<TCRefConfig> tcRefCfgList = new ArrayList<TCRefConfig>();
638         List<String JavaDoc> openedTcIDs = mode.getOpenedTopComponentsIDs();
639         for(Iterator it = mode.getTopComponentsIDs().iterator(); it.hasNext(); ) {
640             String JavaDoc tcID = (String JavaDoc)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             // #45981: save previous mode even for closed tcs
651
String JavaDoc 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); // NOI18N
663
}
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(""); // NOI18N
685
debugLog("group name=" + groupCfg.name); // NOI18N
686
}
687         Set<String JavaDoc> openSet = tcGroup.getOpeningSetIDs();
688         Set<String JavaDoc> closeSet = tcGroup.getClosingSetIDs();
689         Set<String JavaDoc> wasOpenedSet = tcGroup.getGroupOpenedTopComponentsIDs();
690         
691         Map<String JavaDoc, TCGroupConfig> tcGroupCfgMap = new HashMap<String JavaDoc, TCGroupConfig>();
692         
693         for (String JavaDoc 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 // NOI18N
711
+ ", open=" + tcGroupCfg.open // NOI18N
712
+ ", close=" + tcGroupCfg.close // NOI18N
713
+ ", wasOpened=" + tcGroupCfg.wasOpened); // NOI18N
714
}
715         }
716         
717         groupCfg.tcGroupConfigs = tcGroupCfgMap.values().toArray(new TCGroupConfig[0]);
718         return groupCfg;
719     }
720     
721     
722     /** Handles adding mode to model.
723      * @param modeConfig configuration data of added mode
724      */

725     public synchronized void modeConfigAdded(ModeConfig modeConfig) {
726         if(DEBUG) {
727             debugLog("WMI.modeConfigAdded mo:" + modeConfig.name); // NOI18N
728
}
729         ModeImpl mode = getModeFromConfig(modeConfig);
730         initModeFromConfig(mode, modeConfig);
731     }
732     
733     /** Handles removing mode from model.
734      * @param modeName unique name of removed mode
735      */

736     public synchronized void modeConfigRemoved(String JavaDoc modeName) {
737         if(DEBUG) {
738             debugLog("WMI.modeConfigRemoved mo:" + modeName); // NOI18N
739
}
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 JavaDoc("Mode for name=" +
746                                                                  modeName +
747                                                                  " was not created")); // NOI18N
748
}
749     }
750     
751     /** Handles adding tcRef to model.
752      * @param modeName unique name of parent mode.
753      * @param tcRefConfig configuration data of added tcRef
754      * @param tcRefNames array of tcIds to pass ordering of new tcRef,
755      * if there is no ordering defined tcRef is appended to end of array
756      */

757     public synchronized void topComponentRefConfigAdded
758     (String JavaDoc modeName, TCRefConfig tcRefConfig, String JavaDoc [] tcRefNames) {
759         if(DEBUG) {
760             debugLog("WMI.topComponentRefConfigAdded mo:" + modeName + " tcRef:" + tcRefConfig.tc_id); // NOI18N
761
}
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     /** Handles removing tcRef from model.
782      * @param tc_id unique id of removed tcRef
783      */

784     public synchronized void topComponentRefConfigRemoved(String JavaDoc tc_id) {
785         if(DEBUG) {
786             debugLog("WMI.topComponentRefConfigRemoved tcRef:" + tc_id); // NOI18N
787
}
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     /** Handles adding group to model.
805      * @param groupConfig configuration data of added group
806      */

807     public synchronized void groupConfigAdded(GroupConfig groupConfig) {
808         if(DEBUG) {
809             debugLog("WMI.groupConfigAdded group:" + groupConfig.name); // NOI18N
810
}
811         createTopComponentGroupFromConfig(groupConfig);
812     }
813     
814     /** Handles removing group from model.
815      * @param groupName unique name of removed group
816      */

817     public synchronized void groupConfigRemoved(String JavaDoc groupName) {
818         if(DEBUG) {
819             debugLog("WMI.groupConfigRemoved group:" + groupName); // NOI18N
820
}
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 JavaDoc("Null group for name=" +
827                                                                  groupName));
828         }
829     }
830     
831     /** Handles adding tcGroup to model.
832      * @param groupName unique name of parent group
833      * @param tcGroupConfig configuration data of added tcGroup
834      */

835     public synchronized void topComponentGroupConfigAdded(String JavaDoc groupName, TCGroupConfig tcGroupConfig) {
836         if(DEBUG) {
837             debugLog("WMI.topComponentGroupConfigAdded group:" + groupName + " tcGroup:" + tcGroupConfig.tc_id); // NOI18N
838
}
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     /** Handles removing tcGroup from model.
854      * @param groupName unique name of parent group
855      * @param tc_id unique id of removed tcGroup
856      */

857     public synchronized void topComponentGroupConfigRemoved(String JavaDoc groupName, String JavaDoc tc_id) {
858         if(DEBUG) {
859             debugLog("WMI.topComponentGroupConfigRemoved group:" + groupName + " tcGroup:" + tc_id); // NOI18N
860
}
861         
862         TopComponentGroupImpl group = (TopComponentGroupImpl)name2group.get(groupName);
863         if(group != null) {
864             group.removeUnloadedTopComponent(tc_id);
865         }
866     }
867     
868     /** Dump window manager configuration data to standard output. */
869     private static String JavaDoc dumpConfig (WindowManagerConfig wmc) {
870         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
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 JavaDoc message) {
958         Debug.log(PersistenceHandler.class, message);
959     }
960     
961     private static Rectangle JavaDoc computeBounds(boolean centeredHorizontaly, boolean centeredVerticaly,
962     int x, int y, int width, int height, float relativeX, float relativeY, float relativeWidth, float relativeHeight) {
963         Rectangle JavaDoc bounds;
964         if(width > 0 && height > 0) {
965             // From absoute values.
966
bounds = new Rectangle JavaDoc(x, y, width, height);
967             // #33288 fix start- when screen resolution changes, some windows may get completely out of the screen.
968
Rectangle JavaDoc screen = Utilities.getUsableScreenBounds();
969             int xlimit = screen.x + screen.width - 20; // 20 = let's have some buffer area..
970
int ylimit = screen.y + screen.height - 20; // 20 = let's have some buffer area..
971
// will make sure that the out-of-screen windows get thrown in.
972
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             // #33288 fix end
979
} else if(relativeWidth > 0F && relativeHeight > 0F) {
980             // From relative values.
981
Rectangle JavaDoc screen = Utilities.getUsableScreenBounds();
982             bounds = new Rectangle JavaDoc((int)(screen.width * relativeX), (int)(screen.height * relativeY),
983                         (int)(screen.width * relativeWidth), (int)(screen.height * relativeHeight));
984         } else {
985             return null;
986         }
987         // Center the bounds if necessary.
988
if(centeredHorizontaly || centeredVerticaly) {
989             Rectangle JavaDoc centered = Utilities.findCenterBounds(new Dimension JavaDoc(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