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.XToolkit;
33 import com.sun.star.awt.XWindow;
34 import com.sun.star.awt.XWindowPeer;
35 import com.sun.star.drawing.XControlShape;
36 import com.sun.star.drawing.XShape;
37 import com.sun.star.frame.XController;
38 import com.sun.star.frame.XFrame;
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 UnoControlButton extends TestCase {
59     private static XTextDocument xTextDoc;
60     private static XTextDocument xTD2;
61 
62     protected void initialize(TestParameters Param, PrintWriter log) {
63         SOfficeFactory SOF = SOfficeFactory.getFactory(
64                                      (XMultiServiceFactory) Param.getMSF());
65 
66         try {
67             log.println("creating a textdocument");
68             xTextDoc = SOF.createTextDoc(null);
69             xTD2 = WriterTools.createTextDoc(
70                            (XMultiServiceFactory) Param.getMSF());
71         } catch (com.sun.star.uno.Exception e) {
72             // Some exception occures.FAILED
73             e.printStackTrace(log);
74             throw new StatusException("Couldn't create document", e);
75         }
76     }
77 
78     protected void cleanup(TestParameters tParam, PrintWriter log) {
79         log.println("    disposing xTextDoc ");
80 
81         util.DesktopTools.closeDoc(xTextDoc);
82         util.DesktopTools.closeDoc(xTD2);
83     }
84 
85     public TestEnvironment createTestEnvironment(TestParameters Param,
86                                                  PrintWriter log) {
87         XInterface oObj = null;
88         XWindowPeer the_win = null;
89         XToolkit the_kit = null;
90         XDevice aDevice = null;
91         XGraphics aGraphic = null;
92         XWindow anotherWindow = null;
93 
94         //Insert a ControlShape and get the ControlModel
95         XControlShape aShape = FormTools.createUnoControlShape(xTextDoc, 3000,
96                                                                4500, 15000,
97                                                                10000,
98                                                                "CommandButton",
99                                                                "UnoControlButton");
100 
101         WriterTools.getDrawPage(xTextDoc).add((XShape) aShape);
102 
103         XControlModel the_Model = aShape.getControl();
104 
105         //Try to query XControlAccess
106         XControlAccess the_access = (XControlAccess) UnoRuntime.queryInterface(
107                                             XControlAccess.class,
108                                             xTextDoc.getCurrentController());
109 
110         //get the ButtonControl for the needed Object relations
111         try {
112             oObj = the_access.getControl(the_Model);
113             the_win = the_access.getControl(the_Model).getPeer();
114             the_kit = the_win.getToolkit();
115             aDevice = the_kit.createScreenCompatibleDevice(200, 200);
116             aGraphic = aDevice.createGraphics();
117         } catch (Exception e) {
118             log.println("Couldn't get ButtonControl");
119             e.printStackTrace(log);
120             throw new StatusException("Couldn't get ButtonControl", e);
121         }
122 
123         log.println("creating a new environment for UnoControlButton object");
124 
125         TestEnvironment tEnv = new TestEnvironment(oObj);
126 
127 
128         //Adding ObjRelation for XView
129         tEnv.addObjRelation("GRAPHICS", aGraphic);
130 
131 
132         //Adding ObjRelation for XControl
133         tEnv.addObjRelation("CONTEXT", xTextDoc);
134         tEnv.addObjRelation("WINPEER", the_win);
135         tEnv.addObjRelation("TOOLKIT", the_kit);
136         tEnv.addObjRelation("MODEL", the_Model);
137 
138         System.out.println("ImplementationName: " + utils.getImplName(oObj));
139 
140         try {
141             XController aController = xTD2.getCurrentController();
142             XFrame aFrame = aController.getFrame();
143             anotherWindow = aFrame.getComponentWindow();
144         } catch (Exception e) {
145             e.printStackTrace(log);
146             throw new StatusException("Couldn't create XWindow", e);
147         }
148 
149 
150         // Object Relation for XWindow
151         tEnv.addObjRelation("XWindow.AnotherWindow", anotherWindow);
152         tEnv.addObjRelation("XWindow.ControlShape", aShape);
153 
154         return tEnv;
155     } // finish method getTestEnvironment
156 } // finish class UnoControlButton
157