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.XControl;
26 import com.sun.star.awt.XControlModel;
27 import com.sun.star.awt.XDevice;
28 import com.sun.star.awt.XGraphics;
29 import com.sun.star.awt.XTextComponent;
30 import com.sun.star.awt.XToolkit;
31 import com.sun.star.awt.XWindow;
32 import com.sun.star.awt.XWindowPeer;
33 import com.sun.star.drawing.XControlShape;
34 import com.sun.star.drawing.XShape;
35 import com.sun.star.lang.XMultiServiceFactory;
36 import com.sun.star.text.XTextDocument;
37 import com.sun.star.uno.UnoRuntime;
38 import com.sun.star.uno.XInterface;
39 import com.sun.star.view.XControlAccess;
40 
41 import java.io.PrintWriter;
42 
43 import lib.StatusException;
44 import lib.TestCase;
45 import lib.TestEnvironment;
46 import lib.TestParameters;
47 
48 import util.FormTools;
49 import util.SOfficeFactory;
50 import util.WriterTools;
51 import util.utils;
52 
53 
54 public class UnoControlFileControl extends TestCase {
55     private static XTextDocument xTextDoc;
56 
initialize(TestParameters Param, PrintWriter log)57     protected void initialize(TestParameters Param, PrintWriter log) {
58         SOfficeFactory SOF = SOfficeFactory.getFactory(
59                                      (XMultiServiceFactory) Param.getMSF());
60 
61         try {
62             log.println("creating a textdocument");
63             xTextDoc = SOF.createTextDoc(null);
64         } catch (com.sun.star.uno.Exception e) {
65             // Some exception occures.FAILED
66             e.printStackTrace(log);
67             throw new StatusException("Couldn't create document", e);
68         }
69     }
70 
cleanup(TestParameters tParam, PrintWriter log)71     protected void cleanup(TestParameters tParam, PrintWriter log) {
72         log.println("    disposing xTextDoc ");
73 
74         util.DesktopTools.closeDoc(xTextDoc);
75     }
76 
createTestEnvironment(TestParameters Param, PrintWriter log)77     protected TestEnvironment createTestEnvironment(TestParameters Param,
78                                                     PrintWriter log) {
79         XInterface oObj = null;
80         XWindowPeer the_win = null;
81         XToolkit the_kit = null;
82         XDevice aDevice = null;
83         XGraphics aGraphic = null;
84         XControl aControl = null;
85 
86         //Insert a ControlShape and get the ControlModel
87         XControlShape aShape = FormTools.createUnoControlShape(xTextDoc, 3000,
88                                                                4500, 15000,
89                                                                10000,
90                                                                "FileControl",
91                                                                "UnoControlFileControl");
92 
93         WriterTools.getDrawPage(xTextDoc).add((XShape) aShape);
94 
95         XControlModel the_Model = aShape.getControl();
96 
97         XControlShape aShape2 = FormTools.createControlShape(xTextDoc, 3000,
98                                                              4500, 5000, 10000,
99                                                              "TextField");
100 
101         WriterTools.getDrawPage(xTextDoc).add((XShape) aShape2);
102 
103         XControlModel the_Model2 = aShape2.getControl();
104 
105         //Try to query XControlAccess
106         XControlAccess the_access = (XControlAccess) UnoRuntime.queryInterface(
107                                             XControlAccess.class,
108                                             xTextDoc.getCurrentController());
109 
110         //get the FileControlControl for the needed Object relations
111         try {
112             oObj = the_access.getControl(the_Model);
113             aControl = the_access.getControl(the_Model2);
114             the_win = the_access.getControl(the_Model).getPeer();
115             the_kit = the_win.getToolkit();
116             aDevice = the_kit.createScreenCompatibleDevice(200, 200);
117             aGraphic = aDevice.createGraphics();
118         } catch (Exception e) {
119             log.println("Couldn't get FileControlControl");
120             e.printStackTrace(log);
121             throw new StatusException("Couldn't get FileControlControl", e);
122         }
123 
124         log.println(
125                 "creating a new environment for UnoControlFileControl object");
126 
127         TestEnvironment tEnv = new TestEnvironment(oObj);
128 
129 
130         //Adding ObjRelation for XView
131         tEnv.addObjRelation("GRAPHICS", aGraphic);
132 
133 
134         //Adding ObjRelation for XControl
135         tEnv.addObjRelation("CONTEXT", xTextDoc);
136         tEnv.addObjRelation("WINPEER", the_win);
137         tEnv.addObjRelation("TOOLKIT", the_kit);
138         tEnv.addObjRelation("MODEL", the_Model);
139 
140         XWindow forObjRel = (XWindow) UnoRuntime.queryInterface(XWindow.class,
141                                                                 aControl);
142 
143         tEnv.addObjRelation("XWindow.AnotherWindow", forObjRel);
144         tEnv.addObjRelation("XWindow.ControlShape", aShape);
145 
146         // Adding relation for XTextListener
147         ifc.awt._XTextListener.TestTextListener listener =
148                 new ifc.awt._XTextListener.TestTextListener();
149         XTextComponent textComp = (XTextComponent) UnoRuntime.queryInterface(
150                                           XTextComponent.class, oObj);
151         textComp.addTextListener(listener);
152         tEnv.addObjRelation("TestTextListener", listener);
153 
154         log.println("ImplementationName: " + utils.getImplName(oObj));
155 
156         return tEnv;
157     } // finish method getTestEnvironment
158 } // finish class UnoControlFileControl
159