Java Code Examples for com.sun.star.lang.XMultiServiceFactory#createInstance()

The following examples show how to use com.sun.star.lang.XMultiServiceFactory#createInstance() . 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: VariableTextFieldMaster.java    From noa-libre with GNU Lesser General Public License v2.1 6 votes vote down vote up
/**
 * Constructs new variable text field on the basis of this variable text field master.
 * TODO maybe some more parameters are needed???
 * 
 * @param content the content of the variable textfield
 * @param visible if the variable should be visible
 * @param numberFormat the number format used for the variable
 * @param isFormula if the given content is a formula
 * 
 * @return new constructed variable text field on the basis of this variable text field master
 * 
 * @throws NOAException if the new variable text field can not be constructed
 * 
 * @author Markus Krüger
 * @date 30.05.2007
 */
public ITextField constructNewVariableTextField(String content, boolean visible,
    INumberFormat numberFormat, boolean isFormula) throws NOAException {
  try {
    XMultiServiceFactory xMultiServiceFactory = (XMultiServiceFactory)UnoRuntime.queryInterface(XMultiServiceFactory.class, textDocument.getXTextDocument());
    Object textField = xMultiServiceFactory.createInstance(ITextFieldService.VARIABLES_TEXTFIELD_ID);
    XDependentTextField xDependentTextField = (XDependentTextField)UnoRuntime.queryInterface(XDependentTextField.class, textField);
    xDependentTextField.attachTextFieldMaster(xPropertySet);

    XPropertySet xPropertySetField = (XPropertySet)UnoRuntime.queryInterface(XPropertySet.class, xDependentTextField);
    xPropertySetField.setPropertyValue("IsVisible", new Boolean(visible));
    xPropertySetField.setPropertyValue("IsFixedLanguage", new Boolean(false));  
    
    ITextField variableTextField = new TextField(textDocument, xDependentTextField);

    INumberFormatService numberFormatService = textDocument.getNumberFormatService();
    VariableTextFieldHelper.setContent(content,variableTextField,isFormula,numberFormatService);
    if(numberFormat != null)
      VariableTextFieldHelper.applyNumberFormat(numberFormat,variableTextField,isFormula,numberFormatService);
    
    return variableTextField;
  }
  catch(Throwable throwable) {
    throw new NOAException(throwable);
  }
}
 
Example 2
Source File: TextTableService.java    From noa-libre with GNU Lesser General Public License v2.1 6 votes vote down vote up
/**
 * Constructs new text table.
 * 
 * @param rows rows to be added
 * @param columns columns to be added
 * 
 * @return new table
 * 
 * @throws TextException if the new text table can not be constructed
 * 
 * @author Andreas Bröker
 */
public ITextTable constructTextTable(int rows, int columns) throws TextException {
	if(columns > ITextTable.MAX_COLUMNS_IN_TABLE) {
		throw new TextException("The submitted table is not valid");
	}
  try {
    XMultiServiceFactory xMultiServiceFactory = (XMultiServiceFactory)UnoRuntime.queryInterface(XMultiServiceFactory.class, textDocument.getXTextDocument());
    Object newTable = xMultiServiceFactory.createInstance("com.sun.star.text.TextTable");
    XTextTable newTextTable = (XTextTable)UnoRuntime.queryInterface(XTextTable.class, newTable);
    newTextTable.initialize(rows,columns);
    
    TextTable textTable = new TextTable(textDocument, newTextTable);
    return textTable;
  }
  catch(Exception exception) {
    TextException textException = new TextException(exception.getMessage());
    textException.initCause(exception);
    throw textException;
  }
}
 
