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