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.XControl;
26 import com.sun.star.awt.XControlModel;
27 import com.sun.star.awt.XDevice;
28 import com.sun.star.awt.XGraphics;
29 import com.sun.star.awt.XListBox;
30 import com.sun.star.awt.XToolkit;
31 import com.sun.star.awt.XWindow;
32 import com.sun.star.awt.XWindowPeer;
33 import com.sun.star.drawing.XControlShape;
34 import com.sun.star.drawing.XShape;
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 UnoControlListBox extends TestCase {
55     private static XTextDocument xTextDoc;
56 
initialize(TestParameters Param, PrintWriter log)57     protected void initialize(TestParameters Param, PrintWriter log) {
58         SOfficeFactory SOF = SOfficeFactory.getFactory(
59                                      (XMultiServiceFactory) Param.getMSF());
60 
61         try {
62             log.println("creating a textdocument");
63             xTextDoc = SOF.createTextDoc(null);
64         } catch (com.sun.star.uno.Exception e) {
65             // Some exception occures.FAILED
66             e.printStackTrace(log);
67             throw new StatusException("Couldn't create document", e);
68         }
69     }
70 
cleanup(TestParameters tParam, PrintWriter log)71     protected void cleanup(TestParameters tParam, PrintWriter log) {
72         log.println("    disposing xTextDoc ");
73 
74         util.DesktopTools.closeDoc(xTextDoc);
75     }
76 
createTestEnvironment(TestParameters Param, PrintWriter log)77     protected TestEnvironment createTestEnvironment(TestParameters Param,
78                                                     PrintWriter log) {
79         XInterface oObj = null;
80         XWindowPeer the_win = null;
81         XToolkit the_kit = null;
82         XDevice aDevice = null;
83         XGraphics aGraphic = null;
84         XControl aControl = null;
85 
86         //Insert a ControlShape and get the ControlModel
87         XControlShape aShape = FormTools.createUnoControlShape(xTextDoc, 3000,
88                                                                4500, 15000,
89                                                                10000,
90                                                                "ListBox",
91                                                                "UnoControlListBox");
92 
93         WriterTools.getDrawPage(xTextDoc).add((XShape) aShape);
94 
95         XControlModel the_Model = aShape.getControl();
96 
97         XControlShape aShape2 = FormTools.createControlShape(xTextDoc, 3000,
98                                                              4500, 5000, 10000,
99                                                              "TextField");
100 
101         WriterTools.getDrawPage(xTextDoc).add((XShape) aShape2);
102 
103         XControlModel the_Model2 = aShape2.getControl();
104 
105         //Try to query XControlAccess
106         XControlAccess the_access = (XControlAccess) UnoRuntime.queryInterface(
107                                             XControlAccess.class,
108                                             xTextDoc.getCurrentController());
109 
110         //get the ListBoxControl for the needed Object relations
111         try {
112             oObj = the_access.getControl(the_Model);
113             aControl = the_access.getControl(the_Model2);
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 ListBoxControl");
120             e.printStackTrace(log);
121             throw new StatusException("Couldn't get ListBoxControl", e);
122         }
123 
124         log.println("creating a new environment for UnoControlListBox 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         XListBox list = (XListBox) UnoRuntime.queryInterface(XListBox.class,
143                                                              oObj);
144         list.addItemListener(listener);
145         tEnv.addObjRelation("TestItemListener", listener);
146 
147         XWindow forObjRel = (XWindow) UnoRuntime.queryInterface(XWindow.class,
148                                                                 aControl);
149 
150         tEnv.addObjRelation("XWindow.AnotherWindow", forObjRel);
151         tEnv.addObjRelation("XWindow.ControlShape", aShape);
152 
153         System.out.println("ImplementationName: " + utils.getImplName(oObj));
154 
155         return tEnv;
156     } // finish method getTestEnvironment
157 } // finish class UnoControlListBox
158