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