KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > editor > FormatterIndentEngine


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;
21
22 import java.io.*;
23 import javax.swing.text.Document JavaDoc;
24 import org.netbeans.editor.SettingsNames;
25 import org.netbeans.editor.ext.ExtFormatter;
26 import org.netbeans.editor.SettingsDefaults;
27 import org.openide.text.IndentEngine;
28
29 /**
30 * Indent engine that delegates to formatter
31 *
32 * @author Miloslav Metelka
33 */

34
35 public abstract class FormatterIndentEngine extends IndentEngine {
36
37     public static final String JavaDoc EXPAND_TABS_PROP = "expandTabs"; //NOI18N
38

39     public static final String JavaDoc SPACES_PER_TAB_PROP = "spacesPerTab"; //NOI18N
40

41     static final long serialVersionUID = -3408217516931076216L;
42
43     /** Formatter to delegate to. It's checked before use and if it's null
44      * the createFormatter() is called to initialize it.
45      */

46     private transient ExtFormatter formatter;
47
48     private String JavaDoc[] acceptedMimeTypes;
49
50     /** Get the formatter to which this indentation engine delegates. */
51     public ExtFormatter getFormatter() {
52         if (formatter == null) {
53             formatter = createFormatter();
54         }
55         return formatter;
56     }
57
58     /** Create the formatter. */
59     protected abstract ExtFormatter createFormatter();
60
61     public Object JavaDoc getValue(String JavaDoc settingName) {
62         return getFormatter().getSettingValue(settingName);
63     }
64
65     public void setValue(String JavaDoc settingName, Object JavaDoc newValue, String JavaDoc propertyName) {
66         Object JavaDoc oldValue = getValue(settingName);
67         if ((oldValue == null && newValue == null)
68                 || (oldValue != null && oldValue.equals(newValue))
69            ) {
70             return; // no change
71
}
72
73         getFormatter().setSettingValue(settingName, newValue);
74         
75         if (propertyName != null){
76             firePropertyChange(propertyName, oldValue, newValue);
77         }
78     }
79
80     /**
81      * @deprecated use {@link #setValue(java.lang.String, java.lang.Object, java.lang.String)} instead
82      * with properly specified propertyName
83      */

84     public void setValue(String JavaDoc settingName, Object JavaDoc newValue) {
85         setValue(settingName, newValue, null);
86     }
87     
88     
89     public int indentLine(Document JavaDoc doc, int offset) {
90         return getFormatter().indentLine(doc, offset);
91     }
92
93     public int indentNewLine(Document JavaDoc doc, int offset) {
94         return getFormatter().indentNewLine(doc, offset);
95     }
96
97     public Writer createWriter(Document JavaDoc doc, int offset, Writer writer) {
98         return getFormatter().createWriter(doc, offset, writer);
99     }
100
101     protected boolean acceptMimeType(String JavaDoc mimeType) {
102         if (acceptedMimeTypes != null) {
103             for (int i = acceptedMimeTypes.length - 1; i >= 0; i--) {
104                 if (acceptedMimeTypes[i].equals(mimeType)) {
105                     return true;
106                 }
107             }
108         }
109
110         return false;
111     }
112
113     public boolean isExpandTabs() {
114         return getFormatter().expandTabs();
115     }
116
117     public void setExpandTabs(boolean expandTabs) {
118         boolean old = getFormatter().expandTabs();
119         // Must call setter because of turning into custom property
120
getFormatter().setExpandTabs(expandTabs);
121         if (old != expandTabs) {
122             setValue(SettingsNames.EXPAND_TABS,
123                 Boolean.valueOf(expandTabs), EXPAND_TABS_PROP);
124             
125             firePropertyChange(EXPAND_TABS_PROP,
126                 old ? Boolean.TRUE : Boolean.FALSE,
127                 expandTabs ? Boolean.TRUE : Boolean.FALSE
128             );
129         }
130         
131     }
132
133     public int getSpacesPerTab() {
134         return getFormatter().getSpacesPerTab();
135     }
136
137     public void setSpacesPerTab(int spacesPerTab) {
138         if (spacesPerTab <= 0) {
139             NbEditorUtilities.invalidArgument("MSG_NegativeValue"); // NOI18N
140
return; // value unchanged
141
}
142
143         int old = getFormatter().getSpacesPerTab();
144         getFormatter().setSpacesPerTab(spacesPerTab);
145         if (old != spacesPerTab) {
146             setValue(SettingsNames.SPACES_PER_TAB,
147                 new Integer JavaDoc(spacesPerTab), SPACES_PER_TAB_PROP);
148             
149             firePropertyChange(SPACES_PER_TAB_PROP,
150                 new Integer JavaDoc(old),
151                 new Integer JavaDoc(spacesPerTab)
152             );
153         }
154     }
155
156     public void setAcceptedMimeTypes(String JavaDoc[] mimes) {
157         this.acceptedMimeTypes = mimes;
158     }
159
160     public String JavaDoc[] getAcceptedMimeTypes() {
161         return acceptedMimeTypes;
162     }
163     
164     
165
166     // Serialization ------------------------------------------------------------
167

168     private static final ObjectStreamField[] serialPersistentFields = {
169         new ObjectStreamField(EXPAND_TABS_PROP, Boolean.TYPE),
170         new ObjectStreamField(SPACES_PER_TAB_PROP, Integer.TYPE)
171     };
172     
173     private void readObject(java.io.ObjectInputStream JavaDoc ois)
174     throws IOException, ClassNotFoundException JavaDoc {
175         ObjectInputStream.GetField fields = ois.readFields();
176         setExpandTabs(fields.get(EXPAND_TABS_PROP, SettingsDefaults.defaultExpandTabs.booleanValue()));
177         setSpacesPerTab(fields.get(SPACES_PER_TAB_PROP, SettingsDefaults.defaultSpacesPerTab.intValue()));
178     }
179
180     private void writeObject(java.io.ObjectOutputStream JavaDoc oos)
181     throws IOException, ClassNotFoundException JavaDoc {
182         ObjectOutputStream.PutField fields = oos.putFields();
183         fields.put(EXPAND_TABS_PROP, isExpandTabs());
184         fields.put(SPACES_PER_TAB_PROP, getSpacesPerTab());
185         oos.writeFields();
186     }
187
188 }
189
190
Popular Tags