~ubuntu-branches/ubuntu/quantal/netbeans/quantal

« back to all changes in this revision

Viewing changes to core/windows/src/org/netbeans/core/windows/model/ModesSubModel.java

  • Committer: Bazaar Package Importer
  • Author(s): Marek Slama
  • Date: 2008-01-29 14:11:22 UTC
  • Revision ID: james.westby@ubuntu.com-20080129141122-fnzjbo11ntghxfu7
Tags: upstream-6.0.1
ImportĀ upstreamĀ versionĀ 6.0.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 
3
 *
 
4
 * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
 
5
 *
 
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]"
 
23
 *
 
24
 * Contributor(s):
 
25
 *
 
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.
 
29
 *
 
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.
 
40
 */
 
41
 
 
42
package org.netbeans.core.windows.model;
 
43
 
 
44
import java.util.Collections;
 
45
import java.util.HashMap;
 
46
import java.util.Iterator;
 
47
import java.util.Map;
 
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;
 
52
 
 
53
import java.util.HashSet;
 
54
import java.util.Set;
 
55
import org.netbeans.core.windows.WindowManagerImpl;
 
56
import org.openide.windows.TopComponent;
 
57
 
 
58
 
 
59
/**
 
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.)
 
66
 *
 
67
 * @author  Peter Zavadsky
 
68
 */
 
69
final class ModesSubModel {
 
70
 
 
71
    /** Associated parent model. */
 
72
    private final Model parentModel;
 
73
    
 
74
    /** Set of modes. */
 
75
    private final Set<ModeImpl> modes = new HashSet<ModeImpl>(10);
 
76
 
 
77
    /** Represents split model of modes, also contains special editor area. */
 
78
    private final EditorSplitSubModel editorSplitSubModel;
 
79
   
 
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);
 
84
 
 
85
    /** Active mode. */
 
86
    private ModeImpl activeMode;
 
87
    /** Maximized mode. */
 
88
    private ModeImpl editorMaximizedMode;
 
89
    private ModeImpl viewMaximizedMode;
 
90
    
 
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);
 
94
     
 
95
    
 
96
    /** Creates a new instance of ModesModel */
 
97
    public ModesSubModel(Model parentModel) {
 
98
        this.parentModel = parentModel;
 
99
 
 
100
        this.editorSplitSubModel = new EditorSplitSubModel(parentModel, new SplitSubModel(parentModel));
 
101
    }
 
102
 
 
103
    
 
104
    public void setEditorAreaConstraints(SplitConstraint[] editorAreaConstraints) {
 
105
        editorSplitSubModel.setEditorNodeConstraints(editorAreaConstraints);
 
106
    }
 
107
    
 
108
    public SplitConstraint[] getModelElementConstraints(ModelElement element) {
 
109
        return editorSplitSubModel.getModelElementConstraints(element);
 
110
    }
 
111
    
 
112
    public SplitConstraint[] getEditorAreaConstraints() {
 
113
        return editorSplitSubModel.getEditorNodeConstraints();
 
114
    }
 
115
    
 
116
    public SplitConstraint[] getModeConstraints(ModeImpl mode) {
 
117
        if(mode.getKind() == Constants.MODE_KIND_EDITOR) {
 
118
            return editorSplitSubModel.getEditorArea().getModeConstraints(mode);
 
119
        } else {
 
120
            return editorSplitSubModel.getModeConstraints(mode);
 
121
        }
 
122
    }
 
123
    
 
124
    /**
 
125
     * Find the side (LEFT/RIGHT/BOTTOM) where the TopComponent from the given
 
126
     * mode should slide to.
 
127
     * 
 
128
     * @param mode Mode
 
129
     * @return The slide side for TopComponents from the given mode.
 
130
     */
 
131
    public String getSlideSideForMode( ModeImpl mode ) {
 
132
        return editorSplitSubModel.getSlideSideForMode( mode );
 
133
    }
 
134
    
 
135
    public String getSlidingModeConstraints(ModeImpl mode) {
 
136
        return slidingModes2Sides.get(mode);
 
137
    }
 
138
    
 
139
    public ModeImpl getSlidingMode(String side) {
 
140
        return slidingSides2Modes.get(side);
 
141
    }
 
