KickJava   Java API By Example, From Geeks To Geeks.

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


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.util.logging.Level JavaDoc;
23 import java.util.logging.Logger JavaDoc;
24 import javax.swing.text.BadLocationException JavaDoc;
25 import javax.swing.text.Position JavaDoc;
26 import javax.swing.text.StyledDocument JavaDoc;
27
28 /**
29  * Represents a simple guarded section.
30  * It consists of one contiguous block.
31  */

32 public final class SimpleSectionImpl extends GuardedSectionImpl {
33     /** Text range of the guarded section. */
34     private PositionBounds bounds;
35
36     /**
37      * Creates new section.
38      * @param name Name of the new section.
39      * @param bounds The range of the section.
40      */

41     SimpleSectionImpl(String JavaDoc name, PositionBounds bounds, GuardedSectionsImpl guards) {
42         super(name, guards);
43         this.bounds = bounds;
44     }
45
46     /**
47      * Set the text of the section.
48      * @param text the new text
49      */

50     public void setText(String JavaDoc text) {
51         setText(bounds, text, true);
52     }
53
54     void markGuarded(StyledDocument JavaDoc doc) {
55         markGuarded(doc, bounds, true);
56     }
57
58     /**
59      * Unmarks the section as guarded.
60      * @param doc The styled document where this section placed in.
61      */

62     void unmarkGuarded(StyledDocument JavaDoc doc) {
63         markGuarded(doc, bounds, false);
64     }
65
66     public Position JavaDoc getCaretPosition() {
67         return bounds.getBegin();
68     }
69
70     public String JavaDoc getText() {
71         String JavaDoc text = ""; // NOI18N
72
try {
73             text = bounds.getText();
74         } catch (BadLocationException JavaDoc ex) {
75             // ignore
76
Logger.getLogger("guards").log(Level.ALL, null, ex);
77         }
78         return text;
79     }
80
81     /*
82     public String toString() {
83       StringBuffer buf = new StringBuffer("SimpleSection:"+name); // NOI18N
84       buf.append("\"");
85       try {
86         buf.append(bounds.getText());
87       }
88       catch (Exception e) {
89         buf.append("EXCEPTION:"); // NOI18N
90         buf.append(e.getMessage());
91       }
92       buf.append("\"");
93       return buf.toString();
94     }*/

95
96     public Position JavaDoc getEndPosition() {
97         return bounds.getEnd();
98     }
99
100     public boolean contains(Position JavaDoc pos, boolean allowHoles) {
101         return bounds.getBegin().getOffset() <= pos.getOffset() &&
102                 bounds.getEnd().getOffset() >= pos.getOffset();
103     }
104
105     public Position JavaDoc getStartPosition() {
106         return bounds.getBegin();
107     }
108
109     public void resolvePositions() throws BadLocationException JavaDoc {
110         bounds.resolvePositions();
111     }
112 }
113
Popular Tags