KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > editor > java > JavaIndentEngine


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.java;
21
22 import java.io.*;
23 import javax.swing.text.Document JavaDoc;
24 import org.netbeans.editor.ext.ExtFormatter;
25 import org.netbeans.editor.ext.java.JavaFormatter;
26 import org.netbeans.editor.ext.java.JavaSettingsNames;
27 import org.netbeans.editor.ext.java.JavaSettingsDefaults;
28 import org.netbeans.modules.editor.EditorModule;
29 import org.netbeans.modules.editor.FormatterIndentEngine;
30 import org.openide.text.IndentEngine;
31
32 /**
33 * Java indentation engine that delegates to java formatter
34 *
35 * @author Miloslav Metelka
36 */

37
38 public class JavaIndentEngine extends FormatterIndentEngine {
39
40     public static final String JavaDoc JAVA_FORMAT_NEWLINE_BEFORE_BRACE_PROP
41         = "javaFormatNewlineBeforeBrace"; // NOI18N
42

43     public static final String JavaDoc JAVA_FORMAT_SPACE_BEFORE_PARENTHESIS_PROP
44         = "javaFormatSpaceBeforeParenthesis"; // NOI18N
45

46     public static final String JavaDoc JAVA_FORMAT_LEADING_STAR_IN_COMMENT_PROP
47         = "javaFormatLeadingStarInComment"; // NOI18N
48

49     public static final String JavaDoc JAVA_FORMAT_STATEMENT_CONTINUATION_INDENT_PROP
50         = "javaFormatStatementContinuationIndent"; // NOI18N
51

52     static final long serialVersionUID = -7936605291288152329L;
53
54     public JavaIndentEngine() {
55         setAcceptedMimeTypes(new String JavaDoc[] { JavaKit.JAVA_MIME_TYPE });
56     }
57
58     protected ExtFormatter createFormatter() {
59         return new JavaFormatter(JavaKit.class);
60     }
61
62
63     public boolean getJavaFormatSpaceBeforeParenthesis() {
64         Boolean JavaDoc b = (Boolean JavaDoc)getValue(JavaSettingsNames.JAVA_FORMAT_SPACE_BEFORE_PARENTHESIS);
65         if (b == null) {
66             b = JavaSettingsDefaults.defaultJavaFormatSpaceBeforeParenthesis;
67         }
68         return b.booleanValue();
69     }
70     public void setJavaFormatSpaceBeforeParenthesis(boolean b) {
71         setValue(JavaSettingsNames.JAVA_FORMAT_SPACE_BEFORE_PARENTHESIS, b ? Boolean.TRUE : Boolean.FALSE, JAVA_FORMAT_SPACE_BEFORE_PARENTHESIS_PROP);
72     }
73
74     public boolean getJavaFormatNewlineBeforeBrace() {
75         Boolean JavaDoc b = (Boolean JavaDoc)getValue(JavaSettingsNames.JAVA_FORMAT_NEWLINE_BEFORE_BRACE);
76         if (b == null) {
77             b = JavaSettingsDefaults.defaultJavaFormatNewlineBeforeBrace;
78         }
79         return b.booleanValue();
80     }
81     public void setJavaFormatNewlineBeforeBrace(boolean b) {
82         setValue(JavaSettingsNames.JAVA_FORMAT_NEWLINE_BEFORE_BRACE, b ? Boolean.TRUE : Boolean.FALSE, JAVA_FORMAT_NEWLINE_BEFORE_BRACE_PROP);
83     }
84     
85     public boolean getJavaFormatLeadingStarInComment() {
86         Boolean JavaDoc b = (Boolean JavaDoc)getValue(JavaSettingsNames.JAVA_FORMAT_LEADING_STAR_IN_COMMENT);
87         if (b == null) {
88             b = JavaSettingsDefaults.defaultJavaFormatLeadingStarInComment;
89         }
90         return b.booleanValue();
91     }
92     public void setJavaFormatLeadingStarInComment(boolean b) {
93         setValue(JavaSettingsNames.JAVA_FORMAT_LEADING_STAR_IN_COMMENT, b ? Boolean.TRUE : Boolean.FALSE, JAVA_FORMAT_LEADING_STAR_IN_COMMENT_PROP);
94     }
95     
96     public int getJavaFormatStatementContinuationIndent() {
97         Integer JavaDoc i = (Integer JavaDoc)getValue(JavaSettingsNames.JAVA_FORMAT_STATEMENT_CONTINUATION_INDENT);
98         if (i == null) {
99             i = JavaSettingsDefaults.defaultJavaFormatStatementContinuationIndent;
100         }
101         return i.intValue();
102     }
103
104     public void setJavaFormatStatementContinuationIndent(int javaFormatStatementContinuationIndent) {
105         setValue(JavaSettingsNames.JAVA_FORMAT_STATEMENT_CONTINUATION_INDENT,
106             new Integer JavaDoc(javaFormatStatementContinuationIndent), JAVA_FORMAT_STATEMENT_CONTINUATION_INDENT_PROP);
107     }
108
109     // Serialization ------------------------------------------------------------
110

111     private static final ObjectStreamField[] serialPersistentFields = {
112         new ObjectStreamField(JAVA_FORMAT_NEWLINE_BEFORE_BRACE_PROP, Boolean.TYPE),
113         new ObjectStreamField(JAVA_FORMAT_SPACE_BEFORE_PARENTHESIS_PROP, Boolean.TYPE),
114         new ObjectStreamField(JAVA_FORMAT_LEADING_STAR_IN_COMMENT_PROP, Boolean.TYPE),
115         new ObjectStreamField(JAVA_FORMAT_STATEMENT_CONTINUATION_INDENT_PROP, Integer.TYPE)
116     };
117     
118     private void readObject(java.io.ObjectInputStream JavaDoc ois)
119     throws IOException, ClassNotFoundException JavaDoc {
120         ObjectInputStream.GetField fields = ois.readFields();
121         setJavaFormatNewlineBeforeBrace(fields.get(JAVA_FORMAT_NEWLINE_BEFORE_BRACE_PROP,
122             getJavaFormatNewlineBeforeBrace()));
123         setJavaFormatSpaceBeforeParenthesis(fields.get(JAVA_FORMAT_SPACE_BEFORE_PARENTHESIS_PROP,
124             getJavaFormatSpaceBeforeParenthesis()));
125         setJavaFormatLeadingStarInComment(fields.get(JAVA_FORMAT_LEADING_STAR_IN_COMMENT_PROP,
126             getJavaFormatLeadingStarInComment()));
127         setJavaFormatStatementContinuationIndent(fields.get(JAVA_FORMAT_STATEMENT_CONTINUATION_INDENT_PROP,
128             getJavaFormatStatementContinuationIndent()));
129     }
130
131     private void writeObject(java.io.ObjectOutputStream JavaDoc oos)
132     throws IOException, ClassNotFoundException JavaDoc {
133         ObjectOutputStream.PutField fields = oos.putFields();
134         fields.put(JAVA_FORMAT_NEWLINE_BEFORE_BRACE_PROP, getJavaFormatNewlineBeforeBrace());
135         fields.put(JAVA_FORMAT_SPACE_BEFORE_PARENTHESIS_PROP, getJavaFormatSpaceBeforeParenthesis());
136         fields.put(JAVA_FORMAT_LEADING_STAR_IN_COMMENT_PROP, getJavaFormatLeadingStarInComment());
137         fields.put(JAVA_FORMAT_STATEMENT_CONTINUATION_INDENT_PROP, getJavaFormatStatementContinuationIndent());
138         oos.writeFields();
139     }
140
141 }
142
143
Popular Tags