1 /************************************************************************* 2 * 3 * The Contents of this file are made available subject to the terms of 4 * the BSD license. 5 * 6 * Copyright 2000, 2010 Oracle and/or its affiliates. 7 * All rights reserved. 8 * 9 * Redistribution and use in source and binary forms, with or without 10 * modification, are permitted provided that the following conditions 11 * are met: 12 * 1. Redistributions of source code must retain the above copyright 13 * notice, this list of conditions and the following disclaimer. 14 * 2. Redistributions in binary form must reproduce the above copyright 15 * notice, this list of conditions and the following disclaimer in the 16 * documentation and/or other materials provided with the distribution. 17 * 3. Neither the name of Sun Microsystems, Inc. nor the names of its 18 * contributors may be used to endorse or promote products derived 19 * from this software without specific prior written permission. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 24 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 25 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 26 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 27 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS 28 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 29 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR 30 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE 31 * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 32 * 33 *************************************************************************/ 34 35 import com.sun.star.awt.PushButtonType; 36 import com.sun.star.awt.XDialog; 37 import com.sun.star.beans.PropertyValue; 38 import com.sun.star.beans.XMultiPropertySet; 39 import com.sun.star.beans.XPropertySet; 40 import com.sun.star.graphic.XGraphic; 41 import com.sun.star.graphic.XGraphicProvider; 42 import com.sun.star.lang.XMultiComponentFactory; 43 import com.sun.star.ucb.XFileIdentifierConverter; 44 import com.sun.star.uno.UnoRuntime; 45 import com.sun.star.uno.XComponentContext; 46 47 48 public class ImageControlSample extends UnoDialogSample{ 49 /** 50 * Creates a new instance of ImageControlSample 51 */ 52 public ImageControlSample(XComponentContext _xContext, XMultiComponentFactory _xMCF){ 53 super(_xContext, _xMCF); 54 super.createDialog(_xMCF); 55 } 56 57 // to start this script pass a parameter denoting the system path to a graphic to be displayed 58 public static void main(String args[]) { 59 ImageControlSample oImageControlSample = null; 60 try { 61 if (args.length == 0) { 62 System.out.println("Please pass a parameter denoting the system path to your graphic!"); 63 return; 64 } 65 XComponentContext xContext = com.sun.star.comp.helper.Bootstrap.bootstrap(); 66 if(xContext != null ){ 67 System.out.println("Connected to a running office ..."); 68 } 69 XMultiComponentFactory xMCF = xContext.getServiceManager(); 70 oImageControlSample = new ImageControlSample(xContext, xMCF); 71 oImageControlSample.initialize( new String[] {"Height", "Moveable", "Name","PositionX","PositionY", "Step", "TabIndex","Title","Width"}, 72 new Object[] { new Integer(100), Boolean.TRUE, "MyTestDialog", new Integer(102),new Integer(41), new Integer(0), new Short((short) 0), "OpenOffice", new Integer(230)}); 73 Object oFTHeaderModel = oImageControlSample.m_xMSFDialogModel.createInstance("com.sun.star.awt.UnoControlFixedTextModel"); 74 XMultiPropertySet xFTHeaderModelMPSet = (XMultiPropertySet) UnoRuntime.queryInterface(XMultiPropertySet.class, oFTHeaderModel); 75 xFTHeaderModelMPSet.setPropertyValues( 76 new String[] {"Height", "Label", "MultiLine", "Name", "PositionX", "PositionY", "Width"}, 77 new Object[] { new Integer(16), "This code-sample demonstrates how to create an ImageControlSample within a dialog", Boolean.TRUE, "HeaderLabel", new Integer(6), new Integer(6), new Integer(210)}); 78 // add the model to the NameContainer of the dialog model 79 oImageControlSample.m_xDlgModelNameContainer.insertByName("Headerlabel", oFTHeaderModel); 80 XPropertySet xICModelPropertySet = oImageControlSample.insertImageControl(xMCF, 68, 30, 32, 90); 81 oImageControlSample.insertButton(oImageControlSample, 90, 75, 50, "~Close dialog", (short) PushButtonType.OK_value); 82 oImageControlSample.createWindowPeer(); 83 // note: due to issue i76718 ("Setting graphic at a controlmodel required dialog peer") the graphic of the image control 84 // may not be set before the peer of the dialog has been created. 85 XGraphic xGraphic = oImageControlSample.getGraphic(oImageControlSample.m_xMCF, args[0]); 86 xICModelPropertySet.setPropertyValue("Graphic", xGraphic); 87 oImageControlSample.xDialog = (XDialog) UnoRuntime.queryInterface(XDialog.class, oImageControlSample.m_xDialogControl); 88 oImageControlSample.executeDialog(); 89 }catch( Exception e ) { 90 System.err.println( e + e.getMessage()); 91 e.printStackTrace(); 92 } finally{ 93 //make sure always to dispose the component and free the memory! 94 if (oImageControlSample != null){ 95 if (oImageControlSample.m_xComponent != null){ 96 oImageControlSample.m_xComponent.dispose(); 97 } 98 } 99 } 100 System.exit( 0 ); 101 } 102 103 104 public XPropertySet insertImageControl(XMultiComponentFactory _xMCF, int _nPosX, int _nPosY, int _nHeight, int _nWidth){ 105 XPropertySet xICModelPropertySet = null; 106 try{ 107 // create a unique name by means of an own implementation... 108 String sName = createUniqueName(m_xDlgModelNameContainer, "ImageControl"); 109 // convert the system path to the image to a FileUrl 110 111 // create a controlmodel at the multiservicefactory of the dialog model... 112 Object oICModel = m_xMSFDialogModel.createInstance("com.sun.star.awt.UnoControlImageControlModel"); 113 XMultiPropertySet xICModelMPSet = (XMultiPropertySet) UnoRuntime.queryInterface(XMultiPropertySet.class, oICModel); 114 xICModelPropertySet =(XPropertySet) UnoRuntime.queryInterface(XPropertySet.class, oICModel); 115 // Set the properties at the model - keep in mind to pass the property names in alphabetical order! 116 // The image is not scaled 117 xICModelMPSet.setPropertyValues( 118 new String[] {"Border", "Height", "Name", "PositionX", "PositionY", "ScaleImage", "Width"}, 119 new Object[] { new Short((short) 1), new Integer(_nHeight), sName, new Integer(_nPosX), new Integer(_nPosY), Boolean.FALSE, new Integer(_nWidth)}); 120 121 // The controlmodel is not really available until inserted to the Dialog container 122 m_xDlgModelNameContainer.insertByName(sName, oICModel); 123 }catch (com.sun.star.uno.Exception ex){ 124 /* perform individual exception handling here. 125 * Possible exception types are: 126 * com.sun.star.lang.IllegalArgumentException, 127 * com.sun.star.lang.WrappedTargetException, 128 * com.sun.star.container.ElementExistException, 129 * com.sun.star.beans.PropertyVetoException, 130 * com.sun.star.beans.UnknownPropertyException, 131 * com.sun.star.uno.Exception 132 */ 133 ex.printStackTrace(System.out); 134 } 135 return xICModelPropertySet; 136 } 137 138 139 // creates a UNO graphic object that can be used to be assigned 140 // to the property "Graphic" of a controlmodel 141 public XGraphic getGraphic(XMultiComponentFactory _xMCF, String _sImageSystemPath){ 142 XGraphic xGraphic = null; 143 try{ 144 java.io.File oFile = new java.io.File(_sImageSystemPath); 145 Object oFCProvider = _xMCF.createInstanceWithContext("com.sun.star.ucb.FileContentProvider", this.m_xContext); 146 XFileIdentifierConverter xFileIdentifierConverter = (XFileIdentifierConverter) UnoRuntime.queryInterface(XFileIdentifierConverter.class, oFCProvider); 147 String sImageUrl = xFileIdentifierConverter.getFileURLFromSystemPath(_sImageSystemPath, oFile.getAbsolutePath()); 148 149 // create a GraphicProvider at the global service manager... 150 Object oGraphicProvider = m_xMCF.createInstanceWithContext("com.sun.star.graphic.GraphicProvider", m_xContext); 151 XGraphicProvider xGraphicProvider = (XGraphicProvider) UnoRuntime.queryInterface(XGraphicProvider.class, oGraphicProvider); 152 // create the graphic object 153 PropertyValue[] aPropertyValues = new PropertyValue[1]; 154 PropertyValue aPropertyValue = new PropertyValue(); 155 aPropertyValue.Name = "URL"; 156 aPropertyValue.Value = sImageUrl; 157 aPropertyValues[0] = aPropertyValue; 158 xGraphic = xGraphicProvider.queryGraphic(aPropertyValues); 159 return xGraphic; 160 }catch (com.sun.star.uno.Exception ex){ 161 throw new java.lang.RuntimeException("cannot happen..."); 162 }} 163 } 164