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.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.lang.XMultiServiceFactory;
39 import com.sun.star.text.XTextDocument;
40 import com.sun.star.uno.UnoRuntime;
41 import com.sun.star.uno.XInterface;
42 import com.sun.star.view.XControlAccess;
43 
44 import java.io.PrintWriter;
45 
46 import lib.StatusException;
47 import lib.TestCase;
48 import lib.TestEnvironment;
49 import lib.TestParameters;
50 
51 import util.FormTools;
52 import util.SOfficeFactory;
53 import util.WriterTools;
54 import util.utils;
55 
56 
57 public class UnoControlFixedText extends TestCase {
58     private static XTextDocument xTextDoc;
59 
60     protected void initialize(TestParameters Param, PrintWriter log) {
61         SOfficeFactory SOF = SOfficeFactory.getFactory(
62                                      (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         util.DesktopTools.closeDoc(xTextDoc);
78     }
79 
80     protected TestEnvironment createTestEnvironment(TestParameters Param,
81                                                     PrintWriter log) {
82         XInterface oObj = null;
83         XWindowPeer the_win = null;
84         XToolkit the_kit = null;
85         XDevice aDevice = null;
86         XGraphics aGraphic = null;
87         XControl aControl = null;
88 
89         //Insert a ControlShape and get the ControlModel
90         XControlShape aShape = FormTools.createUnoControlShape(xTextDoc, 3000,
91                                                                4500, 15000,
92                                                                10000,
93                                                                "FixedText",
94                                                                "UnoControlFixedText");
95 
96         WriterTools.getDrawPage(xTextDoc).add((XShape) aShape);
97 
98         XControlModel the_Model = aShape.getControl();
99 
100         XControlShape aShape2 = FormTools.createControlShape(xTextDoc, 3000,
101                                                              4500, 5000, 10000,
102                                                              "TextField");
103 
104         WriterTools.getDrawPage(xTextDoc).add((XShape) aShape2);
105 
106         XControlModel the_Model2 = aShape2.getControl();
107 
108         //Try to query XControlAccess
109         XControlAccess the_access = (XControlAccess) UnoRuntime.queryInterface(
110                                             XControlAccess.class,
111                                             xTextDoc.getCurrentController());
112 
113         //get the FixedTextControl for the needed Object relations
114         try {
115             oObj = the_access.getControl(the_Model);
116             aControl = the_access.getControl(the_Model2);
117             the_win = the_access.getControl(the_Model).getPeer();
118             the_kit = the_win.getToolkit();
119             aDevice = the_kit.createScreenCompatibleDevice(200, 200);
120             aGraphic = aDevice.createGraphics();
121         } catch (Exception e) {
122             log.println("Couldn't get FixedTextControl");
123             e.printStackTrace(log);
124             throw new StatusException("Couldn't get FixedTextControl", e);
125         }
126 
127         log.println(
128                 "creating a new environment for UnoControlFixedText object");
129 
130         TestEnvironment tEnv = new TestEnvironment(oObj);
131 
132 
133         //Adding ObjRelation for XView
134         tEnv.addObjRelation("GRAPHICS", aGraphic);
135 
136 
137         //Adding ObjRelation for XControl
138         tEnv.addObjRelation("CONTEXT", xTextDoc);
139         tEnv.addObjRelation("WINPEER", the_win);
140         tEnv.addObjRelation("TOOLKIT", the_kit);
141         tEnv.addObjRelation("MODEL", the_Model);
142 
143         XWindow forObjRel = (XWindow) UnoRuntime.queryInterface(XWindow.class,
144                                                                 aControl);
145 
146         tEnv.addObjRelation("XWindow.AnotherWindow", forObjRel);
147 
148         System.out.println("ImplementationName: " + utils.getImplName(oObj));
149 
150         return tEnv;
151     } // finish method getTestEnvironment
152 } // finish class UnoControlFixedText
153