142
    
 
143
    public Set<ModeImpl> getSlidingModes() {
 
144
        return Collections.unmodifiableSet(slidingModes2Sides.keySet());
 
145
    }
 
146
 
 
147
    public boolean addMode(ModeImpl mode, SplitConstraint[] constraints) {
 
148
        if(modes.contains(mode)) {
 
149
            return false;
 
150
        }
 
151
 
 
152
        boolean result;
 
153
        if(mode.getKind() == Constants.MODE_KIND_EDITOR &&
 
154
           mode.getState() == Constants.MODE_STATE_JOINED) {
 
155
            result = editorSplitSubModel.getEditorArea().addMode(mode, constraints);
 
156
        } else {
 
157
            result = editorSplitSubModel.addMode(mode, constraints);
 
158
        }
 
159
 
 
160
        if(result) {
 
161
            modes.add(mode);
 
162
        }
 
163
        return result;
 
164
    }
 
165
    
 
166
    // XXX
 
167
    public boolean addModeToSide(ModeImpl mode, ModeImpl attachMode, String side) {
 
168
        if(modes.contains(mode)) {
 
169
            return false;
 
170
        }
 
171
 
 
172
        boolean result;
 
173
        // XXX PENDING
 
174
        if(mode.getKind() == Constants.MODE_KIND_EDITOR) {
 
175
            result = editorSplitSubModel.getEditorArea().addModeToSide(mode, attachMode, side);
 
176
        } else {
 
177
            result = editorSplitSubModel.addModeToSide(mode, attachMode, side);
 
178
        }
 
179
 
 
180
        if(result) {
 
181
            modes.add(mode);
 
182
        }
 
183
        return result;
 
184
    }
 
185
    
 
186
    // XXX
 
187
    public boolean addModeAround(ModeImpl mode, String side) {
 
188
        if(modes.contains(mode)) {
 
189
            return false;
 
190
        }
 
191
 
 
192
        boolean result;
 
193
        // XXX PENDING
 
194
        if(mode.getKind() == Constants.MODE_KIND_EDITOR) {
 
195
            result = false;
 
196
        } else {
 
197
            result = editorSplitSubModel.addModeAround(mode, side);
 
198
        }
 
199
 
 
200
        if(result) {
 
201
            modes.add(mode);
 
202
        }
 
203
        return result;
 
204
    }
 
205
    
 
206
    // XXX
 
207
    public boolean addModeAroundEditor(ModeImpl mode, String side) {
 
208
        if(modes.contains(mode)) {
 
209
            return false;
 
210
        }
 
211
 
 
212
        boolean result;
 
213
        // XXX PENDING
 
214
        if(mode.getKind() == Constants.MODE_KIND_EDITOR) {
 
215
            result = editorSplitSubModel.getEditorArea().addModeToSideRoot(mode, side);
 
216
        } else {
 
217
            result = editorSplitSubModel.addModeAroundEditor(mode, side);
 
218
        }
 
219
 
 
220
        if(result) {
 
221
            modes.add(mode);
 
222
        }
 
223
        return result;
 
224
    }
 
225
    
 
226
    public boolean addModeSliding(ModeImpl mode, String side, Map<String,Integer> slideInSizes) {
 
227
        if(modes.contains(mode) || (mode.getKind() != Constants.MODE_KIND_SLIDING)) {
 
228
            return false;
 
229
        }
 
230
 
 
231
        slidingModes2Sides.put(mode, side);
 
232
        slidingSides2Modes.put(side, mode);
 
233
        
 
234
        modes.add(mode);
 
235
        
 
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 ) );
 
240
            }
 
241
        }
 
242
        
 
243
        return true;
 
244
    }
 
245
 
 
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 );
 
254
            }
 
255
        }
 
256
        return res;
 
257
    }
 
258
    
 
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 );
 
266
            if( null != size ) {
 
267
                res.put( tc, size );
 
268
            }
 
269
        }
 
270
        return res;
 
271
    }
 
272
    
 
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) );
 
277
        }
 
278
    }
 
