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