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.XTextComponent;
34 import com.sun.star.awt.XToolkit;
35 import com.sun.star.awt.XWindow;
36 import com.sun.star.awt.XWindowPeer;
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 UnoControlCurrencyField 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                                                                "CurrencyField",
95                                                                "UnoControlCurrencyField");
96 
97         WriterTools.getDrawPage(xTextDoc).add((XShape) aShape);
98 
99         XControlModel the_Model = aShape.getControl();
100 
101         XControlShape aShape2 = FormTools.createControlShape(xTextDoc, 3000,
102                                                              4500, 5000, 10000,
103                                                              "TextField");
104 
105         WriterTools.getDrawPage(xTextDoc).add((XShape) aShape2);
106 
107         XControlModel the_Model2 = aShape2.getControl();
108 
109         //Try to query XControlAccess
110         XControlAccess the_access = (XControlAccess) UnoRuntime.queryInterface(
111                                             XControlAccess.class,
112                                             xTextDoc.getCurrentController());
113 
114         //get the CurrencyFieldControl for the needed Object relations
115         try {
116             oObj = the_access.getControl(the_Model);
117             aControl = the_access.getControl(the_Model2);
118             the_win = the_access.getControl(the_Model).getPeer();
119             the_kit = the_win.getToolkit();
120             aDevice = the_kit.createScreenCompatibleDevice(200, 200);
121             aGraphic = aDevice.createGraphics();
122         } catch (Exception e) {
123             log.println("Couldn't get CurrencyFieldControl");
124             e.printStackTrace(log);
125             throw new StatusException("Couldn't get CurrencyFieldControl", e);
126         }
127 
128         log.println(
129                 "creating a new environment for UnoControlCurrencyField object");
130 
131         TestEnvironment tEnv = new TestEnvironment(oObj);
132 
133 
134         //Adding ObjRelation for XView
135         tEnv.addObjRelation("GRAPHICS", aGraphic);
136 
137 
138         //Adding ObjRelation for XControl
139         tEnv.addObjRelation("CONTEXT", xTextDoc);
140         tEnv.addObjRelation("WINPEER", the_win);
141         tEnv.addObjRelation("TOOLKIT", the_kit);
142         tEnv.addObjRelation("MODEL", the_Model);
143 
144         XWindow forObjRel = (XWindow) UnoRuntime.queryInterface(XWindow.class,
145                                                                 aControl);
146 
147         tEnv.addObjRelation("XWindow.AnotherWindow", forObjRel);
148         tEnv.addObjRelation("XWindow.ControlShape", aShape);
149 
150         // Adding relation for XTextListener
151         ifc.awt._XTextListener.TestTextListener listener =
152                 new ifc.awt._XTextListener.TestTextListener();
153         XTextComponent textComp = (XTextComponent) UnoRuntime.queryInterface(
154                                           XTextComponent.class, oObj);
155         textComp.addTextListener(listener);
156         tEnv.addObjRelation("TestTextListener", listener);
157 
158         log.println("ImplementationName: " + utils.getImplName(oObj));
159 
160         return tEnv;
161     } // finish method getTestEnvironment
162 } // finish class UnoControlCurrencyField
163