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.XCheckBox;
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.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 UnoControlCheckBox 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                                                                "CheckBox",
100                                                                "UnoControlCheckBox");
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 CheckBoxControl 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 CheckBoxControl");
120             e.printStackTrace(log);
121             throw new StatusException("Couldn't get CheckBoxControl", e);
122         }
123 
124         log.println("creating a new environment for UnoControlCheckBox object");
125 
126         TestEnvironment tEnv = new TestEnvironment(oObj);
127 
128 
129         //Adding ObjRelation for XView
130         tEnv.addObjRelation("GRAPHICS", aGraphic);
131 
132 
133         //Adding ObjRelation for XControl
134         tEnv.addObjRelation("CONTEXT", xTextDoc);
135         tEnv.addObjRelation("WINPEER", the_win);
136         tEnv.addObjRelation("TOOLKIT", the_kit);
137         tEnv.addObjRelation("MODEL", the_Model);
138 
139         // adding object relation for XItemListener
140         ifc.awt._XItemListener.TestItemListener listener =
141                 new ifc.awt._XItemListener.TestItemListener();
142         XCheckBox check = (XCheckBox) UnoRuntime.queryInterface(
143                                   XCheckBox.class, oObj);
144         check.addItemListener(listener);
145         tEnv.addObjRelation("TestItemListener", listener);
146 
147         System.out.println("ImplementationName: " + utils.getImplName(oObj));
148 
149         try {
150             XController aController = xTD2.getCurrentController();
151             XFrame aFrame = aController.getFrame();
152             anotherWindow = aFrame.getComponentWindow();
153         } catch (Exception e) {
154             e.printStackTrace(log);
155             throw new StatusException("Couldn't create XWindow", e);
156         }
157 
158 
159         // Object Relation for XWindow
160         tEnv.addObjRelation("XWindow.AnotherWindow", anotherWindow);
161 
162         return tEnv;
163     } // finish method getTestEnvironment
164 } // finish class UnoControlCheckBox
165