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.XControlModel;
30 import com.sun.star.awt.XDevice;
31 import com.sun.star.awt.XGraphics;
32 import com.sun.star.awt.XTextComponent;
33 import com.sun.star.awt.XToolkit;
34 import com.sun.star.awt.XWindow;
35 import com.sun.star.awt.XWindowPeer;
36 import com.sun.star.drawing.XControlShape;
37 import com.sun.star.drawing.XShape;
38 import com.sun.star.frame.XController;
39 import com.sun.star.frame.XFrame;
40 import com.sun.star.lang.XMultiServiceFactory;
41 import com.sun.star.text.XTextDocument;
42 import com.sun.star.uno.UnoRuntime;
43 import com.sun.star.uno.XInterface;
44 import com.sun.star.view.XControlAccess;
45 
46 import java.io.PrintWriter;
47 
48 import lib.StatusException;
49 import lib.TestCase;
50 import lib.TestEnvironment;
51 import lib.TestParameters;
52 
53 import util.FormTools;
54 import util.SOfficeFactory;
55 import util.WriterTools;
56 import util.utils;
57 
58 
59 public class UnoControlDateField extends TestCase {
60     private static XTextDocument xTextDoc;
61     private static XTextDocument xTD2;
62 
63     protected void initialize(TestParameters Param, PrintWriter log) {
64         SOfficeFactory SOF = SOfficeFactory.getFactory(
65                                      (XMultiServiceFactory) Param.getMSF());
66 
67         try {
68             log.println("creating a textdocument");
69             xTextDoc = SOF.createTextDoc(null);
70             xTD2 = WriterTools.createTextDoc(
71                            (XMultiServiceFactory) Param.getMSF());
72         } catch (com.sun.star.uno.Exception e) {
73             // Some exception occures.FAILED
74             e.printStackTrace(log);
75             throw new StatusException("Couldn't create document", e);
76         }
77     }
78 
79     protected void cleanup(TestParameters tParam, PrintWriter log) {
80         log.println("    disposing xTextDoc ");
81 
82         util.DesktopTools.closeDoc(xTextDoc);
83         util.DesktopTools.closeDoc(xTD2);
84     }
85 
86     protected TestEnvironment createTestEnvironment(TestParameters Param,
87                                                     PrintWriter log) {
88         XInterface oObj = null;
89         XWindowPeer the_win = null;
90         XToolkit the_kit = null;
91         XDevice aDevice = null;
92         XGraphics aGraphic = null;
93         XWindow anotherWindow = null;
94 
95         //Insert a ControlShape and get the ControlModel
96         XControlShape aShape = FormTools.createUnoControlShape(xTextDoc, 3000,
97                                                                4500, 15000,
98                                                                10000,
99                                                                "DateField",
100                                                                "UnoControlDateField");
101 
102         WriterTools.getDrawPage(xTextDoc).add((XShape) aShape);
103 
104         XControlModel the_Model = aShape.getControl();
105 
106         //Try to query XControlAccess
107         XControlAccess the_access = (XControlAccess) UnoRuntime.queryInterface(
108                                             XControlAccess.class,
109                                             xTextDoc.getCurrentController());
110 
111         //get the DateFieldControl for the needed Object relations
112         try {
113             oObj = the_access.getControl(the_Model);
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 DateFieldControl");
120             e.printStackTrace(log);
121             throw new StatusException("Couldn't get DateFieldControl", e);
122         }
123 
124         log.println(
125                 "creating a new environment for UnoControlDateField 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         System.out.println("ImplementationName: " + utils.getImplName(oObj));
141 
142         try {
143             XController aController = xTD2.getCurrentController();
144             XFrame aFrame = aController.getFrame();
145             anotherWindow = aFrame.getComponentWindow();
146         } catch (Exception e) {
147             e.printStackTrace(log);
148             throw new StatusException("Couldn't create XWindow", e);
149         }
150 
151 
152         // Object Relation for XWindow
153         tEnv.addObjRelation("XWindow.AnotherWindow", anotherWindow);
154         tEnv.addObjRelation("XWindow.ControlShape", aShape);
155 
156         // Adding relation for XTextListener
157         ifc.awt._XTextListener.TestTextListener listener =
158                 new ifc.awt._XTextListener.TestTextListener();
159         XTextComponent textComp = (XTextComponent) UnoRuntime.queryInterface(
160                                           XTextComponent.class, oObj);
161         textComp.addTextListener(listener);
162         tEnv.addObjRelation("TestTextListener", listener);
163 
164         return tEnv;
165     } // finish method getTestEnvironment
166 } // finish class UnoControlDateField
167