1*34dd1e25SAndrew Rist /**************************************************************
2*34dd1e25SAndrew Rist  *
3*34dd1e25SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*34dd1e25SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*34dd1e25SAndrew Rist  * distributed with this work for additional information
6*34dd1e25SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*34dd1e25SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*34dd1e25SAndrew Rist  * "License"); you may not use this file except in compliance
9*34dd1e25SAndrew Rist  * with the License.  You may obtain a copy of the License at
10*34dd1e25SAndrew Rist  *
11*34dd1e25SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*34dd1e25SAndrew Rist  *
13*34dd1e25SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*34dd1e25SAndrew Rist  * software distributed under the License is distributed on an
15*34dd1e25SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*34dd1e25SAndrew Rist  * KIND, either express or implied.  See the License for the
17*34dd1e25SAndrew Rist  * specific language governing permissions and limitations
18*34dd1e25SAndrew Rist  * under the License.
19*34dd1e25SAndrew Rist  *
20*34dd1e25SAndrew Rist  *************************************************************/
21*34dd1e25SAndrew Rist 
22*34dd1e25SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir import com.sun.star.awt.PushButtonType;
25cdf0e10cSrcweir import com.sun.star.awt.XDialog;
26cdf0e10cSrcweir import com.sun.star.beans.PropertyValue;
27cdf0e10cSrcweir import com.sun.star.beans.XMultiPropertySet;
28cdf0e10cSrcweir import com.sun.star.beans.XPropertySet;
29cdf0e10cSrcweir import com.sun.star.graphic.XGraphic;
30cdf0e10cSrcweir import com.sun.star.graphic.XGraphicProvider;
31cdf0e10cSrcweir import com.sun.star.lang.XMultiComponentFactory;
32cdf0e10cSrcweir import com.sun.star.ucb.XFileIdentifierConverter;
33cdf0e10cSrcweir import com.sun.star.uno.UnoRuntime;
34cdf0e10cSrcweir import com.sun.star.uno.XComponentContext;
35cdf0e10cSrcweir 
36cdf0e10cSrcweir 
37cdf0e10cSrcweir public class ImageControlSample extends UnoDialogSample{
38cdf0e10cSrcweir     /**
39cdf0e10cSrcweir      * Creates a new instance of ImageControlSample
40cdf0e10cSrcweir      */
ImageControlSample(XComponentContext _xContext, XMultiComponentFactory _xMCF)41cdf0e10cSrcweir     public ImageControlSample(XComponentContext _xContext, XMultiComponentFactory _xMCF){
42cdf0e10cSrcweir         super(_xContext, _xMCF);
43cdf0e10cSrcweir         super.createDialog(_xMCF);
44cdf0e10cSrcweir     }
45cdf0e10cSrcweir 
46cdf0e10cSrcweir     // to start this script pass a parameter denoting the system path to a graphic to be displayed
main(String args[])47cdf0e10cSrcweir     public static void main(String args[]) {
48cdf0e10cSrcweir         ImageControlSample oImageControlSample = null;
49cdf0e10cSrcweir         try {
50cdf0e10cSrcweir             if (args.length == 0) {
51cdf0e10cSrcweir                 System.out.println("Please pass a parameter denoting the system path to your graphic!");
52cdf0e10cSrcweir                 return;
53cdf0e10cSrcweir             }
54cdf0e10cSrcweir             XComponentContext xContext = com.sun.star.comp.helper.Bootstrap.bootstrap();
55cdf0e10cSrcweir             if(xContext != null ){
56cdf0e10cSrcweir                 System.out.println("Connected to a running office ...");
57cdf0e10cSrcweir             }
58cdf0e10cSrcweir             XMultiComponentFactory xMCF = xContext.getServiceManager();
59cdf0e10cSrcweir             oImageControlSample = new ImageControlSample(xContext, xMCF);
60cdf0e10cSrcweir             oImageControlSample.initialize( new String[] {"Height", "Moveable", "Name","PositionX","PositionY", "Step", "TabIndex","Title","Width"},
61cdf0e10cSrcweir                     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)});
62cdf0e10cSrcweir             Object oFTHeaderModel = oImageControlSample.m_xMSFDialogModel.createInstance("com.sun.star.awt.UnoControlFixedTextModel");
63cdf0e10cSrcweir             XMultiPropertySet xFTHeaderModelMPSet = (XMultiPropertySet) UnoRuntime.queryInterface(XMultiPropertySet.class, oFTHeaderModel);
64cdf0e10cSrcweir             xFTHeaderModelMPSet.setPropertyValues(
65cdf0e10cSrcweir                     new String[] {"Height", "Label", "MultiLine", "Name", "PositionX", "PositionY", "Width"},
66cdf0e10cSrcweir                     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)});
67cdf0e10cSrcweir             // add the model to the NameContainer of the dialog model
68cdf0e10cSrcweir             oImageControlSample.m_xDlgModelNameContainer.insertByName("Headerlabel", oFTHeaderModel);
69cdf0e10cSrcweir             XPropertySet xICModelPropertySet = oImageControlSample.insertImageControl(xMCF, 68, 30, 32, 90);
70cdf0e10cSrcweir             oImageControlSample.insertButton(oImageControlSample, 90, 75, 50, "~Close dialog", (short) PushButtonType.OK_value);
71cdf0e10cSrcweir             oImageControlSample.createWindowPeer();
72cdf0e10cSrcweir             // note: due to issue i76718 ("Setting graphic at a controlmodel required dialog peer") the graphic of the image control
73cdf0e10cSrcweir             // may not be set before the peer of the dialog has been created.
74cdf0e10cSrcweir             XGraphic xGraphic = oImageControlSample.getGraphic(oImageControlSample.m_xMCF, args[0]);
75cdf0e10cSrcweir             xICModelPropertySet.setPropertyValue("Graphic", xGraphic);
76cdf0e10cSrcweir             oImageControlSample.xDialog = (XDialog) UnoRuntime.queryInterface(XDialog.class, oImageControlSample.m_xDialogControl);
77cdf0e10cSrcweir             oImageControlSample.executeDialog();
78cdf0e10cSrcweir         }catch( Exception e ) {
79cdf0e10cSrcweir             System.err.println( e + e.getMessage());
80cdf0e10cSrcweir             e.printStackTrace();
81cdf0e10cSrcweir         } finally{
82cdf0e10cSrcweir             //make sure always to dispose the component and free the memory!
83cdf0e10cSrcweir             if (oImageControlSample != null){
84cdf0e10cSrcweir                 if (oImageControlSample.m_xComponent != null){
85cdf0e10cSrcweir                     oImageControlSample.m_xComponent.dispose();
86cdf0e10cSrcweir                 }
87cdf0e10cSrcweir             }
88cdf0e10cSrcweir         }
89cdf0e10cSrcweir         System.exit( 0 );
90cdf0e10cSrcweir     }
91cdf0e10cSrcweir 
92cdf0e10cSrcweir 
insertImageControl(XMultiComponentFactory _xMCF, int _nPosX, int _nPosY, int _nHeight, int _nWidth)93cdf0e10cSrcweir     public XPropertySet insertImageControl(XMultiComponentFactory _xMCF, int _nPosX, int _nPosY, int _nHeight, int _nWidth){
94cdf0e10cSrcweir         XPropertySet xICModelPropertySet = null;
95cdf0e10cSrcweir         try{
96cdf0e10cSrcweir             // create a unique name by means of an own implementation...
97cdf0e10cSrcweir             String sName = createUniqueName(m_xDlgModelNameContainer, "ImageControl");
98cdf0e10cSrcweir             // convert the system path to the image to a FileUrl
99cdf0e10cSrcweir 
100cdf0e10cSrcweir             // create a controlmodel at the multiservicefactory of the dialog model...
101cdf0e10cSrcweir             Object oICModel = m_xMSFDialogModel.createInstance("com.sun.star.awt.UnoControlImageControlModel");
102cdf0e10cSrcweir             XMultiPropertySet xICModelMPSet = (XMultiPropertySet) UnoRuntime.queryInterface(XMultiPropertySet.class, oICModel);
103cdf0e10cSrcweir             xICModelPropertySet =(XPropertySet) UnoRuntime.queryInterface(XPropertySet.class, oICModel);
104cdf0e10cSrcweir             // Set the properties at the model - keep in mind to pass the property names in alphabetical order!
105cdf0e10cSrcweir             // The image is not scaled
106cdf0e10cSrcweir             xICModelMPSet.setPropertyValues(
107cdf0e10cSrcweir                     new String[] {"Border", "Height", "Name", "PositionX", "PositionY", "ScaleImage", "Width"},
108cdf0e10cSrcweir                     new Object[] { new Short((short) 1), new Integer(_nHeight), sName, new Integer(_nPosX), new Integer(_nPosY), Boolean.FALSE, new Integer(_nWidth)});
109cdf0e10cSrcweir 
110cdf0e10cSrcweir             // The controlmodel is not really available until inserted to the Dialog container
111cdf0e10cSrcweir             m_xDlgModelNameContainer.insertByName(sName, oICModel);
112cdf0e10cSrcweir         }catch (com.sun.star.uno.Exception ex){
113cdf0e10cSrcweir             /* perform individual exception handling here.
114cdf0e10cSrcweir              * Possible exception types are:
115cdf0e10cSrcweir              * com.sun.star.lang.IllegalArgumentException,
116cdf0e10cSrcweir              * com.sun.star.lang.WrappedTargetException,
117cdf0e10cSrcweir              * com.sun.star.container.ElementExistException,
118cdf0e10cSrcweir              * com.sun.star.beans.PropertyVetoException,
119cdf0e10cSrcweir              * com.sun.star.beans.UnknownPropertyException,
120cdf0e10cSrcweir              * com.sun.star.uno.Exception
121cdf0e10cSrcweir              */
122cdf0e10cSrcweir             ex.printStackTrace(System.out);
123cdf0e10cSrcweir         }
124cdf0e10cSrcweir         return xICModelPropertySet;
125cdf0e10cSrcweir     }
126cdf0e10cSrcweir 
127cdf0e10cSrcweir 
128cdf0e10cSrcweir // creates a UNO graphic object that can be used to be assigned
129cdf0e10cSrcweir // to the property "Graphic" of a controlmodel
getGraphic(XMultiComponentFactory _xMCF, String _sImageSystemPath)130cdf0e10cSrcweir     public XGraphic getGraphic(XMultiComponentFactory _xMCF, String _sImageSystemPath){
131cdf0e10cSrcweir         XGraphic xGraphic = null;
132cdf0e10cSrcweir         try{
133cdf0e10cSrcweir             java.io.File oFile = new java.io.File(_sImageSystemPath);
134cdf0e10cSrcweir             Object oFCProvider = _xMCF.createInstanceWithContext("com.sun.star.ucb.FileContentProvider", this.m_xContext);
135cdf0e10cSrcweir             XFileIdentifierConverter xFileIdentifierConverter = (XFileIdentifierConverter) UnoRuntime.queryInterface(XFileIdentifierConverter.class, oFCProvider);
136cdf0e10cSrcweir             String sImageUrl = xFileIdentifierConverter.getFileURLFromSystemPath(_sImageSystemPath, oFile.getAbsolutePath());
137cdf0e10cSrcweir 
138cdf0e10cSrcweir             // create a GraphicProvider at the global service manager...
139cdf0e10cSrcweir             Object oGraphicProvider = m_xMCF.createInstanceWithContext("com.sun.star.graphic.GraphicProvider", m_xContext);
140cdf0e10cSrcweir             XGraphicProvider xGraphicProvider = (XGraphicProvider) UnoRuntime.queryInterface(XGraphicProvider.class, oGraphicProvider);
141cdf0e10cSrcweir             // create the graphic object
142cdf0e10cSrcweir             PropertyValue[] aPropertyValues = new PropertyValue[1];
143cdf0e10cSrcweir             PropertyValue aPropertyValue = new PropertyValue();
144cdf0e10cSrcweir             aPropertyValue.Name = "URL";
145cdf0e10cSrcweir             aPropertyValue.Value = sImageUrl;
146cdf0e10cSrcweir             aPropertyValues[0] = aPropertyValue;
147cdf0e10cSrcweir             xGraphic = xGraphicProvider.queryGraphic(aPropertyValues);
148cdf0e10cSrcweir             return xGraphic;
149cdf0e10cSrcweir         }catch (com.sun.star.uno.Exception ex){
150cdf0e10cSrcweir             throw new java.lang.RuntimeException("cannot happen...");
151cdf0e10cSrcweir         }}
152cdf0e10cSrcweir }
153