com.sun.star.uno.UnoRuntime Java Examples

The following examples show how to use com.sun.star.uno.UnoRuntime. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: DocumentWriter.java    From noa-libre with GNU Lesser General Public License v2.1 6 votes vote down vote up
/**
 * Stores document on the basis of the submitted xOutputStream implementation.
 * 
 * @param document document to be stored
 * @param xOutputStream OpenOffice.org XOutputStream inplementation
 * @param properties properties for OpenOffice.org
 * 
 * @throws IOException if any error occurs
 */
public static void storeDocument(IDocument document, XOutputStream xOutputStream, PropertyValue[] properties) 
throws IOException {
  if(properties == null) {
    properties = new PropertyValue[0];
  }
  
  PropertyValue[] newProperties = new PropertyValue[properties.length + 1];
  for(int i=0; i<properties.length; i++) {
    newProperties[i] = properties[i];
  }
  newProperties[properties.length] = new PropertyValue(); 
  newProperties[properties.length].Name = "OutputStream"; 
  newProperties[properties.length].Value = xOutputStream;
  
  XStorable xStorable = (XStorable)UnoRuntime.queryInterface(XStorable.class, document.getXComponent());
  try {
    xStorable.storeToURL("private:stream", newProperties);
  }
  catch(com.sun.star.io.IOException ioException) {
    throw new IOException(ioException.getMessage());
  }    
}
 
Example #2
Source File: ApplicationInfo.java    From noa-libre with GNU Lesser General Public License v2.1 6 votes vote down vote up
/**
 * This method can be used to get any info from the application that is available.
 * (Possible paths and keys can be retrieved with the dumpInfo method).
 * Returns the object described by the path and key, or <code>null</code> if not available.
 * 
 * @param path the path to the key information
 * @param key the key to get the value for
 * 
 * @return the object described by the path and key, or <code>null</code> if not available
 * 
 * @throws Exception if retreiving the value fails
 * 
 * @author Markus Krüger
 * @date 18.11.2008
 */
public Object getInfo(String path, String key) throws Exception {
  Object configProviderObject = serviceProvider.createService("com.sun.star.comp.configuration.ConfigurationProvider");
  XMultiServiceFactory xConfigServiceFactory = (XMultiServiceFactory) UnoRuntime.queryInterface(XMultiServiceFactory.class,
      configProviderObject);
  String readConfAccess = "com.sun.star.configuration.ConfigurationAccess";
  PropertyValue[] properties = new PropertyValue[1];
  properties[0] = new PropertyValue();
  properties[0].Name = "nodepath";
  properties[0].Value = path;
  Object configReadAccessObject = xConfigServiceFactory.createInstanceWithArguments(readConfAccess,
      properties);
  XNameAccess xConfigNameAccess = (XNameAccess) UnoRuntime.queryInterface(XNameAccess.class,
      configReadAccessObject);
  return xConfigNameAccess.getByName(key);
}
 
Example #3
Source File: TextTableService.java    From noa-libre with GNU Lesser General Public License v2.1 6 votes vote down vote up
/**
 * Returns table with the submitted name.
 * 
 * @param name name of the table
 * 
 * @return table with the submitted name
 * 
 * @throws TextException if the table does not exist
 * 
 * @author Andreas Bröker
 */
public ITextTable getTextTable(String name) throws TextException {
  try {
    XTextTablesSupplier xTextTablesSupplier = (XTextTablesSupplier)UnoRuntime.queryInterface(XTextTablesSupplier.class, textDocument.getXTextDocument());
    XNameAccess xNameAccess = xTextTablesSupplier.getTextTables();
    Any any = (Any)xNameAccess.getByName(name);
    XTextTable textTable = (XTextTable)any.getObject();
    if(textTable.getColumns().getCount() <= ITextTable.MAX_COLUMNS_IN_TABLE) {
    	return new TextTable(textDocument, textTable);
    }
    else {
    	throw new TextException("The submitted table is not valid");
    }
  }
  catch(Exception exception) {
    TextException textException = new TextException(exception.getMessage());
    textException.initCause(exception);
    throw textException;
  }
}
 
Example #4
Source File: NumberFormatService.java    From noa-libre with GNU Lesser General Public License v2.1 6 votes vote down vote up
/**
 * Returns number format on the basis of the submitted key.
 * 
 * @param key key of the number format
 * 
 * @return number format on the basis of the submitted key
 * 
 * @throws UtilException if the number format is not available
 * 
 * @author Andreas Bröker
 * @author Markus Krüger
 */
