2
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
4
* Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
6
* The contents of this file are subject to the terms of either the GNU
7
* General Public License Version 2 only ("GPL") or the Common
8
* Development and Distribution License("CDDL") (collectively, the
9
* "License"). You may not use this file except in compliance with the
10
* License. You can obtain a copy of the License at
11
* http://www.netbeans.org/cddl-gplv2.html
12
* or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
13
* specific language governing permissions and limitations under the
14
* License. When distributing the software, include this License Header
15
* Notice in each file and include the License file at
16
* nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
17
* particular file as subject to the "Classpath" exception as provided
18
* by Sun in the GPL Version 2 section of the License file that
19
* accompanied this code. If applicable, add the following below the
20
* License Header, with the fields enclosed by brackets [] replaced by
21
* your own identifying information:
22
* "Portions Copyrighted [year] [name of copyright owner]"
26
* The Original Software is NetBeans. The Initial Developer of the Original
27
* Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
28
* Microsystems, Inc. All Rights Reserved.
30
* If you wish your version of this file to be governed by only the CDDL
31
* or only the GPL Version 2, indicate your decision by adding
32
* "[Contributor] elects to include this software in this distribution
33
* under the [CDDL or GPL Version 2] license." If you do not indicate a
34
* single choice of license, a recipient has the option to distribute
35
* your version of this file under either the CDDL, the GPL Version 2 or
36
* to extend the choice of license to its licensees as provided above.
37
* However, if you add GPL Version 2 code and therefore, elected the GPL
38
* Version 2 license, then the option applies only if the new code is
39
* made subject to such option by the copyright holder.
42
package org.netbeans.core.windows.model;
44
import java.util.Collections;
45
import java.util.HashMap;
46
import java.util.Iterator;
48
import org.netbeans.core.windows.Constants;
49
import org.netbeans.core.windows.ModeImpl;
50
import org.netbeans.core.windows.ModeStructureSnapshot;
51
import org.netbeans.core.windows.SplitConstraint;
53
import java.util.HashSet;
55
import org.netbeans.core.windows.WindowManagerImpl;
56
import org.openide.windows.TopComponent;
60
* Sub-model which keeps modes data strucute, i.e. as split and also separate
61
* ones. Note, it keeps editor ones in another field of split model, for better manipulation.
62
* See UI spec about editor area and editor/view component types.
63
* Note: this instances aren't thread safe, thus they has to be properly synchronized.
64
* The client has to synchronize it.
65
* (see in DefaultModel, where it is only place supposed to be used.)
67
* @author Peter Zavadsky
69
final class ModesSubModel {
71
/** Associated parent model. */
72
private final Model parentModel;
75
private final Set<ModeImpl> modes = new HashSet<ModeImpl>(10);
77
/** Represents split model of modes, also contains special editor area. */
78
private final EditorSplitSubModel editorSplitSubModel;
80
/** Sliding modes model, <ModeImpl, String> mapping
81
of mode and side of presence */
82
private final HashMap<ModeImpl, String> slidingModes2Sides = new HashMap<ModeImpl, String>(5);
83
private final HashMap<String, ModeImpl> slidingSides2Modes = new HashMap<String, ModeImpl>(5);
86
private ModeImpl activeMode;
87
/** Maximized mode. */
88
private ModeImpl editorMaximizedMode;
89
private ModeImpl viewMaximizedMode;
91
// (sliding side + TopComponent ID) -> size in pixels (width or height
92
//depending on the sliding side)
93
private final Map<String,Integer> slideInSizes = new HashMap<String,Integer>(15);
96
/** Creates a new instance of ModesModel */
97
public ModesSubModel(Model parentModel) {
98
this.parentModel = parentModel;
100
this.editorSplitSubModel = new EditorSplitSubModel(parentModel, new SplitSubModel(parentModel));
104
public void setEditorAreaConstraints(SplitConstraint[] editorAreaConstraints) {
105
editorSplitSubModel.setEditorNodeConstraints(editorAreaConstraints);
108
public SplitConstraint[] getModelElementConstraints(ModelElement element) {
109
return editorSplitSubModel.getModelElementConstraints(element);
112
public SplitConstraint[] getEditorAreaConstraints() {
113
return editorSplitSubModel.getEditorNodeConstraints();
116
public SplitConstraint[] getModeConstraints(ModeImpl mode) {
117
if(mode.getKind() == Constants.MODE_KIND_EDITOR) {
118
return editorSplitSubModel.getEditorArea().getModeConstraints(mode);
120
return editorSplitSubModel.getModeConstraints(mode);
125
* Find the side (LEFT/RIGHT/BOTTOM) where the TopComponent from the given
126
* mode should slide to.
129
* @return The slide side for TopComponents from the given mode.
131
public String getSlideSideForMode( ModeImpl mode ) {
132
return editorSplitSubModel.getSlideSideForMode( mode );
135
public String getSlidingModeConstraints(ModeImpl mode) {
136
return slidingModes2Sides.get(mode);
139
public ModeImpl getSlidingMode(String side) {
140
return slidingSides2Modes.get(side);
143
public Set<ModeImpl> getSlidingModes() {
144
return Collections.unmodifiableSet(slidingModes2Sides.keySet());
147
public boolean addMode(ModeImpl mode, SplitConstraint[] constraints) {
148
if(modes.contains(mode)) {
153
if(mode.getKind() == Constants.MODE_KIND_EDITOR &&
154
mode.getState() == Constants.MODE_STATE_JOINED) {
155
result = editorSplitSubModel.getEditorArea().addMode(mode, constraints);
157
result = editorSplitSubModel.addMode(mode, constraints);
167
public boolean addModeToSide(ModeImpl mode, ModeImpl attachMode, String side) {
168
if(modes.contains(mode)) {
174
if(mode.getKind() == Constants.MODE_KIND_EDITOR) {
175
result = editorSplitSubModel.getEditorArea().addModeToSide(mode, attachMode, side);
177
result = editorSplitSubModel.addModeToSide(mode, attachMode, side);
187
public boolean addModeAround(ModeImpl mode, String side) {
188
if(modes.contains(mode)) {
194
if(mode.getKind() == Constants.MODE_KIND_EDITOR) {
197
result = editorSplitSubModel.addModeAround(mode, side);
207
public boolean addModeAroundEditor(ModeImpl mode, String side) {
208
if(modes.contains(mode)) {
214
if(mode.getKind() == Constants.MODE_KIND_EDITOR) {
215
result = editorSplitSubModel.getEditorArea().addModeToSideRoot(mode, side);
217
result = editorSplitSubModel.addModeAroundEditor(mode, side);
226
public boolean addModeSliding(ModeImpl mode, String side, Map<String,Integer> slideInSizes) {
227
if(modes.contains(mode) || (mode.getKind() != Constants.MODE_KIND_SLIDING)) {
231
slidingModes2Sides.put(mode, side);
232
slidingSides2Modes.put(side, mode);
236
if( null != slideInSizes ) {
237
for( Iterator<String> i=slideInSizes.keySet().iterator(); i.hasNext(); ) {
238
String tcId = i.next();
239
this.slideInSizes.put( side+tcId, slideInSizes.get( tcId ) );
246
public Map<String, Integer> getSlideInSizes(String side) {
247
Map<String,Integer> res = new HashMap<String,Integer>( 5 );
248
for( Iterator<String> i=slideInSizes.keySet().iterator(); i.hasNext(); ) {
249
String key = i.next();
250
if( key.startsWith( side ) ) {
251
String tcId = key.substring( side.length() );
252
Integer size = slideInSizes.get( key );
253
res.put( tcId, size );
259
public Map<TopComponent, Integer> getSlideInSizes(ModeImpl mode) {
260
WindowManagerImpl wm = WindowManagerImpl.getInstance();
261
TopComponent[] tcs = mode.getTopComponents();
262
Map<TopComponent,Integer> res = new HashMap<TopComponent,Integer>( tcs.length );
263
for( TopComponent tc : tcs ) {
264
String tcId = wm.findTopComponentID( tc );
265
Integer size = slideInSizes.get( mode.getSide() + tcId );
273
public void setSlideInSize(String side, TopComponent tc, int size) {
274
if( null != tc && null != side ) {
275
String tcId = WindowManagerImpl.getInstance().findTopComponentID(tc);
276
slideInSizes.put( side+tcId, new Integer(size) );
280
public boolean removeMode(ModeImpl mode) {
281
int kind = mode.getKind();
282
if (kind == Constants.MODE_KIND_SLIDING) {
284
// don't remove the sliding modes, to make dnd easier..
285
// slidingSides2Modes.remove(side);
286
// return slidingModes2Sides.remove(mode) != null;
289
if(mode.getKind() == Constants.MODE_KIND_EDITOR) {
290
return editorSplitSubModel.getEditorArea().removeMode(mode);
292
return editorSplitSubModel.removeMode(mode);
296
/** Sets active mode. */
297
public boolean setActiveMode(ModeImpl activeMode) {
298
if(activeMode == null || modes.contains(activeMode)) {
299
this.activeMode = activeMode;
306
/** Gets acitve mode. */
307
public ModeImpl getActiveMode() {
308
return this.activeMode;
311
/** Sets maximized mode for editor components. */
312
public boolean setEditorMaximizedMode(ModeImpl maximizedMode) {
313
if(maximizedMode == null || modes.contains(maximizedMode)) {
314
this.editorMaximizedMode = maximizedMode;
321
/** Gets maximized mode for editor components. */
322
public ModeImpl getEditorMaximizedMode() {
323
return this.editorMaximizedMode;
326
/** Sets maximized mode for non-editor components. */
327
public boolean setViewMaximizedMode(ModeImpl maximizedMode) {
328
if(maximizedMode == null || modes.contains(maximizedMode)) {
329
this.viewMaximizedMode = maximizedMode;
336
/** Gets maximized mode for non-editor components. */
337
public ModeImpl getViewMaximizedMode() {
338
return this.viewMaximizedMode;
341
public Set<ModeImpl> getModes() {
342
return new HashSet<ModeImpl>(modes);
345
public void setSplitWeights( ModelElement[] snapshots, double[] splitWeights ) {
346
editorSplitSubModel.setSplitWeights( snapshots, splitWeights );
349
public String toString() {
350
return getClass().getName() + "@" + Integer.toHexString(hashCode()) // NOI18N
351
+ "\n" + editorSplitSubModel; // NOI18N
354
/////////////////////////////////////////////
355
// used when creating snapshot of this model.
356
public ModeStructureSnapshot.ElementSnapshot createSplitSnapshot() {
357
return editorSplitSubModel.createSplitSnapshot();
360
/** Set of mode element snapshots. */
361
public Set<ModeStructureSnapshot.ModeSnapshot> createSeparateModeSnapshots() {
362
Set<ModeStructureSnapshot.ModeSnapshot> s =
363
new HashSet<ModeStructureSnapshot.ModeSnapshot>();
365
s.addAll(editorSplitSubModel.createSeparateSnapshots());
370
public Set<ModeStructureSnapshot.SlidingModeSnapshot> createSlidingModeSnapshots() {
371
Set<ModeStructureSnapshot.SlidingModeSnapshot> result =
372
new HashSet<ModeStructureSnapshot.SlidingModeSnapshot>();
373
for (Map.Entry<ModeImpl, String> curEntry: slidingModes2Sides.entrySet()) {
374
result.add(new ModeStructureSnapshot.SlidingModeSnapshot(
375
curEntry.getKey(), curEntry.getValue(),
376
getSlideInSizes(curEntry.getKey())));
382
////////////////////////////////////////////
385
//////////////////////////////
386
// Controller updates >>
388
public ModeImpl getModeForOriginator(ModelElement originator) {
389
ModeImpl mode = editorSplitSubModel.getModeForOriginator(originator);
391
if(modes.contains(mode)) {
398
// Controller updates <<
399
///////////////////////////////