Java Code Examples for com.sun.star.uno.UnoRuntime#queryInterface()

The following examples show how to use com.sun.star.uno.UnoRuntime#queryInterface() . 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: TextTableService.java    From noa-libre with GNU Lesser General Public License v2.1 6 votes vote down vote up
/**
 * Returns all available text tables.
 * 
 * @return all available text tables
 * 
 * @author Andreas Bröker
 */
public ITextTable[] getTextTables() {
  XTextTablesSupplier xTextTablesSupplier = (XTextTablesSupplier)UnoRuntime.queryInterface(XTextTablesSupplier.class, textDocument.getXTextDocument());
  XNameAccess xNameAccess = xTextTablesSupplier.getTextTables();
  XIndexAccess xIndexAccess = (XIndexAccess)UnoRuntime.queryInterface(XIndexAccess.class, xNameAccess);
  ITextTable[] textTables = new ITextTable[xIndexAccess.getCount()];
  for(int i=0, n=xIndexAccess.getCount(); i<n; i++) {
    try {
      Any any = (Any)xIndexAccess.getByIndex(i);
      XTextTable textTable = (XTextTable)any.getObject();
      if(textTable.getColumns().getCount() <= ITextTable.MAX_COLUMNS_IN_TABLE) {
        textTables[i] = new TextTable(textDocument, textTable);
    	}
    }
    catch(Exception exception) {
      //do nothing
    }
  }
  return textTables;
}
 
Example 2
Source File: TextTableCell.java    From noa-libre with GNU Lesser General Public License v2.1 6 votes vote down vote up
/**
 * Returns content type.
 * 
 * @return content type
 * 
 * @author Andreas Bröker
 */
public int getContentType() {
  String formula = xCell.getFormula();
  if(formula != null) {
    if(formula.length() != 0)
      return ITextTableCell.TYPE_FORMULA;
  }
  double value = xCell.getValue();
  if(value != 0)
    return ITextTableCell.TYPE_VALUE;
  XText xText = (XText)UnoRuntime.queryInterface(XText.class, xCell);
  String content = xText.getString();
  if(content.length() == 0)
    return ITextTableCell.TYPE_EMPTY;
  char chars[] = content.toCharArray();
  for(int i=0; i<chars.length; i++) {
    if(Character.isDigit(chars[i])) {
      if(chars[i] != 0)
        return ITextTableCell.TYPE_TEXT;
    }
  }
  return ITextTableCell.TYPE_TEXT;    
}
 
Example 3
Source File: DocumentLoader.java    From noa-libre with GNU Lesser General Public License v2.1 6 votes vote down vote up
/**
 * Loads document from the submitted URL into the OpenOffice.org frame.
 * 
 * @param serviceProvider the service provider to be used
 * @param xFrame frame to used for document
 * @param URL URL of the document
 * @param searchFlags search flags for the target frame
 * @param properties properties for OpenOffice.org
 * 
 * @return loaded document
 * 
 * @throws Exception if an OpenOffice.org communication error occurs
 * @throws IOException if document can not be found
 */
public static IDocument loadDocument(IServiceProvider serviceProvider, XFrame xFrame, String URL,
    int searchFlags, PropertyValue[] properties) throws Exception, IOException {
  if (xFrame != null) {
    if (properties == null) {
      properties = new PropertyValue[0];
    }
    XComponentLoader xComponentLoader = (XComponentLoader) UnoRuntime.queryInterface(XComponentLoader.class,
        xFrame);
    return loadDocument(serviceProvider,
        xComponentLoader,
        URL,
        xFrame.getName(),
        searchFlags,
        properties);
  }
  return null;
}
 
Example 4
Source File: FormService.java    From noa-libre with GNU Lesser General Public License v2.1 6 votes vote down vote up
/**
 * Returns the OpenOffice.org XEventAttacherManager interface for the given form,
 * or null if not available.
 * 
 * @param form the form to be used
 * 
 * @return the OpenOffice.org XEventAttacherManager interface for the given form,
 * or null
 * 
 * @throws NOAException if the return of OpenOffice.org XEventAttacherManager interface fails
 * 
 * @author Markus Krüger
 * @date 26.01.2007
 */
