KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > spi > editor > guards > support > AbstractGuardedSectionsProvider


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.spi.editor.guards.support;
21
22 import java.io.InputStream JavaDoc;
23 import java.io.OutputStream JavaDoc;
24 import java.io.Reader JavaDoc;
25 import java.io.UnsupportedEncodingException JavaDoc;
26 import java.io.Writer JavaDoc;
27 import java.util.List JavaDoc;
28 import javax.swing.text.BadLocationException JavaDoc;
29 import org.netbeans.api.editor.guards.GuardedSection;
30 import org.netbeans.api.editor.guards.InteriorSection;
31 import org.netbeans.api.editor.guards.SimpleSection;
32 import org.netbeans.modules.editor.guards.GuardedSectionsImpl;
33 import org.netbeans.modules.editor.guards.PositionBounds;
34 import org.netbeans.spi.editor.guards.GuardedEditorSupport;
35 import org.netbeans.spi.editor.guards.GuardedSectionsProvider;
36
37 /**
38  * The helper class that simplifies writing particular {@link GuardedSectionsProvider}
39  * implementations. Subclasses have to implement just {@link #readSections}
40  * and {@link #writeSections} methods.
41  *
42  * @author Jan Pokorsky
43  */

44 public abstract class AbstractGuardedSectionsProvider implements GuardedSectionsProvider {
45
46     private final GuardedSectionsImpl impl;
47     
48     protected AbstractGuardedSectionsProvider(GuardedEditorSupport editor) {
49         this.impl = new GuardedSectionsImpl(editor);
50     }
51
52     public final Reader JavaDoc createGuardedReader(InputStream JavaDoc stream, String JavaDoc encoding) throws UnsupportedEncodingException JavaDoc {
53         return impl.createGuardedReader(this, stream, encoding);
54     }
55
56     public final Writer JavaDoc createGuardedWriter(OutputStream JavaDoc stream, String JavaDoc encoding) throws UnsupportedEncodingException JavaDoc {
57         return impl.createGuardedWriter(this, stream, encoding);
58     }
59     
60     /**
61      * This should be implemented to persist a list of guarded sections inside
62      * the passed content.
63      * @param sections guarded sections to persist
64      * @param content content
65      * @return content including guarded sections
66      */

67     public abstract char[] writeSections(List JavaDoc<GuardedSection> sections, char[] content);
68     
69     /**
70      * This should be implemented to extract guarded sections out of the passed
71      * content.
72      * @param content content including guarded sections
73      * @return the content that will be presented to users and the list of guarded sections
74      */

75     public abstract Result readSections(char[] content);
76     
77     /**
78      * Creates a simple section object to represent section read by
79      * the {@link #readSections readSections}.
80      * @param name the section name
81      * @param begin the start offset
82      * @param end the end offset
83      * @return the simple section instance
84      * @exception BadLocationException if the given position does not
85      * represent a valid location in the associated document
86      */

87     public final SimpleSection createSimpleSection(String JavaDoc name, int begin, int end) throws BadLocationException JavaDoc {
88         return impl.createSimpleSectionObject(name, PositionBounds.createUnresolved(begin, end, impl));
89     }
90     
91     /**
92      * Creates an interior section object to represent section read by
93      * the {@link #readSections readSections}.
94      * @param name the section name
95      * @param headerBegin begin the start offset of the first guarded part
96      * @param headerEnd end the end offset of the first guarded part
97      * @param footerBegin begin the start offset of the second guarded part
98      * @param footerEnd end the end offset of the second guarded part
99      * @return the interior section object
100      * @exception BadLocationException if the given position does not
101      * represent a valid location in the associated document
102      */

103     public final InteriorSection createInteriorSection(String JavaDoc name, int headerBegin, int headerEnd, int footerBegin, int footerEnd) throws BadLocationException JavaDoc {
104         return impl.createInteriorSectionObject(
105                 name,
106                 PositionBounds.createUnresolved(headerBegin, headerEnd, impl),
107                 PositionBounds.createBodyUnresolved(headerEnd + 1, footerBegin - 1, impl),
108                 PositionBounds.createUnresolved(footerBegin, footerEnd, impl)
109                 );
110     }
111     
112     public final class Result {
113
114         private final char[] content;
115
116         private final List JavaDoc<GuardedSection> sections;
117         
118         public Result (char[] content, List JavaDoc<GuardedSection> sections) {
119             this.content = content;
120             this.sections = sections;
121         }
122         
123         public char[] getContent() {
124             return this.content;
125         }
126         
127         public List JavaDoc<GuardedSection> getGuardedSections() {
128             return this.sections;
129         }
130     }
131 }
132
Popular Tags