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