Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- package com.kuijper;
- import com.sun.star.awt.XControl;
- import com.sun.star.awt.XControlContainer;
- import com.sun.star.awt.XControlModel;
- import com.sun.star.awt.XDialog;
- import com.sun.star.awt.XToolkit;
- import com.sun.star.awt.XTopWindow;
- import com.sun.star.awt.XWindow;
- import com.sun.star.awt.XWindowPeer;
- import com.sun.star.awt.tab.XTabPageContainerModel;
- import com.sun.star.awt.tab.XTabPageModel;
- import com.sun.star.beans.PropertyVetoException;
- import com.sun.star.beans.UnknownPropertyException;
- import com.sun.star.beans.XMultiPropertySet;
- import com.sun.star.beans.XPropertySet;
- import com.sun.star.container.XNameAccess;
- import com.sun.star.container.XNameContainer;
- import com.sun.star.lang.WrappedTargetException;
- import com.sun.star.lang.XComponent;
- import com.sun.star.lang.XMultiComponentFactory;
- import com.sun.star.lang.XMultiServiceFactory;
- import com.sun.star.uno.XComponentContext;
- import com.sun.star.uno.UnoRuntime;
- /**
- * BTGUI: base class for all the graphics involved in BibTeXLibreOffice
- *
- *
- * see also chapter 19 of the openoffice developer's guide
- */
- public abstract class BTGUI {
- // the openoffice global context
- private final XComponentContext m_xContext;
- // the 'lowest level' multicomponentfactory
- // which is used to create dialogs etc.
- XMultiComponentFactory m_xMCF = null;
- // the dialog's multiservice factory
- // which is used to create interfaces for elements in dialogs
- XMultiServiceFactory m_xMSFDialog = null;
- // name of the dialog model type
- public XNameContainer m_xDlgModelNameContainer = null;
- // name of the control associated with this gui component
- public XControl m_xDialogControl = null;
- public XDialog m_xDialog = null;
- // window peer
- public XWindowPeer m_xWindowPeer = null;
- // name of the control container of this gui component
- public XControlContainer m_xDlgContainer = null;
- // the top window
- public XTopWindow m_xTopWindow = null;
- // list of property keys
- String[] m_sPropertyNames = new String[]{
- "Height",
- "Moveable",
- "Name",
- "PositionX",
- "PositionY",
- "Step",
- "TabIndex",
- "Title",
- "Width"
- };
- // list of property values
- Object[] m_oPropertyValues = new Object[]{
- new Integer(380),
- Boolean.TRUE,
- "BibTeXDialog",
- new Integer(19),
- new Integer(23),
- new Integer(35),
- new Short((short) 0),
- "BibTex LibreOffice",
- new Integer(300)
- };
- // c'tor
- public BTGUI(XComponentContext context) {
- m_xContext = context;
- m_xMCF = m_xContext.getServiceManager();
- // create the dialog
- createDialog();
- setPropertyValues(m_sPropertyNames, m_oPropertyValues);
- };
- // instantiate the dialog
- private void createDialog() {
- try {
- // set up a dialog model
- Object oDialogModel = m_xMCF.createInstanceWithContext(
- "com.sun.star.awt.UnoControlDialogModel", m_xContext);
- // create a multiservice factory model that belongs to the dialog
- // With this MSF, interfaces of sub-elements of the dialog can be
- // created
- m_xMSFDialog = (XMultiServiceFactory) UnoRuntime.queryInterface(
- XMultiServiceFactory.class, oDialogModel);
- // instantiate the controls using a multiservice factory
- // The named container is used to insert the created controls into...
- m_xDlgModelNameContainer = (XNameContainer) UnoRuntime.queryInterface(
- XNameContainer.class, oDialogModel);
- // create the actual dialog
- Object oUnoDialog = m_xMCF.createInstanceWithContext(
- "com.sun.star.awt.UnoControlDialog", m_xContext);
- // get the controls
- m_xDialogControl = (XControl) UnoRuntime.queryInterface(
- XControl.class, oUnoDialog);
- // get the control container
- m_xDlgContainer = (XControlContainer) UnoRuntime.queryInterface(
- XControlContainer.class, oUnoDialog);
- // get the window
- m_xTopWindow = (XTopWindow) UnoRuntime.queryInterface(
- XTopWindow.class,
- m_xDlgContainer);
- // link the model and the dialog
- XControlModel xControlModel
- = (XControlModel) UnoRuntime.queryInterface(
- XControlModel.class, oDialogModel
- );
- m_xDialogControl.setModel(xControlModel);
- } catch (com.sun.star.uno.Exception exception) {
- exception.printStackTrace(System.out);
- }
- }
- /**
- * display a dialog
- *
- * @param propNames
- * @param propVals
- */
- public short initExecuteDialog() throws com.sun.star.script.BasicErrorException {
- // get the interface of a dialog
- XWindow xWindow = (XWindow) UnoRuntime.queryInterface(
- XWindow.class, m_xDlgContainer);
- // set the interface to invisible until it is executed
- xWindow.setVisible(false);
- Object oToolkit = null;
- // initialize the toolkit
- try {
- oToolkit = m_xMCF.createInstanceWithContext(
- "com.sun.star.awt.Toolkit",
- m_xContext);
- } catch (com.sun.star.uno.Exception ex) {
- ex.printStackTrace(System.out);
- }
- // get a window peer
- XWindowPeer xWindowParentPeer = ((XToolkit) UnoRuntime.queryInterface(XToolkit.class, oToolkit)).getDesktopWindow();
- // get the toolkit interface
- XToolkit xToolkit = (XToolkit) UnoRuntime.queryInterface(
- XToolkit.class, oToolkit);
- m_xDialogControl.createPeer(xToolkit, xWindowParentPeer);
- m_xWindowPeer = m_xDialogControl.getPeer();
- m_xDialog = (XDialog) UnoRuntime.queryInterface(
- XDialog.class,
- m_xDialogControl
- );
- XComponent xDialogComponent = (XComponent) UnoRuntime.queryInterface(
- XComponent.class, m_xDialogControl);
- short nReturnValue = m_xDialog.execute();
- xDialogComponent.dispose();
- return (nReturnValue);
- }
- /**
- * set the property values of a dialog
- *
- * @param propNames
- * @param propVals
- */
- public void setPropertyValues(String[] propNames, Object[] propVals) {
- try {
- XMultiPropertySet xMultiPropSet
- = (XMultiPropertySet) UnoRuntime.queryInterface(
- XMultiPropertySet.class, m_xDlgModelNameContainer);
- xMultiPropSet.setPropertyValues(propNames, propVals);
- } catch (com.sun.star.uno.Exception ex) {
- ex.printStackTrace(System.out);
- }
- }
- /**
- * insert a container for multiple tab pages
- *
- * @param nPosX x position
- * @param nPosY y position
- * @param nWidth width
- * @param nHeight height
- *
- *
- */
- public Object insertTabGroup(
- int nPosX,
- int nPosY,
- int nWidth,
- int nHeight
- ) throws com.sun.star.uno.Exception {
- // create a model of the tab group
- Object oTabPageContainerModel = m_xMSFDialog.createInstance(
- "com.sun.star.awt.tab.UnoControlTabPageContainerModel");
- if (oTabPageContainerModel == null)
- {
- throw new NullPointerException();
- }
- // set the properties of the tab group
- // for this, we first need to get a multipropertyset interface
- XMultiPropertySet xTabModelMultiPropertySet
- = (XMultiPropertySet) UnoRuntime.queryInterface(
- XMultiPropertySet.class, oTabPageContainerModel);
- // generate a unique name for the tab field
- String unName = uniqueElementName(m_xDlgModelNameContainer, "TabPageContainer");
- // then, add the properties to the model
- xTabModelMultiPropertySet.setPropertyValues(
- new String[]{
- "Height",
- "Name",
- "PositionX",
- "PositionY",
- "Width"
- },
- new Object[]{
- new Integer(nHeight),
- unName,
- new Integer(nPosX),
- new Integer(nPosY),
- new Integer(nWidth)
- });
- // add the tab to the name container of the dialog model
- m_xDlgModelNameContainer.insertByName(unName, oTabPageContainerModel);
- return(oTabPageContainerModel);
- }
- /**
- * inserts a single tab page to a tabgroup
- * @param pageTitle
- * @param enabled
- * @param toolTip
- * @return
- * @throws com.sun.star.uno.Exception
- */
- public Object insertTabPage(
- Object oTabPageContainerModel,
- String sPageTitle,
- String sToolTip,
- int nHeight,
- int nWidth,
- short nOrderID
- ) throws com.sun.star.uno.Exception {
- if (oTabPageContainerModel == null) {
- throw new NullPointerException();
- }
- // create a unique name for this tab page
- // by assessing whether there is a similar name in the
- String unName = uniqueElementName(m_xDlgModelNameContainer,"tabPage");
- System.out.println(unName);
- // create a control model using the dialog's
- // MultiServiceFactory, which is contained in m_xMSFDialog
- Object oTabPageModel = m_xMSFDialog.createInstance(
- "com.sun.star.awt.tab.UnoControlTabPageModel"
- );
- if (oTabPageModel == null) {
- throw new NullPointerException();
- }
- // get the tab page model's interface, so that we can set certain
- // properties to the TabPageModel
- XTabPageModel xTabPageModel = (XTabPageModel)
- UnoRuntime.queryInterface(XTabPageModel.class,oTabPageModel);
- if (xTabPageModel == null) {
- throw new NullPointerException();
- }
- // set properties of the tab page using XMultiPropertySet
- XMultiPropertySet xTabPageMPSet = (XMultiPropertySet)
- UnoRuntime.queryInterface(
- XMultiPropertySet.class,
- xTabPageModel
- );
- // set the actual properties
- xTabPageMPSet.setPropertyValues(
- new String[]{
- // "Height",
- // "Name",
- // "PositionX",
- // "PositionY",
- // "TabPageID",
- "Title"
- // "ToolTip"
- // "Width"
- },
- new Object[]{
- // new Integer(nHeight),
- // unName,
- // new Integer(nPosX),
- // new Integer(nPosY),
- // new Short(nOrderID),
- new String("the title")
- // sToolTip//,
- // new Integer(nWidth)
- });
- // We also have to get the interface to the TabPageContainerModel
- // as this contains
- XTabPageContainerModel xTabPageContainerModel =
- (XTabPageContainerModel) UnoRuntime.queryInterface(
- XTabPageContainerModel.class, oTabPageContainerModel
- );
- if (xTabPageContainerModel == null) {
- throw new NullPointerException();
- }
- // add the model to the name container
- xTabPageContainerModel.insertByIndex(0, oTabPageModel);
- return(oTabPageModel);
- }
- /**
- * set a single property to a target element
- * @param oTarget the target elemtn
- */
- public static Object setSingleProperty(Object oTarget) {
- try {
- XPropertySet xPropSet = (XPropertySet)
- UnoRuntime.queryInterface(
- XPropertySet.class,
- oTarget
- );
- xPropSet.setPropertyValue("TabPageID", new Short((short) 5));
- }
- catch (WrappedTargetException e1) {
- // setPropertyValue
- e1.printStackTrace();
- } catch (PropertyVetoException e2) {
- // setPropertyValue
- e2.printStackTrace();
- } catch (IllegalArgumentException e3) {
- // setPropertyValue
- e3.printStackTrace();
- } catch (UnknownPropertyException e4) {
- // setPropertyValue
- e4.printStackTrace();
- }
- return(oTarget);
- }
- /**
- * make a unique ID string for GUI elements
- *
- * @param _xElementContainer the com.sun.star.access.XNameAccess container
- * in which the new element is going to be inserted into
- * @param _sElementName the stemname of the element
- */
- public static String uniqueElementName(
- XNameAccess _xElementContainer,
- String _sElementName
- ) {
- boolean bElementexists = true;
- int i = 1;
- String sIncSuffix = "";
- String BaseName = _sElementName;
- while (bElementexists) {
- bElementexists = _xElementContainer.hasByName(_sElementName);
- if (bElementexists) {
- i += 1;
- _sElementName = BaseName + Integer.toString(i);
- }
- }
- return _sElementName;
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement