KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > editor > FoldMultiLineView


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.editor;
21
22 import java.util.ArrayList JavaDoc;
23 import java.util.List JavaDoc;
24 import javax.swing.text.BadLocationException JavaDoc;
25 import javax.swing.text.Element JavaDoc;
26 import javax.swing.text.JTextComponent JavaDoc;
27 import javax.swing.text.Position JavaDoc;
28 import javax.swing.text.View JavaDoc;
29 import javax.swing.text.ViewFactory JavaDoc;
30 import org.netbeans.api.editor.fold.Fold;
31 import org.netbeans.editor.Utilities;
32 import org.netbeans.lib.editor.view.GapMultiLineView;
33
34 /**
35  * Possibly multi-line view containing one or more folds
36  *
37  * @author Miloslav Metelka
38  * @version 1.00
39  */

40
41 class FoldMultiLineView extends GapMultiLineView {
42     
43     /**
44      * List containing pairs [fold, ending-line-elem]
45      * where ending-line-elem is a line element
46      * in which the fold ends.
47      */

48     private List JavaDoc foldAndEndLineElemList;
49     
50     FoldMultiLineView(Element JavaDoc firstLineElement, List JavaDoc foldAndEndLineElemList) {
51         super(firstLineElement);
52         this.foldAndEndLineElemList = foldAndEndLineElemList;
53         
54         int foldAndEndLineElemListSize = foldAndEndLineElemList.size();
55 // TODO uncomment assert (foldAndEndLineElemListSize != 0 // non-empty
56
// && ((foldAndEndLineElemListSize & 1) == 0)); // even
57

58         setLastLineElement((Element JavaDoc)foldAndEndLineElemList.get(
59             foldAndEndLineElemList.size() - 1));
60     }
61
62     private JTextComponent JavaDoc getTextComponent() {
63         return (JTextComponent JavaDoc)getContainer();
64     }
65     
66     protected boolean useCustomReloadChildren() {
67         return true;
68     }
69     
70     protected void reloadChildren(int index, int removeLength, int startOffset, int endOffset) {
71         // TODO uncomment assert (index == 0 && removeLength == 0
72
// && startOffset == getStartOffset() && endOffset == getEndOffset());
73

74         // Rebuild all the present child views completely
75
index = 0;
76         removeLength = getViewCount();
77
78         Element JavaDoc lineElem = getElement(); // starting line element
79
View JavaDoc[] added = null;
80         ViewFactory JavaDoc f = getViewFactory();
81         if (f != null) {
82             int lineElemEndOffset = lineElem.getEndOffset();
83             // Ending offset of the previously created view - here start with
84
// begining of the first line
85
int lastViewEndOffset = lineElem.getStartOffset();
86
87             int cnt = foldAndEndLineElemList.size();
88             List JavaDoc childViews = new ArrayList JavaDoc();
89             for (int i = 0; i < cnt; i++) {
90                 Fold fold = (Fold)foldAndEndLineElemList.get(i);
91                 int foldStartOffset = fold.getStartOffset();
92                 int foldEndOffset = fold.getEndOffset();
93
94                 BaseDocument doc = (BaseDocument) lineElem.getDocument();
95                 try {
96                     if (foldStartOffset > lastViewEndOffset) { // need to insert intra-line fragment
97
View JavaDoc lineView = f.create(lineElem); // normal line view
98
View JavaDoc intraFrag = lineView.createFragment(lastViewEndOffset, foldStartOffset);
99                         childViews.add(intraFrag);
100                         lastViewEndOffset = foldStartOffset;
101                     }
102
103                     // Create collapsed view
104
Position JavaDoc viewStartPos = doc.createPosition(foldStartOffset);
105                     Position JavaDoc viewEndPos = doc.createPosition(foldEndOffset);
106                     CollapsedView collapsedView = new CollapsedView(lineElem,
107                         viewStartPos, viewEndPos, fold.getDescription());
108                     childViews.add(collapsedView);
109                     lastViewEndOffset = foldEndOffset;
110
111                 } catch (BadLocationException JavaDoc e) {
112                     throw new IllegalStateException JavaDoc("Failed to create view boundary positions"); // NOI18N
113
}
114
115                 // Fetch line element where the fold ends
116
i++;
117                 lineElem = (Element JavaDoc)foldAndEndLineElemList.get(i);
118                 lineElemEndOffset = lineElem.getEndOffset();
119             }
120
121             // Append ending fragment if necessary
122
// asserted non-empty list => foldEndOffset populated
123
if (lastViewEndOffset < lineElemEndOffset) { // need ending fragment
124
View JavaDoc lineView = f.create(lineElem);
125                 View JavaDoc endingFrag = lineView.createFragment(lastViewEndOffset, lineElemEndOffset);
126                 childViews.add(endingFrag);
127                 // lastViewEndOffset = lineElemEndOffset; <- can be ignored here
128
}
129
130             added = new View JavaDoc[childViews.size()];
131             childViews.toArray(added);
132         }
133
134         
135         replace(index, removeLength, added);
136     }
137     
138 }
139
Popular Tags