1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 package mod._toolkit;
28 
29 import com.sun.star.awt.XControl;
30 import com.sun.star.awt.XControlModel;
31 import com.sun.star.awt.XDevice;
32 import com.sun.star.awt.XGraphics;
33 import com.sun.star.awt.XToolkit;
34 import com.sun.star.awt.XWindow;
35 import com.sun.star.awt.XWindowPeer;
36 import com.sun.star.beans.XPropertySet;
37 import com.sun.star.drawing.XControlShape;
38 import com.sun.star.drawing.XShape;
39 import com.sun.star.lang.XMultiServiceFactory;
40 import com.sun.star.text.XTextDocument;
41 import com.sun.star.uno.UnoRuntime;
42 import com.sun.star.uno.XInterface;
43 import com.sun.star.view.XControlAccess;
44 
45 import java.io.PrintWriter;
46 
47 import lib.StatusException;
48 import lib.TestCase;
49 import lib.TestEnvironment;
50 import lib.TestParameters;
51 
52 import util.FormTools;
53 import util.SOfficeFactory;
54 import util.WriterTools;
55 import util.utils;
56 
57 
58 public class UnoControlImageControl extends TestCase {
59     private static XTextDocument xTextDoc;
60 
61     protected void initialize(TestParameters Param, PrintWriter log) {
62         SOfficeFactory SOF = SOfficeFactory.getFactory(
63                                      (XMultiServiceFactory) Param.getMSF());
64 
65         try {
66             log.println("creating a textdocument");
67             xTextDoc = SOF.createTextDoc(null);
68         } catch (com.sun.star.uno.Exception e) {
69             // Some exception occures.FAILED
70             e.printStackTrace(log);
71             throw new StatusException("Couldn't create document", e);
72         }
73     }
74 
75     protected void cleanup(TestParameters tParam, PrintWriter log) {
76         log.println("    disposing xTextDoc ");
77 
78         util.DesktopTools.closeDoc(xTextDoc);
79     }
80 
81     protected TestEnvironment createTestEnvironment(TestParameters Param,
82                                                     PrintWriter log) {
83         XInterface oObj = null;
84         XWindowPeer the_win = null;
85         XToolkit the_kit = null;
86         XDevice aDevice = null;
87         XGraphics aGraphic = null;
88         XControl aControl = null;
89 
90         //Insert a ControlShape and get the ControlModel
91         XControlShape aShape = FormTools.createUnoControlShape(xTextDoc, 3000,
92                                                                4500, 15000,
93                                                                10000,
94                                                                "DatabaseImageControl",
95                                                                "UnoControlImageControl");
96 
97         WriterTools.getDrawPage(xTextDoc).add((XShape) aShape);
98 
99         XControlModel the_Model = aShape.getControl();
100 
101         XPropertySet xPS = (XPropertySet) UnoRuntime.queryInterface(
102                                    XPropertySet.class, the_Model);
103 
104         XControlShape aShape2 = FormTools.createControlShape(xTextDoc, 3000,
105                                                              4500, 5000, 10000,
106                                                              "TextField");
107 
108         WriterTools.getDrawPage(xTextDoc).add((XShape) aShape2);
109 
110         XControlModel the_Model2 = aShape2.getControl();
111 
112         //Try to query XControlAccess
113         XControlAccess the_access = (XControlAccess) UnoRuntime.queryInterface(
114                                             XControlAccess.class,
115                                             xTextDoc.getCurrentController());
116 
117         //get the ImageControlControl for the needed Object relations
118         try {
119             oObj = the_access.getControl(the_Model);
120             aControl = the_access.getControl(the_Model2);
121             the_win = the_access.getControl(the_Model).getPeer();
122             the_kit = the_win.getToolkit();
123             aDevice = the_kit.createScreenCompatibleDevice(200, 200);
124             aGraphic = aDevice.createGraphics();
125 
126             String imgUrl = util.utils.getFullTestURL("poliball.gif");
127 
128             xPS.setPropertyValue("ImageURL", imgUrl);
129         } catch (com.sun.star.uno.Exception e) {
130             log.println("Couldn't get ImageControlControl");
131             e.printStackTrace(log);
132             throw new StatusException("Couldn't get ImageControlControl", e);
133         }
134 
135         log.println(
136                 "creating a new environment for UnoControlImageControl object");
137 
138         TestEnvironment tEnv = new TestEnvironment(oObj);
139 
140 
141         //Adding ObjRelation for XView
142         tEnv.addObjRelation("GRAPHICS", aGraphic);
143 
144 
145         //Adding ObjRelation for XControl
146         tEnv.addObjRelation("CONTEXT", xTextDoc);
147         tEnv.addObjRelation("WINPEER", the_win);
148         tEnv.addObjRelation("TOOLKIT", the_kit);
149         tEnv.addObjRelation("MODEL", the_Model);
150 
151         XWindow forObjRel = (XWindow) UnoRuntime.queryInterface(XWindow.class,
152                                                                 aControl);
153 
154         tEnv.addObjRelation("XWindow.AnotherWindow", forObjRel);
155 
156         System.out.println("ImplementationName: " + utils.getImplName(oObj));
157 
158         return tEnv;
159     } // finish method getTestEnvironment
160 } // finish class UnoControlImageControl
161