public XEventAttacherManager getXEventAttacherManager(IForm form) throws NOAException {
  try {
    if(form != 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)) {
            XEventAttacherManager tmpEventAttacherManager = 
              (XEventAttacherManager) UnoRuntime.queryInterface(XEventAttacherManager.class, tmpForm);
            return tmpEventAttacherManager;
          }
        }
      }
    }
    return null;
  }
  catch(Throwable throwable) {
    throw new NOAException(throwable);
  }
}
 
Example 5
Source File: ViewCursor.java    From noa-libre with GNU Lesser General Public License v2.1 5 votes vote down vote up
/**
 * Returns the page cursor for the view cursor, can be null if no page cursor is available.
 * 
 * @return the page cursor for the view cursor, can be null
 * 
 * @author Markus Krüger
 */
public IPageCursor getPageCursor() {
  if (pageCursor == null) {
    XPageCursor xPageCursor = (XPageCursor) UnoRuntime.queryInterface(XPageCursor.class,
        xTextViewCursor);
    if (xPageCursor != null) {
      pageCursor = new PageCursor(xPageCursor);
    }
  }
  return pageCursor;
}
 
Example 6
Source File: PageService.java    From noa-libre with GNU Lesser General Public License v2.1 5 votes vote down vote up
/**
 * Returns number of available pages.
 * 
 * @return number of available pages
 * 
 * @author Markus Krüger
 * @date 07.01.2008
 */
public int getPageCount() {
  try {
    XDrawPagesSupplier dSupplier = (XDrawPagesSupplier) UnoRuntime.queryInterface(XDrawPagesSupplier.class, presentationDocument.getXComponent()); 
    XDrawPages pages = dSupplier.getDrawPages(); 
    return pages.getCount();
  }
  catch(Exception exception) {
    return 0;
  }
}
 
Example 7
Source File: TextCursor.java    From noa-libre with GNU Lesser General Public License v2.1 5 votes vote down vote up
/**
 * Inserts a text document at the current cursor location.
 * 
 * @param url URL of the document (must look like file:///c:/test.odt)
 * 
 * @throws NOAException if the text document can not be inserted
 * 
 * @author Andreas Bröker
 * @date 27.10.2006
 */
public void insertDocument(String url) throws NOAException {
  if(url == null)
    return;
  try {
    XDocumentInsertable xDocumentInsertable = (XDocumentInsertable)UnoRuntime.queryInterface(XDocumentInsertable.class, xTextCursor);
    if(xDocumentInsertable != null)
      xDocumentInsertable.insertDocumentFromURL(URLAdapter.adaptURL(url), new PropertyValue[0]);
  }
  catch(Throwable throwable) {
    throw new NOAException(throwable);
  }
}
 
Example 8
Source File: ScriptProvider.java    From noa-libre with GNU Lesser General Public License v2.1 5 votes vote down vote up
/**
 * Returns all scripts.
 * 
 * @return all scripts
 * 
 * @author Markus Krüger
 * @date 17.07.2007
 */
public IScript[] getScripts() {
  XBrowseNode rootNode = (XBrowseNode)UnoRuntime.queryInterface(XBrowseNode.class, scriptProvider);
  XBrowseNode[] typeNodes = rootNode.getChildNodes();
  List list = new ArrayList();
  for(int i=0, n=typeNodes.length; i<n; i++) {
    XBrowseNode typeNode = typeNodes[i];       
    XBrowseNode[] libraryNodes = typeNode.getChildNodes();
    for(int j=0, m=libraryNodes.length; j<m; j++) {
      XBrowseNode libraryNode = libraryNodes[j];
      buildScripts(list, libraryNode);
    }   
  }
  return (IScript[])list.toArray(new IScript[list.size()]);
}
 
