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: package org.netbeans.modules.print.impl.provider;
042:
043: import java.awt.Color;
044: import java.awt.Font;
045:
046: import java.text.AttributedCharacterIterator;
047: import java.util.ArrayList;
048: import java.util.Date;
049: import java.util.List;
050:
051: import javax.swing.JComponent;
052: import javax.swing.JEditorPane;
053: import javax.swing.text.BadLocationException;
054: import javax.swing.text.Document;
055:
056: import org.openide.cookies.EditorCookie;
057: import org.openide.text.AttributedCharacters;
058: import org.netbeans.editor.BaseDocument;
059:
060: import org.netbeans.modules.print.impl.util.Option;
061: import static org.netbeans.modules.print.impl.util.UI.*;
062:
063: /**
064: * @author Vladimir Yaroslavskiy
065: * @version 2006.04.04
066: */
067: public final class TextProvider extends ComponentProvider {
068:
069: public TextProvider(EditorCookie editor, Date modified) {
070: super (null, getName(editor), modified);
071: myEditor = editor;
072: }
073:
074: @Override
075: protected JComponent getComponent() {
076: if (Option.getDefault().isAsEditor()) {
077: JEditorPane[] panes = myEditor.getOpenedPanes();
078:
079: if (panes != null && panes.length != 0) {
080: return panes[0];
081: }
082: return null;
083: }
084: Document document = myEditor.getDocument();
085:
086: if (document == null) {
087: return null;
088: }
089: if (document instanceof BaseDocument
090: && document.getLength() < MAX_ATTR_TEXT_SIZE) {
091: //out();
092: //out("GET ITERATOR");
093: //out();
094: PrintContainer container = new PrintContainer();
095: ((BaseDocument) document).print(container, false, true, 0,
096: document.getLength());
097: return new ComponentDocument(container.getIterators());
098: }
099: return new ComponentDocument(getText(document));
100: }
101:
102: private String getText(Document document) {
103: try {
104: return document.getText(0, document.getLength());
105: } catch (BadLocationException e) {
106: return ""; // NOI18N
107: }
108: }
109:
110: private static String getName(EditorCookie editor) {
111: Document document = editor.getDocument();
112:
113: if (document == null) {
114: return null;
115: }
116: return ((String) document.getProperty(Document.TitleProperty))
117: .replace('\\', '/'); // NOI18N
118: }
119:
120: // --------------------------------------
121: private static final class PrintContainer implements
122: org.netbeans.editor.PrintContainer {
123: PrintContainer() {
124: myCharacters = new AttributedCharacters();
125: myCharactersList = new ArrayList<AttributedCharacters>();
126: }
127:
128: public void add(char[] chars, Font font, Color foreColor,
129: Color backColor) {
130: //out(getString(foreColor) + " " + getString(backColor) + " " + getString(font) + " " + new String(chars));
131: myCharacters.append(chars, font, foreColor);
132: }
133:
134: public void eol() {
135: //out();
136: myCharactersList.add(myCharacters);
137: myCharacters = new AttributedCharacters();
138: }
139:
140: public boolean initEmptyLines() {
141: return false;
142: }
143:
144: AttributedCharacterIterator[] getIterators() {
145: AttributedCharacterIterator[] iterators = new AttributedCharacterIterator[myCharactersList
146: .size()];
147:
148: for (int i = 0; i < myCharactersList.size(); i++) {
149: iterators[i] = myCharactersList.get(i).iterator();
150: }
151: return iterators;
152: }
153:
154: private AttributedCharacters myCharacters;
155: private List<AttributedCharacters> myCharactersList;
156: }
157:
158: private EditorCookie myEditor;
159: private static final int MAX_ATTR_TEXT_SIZE = 64000;
160: }
|