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