KickJava   Java API By Example, From Geeks To Geeks.

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


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 package org.netbeans.core.windows;
21
22
23 import java.awt.Component JavaDoc;
24 import java.awt.Dimension JavaDoc;
25 import java.awt.Frame JavaDoc;
26 import java.awt.Point JavaDoc;
27 import java.awt.Rectangle JavaDoc;
28 import java.util.ArrayList JavaDoc;
29 import java.util.Arrays JavaDoc;
30 import java.util.Collections JavaDoc;
31 import java.util.HashSet JavaDoc;
32 import java.util.Iterator JavaDoc;
33 import java.util.List JavaDoc;
34 import java.util.Map JavaDoc;
35 import java.util.Set JavaDoc;
36 import javax.swing.SwingUtilities JavaDoc;
37
38 import org.netbeans.core.windows.model.DockingStatus;
39 import org.netbeans.core.windows.model.Model;
40 import org.netbeans.core.windows.model.ModelElement;
41 import org.netbeans.core.windows.model.ModelFactory;
42 import org.netbeans.core.windows.persistence.PersistenceManager;
43 import org.netbeans.core.windows.view.ControllerHandler;
44 import org.netbeans.core.windows.view.View;
45 import org.openide.windows.Mode;
46 import org.openide.windows.TopComponent;
47 import org.openide.windows.WindowManager;
48
49
50
51 /**
52  * This class is a central unit of window system. It controls communication
53  * flows to model, to view, from controller and from API calls.
54  *
55  * @author Peter Zavadsky
56  */

