Advertisement
Guest User

Untitled

a guest
Dec 30th, 2014
361
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 14.14 KB | None | 0 0
  1. /*
  2.  * To change this license header, choose License Headers in Project Properties.
  3.  * To change this template file, choose Tools | Templates
  4.  * and open the template in the editor.
  5.  */
  6. package com.kuijper;
  7.  
  8. import com.sun.star.awt.XControl;
  9. import com.sun.star.awt.XControlContainer;
  10. import com.sun.star.awt.XControlModel;
  11. import com.sun.star.awt.XDialog;
  12. import com.sun.star.awt.XToolkit;
  13. import com.sun.star.awt.XTopWindow;
  14. import com.sun.star.awt.XWindow;
  15. import com.sun.star.awt.XWindowPeer;
  16. import com.sun.star.awt.tab.XTabPageContainerModel;
  17. import com.sun.star.awt.tab.XTabPageModel;
  18. import com.sun.star.beans.PropertyVetoException;
  19. import com.sun.star.beans.UnknownPropertyException;
  20. import com.sun.star.beans.XMultiPropertySet;
  21. import com.sun.star.beans.XPropertySet;
  22. import com.sun.star.container.XNameAccess;
  23. import com.sun.star.container.XNameContainer;
  24. import com.sun.star.lang.WrappedTargetException;
  25. import com.sun.star.lang.XComponent;
  26. import com.sun.star.lang.XMultiComponentFactory;
  27. import com.sun.star.lang.XMultiServiceFactory;
  28. import com.sun.star.uno.XComponentContext;
  29. import com.sun.star.uno.UnoRuntime;
  30.  
  31. /**
  32.  * BTGUI: base class for all the graphics involved in BibTeXLibreOffice
  33.  *
  34.  *
  35.  * see also chapter 19 of the openoffice developer's guide
  36.  */
  37. public abstract class BTGUI {
  38.  
  39.     // the openoffice global context
  40.     private final XComponentContext m_xContext;
  41.  
  42.     // the 'lowest level' multicomponentfactory
  43.     // which is used to create dialogs etc.
  44.     XMultiComponentFactory m_xMCF = null;
  45.  
  46.     // the dialog's multiservice factory
  47.     // which is used to create interfaces for elements in dialogs
  48.     XMultiServiceFactory m_xMSFDialog = null;
  49.  
  50.     // name of the dialog model type
  51.     public XNameContainer m_xDlgModelNameContainer = null;
  52.  
  53.     // name of the control associated with this gui component
  54.     public XControl m_xDialogControl = null;
  55.  
  56.     public XDialog m_xDialog = null;
  57.  
  58.     // window peer
  59.     public XWindowPeer m_xWindowPeer = null;
  60.  
  61.     // name of the control container of this gui component
  62.     public XControlContainer m_xDlgContainer = null;
  63.  
  64.     // the top window
  65.     public XTopWindow m_xTopWindow = null;
  66.  
  67.     // list of property keys
  68.     String[] m_sPropertyNames = new String[]{
  69.         "Height",
  70.         "Moveable",
  71.         "Name",
  72.         "PositionX",
  73.         "PositionY",
  74.         "Step",
  75.         "TabIndex",
  76.         "Title",
  77.         "Width"
  78.     };
  79.  
  80.     // list of property values
  81.     Object[] m_oPropertyValues = new Object[]{
  82.         new Integer(380),
  83.         Boolean.TRUE,
  84.         "BibTeXDialog",
  85.         new Integer(19),
  86.         new Integer(23),
  87.         new Integer(35),
  88.         new Short((short) 0),
  89.         "BibTex LibreOffice",
  90.         new Integer(300)
  91.     };
  92.  
  93.     // c'tor
  94.     public BTGUI(XComponentContext context) {
  95.         m_xContext = context;
  96.  
  97.         m_xMCF = m_xContext.getServiceManager();
  98.  
  99.         // create the dialog
  100.         createDialog();
  101.  
  102.         setPropertyValues(m_sPropertyNames, m_oPropertyValues);
  103.  
  104.     };
  105.    
  106.     // instantiate the dialog
  107.     private void createDialog() {
  108.         try {
  109.             // set up a dialog model
  110.             Object oDialogModel = m_xMCF.createInstanceWithContext(
  111.                     "com.sun.star.awt.UnoControlDialogModel", m_xContext);
  112.  
  113.             // create a multiservice factory model that belongs to the dialog
  114.             // With this MSF, interfaces of sub-elements of the dialog can be
  115.             // created
  116.             m_xMSFDialog = (XMultiServiceFactory) UnoRuntime.queryInterface(
  117.                     XMultiServiceFactory.class, oDialogModel);
  118.  
  119.             // instantiate the controls using a multiservice factory
  120.             // The named container is used to insert the created controls into...
  121.             m_xDlgModelNameContainer = (XNameContainer) UnoRuntime.queryInterface(
  122.                     XNameContainer.class, oDialogModel);
  123.  
  124.             // create the actual dialog
  125.             Object oUnoDialog = m_xMCF.createInstanceWithContext(
  126.                     "com.sun.star.awt.UnoControlDialog", m_xContext);
  127.  
  128.             // get the controls
  129.             m_xDialogControl = (XControl) UnoRuntime.queryInterface(
  130.                     XControl.class, oUnoDialog);
  131.  
  132.             // get the control container
  133.             m_xDlgContainer = (XControlContainer) UnoRuntime.queryInterface(
  134.                     XControlContainer.class, oUnoDialog);
  135.  
  136.             // get the window
  137.             m_xTopWindow = (XTopWindow) UnoRuntime.queryInterface(
  138.                     XTopWindow.class,
  139.                     m_xDlgContainer);
  140.  
  141.             // link the model and the dialog
  142.             XControlModel xControlModel
  143.                     = (XControlModel) UnoRuntime.queryInterface(
  144.                             XControlModel.class, oDialogModel
  145.                     );
  146.  
  147.             m_xDialogControl.setModel(xControlModel);
  148.  
  149.         } catch (com.sun.star.uno.Exception exception) {
  150.             exception.printStackTrace(System.out);
  151.         }
  152.  
  153.     }
  154.  
  155.     /**
  156.      * display a dialog
  157.      *
  158.      * @param propNames
  159.      * @param propVals
  160.      */
  161.     public short initExecuteDialog() throws com.sun.star.script.BasicErrorException {
  162.  
  163.         // get the interface of a dialog
  164.         XWindow xWindow = (XWindow) UnoRuntime.queryInterface(
  165.                 XWindow.class, m_xDlgContainer);
  166.  
  167.         // set the interface to invisible until it is executed
  168.         xWindow.setVisible(false);
  169.  
  170.         Object oToolkit = null;
  171.  
  172.         // initialize the toolkit
  173.         try {
  174.             oToolkit = m_xMCF.createInstanceWithContext(
  175.                     "com.sun.star.awt.Toolkit",
  176.                     m_xContext);
  177.         } catch (com.sun.star.uno.Exception ex) {
  178.             ex.printStackTrace(System.out);
  179.         }
  180.  
  181.         // get a window peer
  182.         XWindowPeer xWindowParentPeer = ((XToolkit) UnoRuntime.queryInterface(XToolkit.class, oToolkit)).getDesktopWindow();
  183.  
  184.         // get the toolkit interface
  185.         XToolkit xToolkit = (XToolkit) UnoRuntime.queryInterface(
  186.                 XToolkit.class, oToolkit);
  187.  
  188.         m_xDialogControl.createPeer(xToolkit, xWindowParentPeer);
  189.  
  190.         m_xWindowPeer = m_xDialogControl.getPeer();
  191.  
  192.         m_xDialog = (XDialog) UnoRuntime.queryInterface(
  193.                 XDialog.class,
  194.                 m_xDialogControl
  195.         );
  196.  
  197.         XComponent xDialogComponent = (XComponent) UnoRuntime.queryInterface(
  198.                 XComponent.class, m_xDialogControl);
  199.  
  200.         short nReturnValue = m_xDialog.execute();
  201.  
  202.         xDialogComponent.dispose();
  203.  
  204.         return (nReturnValue);
  205.     }
  206.  
  207.     /**
  208.      * set the property values of a dialog
  209.      *
  210.      * @param propNames
  211.      * @param propVals
  212.      */
  213.     public void setPropertyValues(String[] propNames, Object[] propVals) {
  214.  
  215.         try {
  216.             XMultiPropertySet xMultiPropSet
  217.                     = (XMultiPropertySet) UnoRuntime.queryInterface(
  218.                             XMultiPropertySet.class, m_xDlgModelNameContainer);
  219.  
  220.             xMultiPropSet.setPropertyValues(propNames, propVals);
  221.  
  222.         } catch (com.sun.star.uno.Exception ex) {
  223.             ex.printStackTrace(System.out);
  224.         }
  225.  
  226.     }
  227.  
  228.     /**
  229.      * insert a container for multiple tab pages
  230.      *
  231.      * @param nPosX x position
  232.      * @param nPosY y position
  233.      * @param nWidth width
  234.      * @param nHeight height
  235.      *
  236.      *
  237.      */
  238.     public Object insertTabGroup(
  239.             int nPosX,
  240.             int nPosY,
  241.             int nWidth,
  242.             int nHeight
  243.     ) throws com.sun.star.uno.Exception {
  244.  
  245.         // create a model of the tab group
  246.         Object oTabPageContainerModel = m_xMSFDialog.createInstance(
  247.                 "com.sun.star.awt.tab.UnoControlTabPageContainerModel");
  248.        
  249.         if (oTabPageContainerModel == null)
  250.         {
  251.             throw new NullPointerException();
  252.         }
  253.  
  254.         // set the properties of the tab group
  255.         // for this, we first need to get a multipropertyset interface
  256.         XMultiPropertySet xTabModelMultiPropertySet
  257.                 = (XMultiPropertySet) UnoRuntime.queryInterface(
  258.                         XMultiPropertySet.class, oTabPageContainerModel);
  259.        
  260.         // generate a unique name for the tab field
  261.         String unName = uniqueElementName(m_xDlgModelNameContainer, "TabPageContainer");
  262.        
  263.         // then, add the properties to the model
  264.         xTabModelMultiPropertySet.setPropertyValues(
  265.                 new String[]{
  266.                     "Height",
  267.                     "Name",
  268.                     "PositionX",
  269.                     "PositionY",
  270.                     "Width"
  271.                 },
  272.                 new Object[]{
  273.                     new Integer(nHeight),
  274.                     unName,
  275.                     new Integer(nPosX),
  276.                     new Integer(nPosY),
  277.                     new Integer(nWidth)
  278.                 });
  279.  
  280.         // add the tab to the name container of the dialog model
  281.         m_xDlgModelNameContainer.insertByName(unName, oTabPageContainerModel);
  282.                
  283.         return(oTabPageContainerModel);
  284.     }
  285.    
  286.     /**
  287.      * inserts a single tab page to a tabgroup
  288.      * @param pageTitle
  289.      * @param enabled
  290.      * @param toolTip
  291.      * @return
  292.      * @throws com.sun.star.uno.Exception
  293.      */
  294.     public Object insertTabPage(
  295.             Object oTabPageContainerModel,
  296.             String sPageTitle,
  297.             String sToolTip,
  298.             int nHeight,
  299.             int nWidth,
  300.             short nOrderID
  301.     ) throws com.sun.star.uno.Exception {
  302.                
  303.         if (oTabPageContainerModel == null) {
  304.             throw new NullPointerException();
  305.         }
  306.                
  307.         // create a unique name for this tab page
  308.         // by assessing whether there is a similar name in the
  309.         String unName = uniqueElementName(m_xDlgModelNameContainer,"tabPage");
  310.        
  311.         System.out.println(unName);
  312.        
  313.         // create a control model using the dialog's
  314.         // MultiServiceFactory, which is contained in m_xMSFDialog
  315.         Object oTabPageModel = m_xMSFDialog.createInstance(
  316.                 "com.sun.star.awt.tab.UnoControlTabPageModel"
  317.             );
  318.  
  319.         if (oTabPageModel == null) {
  320.             throw new NullPointerException();
  321.         }
  322.        
  323.         // get the tab page model's interface, so that we can set certain
  324.         // properties to the TabPageModel
  325.         XTabPageModel xTabPageModel = (XTabPageModel)
  326.                 UnoRuntime.queryInterface(XTabPageModel.class,oTabPageModel);
  327.        
  328.         if (xTabPageModel == null) {
  329.             throw new NullPointerException();
  330.         }
  331.        
  332.  
  333.         // set properties of the tab page using XMultiPropertySet
  334.         XMultiPropertySet xTabPageMPSet = (XMultiPropertySet)
  335.                 UnoRuntime.queryInterface(
  336.                         XMultiPropertySet.class,
  337.                         xTabPageModel
  338.                 );
  339.      
  340.         // set the actual properties
  341.         xTabPageMPSet.setPropertyValues(
  342.                 new String[]{
  343. //                    "Height",    
  344.   //                  "Name",
  345. //                    "PositionX",
  346. //                    "PositionY",
  347.  //                   "TabPageID",
  348.                     "Title"
  349. //                    "ToolTip"
  350. //                    "Width"
  351.                 },
  352.                 new Object[]{
  353.  //                   new Integer(nHeight),
  354. //                   unName,
  355. //                    new Integer(nPosX),
  356. //                    new Integer(nPosY),
  357.  //                   new Short(nOrderID),
  358.                     new String("the title")
  359.  //                   sToolTip//,
  360. //                  new Integer(nWidth)
  361.                 });
  362.        
  363.         // We also have to get the interface to the TabPageContainerModel
  364.         // as this contains
  365.         XTabPageContainerModel xTabPageContainerModel =
  366.                 (XTabPageContainerModel) UnoRuntime.queryInterface(
  367.                         XTabPageContainerModel.class, oTabPageContainerModel
  368.                 );
  369.        
  370.         if (xTabPageContainerModel == null) {
  371.             throw new NullPointerException();
  372.         }
  373.  
  374.      
  375.         // add the model to the name container
  376.         xTabPageContainerModel.insertByIndex(0, oTabPageModel);
  377.        
  378.        
  379.         return(oTabPageModel);
  380.        
  381.     }
  382.    
  383.     /**
  384.      * set a single property to a target element
  385.      * @param oTarget the target elemtn
  386.      */
  387.     public static Object setSingleProperty(Object oTarget) {
  388.        
  389.         try {
  390.             XPropertySet xPropSet = (XPropertySet)
  391.                 UnoRuntime.queryInterface(
  392.                         XPropertySet.class,
  393.                         oTarget
  394.                 );
  395.            
  396.             xPropSet.setPropertyValue("TabPageID", new Short((short) 5));
  397.            
  398.         }
  399.         catch (WrappedTargetException e1) {
  400.             // setPropertyValue
  401.             e1.printStackTrace();
  402.         } catch (PropertyVetoException e2) {
  403.             // setPropertyValue
  404.             e2.printStackTrace();
  405.         } catch (IllegalArgumentException e3) {
  406.             // setPropertyValue
  407.             e3.printStackTrace();
  408.         } catch (UnknownPropertyException e4) {
  409.             // setPropertyValue
  410.             e4.printStackTrace();
  411.         }
  412.  
  413.         return(oTarget);
  414.     }
  415.  
  416.     /**
  417.      * make a unique ID string for GUI elements
  418.      *
  419.      * @param _xElementContainer the com.sun.star.access.XNameAccess container
  420.      * in which the new element is going to be inserted into
  421.      * @param _sElementName the stemname of the element
  422.      */
  423.     public static String uniqueElementName(
  424.             XNameAccess _xElementContainer,
  425.             String _sElementName
  426.     ) {
  427.         boolean bElementexists = true;
  428.         int i = 1;
  429.         String sIncSuffix = "";
  430.         String BaseName = _sElementName;
  431.         while (bElementexists) {
  432.             bElementexists = _xElementContainer.hasByName(_sElementName);
  433.             if (bElementexists) {
  434.                 i += 1;
  435.                 _sElementName = BaseName + Integer.toString(i);
  436.             }
  437.         }
  438.         return _sElementName;
  439.     }
  440. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement