KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > api > editor > guards > GuardedSectionManager


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.api.editor.guards;
21
22 import javax.swing.text.BadLocationException JavaDoc;
23 import javax.swing.text.Position JavaDoc;
24 import javax.swing.text.StyledDocument JavaDoc;
25 import org.netbeans.modules.editor.guards.GuardedSectionImpl;
26 import org.netbeans.modules.editor.guards.GuardedSectionsImpl;
27 import org.netbeans.modules.editor.guards.GuardsAccessor;
28 import org.netbeans.modules.editor.guards.InteriorSectionImpl;
29 import org.netbeans.modules.editor.guards.SimpleSectionImpl;
30
31 /**
32  * This is the entry point for clients to manipulate guarded sections
33  * of the given document.
34  *
35  * @author Jan Pokorsky
36  */

37 public final class GuardedSectionManager {
38
39     /**
40      * Gets the manager instance.
41      * @param doc a document containing guarded sections
42      * @return the manager instance or <code>null</code>.
43      */

44     public static GuardedSectionManager getInstance(StyledDocument JavaDoc doc) {
45         return (GuardedSectionManager) doc.getProperty(GuardedSectionManager.class);
46     }
47
48     /**
49      * Tries to find the simple section of the given name.
50      * @param name the name of the requested section
51      * @return the found guarded section or <code>null</code> if there is no section
52      * of the given name
53      */

54     public SimpleSection findSimpleSection(String JavaDoc name) {
55         GuardedSection s = impl.findSection(name);
56         return (s instanceof SimpleSection) ? (SimpleSection) s : null;
57     }
58
59     /**
60      * Tries to find the interior section of the given name.
61      * @param name the name of the looked-for section
62      * @return the found guarded section or <code>null</code> if there is no section
63      * of the given name
64      */

65     public InteriorSection findInteriorSection(String JavaDoc name) {
66         GuardedSection s = impl.findSection(name);
67         return (s instanceof InteriorSection) ? (InteriorSection) s : null;
68     }
69     
70     /**
71      * Creates an empty simple section at the given position.
72      * The position must not be within any existing guarded section
73      * and the passed name must not be registered for other
74      * already existing section. The created section will initially contain
75      * one space and a newline.
76      * @return SimpleSection instance that can be used for generating text into
77      * the protected region
78      * @throws IllegalArgumentException if either the name has been already used, or
79      * the position is inside another section or Java Element.
80      * @throws BadLocationException if pos is outside of document's scope, or
81      * the document does not permit creating the guarded section.
82      */

83     public SimpleSection createSimpleSection(Position JavaDoc pos, String JavaDoc name)
84             throws IllegalArgumentException JavaDoc, BadLocationException JavaDoc {
85         return impl.createSimpleSection(pos, name);
86     }
87
88     /**
89      * Creates an empty interior section at the given position.
90      * The position must not be within any existing guarded section
91      * and the passed name must not be registered to other
92      * already existing section. The created section will initially contain
93      * one space and a newline in all its parts (header, body and footer).
94      * @return InteriorSection instance that can be used for generating text into
95      * the protected region
96      * @throws IllegalArgumentException if either the name has been already used, or
97      * the position is inside another section or Java Element.
98      * @throws BadLocationException if pos is outside of document's scope, or
99      * the document does not permit creating the guarded section.
100      */

101     public InteriorSection createInteriorSection(Position JavaDoc pos, String JavaDoc name)
102             throws IllegalArgumentException JavaDoc, BadLocationException JavaDoc {
103         return impl.createInteriorSection(pos, name);
104     }
105     
106     /** Gets all sections.
107      * @return an iterable over {@link GuardedSection}s
108      */

109     public Iterable JavaDoc<GuardedSection> getGuardedSections() {
110         return impl.getGuardedSections();
111     }
112     
113     // package
114

115     // private
116

117     static {
118         GuardsAccessor.DEFAULT = new GuardsAccessor() {
119             public GuardedSectionManager createGuardedSections(GuardedSectionsImpl impl) {
120                 return new GuardedSectionManager(impl);
121             }
122
123             public SimpleSection createSimpleSection(SimpleSectionImpl impl) {
124                 return new SimpleSection(impl);
125             }
126
127             public InteriorSection createInteriorSection(InteriorSectionImpl impl) {
128                 return new InteriorSection(impl);
129             }
130             
131             public GuardedSectionImpl getImpl(GuardedSection gs) {
132                 return gs.getImpl();
133             }
134             
135         };
136     }
137     
138     /** Creates a new instance of GuardedDocument */
139     private GuardedSectionManager(GuardedSectionsImpl impl) {
140         this.impl = impl;
141     }
142
143     private final GuardedSectionsImpl impl;
144
145 }
146
Popular Tags