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