PageRenderTime 14ms CodeModel.GetById 2ms app.highlight 9ms RepoModel.GetById 1ms app.codeStats 0ms

/src/enoa/handler/TextHandler.java

http://mp-rechnungs-und-kundenverwaltung.googlecode.com/
Java | 142 lines | 63 code | 13 blank | 66 comment | 1 complexity | 5dd6362cc8836f40ac4e41e7fbd8ec73 MD5 | raw file
  1/*
  2 *  This file is part of YaBS.
  3 *
  4 *      YaBS is free software: you can redistribute it and/or modify
  5 *      it under the terms of the GNU General Public License as published by
  6 *      the Free Software Foundation, either version 3 of the License, or
  7 *      (at your option) any later version.
  8 *
  9 *      YaBS is distributed in the hope that it will be useful,
 10 *      but WITHOUT ANY WARRANTY; without even the implied warranty of
 11 *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 12 *      GNU General Public License for more details.
 13 *
 14 *      You should have received a copy of the GNU General Public License
 15 *      along with YaBS.  If not, see <http://www.gnu.org/licenses/>.
 16 */
 17package enoa.handler;
 18
 19import ag.ion.bion.officelayer.document.IDocument;
 20import ag.ion.bion.officelayer.text.IParagraph;
 21import ag.ion.bion.officelayer.text.IParagraphProperties;
 22import ag.ion.bion.officelayer.text.ITextCursor;
 23import ag.ion.bion.officelayer.text.ITextDocument;
 24import ag.ion.bion.officelayer.text.ITextFieldService;
 25import ag.ion.bion.officelayer.text.ITextService;
 26import ag.ion.bion.officelayer.text.TextException;
 27import java.awt.Font;
 28
 29/**
 30 *This class handles text inside OO documents
 31 */
 32public class TextHandler {
 33
 34    private final ITextDocument doc;
 35    private final ITextService textservice;
 36    private final ITextFieldService textfieldservice;
 37
 38    /**
 39     * Creates a new text handler for the given document
 40     * @param doc
 41     */
 42    public TextHandler(ITextDocument doc) {
 43        this.doc = doc;
 44        this.textservice = doc.getTextService();
 45        this.textfieldservice = doc.getTextFieldService();
 46    }
 47
 48    /**
 49     * Append some text to the document
 50     * @param text
 51     * @throws TextException
 52     */
 53    public void append(String text) throws TextException {
 54        ITextCursor textCursor = getTextservice().getText().getTextCursorService().getTextCursor();
 55        textCursor.getEnd().setText(text);
 56    }
 57
 58    /**
 59     * Append some text to the beginning of the document
 60     * @param text
 61     * @throws TextException
 62     */
 63    public void appendBefore(String text) throws TextException {
 64        ITextCursor textCursor = getTextservice().getText().getTextCursorService().getTextCursor();
 65        textCursor.getStart().setText(text);
 66    }
 67
 68    /**
 69     * Set the align of the given paragraph inside this document
 70     * @param paragraph
 71     * @param align
 72     * @throws TextException
 73     */
 74    public void setAlign(int paragraph, short align) throws TextException {
 75        IParagraph[] paragraphs = getDoc().getTextService().getText().getTextContentEnumeration().getParagraphs();
 76        IParagraphProperties paragraphPropoerties = paragraphs[paragraph].getParagraphProperties();
 77        paragraphPropoerties.setParaAdjust(align);
 78    }
 79
 80    /**
 81     * Set the text format of the given paragraph inside this document
 82     * @param paragraph
 83     * @param font
 84     * @param color
 85     * @throws TextException
 86     */
 87    public void setTextFormat(int paragraph, Font font, int color) throws TextException {
 88        IParagraph[] paragraphs = getTextservice().getText().getTextContentEnumeration().getParagraphs();
 89        IParagraphProperties paragraphPropoerties = paragraphs[paragraph].getParagraphProperties();
 90        paragraphPropoerties.getCharacterProperties().setFontBold(font.isBold());
 91        paragraphPropoerties.getCharacterProperties().setFontSize(font.getSize());
 92        paragraphPropoerties.getCharacterProperties().setFontItalic(font.isItalic());
 93        paragraphPropoerties.getCharacterProperties().setFontColor(color);
 94    }
 95
 96    /**
 97     * Add paragraphs, each array row symbolizes a new paragraph
 98     * @param text
 99     * @throws TextException
100     */
101    public void addParagraphs(String[] text) throws TextException {
102        for (int i = 0; i < text.length; i++) {
103            ITextCursor textCursor = getTextservice().getText().getTextCursorService().getTextCursor();
104            textCursor.gotoEnd(false);
105            IParagraph paragraph = getTextservice().getTextContentService().constructNewParagraph();
106            textCursor.gotoEnd(false);
107            getTextservice().getTextContentService().insertTextContent(textCursor.getEnd(), paragraph);
108            paragraph.setParagraphText(text[i]);
109        }
110    }
111
112    /**
113     * Returns the text of the given paragraph
114     * @param paragraph
115     * @return
116     * @throws TextException
117     */
118    public String getParagraph(int paragraph) throws TextException {
119        return getTextservice().getText().getTextContentEnumeration().getParagraphs()[paragraph].getParagraphText();
120    }
121
122    /**
123     * @return the doc
124     */
125    public ITextDocument getDoc() {
126        return doc;
127    }
128
129    /**
130     * @return the textservice
131     */
132    public ITextService getTextservice() {
133        return textservice;
134    }
135
136    /**
137     * @return the textfieldservice
138     */
139    public ITextFieldService getTextfieldservice() {
140        return textfieldservice;
141    }
142}