1 /*
2  * This file is part of the LibreOffice project.
3  *
4  * This Source Code Form is subject to the terms of the Mozilla Public
5  * License, v. 2.0. If a copy of the MPL was not distributed with this
6  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
7  *
8  * This file incorporates work covered by the following license notice:
9  *
10  *   Licensed to the Apache Software Foundation (ASF) under one or more
11  *   contributor license agreements. See the NOTICE file distributed
12  *   with this work for additional information regarding copyright
13  *   ownership. The ASF licenses this file to you under the Apache
14  *   License, Version 2.0 (the "License"); you may not use this file
15  *   except in compliance with the License. You may obtain a copy of
16  *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
17  */
18 
19 package mod._forms;
20 
21 import java.io.PrintWriter;
22 
23 import lib.TestCase;
24 import lib.TestEnvironment;
25 import lib.TestParameters;
26 import util.FormTools;
27 import util.SOfficeFactory;
28 import util.WriterTools;
29 
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.XListBox;
34 import com.sun.star.awt.XToolkit;
35 import com.sun.star.awt.XWindow;
36 import com.sun.star.awt.XWindowPeer;
37 import com.sun.star.drawing.XControlShape;
38 import com.sun.star.text.XTextDocument;
39 import com.sun.star.uno.UnoRuntime;
40 import com.sun.star.uno.XInterface;
41 import com.sun.star.util.XCloseable;
42 import com.sun.star.view.XControlAccess;
43 
44 public class OListBoxControl extends TestCase {
45 
46     XTextDocument xTextDoc;
47 
48     @Override
initialize( TestParameters Param, PrintWriter log)49     protected void initialize ( TestParameters Param, PrintWriter log) throws Exception {
50         SOfficeFactory SOF = SOfficeFactory.getFactory( Param.getMSF() );
51         log.println( "creating a textdocument" );
52         xTextDoc = SOF.createTextDoc( null );
53     }
54 
55     @Override
cleanup(TestParameters tParam, PrintWriter log)56     protected void cleanup(TestParameters tParam, PrintWriter log) {
57         log.println("    disposing xTextDoc ");
58 
59         try {
60             XCloseable closer = UnoRuntime.queryInterface(
61                                         XCloseable.class, xTextDoc);
62             closer.close(true);
63         } catch (com.sun.star.util.CloseVetoException e) {
64             log.println("couldn't close document");
65         } catch (com.sun.star.lang.DisposedException e) {
66             log.println("couldn't close document");
67         }
68     }
69 
70     @Override
createTestEnvironment(TestParameters Param, PrintWriter log)71     protected TestEnvironment createTestEnvironment(TestParameters Param, PrintWriter log) throws Exception {
72         XInterface oObj = null;
73         Object anotherCtrl = null ;
74         XWindowPeer the_win = null;
75         XToolkit the_kit = null;
76         XDevice aDevice = null;
77         XGraphics aGraphic = null;
78         //Insert a ControlShape and get the ControlModel
79         XControlShape aShape = FormTools.createControlShape(
80                                 xTextDoc,3000,4500,15000,10000,"ListBox");
81 
82         WriterTools.getDrawPage(xTextDoc).add(aShape);
83 
84         XControlModel the_Model = aShape.getControl();
85 
86         XControlShape aShape2 = FormTools.createControlShape(
87                                 xTextDoc,3000,4500,5000,10000,"TextField");
88 
89         WriterTools.getDrawPage(xTextDoc).add(aShape2);
90 
91         XControlModel the_Model2 = aShape2.getControl();
92 
93         //Try to query XControlAccess
94         XControlAccess the_access = UnoRuntime.queryInterface(
95                         XControlAccess.class,xTextDoc.getCurrentController());
96 
97         //now get the OListBoxControl
98         oObj = the_access.getControl(the_Model);
99         anotherCtrl = the_access.getControl(the_Model2);
100         the_win = the_access.getControl(the_Model).getPeer();
101         the_kit = the_win.getToolkit();
102         aDevice = the_kit.createScreenCompatibleDevice(200,200);
103         aGraphic = aDevice.createGraphics();
104 
105         log.println( "creating a new environment for OListBoxControl object" );
106         TestEnvironment tEnv = new TestEnvironment( oObj );
107 
108         //Adding ObjRelation for XView
109         tEnv.addObjRelation("GRAPHICS",aGraphic);
110 
111         //Adding ObjRelation for XControl
112         tEnv.addObjRelation("CONTEXT",xTextDoc);
113         tEnv.addObjRelation("WINPEER",the_win);
114         tEnv.addObjRelation("TOOLKIT",the_kit);
115         tEnv.addObjRelation("MODEL",the_Model);
116 
117         // Adding relation for XItemListener
118         ifc.awt._XItemListener.TestItemListener listener =
119             new ifc.awt._XItemListener.TestItemListener() ;
120         final XListBox box = UnoRuntime.queryInterface(XListBox.class, oObj) ;
121         box.addItemListener(listener) ;
122         tEnv.addObjRelation("TestItemListener", listener) ;
123 
124         // Adding relation for XWindow
125         XWindow forObjRel = UnoRuntime.queryInterface(XWindow.class, anotherCtrl);
126 
127         XWindow objWin = UnoRuntime.queryInterface(XWindow.class, oObj);
128 
129         tEnv.addObjRelation("XWindow.AnotherWindow",forObjRel);
130         tEnv.addObjRelation("XWindow.ControlShape",aShape);
131 
132         tEnv.addObjRelation("Win1",objWin);
133         tEnv.addObjRelation("Win2",forObjRel);
134 
135         tEnv.addObjRelation("CONTROL",anotherCtrl);
136 
137         // adding relation for XChangeBroadcaster
138         box.addItem("Item1", (short) 0);
139         box.addItem("Item2", (short) 1);
140 
141         tEnv.addObjRelation("XChangeBroadcaster.Changer",
142             new ifc.form._XChangeBroadcaster.Changer() {
143                 public void change(){
144                     box.addItem("Item1", (short) 0);
145                     box.addItem("Item2", (short) 1);
146                     box.selectItemPos((short) 0, true);
147                     box.selectItemPos((short) 1, true);
148                 }
149             }
150         );
151 
152         return tEnv;
153     } // finish method getTestEnvironment
154 
155 }    // finish class OListBoxControl
156 
157