Example 3
Source File: AbstractInliner.java    From yarg with Apache License 2.0 6 votes vote down vote up
protected void insertImage(XComponent document, OfficeResourceProvider officeResourceProvider, XText destination, XTextRange textRange,
                           Image image) throws Exception {
    XMultiServiceFactory xFactory = as(XMultiServiceFactory.class, document);
    XComponentContext xComponentContext = officeResourceProvider.getXComponentContext();
    XMultiComponentFactory serviceManager = xComponentContext.getServiceManager();

    Object oImage = xFactory.createInstance(TEXT_GRAPHIC_OBJECT);
    Object oGraphicProvider = serviceManager.createInstanceWithContext(GRAPHIC_PROVIDER_OBJECT, xComponentContext);

    XGraphicProvider xGraphicProvider = as(XGraphicProvider.class, oGraphicProvider);

    XPropertySet imageProperties = buildImageProperties(xGraphicProvider, oImage, image.imageContent);
    XTextContent xTextContent = as(XTextContent.class, oImage);
    destination.insertTextContent(textRange, xTextContent, true);
    setImageSize(image.width, image.height, oImage, imageProperties);
}
 
Example 4
Source File: TextFieldMaster.java    From noa-libre with GNU Lesser General Public License v2.1 5 votes vote down vote up
/**
 * Constructs new textfield on the basis of this textfield master.
 * 
 * @return new constructed textfield on the basis of this textfield master
 * 
 * @throws NOAException if the new textfield can not be constructed
 * 
 * @author Andreas Bröker
 * @date 16.02.2006
 */
public ITextField constructNewTextField() throws NOAException {
	try {
 	XMultiServiceFactory xMultiServiceFactory = (XMultiServiceFactory)UnoRuntime.queryInterface(XMultiServiceFactory.class, textDocument.getXTextDocument());
   Object textField = xMultiServiceFactory.createInstance(ITextFieldService.USER_TEXTFIELD_ID);
   XDependentTextField xDependentTextField = (XDependentTextField)UnoRuntime.queryInterface(XDependentTextField.class, textField);
   xDependentTextField.attachTextFieldMaster(xPropertySet);
   return new TextField(textDocument, xDependentTextField);
	}
	catch(Throwable throwable) {
		throw new NOAException(throwable);
	}
}
 
Example 5
Source File: TextFieldService.java    From noa-libre with GNU Lesser General Public License v2.1 5 votes vote down vote up
/**
 * Adds new user textfield.
 * 
 * @param name
 *            name of the textfield
 * @param content
 *            content of the textfield
 * 
 * @return new textfield
 * 
 * @throws TextException
 *             if any error occurs during textfield creation
 * 
 * @author Andreas Bröker
 */
public ITextField addUserTextField(String name, String content)
		throws TextException {
	try {
		XMultiServiceFactory xMultiServiceFactory = (XMultiServiceFactory) UnoRuntime
				.queryInterface(XMultiServiceFactory.class,
						textDocument.getXTextDocument());
		Object textField = xMultiServiceFactory
				.createInstance(ITextFieldService.USER_TEXTFIELD_ID);
		XDependentTextField xDependentTextField = (XDependentTextField) UnoRuntime
				.queryInterface(XDependentTextField.class, textField);

		Object oFieldMaster = xMultiServiceFactory
				.createInstance(ITextFieldService.USER_TEXTFIELD_MASTER_ID);
		XPropertySet xPropertySet = (XPropertySet) UnoRuntime
				.queryInterface(XPropertySet.class, oFieldMaster);

		xPropertySet.setPropertyValue("Name", name);
		xPropertySet.setPropertyValue("Content", content);

		xDependentTextField.attachTextFieldMaster(xPropertySet);

		return new TextField(textDocument, xDependentTextField);
	} catch (Exception exception) {
		throw new TextException(exception);
	}
}
 
Example 6
Source File: TextFieldService.java    From noa-libre with GNU Lesser General Public License v2.1 5 votes vote down vote up
/**
 * Creates a new placeholder textfield.
 * 
 * @param name
 *            name of the placeholder textfield
 * @param hint
 *            the hint of the placeholder textfield, may be null
 * @param placeholderType
 *            the type of the placeholder found in static members of
 *            com.sun.star.text.PlaceholderType (i.e. PlaceholderType.TEXT)
 * 
 * @return new placeholder textfield
 * 
 * @throws TextException
 *             if any error occurs during placeholder textfield creation
 * 
 * @author Markus Krüger
 * @date 30.05.2007
 */
