001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.modules.editor;
043:
044: import java.io.*;
045: import javax.swing.text.Document;
046: import org.netbeans.editor.BaseKit;
047: import org.netbeans.editor.SettingsNames;
048: import org.netbeans.editor.ext.ExtFormatter;
049: import org.netbeans.editor.SettingsDefaults;
050: import org.openide.text.IndentEngine;
051:
052: /**
053: * Indent engine that delegates to formatter
054: *
055: * @author Miloslav Metelka
056: */
057:
058: public abstract class FormatterIndentEngine extends IndentEngine {
059:
060: public static final String EXPAND_TABS_PROP = "expandTabs"; //NOI18N
061:
062: public static final String SPACES_PER_TAB_PROP = "spacesPerTab"; //NOI18N
063:
064: static final long serialVersionUID = -3408217516931076216L;
065:
066: /** Formatter to delegate to. It's checked before use and if it's null
067: * the createFormatter() is called to initialize it.
068: */
069: private transient ExtFormatter formatter;
070:
071: private String[] acceptedMimeTypes;
072:
073: /** Get the formatter to which this indentation engine delegates. */
074: public ExtFormatter getFormatter() {
075: if (formatter == null) {
076: formatter = createFormatter();
077: // Fallback if no formatter is registered (can happen with new formatting api)
078: if (formatter == null) {
079: formatter = new ExtFormatter(BaseKit.class);
080: }
081: }
082: return formatter;
083: }
084:
085: /** Create the formatter. */
086: protected abstract ExtFormatter createFormatter();
087:
088: public Object getValue(String settingName) {
089: return getFormatter().getSettingValue(settingName);
090: }
091:
092: public void setValue(String settingName, Object newValue,
093: String propertyName) {
094: Object oldValue = getValue(settingName);
095: if ((oldValue == null && newValue == null)
096: || (oldValue != null && oldValue.equals(newValue))) {
097: return; // no change
098: }
099:
100: getFormatter().setSettingValue(settingName, newValue);
101:
102: if (propertyName != null) {
103: firePropertyChange(propertyName, oldValue, newValue);
104: }
105: }
106:
107: /**
108: * @deprecated use {@link #setValue(java.lang.String, java.lang.Object, java.lang.String)} instead
109: * with properly specified propertyName
110: */
111: public void setValue(String settingName, Object newValue) {
112: setValue(settingName, newValue, null);
113: }
114:
115: public int indentLine(Document doc, int offset) {
116: return getFormatter().indentLine(doc, offset);
117: }
118:
119: public int indentNewLine(Document doc, int offset) {
120: return getFormatter().indentNewLine(doc, offset);
121: }
122:
123: public Writer createWriter(Document doc, int offset, Writer writer) {
124: return getFormatter().createWriter(doc, offset, writer);
125: }
126:
127: protected boolean acceptMimeType(String mimeType) {
128: if (acceptedMimeTypes != null) {
129: for (int i = acceptedMimeTypes.length - 1; i >= 0; i--) {
130: if (acceptedMimeTypes[i].equals(mimeType)) {
131: return true;
132: }
133: }
134: }
135:
136: return false;
137: }
138:
139: public boolean isExpandTabs() {
140: return getFormatter().expandTabs();
141: }
142:
143: public void setExpandTabs(boolean expandTabs) {
144: boolean old = getFormatter().expandTabs();
145: // Must call setter because of turning into custom property
146: getFormatter().setExpandTabs(expandTabs);
147: if (old != expandTabs) {
148: setValue(SettingsNames.EXPAND_TABS, Boolean
149: .valueOf(expandTabs), EXPAND_TABS_PROP);
150:
151: firePropertyChange(EXPAND_TABS_PROP, old ? Boolean.TRUE
152: : Boolean.FALSE, expandTabs ? Boolean.TRUE
153: : Boolean.FALSE);
154: }
155:
156: }
157:
158: public int getSpacesPerTab() {
159: return getFormatter().getSpacesPerTab();
160: }
161:
162: public void setSpacesPerTab(int spacesPerTab) {
163: if (spacesPerTab <= 0) {
164: NbEditorUtilities.invalidArgument("MSG_NegativeValue"); // NOI18N
165: return; // value unchanged
166: }
167:
168: int old = getFormatter().getSpacesPerTab();
169: getFormatter().setSpacesPerTab(spacesPerTab);
170: if (old != spacesPerTab) {
171: setValue(SettingsNames.SPACES_PER_TAB, new Integer(
172: spacesPerTab), SPACES_PER_TAB_PROP);
173:
174: firePropertyChange(SPACES_PER_TAB_PROP, new Integer(old),
175: new Integer(spacesPerTab));
176: }
177: }
178:
179: public void setAcceptedMimeTypes(String[] mimes) {
180: this .acceptedMimeTypes = mimes;
181: }
182:
183: public String[] getAcceptedMimeTypes() {
184: return acceptedMimeTypes;
185: }
186:
187: // Serialization ------------------------------------------------------------
188:
189: private static final ObjectStreamField[] serialPersistentFields = {
190: new ObjectStreamField(EXPAND_TABS_PROP, Boolean.TYPE),
191: new ObjectStreamField(SPACES_PER_TAB_PROP, Integer.TYPE) };
192:
193: private void readObject(java.io.ObjectInputStream ois)
194: throws IOException, ClassNotFoundException {
195: ObjectInputStream.GetField fields = ois.readFields();
196: setExpandTabs(fields.get(EXPAND_TABS_PROP,
197: SettingsDefaults.defaultExpandTabs.booleanValue()));
198: setSpacesPerTab(fields.get(SPACES_PER_TAB_PROP,
199: SettingsDefaults.defaultSpacesPerTab.intValue()));
200: }
201:
202: private void writeObject(java.io.ObjectOutputStream oos)
203: throws IOException, ClassNotFoundException {
204: ObjectOutputStream.PutField fields = oos.putFields();
205: fields.put(EXPAND_TABS_PROP, isExpandTabs());
206: fields.put(SPACES_PER_TAB_PROP, getSpacesPerTab());
207: oos.writeFields();
208: }
209:
210: }
|