Avoid potential negative array index access to cached text.
[LibreOffice.git] / qadevOOo / tests / java / mod / _toolkit / MutableTreeNode.java
blobe08d2dd6514bdfb8bf1e5e0015f84b628f01c54d
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.tree.XMutableTreeDataModel;
21 import com.sun.star.awt.tree.XMutableTreeNode;
22 import com.sun.star.lang.XMultiServiceFactory;
23 import com.sun.star.ucb.CommandAbortedException;
24 import com.sun.star.ucb.XSimpleFileAccess;
25 import com.sun.star.uno.UnoRuntime;
26 import com.sun.star.uno.XInterface;
28 import ifc.awt.tree._XMutableTreeNode.XMutableTreeNodeCreator;
30 import java.io.PrintWriter;
32 import lib.TestCase;
33 import lib.TestEnvironment;
34 import lib.TestParameters;
35 import util.utils;
38 public class MutableTreeNode extends TestCase {
39 private static XInterface oObj = null;
40 private static XMutableTreeDataModel mXTreeDataModel;
41 private static XMultiServiceFactory mxMSF;
43 /**
44 * Creates StarOffice Writer document.
46 @Override
47 protected void initialize(TestParameters tParam, PrintWriter log) throws Exception {
48 mxMSF = tParam.getMSF();
51 @Override
52 protected TestEnvironment createTestEnvironment(TestParameters Param,
53 PrintWriter log) throws Exception {
54 XMutableTreeNode xNode;
56 mXTreeDataModel = UnoRuntime.queryInterface(XMutableTreeDataModel.class,
57 mxMSF.createInstance("com.sun.star.awt.tree.MutableTreeDataModel"));
59 xNode = mXTreeDataModel.createNode("UnoTreeControl", false);
61 String sDisplayValue = "UnoTreeControl";
62 String sExpandedGraphicURL = "private:graphicrepository/sd/res/triangle_down.png";
63 String sCollapsedGraphicURL = "private:graphicrepository/sd/res/triangle_right.png";
64 String sNodeGraphicURL = "private:graphicrepository/sw/res/nc20010.png";
66 xNode.setDisplayValue( sDisplayValue);
67 xNode.setDataValue(sDisplayValue);
68 xNode.setExpandedGraphicURL(sExpandedGraphicURL);
69 xNode.setCollapsedGraphicURL(sCollapsedGraphicURL);
70 xNode.setNodeGraphicURL(sNodeGraphicURL);
71 xNode.setHasChildrenOnDemand(true);
73 fillNode(xNode);
75 TestEnvironment tEnv = new TestEnvironment(xNode);
77 tEnv.addObjRelation("OBJNAME", "toolkit.MutableTreeDataModel");
78 log.println("ImplementationName: " + utils.getImplName(oObj));
80 tEnv.addObjRelation("XTreeNode_DisplayValue", sDisplayValue);
81 tEnv.addObjRelation("XTreeNode_ExpandedGraphicURL", sExpandedGraphicURL);
82 tEnv.addObjRelation("XTreeNode_CollapsedGraphicURL", sCollapsedGraphicURL);
83 tEnv.addObjRelation("XTreeNode_NodeGraphicURL", sNodeGraphicURL);
85 tEnv.addObjRelation("XMutableTreeNode_NodeToAppend",
86 mXTreeDataModel.createNode("XMutableTreeNode_NodeToAppend", true));
88 tEnv.addObjRelation("XMutableTreeNodeCreator", new XMutableTreeNodeCreator(){
89 public XMutableTreeNode createNode(String name){
90 return mXTreeDataModel.createNode(name, true);
92 });
94 return tEnv;
95 } // finish method getTestEnvironment
97 private void fillNode( XMutableTreeNode xNode ) throws com.sun.star.uno.Exception {
99 if( xNode.getChildCount() == 0 )
101 xNode.getDataValue();
103 String officeUserPath = utils.getOfficeUserPath(mxMSF);
104 Object fileacc = mxMSF.createInstance("com.sun.star.comp.ucb.SimpleFileAccess");
105 UnoRuntime.queryInterface(XSimpleFileAccess.class,fileacc);
108 dirlist(officeUserPath, xNode);
112 private void dirlist(String dir, XMutableTreeNode xNode){
114 Object fileacc = null;
115 try {
116 fileacc = mxMSF.createInstance("com.sun.star.comp.ucb.SimpleFileAccess");
117 } catch (com.sun.star.uno.Exception ex) {
118 ex.printStackTrace();
120 XSimpleFileAccess sfa = UnoRuntime.queryInterface(XSimpleFileAccess.class,fileacc);
121 XMutableTreeNode xChildNode = null;
122 try {
123 xChildNode = mXTreeDataModel.createNode(dir.substring(dir.lastIndexOf("/")+1, dir.length()), sfa.isFolder(dir));
124 xChildNode.setDataValue(dir);
125 sfa.isFolder(dir);
126 if (sfa.isFolder(dir)){
127 xChildNode.setExpandedGraphicURL( "private:graphicrepository/sd/res/triangle_down.png");
128 xChildNode.setCollapsedGraphicURL("private:graphicrepository/sd/res/triangle_right.png");
129 String[] children = sfa.getFolderContents(dir, true);
130 if (children != null){
131 for (int i=0; i<children.length; i++) {
132 // Get filename of file or directory
133 String filename = children[i];
134 dirlist( filename , xChildNode);
138 else{
139 xChildNode.setNodeGraphicURL( "private:graphicrepository/sw/res/nc20010.png");
141 } catch (CommandAbortedException ex) {
142 ex.printStackTrace();
143 } catch (com.sun.star.uno.Exception ex) {
144 ex.printStackTrace();
147 try {
148 xNode.appendChild( xChildNode );
149 } catch (com.sun.star.lang.IllegalArgumentException ex) {
150 ex.printStackTrace();
153 } // finish class UnoControlListBoxModel