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