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 
24 package mod._forms;
25 
26 import java.io.PrintWriter;
27 
28 import lib.StatusException;
29 import lib.TestCase;
30 import lib.TestEnvironment;
31 import lib.TestParameters;
32 import util.FormTools;
33 import util.SOfficeFactory;
34 import util.WriterTools;
35 
36 import com.sun.star.awt.XControlModel;
37 import com.sun.star.awt.XDevice;
38 import com.sun.star.awt.XGraphics;
39 import com.sun.star.awt.XListBox;
40 import com.sun.star.awt.XToolkit;
41 import com.sun.star.awt.XWindow;
42 import com.sun.star.awt.XWindowPeer;
43 import com.sun.star.drawing.XControlShape;
44 import com.sun.star.drawing.XShape;
45 import com.sun.star.lang.XMultiServiceFactory;
46 import com.sun.star.text.XTextDocument;
47 import com.sun.star.uno.UnoRuntime;
48 import com.sun.star.uno.XInterface;
49 import com.sun.star.util.XCloseable;
50 import com.sun.star.view.XControlAccess;
51 
52 public class OListBoxControl extends TestCase {
53 
54     XTextDocument xTextDoc;
55 
initialize( TestParameters Param, PrintWriter log)56     protected void initialize ( TestParameters Param, PrintWriter log) {
57         SOfficeFactory SOF = SOfficeFactory.getFactory( ((XMultiServiceFactory) Param.getMSF()) );
58 
59         try {
60             log.println( "creating a textdocument" );
61             xTextDoc = SOF.createTextDoc( null );
62         } catch ( com.sun.star.uno.Exception e ) {
63             // Some exception occured.FAILED
64             e.printStackTrace( log );
65             throw new StatusException( "Couldn't create document", e );
66         }
67     }
68 
cleanup(TestParameters tParam, PrintWriter log)69     protected void cleanup(TestParameters tParam, PrintWriter log) {
70         log.println("    disposing xTextDoc ");
71 
72         try {
73             XCloseable closer = (XCloseable) UnoRuntime.queryInterface(
74                                         XCloseable.class, xTextDoc);
75             closer.close(true);
76         } catch (com.sun.star.util.CloseVetoException e) {
77             log.println("couldn't close document");
78         } catch (com.sun.star.lang.DisposedException e) {
79             log.println("couldn't close document");
80         }
81     }
82 
createTestEnvironment(TestParameters Param, PrintWriter log)83     protected TestEnvironment createTestEnvironment(TestParameters Param, PrintWriter log) {
84         XInterface oObj = null;
85         Object anotherCtrl = null ;
86         XWindowPeer the_win = null;
87         XToolkit the_kit = null;
88         XDevice aDevice = null;
89         XGraphics aGraphic = null;
90         //Insert a ControlShape and get the ControlModel
91         XControlShape aShape = FormTools.createControlShape(
92                                 xTextDoc,3000,4500,15000,10000,"ListBox");
93 
94         WriterTools.getDrawPage(xTextDoc).add((XShape) aShape);
95 
96         XControlModel the_Model = aShape.getControl();
97 
98         XControlShape aShape2 = FormTools.createControlShape(
99                                 xTextDoc,3000,4500,5000,10000,"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,xTextDoc.getCurrentController());
108 
109         //now get the OListBoxControl
110         try {
111             oObj = the_access.getControl(the_Model);
112             anotherCtrl = the_access.getControl(the_Model2);
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 OListBoxControl");
119             e.printStackTrace(log);
120             throw new StatusException("Couldn't get OListBoxControl", e );
121         }
122 
123         log.println( "creating a new environment for OListBoxControl object" );
124         TestEnvironment tEnv = new TestEnvironment( oObj );
125 
126         //Adding ObjRelation for XView
127         tEnv.addObjRelation("GRAPHICS",aGraphic);
128 
129         //Adding ObjRelation for XControl
130         tEnv.addObjRelation("CONTEXT",xTextDoc);
131         tEnv.addObjRelation("WINPEER",the_win);
132         tEnv.addObjRelation("TOOLKIT",the_kit);
133         tEnv.addObjRelation("MODEL",the_Model);
134 
135         // Adding relation for XItemListener
136         ifc.awt._XItemListener.TestItemListener listener =
137             new ifc.awt._XItemListener.TestItemListener() ;
138         final XListBox box = (XListBox) UnoRuntime.queryInterface(XListBox.class, oObj) ;
139         box.addItemListener(listener) ;
140         tEnv.addObjRelation("TestItemListener", listener) ;
141 
142         // Adding relation for XWindow
143         XWindow forObjRel = (XWindow)
144                             UnoRuntime.queryInterface(XWindow.class, anotherCtrl);
145 
146         XWindow objWin = (XWindow)
147                             UnoRuntime.queryInterface(XWindow.class, oObj);
148 
149         tEnv.addObjRelation("XWindow.AnotherWindow",forObjRel);
150         tEnv.addObjRelation("XWindow.ControlShape",aShape);
151 
152         tEnv.addObjRelation("Win1",objWin);
153         tEnv.addObjRelation("Win2",forObjRel);
154 
155         tEnv.addObjRelation("CONTROL",anotherCtrl);
156 
157         // adding relation for XChangeBroadcaster
158         box.addItem("Item1", (short) 0);
159         box.addItem("Item2", (short) 1);
160 
161         tEnv.addObjRelation("XChangeBroadcaster.Changer",
162             new ifc.form._XChangeBroadcaster.Changer() {
163                 public void change(){
164                     box.addItem("Item1", (short) 0);
165                     box.addItem("Item2", (short) 1);
166                     box.selectItemPos((short) 0, true);
167                     box.selectItemPos((short) 1, true);
168                 }
169             }
170         );
171 
172         return tEnv;
173     } // finish method getTestEnvironment
174 
175 }    // finish class OListBoxControl
176 
177