KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > core > windows > model > EditorSplitSubModel


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

19
20
21 package org.netbeans.core.windows.model;
22
23
24 import java.util.ArrayList JavaDoc;
25 import org.netbeans.core.windows.Constants;
26 import org.netbeans.core.windows.ModeStructureSnapshot;
27 import org.netbeans.core.windows.SplitConstraint;
28
29
30 /**
31  * Model whidh represents sub model of split modes. It adds special notion
32  * of positioning of editor area. The editor area itself is represented
33  * by exact instance of SplitSubModel.
34  *
35  * @author Peter Zavadsky
36  */

37 final class EditorSplitSubModel extends SplitSubModel {
38
39     /** Only instance of EditorNode representing position
40      * of editor area in this sub model. */

41     private final EditorNode editorNode;
42
43
44     public EditorSplitSubModel(Model parentModel, SplitSubModel editorArea) {
45         super(parentModel);
46         
47         this.editorNode = new EditorNode(editorArea);
48         
49         // XXX The editor node has to be always present.
50
addNodeToTree(editorNode, new SplitConstraint[0]);
51     }
52     
53
54     /** Overrides superclass method to prevent removing of editor node. */
55     protected boolean removeNodeFromTree(Node node) {
56         if(node == editorNode) {
57             // XXX Prevents removing of editor node.
58
return false;
59         }
60         
61         return super.removeNodeFromTree(node);
62     }
63
64     public boolean setEditorNodeConstraints(SplitConstraint[] editorNodeConstraints) {
65         super.removeNodeFromTree(editorNode);
66         return addNodeToTree(editorNode, editorNodeConstraints);
67     }
68     
69     public SplitConstraint[] getEditorNodeConstraints() {
70         return editorNode.getNodeConstraints();
71     }
72     
73     public SplitSubModel getEditorArea() {
74         return editorNode.getEditorArea();
75     }
76
77     public boolean setSplitWeights( ModelElement[] snapshots, double[] splitWeights) {
78         if( super.setSplitWeights( snapshots, splitWeights ) ) {
79             return true;
80         }
81         
82         return getEditorArea().setSplitWeights( snapshots, splitWeights );
83     }
84     
85     
86     /** Class which represents editor area position in EditorSplitSubModel. */
87     static class EditorNode extends SplitSubModel.Node {
88         /** Ref to editor area. */
89         private final SplitSubModel editorArea;
90         
91         /** Creates a new instance of EditorNode */
92         public EditorNode(SplitSubModel editorArea) {
93             this.editorArea = editorArea;
94         }
95
96         public boolean isVisibleInSplit() {
97             return true;
98         }
99         
100         public SplitSubModel getEditorArea() {
101             return editorArea;
102         }
103         
104         public double getResizeWeight() {
105             return 1D;
106         }
107         
108         public ModeStructureSnapshot.ElementSnapshot createSnapshot() {
109             return new ModeStructureSnapshot.EditorSnapshot(this, null,
110                 editorArea.createSplitSnapshot(), getResizeWeight());
111         }
112     } // End of nested EditorNode class.
113

114     
115     // XXX
116
protected boolean addNodeToTreeAroundEditor(Node addingNode, String JavaDoc side) {
117         // Update
118
Node attachNode = editorNode;
119         if(attachNode == root) {
120             int addingIndex = (side == Constants.TOP || side == Constants.LEFT) ? 0 : -1;
121             int oldIndex = addingIndex == 0 ? -1 : 0;
122             // Create new branch.
123
int orientation = (side == Constants.TOP || side == Constants.BOTTOM) ? Constants.VERTICAL : Constants.HORIZONTAL;
124             SplitNode newSplit = new SplitNode(orientation);
125             newSplit.setChildAt(addingIndex, Constants.DROP_TO_SIDE_RATIO, addingNode);
126             newSplit.setChildAt(oldIndex, 1D - Constants.DROP_TO_SIDE_RATIO, attachNode);
127             root = newSplit;
128         } else {
129             SplitNode parent = attachNode.getParent();
130             if(parent == null) {
131                 return false;
132             }
133
134             int attachIndex = parent.getChildIndex(attachNode);
135             double attachWeight = parent.getChildSplitWeight(attachNode);
136             // Create new branch.
137
int orientation = (side == Constants.TOP || side == Constants.BOTTOM) ? Constants.VERTICAL : Constants.HORIZONTAL;
138             SplitNode newSplit = new SplitNode(orientation);
139             parent.removeChild(attachNode);
140             int addingIndex = (side == Constants.TOP || side == Constants.LEFT) ? 0 : -1;
141             int oldIndex = addingIndex == 0 ? -1 : 0;
142             newSplit.setChildAt(addingIndex, Constants.DROP_AROUND_RATIO, addingNode);
143             newSplit.setChildAt(oldIndex, 1D - Constants.DROP_AROUND_RATIO, attachNode);
144             parent.setChildAt(attachIndex, attachWeight, newSplit);
145         }
146         
147         return true;
148     }
149
150 }
151
152
Popular Tags