KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > editor > fold > FoldOperationImpl


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.modules.editor.fold;
21
22 import javax.swing.event.DocumentEvent JavaDoc;
23 import javax.swing.text.BadLocationException JavaDoc;
24 import javax.swing.text.Document JavaDoc;
25 import org.netbeans.spi.editor.fold.FoldManager;
26 import org.netbeans.api.editor.fold.Fold;
27 import org.netbeans.api.editor.fold.FoldHierarchy;
28 import org.netbeans.spi.editor.fold.FoldOperation;
29 import org.netbeans.api.editor.fold.FoldType;
30
31
32 /**
33  * This is SPI (Service Provider Interface) object corresponding
34  * to the <code>FoldHierarchy</code> in one-to-one relationship.
35  * <br>
36  * The <code>FoldHierarchy</code> delegates all its operations
37  * to this object.
38  *
39  * <p>
40  * All the changes performed in to the folds are always done
41  * in terms of a transaction represented by {@link FoldHierarchyTransaction}.
42  * The transaction can be opened by {@link #openTransaction()}.
43  *
44  * <p>
45  * This class changes its state upon displayability change
46  * of the associated copmonent by listening on "ancestor" component property.
47  * <br>
48  * If the component is not displayable then the list of root folds becomes empty
49  * while if the component gets displayable the root folds are created
50  * according to registered managers.
51  *
52  * @author Miloslav Metelka
53  * @version 1.00
54  */

55
56 public final class FoldOperationImpl {
57     
58     private static final boolean debug
59         = Boolean.getBoolean("netbeans.debug.editor.fold");
60     
61     /** Dump the stacktraces for certain opertaions
62      * such as creating of the folds.
63      * Has only effect with the "debug" turned on.
64      */

65     private static final boolean debugStack
66         = Boolean.getBoolean("netbeans.debug.editor.fold.stack");
67     
68     
69     private FoldOperation operation;
70     
71     private FoldHierarchyExecution execution;
72     
73     private FoldManager manager;
74     
75     private int priority;
76     
77     private boolean released;
78     
79     public FoldOperationImpl(FoldHierarchyExecution execution,
80     FoldManager manager, int priority) {
81         this.execution = execution;
82         this.manager = manager;
83         this.priority = priority;
84
85         this.operation = SpiPackageAccessor.get().createFoldOperation(this);
86         if (manager != null) { // manager for root-fold is null
87
manager.init(getOperation());
88         }
89     }
90     
91     public FoldOperation getOperation() {
92         return operation;
93     }
94     
95     public void initFolds(FoldHierarchyTransactionImpl transaction) {
96         manager.initFolds(transaction.getTransaction());
97     }
98     
99     public FoldHierarchy getHierarchy() {
100         return execution.getHierarchy();
101     }
102     
103     public FoldManager getManager() {
104         return manager;
105     }
106     
107     public int getPriority() {
108         return priority;
109     }
110     
111     public Document JavaDoc getDocument() {
112         return execution.getComponent().getDocument();
113     }
114     
115     public Fold createFold(FoldType type, String JavaDoc description, boolean collapsed,
116     int startOffset, int endOffset, int startGuardedLength, int endGuardedLength,
117     Object JavaDoc extraInfo)
118     throws BadLocationException JavaDoc {
119         if (debug) {
120             /*DEBUG*/System.err.println("Creating fold: type=" + type // NOI18N
121
+ ", description='" + description + "', collapsed=" + collapsed // NOI18N
122
+ ", startOffset=" + startOffset + ", endOffset=" + endOffset // NOI18N
123
+ ", startGuardedLength=" + startGuardedLength // NOI18N
124
+ ", endGuardedLength=" + endGuardedLength // NOI18N
125
+ ", extraInfo=" + extraInfo // NOI18N
126
);
127             
128             if (debugStack) {
129                 /*DEBUG*/Thread.dumpStack();
130             }
131         }
132
133         return getAccessor().createFold(this,
134             type, description, collapsed,
135             getDocument(), startOffset, endOffset,
136             startGuardedLength, endGuardedLength,
137             extraInfo
138         );
139     }
140     
141     public Object JavaDoc getExtraInfo(Fold fold) {
142         checkFoldOperation(fold);
143         return getAccessor().foldGetExtraInfo(fold);
144     }
145     
146     public boolean isStartDamaged(Fold fold) {
147         checkFoldOperation(fold);
148         return getAccessor().foldIsStartDamaged(fold);
149     }
150     
151     public boolean isEndDamaged(Fold fold) {
152         checkFoldOperation(fold);
153         return getAccessor().foldIsEndDamaged(fold);
154     }
155     
156     public FoldHierarchyTransactionImpl openTransaction() {
157         return execution.openTransaction();
158     }
159     
160     public boolean addToHierarchy(Fold fold, FoldHierarchyTransactionImpl transaction) {
161         checkFoldOperation(fold);
162         return execution.add(fold, transaction);
163     }
164
165     public void removeFromHierarchy(Fold fold, FoldHierarchyTransactionImpl transaction) {
166         checkFoldOperation(fold);
167         execution.remove(fold, transaction);
168     }
169     
170     public boolean isAddedOrBlocked(Fold fold) {
171         checkFoldOperation(fold);
172         return execution.isAddedOrBlocked(fold);
173     }
174     
175     public boolean isBlocked(Fold fold) {
176         checkFoldOperation(fold);
177         return execution.isBlocked(fold);
178     }
179     
180     public void setEndOffset(Fold fold, int endOffset, FoldHierarchyTransactionImpl transaction)
181     throws BadLocationException JavaDoc {
182         checkFoldOperation(fold);
183         int origEndOffset = fold.getEndOffset();
184         ApiPackageAccessor api = getAccessor();
185         api.foldSetEndOffset(fold, getDocument(), endOffset);
186         api.foldStateChangeEndOffsetChanged(transaction.getFoldStateChange(fold), origEndOffset);
187     }
188     
189     public void insertUpdate(DocumentEvent JavaDoc evt, FoldHierarchyTransactionImpl transaction) {
190         manager.insertUpdate(evt, transaction.getTransaction());
191     }
192     
193     public void removeUpdate(DocumentEvent JavaDoc evt, FoldHierarchyTransactionImpl transaction) {
194         manager.removeUpdate(evt, transaction.getTransaction());
195     }
196     
197     public void changedUpdate(DocumentEvent JavaDoc evt, FoldHierarchyTransactionImpl transaction) {
198         manager.changedUpdate(evt, transaction.getTransaction());
199     }
200
201     public void release() {
202         released = true;
203         manager.release();
204     }
205     
206     public boolean isReleased() {
207         return released;
208     }
209
210     private void checkFoldOperation(Fold fold) {
211         FoldOperationImpl foldOperation = getAccessor().foldGetOperation(fold);
212         if (foldOperation != this) {
213             throw new IllegalStateException JavaDoc(
214                 "Attempt to use the fold " + fold // NOI18N
215
+ " with invalid fold operation " // NOI18N
216
+ foldOperation + " instead of " + this // NOI18N
217
);
218         }
219     }
220     
221     private static ApiPackageAccessor getAccessor() {
222         return ApiPackageAccessor.get();
223     }
224
225 }
226
Popular Tags