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 java.io.PrintWriter;
26 
27 import lib.StatusException;
28 import lib.TestCase;
29 import lib.TestEnvironment;
30 import lib.TestParameters;
31 import util.FormTools;
32 import util.SOfficeFactory;
33 import util.WriterTools;
34 import util.utils;
35 
36 import com.sun.star.awt.XControl;
37 import com.sun.star.awt.XControlModel;
38 import com.sun.star.awt.XDevice;
39 import com.sun.star.awt.XGraphics;
40 import com.sun.star.awt.XTextComponent;
41 import com.sun.star.awt.XToolkit;
42 import com.sun.star.awt.XWindow;
43 import com.sun.star.awt.XWindowPeer;
44 import com.sun.star.drawing.XControlShape;
45 import com.sun.star.drawing.XShape;
46 import com.sun.star.lang.XMultiServiceFactory;
47 import com.sun.star.text.XTextDocument;
48 import com.sun.star.uno.UnoRuntime;
49 import com.sun.star.uno.XInterface;
50 import com.sun.star.util.XCloseable;
51 import com.sun.star.view.XControlAccess;
52 
53 
54 public class UnoControlNumericField 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( (XMultiServiceFactory) Param.getMSF());
59 
60         try {
61             log.println("creating a textdocument");
62             xTextDoc = SOF.createTextDoc(null);
63         } catch (com.sun.star.uno.Exception e) {
64             // Some exception occures.FAILED
65             e.printStackTrace(log);
66             throw new StatusException("Couldn't create document", e);
67         }
68     }
69 
cleanup(TestParameters tParam, PrintWriter log)70     protected void cleanup(TestParameters tParam, PrintWriter log) {
71         log.println("    disposing xTextDoc ");
72 
73         try {
74             XCloseable closer = (XCloseable) UnoRuntime.queryInterface(
75                                         XCloseable.class, xTextDoc);
76             closer.close(true);
77         } catch (com.sun.star.util.CloseVetoException e) {
78             log.println("couldn't close document");
79         } catch (com.sun.star.lang.DisposedException e) {
80             log.println("couldn't close document");
81         }
82     }
83 
createTestEnvironment(TestParameters Param, PrintWriter log)84     protected TestEnvironment createTestEnvironment(TestParameters Param,
85                                                     PrintWriter log) {
86         XInterface oObj = null;
87         XWindowPeer the_win = null;
88         XToolkit the_kit = null;
89         XDevice aDevice = null;
90         XGraphics aGraphic = null;
91         XControl aControl = null;
92 
93         //Insert a ControlShape and get the ControlModel
94         XControlShape aShape = FormTools.createUnoControlShape(xTextDoc, 3000,
95                                                                4500, 15000,
96                                                                10000,
97                                                                "NumericField",
98                                                                "UnoControlNumericField");
99 
100         WriterTools.getDrawPage(xTextDoc).add((XShape) aShape);
101 
102         XControlModel the_Model = aShape.getControl();
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 NumericFieldControl 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         } catch (Exception e) {
126             log.println("Couldn't get NumericFieldControl");
127             e.printStackTrace(log);
128             throw new StatusException("Couldn't get NumericFieldControl", e);
129         }
130 
131         log.println(
132                 "creating a new environment for UnoControlNumericField object");
133 
134         TestEnvironment tEnv = new TestEnvironment(oObj);
135 
136 
137         //Adding ObjRelation for XView
138         tEnv.addObjRelation("GRAPHICS", aGraphic);
139 
140 
141         //Adding ObjRelation for XControl
142         tEnv.addObjRelation("CONTEXT", xTextDoc);
143         tEnv.addObjRelation("WINPEER", the_win);
144         tEnv.addObjRelation("TOOLKIT", the_kit);
145         tEnv.addObjRelation("MODEL", the_Model);
146 
147         XWindow forObjRel = (XWindow) UnoRuntime.queryInterface(XWindow.class,
148                                                                 aControl);
149 
150         tEnv.addObjRelation("XWindow.AnotherWindow", forObjRel);
151         tEnv.addObjRelation("XWindow.ControlShape", aShape);
152 
153         // Adding relation for XTextListener
154         ifc.awt._XTextListener.TestTextListener listener =
155                 new ifc.awt._XTextListener.TestTextListener();
156         XTextComponent textComp = (XTextComponent) UnoRuntime.queryInterface(
157                                           XTextComponent.class, oObj);
158         textComp.addTextListener(listener);
159         tEnv.addObjRelation("TestTextListener", listener);
160 
161         tEnv.addObjRelation("XTextComponent.onlyNumbers", "");
162 
163         log.println("ImplementationName: " + utils.getImplName(oObj));
164 
165         return tEnv;
166     } // finish method getTestEnvironment
167 } // finish class UnoControlNumericField
168