279
    
 
280
    public boolean removeMode(ModeImpl mode) {
 
281
        int kind = mode.getKind();
 
282
        if (kind == Constants.MODE_KIND_SLIDING) {
 
283
            return true;
 
284
            // don't remove the sliding modes, to make dnd easier..
 
285
//            slidingSides2Modes.remove(side);
 
286
//            return slidingModes2Sides.remove(mode) != null;
 
287
        }
 
288
        modes.remove(mode);
 
289
        if(mode.getKind() == Constants.MODE_KIND_EDITOR) {
 
290
            return editorSplitSubModel.getEditorArea().removeMode(mode);
 
291
        } else {
 
292
            return editorSplitSubModel.removeMode(mode);
 
293
        }
 
294
    }
 
295
    
 
296
    /** Sets active mode. */
 
297
    public boolean setActiveMode(ModeImpl activeMode) {
 
298
        if(activeMode == null || modes.contains(activeMode)) {
 
299
            this.activeMode = activeMode;
 
300
            return true;
 
301
        }
 
302
        
 
303
        return false;
 
304
    }
 
305
    
 
306
    /** Gets acitve mode. */
 
307
    public ModeImpl getActiveMode() {
 
308
        return this.activeMode;
 
309
    }
 
310
    
 
311
    /** Sets maximized mode for editor components. */
 
312
    public boolean setEditorMaximizedMode(ModeImpl maximizedMode) {
 
313
        if(maximizedMode == null || modes.contains(maximizedMode)) {
 
314
            this.editorMaximizedMode = maximizedMode;
 
315
            return true;
 
316
        }
 
317
        
 
318
        return false;
 
319
    }
 
320
    
 
321
    /** Gets maximized mode for editor components. */
 
322
    public ModeImpl getEditorMaximizedMode() {
 
323
        return this.editorMaximizedMode;
 
324
    }
 
325
    
 
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;
 
330
            return true;
 
331
        }
 
332
        
 
333
        return false;
 
334
    }
 
335
    
 
336
    /** Gets maximized mode for non-editor components. */
 
337
    public ModeImpl getViewMaximizedMode() {
 
338
        return this.viewMaximizedMode;
 
339
    }
 
340
 
 
341
    public Set<ModeImpl> getModes() {
 
342
        return new HashSet<ModeImpl>(modes);
 
343
    }
 
344
    
 
345
    public void setSplitWeights( ModelElement[] snapshots, double[] splitWeights ) {
 
346
        editorSplitSubModel.setSplitWeights( snapshots, splitWeights );
 
347
    }
 
348
    
 
349
    public String toString() {
 
350
        return getClass().getName() + "@" + Integer.toHexString(hashCode()) // NOI18N
 
351
            + "\n" + editorSplitSubModel; // NOI18N
 
352
    }
 
353
 
 
354
    /////////////////////////////////////////////
 
355
    // used when creating snapshot of this model.
 
356
    public ModeStructureSnapshot.ElementSnapshot createSplitSnapshot() {
 
357
        return editorSplitSubModel.createSplitSnapshot();
 
358
    }
 
359
 
 
360
    /** Set of mode element snapshots. */
 
361
    public Set<ModeStructureSnapshot.ModeSnapshot> createSeparateModeSnapshots() {
 
362
        Set<ModeStructureSnapshot.ModeSnapshot> s = 
 
363
                new HashSet<ModeStructureSnapshot.ModeSnapshot>();
 
364
        
 
365
        s.addAll(editorSplitSubModel.createSeparateSnapshots());
 
366
        
 
367
        return s;
 
368
    }
 
369
    
 
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())));
 
377
        }
 
378
        
 
379
        return result;
 
380
    }
 
381
    
 
382
    ////////////////////////////////////////////
 
383
 
 
384
    
 
385
    //////////////////////////////
 
386
    // Controller updates >>
 
387
    
 
388
    public ModeImpl getModeForOriginator(ModelElement originator) {
 
389
        ModeImpl mode = editorSplitSubModel.getModeForOriginator(originator);
 
390
        
 
391
        if(modes.contains(mode)) {
 
392
            return mode;
 
393
        } else {
 
394
            return null;
 
395
        }
 
396
    }
 
397
    
 
398
    // Controller updates <<
 
399
    ///////////////////////////////
 
400
 
 
401
}
 
402