public INumberFormat getNumberFormat(int key) throws UtilException {
  INumberFormat[] allFormats = getNumberFormats();
  for(int i = 0; i < allFormats.length; i++) {
    if(key == allFormats[i].getFormatKey())
      return allFormats[i];
  }
  try {
    XNumberFormats xNumberFormats = xNumberFormatsSupplier.getNumberFormats();
    XPropertySet docProps = (XPropertySet)UnoRuntime.queryInterface(XPropertySet.class, textDocument.getXTextDocument());
    Locale docLocale = (Locale) docProps.getPropertyValue("CharLocale");
    XNumberFormatTypes numberFormatTypes = (XNumberFormatTypes)UnoRuntime.queryInterface(XNumberFormatTypes.class, xNumberFormats);
    int newKey = numberFormatTypes.getFormatForLocale(key,docLocale);
    for(int i = 0; i < allFormats.length; i++) {
      if(newKey == allFormats[i].getFormatKey())
        return allFormats[i];
    }
  }
  catch(Exception exception) {
    UtilException utilException = new UtilException(exception.getMessage());
    utilException.initCause(exception);
    throw utilException;
  }
  throw new UtilException("The number format is not available.");
}
 
Example #5
Source File: MemoryUsage.java    From kkFileView with Apache License 2.0 6 votes vote down vote up
private XSpreadsheet createSpreadsheet(XScriptContext ctxt)
    throws Exception
{
    XComponentLoader loader = (XComponentLoader)
        UnoRuntime.queryInterface(
            XComponentLoader.class, ctxt.getDesktop());

    XComponent comp = loader.loadComponentFromURL(
        "private:factory/scalc", "_blank", 4, new PropertyValue[0]);

    XSpreadsheetDocument doc = (XSpreadsheetDocument)
        UnoRuntime.queryInterface(XSpreadsheetDocument.class, comp);

    XIndexAccess index = (XIndexAccess)
        UnoRuntime.queryInterface(XIndexAccess.class, doc.getSheets());

    XSpreadsheet sheet = (XSpreadsheet) AnyConverter.toObject(
        new Type(com.sun.star.sheet.XSpreadsheet.class), index.getByIndex(0));

    return sheet;
}
 
Example #6
Source File: TextTable.java    From noa-libre with GNU Lesser General Public License v2.1 6 votes vote down vote up
/**
 * Returns cell range on the basis submitted index informations.
 * 
 * @param firstColumnIndex index of first column inside the range
 * @param firstRowIndex index of first row inside the range
 * @param lastColumnIndex index of last column inside the range
 * @param lastRowIndex index of last row inside the range
 * 
 * @return cell range on the basis submitted index informations
 * 
 * @throws TextException if the cell range is not available
 * 
 * @author Andreas Bröker
 */
public ITextTableCellRange getCellRange(int firstColumnIndex, int firstRowIndex,
    int lastColumnIndex, int lastRowIndex) throws TextException {
  String cellRangeName = TextTableCellNameHelper.getRangeName(firstColumnIndex,
      firstRowIndex,
      lastColumnIndex,
      lastRowIndex);
  try {
    if (xCellRange == null)
      xCellRange = (XCellRange) UnoRuntime.queryInterface(XCellRange.class, xTextTable);
    XCellRange newXCellRange = xCellRange.getCellRangeByPosition(firstColumnIndex,
        firstRowIndex,
        lastColumnIndex,
        lastRowIndex);
    TextTableCellRangeName textTableCellRangeName = new TextTableCellRangeName(cellRangeName);
    TextTableCellRange textTableCellRange = new TextTableCellRange(textDocument,
        newXCellRange,
        textTableCellRangeName);
    return textTableCellRange;
  }
  catch (Exception exception) {
    TextException textException = new TextException(exception.getMessage());
    textException.initCause(exception);
    throw textException;
  }
}
 
Example #7
Source File: Frame.java    From noa-libre with GNU Lesser General Public License v2.1 6 votes vote down vote up
/**
 * Returns dispatch for the submitted command URL.
 * 
 * @param commandURL command URL of the dispatch
 * 
 * @return dispatch for the submitted command URL
 * 
 * @throws NOAException if a dispatch for the submitted command URL
 * can not be provided
 * 
 * @author Andreas Bröker
 * @date 14.06.2006
 */