public ITextField createPlaceholderTextField(String name, String hint,
		short placeholderType) throws TextException {
	try {
		if (name == null) {
			throw new TextException(
					"The placeholders name to create can not be null.");
		}
		if (placeholderType < 0 || placeholderType > 4) {
			throw new TextException(
					"The placeholder type must be one of the valid static members of com.sun.star.text.PlaceholderType.");
		}
		XMultiServiceFactory xMultiServiceFactory = (XMultiServiceFactory) UnoRuntime
				.queryInterface(XMultiServiceFactory.class,
						textDocument.getXTextDocument());
		Object textField = xMultiServiceFactory
				.createInstance(ITextFieldService.PLACEHOLDER_TEXTFIELD_ID);

		XTextField xTextField = (XTextField) UnoRuntime.queryInterface(
				XTextField.class, textField);

		XPropertySet xPropertySet = (XPropertySet) UnoRuntime
				.queryInterface(XPropertySet.class, xTextField);

		// Grundeinstellung festlegen
		xPropertySet.setPropertyValue("PlaceHolder", name);
		xPropertySet.setPropertyValue("PlaceHolderType", new Short(
				placeholderType));
		if (hint != null) {
			xPropertySet.setPropertyValue("Hint", hint);
		}

		return new TextField(textDocument, xTextField);
	} catch (Exception exception) {
		throw new TextException(exception);
	}
}
 
Example 7
Source File: TextFieldService.java    From noa-libre with GNU Lesser General Public License v2.1 5 votes vote down vote up
/**
 * Creates a new variable textfield master and returns it, or returns the
 * one that already exists, if it does. TODO maybe some more parameters are
 * needed???
 * 
 * @param name
 *            name of the variable textfield master
 * @param variableType
 *            the type of the variable master found in static members of
 *            com.sun.star.text.SetVariableType (i.e.
 *            SetVariableType.STRING)
 * 
 * @return the variable textfield master with the given name
 * 
 * @throws TextException
 *             if any error occurs during variable textfield master creation
 * 
 * @author Markus Krüger
 * @date 30.05.2007
 */
public IVariableTextFieldMaster createVariableTextFieldMaster(String name,
		short variableType) throws TextException {
	try {
		if (name == null) {
			throw new TextException(
					"The variable name to create can not be null.");
		}
		if (variableType < 0 || variableType > 3) {
			throw new TextException(
					"The variable type must be one of the valid static members of com.sun.star.text.SetVariableType.");
		}

		XMultiServiceFactory xMultiServiceFactory = (XMultiServiceFactory) UnoRuntime
				.queryInterface(XMultiServiceFactory.class,
						textDocument.getXTextDocument());

		Object oFieldMaster = xMultiServiceFactory
				.createInstance(ITextFieldService.VARIABLES_TEXTFIELD_MASTER_ID);
		XPropertySet xMasterPropertySet = (XPropertySet) UnoRuntime
				.queryInterface(XPropertySet.class, oFieldMaster);

		// Grundeinstellung festlegen
		xMasterPropertySet.setPropertyValue("Name", name);
		xMasterPropertySet.setPropertyValue("SubType", new Short(
				variableType));
		return new VariableTextFieldMaster(textDocument, xMasterPropertySet);
	} catch (Exception exception) {
		throw new TextException(exception);
	}
}
 
Example 8
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;
	}
}
 
