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.MouseEvent;
23 import com.sun.star.awt.Rectangle;
24 import com.sun.star.awt.XControl;
25 import com.sun.star.awt.XMouseListener;
26 import com.sun.star.awt.XTopWindow;
27 import com.sun.star.awt.XWindow;
28 import com.sun.star.beans.XMultiPropertySet;
29 import com.sun.star.lang.EventObject;
30 import com.sun.star.lang.XMultiComponentFactory;
31 import com.sun.star.uno.UnoRuntime;
32 import com.sun.star.uno.XComponentContext;
33 
34 
35 public class UnoMenu2 extends UnoMenu implements XMouseListener{
36 
UnoMenu2(XComponentContext _xContext, XMultiComponentFactory _xMCF)37 public UnoMenu2(XComponentContext _xContext, XMultiComponentFactory _xMCF) {
38     super(_xContext, _xMCF);
39 }
40 
main(String args[])41     public static void main(String args[]){
42         UnoMenu2 oUnoMenu2 = null;
43         try {
44         XComponentContext xContext = com.sun.star.comp.helper.Bootstrap.bootstrap();
45         if(xContext != null )
46             System.out.println("Connected to a running office ...");
47         XMultiComponentFactory xMCF = xContext.getServiceManager();
48         oUnoMenu2 = new UnoMenu2(xContext, xMCF);
49         oUnoMenu2.initialize( new String[] {"Height", "Moveable", "Name","PositionX","PositionY", "Step", "TabIndex","Title","Width"},
50                                     new Object[] { new Integer(140), Boolean.TRUE, "Dialog1", new Integer(102),new Integer(41), new Integer(1), new Short((short) 0), "Menu-Dialog", new Integer(200)});
51 
52         Object oFTHeaderModel = oUnoMenu2.m_xMSFDialogModel.createInstance("com.sun.star.awt.UnoControlFixedTextModel");
53         XMultiPropertySet xFTHeaderModelMPSet = (XMultiPropertySet) UnoRuntime.queryInterface(XMultiPropertySet.class, oFTHeaderModel);
54         xFTHeaderModelMPSet.setPropertyValues(
55             new String[] {"Height", "Label", "Name", "PositionX", "PositionY", "Width"},
56             new Object[] { new Integer(8), "This code-sample demonstrates the creation of a popup-menu", "HeaderLabel", new Integer(6), new Integer(6), new Integer(200)});
57         // add the model to the NameContainer of the dialog model
58         oUnoMenu2.m_xDlgModelNameContainer.insertByName("Headerlabel", oFTHeaderModel);
59         oUnoMenu2.addLabelForPopupMenu();
60         oUnoMenu2.executeDialog();
61         }catch( Exception ex ) {
62             ex.printStackTrace(System.out);
63         }
64         finally{
65         //make sure always to dispose the component and free the memory!
66         if (oUnoMenu2 != null) {
67             if (oUnoMenu2.m_xComponent != null){
68                 oUnoMenu2.m_xComponent.dispose();
69             }
70         }
71         System.exit( 0 );
72     }}
73 
74 
addLabelForPopupMenu()75     public void addLabelForPopupMenu(){
76     try{
77         String sName = "lblPopup";
78         Object oFTModel = m_xMSFDialogModel.createInstance("com.sun.star.awt.UnoControlFixedTextModel");
79         XMultiPropertySet xFTModelMPSet = (XMultiPropertySet) UnoRuntime.queryInterface(XMultiPropertySet.class, oFTModel);
80         // Set the properties at the model - keep in mind to pass the property names in alphabetical order!
81         xFTModelMPSet.setPropertyValues(
82             new String[] {"Height", "Label", "Name", "PositionX", "PositionY", "Width"},
83             new Object[] { new Integer(8), "Right-click here", sName, new Integer(50), new Integer(50), new Integer(100)});
84         // add the model to the NameContainer of the dialog model
85         m_xDlgModelNameContainer.insertByName(sName, oFTModel);
86         XWindow xWindow = (XWindow) UnoRuntime.queryInterface(XWindow.class, m_xDlgContainer.getControl(sName));
87         xWindow.addMouseListener(this);
88     }catch( Exception e ) {
89         System.err.println( e + e.getMessage());
90         e.printStackTrace();
91     }}
92 
closeDialog()93     protected void closeDialog(){
94         xDialog.endExecute();
95     }
96 
mouseReleased(MouseEvent mouseEvent)97     public void mouseReleased(MouseEvent mouseEvent) {
98     }
99 
mousePressed(MouseEvent mouseEvent)100     public void mousePressed(MouseEvent mouseEvent) {
101         if (mouseEvent.PopupTrigger){
102             Rectangle aPos = new Rectangle(mouseEvent.X, mouseEvent.Y, 0, 0);
103             XControl xControl = (XControl) UnoRuntime.queryInterface(XControl.class, mouseEvent.Source);
104             getPopupMenu().execute( xControl.getPeer(), aPos, com.sun.star.awt.PopupMenuDirection.EXECUTE_DEFAULT);
105         }
106     }
107 
mouseExited(MouseEvent mouseEvent)108     public void mouseExited(MouseEvent mouseEvent) {
109     }
110 
mouseEntered(MouseEvent mouseEvent)111     public void mouseEntered(MouseEvent mouseEvent) {
112     }
113 
disposing(EventObject eventObject)114     public void disposing(EventObject eventObject) {
115     }
116 }
117