Example 9
Source File: OPConnection.java    From translationstudio8 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * (non-Javadoc)
 * @see net.heartsome.cat.converter.ooconnect.OPconnect#connect()
 * @throws ConnectException
 */
public void connect() throws ConnectException {
	try {
		XComponentContext localContext;

		localContext = Bootstrap.createInitialComponentContext(null);

		XMultiComponentFactory localServiceManager = localContext.getServiceManager();
		XConnector connector = (XConnector) UnoRuntime.queryInterface(XConnector.class, localServiceManager
				.createInstanceWithContext("com.sun.star.connection.Connector", localContext)); //$NON-NLS-1$
		XConnection connection = connector.connect(strConnection);
		XBridgeFactory bridgeFactory = (XBridgeFactory) UnoRuntime.queryInterface(XBridgeFactory.class,
				localServiceManager.createInstanceWithContext("com.sun.star.bridge.BridgeFactory", localContext)); //$NON-NLS-1$
		bridge = bridgeFactory.createBridge("ms2ooBridge", "urp", connection, null); //$NON-NLS-1$ //$NON-NLS-2$
		bgComponent = (XComponent) UnoRuntime.queryInterface(XComponent.class, bridge);
		// bgComponent.addEventListener(this);
		serviceMg = (XMultiComponentFactory) UnoRuntime.queryInterface(XMultiComponentFactory.class, bridge
				.getInstance("StarOffice.ServiceManager")); //$NON-NLS-1$
		XPropertySet properties = (XPropertySet) UnoRuntime.queryInterface(XPropertySet.class, serviceMg);
		componentContext = (XComponentContext) UnoRuntime.queryInterface(XComponentContext.class, properties
				.getPropertyValue("DefaultContext")); //$NON-NLS-1$
		connected = true;
		if (connected) {
			System.out.println("has already connected"); //$NON-NLS-1$
		} else {
			System.out.println("connect to Openoffice fail,please check OpenOffice service that have to open"); //$NON-NLS-1$
		}

	} catch (NoConnectException connectException) {
		throw new ConnectException(MessageFormat.format(Messages.getString("ooconnect.OPConnection.msg"), strConnection + ": " + connectException.getMessage())); //$NON-NLS-1$ //$NON-NLS-2$
	} catch (Exception exception) {
		throw new OPException(MessageFormat.format(Messages.getString("ooconnect.OPConnection.msg"), strConnection), exception); //$NON-NLS-1$
	} catch (java.lang.Exception e) {
		if (Converter.DEBUG_MODE) {
			e.printStackTrace();
		}
	}
}
 
Example 10
Source File: TextDocument.java    From noa-libre with GNU Lesser General Public License v2.1 5 votes vote down vote up
/**
 * Sets the zoom of the document.
 * 
 * @param zoomType the type of the zoom as in class {@link DocumentZoomType}
 * @param zoomValue the value of the zoom, does only take afect if zoom type is
 * set to DocumentZoomType.BY_VALUE. Values between 20 and 600 are allowed.
 * 
 * @throws DocumentException if zoom fails
 * 
 * @author Markus Krüger
 * @date 06.07.2007
 */
