bump product version to 5.0.4.1
[LibreOffice.git] / qadevOOo / tests / java / mod / _toolkit / UnoTreeControl.java
blob1fc6c51fe8c5a86fe17e95f3ffa749ef33eda337
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 @Override
55 protected void initialize(TestParameters Param, PrintWriter log) {
56 SOfficeFactory SOF = SOfficeFactory.getFactory(
57 Param.getMSF());
59 try {
60 log.println("creating a textdocument");
61 SOF.createTextDoc(null);
62 } catch (com.sun.star.uno.Exception e) {
63 // Some exception occurs.FAILED
64 e.printStackTrace(log);
65 throw new StatusException("Couldn't create document", e);
69 @Override
70 protected void cleanup(TestParameters tParam, PrintWriter log) {
73 @Override
74 protected TestEnvironment createTestEnvironment(TestParameters Param,
75 PrintWriter log) {
76 String sTreeControlName = "UnoTreeControl-Test";
77 mxMSF = 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", Integer.valueOf(50) );
101 xDialogPropertySet.setPropertyValue( "PositionY", Integer.valueOf(50) );
102 xDialogPropertySet.setPropertyValue( "Width", Integer.valueOf(256) );
103 xDialogPropertySet.setPropertyValue( "Height", Integer.valueOf(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", Integer.valueOf(3 ));
115 XTreeControlModelSet.setPropertyValue( "PositionY", Integer.valueOf(3 ));
116 XTreeControlModelSet.setPropertyValue( "Width", Integer.valueOf(253));
117 XTreeControlModelSet.setPropertyValue( "Height", Integer.valueOf(253) );
118 XTreeControlModelSet.setPropertyValue( "DataModel", mXTreeDataModel );
119 XTreeControlModelSet.setPropertyValue( "ShowsRootHandles", Boolean.FALSE);
120 XTreeControlModelSet.setPropertyValue( "ShowsHandles", Boolean.FALSE);
121 XTreeControlModelSet.setPropertyValue( "RootDisplayed", Boolean.TRUE);
122 XTreeControlModelSet.setPropertyValue( "Editable", 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 } catch (com.sun.star.uno.Exception ex) {
153 ex.printStackTrace();
155 TestEnvironment tEnv = new TestEnvironment(oObj);
157 tEnv.addObjRelation("XTreeControl_Node", xNode);
159 //com.sun.star.view.XSelectionSupplier
160 try {
162 System.out.println("count of children: " + xNode.getChildCount());
163 tEnv.addObjRelation("Selections", new Object[]{xNode.getChildAt(0), xNode});
164 } catch (com.sun.star.lang.IndexOutOfBoundsException ex) {
165 log.println("ERROR: could not add object relation 'Selections' because 'xNode.getChildAt(1) failed: " +
166 ex.toString());
169 tEnv.addObjRelation("Comparer",
170 new Comparator<Object>() {
171 public int compare(Object o1, Object o2) {
172 XMutableTreeNode xNode1 = UnoRuntime.queryInterface(
173 XMutableTreeNode.class, o1);
174 XTreeNode xNode2a = null;
175 try {
176 xNode2a = (XTreeNode) AnyConverter.toObject(new Type(XTreeNode.class), o2);
177 } catch (com.sun.star.lang.IllegalArgumentException ex) {
178 ex.printStackTrace();
181 XMutableTreeNode xNode2 = UnoRuntime.queryInterface(
182 XMutableTreeNode.class, xNode2a);
184 if (((String) xNode1.getDataValue()).equals(xNode2.getDataValue())) {
185 return 0;
188 return -1;
191 System.out.println("ImplementationName: " + utils.getImplName(oObj));
193 return tEnv;
194 } // finish method getTestEnvironment
196 private void fillNode( XMutableTreeNode xNode ){
198 if( xNode.getChildCount() == 0 )
200 xNode.getDataValue();
202 String officeUserPath = utils.getOfficeUserPath(mxMSF);
203 Object fileacc = null;
204 try {
205 fileacc = mxMSF.createInstance("com.sun.star.comp.ucb.SimpleFileAccess");
206 } catch (com.sun.star.uno.Exception ex) {
207 ex.printStackTrace();
209 UnoRuntime.queryInterface(XSimpleFileAccess.class,fileacc);
212 dirlist(officeUserPath, xNode);
216 private void dirlist(String dir, XMutableTreeNode xNode){
218 Object fileacc = null;
219 try {
220 fileacc = mxMSF.createInstance("com.sun.star.comp.ucb.SimpleFileAccess");
221 } catch (com.sun.star.uno.Exception ex) {
222 ex.printStackTrace();
224 XSimpleFileAccess sfa = UnoRuntime.queryInterface(XSimpleFileAccess.class,fileacc);
225 XMutableTreeNode xChildNode = null;
226 try {
227 xChildNode = mXTreeDataModel.createNode(dir.substring(dir.lastIndexOf("/")+1, dir.length()), sfa.isFolder(dir));
228 xChildNode.setDataValue(dir);
229 sfa.isFolder(dir);
230 if (sfa.isFolder(dir)){
231 xChildNode.setExpandedGraphicURL( "private:graphicrepository/sd/res/triangle_down.png");
232 xChildNode.setCollapsedGraphicURL("private:graphicrepository/sd/res/triangle_right.png");
233 String[] children = sfa.getFolderContents(dir, true);
234 if (children != null){
235 for (int i=0; i<children.length; i++) {
236 // Get filename of file or directory
237 String filename = children[i];
238 dirlist( filename , xChildNode);
242 else{
243 xChildNode.setNodeGraphicURL( "private:graphicrepository/sw/imglst/nc20010.png");
245 } catch (CommandAbortedException ex) {
246 ex.printStackTrace();
247 } catch (com.sun.star.uno.Exception ex) {
248 ex.printStackTrace();
251 try {
252 xNode.appendChild( xChildNode );
253 } catch (com.sun.star.lang.IllegalArgumentException ex) {
254 ex.printStackTrace();
258 private class execurteDialog extends Thread{
259 private final XDialog mXDialog;
261 public execurteDialog(XDialog xDialog){
262 mXDialog = xDialog;
265 @Override
266 public void run() {
267 mXDialog.endExecute();
272 } // finish class UnoControlRadioButton