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