public IDispatch getDispatch(String commandURL) throws NOAException {
	if(commandURL == null)
		throw new NOAException("The command URL is not valid.");
	try {  		
 	XDispatchProvider xDispatchProvider = (XDispatchProvider)UnoRuntime.queryInterface(XDispatchProvider.class, xFrame);
 	URL[] urls = new URL[1];
 	urls[0] = new URL();
 	urls[0].Complete = commandURL;
 	Object service = null;
 	if(officeConnection != null)
 	  service = officeConnection.createService("com.sun.star.util.URLTransformer");
 	else
 	  service = serviceProvider.createService("com.sun.star.util.URLTransformer");
 	XURLTransformer xURLTranformer = (XURLTransformer)UnoRuntime.queryInterface(XURLTransformer.class, service);
 	xURLTranformer.parseStrict(urls);
 	XDispatch xDispatch = xDispatchProvider.queryDispatch(urls[0], "", FrameSearchFlag.GLOBAL);	  	
 	if(xDispatch == null)
 		throw new NOAException("The command URL is not valid");
 	return new Dispatch(xDispatch, urls[0]);
	}
	catch(Throwable throwable) {
		throw new NOAException(throwable);
	}
}
 
Example #8
Source File: TextTableRow.java    From noa-libre with GNU Lesser General Public License v2.1 6 votes vote down vote up
/**
 * Returns the row height.
 * 
 * @return the row height
 * 
 * @author Markus Krüger
 */
public int getHeight() {
  if(textTableCellRange == null)
    return 0;
  try {
    
    XTextTable xTextTable = (XTextTable)textTableCellRange.getCell(0,0).getTextTable().getXTextContent();
    XTableRows tableRows = xTextTable.getRows();
    Object row = tableRows.getByIndex(textTableCellRange.getRangeName().getRangeStartRowIndex());
    XPropertySet propertySetRow = (XPropertySet)UnoRuntime.queryInterface(XPropertySet.class, row);
    Integer rowHeight = (Integer)propertySetRow.getPropertyValue("Height");
    return rowHeight.intValue();
  }
  catch (Exception exception) {
    return 0;
  }
}
 
Example #9
Source File: TextFieldService.java    From noa-libre with GNU Lesser General Public License v2.1 6 votes vote down vote up
/**
 * Returns all available user textfields.
 * 
 * @return all available user textfields
 * 
 * @throws TextException
 *             if the user textfields can not be constructed
 * 
 * @author Andreas Bröker
 */
public ITextField[] getUserTextFields() throws TextException {
	try {
		XTextFieldsSupplier xTextFieldsSupplier = (XTextFieldsSupplier) UnoRuntime
				.queryInterface(XTextFieldsSupplier.class,
						textDocument.getXTextDocument());
		XEnumerationAccess xEnumerationAccess = xTextFieldsSupplier
				.getTextFields();
		XEnumeration xEnumeration = xEnumerationAccess.createEnumeration();
		ArrayList arrayList = new ArrayList();
		while (xEnumeration.hasMoreElements()) {
			Object object = xEnumeration.nextElement();
			XTextField xTextField = (XTextField) UnoRuntime.queryInterface(
					XTextField.class, object);
			arrayList.add(new TextField(textDocument, xTextField));
		}
		return (ITextField[]) arrayList.toArray(new ITextField[arrayList
				.size()]);
	} catch (Exception exception) {
		throw new TextException(exception);
	}
}
 
Example #10
Source File: TextTableCell.java    From noa-libre with GNU Lesser General Public License v2.1 6 votes vote down vote up
/**
 * Returns text table of the cell.
 * 
 * @return text table of the cell
 * 
 * @throws TextException if the text table is not available
 * 
 * @author Andreas Bröker
 * @author Markus Krüger
 */
public ITextTable getTextTable() throws TextException {
  if(textTable == null) {
    try {
      XText xText = (XText)UnoRuntime.queryInterface(XText.class, xCell);
      XPropertySet xPropertySet = (XPropertySet)UnoRuntime.queryInterface(XPropertySet.class, xText.getStart());
      Any any = (Any)xPropertySet.getPropertyValue("TextTable");
      XTextTable xTextTable = (XTextTable)any.getObject();
      textTable =  new TextTable(textDocument, xTextTable);
    }
    catch(Exception exception) {
      TextException textException = new TextException(exception.getMessage());
      textException.initCause(exception);
      throw textException;
    }
  }
  return textTable;
}
 
