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