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.PosSize; 30 import com.sun.star.awt.Rectangle; 31 import com.sun.star.awt.XControl; 32 import com.sun.star.awt.XControlContainer; 33 import com.sun.star.awt.XControlModel; 34 import com.sun.star.awt.XDevice; 35 import com.sun.star.awt.XGraphics; 36 import com.sun.star.awt.XToolkit; 37 import com.sun.star.awt.XWindow; 38 import com.sun.star.awt.XWindowPeer; 39 import com.sun.star.drawing.XControlShape; 40 import com.sun.star.drawing.XShape; 41 import com.sun.star.frame.XController; 42 import com.sun.star.frame.XFrame; 43 import com.sun.star.lang.XMultiServiceFactory; 44 import com.sun.star.text.XTextDocument; 45 import com.sun.star.uno.UnoRuntime; 46 import com.sun.star.uno.XInterface; 47 import com.sun.star.view.XControlAccess; 48 49 import java.io.PrintWriter; 50 51 import lib.StatusException; 52 import lib.TestCase; 53 import lib.TestEnvironment; 54 import lib.TestParameters; 55 56 import util.FormTools; 57 import util.WriterTools; 58 import util.utils; 59 60 61 public class UnoControlContainer extends TestCase { 62 private static XTextDocument xTextDoc; 63 private static XTextDocument xTD2; 64 private static XControl xCtrl; 65 private static XControl xCtrl1; 66 private static XControl xCtrl2; 67 68 protected void initialize(TestParameters param, PrintWriter log) { 69 try { 70 log.println("creating a textdocument"); 71 xTD2 = WriterTools.createTextDoc( 72 (XMultiServiceFactory) param.getMSF()); 73 xTextDoc = WriterTools.createTextDoc( 74 (XMultiServiceFactory) param.getMSF()); 75 } catch (Exception e) { 76 // Some exception occures.FAILED 77 e.printStackTrace(log); 78 throw new StatusException("Couldn't create document", e); 79 } 80 } 81 82 protected void cleanup(TestParameters tParam, PrintWriter log) { 83 log.println(" disposing xTextDoc "); 84 85 util.DesktopTools.closeDoc(xTextDoc); 86 util.DesktopTools.closeDoc(xTD2); 87 } 88 89 public TestEnvironment createTestEnvironment(TestParameters param, 90 PrintWriter log) { 91 // create Object Relations ------------------------------------------- 92 XInterface oObj = null; 93 XControlShape shape = null; 94 XControlModel model = null; 95 XControlAccess access = null; 96 XWindow anotherWindow = null; 97 98 // for XControl 99 XWindowPeer the_win = null; 100 XToolkit the_kit = null; 101 102 XControlContainer ctrlCont = null; 103 104 XGraphics aGraphic = null; 105 106 107 // create 3 XControls 108 // create first XControl 109 shape = FormTools.createControlShape(xTextDoc, 3000, 4500, 15000, 110 10000, "TextField"); 111 WriterTools.getDrawPage(xTextDoc).add((XShape) shape); 112 model = shape.getControl(); 113 access = (XControlAccess) UnoRuntime.queryInterface( 114 XControlAccess.class, xTextDoc.getCurrentController()); 115 116 try { 117 xCtrl = access.getControl(model); 118 } catch (Exception e) { 119 e.printStackTrace(log); 120 throw new StatusException("Couldn't create XControl", e); 121 } 122 123 124 // create second XControl 125 shape = FormTools.createControlShape(xTextDoc, 3000, 4500, 15000, 126 10000, "TextField"); 127 WriterTools.getDrawPage(xTextDoc).add((XShape) shape); 128 model = shape.getControl(); 129 access = (XControlAccess) UnoRuntime.queryInterface( 130 XControlAccess.class, xTextDoc.getCurrentController()); 131 132 try { 133 xCtrl1 = access.getControl(model); 134 } catch (Exception e) { 135 e.printStackTrace(log); 136 throw new StatusException("Couldn't create XControl", e); 137 } 138 139 140 // create third XControl 141 shape = FormTools.createControlShape(xTextDoc, 3000, 4500, 15000, 142 10000, "CommandButton"); 143 WriterTools.getDrawPage(xTextDoc).add((XShape) shape); 144 model = shape.getControl(); 145 access = (XControlAccess) UnoRuntime.queryInterface( 146 XControlAccess.class, xTextDoc.getCurrentController()); 147 148 try { 149 xCtrl2 = access.getControl(model); 150 } catch (Exception e) { 151 e.printStackTrace(log); 152 throw new StatusException("Couldn't create XControl", e); 153 } 154 155 // create XToolkit, XWindowPeer, XDevice 156 //Insert a ControlShape and get the ControlModel 157 XControlShape aShape = FormTools.createUnoControlShape(xTextDoc, 3000, 158 4500, 15000, 159 10000, 160 "CommandButton", 161 "UnoControlButton"); 162 163 WriterTools.getDrawPage(xTD2).add((XShape) aShape); 164 165 XControlModel the_Model = aShape.getControl(); 166 167 //Try to query XControlAccess 168 XControlAccess the_access = (XControlAccess) UnoRuntime.queryInterface( 169 XControlAccess.class, 170 xTD2.getCurrentController()); 171 172 //get the ButtonControl for the needed Object relations 173 try { 174 the_win = the_access.getControl(the_Model).getPeer(); 175 the_kit = the_win.getToolkit(); 176 177 XDevice aDevice = the_kit.createScreenCompatibleDevice(200, 200); 178 aGraphic = aDevice.createGraphics(); 179 } catch (Exception e) { 180 log.println("Couldn't get ButtonControl"); 181 e.printStackTrace(log); 182 throw new StatusException("Couldn't get ButtonControl", e); 183 } 184 185 try { 186 XController aController = xTD2.getCurrentController(); 187 XFrame aFrame = aController.getFrame(); 188 anotherWindow = aFrame.getComponentWindow(); 189 } catch (Exception e) { 190 e.printStackTrace(log); 191 throw new StatusException("Couldn't create XWindow", e); 192 } 193 194 // finished create Object Relations ----------------------------------- 195 // create the UnoControlContainer 196 try { 197 oObj = (XInterface) ((XMultiServiceFactory) param.getMSF()).createInstance( 198 "com.sun.star.awt.UnoControlContainer"); 199 200 XControl xCtrl = (XControl) UnoRuntime.queryInterface( 201 XControl.class, oObj); 202 xCtrl.setModel(the_Model); 203 204 ctrlCont = (XControlContainer) UnoRuntime.queryInterface( 205 XControlContainer.class, oObj); 206 ctrlCont.addControl("jupp", access.getControl(aShape.getControl())); 207 } catch (Exception e) { 208 e.printStackTrace(log); 209 throw new StatusException("Couldn't create UnoControlContainer", e); 210 } 211 212 log.println( 213 "creating a new environment for UnoControlContainer object"); 214 215 TestEnvironment tEnv = new TestEnvironment(oObj); 216 217 XWindow xWindow = (XWindow) UnoRuntime.queryInterface(XWindow.class, oObj); 218 Rectangle ps = xWindow.getPosSize(); 219 xWindow.setPosSize(ps.X+10, ps.Y+10, ps.Width+10, ps.Height+10, PosSize.POSSIZE); 220 221 String objName = "UnoControlContainer"; 222 tEnv.addObjRelation("OBJNAME", "toolkit." + objName); 223 224 225 // Object relation for XContainer 226 tEnv.addObjRelation("XContainer.Container", ctrlCont); 227 tEnv.addObjRelation("INSTANCE", xCtrl); 228 229 230 //Adding ObjRelation for XView 231 tEnv.addObjRelation("GRAPHICS", aGraphic); 232 233 234 // Object Relation for XControlContainer 235 tEnv.addObjRelation("CONTROL1", xCtrl1); 236 tEnv.addObjRelation("CONTROL2", xCtrl2); 237 238 239 // Object Relation for XControl 240 tEnv.addObjRelation("CONTEXT", xTD2); 241 tEnv.addObjRelation("WINPEER", the_win); 242 tEnv.addObjRelation("TOOLKIT", the_kit); 243 tEnv.addObjRelation("MODEL", the_Model); 244 245 246 // Object Relation for XWindow 247 tEnv.addObjRelation("XWindow.AnotherWindow", anotherWindow); 248 System.out.println("ImplementationName: " + utils.getImplName(oObj)); 249 250 return tEnv; 251 } 252 } 253