PageRenderTime 28ms CodeModel.GetById 18ms RepoModel.GetById 0ms 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
Possible License(s): LGPL-3.0, Apache-2.0, GPL-3.0, GPL-2.0, AGPL-3.0, JSON, BSD-3-Clause
  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. */
  17. package enoa.handler;
  18. import ag.ion.bion.officelayer.document.IDocument;
  19. import ag.ion.bion.officelayer.text.IParagraph;
  20. import ag.ion.bion.officelayer.text.IParagraphProperties;
  21. import ag.ion.bion.officelayer.text.ITextCursor;
  22. import ag.ion.bion.officelayer.text.ITextDocument;
  23. import ag.ion.bion.officelayer.text.ITextFieldService;
  24. import ag.ion.bion.officelayer.text.ITextService;
  25. import ag.ion.bion.officelayer.text.TextException;
  26. import java.awt.Font;
  27. /**
  28. *This class handles text inside OO documents
  29. */
  30. public class TextHandler {
  31. private final ITextDocument doc;
  32. private final ITextService textservice;
  33. private final ITextFieldService textfieldservice;
  34. /**
  35. * Creates a new text handler for the given document
  36. * @param doc
  37. */
  38. public TextHandler(ITextDocument doc) {
  39. this.doc = doc;
  40. this.textservice = doc.getTextService();
  41. this.textfieldservice = doc.getTextFieldService();
  42. }
  43. /**
  44. * Append some text to the document
  45. * @param text
  46. * @throws TextException
  47. */
  48. public void append(String text) throws TextException {
  49. ITextCursor textCursor = getTextservice().getText().getTextCursorService().getTextCursor();
  50. textCursor.getEnd().setText(text);
  51. }
  52. /**
  53. * Append some text to the beginning of the document
  54. * @param text
  55. * @throws TextException
  56. */
  57. public void appendBefore(String text) throws TextException {
  58. ITextCursor textCursor = getTextservice().getText().getTextCursorService().getTextCursor();
  59. textCursor.getStart().setText(text);
  60. }
  61. /**
  62. * Set the align of the given paragraph inside this document
  63. * @param paragraph
  64. * @param align
  65. * @throws TextException
  66. */
  67. public void setAlign(int paragraph, short align) throws TextException {
  68. IParagraph[] paragraphs = getDoc().getTextService().getText().getTextContentEnumeration().getParagraphs();
  69. IParagraphProperties paragraphPropoerties = paragraphs[paragraph].getParagraphProperties();
  70. paragraphPropoerties.setParaAdjust(align);
  71. }
  72. /**
  73. * Set the text format of the given paragraph inside this document
  74. * @param paragraph
  75. * @param font
  76. * @param color
  77. * @throws TextException
  78. */
  79. public void setTextFormat(int paragraph, Font font, int color) throws TextException {
  80. IParagraph[] paragraphs = getTextservice().getText().getTextContentEnumeration().getParagraphs();
  81. IParagraphProperties paragraphPropoerties = paragraphs[paragraph].getParagraphProperties();
  82. paragraphPropoerties.getCharacterProperties().setFontBold(font.isBold());
  83. paragraphPropoerties.getCharacterProperties().setFontSize(font.getSize());
  84. paragraphPropoerties.getCharacterProperties().setFontItalic(font.isItalic());
  85. paragraphPropoerties.getCharacterProperties().setFontColor(color);
  86. }
  87. /**
  88. * Add paragraphs, each array row symbolizes a new paragraph
  89. * @param text
  90. * @throws TextException
  91. */
  92. public void addParagraphs(String[] text) throws TextException {
  93. for (int i = 0; i < text.length; i++) {
  94. ITextCursor textCursor = getTextservice().getText().getTextCursorService().getTextCursor();
  95. textCursor.gotoEnd(false);
  96. IParagraph paragraph = getTextservice().getTextContentService().constructNewParagraph();
  97. textCursor.gotoEnd(false);
  98. getTextservice().getTextContentService().insertTextContent(textCursor.getEnd(), paragraph);
  99. paragraph.setParagraphText(text[i]);
  100. }
  101. }
  102. /**
  103. * Returns the text of the given paragraph
  104. * @param paragraph
  105. * @return
  106. * @throws TextException
  107. */
  108. public String getParagraph(int paragraph) throws TextException {
  109. return getTextservice().getText().getTextContentEnumeration().getParagraphs()[paragraph].getParagraphText();
  110. }
  111. /**
  112. * @return the doc
  113. */
  114. public ITextDocument getDoc() {
  115. return doc;
  116. }
  117. /**
  118. * @return the textservice
  119. */
  120. public ITextService getTextservice() {
  121. return textservice;
  122. }
  123. /**
  124. * @return the textfieldservice
  125. */
  126. public ITextFieldService getTextfieldservice() {
  127. return textfieldservice;
  128. }
  129. }