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