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.vmd.api.codegen;
043:
044: import org.netbeans.api.editor.guards.SimpleSection;
045: import org.netbeans.modules.vmd.api.model.Debug;
046: import org.openide.text.IndentEngine;
047:
048: import javax.swing.text.BadLocationException;
049: import javax.swing.text.StyledDocument;
050: import java.io.IOException;
051: import java.io.StringWriter;
052: import java.io.Writer;
053:
054: /**
055: * @author David Kaspar
056: */
057: public final class CodeWriter {
058:
059: private StyledDocument document;
060: private String forceValue;
061: private SimpleSection section;
062: private int offset;
063: private int endOffset;
064: private Writer writer;
065: private StringWriter memory;
066: private boolean committed;
067:
068: public CodeWriter(StyledDocument document, SimpleSection section) {
069: this (document, section.getStartPosition().getOffset(),
070: Integer.MIN_VALUE, null);
071: this .section = section;
072: }
073:
074: public CodeWriter(StyledDocument document,
075: SimpleSection beforeSection, SimpleSection afterSection,
076: String forceValue) {
077: this (document, beforeSection.getEndPosition().getOffset() + 1,
078: afterSection.getStartPosition().getOffset(), forceValue);
079: }
080:
081: private CodeWriter(StyledDocument document, int beginOffset,
082: int endOffset, String forceValue) {
083: this .document = document;
084: this .forceValue = forceValue;
085: this .offset = beginOffset;
086: this .endOffset = endOffset;
087: }
088:
089: public CodeWriter write(String text) {
090: assert !committed;
091: if (forceValue != null)
092: return this ;
093: try {
094: if (writer == null) {
095: memory = new StringWriter(512);
096: IndentEngine indentEngine = IndentEngine.find(document);
097: if (indentEngine != null)
098: writer = indentEngine.createWriter(document,
099: offset, memory);
100: else
101: writer = memory;
102: }
103: writer.write(text);
104: return this ;
105: } catch (IOException e) {
106: throw Debug.error(e);
107: }
108: }
109:
110: public void commit() {
111: assert !committed;
112: try {
113: String text;
114: if (forceValue != null) {
115: text = forceValue;
116: } else {
117: if (writer != null) {
118: writer.flush();
119: writer.close();
120: text = memory.getBuffer().toString();
121: } else
122: text = ""; // NOI18N
123: }
124:
125: if (section != null)
126: section.setText(text);
127: else {
128: if (endOffset != Integer.MIN_VALUE)
129: document.remove(offset, endOffset - offset);
130: document.insertString(offset, text, null);
131: }
132:
133: committed = true;
134: writer = null;
135: memory = null;
136: } catch (IOException e) {
137: throw Debug.error(e);
138: } catch (BadLocationException e) {
139: throw Debug.error(e);
140: }
141: }
142:
143: public boolean isCommitted() {
144: return committed;
145: }
146:
147: }
|