Example 9
Source File: MemoryUsage.java    From kkFileViewOfficeEdit with Apache License 2.0 4 votes vote down vote up
private void addChart(XSpreadsheet sheet)
    throws Exception
{
    Rectangle rect = new Rectangle();
    rect.X = 500;
    rect.Y = 3000;
    rect.Width = 10000;
    rect.Height = 8000;

    XCellRange range = (XCellRange)
        UnoRuntime.queryInterface(XCellRange.class, sheet);

    XCellRange myRange =
        range.getCellRangeByName("A1:B2");

    XCellRangeAddressable rangeAddr = (XCellRangeAddressable)
        UnoRuntime.queryInterface(XCellRangeAddressable.class, myRange);

    CellRangeAddress myAddr = rangeAddr.getRangeAddress();

    CellRangeAddress[] addr = new CellRangeAddress[1];
    addr[0] = myAddr;

    XTableChartsSupplier supp = (XTableChartsSupplier)
        UnoRuntime.queryInterface( XTableChartsSupplier.class, sheet);

    XTableCharts charts = supp.getCharts();
    charts.addNewByName("Example", rect, addr, false, true);

    try { Thread.sleep(3000); } catch (java.lang.InterruptedException e) { }

    // get the diagram and Change some of the properties
    XNameAccess chartsAccess = (XNameAccess)
        UnoRuntime.queryInterface( XNameAccess.class, charts);

    XTableChart tchart = (XTableChart)
        UnoRuntime.queryInterface(
            XTableChart.class, chartsAccess.getByName("Example"));

    XEmbeddedObjectSupplier eos = (XEmbeddedObjectSupplier)
        UnoRuntime.queryInterface( XEmbeddedObjectSupplier.class, tchart );

    XInterface xifc = eos.getEmbeddedObject();

    XChartDocument xChart = (XChartDocument)
        UnoRuntime.queryInterface(XChartDocument.class, xifc);

    XMultiServiceFactory xDocMSF = (XMultiServiceFactory)
        UnoRuntime.queryInterface(XMultiServiceFactory.class, xChart);

    Object diagObject =
        xDocMSF.createInstance("com.sun.star.chart.PieDiagram");

    XDiagram xDiagram = (XDiagram)
        UnoRuntime.queryInterface(XDiagram.class, diagObject);

    xChart.setDiagram(xDiagram);

    XPropertySet propset = (XPropertySet)
        UnoRuntime.queryInterface( XPropertySet.class, xChart.getTitle() );
    propset.setPropertyValue("String", "JVM Memory Usage");
}
 
Example 10
Source File: MemoryUsage.java    From kkFileView with Apache License 2.0 4 votes vote down vote up
private void addChart(XSpreadsheet sheet)
    throws Exception
{
    Rectangle rect = new Rectangle();
    rect.X = 500;
    rect.Y = 3000;
    rect.Width = 10000;
    rect.Height = 8000;

    XCellRange range = (XCellRange)
        UnoRuntime.queryInterface(XCellRange.class, sheet);

    XCellRange myRange =
        range.getCellRangeByName("A1:B2");

    XCellRangeAddressable rangeAddr = (XCellRangeAddressable)
        UnoRuntime.queryInterface(XCellRangeAddressable.class, myRange);

    CellRangeAddress myAddr = rangeAddr.getRangeAddress();

    CellRangeAddress[] addr = new CellRangeAddress[1];
    addr[0] = myAddr;

    XTableChartsSupplier supp = (XTableChartsSupplier)
        UnoRuntime.queryInterface( XTableChartsSupplier.class, sheet);

    XTableCharts charts = supp.getCharts();
    charts.addNewByName("Example", rect, addr, false, true);

    try { Thread.sleep(3000); } catch (java.lang.InterruptedException e) { }

    // get the diagram and Change some of the properties
    XNameAccess chartsAccess = (XNameAccess)
        UnoRuntime.queryInterface( XNameAccess.class, charts);

    XTableChart tchart = (XTableChart)
        UnoRuntime.queryInterface(
            XTableChart.class, chartsAccess.getByName("Example"));

    XEmbeddedObjectSupplier eos = (XEmbeddedObjectSupplier)
        UnoRuntime.queryInterface( XEmbeddedObjectSupplier.class, tchart );

    XInterface xifc = eos.getEmbeddedObject();

    XChartDocument xChart = (XChartDocument)
        UnoRuntime.queryInterface(XChartDocument.class, xifc);

    XMultiServiceFactory xDocMSF = (XMultiServiceFactory)
        UnoRuntime.queryInterface(XMultiServiceFactory.class, xChart);

    Object diagObject =
        xDocMSF.createInstance("com.sun.star.chart.PieDiagram");

    XDiagram xDiagram = (XDiagram)
        UnoRuntime.queryInterface(XDiagram.class, diagObject);

    xChart.setDiagram(xDiagram);

    XPropertySet propset = (XPropertySet)
        UnoRuntime.queryInterface( XPropertySet.class, xChart.getTitle() );
    propset.setPropertyValue("String", "JVM Memory Usage");
}