1*cdf0e10cSrcweir import com.sun.star.awt.MenuEvent; 2*cdf0e10cSrcweir import com.sun.star.awt.MenuItemStyle; 3*cdf0e10cSrcweir import com.sun.star.awt.Rectangle; 4*cdf0e10cSrcweir import com.sun.star.awt.WindowAttribute; 5*cdf0e10cSrcweir import com.sun.star.awt.WindowClass; 6*cdf0e10cSrcweir import com.sun.star.awt.XMenuBar; 7*cdf0e10cSrcweir import com.sun.star.awt.XMenuExtended; 8*cdf0e10cSrcweir import com.sun.star.awt.XMenuListener; 9*cdf0e10cSrcweir import com.sun.star.awt.XPopupMenu; 10*cdf0e10cSrcweir import com.sun.star.awt.XToolkit; 11*cdf0e10cSrcweir import com.sun.star.awt.XTopWindow; 12*cdf0e10cSrcweir import com.sun.star.awt.XWindow; 13*cdf0e10cSrcweir import com.sun.star.awt.XWindowPeer; 14*cdf0e10cSrcweir import com.sun.star.frame.XFrame; 15*cdf0e10cSrcweir import com.sun.star.frame.XFramesSupplier; 16*cdf0e10cSrcweir import com.sun.star.lang.XComponent; 17*cdf0e10cSrcweir import com.sun.star.lang.XMultiComponentFactory; 18*cdf0e10cSrcweir import com.sun.star.uno.UnoRuntime; 19*cdf0e10cSrcweir import com.sun.star.uno.XComponentContext; 20*cdf0e10cSrcweir 21*cdf0e10cSrcweir public class UnoMenu extends UnoDialogSample implements XMenuListener { 22*cdf0e10cSrcweir private XTopWindow mxTopWindow = null; 23*cdf0e10cSrcweir 24*cdf0e10cSrcweir public UnoMenu(XComponentContext _xContext, XMultiComponentFactory _xMCF) { 25*cdf0e10cSrcweir super(_xContext, _xMCF); 26*cdf0e10cSrcweir } 27*cdf0e10cSrcweir 28*cdf0e10cSrcweir public static void main(String args[]){ 29*cdf0e10cSrcweir UnoMenu oUnoMenu = null; 30*cdf0e10cSrcweir XComponent xComponent = null; 31*cdf0e10cSrcweir try { 32*cdf0e10cSrcweir XComponentContext xContext = com.sun.star.comp.helper.Bootstrap.bootstrap(); 33*cdf0e10cSrcweir if(xContext != null ) 34*cdf0e10cSrcweir System.out.println("Connected to a running office ..."); 35*cdf0e10cSrcweir XMultiComponentFactory xMCF = xContext.getServiceManager(); 36*cdf0e10cSrcweir oUnoMenu = new UnoMenu(xContext, xMCF); 37*cdf0e10cSrcweir oUnoMenu.mxTopWindow = oUnoMenu.showTopWindow( new Rectangle(100, 100, 500, 500)); //oUnoDialogSample.m_xWindowPeer, 38*cdf0e10cSrcweir oUnoMenu.addMenuBar(oUnoMenu.mxTopWindow, oUnoMenu); 39*cdf0e10cSrcweir }catch( Exception ex ) { 40*cdf0e10cSrcweir ex.printStackTrace(System.out); 41*cdf0e10cSrcweir } 42*cdf0e10cSrcweir } 43*cdf0e10cSrcweir 44*cdf0e10cSrcweir 45*cdf0e10cSrcweir public XPopupMenu getPopupMenu(){ 46*cdf0e10cSrcweir XPopupMenu xPopupMenu = null; 47*cdf0e10cSrcweir try{ 48*cdf0e10cSrcweir // create a popup menu 49*cdf0e10cSrcweir Object oPopupMenu = m_xMCF.createInstanceWithContext("stardiv.Toolkit.VCLXPopupMenu", m_xContext); 50*cdf0e10cSrcweir xPopupMenu = (XPopupMenu) UnoRuntime.queryInterface(XPopupMenu.class, oPopupMenu); 51*cdf0e10cSrcweir XMenuExtended xMenuExtended = (XMenuExtended) UnoRuntime.queryInterface(XMenuExtended.class, xPopupMenu); 52*cdf0e10cSrcweir 53*cdf0e10cSrcweir xPopupMenu.insertItem((short) 0, "~First Entry", MenuItemStyle.AUTOCHECK, (short) 0); 54*cdf0e10cSrcweir xPopupMenu.insertItem((short) 1, "First ~Radio Entry", (short) (MenuItemStyle.RADIOCHECK + MenuItemStyle.AUTOCHECK), (short) 1); 55*cdf0e10cSrcweir xPopupMenu.insertItem((short) 2, "~Second Radio Entry", (short) (MenuItemStyle.RADIOCHECK + MenuItemStyle.AUTOCHECK), (short) 2); 56*cdf0e10cSrcweir xPopupMenu.insertItem((short) 3, "~Third RadioEntry",(short) (MenuItemStyle.RADIOCHECK + MenuItemStyle.AUTOCHECK), (short) 3); 57*cdf0e10cSrcweir xPopupMenu.insertSeparator((short)4); 58*cdf0e10cSrcweir xPopupMenu.insertItem((short) 4, "F~ifth Entry", (short) (MenuItemStyle.CHECKABLE + MenuItemStyle.AUTOCHECK), (short) 5); 59*cdf0e10cSrcweir xPopupMenu.insertItem((short) 5, "~Fourth Entry", (short) (MenuItemStyle.CHECKABLE + MenuItemStyle.AUTOCHECK), (short) 6); 60*cdf0e10cSrcweir xPopupMenu.enableItem((short) 1, false); 61*cdf0e10cSrcweir xPopupMenu.insertItem((short) 6, "~Sixth Entry", (short) 0, (short) 7); 62*cdf0e10cSrcweir xPopupMenu.insertItem((short) 7, "~Close Dialog", (short) 0, (short) 8); 63*cdf0e10cSrcweir xPopupMenu.checkItem((short) 2, true); 64*cdf0e10cSrcweir xPopupMenu.addMenuListener(this); 65*cdf0e10cSrcweir }catch( Exception e ) { 66*cdf0e10cSrcweir throw new java.lang.RuntimeException("cannot happen..."); 67*cdf0e10cSrcweir } 68*cdf0e10cSrcweir return xPopupMenu; 69*cdf0e10cSrcweir } 70*cdf0e10cSrcweir 71*cdf0e10cSrcweir 72*cdf0e10cSrcweir public void addMenuBar(XTopWindow _xTopWindow, XMenuListener _xMenuListener){ 73*cdf0e10cSrcweir try{ 74*cdf0e10cSrcweir // create a menubar at the global MultiComponentFactory... 75*cdf0e10cSrcweir Object oMenuBar = m_xMCF.createInstanceWithContext("stardiv.Toolkit.VCLXMenuBar", m_xContext); 76*cdf0e10cSrcweir // add the menu items... 77*cdf0e10cSrcweir XMenuBar xMenuBar = (XMenuBar) UnoRuntime.queryInterface(XMenuBar.class, oMenuBar); 78*cdf0e10cSrcweir xMenuBar.insertItem((short) 0, "~First MenuBar Item", com.sun.star.awt.MenuItemStyle.AUTOCHECK, (short) 0); 79*cdf0e10cSrcweir xMenuBar.insertItem((short) 1, "~Second MenuBar Item", com.sun.star.awt.MenuItemStyle.AUTOCHECK, (short) 1); 80*cdf0e10cSrcweir xMenuBar.setPopupMenu((short) 0, getPopupMenu()); 81*cdf0e10cSrcweir xMenuBar.addMenuListener(_xMenuListener); 82*cdf0e10cSrcweir _xTopWindow.setMenuBar(xMenuBar); 83*cdf0e10cSrcweir }catch( Exception e ) { 84*cdf0e10cSrcweir throw new java.lang.RuntimeException("cannot happen..."); 85*cdf0e10cSrcweir }} 86*cdf0e10cSrcweir 87*cdf0e10cSrcweir protected void closeDialog(){ 88*cdf0e10cSrcweir XComponent xComponent = (XComponent) UnoRuntime.queryInterface(XComponent.class, mxTopWindow); 89*cdf0e10cSrcweir if (xComponent != null){ 90*cdf0e10cSrcweir xComponent.dispose(); 91*cdf0e10cSrcweir } 92*cdf0e10cSrcweir 93*cdf0e10cSrcweir // to ensure that the Java application terminates 94*cdf0e10cSrcweir System.exit( 0 ); 95*cdf0e10cSrcweir } 96*cdf0e10cSrcweir 97*cdf0e10cSrcweir public XTopWindow showTopWindow( Rectangle _aRectangle){ 98*cdf0e10cSrcweir XTopWindow xTopWindow = null; 99*cdf0e10cSrcweir try { 100*cdf0e10cSrcweir // The Toolkit is the creator of all windows... 101*cdf0e10cSrcweir Object oToolkit = m_xMCF.createInstanceWithContext("com.sun.star.awt.Toolkit", m_xContext); 102*cdf0e10cSrcweir XToolkit xToolkit = (XToolkit) UnoRuntime.queryInterface(XToolkit.class, oToolkit); 103*cdf0e10cSrcweir 104*cdf0e10cSrcweir // set up a window description and create the window. A parent window is always necessary for this... 105*cdf0e10cSrcweir com.sun.star.awt.WindowDescriptor aWindowDescriptor = new com.sun.star.awt.WindowDescriptor(); 106*cdf0e10cSrcweir // a TopWindow is contains a title bar and is able to inlude menus... 107*cdf0e10cSrcweir aWindowDescriptor.Type = WindowClass.TOP; 108*cdf0e10cSrcweir // specify the position and height of the window on the parent window 109*cdf0e10cSrcweir aWindowDescriptor.Bounds = _aRectangle; 110*cdf0e10cSrcweir // set the window attributes... 111*cdf0e10cSrcweir aWindowDescriptor.WindowAttributes = WindowAttribute.SHOW + WindowAttribute.MOVEABLE + WindowAttribute.SIZEABLE + WindowAttribute.CLOSEABLE; 112*cdf0e10cSrcweir 113*cdf0e10cSrcweir // create the window... 114*cdf0e10cSrcweir XWindowPeer xWindowPeer = xToolkit.createWindow(aWindowDescriptor); 115*cdf0e10cSrcweir XWindow xWindow = (XWindow) UnoRuntime.queryInterface(XWindow.class, xWindowPeer); 116*cdf0e10cSrcweir 117*cdf0e10cSrcweir // create a frame and initialize it with the created window... 118*cdf0e10cSrcweir Object oFrame = m_xMCF.createInstanceWithContext("com.sun.star.frame.Frame", m_xContext); 119*cdf0e10cSrcweir m_xFrame = (XFrame) UnoRuntime.queryInterface(XFrame.class, oFrame); 120*cdf0e10cSrcweir 121*cdf0e10cSrcweir Object oDesktop = m_xMCF.createInstanceWithContext("com.sun.star.frame.Desktop", m_xContext); 122*cdf0e10cSrcweir XFramesSupplier xFramesSupplier = (XFramesSupplier) UnoRuntime.queryInterface(XFramesSupplier.class, oDesktop); 123*cdf0e10cSrcweir m_xFrame.setCreator(xFramesSupplier); 124*cdf0e10cSrcweir // get the XTopWindow interface.. 125*cdf0e10cSrcweir xTopWindow = (XTopWindow) UnoRuntime.queryInterface(XTopWindow.class, xWindow); 126*cdf0e10cSrcweir } catch (com.sun.star.lang.IllegalArgumentException ex) { 127*cdf0e10cSrcweir ex.printStackTrace(); 128*cdf0e10cSrcweir } catch (com.sun.star.uno.Exception ex) { 129*cdf0e10cSrcweir ex.printStackTrace(); 130*cdf0e10cSrcweir } 131*cdf0e10cSrcweir return xTopWindow; 132*cdf0e10cSrcweir } 133*cdf0e10cSrcweir 134*cdf0e10cSrcweir public void addMenuBar(XWindow _xWindow){ 135*cdf0e10cSrcweir XTopWindow xTopWindow = (XTopWindow) UnoRuntime.queryInterface(XTopWindow.class, _xWindow); 136*cdf0e10cSrcweir addMenuBar(xTopWindow, this); 137*cdf0e10cSrcweir } 138*cdf0e10cSrcweir 139*cdf0e10cSrcweir public void select(MenuEvent menuEvent){ 140*cdf0e10cSrcweir // find out which menu item has been triggered, 141*cdf0e10cSrcweir // by getting the menu-id... 142*cdf0e10cSrcweir switch (menuEvent.MenuId){ 143*cdf0e10cSrcweir case 0: 144*cdf0e10cSrcweir // add your menu-item-specific code here: 145*cdf0e10cSrcweir break; 146*cdf0e10cSrcweir case 1: 147*cdf0e10cSrcweir // add your menu-item-specific code here: 148*cdf0e10cSrcweir break; 149*cdf0e10cSrcweir case 7: 150*cdf0e10cSrcweir closeDialog(); 151*cdf0e10cSrcweir default: 152*cdf0e10cSrcweir //.. 153*cdf0e10cSrcweir } 154*cdf0e10cSrcweir } 155*cdf0e10cSrcweir 156*cdf0e10cSrcweir public void highlight(MenuEvent menuEvent) { 157*cdf0e10cSrcweir int i = 0; 158*cdf0e10cSrcweir } 159*cdf0e10cSrcweir 160*cdf0e10cSrcweir public void deactivate(MenuEvent menuEvent) { 161*cdf0e10cSrcweir int i = 0; } 162*cdf0e10cSrcweir 163*cdf0e10cSrcweir public void activate(MenuEvent menuEvent) { 164*cdf0e10cSrcweir int i = 0; 165*cdf0e10cSrcweir } 166*cdf0e10cSrcweir 167*cdf0e10cSrcweir } 168