public void zoom(short zoomType, short zoomValue) throws DocumentException {
  try {
    //zoomType valid?
    if (zoomType != DocumentZoomType.BY_VALUE && zoomType != DocumentZoomType.ENTIRE_PAGE
        && zoomType != DocumentZoomType.OPTIMAL
        && zoomType != DocumentZoomType.PAGE_WIDTH
        && zoomType != DocumentZoomType.PAGE_WIDTH_EXACT)
      throw new DocumentException("Invalid zoom type.");
    //zoomType valid?
    if (zoomType == DocumentZoomType.BY_VALUE && (zoomValue < 20 || zoomValue > 600))
      throw new DocumentException("Invalid zoom value. Use values between 20 and 600.");

    XModel xModel = (XModel) UnoRuntime.queryInterface(XModel.class, getXComponent());
    if (xModel != null) {
      XController xController = xModel.getCurrentController();
      XSelectionSupplier selectionSupplier = (XSelectionSupplier) UnoRuntime.queryInterface(XSelectionSupplier.class,
          xController);
      if (selectionSupplier != null) {
        XViewSettingsSupplier viewSettingsSupplier = (XViewSettingsSupplier) UnoRuntime.queryInterface(XViewSettingsSupplier.class,
            xController);
        if (viewSettingsSupplier != null) {
          XPropertySet propertySet = viewSettingsSupplier.getViewSettings();
          propertySet.setPropertyValue("ZoomType", new Short(zoomType));
          if (zoomType == DocumentZoomType.BY_VALUE)
            propertySet.setPropertyValue("ZoomValue", new Short(zoomValue));
        }
      }
    }
  }
  catch (Throwable throwable) {
    throw new DocumentException(throwable);
  }
}
 
Example 11
Source File: OOPresentation.java    From Quelea with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Create a new presentation from a particular file. This must be the path
 * to a presentation file that openoffice supports - so either odp, ppt or
 * pptx at the time of writing. Note that the static init() method on this
 * class must be called successfully before attempting to create any
 * presentation objects, otherwise an IllegalStateException will be thrown.
 *
 * @param file the path to the presentation file.
 * @throws Exception if something goes wrong creating the presentation.
 */
public OOPresentation(String file) throws Exception {
    if (!init) {
        LOGGER.log(Level.SEVERE, "BUG: Tried to create OOPresentation before it was initialised");
        throw new IllegalStateException("I'm not initialised yet! init() needs to be called before creating presentations.");
    }
    slideListeners = new ArrayList<>();
    File sourceFile = new File(file);
    StringBuilder sURL = new StringBuilder("file:///");
    sURL.append(sourceFile.getCanonicalPath().replace('\\', '/'));
    PropertyValue[] props = new PropertyValue[1];
    props[0] = new PropertyValue();
    props[0].Name = "Silent";
    props[0].Value = true;

    doc = Helper.createDocument(xOfficeContext, sURL.toString(), "_blank", 0, props);
    XModel xModel = UnoRuntime.queryInterface(XModel.class, doc);
    XEventBroadcaster xDocEventBroadcaster = UnoRuntime.queryInterface(com.sun.star.document.XEventBroadcaster.class, xModel);
    xDocEventBroadcaster.addEventListener(this);
    xModel.getCurrentController().getFrame().getContainerWindow().setPosSize(0, 0, 1, 1, PosSize.SIZE);
    xModel.getCurrentController().getFrame().getContainerWindow().setVisible(false);
    XPresentationSupplier xPresSupplier = UnoRuntime.queryInterface(XPresentationSupplier.class, doc);
    XPresentation xPresentation_ = xPresSupplier.getPresentation();
    xPresentation = UnoRuntime.queryInterface(XPresentation2.class, xPresentation_);

    Runtime.getRuntime().addShutdownHook(new Thread() {
        @Override
        public void run() {
            checkDisposed();
        }
    });

}
 
Example 12
Source File: TerminationOpenoffice.java    From tmxeditor8 with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Gets the current component.
 * @return the current component
 * @throws Exception
 *             the exception
 */
public static XComponent getCurrentComponent() throws Exception {
	XComponentContext xRemoteContext = com.sun.star.comp.helper.Bootstrap.createInitialComponentContext(null);
	// XComponentContext xRemoteContext =
	// com.sun.star.comp.helper.Bootstrap.bootstrap();

	XMultiComponentFactory xRemoteServiceManager = xRemoteContext.getServiceManager();

	Object desktop = xRemoteServiceManager.createInstanceWithContext("com.sun.star.frame.Desktop", xRemoteContext); //$NON-NLS-1$

	XDesktop xDesktop = (XDesktop) UnoRuntime.queryInterface(XDesktop.class, desktop);

	XComponent currentComponent = xDesktop.getCurrentComponent();

	return currentComponent;
}
 
