1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 package mod._toolkit;
24 
25 import com.sun.star.awt.XControlModel;
26 import com.sun.star.awt.XDevice;
27 import com.sun.star.awt.XGraphics;
28 import com.sun.star.awt.XToolkit;
29 import com.sun.star.awt.XWindow;
30 import com.sun.star.awt.XWindowPeer;
31 import com.sun.star.drawing.XControlShape;
32 import com.sun.star.drawing.XShape;
33 import com.sun.star.frame.XController;
34 import com.sun.star.frame.XFrame;
35 import com.sun.star.lang.XMultiServiceFactory;
36 import com.sun.star.text.XTextDocument;
37 import com.sun.star.uno.UnoRuntime;
38 import com.sun.star.uno.XInterface;
39 import com.sun.star.view.XControlAccess;
40 
41 import java.io.PrintWriter;
42 
43 import lib.StatusException;
44 import lib.TestCase;
45 import lib.TestEnvironment;
46 import lib.TestParameters;
47 
48 import util.FormTools;
49 import util.SOfficeFactory;
50 import util.WriterTools;
51 import util.utils;
52 
53 
54 public class UnoControlButton extends TestCase {
55     private static XTextDocument xTextDoc;
56     private static XTextDocument xTD2;
57 
initialize(TestParameters Param, PrintWriter log)58     protected void initialize(TestParameters Param, PrintWriter log) {
59         SOfficeFactory SOF = SOfficeFactory.getFactory(
60                                      (XMultiServiceFactory) Param.getMSF());
61 
62         try {
63             log.println("creating a textdocument");
64             xTextDoc = SOF.createTextDoc(null);
65             xTD2 = WriterTools.createTextDoc(
66                            (XMultiServiceFactory) Param.getMSF());
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 
cleanup(TestParameters tParam, PrintWriter log)74     protected void cleanup(TestParameters tParam, PrintWriter log) {
75         log.println("    disposing xTextDoc ");
76 
77         util.DesktopTools.closeDoc(xTextDoc);
78         util.DesktopTools.closeDoc(xTD2);
79     }
80 
createTestEnvironment(TestParameters Param, PrintWriter log)81     public 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         XWindow anotherWindow = null;
89 
90         //Insert a ControlShape and get the ControlModel
91         XControlShape aShape = FormTools.createUnoControlShape(xTextDoc, 3000,
92                                                                4500, 15000,
93                                                                10000,
94                                                                "CommandButton",
95                                                                "UnoControlButton");
96 
97         WriterTools.getDrawPage(xTextDoc).add((XShape) aShape);
98 
99         XControlModel the_Model = aShape.getControl();
100 
101         //Try to query XControlAccess
102         XControlAccess the_access = (XControlAccess) UnoRuntime.queryInterface(
103                                             XControlAccess.class,
104                                             xTextDoc.getCurrentController());
105 
106         //get the ButtonControl for the needed Object relations
107         try {
108             oObj = the_access.getControl(the_Model);
109             the_win = the_access.getControl(the_Model).getPeer();
110             the_kit = the_win.getToolkit();
111             aDevice = the_kit.createScreenCompatibleDevice(200, 200);
112             aGraphic = aDevice.createGraphics();
113         } catch (Exception e) {
114             log.println("Couldn't get ButtonControl");
115             e.printStackTrace(log);
116             throw new StatusException("Couldn't get ButtonControl", e);
117         }
118 
119         log.println("creating a new environment for UnoControlButton object");
120 
121         TestEnvironment tEnv = new TestEnvironment(oObj);
122 
123 
124         //Adding ObjRelation for XView
125         tEnv.addObjRelation("GRAPHICS", aGraphic);
126 
127 
128         //Adding ObjRelation for XControl
129         tEnv.addObjRelation("CONTEXT", xTextDoc);
130         tEnv.addObjRelation("WINPEER", the_win);
131         tEnv.addObjRelation("TOOLKIT", the_kit);
132         tEnv.addObjRelation("MODEL", the_Model);
133 
134         System.out.println("ImplementationName: " + utils.getImplName(oObj));
135 
136         try {
137             XController aController = xTD2.getCurrentController();
138             XFrame aFrame = aController.getFrame();
139             anotherWindow = aFrame.getComponentWindow();
140         } catch (Exception e) {
141             e.printStackTrace(log);
142             throw new StatusException("Couldn't create XWindow", e);
143         }
144 
145 
146         // Object Relation for XWindow
147         tEnv.addObjRelation("XWindow.AnotherWindow", anotherWindow);
148         tEnv.addObjRelation("XWindow.ControlShape", aShape);
149 
150         return tEnv;
151     } // finish method getTestEnvironment
152 } // finish class UnoControlButton
153