57 final class Central implements ControllerHandler {
58     
59     /** Model of window system. */
60     private final Model model = ModelFactory.createWindowSystemModel();
61     
62     /** Helper class for managing requests to view. */
63     private final ViewRequestor viewRequestor = new ViewRequestor(this);
64     
65     /** Constructor. */
66     public Central() {
67     }
68
69     
70     public void topComponentRequestAttention (ModeImpl mode, TopComponent tc) {
71         String JavaDoc modeName = getModeName(mode);
72         viewRequestor.scheduleRequest (
73             new ViewRequest(modeName, View.TOPCOMPONENT_REQUEST_ATTENTION, tc, tc));
74     }
75     
76     public void topComponentCancelRequestAttention (ModeImpl mode, TopComponent tc) {
77         String JavaDoc modeName = getModeName(mode);
78         viewRequestor.scheduleRequest (
79             new ViewRequest(modeName, View.TOPCOMPONENT_CANCEL_REQUEST_ATTENTION, tc, tc));
80     }
81     
82     /////////////////////
83
// Mutators >>
84
/** Sets visible or invisible window system and requests view accordingly. */
85     public void setVisible(boolean visible) {
86         if(isVisible() == visible) {
87             return;
88         }
89         
90         model.setVisible(visible);
91         
92         viewRequestor.scheduleRequest(
93             new ViewRequest(null, View.CHANGE_VISIBILITY_CHANGED, null, Boolean.valueOf(visible)));
94     }
95     
96     /** Sets main window bounds (joined[tiled] state) into model and requests view (if needed). */
97     public void setMainWindowBoundsJoined(Rectangle JavaDoc mainWindowBoundsJoined) {
98         if(mainWindowBoundsJoined == null) {
99             return;
100         }
101         
102         Rectangle JavaDoc old = getMainWindowBoundsJoined();
103         if(old.equals(mainWindowBoundsJoined)) {
104             return;
105         }
106         
107         model.setMainWindowBoundsJoined(mainWindowBoundsJoined);
108         
109         if(isVisible()) {
110             viewRequestor.scheduleRequest(
111                 new ViewRequest(null, View.CHANGE_MAIN_WINDOW_BOUNDS_JOINED_CHANGED,
112                     old, mainWindowBoundsJoined));
113         }
114     }
115     
116     /** Sets main window bounds (separated state) into model and requests view (if needed). */
117     public void setMainWindowBoundsSeparated(Rectangle JavaDoc mainWindowBoundsSeparated) {
118         if(mainWindowBoundsSeparated == null) {
119             return;
120         }
121         
122         Rectangle JavaDoc old = getMainWindowBoundsSeparated();
123         if(old.equals(mainWindowBoundsSeparated)) {
124             return;
125         }
126         
127         model.setMainWindowBoundsSeparated(mainWindowBoundsSeparated);
128         
129         if(isVisible()) {
130             viewRequestor.scheduleRequest(
131                 new ViewRequest(null, View.CHANGE_MAIN_WINDOW_BOUNDS_SEPARATED_CHANGED,
132                     old, mainWindowBoundsSeparated));
133         }
134     }
135     
136     public void setMainWindowFrameStateJoined(int frameState) {
137         int old = getMainWindowFrameStateJoined();
138         if(old == frameState) {
139             return;
140         }
141         
142         model.setMainWindowFrameStateJoined(frameState);
143         
144         if(isVisible()) {
145             viewRequestor.scheduleRequest(new ViewRequest(null, View.CHANGE_MAIN_WINDOW_FRAME_STATE_JOINED_CHANGED,
146                 Integer.valueOf(old), Integer.valueOf(frameState)));
147         }
148     }
149     
150     public void setMainWindowFrameStateSeparated(int frameState) {
151         int old = getMainWindowFrameStateSeparated();
152         if(old == frameState) {
153             return;
154         }
155         
156         model.setMainWindowFrameStateSeparated(frameState);
157         
158         if(isVisible()) {
159             viewRequestor.scheduleRequest(new ViewRequest(null, View.CHANGE_MAIN_WINDOW_FRAME_STATE_SEPARATED_CHANGED,
160                 Integer.valueOf(old), Integer.valueOf(frameState)));
161         }
162     }
163     
164     /** Sets active mode into model and requests view (if needed). */
165     public void setActiveMode(final ModeImpl activeMode) {
166         final ModeImpl old = getActiveMode();
167         if(activeMode == old) {
168             // kind of workaround to the scenario when a window slides out automatically
169
// and user clicks in the currently active mode, not allow to exit in such case and fire changes to
170
// force the slided-out window to disappear.
171
ModeImpl impl = model.getSlidingMode(Constants.BOTTOM);
172             boolean bottom = (impl == null || impl.getSelectedTopComponent() == null);
173             impl = model.getSlidingMode(Constants.LEFT);
174             boolean left = (impl == null || impl.getSelectedTopComponent() == null);
175             impl = model.getSlidingMode(Constants.RIGHT);
176             boolean right = (impl == null || impl.getSelectedTopComponent() == null);
177             if (bottom && left && right) {
178                 return;
179             }
180         }
181         
182         model.setActiveMode(activeMode);
183         
184         if(isVisible()) {
185             viewRequestor.scheduleRequest(
186                 new ViewRequest(null, View.CHANGE_ACTIVE_MODE_CHANGED,
187                     old, activeMode));
188         }
189
190         
191         
192         WindowManagerImpl.getInstance().doFirePropertyChange(
193             WindowManagerImpl.PROP_ACTIVE_MODE, old, activeMode);
194         
195         // Notify registry.
196
// active mode can be null, Active mode info is stored in winsys config (system layer) and modes in
197
// project layer, that can cause out of synch state when switching projects.
198
// all subsequent calls should handle the null value correctly.
199
if (activeMode != null) {
200             WindowManagerImpl.notifyRegistryTopComponentActivated(
201             activeMode.getSelectedTopComponent());
202         } else {
203             WindowManagerImpl.notifyRegistryTopComponentActivated(null);
204         }
205     }
206
207     /** Sets editor area bounds into model and requests view (if needed). */
208     public void setEditorAreaBounds(Rectangle JavaDoc editorAreaBounds) {
209         if(editorAreaBounds == null) {
210             return;
211         }
212         
213         Rectangle JavaDoc old = getEditorAreaBounds();
214         if(old.equals(editorAreaBounds)) {
215             return;
216         }
217         
218         model.setEditorAreaBounds(editorAreaBounds);
219         
220         if(isVisible()) {
221             viewRequestor.scheduleRequest(
222                 new ViewRequest(null, View.CHANGE_EDITOR_AREA_BOUNDS_CHANGED,
223                             old, editorAreaBounds));
224         }
225     }
226
227     /** Sets editor area constraints into model and requests view (if needed). */
228     public void setEditorAreaConstraints(SplitConstraint[] editorAreaConstraints) {
229         SplitConstraint[] old = getEditorAreaConstraints();
230         if(Arrays.equals(old, editorAreaConstraints)) {
231             return;
232         }
233         
234         model.setEditorAreaConstraints(editorAreaConstraints);
235
236         if(isVisible()) {
237             viewRequestor.scheduleRequest(
238                 new ViewRequest(null, View.CHANGE_EDITOR_AREA_CONSTRAINTS_CHANGED,
239                                 old, editorAreaConstraints));
240         }
241     }
242
243     /** Sets editor area state into model and requests view (if needed). */
244     public void setEditorAreaState(int editorAreaState) {
245         int old = getEditorAreaState();
246         if(editorAreaState == old) {
247             return;
248         }
249         
250         int requiredState = editorAreaState == Constants.EDITOR_AREA_JOINED
251                                                 ? Constants.MODE_STATE_JOINED
252                                                 : Constants.MODE_STATE_SEPARATED;
253                                                 
254         for(Iterator JavaDoc it = getModes().iterator(); it.hasNext(); ) {
255             ModeImpl mode = (ModeImpl)it.next();
256             if(mode.getKind() == Constants.MODE_KIND_VIEW
257             && mode.getState() != requiredState) {
258                 model.setModeState(mode, requiredState);
259                 // Adjust bounds if necessary.
260
if(editorAreaState == Constants.EDITOR_AREA_SEPARATED) {
261                     Rectangle JavaDoc bounds = model.getModeBounds(mode);
262                     if(bounds.isEmpty()) {
263                         model.setModeBounds(mode, model.getModeBoundsSeparatedHelp(mode));
264                     }
265                 }
266             }
267             // when switching to SDI, undock sliding windows
268
// #51992 -start
269
if (mode.getKind() == Constants.MODE_KIND_SLIDING && editorAreaState == Constants.EDITOR_AREA_SEPARATED) {
270                 TopComponent[] tcs = mode.getTopComponents();
271                 for (int i = 0; i < tcs.length;i++) {
272                     String JavaDoc tcID = WindowManagerImpl.getInstance().findTopComponentID(tcs[i]);
273                     ModeImpl targetMode = model.getModeTopComponentPreviousMode(mode, tcID);
274                     if ((targetMode == null) || !model.getModes().contains(targetMode)) {
275                         SplitConstraint[] constraints = model.getModeTopComponentPreviousConstraints(mode, tcID);
276                         constraints = constraints == null ? new SplitConstraint[0] : constraints;
277                         // create mode to dock topcomponent back into
278
targetMode = WindowManagerImpl.getInstance().createModeImpl(
279                             ModeImpl.getUnusedModeName(), Constants.MODE_KIND_VIEW, false);
280                         model.setModeState(targetMode, requiredState);
281                         model.addMode(targetMode, constraints);
282                     }
283                     moveTopComponentsIntoMode(targetMode, new TopComponent[] { tcs[i] } );
284                 }
285             }
286             // #51992 - end
287
}
288                                                 
289         if(editorAreaState == Constants.EDITOR_AREA_SEPARATED) {
290             Rectangle JavaDoc editorAreaBounds = model.getEditorAreaBounds();
291             // Adjust bounds if necessary.
292
if(editorAreaBounds.isEmpty()) {
293                 model.setEditorAreaBounds(model.getEditorAreaBoundsHelp());
294             }
295             
296             // Adjust bounds if necessary.
297
Rectangle JavaDoc mainWindowBoundsSeparated = model.getMainWindowBoundsSeparated();
298             if(mainWindowBoundsSeparated.isEmpty()) {
299                 model.setMainWindowBoundsSeparated(model.getMainWindowBoundsSeparatedHelp());
300             }
301         }
302         
303         model.setEditorAreaState(editorAreaState);
304         
305         if(isVisible()) {
306             viewRequestor.scheduleRequest(
307                 new ViewRequest(null, View.CHANGE_EDITOR_AREA_STATE_CHANGED,
308                         Integer.valueOf(old), Integer.valueOf(editorAreaState)));
309         }
310         
311         WindowManagerImpl.getInstance().doFirePropertyChange(
312             WindowManagerImpl.PROP_EDITOR_AREA_STATE, Integer.valueOf(old), Integer.valueOf(editorAreaState));
313     }
314
315     public void setEditorAreaFrameState(int frameState) {
316         int old = getEditorAreaFrameState();
317         if(old == frameState) {
318             return;
319         }
320         model.setEditorAreaFrameState(frameState);
321         
322         if(isVisible()) {
323             viewRequestor.scheduleRequest(new ViewRequest(null, View.CHANGE_EDITOR_AREA_FRAME_STATE_CHANGED,
324                 Integer.valueOf(old), Integer.valueOf(frameState)));
325         }
326     }
327     
328     /** Sets new maximized mode into model and requests view update (if needed). */
329     void switchMaximizedMode(ModeImpl newMaximizedMode) {
330         ModeImpl old = getCurrentMaximizedMode();
331         if(newMaximizedMode == old) {
332             return;
333         }
334
335         WindowManagerImpl wm = WindowManagerImpl.getInstance();
336         if( null == newMaximizedMode ) {
337             //cancel current maximized mode
338
if( isViewMaximized() ) {
339                 //some non-editor TopComponent is currently maximized
340

341                 //restore docking status of other components
342
if( isEditorMaximized() ) {
343                     restoreViews( model.getMaximizedDockingStatus() );
344                 } else {
345                     restoreViews( model.getDefaultDockingStatus() );
346                 }
347                 
348                 //return the maximized TopComponent to its original mode
349
ModeImpl currentMaximizedMode = getViewMaximizedMode();
350                 assert currentMaximizedMode.getTopComponents().length == 1;
351                 TopComponent maximizedTC = currentMaximizedMode.getTopComponents()[0];
352                 String JavaDoc tcID = wm.findTopComponentID( maximizedTC );
353                 //find the mode where the TopComponent was before its maximization
354
ModeImpl prevMode = getModeTopComponentPreviousMode( tcID, currentMaximizedMode );
355                 int prevIndex = model.getModeTopComponentPreviousIndex( currentMaximizedMode, tcID );
356                 if( null == prevMode ) {
357                     //TODO log a warning here because we somehow lost the previous mode
358
if ((prevMode == null) || !model.getModes().contains(prevMode)) {
359                         // mode to return to isn't valid anymore, try constraints
360
SplitConstraint[] constraints = model.getModeTopComponentPreviousConstraints(currentMaximizedMode, tcID);
361                         if (constraints != null) {
362                             // create mode with the same constraints to dock topcomponent back into
363
prevMode = WindowManagerImpl.getInstance().createModeImpl(
364                                     ModeImpl.getUnusedModeName(), Constants.MODE_KIND_VIEW, false);
365                             model.addMode(prevMode, constraints);
366                         }
367                     }
368
369                     if (prevMode == null) {
370                         // fallback, previous saved mode not found somehow, use default modes
371
prevMode = WindowManagerImpl.getInstance().getDefaultViewMode();
372                     }
373                 }
374                 prevMode.addOpenedTopComponent( maximizedTC, prevIndex );
375                 prevMode.setSelectedTopComponent( maximizedTC );
376                 setActiveMode(prevMode);
377                 model.removeMode( currentMaximizedMode );
378                 //cancel the maximized mode
379
setViewMaximizedMode( null );
380                 
381             } else if( isEditorMaximized() ) {
382                 //an editor TopComponent is maximized
383
model.getMaximizedDockingStatus().mark();
384                 ModeImpl prevActiveMode = getActiveMode();
385                 //restore the docking status of other components
386
restoreViews( model.getDefaultDockingStatus() );
387                 
388                 //cancel the maximized mode
389
setEditorMaximizedMode( null );
390                 
391                 setActiveMode( prevActiveMode );
392                 
393             }
394         } else {
395             assert !isViewMaximized();
396             
397             //set new maximized mode
398
if( newMaximizedMode.getKind() == Constants.MODE_KIND_EDITOR ) {
399                 //the new maximized mode is an editor TopComponent
400

401                 //remember the current docking status of opened TopComponents
402
model.getDefaultDockingStatus().mark();
403                 //slide-out/dock some TopComponents according to their previous state in maximized mode
404
restoreViews( model.getMaximizedDockingStatus() );
405
406                 setEditorMaximizedMode( newMaximizedMode );
407
408             } else if( newMaximizedMode.getKind() == Constants.MODE_KIND_VIEW ) {
409                 //the new maximized mode is non-editor TopComponent
410

411                 //remember the docking status of opened components
412
if( isEditorMaximized() ) {
413                     model.getMaximizedDockingStatus().mark();
414                 } else {
415                     model.getDefaultDockingStatus().mark();
416                 }
417
418                 //get the TopComponent that will be maximized
419
TopComponent tcToMaximize = newMaximizedMode.getSelectedTopComponent();
420                 String JavaDoc tcID = wm.findTopComponentID( tcToMaximize );
421                 int prevIndex = newMaximizedMode.getOpenedTopComponents().indexOf( tcToMaximize );
422
423                 //create a new mode for the maximization
424
ModeImpl mode = WindowManagerImpl.getInstance().createModeImpl(ModeImpl.getUnusedModeName(), Constants.MODE_KIND_VIEW, true);
425                 model.addMode(mode, new SplitConstraint[0]);
426                 //the mode has just one TopComponent
427
mode.addOpenedTopComponent( tcToMaximize );
428                 mode.setSelectedTopComponent( tcToMaximize );
429                 //remember where to put the TopComponent back when un-maximizing
430
setModeTopComponentPreviousMode( tcID, mode, newMaximizedMode, prevIndex );
431
432                 setViewMaximizedMode( mode );
433                 
434                 //slide-out all other TopComponents (the editor area won't be visible)
435
slideAllViews();
436                 
437                 setActiveMode( mode );
438
439             } else {
440                 throw new IllegalArgumentException JavaDoc( "Cannot maximize a sliding view" );
441             }
442         }
443
444         if(isVisible()) {
445             viewRequestor.scheduleRequest(
446                 new ViewRequest(null, View.CHANGE_MAXIMIZED_MODE_CHANGED,
447                     old, getCurrentMaximizedMode()));
448         }
449         
450         WindowManagerImpl.getInstance().doFirePropertyChange(
451             WindowManagerImpl.PROP_MAXIMIZED_MODE, old, getCurrentMaximizedMode());
452     }
453     
454     /** Sets editor mode that is currenlty maximized (used when the window system loads) */
455     void setEditorMaximizedMode(ModeImpl editorMaximizedMode) {
456         model.setEditorMaximizedMode( editorMaximizedMode );
457     }
458     
459     /** Sets view mode that is currenlty maximized (used when the window system loads) */
460     void setViewMaximizedMode(ModeImpl viewMaximizedMode) {
461         model.setViewMaximizedMode( viewMaximizedMode );
462     }
463     
464     /** Sets constraints for mode into model and requests view (if needed). */
465     public void setModeConstraints(ModeImpl mode, SplitConstraint[] modeConstraints) {
466         SplitConstraint[] old = getModeConstraints(mode);
467         if(Arrays.equals(modeConstraints, old)) {
468             return;
469         }
470         
471         model.setModeConstraints(mode, modeConstraints);
472         
473         if(isVisible()) {
474             viewRequestor.scheduleRequest(
475                 new ViewRequest(null, View.CHANGE_MODE_CONSTRAINTS_CHANGED,
476                         old, modeConstraints));
477         }
478     }
479
480     /** Adds mode into model and requests view (if needed). */
481     public void addMode(ModeImpl mode, SplitConstraint[] modeConstraints) {
482         // PENDING which one to use?
483
// if(getModes().contains(mode)) {
484
// return;
485
// }
486
SplitConstraint[] old = getModeConstraints(mode);
487         if(modeConstraints == old) {
488             return;
489         }
490         
491         model.addMode(mode, modeConstraints);
492         
493         if(isVisible()) {
494             viewRequestor.scheduleRequest(
495                 new ViewRequest(null, View.CHANGE_MODE_ADDED, null, mode));
496         }
497         
498         WindowManagerImpl.getInstance().doFirePropertyChange(
499             WindowManager.PROP_MODES, null, null);
500     }
501
502     /** Removes mode from model and requests view (if needed). */
503     public void removeMode(ModeImpl mode) {
504         if(!getModes().contains(mode)) {
505             return;
506         }
507 // debugLog("removeMode()=" + mode.getDisplayName());
508
model.removeMode(mode);
509         if(isVisible()) {
510             viewRequestor.scheduleRequest(
511                 new ViewRequest(null, View.CHANGE_MODE_REMOVED, null, mode));
512         }
513         WindowManagerImpl.getInstance().doFirePropertyChange(
514             WindowManager.PROP_MODES, null, null);
515     }
516
517     /** Sets toolbar configuration name and requests view (if needed). */
518     public void setToolbarConfigName(String JavaDoc toolbarConfigName) {
519         String JavaDoc old = getToolbarConfigName();
520         if(old.equals(toolbarConfigName)) {
521             return;
522         }
523             
524         model.setToolbarConfigName(toolbarConfigName);
525         
526         if(isVisible()) {
527             viewRequestor.scheduleRequest(
528                 new ViewRequest(null, View.CHANGE_TOOLBAR_CONFIGURATION_CHANGED,
529                             old, toolbarConfigName));
530         }
531     }
532
533     /** Updates UI. */
534     public void updateUI() {
535         // Pure request, no model change.
536
if(isVisible()) {
537             viewRequestor.scheduleRequest(new ViewRequest(null, View.CHANGE_UI_UPDATE, null, null));
538         }
539     }
540     
541     ////////////////////////////
542
// Mode specific >>
543
private void closeMode(ModeImpl mode) {
544         if(mode == null) {
545             return;
546         }
547         
548         TopComponent[] tcs = getModeOpenedTopComponents(mode).toArray(new TopComponent[0]);
549         
550         for(int i = 0; i < tcs.length; i++) {
551             TopComponent tc = tcs[i];
552             if(WindowManagerImpl.getInstance().isTopComponentPersistentWhenClosed(tc)) {
553                 model.addModeClosedTopComponent(mode, tc);
554             } else {
555                 //mkleint since one cannot close the sliding mode just like that, we don't need to check the previous mode of the tc.
556
model.removeModeTopComponent(mode, tc);
557                 String JavaDoc id = WindowManagerImpl.getInstance().findTopComponentID(tc);
558                 PersistenceManager.getDefault().removeGlobalTopComponentID(id);
559             }
560         }
561         
562         ModeImpl oldActive = getActiveMode();
563         ModeImpl newActive;
564         if(mode == oldActive) {
565             newActive = setSomeModeActive();
566         } else {
567             newActive = oldActive;
568         }
569 // debugLog("closeMode()");
570

571         // Remove mode from model if is not permanennt and emptied.
572
boolean modeRemoved = false;
573         if(!mode.isPermanent() && model.getModeTopComponents(mode).isEmpty()) {
574             // only if no sliding modes' tc points to this mode, then it's ok to remove it.
575
if (doCheckSlidingModes(mode)) {
576 // debugLog("do close mode=" + mode.getDisplayName());
577
model.removeMode(mode);
578                 modeRemoved = true;
579             }
580         }
581         
582         if(isVisible()) {
583             viewRequestor.scheduleRequest(new ViewRequest(mode, View.CHANGE_MODE_CLOSED, null, null));
584         }
585
586         // Notify closed.
587
for(int i = 0; i < tcs.length; i++) {
588             // Notify TopComponent was closed.
589
WindowManagerImpl.getInstance().notifyTopComponentClosed(tcs[i]);
590         }
591         
592         if(oldActive != newActive) {
593             WindowManagerImpl.getInstance().doFirePropertyChange(
594                 WindowManagerImpl.PROP_ACTIVE_MODE, oldActive, newActive);
595         }
596     
597         if(modeRemoved) {
598             WindowManagerImpl.getInstance().doFirePropertyChange(
599                 WindowManager.PROP_MODES, null, null);
600         }
601         
602         // Notify new active.
603
if(newActive != null) {
604             // Notify registry.
605
WindowManagerImpl.notifyRegistryTopComponentActivated(
606                 newActive.getSelectedTopComponent());
607         } else {
608             WindowManagerImpl.notifyRegistryTopComponentActivated(null);
609         }
610     }
611     
612     // XXX TODO Model should handle this on its own.
613
private ModeImpl setSomeModeActive() {
614         for(Iterator JavaDoc it = getModes().iterator(); it.hasNext(); ) {
615             ModeImpl mode = (ModeImpl)it.next();
616             if(!mode.getOpenedTopComponents().isEmpty() && Constants.MODE_KIND_SLIDING != mode.getKind()) {
617                 model.setActiveMode(mode);
618                 return mode;
619             }
620         }
621         model.setActiveMode(null);
622         return model.getActiveMode();
623     }
624
625     
626     /** Sets bounds into model and requests view (if needed). */
627     public void setModeBounds(ModeImpl mode, Rectangle JavaDoc bounds) {
628         if(bounds == null) {
629             return;
630         }
631         
632         Rectangle JavaDoc old = getModeBounds(mode);
633         if(old.equals(bounds)) {
634             return;
635         }
636         
637         model.setModeBounds(mode, bounds);
638         
639         if(isVisible() && getEditorAreaState() == Constants.EDITOR_AREA_SEPARATED) {
640             viewRequestor.scheduleRequest(new ViewRequest(
641                 mode, View.CHANGE_MODE_BOUNDS_CHANGED, old, bounds));
642         }
643         
644         mode.doFirePropertyChange(ModeImpl.PROP_BOUNDS, old, bounds);
645     }
646     
647     /** Sets frame state. */
648     public void setModeFrameState(ModeImpl mode, int frameState) {
649         int old = getModeFrameState(mode);
650         if(frameState == old) {
651             return;
652         }
653         
654         model.setModeFrameState(mode, frameState);
655         
656         if(isVisible()) {
657             viewRequestor.scheduleRequest(new ViewRequest(
658                 mode, View.CHANGE_MODE_FRAME_STATE_CHANGED,
659                 Integer.valueOf(old), Integer.valueOf(frameState)));
660         }
661     }
662     
663     /** Sets seleted TopComponent into model and requests view (if needed). */
664     public void setModeSelectedTopComponent(ModeImpl mode, TopComponent selected) {
665         // don't apply check for sliding kind when clearing selection to null
666
if (mode.getKind() != Constants.MODE_KIND_SLIDING || selected != null) {
667             if(!getModeOpenedTopComponents(mode).contains(selected)) {
668                 return;
669             }
670         }
671         
672         TopComponent old = getModeSelectedTopComponent(mode);
673         if(selected == old) {
674             return;
675         }
676         
677         model.setModeSelectedTopComponent(mode, selected);
678         
679         if(isVisible()) {
680             viewRequestor.scheduleRequest(
681                 new ViewRequest(mode, View.CHANGE_MODE_SELECTED_TOPCOMPONENT_CHANGED,
682                 old, selected));
683         }
684         
685         // Notify registry.
686
if(mode == getActiveMode()) {
687             WindowManagerImpl.notifyRegistryTopComponentActivated(selected);
688         }
689     }
690     
691     /**
692      * Remember which TopComponent was previously the selected one, used when switching to/from maximized mode.
693      *
694      * @param mode
695      * @param tc TopComponent that was previously selected.
696      */

697     public void setModePreviousSelectedTopComponent(ModeImpl mode, TopComponent tc) {
698         model.setModePreviousSelectedTopComponent( mode, tc );
699     }
700
701     /** Adds opened TopComponent into model and requests view (if needed). */
702     public void addModeOpenedTopComponent(ModeImpl mode, TopComponent tc) {
703         if(getModeOpenedTopComponents(mode).contains(tc)) {
704             return;
705         }
706
707         // Validate the TopComponent was removed from other modes.
708
removeTopComponentFromOtherModes(mode, tc);
709         
710         model.addModeOpenedTopComponent(mode, tc);
711         
712         if(isVisible()) {
713             viewRequestor.scheduleRequest(
714                 new ViewRequest(mode, View.CHANGE_MODE_TOPCOMPONENT_ADDED,
715                 null, tc));
716         }
717         
718         // Notify opened.
719
WindowManagerImpl.getInstance().notifyTopComponentOpened(tc);
720     }
721     
722     public void insertModeOpenedTopComponent(ModeImpl mode, TopComponent tc, int index) {
723         List JavaDoc openedTcs = getModeOpenedTopComponents(mode);
724         if(index >= 0 && !openedTcs.isEmpty()
725         && openedTcs.size() > index && openedTcs.get(index) == tc) {
726             return;
727         }
728         
729         // Validate the TopComponent was removed from other modes.
730
removeTopComponentFromOtherModes(mode, tc);
731         
732         model.insertModeOpenedTopComponent(mode, tc, index);
733         
734         if(isVisible()) {
735             viewRequestor.scheduleRequest(
736                 new ViewRequest(mode, View.CHANGE_MODE_TOPCOMPONENT_ADDED, // PENDING inserted?
737
null, tc));
738         }
739     }
740     
741     public void addModeClosedTopComponent(ModeImpl mode, TopComponent tc) {
742         boolean opened = getModeOpenedTopComponents(mode).contains(tc);
743         
744         if(opened && !tc.canClose()) {
745             return;
746         }
747         
748         if(containsModeTopComponent(mode,tc) && !opened) {
749             return;
750         }
751         
752         // Validate the TopComponent was removed from other modes.
753
removeTopComponentFromOtherModes(mode, tc);
754         
755         model.addModeClosedTopComponent(mode, tc);
756
757         ModeImpl oldActive = getActiveMode();
758         ModeImpl newActive;
759         if(model.getModeOpenedTopComponents(mode).isEmpty() && mode == oldActive) {
760             newActive = setSomeModeActive();
761         } else {
762             newActive = oldActive;
763         }
764
765         // Unmaximize mode if necessary.
766
if(getCurrentMaximizedMode() == mode && model.getModeOpenedTopComponents(mode).isEmpty()) {
767             switchMaximizedMode(null);
768         }
769         
770         if(isVisible() && opened) {
771             viewRequestor.scheduleRequest(
772                 new ViewRequest(mode, View.CHANGE_MODE_TOPCOMPONENT_REMOVED,
773                 null, tc));
774         }
775
776         // Notify closed.
777
if(opened) {
778             WindowManagerImpl.getInstance().notifyTopComponentClosed(tc);
779         }
780         
781         if(oldActive != newActive) {
782             WindowManagerImpl.getInstance().doFirePropertyChange(
783                 WindowManagerImpl.PROP_ACTIVE_MODE, oldActive, newActive);
784         }
785
786         if(newActive != null) {
787             // Notify registry.
788
WindowManagerImpl.notifyRegistryTopComponentActivated(
789                 newActive.getSelectedTopComponent());
790         } else {
791             WindowManagerImpl.notifyRegistryTopComponentActivated(null);
792         }
793     }
794
795     // XXX Could be called only during load phase of window system.
796
public void addModeUnloadedTopComponent(ModeImpl mode, String JavaDoc tcID) {
797         model.addModeUnloadedTopComponent(mode, tcID);
798     }
799     
800     // XXX
801
public void setUnloadedSelectedTopComponent(ModeImpl mode, String JavaDoc tcID) {
802         model.setModeUnloadedSelectedTopComponent(mode, tcID);
803     }
804     
805     public void setUnloadedPreviousSelectedTopComponent(ModeImpl mode, String JavaDoc tcID) {
806         model.setModeUnloadedPreviousSelectedTopComponent(mode, tcID);
807     }
808
809     // XXX
810
public List JavaDoc<String JavaDoc> getModeOpenedTopComponentsIDs(ModeImpl mode) {
811         return model.getModeOpenedTopComponentsIDs(mode);
812     }
813     // XXX
814
public List JavaDoc getModeClosedTopComponentsIDs(ModeImpl mode) {
815         return model.getModeClosedTopComponentsIDs(mode);
816     }
817     // XXX
818
public List JavaDoc getModeTopComponentsIDs(ModeImpl mode) {
819         return model.getModeTopComponentsIDs(mode);
820     }
821     
822     /** Helper validation. */
823     private boolean removeTopComponentFromOtherModes(ModeImpl mode, TopComponent tc) {
824         boolean tcRemoved = false;
825         for(Iterator JavaDoc it = model.getModes().iterator(); it.hasNext(); ) {
826             ModeImpl m = (ModeImpl)it.next();
827             if(m == mode) {
828                 continue;
829             }
830             
831             if(model.containsModeTopComponent(m, tc)) {
832                 tcRemoved = true;
833                 model.removeModeTopComponent(m, tc);
834 // debugLog("removeTopComponentFromOtherModes()");
835

836                 // Remove mode from model if is not permanennt and emptied.
837
boolean modeRemoved = false;
838                 if(!m.isPermanent() && m.isEmpty() && doCheckSlidingModes(m)
839                     // now the tc is not added to the sliding mode yet, but is *somehow* expected to be..
840
// maybe needs redesign..
841
&& mode.getKind() != Constants.MODE_KIND_SLIDING) {
842 // debugLog("removeTopComponentFromOtherModes() - really removing=" + m.getDisplayName());
843
model.removeMode(m);
844                     modeRemoved = true;
845                 }
846             
847                 if(modeRemoved) {
848                     WindowManagerImpl.getInstance().doFirePropertyChange(
849                         WindowManager.PROP_MODES, null, null);
850                 }
851             }
852         }
853         
854         return tcRemoved;
855     }
856     
857     /** Removed top component from model and requests view (if needed). */
858     public void removeModeTopComponent(ModeImpl mode, TopComponent tc) {
859         if(!containsModeTopComponent(mode, tc)) {
860             return;
861         }
862         
863         boolean viewChange = getModeOpenedTopComponents(mode).contains(tc);
864         
865         if(viewChange && !tc.canClose()) {
866             return;
867         }
868         
869         model.removeModeTopComponent(mode, tc);
870         String JavaDoc id = WindowManagerImpl.getInstance().findTopComponentID(tc);
871         PersistenceManager.getDefault().removeGlobalTopComponentID(id);
872
873         ModeImpl oldActive = getActiveMode();
874         ModeImpl newActive;
875         if(model.getModeOpenedTopComponents(mode).isEmpty() && mode == oldActive) {
876             newActive = setSomeModeActive();
877         } else {
878             newActive = oldActive;
879         }
880
881         // Unmaximize mode if necessary.
882
if(getCurrentMaximizedMode() == mode && model.getModeOpenedTopComponents(mode).isEmpty()) {
883             switchMaximizedMode(null);
884         }
885         
886 // debugLog("removeModeTopComponent()");
887
// Remove mode from model if is not permanennt and emptied.
888
boolean modeRemoved = false;
889         if(!mode.isPermanent() && model.getModeTopComponents(mode).isEmpty()) {
890             // remove only if there's no other component in sliding modes that has this one as the previous mode.
891
//TODO
892
if (doCheckSlidingModes(mode)) {
893 // debugLog("removeModeTopComponent() -removing " + mode.getDisplayName());
894
model.removeMode(mode);
895                 modeRemoved = true;
896             }
897         }
898         
899         
900         
901         if(viewChange && isVisible()) {
902             viewRequestor.scheduleRequest(new ViewRequest(
903                 mode, View.CHANGE_MODE_TOPCOMPONENT_REMOVED,
904                 null, tc));
905         }
906         
907         // Notify closed.
908
if(viewChange) {
909             WindowManagerImpl.getInstance().notifyTopComponentClosed(tc);
910         }
911         
912         if(oldActive != newActive) {
913             WindowManagerImpl.getInstance().doFirePropertyChange(
914                 WindowManagerImpl.PROP_ACTIVE_MODE, oldActive, newActive);
915         }
916     
917         if(modeRemoved) {
918             WindowManagerImpl.getInstance().doFirePropertyChange(
919                 WindowManager.PROP_MODES, null, null);
920         }
921             
922         if(newActive != null) {
923             // Notify registry.
924
WindowManagerImpl.notifyRegistryTopComponentActivated(
925                 newActive.getSelectedTopComponent());
926         } else {
927             WindowManagerImpl.notifyRegistryTopComponentActivated(
928                 null);
929         }
930     }
931     
932    // remove the mode only if there's no other component in sliding modes that has this one as the previous mode.
933
private boolean doCheckSlidingModes(ModeImpl mode) {
934         ModeImpl slid = model.getSlidingMode(Constants.BOTTOM);
935         if (slid != null) {
936             TopComponent[] tcs = slid.getTopComponents();
937             for (int i = 0; i < tcs.length; i++) {
938                 String JavaDoc tcID = WindowManagerImpl.getInstance().findTopComponentID(tcs[i]);
939                 ModeImpl impl = model.getModeTopComponentPreviousMode(slid, tcID);
940                 if (impl == mode) {
941                     return false;
942                 }
943             }
944         }
945         slid = model.getSlidingMode(Constants.LEFT);
946         if (slid != null) {
947             TopComponent[] tcs = slid.getTopComponents();
948             for (int i = 0; i < tcs.length; i++) {
949                 String JavaDoc tcID = WindowManagerImpl.getInstance().findTopComponentID(tcs[i]);
950                 ModeImpl impl = model.getModeTopComponentPreviousMode(slid, tcID);
951                 if (impl == mode) {
952                     return false;
953                 }
954             }
955         }
956         slid = model.getSlidingMode(Constants.RIGHT);
957         if (slid != null) {
958             TopComponent[] tcs = slid.getTopComponents();
959             for (int i = 0; i < tcs.length; i++) {
960                 String JavaDoc tcID = WindowManagerImpl.getInstance().findTopComponentID(tcs[i]);
961                 ModeImpl impl = model.getModeTopComponentPreviousMode(slid, tcID);
962                 if (impl == mode) {
963                     return false;
964                 }
965             }
966         }
967         return true;
968     }
969     
970     // XXX
971
public void removeModeClosedTopComponentID(ModeImpl mode, String JavaDoc tcID) {
972         // It is silent now, has to be used only for closed yet unloaded components!
973
model.removeModeClosedTopComponentID(mode, tcID);
974     }
975     /// << Mode specific
976
//////////////////////////////
977

978     // TopComponentGroup>>
979
public boolean isGroupOpened(TopComponentGroupImpl tcGroup) {
980         return model.isGroupOpened(tcGroup);
981     }
982     
983     /** Opens TopComponentGroup. */
984     public void openGroup(TopComponentGroupImpl tcGroup) {
985         if(isGroupOpened(tcGroup)) {
986             return;
987         }
988         
989         if( isEditorMaximized() && isViewMaximized() )
990             switchMaximizedMode( null );
991
992         Set JavaDoc<TopComponent> openedBeforeTopComponents = new HashSet JavaDoc<TopComponent>();
993         Set JavaDoc<TopComponent> tcs = tcGroup.getTopComponents();
994         for(Iterator JavaDoc<TopComponent> it = tcs.iterator(); it.hasNext(); ) {
995             TopComponent tc = it.next();
996             if( tc.isOpened() ) {
997                 openedBeforeTopComponents.add( tc );
998             }
999         }
1000        
1001        tcs = tcGroup.getOpeningSet();
1002        HashSet JavaDoc<ModeImpl> openedModes = new HashSet JavaDoc<ModeImpl>( tcs.size() );
1003        List JavaDoc<TopComponent> openedTcs = new ArrayList JavaDoc<TopComponent>();
1004        for(Iterator JavaDoc<TopComponent> it = tcs.iterator(); it.hasNext(); ) {
1005            TopComponent tc = it.next();
1006            if(!tc.isOpened()) {
1007                WindowManagerImpl wm = WindowManagerImpl.getInstance();
1008                ModeImpl mode = (ModeImpl)wm.findMode(tc);
1009                if(mode == null) {
1010                    // Only view TopComponent is in group.
1011
mode = wm.getDefaultViewMode();
1012                } else {
1013                    if( mode.getOpenedTopComponentsIDs().isEmpty() ) {
1014                        openedModes.add( mode );
1015                    }
1016                }
1017                model.addModeOpenedTopComponent(mode, tc);
1018                
1019                if( isEditorMaximized() && mode.getState() != Constants.MODE_STATE_SEPARATED ) {
1020                    String JavaDoc tcID = wm.findTopComponentID( tc );
1021                    if( !isTopComponentDockedInMaximizedMode( tcID ) && mode.getKind() != Constants.MODE_KIND_SLIDING ) {
1022                        //slide the TopComponent to edgebar and slide it out
1023
slide( tc, mode, getSlideSideForMode( mode ) );
1024                    }
1025                }
1026                openedTcs.add(tc);
1027            }
1028        }
1029
1030        
1031        model.openGroup(tcGroup, new HashSet JavaDoc<TopComponent>(openedTcs), openedBeforeTopComponents);
1032        
1033        //restore selected TopComponents
1034
for( ModeImpl mode : openedModes ) {
1035            TopComponent prevSelTC = mode.getPreviousSelectedTopComponent();
1036            if( null != prevSelTC )
1037                mode.setSelectedTopComponent( prevSelTC );
1038        }
1039        
1040        if(isVisible()) {
1041            viewRequestor.scheduleRequest(new ViewRequest(tcGroup,
1042                View.CHANGE_TOPCOMPONENT_ARRAY_ADDED, null,
1043                openedTcs.toArray(new TopComponent[0])));
1044        }
1045
1046        // Notify oepned.
1047
for(TopComponent tc: openedTcs) {
1048            WindowManagerImpl.getInstance().notifyTopComponentOpened(tc);
1049        }
1050    }
1051    
1052    /** Closes TopComponentGroup. */
1053    public void closeGroup(TopComponentGroupImpl tcGroup) {
1054        if(!isGroupOpened(tcGroup)) {
1055            return;
1056        }
1057        
1058        Set JavaDoc tcs = tcGroup.getClosingSet();
1059        List JavaDoc<TopComponent> closedTcs = new ArrayList JavaDoc<TopComponent>();
1060        
1061        Set JavaDoc<TopComponent> openedTcsByGroup = model.getGroupOpenedTopComponents(tcGroup);
1062        
1063        // Find out TC which were opened before the group was opened.
1064
Set JavaDoc<TopComponent> openedTcsBefore = model.getGroupOpenedBeforeTopComponents(tcGroup);
1065
1066        // Adjust opening flags.
1067
for(Iterator JavaDoc<TopComponent> it = model.getGroupTopComponents(tcGroup).iterator(); it.hasNext(); ) {
1068            TopComponent tc = it.next();
1069            boolean wasOpenedBefore = openedTcsBefore.contains(tc);
1070            boolean openedByGroup = openedTcsByGroup.contains(tc);
1071            
1072            if(tc.isOpened()) {
1073                if(!wasOpenedBefore && !openedByGroup) {
1074                    // Open by group next time, user opened it while group was opened.
1075
model.addGroupOpeningTopComponent(tcGroup, tc);
1076                }
1077            } else {
1078                if(wasOpenedBefore || openedByGroup) {
1079                    // Don't open by group next time, user closed it while group was opened.
1080
model.removeGroupOpeningTopComponent(tcGroup, tc);
1081                }
1082            }
1083        }
1084
1085        ArrayList JavaDoc<ModeImpl> groupModes = new ArrayList JavaDoc<ModeImpl>( tcs.size() );
1086        //remember which TCs are active
1087
for(Iterator JavaDoc it = tcs.iterator(); it.hasNext(); ) {
1088            TopComponent tc = (TopComponent)it.next();
1089            if( !tc.isOpened() || openedTcsBefore.contains(tc)) {
1090                continue;
1091            }
1092            ModeImpl mode = (ModeImpl)WindowManagerImpl.getInstance().findMode(tc);
1093            if( null != mode )
1094                groupModes.add( mode );
1095        }
1096        for( ModeImpl mode : groupModes ) {
1097            TopComponent selTC = mode.getSelectedTopComponent();
1098            if( null != selTC )
1099                setModePreviousSelectedTopComponent( mode, selTC );
1100        }
1101        
1102        // Now close those which needed.
1103
for(Iterator JavaDoc it = tcs.iterator(); it.hasNext(); ) {
1104            TopComponent tc = (TopComponent)it.next();
1105            if(tc.isOpened()) {
1106                // Whether to ignore closing flag.
1107
if(openedTcsBefore.contains(tc)) {
1108                    continue;
1109                }
1110                
1111                boolean ignore = false;
1112                for(Iterator JavaDoc it2 = model.getTopComponentGroups().iterator(); it2.hasNext(); ) {
1113                    TopComponentGroupImpl group = (TopComponentGroupImpl)it2.next();
1114                    if(group == tcGroup) {
1115                        continue;
1116                    }
1117                    if(group.isOpened() && group.getOpeningSet().contains(tc)) {
1118                        ignore = true;
1119                        break;
1120                    }
1121                }
1122                if(ignore) {
1123                    continue;
1124                }
1125                
1126                // Now you can close it.
1127
ModeImpl mode = (ModeImpl)WindowManagerImpl.getInstance().findMode(tc);
1128                if(mode != null) {
1129                    if(WindowManagerImpl.getInstance().isTopComponentPersistentWhenClosed(tc)) {
1130                        model.addModeClosedTopComponent(mode, tc);
1131                    } else {
1132                        model.removeModeTopComponent(mode, tc);
1133                        String JavaDoc id = WindowManagerImpl.getInstance().findTopComponentID(tc);
1134                        PersistenceManager.getDefault().removeGlobalTopComponentID(id);
1135                    }
1136                    closedTcs.add(tc);
1137                }
1138            }
1139        }
1140
1141        model.closeGroup(tcGroup);
1142        
1143        if(isVisible()) {
1144            viewRequestor.scheduleRequest(new ViewRequest(tcGroup,
1145                View.CHANGE_TOPCOMPONENT_ARRAY_REMOVED, null,
1146                closedTcs.toArray(new TopComponent[0])));
1147        }
1148        
1149        // Notify closed.
1150
for(TopComponent tc: closedTcs) {
1151            WindowManagerImpl.getInstance().notifyTopComponentClosed(tc);
1152        }
1153    }
1154    
1155    /** Adds TopComponentGroup into model. */
1156    public void addTopComponentGroup(TopComponentGroupImpl tcGroup) {
1157        model.addTopComponentGroup(tcGroup);
1158    }
1159    
1160    /** Removes TopComponentGroup from model. */
1161    public void removeTopComponentGroup(TopComponentGroupImpl tcGroup) {
1162        model.removeTopComponentGroup(tcGroup);
1163    }
1164    
1165    public boolean addGroupUnloadedTopComponent(TopComponentGroupImpl tcGroup, String JavaDoc tcID) {
1166        return model.addGroupUnloadedTopComponent(tcGroup, tcID);
1167    }
1168    
1169    public boolean removeGroupUnloadedTopComponent(TopComponentGroupImpl tcGroup, String JavaDoc tcID) {
1170        return model.removeGroupUnloadedTopComponent(tcGroup, tcID);
1171    }
1172    
1173    /** Adds opening top component for set into model. */
1174    public boolean addGroupUnloadedOpeningTopComponent(TopComponentGroupImpl tcGroup, String JavaDoc tcID) {
1175        return model.addGroupUnloadedOpeningTopComponent(tcGroup, tcID);
1176    }
1177    
1178    /** Removes opening top component from model. */
1179    public boolean removeGroupUnloadedOpeningTopComponent(TopComponentGroupImpl tcGroup, String JavaDoc tcID) {
1180        return model.removeGroupUnloadedOpeningTopComponent(tcGroup, tcID);
1181    }
1182    
1183    /** Adds closing top component for set into model. */
1184    public boolean addGroupUnloadedClosingTopComponent(TopComponentGroupImpl tcGroup, String JavaDoc tcID) {
1185        return model.addGroupUnloadedClosingTopComponent(tcGroup, tcID);
1186    }
1187    
1188    /** Removes closing top component for set from model. */
1189    public boolean removeGroupUnloadedClosingTopComponent(TopComponentGroupImpl tcGroup, String JavaDoc tcID) {
1190        return model.removeGroupUnloadedClosingTopComponent(tcGroup, tcID);
1191    }
1192    
1193    // XXX Just helper for persistence.
1194
public boolean addGroupUnloadedOpenedTopComponent(TopComponentGroupImpl tcGroup, String JavaDoc tcID) {
1195        if(!isGroupOpened(tcGroup)) {
1196            return false;
1197        }
1198        
1199        return model.addGroupUnloadedOpenedTopComponent(tcGroup, tcID);
1200    }
1201    
1202    // XXX Just helper for persistence
1203
public Set JavaDoc getGroupOpenedTopComponents(TopComponentGroupImpl tcGroup) {
1204        return model.getGroupOpenedTopComponents(tcGroup);
1205    }
1206    
1207    // XXX>>
1208
public Set JavaDoc<String JavaDoc> getGroupTopComponentsIDs(TopComponentGroupImpl tcGroup) {
1209        return model.getGroupTopComponentsIDs(tcGroup);
1210    }
1211    
1212    public Set JavaDoc<String JavaDoc> getGroupOpeningSetIDs(TopComponentGroupImpl tcGroup) {
1213        return model.getGroupOpeningSetIDs(tcGroup);
1214    }
1215    
1216    public Set JavaDoc<String JavaDoc> getGroupClosingSetIDs(TopComponentGroupImpl tcGroup) {
1217        return model.getGroupClosingSetIDs(tcGroup);
1218    }
1219    
1220    public Set JavaDoc<String JavaDoc> getGroupOpenedTopComponentsIDs(TopComponentGroupImpl tcGroup) {
1221        return model.getGroupOpenedTopComponentsIDs(tcGroup);
1222    }
1223    // XXX<<
1224
// TopComponentGroup<<
1225
//////////////////////////////
1226

1227    // Mutators <<
1228
/////////////////////
1229

1230    
1231    /////////////////////
1232
// Accessors>>
1233

1234    /** Indicates whether windows system shows GUI. */
1235    public boolean isVisible() {
1236        return model.isVisible();
1237    }
1238    
1239    /** Gets <code>Set</code> of all <code>Mode</code>'s. */
1240    public Set JavaDoc<? extends Mode> getModes () {
1241        return model.getModes();
1242    }
1243
1244    /** Gets main window bounds for joined(tiled) state. */
1245    public Rectangle JavaDoc getMainWindowBoundsJoined() {
1246        return model.getMainWindowBoundsJoined();
1247    }
1248    
1249    /** Gets main window bounds for separated state. */
1250    public Rectangle JavaDoc getMainWindowBoundsSeparated() {
1251        return model.getMainWindowBoundsSeparated();
1252    }
1253    
1254    public int getMainWindowFrameStateJoined() {
1255        return model.getMainWindowFrameStateJoined();
1256    }
1257    
1258    public int getMainWindowFrameStateSeparated() {
1259        return model.getMainWindowFrameStateSeparated();
1260    }
1261
1262    /** Gets active mode from model. */
1263    public ModeImpl getActiveMode () {
1264        return model.getActiveMode();
1265    }
1266
1267    /** Gets editor area bounds from model. */
1268    public Rectangle JavaDoc getEditorAreaBounds() {
1269        return model.getEditorAreaBounds();
1270    }
1271
1272    /** Gets editor area constraints from model. */
1273    public SplitConstraint[] getEditorAreaConstraints() {
1274        return model.getEditorAreaConstraints();
1275    }
1276
1277    /** Gets editor area state from model. */
1278    public int getEditorAreaState() {
1279        return model.getEditorAreaState();
1280    }
1281    
1282    public int getEditorAreaFrameState() {
1283        return model.getEditorAreaFrameState();
1284    }
1285    
1286    public Component JavaDoc getEditorAreaComponent() {
1287        return viewRequestor.getEditorAreaComponent();
1288    }
1289
1290    /** Gets mode that is currently maximized (can be an editor or view component). */
1291    ModeImpl getCurrentMaximizedMode() {
1292        if( isViewMaximized() )
1293            return model.getViewMaximizedMode();
1294        if( isEditorMaximized() )
1295            return model.getEditorMaximizedMode();
1296        return null;
1297    }
1298
1299    /** Gets editor maximized mode. */
1300    ModeImpl getEditorMaximizedMode() {
1301        return model.getEditorMaximizedMode();
1302    }
1303
1304    /** Gets view maximized mode. */
1305    ModeImpl getViewMaximizedMode() {
1306        return model.getViewMaximizedMode();
1307    }
1308    
1309    /** Gets constraints for mode from model. */
1310    public SplitConstraint[] getModeConstraints(ModeImpl mode) {
1311        return model.getModeConstraints(mode);
1312    }
1313
1314
1315    /** Gets toolbar configuration name from model. */
1316    public String JavaDoc getToolbarConfigName () {
1317        return model.getToolbarConfigName();
1318    }
1319
1320    ////////////////////////////////
1321
/// >> Mode specific
1322
/** Gets programatic name of mode. */
1323    public String JavaDoc getModeName(ModeImpl mode) {
1324        return model.getModeName(mode);
1325    }
1326    /** Gets bounds. */
1327    public Rectangle JavaDoc getModeBounds(ModeImpl mode) {
1328        return model.getModeBounds(mode);
1329    }
1330    /** Gets State. */
1331    public int getModeState(ModeImpl mode) {
1332        return model.getModeState(mode);
1333    }
1334    /** Gets kind. */
1335    public int getModeKind(ModeImpl mode) {
1336        return model.getModeKind(mode);
1337    }
1338    
1339    /** Gets side. */
1340    public String JavaDoc getModeSide(ModeImpl mode) {
1341        return model.getModeSide(mode);
1342    }
1343    
1344    /** Gets frame state. */
1345    public int getModeFrameState(ModeImpl mode) {
1346        return model.getModeFrameState(mode);
1347    }
1348    /** Gets used defined. */
1349    public boolean isModePermanent(ModeImpl mode) {
1350        return model.isModePermanent(mode);
1351    }
1352    public boolean isModeEmpty(ModeImpl mode) {
1353        return model.isModeEmpty(mode);
1354    }
1355    /** */
1356    public boolean containsModeTopComponent(ModeImpl mode, TopComponent tc) {
1357        return model.containsModeTopComponent(mode, tc);
1358    }
1359    /** Gets selected TopComponent. */
1360    public TopComponent getModeSelectedTopComponent(ModeImpl mode) {
1361        return model.getModeSelectedTopComponent(mode);
1362    }
1363    /**
1364     * @return TopComponent that was previously selected in the given mode or null.
1365     */

1366    public TopComponent getModePreviousSelectedTopComponent(ModeImpl mode) {
1367        return model.getModePreviousSelectedTopComponent( mode );
1368    }
1369    /** Gets list of top components in this workspace. */
1370    public List JavaDoc<TopComponent> getModeTopComponents(ModeImpl mode) {
1371        return model.getModeTopComponents(mode);
1372    }
1373    /** Gets list of top components in this workspace. */
1374    public List JavaDoc<TopComponent> getModeOpenedTopComponents(ModeImpl mode) {
1375        return model.getModeOpenedTopComponents(mode);
1376    }
1377    /// << Mode specific
1378
////////////////////////////////
1379

1380    ////////////////////////////////////
1381
// TopComponentGroup specific >>
1382
public Set JavaDoc<TopComponentGroupImpl> getTopComponentGroups() {
1383        return model.getTopComponentGroups();
1384    }
1385    
1386    public String JavaDoc getGroupName(TopComponentGroupImpl tcGroup) {
1387        return model.getGroupName(tcGroup);
1388    }
1389    
1390    public Set JavaDoc<TopComponent> getGroupTopComponents(TopComponentGroupImpl tcGroup) {
1391        return model.getGroupTopComponents(tcGroup);
1392    }
1393    
1394    /** Gets opening top components for group from model. */
1395    public Set JavaDoc<TopComponent> getGroupOpeningTopComponents(TopComponentGroupImpl tcGroup) {
1396        return model.getGroupOpeningTopComponents(tcGroup);
1397    }
1398    
1399    /** Gets closing top components for group from model. */
1400    public Set JavaDoc getGroupClosingTopComponents(TopComponentGroupImpl tcGroup) {
1401        return model.getGroupClosingTopComponents(tcGroup);
1402    }
1403    // TopComponentGroup specific <<
1404
////////////////////////////////////
1405

1406    // Accessors<<
1407
/////////////////////
1408

1409    
1410    // Others>>
1411
// PENDING>>
1412
public void topComponentDisplayNameChanged(ModeImpl mode, TopComponent tc) {
1413        if(isVisible()) {
1414            viewRequestor.scheduleRequest(new ViewRequest(
1415                mode, View.CHANGE_TOPCOMPONENT_DISPLAY_NAME_CHANGED, null, tc));
1416        }
1417    }
1418
1419    public void topComponentDisplayNameAnnotation(ModeImpl mode, TopComponent tc) {
1420        if(isVisible()) {
1421            viewRequestor.scheduleRequest(new ViewRequest(
1422                mode, View.CHANGE_TOPCOMPONENT_DISPLAY_NAME_ANNOTATION_CHANGED, null, tc));
1423        }
1424    }
1425    // PENDING<<
1426

1427    public void topComponentToolTipChanged(ModeImpl mode, TopComponent tc) {
1428        if(isVisible()) {
1429            viewRequestor.scheduleRequest(new ViewRequest(
1430                mode, View.CHANGE_TOPCOMPONENT_TOOLTIP_CHANGED, null, tc));
1431        }
1432    }
1433    
1434    public void topComponentIconChanged(ModeImpl mode, TopComponent tc) {
1435        if(isVisible()) {
1436            viewRequestor.scheduleRequest(new ViewRequest(
1437                mode, View.CHANGE_TOPCOMPONENT_ICON_CHANGED, null, tc));
1438        }
1439    }
1440    
1441    public void resetModel() {
1442        model.reset();
1443    }
1444    
1445    // Others<<
1446

1447    
1448    // Compound ones>>
1449
public void attachTopComponentsToSide(TopComponent[] tcs, ModeImpl attachMode, String JavaDoc side) {
1450        attachTopComponentsToSide(tcs, attachMode, side, true);
1451    }
1452    
1453    /** Creates new mode on side of specified one and puts there the TopComponentS. */
1454    private void attachTopComponentsToSide(TopComponent[] tcs, ModeImpl attachMode, String JavaDoc side, boolean fireEvents) {
1455        if(tcs == null || tcs.length == 0) {
1456            return;
1457        }
1458        
1459        // New mode. It is necessary to add it yet.
1460
ModeImpl newMode = WindowManagerImpl.getInstance().createModeImpl(
1461            ModeImpl.getUnusedModeName(), attachMode.getKind(), false);
1462
1463        // XXX All others should have the same restriction.
1464
if(!newMode.canContain(tcs[0])) {
1465            return;
1466        }
1467        model.addModeToSide(newMode, attachMode, side);
1468        
1469        attachTopComponentsHelper(tcs, newMode, fireEvents);
1470    }
1471
1472    /** Creates new mode on side of desktops and puts there the TopComponentS. */
1473    private void attachTopComponentsAroundDesktop(TopComponent[] tcs, String JavaDoc side, boolean fireEvents) {
1474        if(tcs == null || tcs.length == 0) {
1475            return;
1476        }
1477        
1478        // New mode. It is necessary to add it yet.
1479
ModeImpl newMode = WindowManagerImpl.getInstance().createModeImpl(
1480            ModeImpl.getUnusedModeName(), Constants.MODE_KIND_VIEW, false);
1481
1482        // XXX All others should have the same restriction.
1483
if(!newMode.canContain(tcs[0])) {
1484            return;
1485        }
1486        model.addModeAround(newMode, side);
1487        
1488        attachTopComponentsHelper(tcs, newMode, fireEvents);
1489    }
1490    
1491    /** Creates new mode on side of editor area and puts there the TopComponentS. */
1492    private void attachTopComponentsAroundEditor(TopComponent[] tcs, String JavaDoc side, boolean fireEvents, int modeKind) {
1493        if(tcs == null || tcs.length == 0) {
1494            return;
1495        }
1496        
1497        // New mode. It is necessary to add it yet.
1498
ModeImpl newMode = WindowManagerImpl.getInstance().createModeImpl(
1499            ModeImpl.getUnusedModeName(), modeKind, false);
1500
1501        // XXX All others should have the same restriction.
1502
if(!newMode.canContain(tcs[0])) {
1503            return;
1504        }
1505        model.addModeAroundEditor(newMode, side);
1506        
1507        attachTopComponentsHelper(tcs, newMode, fireEvents);
1508    }
1509    
1510    private void attachTopComponentsIntoNewMode(TopComponent[] tcs, Rectangle JavaDoc bounds, int modeKind, int modeState) {
1511        if(tcs == null || tcs.length == 0) {
1512            return;
1513        }
1514
1515        WindowManagerImpl wmi = WindowManagerImpl.getInstance();
1516        // New mode. It is necessary to add it yet.
1517
ModeImpl newMode = wmi.createModeImpl(
1518            ModeImpl.getUnusedModeName(), modeKind, modeState, false);
1519        newMode.setBounds(bounds);
1520        
1521        // XXX All others should have the same restriction.
1522
if(!newMode.canContain(tcs[0])) {
1523            return;
1524        }
1525        
1526        model.addMode(newMode, new SplitConstraint[] {new SplitConstraint(Constants.HORIZONTAL, 100, 0.5f)});
1527
1528        if (modeState == Constants.MODE_STATE_SEPARATED) {
1529            // for new separate modes, remember previous modes and constraints
1530
// needed for precise docking back
1531
ModeImpl prevMode;
1532            String JavaDoc tcID;
1533            int prevIndex;
1534            for (int i = 0; i < tcs.length; i++) {
1535                prevMode = (ModeImpl) wmi.findMode(tcs[i]);
1536                tcID = wmi.findTopComponentID(tcs[i]);
1537                if (prevMode.getState() == Constants.MODE_STATE_SEPARATED) {
1538                    prevMode = model.getModeTopComponentPreviousMode(prevMode, tcID);
1539                }
1540                prevIndex = prevMode.getOpenedTopComponentsIDs().indexOf( tcID );
1541                if (prevMode != null) {
1542                    model.setModeTopComponentPreviousMode(newMode, tcID, prevMode, prevIndex);
1543                    model.setModeTopComponentPreviousConstraints(newMode, wmi.findTopComponentID(tcs[i]), prevMode.getConstraints());
1544                }
1545            }
1546        }
1547
1548        attachTopComponentsHelper(tcs, newMode, true);
1549    }
1550
1551    /** Helper method. */
1552    private void attachTopComponentsHelper(TopComponent[] tcs, ModeImpl newMode, boolean fireEvents) {
1553        for(int i = 0; i < tcs.length; i++) {
1554            TopComponent tc = tcs[i];
1555            removeTopComponentFromOtherModes(newMode, tc);
1556            model.addModeOpenedTopComponent(newMode, tc);
1557        }
1558        
1559        ModeImpl oldActiveMode = getActiveMode();
1560        
1561        model.setActiveMode(newMode);
1562        model.setModeSelectedTopComponent(newMode, tcs[0]);
1563
1564        if(isVisible()) {
1565            viewRequestor.scheduleRequest(new ViewRequest(tcs[0],
1566                View.CHANGE_TOPCOMPONENT_ATTACHED, null, newMode));
1567        }
1568
1569        if(!fireEvents) {
1570            return;
1571        }
1572        
1573
1574        // Notify activated.
1575
WindowManagerImpl.notifyRegistryTopComponentActivated(tcs[0]);
1576        
1577        WindowManagerImpl.getInstance().doFirePropertyChange(
1578            WindowManager.PROP_MODES, null, null);
1579        
1580        if(oldActiveMode != newMode) {
1581            WindowManagerImpl.getInstance().doFirePropertyChange(
1582                WindowManagerImpl.PROP_ACTIVE_MODE, oldActiveMode, newMode);
1583        }
1584        
1585        
1586    }
1587    
1588    /** */
1589    public void activateModeTopComponent(ModeImpl mode, TopComponent tc) {
1590        if(!getModeOpenedTopComponents(mode).contains(tc)) {
1591            return;
1592        }
1593        
1594        ModeImpl oldActiveMode = getActiveMode();
1595        //#45650 -some API users call the activation all over again all the time on one item.
1596
// improve performance for such cases.
1597
if (oldActiveMode != null && oldActiveMode.equals(mode)) {
1598            if (tc != null && tc.equals(model.getModeSelectedTopComponent(mode))) {
1599                return;
1600            }
1601        }
1602        model.setActiveMode(mode);
1603        model.setModeSelectedTopComponent(mode, tc);
1604        
1605        if(isVisible()) {
1606            viewRequestor.scheduleRequest(new ViewRequest(mode,
1607                View.CHANGE_TOPCOMPONENT_ACTIVATED, null, tc));
1608        }
1609        
1610        // Notify registry.
1611
WindowManagerImpl.notifyRegistryTopComponentActivated(tc);
1612        
1613        if(oldActiveMode != mode) {
1614            WindowManagerImpl.getInstance().doFirePropertyChange(
1615                WindowManagerImpl.PROP_ACTIVE_MODE, oldActiveMode, mode);
1616        }
1617    }
1618    // Compound ones<<
1619

1620
1621    // Other >>
1622
public boolean isDragInProgress() {
1623        // XXX
1624
return viewRequestor.isDragInProgress();
1625    }
1626    
1627    public Frame JavaDoc getMainWindow() {
1628        // XXX
1629
return viewRequestor.getMainWindow();
1630    }
1631    
1632    public String JavaDoc guessSlideSide(TopComponent tc) {
1633        return viewRequestor.guessSlideSide(tc);
1634    }
1635    
1636    /**
1637     * Find the side (LEFT/RIGHT/BOTTOM) where the TopComponent from the given
1638     * mode should slide to.
1639     *
1640     * @param mode Mode
1641     * @return The slide side for TopComponents from the given mode.
1642     */

1643    String JavaDoc getSlideSideForMode(ModeImpl mode) {
1644        return model.getSlideSideForMode( mode );
1645    }
1646
1647    /** Tells whether given top component is inside joined mode (in main window)
1648     * or inside separate mode (separate window).
1649     *
1650     * @param the component in question
1651     * @return True when given component is docked, which means lives now
1652     * inside main window. False if component lives inside separate window.
1653     */

1654    public boolean isDocked (TopComponent comp) {
1655        ModeImpl mode = (ModeImpl)WindowManagerImpl.getInstance().findMode(comp);
1656        return mode != null && mode.getState() == Constants.MODE_STATE_JOINED;
1657    }
1658
1659    
1660    // Other <<
1661

1662    // Helper methods
1663
/** Creates model for mode, used internally. */
1664    public void createModeModel(ModeImpl mode, String JavaDoc name, int state, int kind, boolean permanent) {
1665        model.createModeModel(mode, name, state, kind, permanent);
1666    }
1667    
1668    
1669    /** Creates model for top component group, used internally. */
1670    public void createGroupModel(TopComponentGroupImpl tcGroup, String JavaDoc name, boolean opened) {
1671        model.createGroupModel(tcGroup, name, opened);
1672    }
1673    
1674    // snapshot
1675
/** Creates window system model snapshot, used for requesting view. */
1676    public WindowSystemSnapshot createWindowSystemSnapshot() {
1677        return model.createWindowSystemSnapshot();
1678    }
1679    
1680    
1681    ///////////////////////////
1682
// ControllerHandler>>
1683
public void userActivatedMode(ModeImpl mode) {
1684        if(mode != null) {
1685            setActiveMode(mode);
1686        }
1687    }
1688    
1689    public void userActivatedModeWindow(ModeImpl mode) {
1690        if(mode != null) {
1691            setActiveMode(mode);
1692        }
1693    }
1694    
1695    public void userActivatedEditorWindow() {
1696        WindowManagerImpl wm = WindowManagerImpl.getInstance();
1697        TopComponent[] tcs = wm.getRecentViewList();
1698        for(int i = 0; i < tcs.length; i++) {
1699            TopComponent tc = tcs[i];
1700            ModeImpl mode = (ModeImpl)wm.findMode(tc);
1701            if(mode != null
1702            && mode.getKind() == Constants.MODE_KIND_EDITOR
1703            && !mode.getOpenedTopComponents().isEmpty()) {
1704                setActiveMode(mode);
1705                return;
1706            }
1707        }
1708        
1709        ModeImpl mode = wm.getDefaultEditorMode();
1710        if(mode != null && !mode.getOpenedTopComponents().isEmpty()) {
1711            setActiveMode(mode);
1712        } else {
1713            // when someone calls this as a matter of activating editor mode as a fallback, but none is opened,
1714
// do unactivate the current selection.
1715
// #44389
1716
setActiveMode(null);
1717        }
1718    }
1719    
1720    public void userActivatedTopComponent(ModeImpl mode, TopComponent selected) {
1721        if(mode != null) {
1722            setModeSelectedTopComponent(mode, selected);
1723        }
1724    }
1725    
1726    public void userResizedMainWindow(Rectangle JavaDoc bounds) {
1727        if(getEditorAreaState() == Constants.EDITOR_AREA_JOINED) {
1728            model.setMainWindowBoundsJoined(bounds);
1729        } else {
1730            model.setMainWindowBoundsSeparated(bounds);
1731        }
1732    }
1733    
1734    public void userResizedMainWindowBoundsSeparatedHelp(Rectangle JavaDoc bounds) {
1735        if(getEditorAreaState() == Constants.EDITOR_AREA_JOINED
1736        && getMainWindowBoundsSeparated().isEmpty()) {
1737            model.setMainWindowBoundsUserSeparatedHelp(bounds);
1738        }
1739    }
1740    
1741    public void userResizedEditorArea(Rectangle JavaDoc bounds) {
1742        model.setEditorAreaBounds(bounds);
1743    }
1744    
1745    public void userResizedEditorAreaBoundsHelp(Rectangle JavaDoc bounds) {
1746        if(getEditorAreaState() == Constants.EDITOR_AREA_JOINED
1747        && getEditorAreaBounds().isEmpty()) {
1748            model.setEditorAreaBoundsUserHelp(bounds);
1749        }
1750    }
1751
1752    public void userResizedModeBounds(ModeImpl mode, Rectangle JavaDoc bounds) {
1753        Rectangle JavaDoc old = model.getModeBounds(mode);
1754        model.setModeBounds(mode, bounds);
1755        
1756        mode.doFirePropertyChange(ModeImpl.PROP_BOUNDS, old, bounds);
1757    }
1758    
1759    public void userResizedModeBoundsSeparatedHelp(ModeImpl mode, Rectangle JavaDoc bounds) {
1760        model.setModeBoundsSeparatedHelp(mode, bounds);
1761    }
1762    
1763    public void userChangedFrameStateMainWindow(int frameState) {
1764        if(getEditorAreaState() == Constants.EDITOR_AREA_JOINED) {
1765            model.setMainWindowFrameStateJoined(frameState);
1766        } else {
1767            model.setMainWindowFrameStateSeparated(frameState);
1768        }
1769    }
1770    
1771    public void userChangedFrameStateEditorArea(int frameState) {
1772        model.setEditorAreaFrameState(frameState);
1773    }
1774    
1775    public void userChangedFrameStateMode(ModeImpl mode, int frameState) {
1776        model.setModeFrameState(mode, frameState);
1777    }
1778    
1779    public void userChangedSplit( ModelElement[] snapshots, double[] splitWeights ) {
1780        model.setSplitWeights( snapshots, splitWeights );
1781    }
1782
1783    public void userClosedTopComponent(ModeImpl mode, TopComponent tc) {
1784// debugLog("userClosedTopComponent");
1785
if(WindowManagerImpl.getInstance().isTopComponentPersistentWhenClosed(tc)) {
1786            addModeClosedTopComponent(mode, tc);
1787        } else {
1788            removeModeTopComponent(mode, tc);
1789        }
1790
1791        // Unmaximize if necessary.
1792
if(mode.getOpenedTopComponents().isEmpty()
1793        && mode == getCurrentMaximizedMode()) {
1794            switchMaximizedMode(null);
1795        }
1796    }
1797    
1798    public void userClosedMode(ModeImpl mode) {
1799        if(mode != null) {
1800            closeMode(mode);
1801            // Unmaximize if necessary.
1802
if(mode.getOpenedTopComponents().isEmpty()
1803                && mode == getCurrentMaximizedMode())
1804            {
1805                switchMaximizedMode(null);
1806            }
1807        }
1808        
1809    }
1810    
1811    
1812    // DnD
1813
public void userDroppedTopComponents(ModeImpl mode, TopComponent[] tcs) {
1814        updateViewAfterDnD(moveTopComponentsIntoMode(mode, tcs));
1815    }
1816    
1817    public void userDroppedTopComponents(ModeImpl mode, TopComponent[] tcs, int index) {
1818        updateViewAfterDnD(moveTopComponentsIntoMode(mode, tcs, index));
1819    }
1820    
1821    public void userDroppedTopComponents(ModeImpl mode, TopComponent[] tcs, String JavaDoc side) {
1822        attachTopComponentsToSide(tcs, mode, side, false);
1823        
1824        updateViewAfterDnD(true);
1825    }
1826    
1827    public void userDroppedTopComponentsIntoEmptyEditor(TopComponent[] tcs) {
1828        // PENDING
1829
ModeImpl mode = (ModeImpl)WindowManagerImpl.getInstance().findMode("editor"); // NOI18N
1830
moveTopComponentsIntoMode(mode, tcs);
1831        updateViewAfterDnD(true);
1832    }
1833    
1834    public void userDroppedTopComponentsAround(TopComponent[] tcs, String JavaDoc side) {
1835        attachTopComponentsAroundDesktop(tcs, side, false);
1836
1837        updateViewAfterDnD(true);
1838    }
1839    
1840    public void userDroppedTopComponentsAroundEditor(TopComponent[] tcs, String JavaDoc side, int modeKind) {
1841        attachTopComponentsAroundEditor(tcs, side, false, modeKind);
1842
1843        updateViewAfterDnD(true);
1844    }
1845    
1846    public void userDroppedTopComponentsIntoFreeArea(TopComponent[] tcs, Rectangle JavaDoc bounds, int modeKind) {
1847        attachTopComponentsIntoNewMode(tcs, bounds, modeKind, Constants.MODE_STATE_SEPARATED);
1848        updateViewAfterDnD(true);
1849    }
1850
1851    public void userUndockedTopComponent(TopComponent tc, int modeKind) {
1852        Point JavaDoc tcLoc = tc.getLocation();
1853        Dimension JavaDoc tcSize = tc.getSize();
1854        SwingUtilities.convertPointToScreen(tcLoc, tc);
1855        Rectangle JavaDoc bounds = new Rectangle JavaDoc(tcLoc, tcSize);
1856        // #89100: update mode kind when undocking view in sliding mode
1857
if (modeKind == Constants.MODE_KIND_SLIDING) {
1858            modeKind = Constants.MODE_KIND_VIEW;
1859        }
1860        attachTopComponentsIntoNewMode(new TopComponent[] { tc }, bounds, modeKind, Constants.MODE_STATE_SEPARATED);
1861        updateViewAfterDnD(true);
1862    }
1863
1864    public void userDockedTopComponent(TopComponent tc, int modeKind) {
1865        ModeImpl dockTo = null;
1866        // find saved previous mode or at least constraints (=the place) to dock back into
1867
String JavaDoc tcID = WindowManagerImpl.getInstance().findTopComponentID(tc);
1868        ModeImpl source = (ModeImpl) WindowManagerImpl.getInstance().findMode(tc);
1869        dockTo = model.getModeTopComponentPreviousMode(source, tcID);
1870        int dockIndex = model.getModeTopComponentPreviousIndex(source, tcID);
1871        if ((dockTo == null) || !model.getModes().contains(dockTo)) {
1872            // mode to dock to back isn't valid anymore, try constraints
1873
SplitConstraint[] constraints = model.getModeTopComponentPreviousConstraints(source, tcID);
1874            if (constraints != null) {
1875                // create mode with the same constraints to dock topcomponent back into
1876
dockTo = WindowManagerImpl.getInstance().createModeImpl(
1877                        ModeImpl.getUnusedModeName(), modeKind, false);
1878                model.addMode(dockTo, constraints);
1879            }
1880        }
1881        if (dockTo == null) {
1882            // fallback, previous saved mode not found somehow, use default modes
1883
dockTo = modeKind == Constants.MODE_KIND_EDITOR
1884                    ? WindowManagerImpl.getInstance().getDefaultEditorMode()
1885                    : WindowManagerImpl.getInstance().getDefaultViewMode();
1886        }
1887
1888        updateViewAfterDnD(moveTopComponentsIntoMode(dockTo, new TopComponent[] { tc }, dockIndex));
1889    }
1890
1891    private boolean moveTopComponentsIntoMode(ModeImpl mode, TopComponent[] tcs) {
1892        return moveTopComponentsIntoMode(mode, tcs, -1);
1893    }
1894    
1895    private boolean moveTopComponentsIntoMode(ModeImpl mode, TopComponent[] tcs, int index) {
1896        boolean moved = false;
1897        boolean intoSliding = mode.getKind() == Constants.MODE_KIND_SLIDING;
1898        boolean intoSeparate = mode.getState() == Constants.MODE_STATE_SEPARATED;
1899        ModeImpl prevMode = null;
1900        for(int i = 0; i < tcs.length; i++) {
1901            TopComponent tc = tcs[i];
1902            String JavaDoc tcID = WindowManagerImpl.getInstance().findTopComponentID(tc);
1903            // XXX
1904
if(!mode.canContain(tc)) {
1905                continue;
1906            }
1907            for(Iterator JavaDoc it = model.getModes().iterator(); it.hasNext(); ) {
1908                ModeImpl m = (ModeImpl)it.next();
1909                if(model.containsModeTopComponent(m, tc)) {
1910                    if (m.getKind() == Constants.MODE_KIND_SLIDING ||
1911                        m.getState() == Constants.MODE_STATE_SEPARATED) {
1912                        prevMode = model.getModeTopComponentPreviousMode(m, tcID);
1913                    } else {
1914                        prevMode = m;
1915                    }
1916                    break;
1917                }
1918            }
1919            int prevIndex = prevMode != null && (intoSliding || intoSeparate) ? prevMode.getOpenedTopComponentsIDs().indexOf( tcID ) : -1;
1920            if(removeTopComponentFromOtherModes(mode, tc)) {
1921                moved = true;
1922            }
1923            if (index > -1) {
1924                model.insertModeOpenedTopComponent(mode, tc, index);
1925            } else {
1926                model.addModeOpenedTopComponent(mode, tc);
1927            }
1928            if (prevMode != null && (intoSliding || intoSeparate)) {
1929                // remember previous mode and constraints for precise de-auto-hide
1930
model.setModeTopComponentPreviousMode(mode, tcID, prevMode, prevIndex);
1931                model.setModeTopComponentPreviousConstraints(mode, tcID, model.getModeConstraints(prevMode));
1932            }
1933        }
1934        if (!intoSliding) {
1935            // make the target mode active after dragging..
1936
model.setActiveMode(mode);
1937            model.setModeSelectedTopComponent(mode, tcs[tcs.length - 1]);
1938        } else {
1939            // don't activate sliding modes, it means the component slides out, that's a bad thing..
1940
// make some other desktop mode active
1941
if(prevMode != null && prevMode == getActiveMode()
1942                   && prevMode.getOpenedTopComponents().isEmpty()) {
1943                setSomeModeActive();
1944            }
1945            // check the drop mode if it was already used, if not, assign it some reasonable size,
1946
// according to the current component.
1947
if (mode.getBounds().width == 0 && mode.getBounds().height == 0) {
1948                // now we have the sliding mode in initial state
1949
mode.setBounds(tcs[tcs.length - 1].getBounds());
1950            }
1951        }
1952        return moved;
1953    }
1954
1955    
1956    private void updateViewAfterDnD(boolean unmaximize) {
1957        if( unmaximize ) {
1958            switchMaximizedMode(null);
1959        }
1960        
1961        if(isVisible()) {
1962            viewRequestor.scheduleRequest(
1963                new ViewRequest(null, View.CHANGE_DND_PERFORMED, null, null));
1964        }
1965    }
1966
1967    
1968    // Sliding
1969

1970   /** Adds mode into model and requests view (if needed). */
1971    public void addSlidingMode(ModeImpl mode, ModeImpl original, String JavaDoc side, Map JavaDoc<String JavaDoc,Integer JavaDoc> slideInSizes) {
1972        ModeImpl targetMode = model.getSlidingMode(side);
1973        if (targetMode != null) {
1974            //TODO what to do here.. something there already
1975
return;
1976        }
1977            targetMode = WindowManagerImpl.getInstance().createModeImpl(
1978                ModeImpl.getUnusedModeName(), Constants.MODE_KIND_SLIDING, false);
1979        
1980        model.addSlidingMode(mode, side, slideInSizes);
1981        
1982        if(isVisible()) {
1983            viewRequestor.scheduleRequest(
1984                new ViewRequest(null, View.CHANGE_MODE_ADDED, null, mode));
1985        }
1986        
1987        WindowManagerImpl.getInstance().doFirePropertyChange(
1988            WindowManager.PROP_MODES, null, null);
1989    }
1990    
1991    public void userEnabledAutoHide(TopComponent tc, ModeImpl source, String JavaDoc targetSide) {
1992        
1993        String JavaDoc tcID = WindowManagerImpl.getInstance().findTopComponentID(tc);
1994        if( isEditorMaximized() )
1995            setTopComponentDockedInMaximizedMode( tcID, false );
1996    
1997        slide( tc, source, targetSide );
1998    }
1999    
2000    /**
2001     * Slide out the given TopComponent
2002     */

2003    void slide(TopComponent tc, ModeImpl source, String JavaDoc targetSide) {
2004        ModeImpl targetMode = model.getSlidingMode(targetSide);
2005        if (targetMode == null) {
2006            targetMode = WindowManagerImpl.getInstance().createModeImpl(
2007                ModeImpl.getUnusedModeName(), Constants.MODE_KIND_SLIDING, false);
2008            model.addSlidingMode(targetMode, targetSide, null);
2009            model.setModeBounds(targetMode, new Rectangle JavaDoc(tc.getBounds()));
2010        }
2011
2012        ModeImpl oldActive = getActiveMode();
2013        moveTopComponentsIntoMode(targetMode, new TopComponent[] {tc});
2014        ModeImpl newActive = getActiveMode();
2015        
2016        if(isVisible()) {
2017            viewRequestor.scheduleRequest(
2018                new ViewRequest(null, View.CHANGE_TOPCOMPONENT_AUTO_HIDE_ENABLED, null, null));
2019        }
2020        
2021        if(oldActive != newActive) {
2022            WindowManagerImpl.getInstance().doFirePropertyChange(
2023                WindowManagerImpl.PROP_ACTIVE_MODE, oldActive, newActive);
2024        }
2025    
2026        // Notify new active.
2027
if(newActive != null) {
2028            // Notify registry.
2029
WindowManagerImpl.notifyRegistryTopComponentActivated(
2030                newActive.getSelectedTopComponent());
2031        } else {
2032            WindowManagerImpl.notifyRegistryTopComponentActivated(null);
2033        }
2034    }
2035    
2036    public void userResizedSlidingMode(ModeImpl mode, Rectangle JavaDoc rect) {
2037        model.setModeBounds(mode, new Rectangle JavaDoc(rect));
2038        //remember user's settings for the slided-in TopComponent size
2039
String JavaDoc side = model.getSlidingModeConstraints( mode );
2040        model.setSlideInSize( side,
2041                mode.getSelectedTopComponent(),
2042                Constants.BOTTOM.equals( side ) ? rect.height : rect.width );
2043        if( null != mode.getSelectedTopComponent() ) {
2044            String JavaDoc tcID = WindowManagerImpl.getInstance().findTopComponentID( mode.getSelectedTopComponent() );
2045            model.setTopComponentMaximizedWhenSlidedIn( tcID, false );
2046        }
2047    }
2048    
2049    
2050    public void userDisabledAutoHide(TopComponent tc, ModeImpl source) {
2051        // unmaximize if needed
2052
if( isViewMaximized() ) {
2053            switchMaximizedMode(null);
2054        }
2055        
2056        String JavaDoc tcID = WindowManagerImpl.getInstance().findTopComponentID(tc);
2057        if( isEditorMaximized() )
2058            setTopComponentDockedInMaximizedMode( tcID, true );
2059        
2060        unSlide( tc, source );
2061    }
2062    
2063    /**
2064     * Cancel the sliding mode of the given TopComponent.
2065     */

2066    private void unSlide(TopComponent tc, ModeImpl source) {
2067        String JavaDoc tcID = WindowManagerImpl.getInstance().findTopComponentID(tc);
2068        
2069        ModeImpl targetMode = model.getModeTopComponentPreviousMode(source, tcID);
2070        int targetIndex = model.getModeTopComponentPreviousIndex(source, tcID);
2071        
2072        if ((targetMode == null) || !model.getModes().contains(targetMode)) {
2073            // mode to return to isn't valid anymore, try constraints
2074
SplitConstraint[] constraints = model.getModeTopComponentPreviousConstraints(source, tcID);
2075            if (constraints != null) {
2076                // create mode with the same constraints to dock topcomponent back into
2077
targetMode = WindowManagerImpl.getInstance().createModeImpl(
2078                        ModeImpl.getUnusedModeName(), source.getKind(), false);
2079                model.addMode(targetMode, constraints);
2080            }
2081        }
2082
2083        if (targetMode == null) {
2084            // fallback, previous saved mode not found somehow, use default modes
2085
targetMode = source.getKind() == Constants.MODE_KIND_EDITOR
2086                    ? WindowManagerImpl.getInstance().getDefaultEditorMode()
2087                    : WindowManagerImpl.getInstance().getDefaultViewMode();
2088        }
2089
2090        moveTopComponentsIntoMode(targetMode, new TopComponent[] { tc }, targetIndex);
2091        
2092        if (source.isEmpty()) {
2093            model.removeMode(source);
2094        }
2095        
2096        if(isVisible()) {
2097            viewRequestor.scheduleRequest(
2098                new ViewRequest(null, View.CHANGE_TOPCOMPONENT_AUTO_HIDE_DISABLED, null, null));
2099        }
2100        WindowManagerImpl.getInstance().doFirePropertyChange(
2101            WindowManagerImpl.PROP_ACTIVE_MODE, null, getActiveMode());
2102    }
2103  
2104    
2105    /**
2106     * @return The mode where the given TopComponent had been before it was moved to sliding or separate mode.
2107     */

2108    public ModeImpl getModeTopComponentPreviousMode(String JavaDoc tcID, ModeImpl currentSlidingMode) {
2109        return model.getModeTopComponentPreviousMode(currentSlidingMode, tcID);
2110    }
2111    
2112    /**
2113     * @return The position (tab index) of the given TopComponent before it was moved to sliding or separate mode.
2114     */

2115    public int getModeTopComponentPreviousIndex(String JavaDoc tcID, ModeImpl currentSlidingMode) {
2116        return model.getModeTopComponentPreviousIndex(currentSlidingMode, tcID);
2117    }
2118    
2119    /**
2120     * Remember the mode and position where the given TopComponent was before moving into sliding or separate mode.
2121     *
2122     * @param tcID TopComponent's id
2123     * @param currentSlidingMode The mode where the TopComponent is at the moment.
2124     * @param prevMode The mode where the TopComponent had been before it was moved to the sliding mode.
2125     * @param prevIndex Tab index of the TopComponent before it was moved to the new mode.
2126     */

2127    public void setModeTopComponentPreviousMode(String JavaDoc tcID, ModeImpl currentSlidingMode, ModeImpl prevMode, int prevIndex) {
2128        model.setModeTopComponentPreviousMode(currentSlidingMode, tcID, prevMode, prevIndex);
2129    }
2130    
2131    Map JavaDoc<String JavaDoc,Integer JavaDoc> getSlideInSizes( String JavaDoc side ) {
2132        return model.getSlideInSizes( side );
2133    }
2134    
2135    /**
2136     * Set the state of the TopComponent when the editor is maximized.
2137     *
2138     * @param tcID TopComponent id
2139     * @param docked True if the TopComponent should stay docked in maximized editor mode,
2140     * false if it should slide out when the editor is maximized.
2141     */

2142    void setTopComponentDockedInMaximizedMode( String JavaDoc tcID, boolean docked ) {
2143        if( docked )
2144            model.getMaximizedDockingStatus().addDocked( tcID );
2145        else
2146            model.getMaximizedDockingStatus().addSlided( tcID );
2147    }
2148    
2149    /**
2150     * Get the state of the TopComponent when the editor is maximized.
2151     *
2152     * @param tcID TopComponent id.
2153     * @return True if the TopComponent should stay docked in maximized editor mode,
2154     * false if it should slide out when the editor is maximized.
2155     */

2156    boolean isTopComponentDockedInMaximizedMode( String JavaDoc tcID ) {
2157        return model.getMaximizedDockingStatus().isDocked( tcID );
2158    }
2159    
2160    /**
2161     * Set the state of the TopComponent when no mode is maximized.
2162     *
2163     * @param tcID TopComponent id
2164     * @param slided True if the TopComponent is slided in the default mode,
2165     * false if it is docked.
2166     */

2167    void setTopComponentSlidedInDefaultMode( String JavaDoc tcID, boolean slided ) {
2168        if( slided )
2169            model.getDefaultDockingStatus().addSlided( tcID );
2170        else
2171            model.getDefaultDockingStatus().addDocked( tcID );
2172    }
2173    
2174    /**
2175     * Get the state of the TopComponent when it is slided-in.
2176     *
2177     * @param tcID TopComponent id.
2178     * @return true if the TopComponent is maximized when slided-in.
2179     */

2180    boolean isTopComponentMaximizedWhenSlidedIn( String JavaDoc tcID ) {
2181        return model.isTopComponentMaximizedWhenSlidedIn( tcID );
2182    }
2183    
2184    /**
2185     * Set the state of the TopComponent when it is slided-in.
2186     *
2187     * @param tcID TopComponent id.
2188     * @param maximized true if the TopComponent is maximized when slided-in.
2189     */

2190    void setTopComponentMaximizedWhenSlidedIn( String JavaDoc tcID, boolean maximized ) {
2191        model.setTopComponentMaximizedWhenSlidedIn( tcID, maximized );
2192    }
2193    
2194    void userToggledTopComponentSlideInMaximize( String JavaDoc tcID ) {
2195        setTopComponentMaximizedWhenSlidedIn( tcID, !isTopComponentMaximizedWhenSlidedIn( tcID ) );
2196        if( isVisible() ) {
2197            TopComponent tc = WindowManagerImpl.getInstance().findTopComponent( tcID );
2198            ModeImpl mode = WindowManagerImpl.getInstance().findModeForOpenedID( tcID );
2199            if( null != tc && null != mode && null != mode.getSide() ) {
2200                viewRequestor.scheduleRequest (
2201                    new ViewRequest(tc, View.CHANGE_MAXIMIZE_TOPCOMPONENT_SLIDE_IN, null, mode.getSide()));
2202            }
2203        }
2204    }
2205    
2206    /**
2207     * Get the state of the TopComponent when no mode is maximized.
2208     *
2209     * @param tcID TopComponent id.
2210     * @return True if the TopComponent is slided in the default mode,
2211     * false if it is docked.
2212     */

2213    boolean isTopComponentSlidedInDefaultMode( String JavaDoc tcID ) {
2214        return model.getDefaultDockingStatus().isSlided( tcID );
2215    }
2216    
2217    boolean isEditorMaximized() {
2218        return null != model.getEditorMaximizedMode();
2219    }
2220    
2221    boolean isViewMaximized() {
2222        return null != model.getViewMaximizedMode();
2223    }
2224    
2225    /**
2226     * Slide-out or dock opened TopComponent according to their previous state.
2227     */

2228    private void restoreViews( DockingStatus viewStatus ) {
2229        WindowManagerImpl wm = WindowManagerImpl.getInstance();
2230        Set JavaDoc<? extends Mode> modes = getModes();
2231        for( Iterator JavaDoc<? extends Mode> i=modes.iterator(); i.hasNext(); ) {
2232            ModeImpl modeImpl = (ModeImpl)i.next();
2233            if( modeImpl.getState() == Constants.MODE_STATE_SEPARATED )
2234                continue;
2235            
2236            if( modeImpl.getKind() == Constants.MODE_KIND_VIEW ) {
2237                List JavaDoc<TopComponent> views = getModeOpenedTopComponents( modeImpl );
2238                Collections.reverse( views );
2239                for( Iterator JavaDoc<TopComponent> j=views.iterator(); j.hasNext(); ) {
2240                    TopComponent tc = j.next();
2241                    String JavaDoc tcID = wm.findTopComponentID( tc );
2242                    if( viewStatus.shouldSlide( tcID ) ) {
2243                        slide( tc, modeImpl, guessSlideSide( tc ) );
2244                    }
2245                }
2246            } else if( modeImpl.getKind() == Constants.MODE_KIND_SLIDING ) {
2247                List JavaDoc<TopComponent> views = getModeOpenedTopComponents( modeImpl );
2248                Collections.reverse( views );
2249                for( Iterator JavaDoc<TopComponent> j=views.iterator(); j.hasNext(); ) {
2250                    TopComponent tc = j.next();
2251                    String JavaDoc tcID = wm.findTopComponentID( tc );
2252                    if( viewStatus.shouldDock( tcID ) ) {
2253                        unSlide( tc, modeImpl );
2254                    }
2255                }
2256            }
2257        }
2258        
2259        //now that all views are slided/restore make sure the right views are selected in each mode
2260
for( Iterator JavaDoc<? extends Mode> i=modes.iterator(); i.hasNext(); ) {
2261            ModeImpl modeImpl = (ModeImpl)i.next();
2262            if( modeImpl.getState() == Constants.MODE_STATE_SEPARATED )
2263                continue;
2264            
2265            if( modeImpl.getKind() == Constants.MODE_KIND_VIEW ) {
2266                //make sure that the same view is selected as before
2267
TopComponent prevActiveTc = modeImpl.getPreviousSelectedTopComponent();
2268                if( null != prevActiveTc ) {
2269                    setModeSelectedTopComponent( modeImpl, prevActiveTc );
2270                }
2271            }
2272        }
2273    }
2274    
2275    /**
2276     * Slide out all non-editor TopComponents.
2277     */

2278    private void slideAllViews() {
2279        Set JavaDoc<? extends Mode> modes = getModes();
2280        for( Iterator JavaDoc<? extends Mode> i=modes.iterator(); i.hasNext(); ) {
2281            ModeImpl modeImpl = (ModeImpl)i.next();
2282            if( modeImpl.getKind() == Constants.MODE_KIND_VIEW
2283                    && modeImpl != getViewMaximizedMode()
2284                    && modeImpl.getState() != Constants.MODE_STATE_SEPARATED ) {
2285                List JavaDoc<TopComponent> views = getModeOpenedTopComponents( modeImpl );
2286                Collections.reverse( views );
2287                for( Iterator JavaDoc<TopComponent> j=views.iterator(); j.hasNext(); ) {
2288                    TopComponent tc = j.next();
2289                    slide( tc, modeImpl, guessSlideSide( tc ) );
2290                }
2291            }
2292        }
2293    }
2294    
2295    // ControllerHandler <<
2296
////////////////////////////
2297

2298    private static void debugLog(String JavaDoc message) {
2299        Debug.log(Central.class, message);
2300    }
2301}
2302
Popular Tags