Branch libreoffice-5-0-4
[LibreOffice.git] / odk / examples / DevelopersGuide / GUI / UnoMenu.java
blobb6e9533ecb8ad278aaedd67d343cd3b47e338a26
1 /*
2 * This file is part of the LibreOffice project.
4 * This Source Code Form is subject to the terms of the Mozilla Public
5 * License, v. 2.0. If a copy of the MPL was not distributed with this
6 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 * This file incorporates work covered by the following license notice:
10 * Licensed to the Apache Software Foundation (ASF) under one or more
11 * contributor license agreements. See the NOTICE file distributed
12 * with this work for additional information regarding copyright
13 * ownership. The ASF licenses this file to you under the Apache
14 * License, Version 2.0 (the "License"); you may not use this file
15 * except in compliance with the License. You may obtain a copy of
16 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
19 import com.sun.star.awt.MenuEvent;
20 import com.sun.star.awt.MenuItemStyle;
21 import com.sun.star.awt.Rectangle;
22 import com.sun.star.awt.WindowAttribute;
23 import com.sun.star.awt.WindowClass;
24 import com.sun.star.awt.XMenuBar;
25 import com.sun.star.awt.XMenuListener;
26 import com.sun.star.awt.XPopupMenu;
27 import com.sun.star.awt.XToolkit;
28 import com.sun.star.awt.XTopWindow;
29 import com.sun.star.awt.XWindow;
30 import com.sun.star.awt.XWindowPeer;
31 import com.sun.star.frame.XFrame;
32 import com.sun.star.frame.XFramesSupplier;
33 import com.sun.star.lang.XComponent;
34 import com.sun.star.lang.XMultiComponentFactory;
35 import com.sun.star.uno.UnoRuntime;
36 import com.sun.star.uno.XComponentContext;
38 public class UnoMenu extends UnoDialogSample implements XMenuListener {
39 private XTopWindow mxTopWindow = null;
41 public UnoMenu(XComponentContext _xContext, XMultiComponentFactory _xMCF) {
42 super(_xContext, _xMCF);
45 public static void main(String args[]){
46 UnoMenu oUnoMenu = null;
47 try {
48 XComponentContext xContext = com.sun.star.comp.helper.Bootstrap.bootstrap();
49 if(xContext != null )
50 System.out.println("Connected to a running office ...");
51 XMultiComponentFactory xMCF = xContext.getServiceManager();
52 oUnoMenu = new UnoMenu(xContext, xMCF);
53 oUnoMenu.mxTopWindow = oUnoMenu.showTopWindow( new Rectangle(100, 100, 500, 500)); //oUnoDialogSample.m_xWindowPeer,
54 oUnoMenu.addMenuBar(oUnoMenu.mxTopWindow, oUnoMenu);
55 }catch( Exception ex ) {
56 ex.printStackTrace(System.err);
61 public XPopupMenu getPopupMenu(){
62 XPopupMenu xPopupMenu = null;
63 try{
64 // create a popup menu
65 Object oPopupMenu = m_xMCF.createInstanceWithContext("com.sun.star.awt.PopupMenu", m_xContext);
66 xPopupMenu = UnoRuntime.queryInterface(XPopupMenu.class, oPopupMenu);
68 // ID must start be > 0
69 short nId = 1;
70 short nPos = 0;
72 xPopupMenu.insertItem(nId++, "First Entry", MenuItemStyle.AUTOCHECK, nPos++);
73 xPopupMenu.insertItem(nId++, "First Radio Entry", (short) (MenuItemStyle.RADIOCHECK + MenuItemStyle.AUTOCHECK), nPos++);
74 xPopupMenu.insertItem(nId++, "Second Radio Entry", (short) (MenuItemStyle.RADIOCHECK + MenuItemStyle.AUTOCHECK), nPos++);
75 xPopupMenu.insertItem(nId++, "Third RadioEntry",(short) (MenuItemStyle.RADIOCHECK + MenuItemStyle.AUTOCHECK), nPos++);
76 xPopupMenu.insertSeparator(nPos++);
77 xPopupMenu.insertItem(nId++, "Fifth Entry", (short) (MenuItemStyle.CHECKABLE + MenuItemStyle.AUTOCHECK), nPos++);
78 xPopupMenu.insertItem(nId++, "Fourth Entry", (short) (MenuItemStyle.CHECKABLE + MenuItemStyle.AUTOCHECK), nPos++);
79 xPopupMenu.insertItem(nId++, "Sixth Entry", (short) 0, nPos++);
80 xPopupMenu.insertItem(nId++, "Close Dialog", (short) 0, nPos++);
82 xPopupMenu.enableItem((short) 2, false);
83 xPopupMenu.checkItem((short) 3, true);
85 xPopupMenu.addMenuListener(this);
86 }catch( Exception e ) {
87 throw new java.lang.RuntimeException("cannot happen...", e);
89 return xPopupMenu;
93 private void addMenuBar(XTopWindow _xTopWindow, XMenuListener _xMenuListener){
94 try{
95 // create a menubar at the global MultiComponentFactory...
96 Object oMenuBar = m_xMCF.createInstanceWithContext("com.sun.star.awt.MenuBar", m_xContext);
97 // add the menu items...
98 XMenuBar xMenuBar = UnoRuntime.queryInterface(XMenuBar.class, oMenuBar);
99 xMenuBar.insertItem((short) 1, "~First MenuBar Item", com.sun.star.awt.MenuItemStyle.AUTOCHECK, (short) 0);
100 xMenuBar.insertItem((short) 2, "~Second MenuBar Item", com.sun.star.awt.MenuItemStyle.AUTOCHECK, (short) 1);
101 xMenuBar.setPopupMenu((short) 1, getPopupMenu());
102 xMenuBar.addMenuListener(_xMenuListener);
103 _xTopWindow.setMenuBar(xMenuBar);
104 }catch( Exception e ) {
105 throw new java.lang.RuntimeException("cannot happen...", e);
108 protected void closeDialog(){
109 XComponent xComponent = UnoRuntime.queryInterface(XComponent.class, mxTopWindow);
110 if (xComponent != null){
111 xComponent.dispose();
114 // to ensure that the Java application terminates
115 System.exit( 0 );
118 private 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 = UnoRuntime.queryInterface(XToolkit.class, oToolkit);
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;
134 // create the window...
135 XWindowPeer xWindowPeer = xToolkit.createWindow(aWindowDescriptor);
136 XWindow xWindow = UnoRuntime.queryInterface(XWindow.class, xWindowPeer);
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 = UnoRuntime.queryInterface(XFrame.class, oFrame);
142 Object oDesktop = m_xMCF.createInstanceWithContext("com.sun.star.frame.Desktop", m_xContext);
143 XFramesSupplier xFramesSupplier = UnoRuntime.queryInterface(XFramesSupplier.class, oDesktop);
144 m_xFrame.setCreator(xFramesSupplier);
145 // get the XTopWindow interface..
146 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();
152 return xTopWindow;
157 public void itemSelected(MenuEvent menuEvent){
158 // find out which menu item has been triggered,
159 // by getting the menu-id...
160 switch (menuEvent.MenuId){
161 case 1:
162 // add your menu-item-specific code here:
163 break;
164 case 2:
165 // add your menu-item-specific code here:
166 break;
167 case 8:
168 closeDialog();
169 default:
174 public void itemHighlighted(MenuEvent menuEvent) {
177 public void itemDeactivated(MenuEvent menuEvent) {
180 public void itemActivated(MenuEvent menuEvent) {