KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > editor > guards > GuardedSectionImpl


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.guards;
21
22 import java.beans.PropertyChangeEvent JavaDoc;
23 import java.beans.PropertyVetoException JavaDoc;
24 import javax.swing.text.BadLocationException JavaDoc;
25 import javax.swing.text.Position JavaDoc;
26 import javax.swing.text.StyledDocument JavaDoc;
27 import org.netbeans.api.editor.guards.GuardedSection;
28 import org.openide.text.NbDocument;
29
30 /** Represents one guarded section.
31  */

32 public abstract class GuardedSectionImpl {
33     /** Name of the section. */
34     String JavaDoc name;
35     
36     /** If the section is valid or if it was removed. */
37     boolean valid = false;
38     
39     final GuardedSectionsImpl guards;
40     
41     GuardedSection guard;
42     
43     /** Get the name of the section.
44      * @return the name
45      */

46     public String JavaDoc getName() {
47         return name;
48     }
49     
50     /** Creates new section.
51      * @param name Name of the new section.
52      */

53     GuardedSectionImpl(String JavaDoc name, GuardedSectionsImpl guards) {
54         this.name = name;
55         this.guards = guards;
56     }
57     
58     public final void attach(GuardedSection guard) {
59         this.guard = guard;
60         valid = true;
61     }
62     
63     /** Set the name of the section.
64      * @param name the new name
65      * @exception PropertyVetoException if the new name is already in use
66      */

67     public void setName(String JavaDoc name) throws PropertyVetoException JavaDoc {
68         if (!this.name.equals(name)) {
69             synchronized (this.guards.sections) {
70                 if (valid) {
71                     if (this.guards.sections.get(name) != null)
72                         throw new PropertyVetoException JavaDoc("", new PropertyChangeEvent JavaDoc(this, "name", this.name, name)); // NOI18N
73
this.guards.sections.remove(this.name);
74                     this.name = name;
75                     this.guards.sections.put(name, this);
76                 }
77             }
78         }
79         
80     }
81     
82     /** Deletes the text of the section and
83      * removes it from the table. The section will then be invalid
84      * and it will be impossible to use its methods.
85      */

86     public void deleteSection() {
87         synchronized (this.guards.sections) {
88             if (valid) {
89                 try {
90                     this.guards.sections.remove(name);
91                     // get document should always return the document, when section
92
// is deleted, because it is still valid (and valid is only
93
// when document is loaded.
94
unmarkGuarded(this.guards.getDocument());
95                     deleteText();
96                     valid = false;
97                 } catch (BadLocationException JavaDoc e) {
98                     throw new IllegalStateException JavaDoc(e);
99                 }
100             }
101         }
102     }
103     
104     /**
105      * Tests if the section is still valid - it is not removed from the
106      * source.
107      */

108     public boolean isValid() {
109         return valid;
110     }
111     
112     /**
113      * Removes the section from the Document, but retains the text contained
114      * within. The method should be used to unprotect a region of code
115      * instead of calling NbDocument.
116      * @return true if the operation succeeded.
117      */

118     public void removeSection() {
119         synchronized (this.guards.sections) {
120             if (valid) {
121                 this.guards.sections.remove(name);
122                 // get document should always return the document, when section
123
// is deleted, because it is still valid (and valid is only
124
// when document is loaded.
125
unmarkGuarded(this.guards.getDocument());
126                 valid = false;
127             }
128         }
129     }
130     
131     /** Set the text contained in this section.
132      * Newlines are automatically added to all text segments handled,
133      * unless there was already one.
134      * All guarded blocks must consist of entire lines.
135      * This applies to the contents of specific guard types as well.
136      * @param bounds the bounds indicating where the text should be set
137      * @param text the new text
138      * @param minLen If true the text has to have length more than 2 chars.
139      * @return <code>true</code> if the operation was successful, otherwise <code>false</code>
140      */

141     protected boolean setText(PositionBounds bounds, String JavaDoc text, boolean minLen) {
142         if (!valid)
143             return false;
144         
145         // modify the text - has to contain at least a space and the length
146
// has to be at least 1 character
147
if (minLen) {
148             if (text.length() == 0 || text.length() == 1 && text.equals("\n"))
149                 text = " "; // NOI18N
150
}
151         
152         if (text.endsWith("\n")) // NOI18N
153
text = text.substring(0, text.length() - 1);
154         
155         try {
156             bounds.setText(text);
157             return true;
158         } catch (BadLocationException JavaDoc e) {
159         }
160         return false;
161     }
162     
163     /** Marks or unmarks the section as guarded.
164      * @param doc The styled document where this section placed in.
165      * @param bounds The rangeof text which should be marked or unmarked.
166      * @param mark true means mark, false unmark.
167      */

168     void markGuarded(StyledDocument JavaDoc doc, PositionBounds bounds, boolean mark) {
169         int begin = bounds.getBegin().getOffset();
170         int end = bounds.getEnd().getOffset();
171         if (mark) {
172             NbDocument.markGuarded(doc, begin, end - begin + 1);
173         } else
174             NbDocument.unmarkGuarded(doc, begin, end - begin + 1);
175     }
176     
177     /** Marks the section as guarded.
178      * @param doc The styled document where this section placed in.
179      */

180     abstract void markGuarded(StyledDocument JavaDoc doc);
181     
182     /** Unmarks the section as guarded.
183      * @param doc The styled document where this section placed in.
184      */

185     abstract void unmarkGuarded(StyledDocument JavaDoc doc);
186     
187     /** Deletes the text in the section.
188      * @exception BadLocationException
189      */

190     final void deleteText() throws BadLocationException JavaDoc {
191         if (valid) {
192             final StyledDocument JavaDoc doc = guards.getDocument();
193             final BadLocationException JavaDoc[] blex = new BadLocationException JavaDoc[1];
194             NbDocument.runAtomic(doc, new Runnable JavaDoc() {
195                 public void run() {
196                     try {
197                         int start = getStartPosition().getOffset();
198                         if (start > 0 && "\n".equals(doc.getText(start - 1, 1))) { // NOI18N
199
start--;
200                         }
201                         doc.remove(start, getEndPosition().getOffset() - start + 1);
202                     } catch (BadLocationException JavaDoc ex) {
203                         blex[0] = ex;
204                     }
205                 }
206             });
207             
208             if (blex[0] != null) {
209                 throw blex[0];
210             }
211         }
212     }
213     
214     /** Gets the begin of section. To this position is set the caret
215      * when section is open in the editor.
216      */

217     public abstract Position JavaDoc getCaretPosition();
218     
219     /** Gets the text contained in the section.
220      * @return The text contained in the section.
221      */

222     public abstract String JavaDoc getText();
223
224     /** Assures that a position is not inside the guarded section. Complex guarded sections
225      * that contain portions of editable text can return true if the tested position is
226      * inside one of such portions provided that permitHoles is true.
227      * @param pos position in question
228      * @param permitHoles if false, guarded section is taken as a monolithic block
229      * without any holes in it regardless of its complexity.
230      */

231     public abstract boolean contains(Position JavaDoc pos, boolean permitHoles);
232     /** Returns a position after the whole guarded block that is safe for insertions.
233      */

234     public abstract Position JavaDoc getEndPosition();
235     /** Returns position before the whole guarded block that is safe for insertions.
236      */

237     public abstract Position JavaDoc getStartPosition();
238     
239     public abstract void resolvePositions() throws BadLocationException JavaDoc;
240
241 }
242
Popular Tags