Example #11
Source File: DialogHelper.java    From libreoffice-starter-extension with MIT License 6 votes vote down vote up
/** Returns a URL to be used with XDialogProvider to create a dialog */
public static String convertToURL(XComponentContext xContext, File dialogFile) {
	String sURL = null;
	try {
		com.sun.star.ucb.XFileIdentifierConverter xFileConverter = (com.sun.star.ucb.XFileIdentifierConverter) UnoRuntime
				.queryInterface(com.sun.star.ucb.XFileIdentifierConverter.class, xContext.getServiceManager()
						.createInstanceWithContext("com.sun.star.ucb.FileContentProvider", xContext));
		sURL = xFileConverter.getFileURLFromSystemPath("", dialogFile.getAbsolutePath());
	} catch (com.sun.star.uno.Exception ex) {
		return null;
	}
	return sURL;
}
 
Example #12
Source File: DocumentService.java    From noa-libre with GNU Lesser General Public License v2.1 6 votes vote down vote up
/**
 * Returns current documents of an application.
 * 
 * @return documents of an application
 * 
 * @throws DocumentException if the documents cannot be provided
 * 
 * @author Markus Krüger
 * @date 11.11.2008
 */
public static IDocument[] getCurrentDocuments(IServiceProvider serviceProvider)
    throws DocumentException {
  try {
    if (serviceProvider == null)
      return new IDocument[0];
    Object desktop = serviceProvider.createService("com.sun.star.frame.Desktop"); //$NON-NLS-1$
    XDesktop xDesktop = (XDesktop) UnoRuntime.queryInterface(XDesktop.class, desktop);
    XEnumeration aktComponents = xDesktop.getComponents().createEnumeration();
    List arrayList = new ArrayList();
    while (aktComponents.hasMoreElements()) {
      Any a = (Any) aktComponents.nextElement();
      arrayList.add(DocumentLoader.getDocument((XComponent) a.getObject(), serviceProvider, null));
    }
    IDocument[] documents = new IDocument[arrayList.size()];
    documents = (IDocument[]) arrayList.toArray(documents);
    return documents;
  }
  catch (Exception exception) {
    throw new DocumentException(exception);
  }
}
 
Example #13
Source File: TextDocument.java    From noa-libre with GNU Lesser General Public License v2.1 6 votes vote down vote up
/**
 * Returns number format service.
 * 
 * @return number format service
 * 
 * @author Andreas Bröker
 */
