KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openide > text > IndentEngine


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 package org.openide.text;
20
21 import org.openide.ServiceType;
22 import org.openide.util.HelpCtx;
23 import org.openide.util.Lookup;
24
25 import java.io.Writer JavaDoc;
26
27 import java.util.Collections JavaDoc;
28 import java.util.Enumeration JavaDoc;
29 import java.util.HashMap JavaDoc;
30 import java.util.Map JavaDoc;
31
32 import javax.swing.text.*;
33
34
35 /** Indentation engine for formating text in documents.
36 * Provides mapping between MIME types and engines, so anybody
37 * can find appropriate type of engine for type of document.
38 *
39 * @author Jaroslav Tulach
40 */

41 public abstract class IndentEngine extends ServiceType {
42     static final long serialVersionUID = -8548906260608507035L;
43
44     /** hashtable mapping MIME type to engine */
45     private static Map JavaDoc<String JavaDoc,IndentEngine> map = new HashMap JavaDoc<String JavaDoc,IndentEngine>(7);
46     private static IndentEngine INSTANCE = null;
47
48     public HelpCtx getHelpCtx() {
49         return HelpCtx.DEFAULT_HELP;
50     }
51
52     /** Indents the current line. Should not effect any other
53     * lines.
54     * @param doc the document to work on
55     * @param offset the offset of a character on the line
56     * @return new offset of the original character
57     */

58     public abstract int indentLine(Document doc, int offset);
59
60     /** Inserts new line at given position and indents the new line with
61     * spaces.
62     *
63     * @param doc the document to work on
64     * @param offset the offset of a character on the line
65     * @return new offset to place cursor to
66     */

67     public abstract int indentNewLine(Document doc, int offset);
68
69     /** Creates writer that formats text that is inserted into it.
70     * The writer should not modify the document but use the
71     * provided writer to write to. Usually the underlaying writer
72     * will modify the document itself and optionally it can remember
73     * the current position in document. That is why the newly created
74     * writer should do no buffering.
75     * <P>
76     * The provided document and offset are only informational,
77     * should not be modified but only used to find correct indentation
78     * strategy.
79     *
80     * @param doc document
81     * @param offset position to begin inserts at
82     * @param writer writer to write to
83     * @return new writer that will format written text and pass it
84     * into the writer
85     */

86     public abstract Writer JavaDoc createWriter(Document doc, int offset, Writer JavaDoc writer);
87
88     /** Allow subclasses to decide whether they are suitable
89      * for the given mime-type or not.
90      * @param mime mime-type string
91      * @return true if this engine is suitable for the given mime-type.
92      */

93     protected boolean acceptMimeType(String JavaDoc mime) {
94         return false;
95     }
96
97     /**
98      * @deprecated IndentEngine now is a ServiceType
99      */

100     @Deprecated JavaDoc
101     public synchronized static void register(String JavaDoc mime, IndentEngine eng) {
102         map.put(mime, eng);
103     }
104
105     /** Returns enumeration of all registered indentation engines.
106      * @return enumeration of IndentEngine
107      */

108     public static Enumeration JavaDoc<? extends IndentEngine> indentEngines() {
109         return Collections.enumeration(Lookup.getDefault().lookupAll(IndentEngine.class));
110     }
111
112     /** Finds engine associated with given mime type.
113     * If no engine is associated returns default one.
114     */

115     public synchronized static IndentEngine find(String JavaDoc mime) {
116         Enumeration JavaDoc<? extends IndentEngine> en = indentEngines();
117
118         while (en.hasMoreElements()) {
119             IndentEngine eng = en.nextElement();
120
121             if (eng.acceptMimeType(mime)) {
122                 return eng;
123             }
124         }
125
126         IndentEngine eng = map.get(mime);
127
128         if (eng != null) {
129             return eng;
130         }
131
132         return getDefault();
133     }
134
135     /** Finds engine associated with given document.
136     * If no engine is associated returns default one.
137     */

138     public static IndentEngine find(Document doc) {
139         Object JavaDoc o = doc.getProperty("indentEngine"); // NOI18N
140

141         if (o instanceof IndentEngine) {
142             return (IndentEngine) o;
143         } else {
144             o = doc.getProperty("mimeType"); // NOI18N
145

146             String JavaDoc s = (o instanceof String JavaDoc) ? (String JavaDoc) o : "text/plain"; // NOI18N
147

148             return find(s);
149         }
150     }
151
152     /** Returns a simple indentation engine that does no formatting. */
153     public static synchronized IndentEngine getDefault() {
154         if (INSTANCE == null) {
155             INSTANCE = new Default();
156         }
157
158         return INSTANCE;
159     }
160
161     /** Default indentation engine.
162     */

163     private static final class Default extends IndentEngine {
164         private static final long serialVersionUID = 4493180326470838469L;
165
166         Default() {
167         }
168
169         public int indentLine(Document doc, int offset) {
170             return offset;
171         }
172
173         public int indentNewLine(Document doc, int offset) {
174             try {
175                 doc.insertString(offset, "\n", null); // NOI18N
176
} catch (BadLocationException ex) {
177                 // ignore
178
}
179
180             return offset + 1;
181         }
182
183         public Writer JavaDoc createWriter(Document doc, int offset, Writer JavaDoc writer) {
184             return writer;
185         }
186
187         protected boolean acceptMimeType(String JavaDoc mime) {
188             return true;
189         }
190
191         public HelpCtx getHelpCtx() {
192             return new HelpCtx(Default.class);
193         }
194     }
195 }
196
Popular Tags