Example 13
Source File: UnoConverter.java    From yarg with Apache License 2.0 4 votes vote down vote up
@SuppressWarnings("unchecked")
public static <T> T as(Class<T> type, Object object) {
    return (T) UnoRuntime.queryInterface(type, object);
}
 
Example 14
Source File: OpenOfficeWorker.java    From scipio-erp with Apache License 2.0 4 votes vote down vote up
public static OpenOfficeByteArrayOutputStream convertOODocByteStreamToByteStream(XMultiComponentFactory xmulticomponentfactory,
        OpenOfficeByteArrayInputStream is, String inputMimeType, String outputMimeType) throws Exception {

    // Query for the XPropertySet interface.
    XPropertySet xpropertysetMultiComponentFactory = (XPropertySet) UnoRuntime.queryInterface(XPropertySet.class, xmulticomponentfactory);

    // Get the default context from the office server.
    Object objectDefaultContext = xpropertysetMultiComponentFactory.getPropertyValue("DefaultContext");

    // Query for the interface XComponentContext.
    XComponentContext xcomponentcontext = (XComponentContext) UnoRuntime.queryInterface(XComponentContext.class, objectDefaultContext);

    /* A desktop environment contains tasks with one or more
       frames in which components can be loaded. Desktop is the
       environment for components which can instanciate within
       frames. */

    Object desktopObj = xmulticomponentfactory.createInstanceWithContext("com.sun.star.frame.Desktop", xcomponentcontext);
    //XDesktop desktop = (XDesktop) UnoRuntime.queryInterface(XDesktop.class, desktopObj);
    XComponentLoader xcomponentloader = (XComponentLoader) UnoRuntime.queryInterface(XComponentLoader.class, desktopObj);

    // Preparing properties for loading the document
    PropertyValue propertyvalue[] = new PropertyValue[2];
    // Setting the flag for hidding the open document
    propertyvalue[0] = new PropertyValue();
    propertyvalue[0].Name = "Hidden";
    propertyvalue[0].Value = Boolean.TRUE;
    //
    propertyvalue[1] = new PropertyValue();
    propertyvalue[1].Name = "InputStream";
    propertyvalue[1].Value = is;

    // Loading the wanted document
    Object objectDocumentToStore = xcomponentloader.loadComponentFromURL("private:stream", "_blank", 0, propertyvalue);
    if (objectDocumentToStore == null) {
        Debug.logError("Could not get objectDocumentToStore object from xcomponentloader.loadComponentFromURL", module);
    }

    // Getting an object that will offer a simple way to store a document to a URL.
    XStorable xstorable = (XStorable) UnoRuntime.queryInterface(XStorable.class, objectDocumentToStore);
    if (xstorable == null) {
        Debug.logError("Could not get XStorable object from UnoRuntime.queryInterface", module);
    }

    // Preparing properties for converting the document
    String filterName = getFilterNameFromMimeType(outputMimeType);
    propertyvalue = new PropertyValue[4];

    propertyvalue[0] = new PropertyValue();
    propertyvalue[0].Name = "OutputStream";
    OpenOfficeByteArrayOutputStream os = new OpenOfficeByteArrayOutputStream();
    propertyvalue[0].Value = os;
    // Setting the filter name
    propertyvalue[1] = new PropertyValue();
    propertyvalue[1].Name = "FilterName";
    propertyvalue[1].Value = filterName;
    // Setting the flag for overwriting
    propertyvalue[3] = new PropertyValue();
    propertyvalue[3].Name = "Overwrite";
    propertyvalue[3].Value = Boolean.TRUE;
    // For PDFs
    propertyvalue[2] = new PropertyValue();
    propertyvalue[2].Name = "CompressionMode";
    propertyvalue[2].Value = "1";

    xstorable.storeToURL("private:stream", propertyvalue);
    //xstorable.storeToURL("file:///home/byersa/testdoc1_file.pdf", propertyvalue);

    // Getting the method dispose() for closing the document
    XComponent xcomponent = (XComponent) UnoRuntime.queryInterface(XComponent.class, xstorable);

    // Closing the converted document
    xcomponent.dispose();

    return os;
}
 