public INumberFormatService getNumberFormatService() {
  if (numberFormatService == null) {
    XNumberFormatsSupplier xNumberFormatsSupplier = (XNumberFormatsSupplier) UnoRuntime.queryInterface(XNumberFormatsSupplier.class,
        xTextDocument);
    numberFormatService = new NumberFormatService(this, xNumberFormatsSupplier);

    //TODO workaround, otherwise the numberformat may not be recognized corretly
    try {
      Thread.sleep(500);
    }
    catch (InterruptedException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
  }
  return numberFormatService;
}
 
Example #14
Source File: LayoutManager.java    From noa-libre with GNU Lesser General Public License v2.1 6 votes vote down vote up
/**
 * Hide UI element with the submitted resource URL.
 * 
 * @param resourceURL URL of the UI resource to be hidden
 * @param if changes should be persistent
 * 
 * @return information whether the UI resource is hidden after method call
 * 
 * @author Markus Krüger
 * @date 06.05.2010
 */
public boolean hideElement(String resourceURL, boolean persistent) {
  if (resourceURL != null) {
    try {
      XUIElement element = xLayoutManager.getElement(resourceURL);
      if (element != null) {
        XPropertySet xps = (XPropertySet) UnoRuntime.queryInterface(XPropertySet.class, element);
        xps.setPropertyValue("Persistent", new Boolean(persistent));
        return xLayoutManager.hideElement(resourceURL);
      }
    }
    catch (Exception e) {
      //ignore and return false
    }
  }
  return false;

}
 
Example #15
Source File: TextRange.java    From noa-libre with GNU Lesser General Public License v2.1 6 votes vote down vote up
/**
 * Returns related page style of the text range.
 * 
 * @return page style of the text range
 * 
 * @throws TextException if the page style is not available
 * 
 * @author Andreas Bröker
 */
public IPageStyle getPageStyle() throws TextException {
  if(document == null || !(document instanceof ITextDocument))
  	throw new TextException("Text style not available");
	try {
    XPropertySet xPropertySet = (XPropertySet)UnoRuntime.queryInterface(XPropertySet.class, xTextRange);
    String pageStyleName = xPropertySet.getPropertyValue("PageStyleName").toString();
    XStyleFamiliesSupplier xStyleFamiliesSupplier = (XStyleFamiliesSupplier)UnoRuntime.queryInterface(XStyleFamiliesSupplier.class, ((ITextDocument)document).getXTextDocument());
    XNameAccess xNameAccess = xStyleFamiliesSupplier.getStyleFamilies();
    Any any = (Any)xNameAccess.getByName("PageStyles");
    XNameContainer xNameContainer = (XNameContainer)any.getObject();
    any = (Any)xNameContainer.getByName(pageStyleName);
    XStyle style = (XStyle)any.getObject();
    return new PageStyle(style);
  }
  catch(Exception exception) {
    TextException textException = new TextException(exception.getMessage());
    textException.initCause(exception);
    throw textException;
  }
}
 
Example #16
Source File: AbstractDocument.java    From noa-libre with GNU Lesser General Public License v2.1 6 votes vote down vote up
/**
 * Removes listener for closing events to the document.
 * 
 * @param closeListener
 *            close listener
 * 
 * @author Andreas Bröker
 * @author Markus Krüger
 */
public void removeCloseListener(ICloseListener closeListener) {
	if (closeListener == null) {
		return;
	}

	if (closeListeners == null) {
		return;
	}

	if (closeListeners.containsKey(closeListener)) {
		CloseListenerWrapper closeListenerWrapper = (CloseListenerWrapper) closeListeners
				.get(closeListener);
		if (closeListenerWrapper != null) {
			XCloseable xCloseable = (XCloseable) UnoRuntime.queryInterface(
					XCloseable.class, xComponent);
			if (xCloseable != null) {
				xCloseable.removeCloseListener(closeListenerWrapper);
			}
		}
	}
}
 
Example #17
Source File: DocumentIndexService.java    From noa-libre with GNU Lesser General Public License v2.1 6 votes vote down vote up
/**
 * Returns available document indexes.
 * 
 * @return available document indexes
 * 
 * @author Andreas Bröker
 * @date 17.08.2006
 */
public IDocumentIndex[] getDocumentIndexes() {
  XDocumentIndexesSupplier documentIndexesSupplier = (XDocumentIndexesSupplier)UnoRuntime.queryInterface(XDocumentIndexesSupplier.class, textDocument);
  if(documentIndexesSupplier == null)
    return new IDocumentIndex[0];
  
  XIndexAccess indexAccess = documentIndexesSupplier.getDocumentIndexes();
  List list = new ArrayList();
  for(int i=0, n=indexAccess.getCount(); i<n; i++) {
    try {
      Object object = indexAccess.getByIndex(i);
      XDocumentIndex documentIndex = (XDocumentIndex)UnoRuntime.queryInterface(XDocumentIndex.class, object);
      if(documentIndex != null)
        list.add(new DocumentIndex(documentIndex));
    }
    catch(Throwable throwable) {
      //do not consume
    }
  }
  return (IDocumentIndex[])list.toArray(new IDocumentIndex[list.size()]);
}
 
Example #18
Source File: VariableTextFieldHelper.java    From noa-libre with GNU Lesser General Public License v2.1 6 votes vote down vote up
/**
 * Applies the given number format to the given variable text field.
 * 
 * @param numberFormat the number format to be set
 * @param variableTextField the variable text field to set number forma for
 * @param isFormula if the variable text field is a formula
 * @param numberFormatService the number format service to be used
 * 
 * @throws NOAException if setting the number format fails
 * 
 * @author Markus Krüger
 * @date 27.07.2007
 */
public static void applyNumberFormat(INumberFormat numberFormat, ITextField variableTextField,
    boolean isFormula, INumberFormatService numberFormatService) throws NOAException {
  try {
    if(numberFormat == null || variableTextField == null || numberFormatService == null)
      return;
    XPropertySet xPropertySetField = 
      (XPropertySet)UnoRuntime.queryInterface(XPropertySet.class, variableTextField.getXTextContent());
    String content = (String)xPropertySetField.getPropertyValue("Content");      
    xPropertySetField.setPropertyValue("NumberFormat", new Integer(numberFormat.getFormatKey()));
    setContent(content,variableTextField,isFormula,numberFormatService);
  }
  catch(Throwable throwable) {
    throw new NOAException(throwable);
  }
}
 
Example #19
Source File: PrintService.java    From noa-libre with GNU Lesser General Public License v2.1 6 votes vote down vote up
/**
 * Returns the active printer.
 * 
 * @return the active printer
 * 
 * @throws NOAException if printer could not be retrieved
 * 
 * @author Markus Krüger
 * @date 16.08.2007
 */
public IPrinter getActivePrinter() throws NOAException {
  try {
    XPrintable xPrintable = (XPrintable)UnoRuntime.queryInterface(XPrintable.class, document.getXComponent());
    PropertyValue[] printerProps = xPrintable.getPrinter();
    String name = null;
    for(int i = 0; i < printerProps.length; i++) {
      if(printerProps[i].Name.equals("Name"))
        name = (String)printerProps[i].Value;
    }
    return new Printer(name);
  }
  catch(Throwable throwable) {
    throw new NOAException(throwable);
  }
}
 
Example #20
Source File: PrintService.java    From noa-libre with GNU Lesser General Public License v2.1 6 votes vote down vote up
/**
 * Returns if the active printer is busy.
 * 
 * @return if the active printer is busy
 * 
 * @throws NOAException if the busy state could not be retrieved
 * 
 * @author Markus Krüger
 * @date 16.08.2007
 */
public boolean isActivePrinterBusy() throws NOAException {
  try {
    XPrintable xPrintable = (XPrintable)UnoRuntime.queryInterface(XPrintable.class, document.getXComponent());
    PropertyValue[] printerProps = xPrintable.getPrinter();
    Boolean busy = new Boolean(false);
    for(int i = 0; i < printerProps.length; i++) {
      if(printerProps[i].Name.equals("IsBusy"))
        busy = (Boolean)printerProps[i].Value;
    }
    return busy.booleanValue();
  }
  catch(Throwable throwable) {
    throw new NOAException(throwable);
  }
}
 
Example #21
Source File: DocumentExporter.java    From noa-libre with GNU Lesser General Public License v2.1 6 votes vote down vote up
/**
 * Exports document on the basis of the submitted filter and URL.
 * 
 * @param document document to be exported
 * @param URL URL to be used
 * @param filter OpenOffice.org filter to be used
 * @param properties properties properties for OpenOffice.org
 * 
 * @throws IOException if any error occurs
 */
public static void exportDocument(IDocument document, String URL, IFilter filter, PropertyValue[] properties) 
throws IOException {
  if(properties == null) {
    properties = new PropertyValue[0];
  }
  PropertyValue[] newProperties = new PropertyValue[properties.length + 1];
  for(int i=0; i<properties.length; i++) {
    newProperties[i] = properties[i];
  }
      
  newProperties[properties.length] = new PropertyValue(); 
  newProperties[properties.length].Name = "FilterName"; 
  newProperties[properties.length].Value = filter.getFilterDefinition(document);
  
  XStorable xStorable = (XStorable)UnoRuntime.queryInterface(XStorable.class, document.getXComponent());
  try {
    xStorable.storeToURL(URL, newProperties);
  }
  catch(com.sun.star.io.IOException ioException) {
    throw new IOException(ioException.getMessage());
  }    
}
 
Example #22
Source File: Paragraph.java    From noa-libre with GNU Lesser General Public License v2.1 6 votes vote down vote up
/**
 * Gets the text contained in this pragraph
 * 
 * @return the paragraph text or null if text cannot be gained
 * 
 * @throws TextException if there occurs an error while fetching the text
 * 
 * @author Sebastian Rösgen 
 */
public String getParagraphText() throws TextException {
  StringBuffer buffer = new StringBuffer();
  XEnumerationAccess contentEnumerationAccess = (XEnumerationAccess) UnoRuntime.queryInterface(XEnumerationAccess.class,
      getXTextContent());
  XEnumeration enumeration = contentEnumerationAccess.createEnumeration();

  while (enumeration.hasMoreElements()) {
    try {
      Any any = (Any) enumeration.nextElement();
      XTextRange content = (XTextRange) any.getObject();

      // since one paragraph can be made out of several portions, we have to put'em together
      buffer.append(content.getString());
    }
    catch (Exception exception) {
      System.out.println("Error getting elements from enumeration while search paragraph text.");
    }
  }

  return buffer.toString();
}
 
Example #23
Source File: FormService.java    From noa-libre with GNU Lesser General Public License v2.1 5 votes vote down vote up
/**
 * Returns the forms for the given form name as an array. There will be only more 
 * than one form if the given form name occurs more than once.
 * 
 * @param formName the form name to be used
 * 
 * @return the forms for the given form name as an array
 * 
 * @throws NOAException if the return of forms fails
 * 
 * @author Markus Krüger
 * @date 26.01.2007
 */
public IForm[] getForms(String formName) throws NOAException {
  try {
    if(formName != null) {
      XFormsSupplier formsSupplier = (XFormsSupplier) UnoRuntime.queryInterface(XFormsSupplier.class, xDrawPage);
      if(formsSupplier != null) {
        XNameContainer nameContainer = formsSupplier.getForms();
        XIndexContainer indexContainer = (XIndexContainer) UnoRuntime.queryInterface(XIndexContainer.class, nameContainer);
        int len = indexContainer.getCount();
        List forms = new ArrayList();
        for(int i = 0; i < len; i++) {
          Object tmpForm = indexContainer.getByIndex(i);
          if(tmpForm != null) {              
            XNamed tmpNamed = 
              (XNamed) UnoRuntime.queryInterface(XNamed.class, tmpForm);
            if(tmpNamed != null && tmpNamed.getName().equalsIgnoreCase(formName)) {
              XFormComponent tmpFormComponent = 
                (XFormComponent) UnoRuntime.queryInterface(XFormComponent.class, tmpForm);
              if(tmpFormComponent != null) {
                forms.add(new Form(document,null,tmpFormComponent));
              }
            }
          }
        }
        return (IForm[]) forms.toArray(new IForm[forms.size()]);
      }
    }
    return new IForm[0];
  }
  catch(Throwable throwable) {
    throw new NOAException(throwable);
  }
}
 
Example #24
Source File: FormService.java    From noa-libre with GNU Lesser General Public License v2.1 5 votes vote down vote up
/**
 * Returns the index of the given form component in the given form, or -1 if not found.
 * 
 * @param form the form to check index in
 * @param formComponent the form component to get index for
 * 
 * @return the index of the given form component in the given form, or -1
 * 
 * @throws NOAException if anything fails
 * 
 * @author Markus Krüger
 * @date 25.01.2007
 */
public int getIndexInForm(IForm form, IFormComponent formComponent) throws NOAException {
  try {
    if(form!= null && formComponent != null) {
      XFormsSupplier formsSupplier = (XFormsSupplier) UnoRuntime.queryInterface(XFormsSupplier.class, xDrawPage);
      if(formsSupplier != null) {
        XNameContainer nameContainer = formsSupplier.getForms();
        XIndexContainer indexContainer = (XIndexContainer) UnoRuntime.queryInterface(XIndexContainer.class, nameContainer);
        int len = indexContainer.getCount();
        for(int i = 0; i < len; i++) {
          XForm tmpForm = (XForm) UnoRuntime.queryInterface(XForm.class, indexContainer.getByIndex(i));
          if(tmpForm != null && UnoRuntime.areSame(form.getXFormComponent(),tmpForm)) {    
            XIndexContainer container = (XIndexContainer) UnoRuntime.queryInterface(XIndexContainer.class, tmpForm);
            int lenFormPComponents = container.getCount();           
            for(int j = 0; j < lenFormPComponents; j++) {
              Object tmpObject = container.getByIndex(j);
              if(tmpObject != null) {
                XFormComponent tmpFormComponent = (XFormComponent) UnoRuntime.queryInterface(XFormComponent.class, tmpObject);
                if(tmpFormComponent != null && UnoRuntime.areSame(tmpFormComponent,formComponent.getXFormComponent()))
                  return j;
              }
            }
          }
        }
      }        
    }
    return -1;
  }
  catch(Throwable throwable) {
    throw new NOAException(throwable);
  }
  
}
 
Example #25
Source File: PagePosition.java    From noa-libre with GNU Lesser General Public License v2.1 5 votes vote down vote up
/**
 * Returns content type of the page position.
 * 
 * @return content type of the page position
 * 
 * @author Andreas Bröker
 */
public short getContentType() {
  XCell xCell = (XCell)UnoRuntime.queryInterface(XCell.class, xTextRange.getText());
  if(xCell == null) {
    return PLAIN_TEXT;
  }
  return TEXT_TABLE_CELL;
}
 
Example #26
Source File: PropertyCollection.java    From noa-libre with GNU Lesser General Public License v2.1 5 votes vote down vote up
/**
 * Sets property collection to submitted OpenOffice.org Uno object.   
 * 
 * @param propertyCollection property collection
 * @param object OpenOffice.org Uno object
 * @param orderProperties property names to be set with the submitted order
 * @param excludeProperties property names to be excluded
 */
public static void setPropertyCollection(PropertyCollection propertyCollection, Object object, String[] orderProperties, String[] excludeProperties) {
  try {
    XPropertySet xPropertySet = (XPropertySet)UnoRuntime.queryInterface(XPropertySet.class, object);
    if(xPropertySet != null) {
      setPropertyCollection(propertyCollection, xPropertySet, orderProperties, excludeProperties);
    }
  }
  catch(Exception exception) {
    //ignore
  }
}
 
Example #27
Source File: TextTableCell.java    From noa-libre with GNU Lesser General Public License v2.1 5 votes vote down vote up
/**
 * Returns text table cell properties.
 * 
 * @return text table cell properties
 * 
 * @author Andreas Bröker
 */
public ITextTableCellProperties getProperties() {
  if(textTableCellProperties == null) {
    XPropertySet xPropertySet = (XPropertySet)UnoRuntime.queryInterface(XPropertySet.class, xCell);
    textTableCellProperties = new TextTableCellProperties(xPropertySet);
  }
  return textTableCellProperties;
}
 
Example #28
Source File: ViewCursor.java    From noa-libre with GNU Lesser General Public License v2.1 5 votes vote down vote up
/**
 * Returns the line cursor for the view cursor, can be null if no line cursor is available.
 * 
 * @return the line cursor for the view cursor, can be null
 * 
 * @author Markus Krüger
 */
public ILineCursor getLineCursor() {
  if (lineCursor == null) {
    XLineCursor xLineCursor = (XLineCursor) UnoRuntime.queryInterface(XLineCursor.class,
        xTextViewCursor);
    if (xLineCursor != null) {
      lineCursor = new LineCursor(xLineCursor);
    }
  }
  return lineCursor;
}
 
Example #29
Source File: StreamOpenOfficeDocumentConverter.java    From tmxeditor8 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Load and export.
 * @param inputStream
 *            the input stream
 * @param importOptions
 *            the import options
 * @param outputStream
 *            the output stream
 * @param exportOptions
 *            the export options
 * @throws Exception
 *             the exception
 */
@SuppressWarnings("unchecked")
private void loadAndExport(InputStream inputStream, Map/* <String,Object> */importOptions,
		OutputStream outputStream, Map/* <String,Object> */exportOptions) throws Exception {
	XComponentLoader desktop = openOfficeConnection.getDesktopObject();

	Map/* <String,Object> */loadProperties = new HashMap();
	loadProperties.putAll(getDefaultLoadProperties());
	loadProperties.putAll(importOptions);
	// doesn't work using InputStreamToXInputStreamAdapter; probably because
	// it's not XSeekable
	// property("InputStream", new
	// InputStreamToXInputStreamAdapter(inputStream))
	loadProperties.put("InputStream", new ByteArrayToXInputStreamAdapter(IOUtils.toByteArray(inputStream))); //$NON-NLS-1$

	XComponent document = desktop.loadComponentFromURL(
			"private:stream", "_blank", 0, toPropertyValues(loadProperties)); //$NON-NLS-1$ //$NON-NLS-2$
	if (document == null) {
		throw new OPException(Messages.getString("ooconverter.StreamOpenOfficeDocumentConverter.6")); //$NON-NLS-1$
	}

	refreshDocument(document);

	Map/* <String,Object> */storeProperties = new HashMap();
	storeProperties.putAll(exportOptions);
	storeProperties.put("OutputStream", new OutputStreamToXOutputStreamAdapter(outputStream)); //$NON-NLS-1$

	try {
		XStorable storable = (XStorable) UnoRuntime.queryInterface(XStorable.class, document);
		storable.storeToURL("private:stream", toPropertyValues(storeProperties)); //$NON-NLS-1$
	} finally {
		document.dispose();
	}
}
 
Example #30
Source File: ServiceInterfaceSupplier.java    From noa-libre with GNU Lesser General Public License v2.1 5 votes vote down vote up
/**
 * Returns the XDesktop interface from the OpenOffice.org desktop service.
 * 
 * @param xMultiServiceFactory factory in order to construct the service
 * 
 * @return XDesktop interface from the OpenOffice.org desktop service
 * 
 * @throws Exception if any error occurs
 */
public static XDesktop getXDesktop(XMultiServiceFactory xMultiServiceFactory) throws Exception {
	Object service = xMultiServiceFactory.createInstance("com.sun.star.frame.Desktop");
	if(service != null) {
		return (XDesktop)UnoRuntime.queryInterface(XDesktop.class, service);
	}
	else {
		return null;
	}
}