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 package mod._toolkit;
24 
25 import com.sun.star.awt.PosSize;
26 import com.sun.star.awt.Rectangle;
27 import com.sun.star.awt.XControl;
28 import com.sun.star.awt.XControlContainer;
29 import com.sun.star.awt.XControlModel;
30 import com.sun.star.awt.XDevice;
31 import com.sun.star.awt.XGraphics;
32 import com.sun.star.awt.XToolkit;
33 import com.sun.star.awt.XWindow;
34 import com.sun.star.awt.XWindowPeer;
35 import com.sun.star.drawing.XControlShape;
36 import com.sun.star.drawing.XShape;
37 import com.sun.star.frame.XController;
38 import com.sun.star.frame.XFrame;
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.WriterTools;
54 import util.utils;
55 
56 
57 public class UnoControlContainer extends TestCase {
58     private static XTextDocument xTextDoc;
59     private static XTextDocument xTD2;
60     private static XControl xCtrl;
61     private static XControl xCtrl1;
62     private static XControl xCtrl2;
63 
initialize(TestParameters param, PrintWriter log)64     protected void initialize(TestParameters param, PrintWriter log) {
65         try {
66             log.println("creating a textdocument");
67             xTD2 = WriterTools.createTextDoc(
68                            (XMultiServiceFactory) param.getMSF());
69             xTextDoc = WriterTools.createTextDoc(
70                                (XMultiServiceFactory) param.getMSF());
71         } catch (Exception e) {
72             // Some exception occures.FAILED
73             e.printStackTrace(log);
74             throw new StatusException("Couldn't create document", e);
75         }
76     }
77 
cleanup(TestParameters tParam, PrintWriter log)78     protected void cleanup(TestParameters tParam, PrintWriter log) {
79         log.println("    disposing xTextDoc ");
80 
81         util.DesktopTools.closeDoc(xTextDoc);
82         util.DesktopTools.closeDoc(xTD2);
83     }
84 
createTestEnvironment(TestParameters param, PrintWriter log)85     public TestEnvironment createTestEnvironment(TestParameters param,
86                                                  PrintWriter log) {
87         // create Object Relations -------------------------------------------
88         XInterface oObj = null;
89         XControlShape shape = null;
90         XControlModel model = null;
91         XControlAccess access = null;
92         XWindow anotherWindow = null;
93 
94         // for XControl
95         XWindowPeer the_win = null;
96         XToolkit the_kit = null;
97 
98         XControlContainer ctrlCont = null;
99 
100         XGraphics aGraphic = null;
101 
102 
103         // create 3 XControls
104         // create first XControl
105         shape = FormTools.createControlShape(xTextDoc, 3000, 4500, 15000,
106                                              10000, "TextField");
107         WriterTools.getDrawPage(xTextDoc).add((XShape) shape);
108         model = shape.getControl();
109         access = (XControlAccess) UnoRuntime.queryInterface(
110                          XControlAccess.class, xTextDoc.getCurrentController());
111 
112         try {
113             xCtrl = access.getControl(model);
114         } catch (Exception e) {
115             e.printStackTrace(log);
116             throw new StatusException("Couldn't create XControl", e);
117         }
118 
119 
120         // create second XControl
121         shape = FormTools.createControlShape(xTextDoc, 3000, 4500, 15000,
122                                              10000, "TextField");
123         WriterTools.getDrawPage(xTextDoc).add((XShape) shape);
124         model = shape.getControl();
125         access = (XControlAccess) UnoRuntime.queryInterface(
126                          XControlAccess.class, xTextDoc.getCurrentController());
127 
128         try {
129             xCtrl1 = access.getControl(model);
130         } catch (Exception e) {
131             e.printStackTrace(log);
132             throw new StatusException("Couldn't create XControl", e);
133         }
134 
135 
136         // create third XControl
137         shape = FormTools.createControlShape(xTextDoc, 3000, 4500, 15000,
138                                              10000, "CommandButton");
139         WriterTools.getDrawPage(xTextDoc).add((XShape) shape);
140         model = shape.getControl();
141         access = (XControlAccess) UnoRuntime.queryInterface(
142                          XControlAccess.class, xTextDoc.getCurrentController());
143 
144         try {
145             xCtrl2 = access.getControl(model);
146         } catch (Exception e) {
147             e.printStackTrace(log);
148             throw new StatusException("Couldn't create XControl", e);
149         }
150 
151         // create XToolkit, XWindowPeer, XDevice
152         //Insert a ControlShape and get the ControlModel
153         XControlShape aShape = FormTools.createUnoControlShape(xTextDoc, 3000,
154                                                                4500, 15000,
155                                                                10000,
156                                                                "CommandButton",
157                                                                "UnoControlButton");
158 
159         WriterTools.getDrawPage(xTD2).add((XShape) aShape);
160 
161         XControlModel the_Model = aShape.getControl();
162 
163         //Try to query XControlAccess
164         XControlAccess the_access = (XControlAccess) UnoRuntime.queryInterface(
165                                             XControlAccess.class,
166                                             xTD2.getCurrentController());
167 
168         //get the ButtonControl for the needed Object relations
169         try {
170             the_win = the_access.getControl(the_Model).getPeer();
171             the_kit = the_win.getToolkit();
172 
173             XDevice aDevice = the_kit.createScreenCompatibleDevice(200, 200);
174             aGraphic = aDevice.createGraphics();
175         } catch (Exception e) {
176             log.println("Couldn't get ButtonControl");
177             e.printStackTrace(log);
178             throw new StatusException("Couldn't get ButtonControl", e);
179         }
180 
181         try {
182             XController aController = xTD2.getCurrentController();
183             XFrame aFrame = aController.getFrame();
184             anotherWindow = aFrame.getComponentWindow();
185         } catch (Exception e) {
186             e.printStackTrace(log);
187             throw new StatusException("Couldn't create XWindow", e);
188         }
189 
190         // finished create Object Relations -----------------------------------
191         // create the UnoControlContainer
192         try {
193             oObj = (XInterface) ((XMultiServiceFactory) param.getMSF()).createInstance(
194                            "com.sun.star.awt.UnoControlContainer");
195 
196             XControl xCtrl = (XControl) UnoRuntime.queryInterface(
197                                      XControl.class, oObj);
198             xCtrl.setModel(the_Model);
199 
200             ctrlCont = (XControlContainer) UnoRuntime.queryInterface(
201                                XControlContainer.class, oObj);
202             ctrlCont.addControl("jupp", access.getControl(aShape.getControl()));
203         } catch (Exception e) {
204             e.printStackTrace(log);
205             throw new StatusException("Couldn't create UnoControlContainer", e);
206         }
207 
208         log.println(
209                 "creating a new environment for UnoControlContainer object");
210 
211         TestEnvironment tEnv = new TestEnvironment(oObj);
212 
213         XWindow xWindow = (XWindow) UnoRuntime.queryInterface(XWindow.class, oObj);
214         Rectangle ps = xWindow.getPosSize();
215         xWindow.setPosSize(ps.X+10, ps.Y+10, ps.Width+10, ps.Height+10, PosSize.POSSIZE);
216 
217         String objName = "UnoControlContainer";
218         tEnv.addObjRelation("OBJNAME", "toolkit." + objName);
219 
220 
221         // Object relation for XContainer
222         tEnv.addObjRelation("XContainer.Container", ctrlCont);
223         tEnv.addObjRelation("INSTANCE", xCtrl);
224 
225 
226         //Adding ObjRelation for XView
227         tEnv.addObjRelation("GRAPHICS", aGraphic);
228 
229 
230         // Object Relation for XControlContainer
231         tEnv.addObjRelation("CONTROL1", xCtrl1);
232         tEnv.addObjRelation("CONTROL2", xCtrl2);
233 
234 
235         // Object Relation for XControl
236         tEnv.addObjRelation("CONTEXT", xTD2);
237         tEnv.addObjRelation("WINPEER", the_win);
238         tEnv.addObjRelation("TOOLKIT", the_kit);
239         tEnv.addObjRelation("MODEL", the_Model);
240 
241 
242         // Object Relation for XWindow
243         tEnv.addObjRelation("XWindow.AnotherWindow", anotherWindow);
244         System.out.println("ImplementationName: " + utils.getImplName(oObj));
245 
246         return tEnv;
247     }
248 }
249