bump product version to 4.2.0.1
[LibreOffice.git] / qadevOOo / tests / java / mod / _toolkit / UnoTreeControl.java
blobddf8ec8440b5742db017c203841fb806c707c413
1 /*
2 * This file is part of the LibreOffice project.
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/.
8 * This file incorporates work covered by the following license notice:
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 .
18 package mod._toolkit;
20 import com.sun.star.awt.XControl;
21 import com.sun.star.awt.XControlContainer;
22 import com.sun.star.awt.XControlModel;
23 import com.sun.star.awt.XDialog;
24 import com.sun.star.awt.XToolkit;
25 import com.sun.star.awt.tree.XMutableTreeDataModel;
26 import com.sun.star.awt.tree.XMutableTreeNode;
27 import com.sun.star.awt.tree.XTreeControl;
28 import com.sun.star.awt.tree.XTreeNode;
29 import com.sun.star.beans.XPropertySet;
30 import com.sun.star.container.XNameContainer;
31 import com.sun.star.lang.XMultiServiceFactory;
32 import com.sun.star.ucb.CommandAbortedException;
33 import com.sun.star.ucb.XSimpleFileAccess;
34 import com.sun.star.uno.AnyConverter;
35 import com.sun.star.uno.Type;
36 import com.sun.star.uno.UnoRuntime;
37 import com.sun.star.uno.XInterface;
39 import java.io.PrintWriter;
40 import java.util.Comparator;
42 import lib.StatusException;
43 import lib.TestCase;
44 import lib.TestEnvironment;
45 import lib.TestParameters;
46 import util.SOfficeFactory;
47 import util.utils;
50 public class UnoTreeControl extends TestCase {
51 private static XMutableTreeDataModel mXTreeDataModel;
52 private static XMultiServiceFactory mxMSF;
54 protected void initialize(TestParameters Param, PrintWriter log) {
55 SOfficeFactory SOF = SOfficeFactory.getFactory(
56 (XMultiServiceFactory) Param.getMSF());
58 try {
59 log.println("creating a textdocument");
60 SOF.createTextDoc(null);
61 } catch (com.sun.star.uno.Exception e) {
62 // Some exception occurs.FAILED
63 e.printStackTrace(log);
64 throw new StatusException("Couldn't create document", e);
68 protected void cleanup(TestParameters tParam, PrintWriter log) {
69 // log.println(" disposing xTextDoc ");
71 // util.DesktopTools.closeDoc(xTextDoc);
74 protected TestEnvironment createTestEnvironment(TestParameters Param,
75 PrintWriter log) {
76 String sTreeControlName = "UnoTreeControl-Test";
77 mxMSF = (XMultiServiceFactory) Param.getMSF();
78 XInterface oObj = null;
79 XMutableTreeNode xNode = null;
81 try {
83 mXTreeDataModel = UnoRuntime.queryInterface(XMutableTreeDataModel.class,
84 mxMSF.createInstance("com.sun.star.awt.tree.MutableTreeDataModel"));
86 xNode = mXTreeDataModel.createNode("UnoTreeControl", false);
88 xNode.setDataValue( "UnoTreeControl");
89 xNode.setExpandedGraphicURL( "private:graphicrepository/sd/res/triangle_down.png");
90 xNode.setCollapsedGraphicURL( "private:graphicrepository/sd/res/triangle_right.png");
92 fillNode(xNode);
94 mXTreeDataModel.setRoot(xNode);
96 XControlModel xDialogModel = UnoRuntime.queryInterface(XControlModel.class,
97 mxMSF.createInstance("com.sun.star.awt.UnoControlDialogModel"));
99 XPropertySet xDialogPropertySet = UnoRuntime.queryInterface(XPropertySet.class, xDialogModel);
100 xDialogPropertySet.setPropertyValue( "PositionX", new Integer(50) );
101 xDialogPropertySet.setPropertyValue( "PositionY", new Integer(50) );
102 xDialogPropertySet.setPropertyValue( "Width", new Integer(256) );
103 xDialogPropertySet.setPropertyValue( "Height", new Integer(256) );
104 xDialogPropertySet.setPropertyValue( "Title", "Tree Control Test");
106 XMultiServiceFactory xDialogMSF = UnoRuntime.queryInterface(XMultiServiceFactory.class, xDialogModel);
108 XControlModel xTreeControlModel = UnoRuntime.queryInterface(XControlModel.class,
109 xDialogMSF.createInstance("com.sun.star.awt.tree.TreeControlModel"));
111 XPropertySet XTreeControlModelSet = UnoRuntime.queryInterface(XPropertySet.class, xTreeControlModel);
113 XTreeControlModelSet.setPropertyValue( "SelectionType",com.sun.star.view.SelectionType.NONE);
114 XTreeControlModelSet.setPropertyValue( "PositionX", new Integer(3 ));
115 XTreeControlModelSet.setPropertyValue( "PositionY", new Integer(3 ));
116 XTreeControlModelSet.setPropertyValue( "Width", new Integer(253));
117 XTreeControlModelSet.setPropertyValue( "Height", new Integer(253) );
118 XTreeControlModelSet.setPropertyValue( "DataModel", mXTreeDataModel );
119 XTreeControlModelSet.setPropertyValue( "ShowsRootHandles",new Boolean (false));
120 XTreeControlModelSet.setPropertyValue( "ShowsHandles", new Boolean (false));
121 XTreeControlModelSet.setPropertyValue( "RootDisplayed", new Boolean (true));
122 XTreeControlModelSet.setPropertyValue( "Editable", new Boolean (true ));
124 XNameContainer xDialogModelContainer = UnoRuntime.queryInterface(XNameContainer.class, xDialogModel);
126 xDialogModelContainer.insertByName( sTreeControlName, xTreeControlModel);
128 XControl xDialogControl = UnoRuntime.queryInterface(XControl.class,
129 mxMSF.createInstance("com.sun.star.awt.UnoControlDialog"));
131 xDialogControl.setModel( xDialogModel );
133 XToolkit xToolkit = UnoRuntime.queryInterface(XToolkit.class,
134 mxMSF.createInstance("com.sun.star.awt.Toolkit" ));
136 xDialogControl.createPeer( xToolkit, null );
138 // get the peers of the sub controls from the dialog peer container
139 XControlContainer xDialogContainer = UnoRuntime.queryInterface(XControlContainer.class ,xDialogControl);
141 XTreeControl xTreeControl = UnoRuntime.queryInterface(XTreeControl.class, xDialogContainer.getControl( sTreeControlName ));
143 xTreeControl.expandNode(xNode);
144 oObj = xTreeControl;
146 XDialog xDialog = UnoRuntime.queryInterface(XDialog.class, xDialogControl);
148 execurteDialog aDialog = new execurteDialog(xDialog);
150 aDialog.start();
152 // xDialog.execute();
154 } catch (com.sun.star.uno.Exception ex) {
155 ex.printStackTrace();
157 TestEnvironment tEnv = new TestEnvironment(oObj);
159 tEnv.addObjRelation("XTreeControl_Node", xNode);
161 //com.sun.star.view.XSelectionSupplier
162 try {
164 System.out.println("count of children: " + xNode.getChildCount());
165 tEnv.addObjRelation("Selections", new Object[]{xNode.getChildAt(0), xNode});
166 } catch (com.sun.star.lang.IndexOutOfBoundsException ex) {
167 log.println("ERROR: could not add object relation 'Selections' because 'xNode.getChildAt(1) failed: " +
168 ex.toString());
171 tEnv.addObjRelation("Comparer",
172 new Comparator<Object>() {
173 public int compare(Object o1, Object o2) {
174 XMutableTreeNode xNode1 = UnoRuntime.queryInterface(
175 XMutableTreeNode.class, o1);
176 XTreeNode xNode2a = null;
177 try {
178 xNode2a = (XTreeNode) AnyConverter.toObject(new Type(XTreeNode.class), o2);
179 } catch (com.sun.star.lang.IllegalArgumentException ex) {
180 ex.printStackTrace();
183 XMutableTreeNode xNode2 = UnoRuntime.queryInterface(
184 XMutableTreeNode.class, xNode2a);
186 if (((String) xNode1.getDataValue()).equals(xNode2.getDataValue())) {
187 return 0;
190 return -1;
193 System.out.println("ImplementationName: " + utils.getImplName(oObj));
195 return tEnv;
196 } // finish method getTestEnvironment
198 private void fillNode( XMutableTreeNode xNode ){
200 if( xNode.getChildCount() == 0 )
202 xNode.getDataValue();
204 String officeUserPath = utils.getOfficeUserPath(mxMSF);
205 Object fileacc = null;
206 try {
207 fileacc = mxMSF.createInstance("com.sun.star.comp.ucb.SimpleFileAccess");
208 } catch (com.sun.star.uno.Exception ex) {
209 ex.printStackTrace();
211 UnoRuntime.queryInterface(XSimpleFileAccess.class,fileacc);
214 dirlist(officeUserPath, xNode);
218 private void dirlist(String dir, XMutableTreeNode xNode){
220 Object fileacc = null;
221 try {
222 fileacc = mxMSF.createInstance("com.sun.star.comp.ucb.SimpleFileAccess");
223 } catch (com.sun.star.uno.Exception ex) {
224 ex.printStackTrace();
226 XSimpleFileAccess sfa = UnoRuntime.queryInterface(XSimpleFileAccess.class,fileacc);
227 XMutableTreeNode xChildNode = null;
228 try {
229 xChildNode = mXTreeDataModel.createNode(dir.substring(dir.lastIndexOf("/")+1, dir.length()), sfa.isFolder(dir));
230 xChildNode.setDataValue(dir);
231 sfa.isFolder(dir);
232 if (sfa.isFolder(dir)){
233 xChildNode.setExpandedGraphicURL( "private:graphicrepository/sd/res/triangle_down.png");
234 xChildNode.setCollapsedGraphicURL("private:graphicrepository/sd/res/triangle_right.png");
235 String[] children = sfa.getFolderContents(dir, true);
236 if (children != null){
237 for (int i=0; i<children.length; i++) {
238 // Get filename of file or directory
239 String filename = children[i];
240 dirlist( filename , xChildNode);
244 else{
245 xChildNode.setNodeGraphicURL( "private:graphicrepository/sw/imglst/nc20010.png");
247 } catch (CommandAbortedException ex) {
248 ex.printStackTrace();
249 } catch (com.sun.star.uno.Exception ex) {
250 ex.printStackTrace();
253 try {
254 xNode.appendChild( xChildNode );
255 } catch (com.sun.star.lang.IllegalArgumentException ex) {
256 ex.printStackTrace();
260 private class execurteDialog extends Thread{
261 private XDialog mXDialog;
263 public execurteDialog(XDialog xDialog){
264 mXDialog = xDialog;
267 public void run() {
268 mXDialog.endExecute();
273 } // finish class UnoControlRadioButton