Example 15
Source File: TextTableCell.java    From noa-libre with GNU Lesser General Public License v2.1 3 votes vote down vote up
/**
 * Returns related page style of the text table cell.
 * 
 * @return related page style of the text table cell
 * 
 * @throws TextException if the page style is not available
 * 
 * @author Andreas Bröker
 */
public IPageStyle getPageStyle() throws TextException {
  if(textRange == null) {
    XText xText = (XText)UnoRuntime.queryInterface(XText.class, xCell);
    textRange = new TextRange(textDocument, xText.getStart());
  }
  return textRange.getPageStyle();
}
 
Example 16
Source File: DocumentLoader.java    From noa-libre with GNU Lesser General Public License v2.1 3 votes vote down vote up
/**
 * Loads document from submitted URL.
 * 
 * @param serviceProvider the service provider to be used
 * @param URL URL of the document
 * @param properties properties for OpenOffice.org
 * 
 * @return loaded document
 * 
 * @throws Exception if an OpenOffice.org communication error occurs
 * @throws IOException if document can not be found
 */
public static IDocument loadDocument(IServiceProvider serviceProvider, String URL,
    PropertyValue[] properties) throws Exception, IOException {
  if (properties == null) {
    properties = new PropertyValue[0];
  }
  Object oDesktop = serviceProvider.createServiceWithContext("com.sun.star.frame.Desktop");
  XComponentLoader xComponentLoader = (XComponentLoader) UnoRuntime.queryInterface(XComponentLoader.class,
      oDesktop);
  return loadDocument(serviceProvider, xComponentLoader, URL, "_blank", 0, properties);
}
 
Example 17
Source File: TextTableCellRange.java    From noa-libre with GNU Lesser General Public License v2.1 2 votes vote down vote up
/**
 * Returns data of the cell range.
 * 
 * @return data of the cell range
 * 
 * @author Andreas Bröker
 */
public Object[][] getData() {
  XCellRangeData xCellRangeData = (XCellRangeData)UnoRuntime.queryInterface(XCellRangeData.class, xCellRange);
  return xCellRangeData.getDataArray();
}
 
Example 18
Source File: TextTable.java    From noa-libre with GNU Lesser General Public License v2.1 2 votes vote down vote up
/**
 * Returns name of the table.
 * 
 * @return name of the table
 * 
 * @author Andreas Bröker
 */
public String getName() {
  XNamed xNamed = (XNamed) UnoRuntime.queryInterface(XNamed.class, xTextTable);
  return xNamed.getName();
}
 
Example 19
Source File: TextTable.java    From noa-libre with GNU Lesser General Public License v2.1 2 votes vote down vote up
/**
 * Sets data of the cell with the submitted name.
 * 
 * @param cellName name of the cell
 * @param content content to be inserted
 * 
 * @throws Exception if any error occurs
 * 
 * @author Andreas Bröker
 */
public void setCellData(String cellName, String content) throws Exception {
  XCell xCell = xTextTable.getCellByName(cellName);
  XText xText = (XText) UnoRuntime.queryInterface(XText.class, xCell);
  xText.setString(content);
}
 
Example 20
Source File: TextCursor.java    From noa-libre with GNU Lesser General Public License v2.1 2 votes vote down vote up
/**
 * Returns the character properties.
 * 
 * @return character properties
 * 
 * @author Miriam Sutter
 */
public ICharacterProperties getCharacterProperties() {
	 XPropertySet xPropertySet = (XPropertySet)UnoRuntime.queryInterface(XPropertySet.class, xTextCursor);
   return new CharacterProperties(xPropertySet);
}