Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.File;
- import java.io.FileOutputStream;
- import java.io.InputStream;
- import java.io.OutputStream;
- import java.net.URL;
- import java.util.ArrayList;
- import java.util.HashMap;
- import java.util.List;
- import java.util.Map;
- import com.sun.star.beans.PropertyValue;
- import com.sun.star.beans.XPropertySet;
- import com.sun.star.comp.beans.NoConnectionException;
- import com.sun.star.comp.beans.OOoBean;
- import com.sun.star.comp.beans.OfficeDocument;
- import com.sun.star.comp.helper.Bootstrap;
- import com.sun.star.container.XEnumerationAccess;
- import com.sun.star.container.XIndexAccess;
- import com.sun.star.container.XNameAccess;
- import com.sun.star.container.XNameContainer;
- import com.sun.star.container.XNamed;
- import com.sun.star.datatransfer.XTransferable;
- import com.sun.star.datatransfer.XTransferableSupplier;
- import com.sun.star.document.XDocumentInsertable;
- import com.sun.star.frame.XController;
- import com.sun.star.frame.XDispatchHelper;
- import com.sun.star.frame.XDispatchProvider;
- import com.sun.star.frame.XFrame;
- import com.sun.star.graphic.XGraphic;
- import com.sun.star.graphic.XGraphicProvider;
- import com.sun.star.lang.IndexOutOfBoundsException;
- import com.sun.star.lang.XMultiServiceFactory;
- import com.sun.star.style.BreakType;
- import com.sun.star.style.XStyleFamiliesSupplier;
- import com.sun.star.text.ControlCharacter;
- import com.sun.star.text.TextContentAnchorType;
- import com.sun.star.text.XBookmarksSupplier;
- import com.sun.star.text.XPageCursor;
- import com.sun.star.text.XReferenceMarksSupplier;
- import com.sun.star.text.XText;
- import com.sun.star.text.XTextContent;
- import com.sun.star.text.XTextCursor;
- import com.sun.star.text.XTextDocument;
- import com.sun.star.text.XTextFieldsSupplier;
- import com.sun.star.text.XTextRange;
- import com.sun.star.text.XTextSection;
- import com.sun.star.text.XTextSectionsSupplier;
- import com.sun.star.text.XTextTable;
- import com.sun.star.text.XTextViewCursor;
- import com.sun.star.text.XTextViewCursorSupplier;
- import com.sun.star.uno.RuntimeException;
- import com.sun.star.uno.UnoRuntime;
- import com.sun.star.util.XRefreshable;
- import com.sun.star.util.XSearchDescriptor;
- import com.sun.star.util.XSearchable;
- /**
- * Classe que encapsula recursos para o tratamento de documentos Writer. Recebe como parâmetro uma instância de office
- * bean do qual obtém o documento a ser tratado
- */
- public class WriterDocument {
- /**
- * Posição do cursor no documento. A posição ATUAL não altera a posição do cursor no documento
- */
- public enum EPosicao {
- INICIO,
- ATUAL,
- FIM
- }
- public static final String MSG_ERRO_OPEN_OFFICE = "Ocorreu um erro inesperado ao manipular documento. Por favor, entre em contato com o suporte técnico.";
- private static String PREFIXO_ARQUIVO = "tce-doe";
- public static void converterParaPDF(String caminhoEntrada, String caminhoSaida) {
- try {
- OOoBean lWorld = LibreOfficeUtil.carregarDocumento(caminhoEntrada);
- PropertyValue[] lProps = new PropertyValue[1];
- lProps[0] = new PropertyValue();
- lProps[0].Name = "FilterName";
- lProps[0].Value = "writer_pdf_Export";
- lWorld.storeToURL(caminhoSaida, lProps);
- } catch (Exception pEx) {
- throw new RuntimeException(MSG_ERRO_OPEN_OFFICE, pEx);
- }
- }
- private final OOoBean bean;
- public WriterDocument(OOoBean bean) {
- this.bean = bean;
- }
- /**
- * Criar um cursor.
- * @return
- * @throws Exception
- */
- public XTextCursor createTextCursor() {
- return getText().createTextCursor();
- }
- /**
- * Cria uma instância de XTextTable
- * @param bean
- * @return
- * @throws Exception
- */
- public XTextTable createTextTable() {
- try {
- return UnoRuntime.queryInterface(XTextTable.class,
- bean.getMultiServiceFactory().createInstance("com.sun.star.text.TextTable"));
- } catch (Exception pEx) {
- throw new RuntimeException(MSG_ERRO_OPEN_OFFICE, pEx);
- }
- }
- public void excluirBookmark(String bookmarkName) {
- try {
- XTextDocument textDocument = getTextDocument();
- XController xController = textDocument.getCurrentController();
- XFrame xFrame = xController.getFrame();
- XDispatchProvider xDispatchProvider = UnoRuntime.queryInterface(XDispatchProvider.class, xFrame);
- Object dispatchHelper = bean.getMultiServiceFactory().createInstance("com.sun.star.frame.DispatchHelper");
- XDispatchHelper helper = UnoRuntime.queryInterface(XDispatchHelper.class, dispatchHelper);
- XBookmarksSupplier bookmarksSupplier = UnoRuntime.queryInterface(XBookmarksSupplier.class, textDocument);
- XRefreshable iRefreshable = UnoRuntime.queryInterface(XRefreshable.class, bookmarksSupplier);
- iRefreshable.refresh(); // Do I HAVE to do that?
- Object iDeleteBookmark = bookmarksSupplier.getBookmarks().getByName(bookmarkName);
- XTextContent iBookmarkContent = UnoRuntime.queryInterface(XTextContent.class, iDeleteBookmark);
- XTextRange iBookmarkRange = iBookmarkContent.getAnchor();
- getTextViewCursor().gotoRange(iBookmarkRange, false);
- // XTextRange iRange = iBookmarkRange.getText().createTextCursorByRange(iBookmarkRange);
- helper.executeDispatch(xDispatchProvider, ".uno:Delete", "", 0, new PropertyValue[] {});
- } catch (Exception e) {
- throw new RuntimeException(MSG_ERRO_OPEN_OFFICE, e);
- }
- }
- public void excluirTexto(XTextRange rangeStart, XTextRange rangeEnd) {
- XTextViewCursorSupplier lSupplier = getTextViewCursorSupplier();
- XTextViewCursor cursor = lSupplier.getViewCursor();
- // Seleciona o texto de rangeStart até rangeEnd e exclui o texto selecionado
- cursor.gotoRange(rangeStart.getStart(), false);
- cursor.gotoRange(rangeEnd.getEnd(), true);
- cursor.setString("");
- }
- public void fecharDocumento() {
- LibreOfficeUtil.fecharDocumento(bean);
- }
- /**
- * Obtém o conteúdo entre duas posições.
- * @param pRangeInicio
- * @param pRangeFim
- * @return
- * @throws Exception
- */
- public XTransferable getConteudoPorRange(XTextRange pRangeInicio, XTextRange pRangeFim) {
- XTextViewCursor lCursor = getTextViewCursor();
- // Seleciona o conteúdo
- lCursor.gotoRange(pRangeInicio.getEnd(), false);
- lCursor.gotoRange(pRangeFim.getStart(), true);
- // cria o mantenedor de transferência
- XTransferableSupplier lTransSupplier = getTransferableSupplier();
- return lTransSupplier.getTransferable();
- }
- /**
- * Obtém o controller corrente.
- * @return XController
- * @throws Exception
- */
- public XController getCurrentController() {
- return getTextDocument().getCurrentController();
- }
- /**
- * Obtém uma instância de XEnumerationAccess.
- * @param bean
- * @return
- * @throws Exception
- */
- public XEnumerationAccess getEnumarationAccess() {
- return UnoRuntime.queryInterface(XEnumerationAccess.class, getTextDocument());
- }
- public OOoBean getOfficeBean() {
- return bean;
- }
- /**
- * Retorna um OfficeDocument - objeto que representa a raiz de um documento do Writer/Calc/Impress/Draw.
- * @return
- * @throws Exception
- */
- private OfficeDocument getOfficeDocument() {
- return LibreOfficeUtil.getOfficeDocument(bean);
- }
- private XPageCursor getPageCursor(XTextViewCursor textViewCursor) {
- XPageCursor pageCursor = UnoRuntime.queryInterface(XPageCursor.class, textViewCursor);
- return pageCursor;
- }
- public XTextRange getPosicaoBookmark(String bookmarkName) {
- try {
- XTextDocument textDocument = getTextDocument();
- XBookmarksSupplier bookmarksSupplier = UnoRuntime.queryInterface(XBookmarksSupplier.class, textDocument);
- XRefreshable iRefreshable = UnoRuntime.queryInterface(XRefreshable.class, bookmarksSupplier);
- iRefreshable.refresh(); // Is it necessary?
- Object iDeleteBookmark = bookmarksSupplier.getBookmarks().getByName(bookmarkName);
- XTextContent iBookmarkContent = UnoRuntime.queryInterface(XTextContent.class, iDeleteBookmark);
- XTextRange iBookmarkRange = iBookmarkContent.getAnchor();
- getTextViewCursor().gotoRange(iBookmarkRange, false);
- return iBookmarkRange;
- } catch (Exception e) {
- throw new RuntimeException(MSG_ERRO_OPEN_OFFICE, e);
- }
- }
- public XTextRange getPosicaoSecao(String sectionName) {
- try {
- XTextSectionsSupplier xTextSectionsSupplier = UnoRuntime.queryInterface(XTextSectionsSupplier.class,
- getTextDocument());
- XTextSection textSection = UnoRuntime.queryInterface(XTextSection.class, xTextSectionsSupplier.getTextSections()
- .getByName(sectionName));
- return textSection.getAnchor();
- } catch (Exception e) {
- throw new RuntimeException(MSG_ERRO_OPEN_OFFICE, e);
- }
- }
- public XSearchable getSearchable() {
- return UnoRuntime.queryInterface(XSearchable.class, getTextDocument());
- }
- public XSearchDescriptor getSearchDescriptor(XSearchable busca) {
- return busca.createSearchDescriptor();
- }
- /**
- * Obtém uma instância de XText.
- * @return
- * @throws Exception
- */
- public XText getText() {
- return getTextDocument().getText();
- }
- public String getText(XTextRange rangeStart, XTextRange rangeEnd) {
- XTextViewCursorSupplier lSupplier = getTextViewCursorSupplier();
- XTextViewCursor cursor = lSupplier.getViewCursor();
- // Seleciona o texto de rangeStart até rangeEnd e retorna o texto selecionado
- cursor.gotoRange(rangeStart.getStart(), false);
- cursor.gotoRange(rangeEnd.getEnd(), true);
- return cursor.getString();
- }
- /**
- * Criar um cursor com limite definido através de um XTextRange.
- * @param range
- * @return
- * @throws Exception
- */
- public XTextCursor getTextCursorPorRange(XTextRange range) {
- return getText().createTextCursorByRange(range);
- }
- /**
- * Obtém uma instância de XTextDocument que é o topo da comunicação com o Writer. A partir dela que as demais
- * interfaces são utilizadas.
- * @return
- * @throws Exception
- */
- public XTextDocument getTextDocument() {
- return UnoRuntime.queryInterface(XTextDocument.class, getOfficeDocument());
- }
- public XTextRange getTextRange(Object object) {
- return UnoRuntime.queryInterface(XTextRange.class, object);
- }
- /**
- * Retorna uma instância de XTextViewCursor.
- * @param bean
- * @return
- * @throws Exception
- */
- public XTextViewCursor getTextViewCursor() {
- return getTextViewCursorSupplier().getViewCursor();
- }
- /**
- * Obtém um fornecedor de cursor de texto.
- * @return
- * @throws Exception
- */
- public XTextViewCursorSupplier getTextViewCursorSupplier() {
- return UnoRuntime.queryInterface(XTextViewCursorSupplier.class, getCurrentController());
- }
- /**
- * Obtêm matenedor de área de transferência
- * @return
- */
- public XTransferableSupplier getTransferableSupplier() {
- return UnoRuntime.queryInterface(XTransferableSupplier.class, getCurrentController());
- }
- public void inserirBookmark(String bookmarkName, XTextRange posicao) {
- try {
- XTextDocument textDocument = getTextDocument();
- XMultiServiceFactory iMSF = UnoRuntime.queryInterface(XMultiServiceFactory.class, textDocument);
- Object bookmark = iMSF.createInstance("com.sun.star.text.Bookmark");
- XNamed xNamed = UnoRuntime.queryInterface(XNamed.class, bookmark);
- xNamed.setName(bookmarkName);
- XTextContent xTextContent = UnoRuntime.queryInterface(XTextContent.class, bookmark);
- textDocument.getText().insertTextContent(posicao, xTextContent, false);
- } catch (Exception e) {
- throw new RuntimeException(MSG_ERRO_OPEN_OFFICE, e);
- }
- }
- public void inserirDocumento(OOoBean bean) {
- inserirDocumento(bean, true);
- }
- /**
- * Insere um documento. Como o LibreOffice geralmente cria uma quebra de página quando um documento é inserido
- * dentro de outro, o parâmetro <code>removerQuebraPagina</code> serve para indicar quando esta quebra de página
- * deve ser removida.
- * @param bean
- * @param removerQuebraPagina - se deve remover ou não a quebra de página gerada pelo LibreOffice
- */
- public void inserirDocumento(OOoBean bean, boolean removerQuebraPagina) {
- try {
- WriterDocument writerOrigem = new WriterDocument(bean);
- XTextRange inicio = writerOrigem.getText().getStart();
- XTextRange fim = writerOrigem.getText().getEnd();
- inserirParagrafo();
- XTransferable transferable = writerOrigem.getConteudoPorRange(inicio, fim);
- setTransferable(transferable);
- if (removerQuebraPagina) {
- // !Importante
- // Solução para remover a quebra de página inserida pelo LibreOffice:
- // 1) Guardar a posição do fim do texto e mover o cursor para o início da página criada
- XTextViewCursor textViewCursor = getTextViewCursor();
- XTextRange posicaoFimTexto = textViewCursor.getStart();
- XPageCursor pageCursor = getPageCursor(textViewCursor);
- pageCursor.jumpToStartOfPage();
- // 2) Selecionar um caractere à esquerda e removê-lo, pois este representa a quebra de página
- textViewCursor.goLeft((short) 1, true);
- textViewCursor.setString("");
- // 3) Mover o cursor de volta para o fim da seção
- textViewCursor.gotoRange(posicaoFimTexto, false);
- }
- } catch (Exception e) {
- throw new RuntimeException(MSG_ERRO_OPEN_OFFICE, e);
- }
- }
- public void inserirDocumento(String caminhoArquivo) {
- try {
- XTextViewCursor textViewCursor = getTextViewCursor();
- XTextCursor textCursor = textViewCursor.getText().createTextCursorByRange(textViewCursor);
- XDocumentInsertable documentInsertable = UnoRuntime.queryInterface(XDocumentInsertable.class, textCursor);
- documentInsertable.insertDocumentFromURL(caminhoArquivo, null);
- } catch (Exception e) {
- throw new RuntimeException(MSG_ERRO_OPEN_OFFICE, e);
- }
- }
- public void inserirImagem(URL imagem) {
- try {
- XMultiServiceFactory lMSFDoc = UnoRuntime.queryInterface(XMultiServiceFactory.class, getOfficeBean()
- .getDocument());
- Object oGraphic = lMSFDoc.createInstance("com.sun.star.text.TextGraphicObject");
- Object lGraphicProviderObject = Bootstrap.bootstrap().getServiceManager()
- .createInstanceWithContext("com.sun.star.graphic.GraphicProvider", Bootstrap.bootstrap());
- XGraphicProvider lGraphicProvider = UnoRuntime.queryInterface(XGraphicProvider.class, lGraphicProviderObject);
- PropertyValue[] lProperties = new PropertyValue[1];
- lProperties[0] = new PropertyValue();
- lProperties[0].Name = "URL";
- lProperties[0].Value = imagem.getProtocol() + "://" + imagem.getPath();
- XGraphic lGraphic = lGraphicProvider.queryGraphic(lProperties);
- XTextContent lImagemTextContent = UnoRuntime.queryInterface(XTextContent.class, oGraphic);
- XPropertySet lPropSet = UnoRuntime.queryInterface(XPropertySet.class, oGraphic);
- lPropSet.setPropertyValue("AnchorType", TextContentAnchorType.AS_CHARACTER);
- lPropSet.setPropertyValue("Graphic", lGraphic);
- getTextViewCursor().getText().insertTextContent(getTextViewCursor(), lImagemTextContent, true);
- } catch (Exception pEx) {
- throw new RuntimeException(MSG_ERRO_OPEN_OFFICE, pEx);
- }
- }
- public void inserirMarcaDagua(InputStream inputStreamImagem) {
- try {
- File lTempFile = File.createTempFile(PREFIXO_ARQUIVO, "img");
- lTempFile.deleteOnExit();
- OutputStream osTempFile = new FileOutputStream(lTempFile);
- int i;
- while ((i = inputStreamImagem.read()) != -1) {
- osTempFile.write(i);
- }
- inputStreamImagem.close();
- XTextDocument oDoc = UnoRuntime.queryInterface(XTextDocument.class, getOfficeBean().getDocument());
- // create a supplier to get the Style family collection
- XStyleFamiliesSupplier xSupplier = UnoRuntime.queryInterface(XStyleFamiliesSupplier.class, oDoc);
- // get the NameAccess interface from the Style family collection
- XNameAccess xNameAccess = xSupplier.getStyleFamilies();
- XNameContainer xPageStyleCollection = UnoRuntime.queryInterface(XNameContainer.class,
- xNameAccess.getByName("PageStyles"));
- // create a PropertySet to set the properties for the new Pagestyle
- String[] styles = xPageStyleCollection.getElementNames();
- for (String style : styles) {
- XPropertySet xPropertySet = UnoRuntime.queryInterface(XPropertySet.class,
- xPageStyleCollection.getByName(style));
- // XPropertySet xPropertySet = (XPropertySet)
- // UnoRuntime.queryInterface(XPropertySet.class,
- // xPageStyleCollection.getByName("Standard"));
- xPropertySet.setPropertyValue("BackGraphicURL", lTempFile.toURI().toURL().getProtocol() + "://"
- + lTempFile.toURI().toURL().getPath());
- // xPropertySet.setPropertyValue("BackGraphicURL", createUNOFileURL("watermark.jpg",
- // xRemoteContext));
- }
- } catch (Exception e) {
- throw new RuntimeException(MSG_ERRO_OPEN_OFFICE);
- }
- }
- public void inserirNovaPagina() {
- XTextCursor textCursor = createTextCursor();
- textCursor.gotoEnd(false);
- XPropertySet properties = UnoRuntime.queryInterface(XPropertySet.class, textCursor);
- try {
- properties.setPropertyValue("BreakType", BreakType.PAGE_AFTER);
- textCursor.getText().insertControlCharacter(textCursor.getEnd(), (short) 0, false);
- } catch (Exception e) {
- throw new RuntimeException(MSG_ERRO_OPEN_OFFICE, e);
- }
- textCursor.gotoEnd(true);
- }
- public void inserirParagrafo() {
- try {
- XTextViewCursor cursor = getTextViewCursor();
- cursor.getText().insertControlCharacter(cursor, ControlCharacter.PARAGRAPH_BREAK, false);
- } catch (Exception e) {
- throw new RuntimeException(MSG_ERRO_OPEN_OFFICE, e);
- }
- }
- public void inserirQuebraLinha() {
- try {
- XTextViewCursor cursor = getTextViewCursor();
- cursor.getText().insertControlCharacter(cursor, ControlCharacter.LINE_BREAK, false);
- } catch (Exception e) {
- throw new RuntimeException(MSG_ERRO_OPEN_OFFICE, e);
- }
- }
- public void inserirReferencia(String referencia) {
- try {
- XMultiServiceFactory lMSFDoc = UnoRuntime.queryInterface(XMultiServiceFactory.class, getOfficeBean()
- .getDocument());
- XNamed lRefMark = UnoRuntime.queryInterface(XNamed.class,
- lMSFDoc.createInstance("com.sun.star.text.ReferenceMark"));
- lRefMark.setName(referencia);
- XTextCursor cursor = getTextViewCursor();
- cursor.gotoRange(cursor.getEnd(), false);
- inserirTexto(referencia);
- cursor.goLeft((short) 1, true);
- XTextRange lRange = getTextRange(cursor);
- XTextContent lContent = UnoRuntime.queryInterface(XTextContent.class, lRefMark);
- cursor.getText().insertTextContent(lRange, lContent, true);
- } catch (Exception pEx) {
- throw new RuntimeException(MSG_ERRO_OPEN_OFFICE, pEx);
- }
- }
- public void inserirTexto(String texto) {
- XTextRange viewCursor = getTextViewCursor();
- XText text = viewCursor.getText();
- text.insertString(viewCursor, texto, false);
- }
- /**
- * Verifica se a conexão com o OpenOffice está estabelicida.
- * @return
- * @throws Exception
- */
- public boolean isAberto() {
- if (bean != null) {
- try {
- if (bean.getOOoConnection() != null) {
- return true;
- }
- } catch (NoConnectionException pEx) {
- throw new RuntimeException(MSG_ERRO_OPEN_OFFICE, pEx);
- }
- }
- return false;
- }
- /**
- * Move o cursor para o fim do documento.
- * @throws Exception
- */
- public void moverCursorFimDocumento() {
- createTextCursor().gotoEnd(false);
- }
- /**
- * Move o cursor para o início do documento.
- * @throws Exception
- */
- public void moverCursorInicioDocumento() {
- createTextCursor().gotoStart(false);
- }
- /**
- * Move o cursor para um range especificado, mas não estando selecionado.
- * @param pRange
- * @throws Exception
- */
- public void moverCursorParaRange(XTextRange pRange) {
- getTextViewCursor().gotoRange(pRange, false);
- }
- public String[] obterReferencias() {
- try {
- XReferenceMarksSupplier lRefSupplier = UnoRuntime.queryInterface(XReferenceMarksSupplier.class, getOfficeBean()
- .getDocument());
- return lRefSupplier.getReferenceMarks().getElementNames();
- } catch (Exception pEx) {
- throw new RuntimeException(MSG_ERRO_OPEN_OFFICE, pEx);
- }
- }
- public File salvarEmPDFTemporario() {
- PropertyValue[] props = new PropertyValue[1];
- props[0] = new PropertyValue();
- props[0].Name = "FilterName";
- props[0].Value = "writer_pdf_Export";
- try {
- File file = File.createTempFile(PREFIXO_ARQUIVO, ".pdf");
- file.deleteOnExit();
- FileOutputStream fileOS = new FileOutputStream(file);
- bean.storeToStream(fileOS, props);
- fileOS.flush();
- return file;
- } catch (Exception pEx) {
- throw new RuntimeException(MSG_ERRO_OPEN_OFFICE, pEx);
- }
- }
- /**
- * Adiciona uma lista de XTransferables para um range destino.
- * @throws Exception
- */
- public void selecionar(XTextRange posicaoInicial, XTextRange posicaoFinal) {
- XTextViewCursor cursor = getTextViewCursor();
- cursor.gotoRange(posicaoInicial, false);
- cursor.gotoRange(posicaoFinal, true);
- }
- /**
- * Seleciona um conteúdo de um XTextRange.
- * @param pRange
- * @throws Exception
- */
- public void selecionarConteudo(XTextRange pRange) {
- getTextViewCursor().gotoRange(pRange, true);
- }
- public void setEstiloParagrafo(String paragraphStyleName) {
- XPropertySet xTextProps = UnoRuntime.queryInterface(XPropertySet.class, getTextViewCursor());
- try {
- xTextProps.setPropertyValue("ParaStyleName", paragraphStyleName);
- } catch (Exception e) {
- throw new RuntimeException(MSG_ERRO_OPEN_OFFICE, e);
- }
- }
- public void setTransferable(XTransferable pTransferable) {
- try {
- getTransferableSupplier().insertTransferable(pTransferable);
- } catch (Exception pEx) {
- throw new RuntimeException(MSG_ERRO_OPEN_OFFICE, pEx);
- }
- }
- public void setValorCampo(String fieldName, String fieldValue) {
- try {
- XTextFieldsSupplier xTextFieldsSupplier = UnoRuntime.queryInterface(XTextFieldsSupplier.class,
- getOfficeDocument());
- XNameAccess xNamedFieldMasters = xTextFieldsSupplier.getTextFieldMasters();
- Object fieldMaster = xNamedFieldMasters.getByName("com.sun.star.text.fieldmaster.User." + fieldName);
- XPropertySet xPropertySet = UnoRuntime.queryInterface(XPropertySet.class, fieldMaster);
- xPropertySet.setPropertyValue("Content", fieldValue);
- } catch (Exception e) {
- e.printStackTrace();
- throw new RuntimeException(MSG_ERRO_OPEN_OFFICE, e);
- }
- }
- /**
- * Substitui o conteúdo no intervalo entre pRangeInicio e pRangeFim
- * @param pNovoTexto - Novo conteúdo que ficará entre os ranges
- * @param pRangeInicio
- * @param pRangeFim
- * @throws Exception
- */
- public void substituirConteudoRange(String pNovoTexto, XTextRange pRangeInicio, XTextRange pRangeFim) {
- // Obtém o cursor
- XTextViewCursor lCursor = getTextViewCursor();
- lCursor.gotoRange(pRangeInicio.getStart(), false);
- lCursor.gotoRange(pRangeFim.getEnd(), true);
- // Cria um cursor para o texto selecionado para permitir o funcionamento em estruturas como
- // tabela:
- XTextCursor lCursorTexto = lCursor.getText().createTextCursor();
- lCursorTexto.gotoRange(pRangeInicio.getStart(), false);
- lCursorTexto.gotoRange(pRangeFim.getEnd(), true);
- lCursorTexto.setString(pNovoTexto);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement