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.tree.XMutableTreeDataModel;
30 import com.sun.star.awt.tree.XMutableTreeNode;
31 import com.sun.star.lang.XMultiServiceFactory;
32 import com.sun.star.text.XTextDocument;
33 import com.sun.star.ucb.CommandAbortedException;
34 import com.sun.star.ucb.XSimpleFileAccess;
35 import com.sun.star.uno.UnoRuntime;
36 import com.sun.star.uno.XInterface;
37 import ifc.awt.tree._XMutableTreeNode.XMutableTreeNodeCreator;
38 
39 import java.io.PrintWriter;
40 import lib.Status;
41 import lib.StatusException;
42 
43 import lib.TestCase;
44 import lib.TestEnvironment;
45 import lib.TestParameters;
46 import util.PropertyName;
47 import util.utils;
48 
49 
50 public class MutableTreeNode extends TestCase {
51     private static XTextDocument xTextDoc;
52     private static XInterface oObj = null;
53     private static XMutableTreeDataModel mXTreeDataModel;
54     private static XMultiServiceFactory mxMSF;
55     private static PrintWriter log;
56     private static boolean debug = false;
57 
58     /**
59      * Creates StarOffice Writer document.
60      */
61     protected void initialize(TestParameters tParam, PrintWriter log) {
62         this.log = log;
63         debug = tParam.getBool(PropertyName.DEBUG_IS_ACTIVE);
64         mxMSF = (XMultiServiceFactory) tParam.getMSF();
65 //        log.println("creating a textdocument");
66 //        xTextDoc = WriterTools.createTextDoc(mxMSF);
67     }
68 
69     /**
70      * Disposes StarOffice Writer document.
71      */
72     protected void cleanup(TestParameters tParam, PrintWriter log) {
73         log.println("    disposing xTextDoc ");
74 
75         util.DesktopTools.closeDoc(xTextDoc);
76     }
77 
78     protected synchronized TestEnvironment createTestEnvironment(TestParameters Param,
79         PrintWriter log) {
80         XMutableTreeNode xNode;
81 
82         try {
83             mXTreeDataModel = (XMutableTreeDataModel) UnoRuntime.queryInterface(XMutableTreeDataModel.class,
84                 mxMSF.createInstance("com.sun.star.awt.tree.MutableTreeDataModel"));
85         } catch (com.sun.star.uno.Exception ex) {
86             throw new StatusException(Status.failed("ERROR: could not create instance of" +
87                 " 'com.sun.star.awt.tree.MutableTreeDataModel'"));
88         }
89 
90         xNode = mXTreeDataModel.createNode("UnoTreeControl", false);
91 
92         String sDisplayValue = "UnoTreeControl";
93         String sExpandedGraphicURL = "private:graphicrepository/sd/res/triangle_down.png";
94         String sCollapsedGraphicURL = "private:graphicrepository/sd/res/triangle_right.png";
95         String sNodeGraphicURL = "private:graphicrepository/sw/imglst/nc20010.png";
96 
97         xNode.setDisplayValue( sDisplayValue);
98         xNode.setDataValue(sDisplayValue);
99         xNode.setExpandedGraphicURL(sExpandedGraphicURL);
100         xNode.setCollapsedGraphicURL(sCollapsedGraphicURL);
101         xNode.setNodeGraphicURL(sNodeGraphicURL);
102         xNode.setHasChildrenOnDemand(true);
103 
104         fillNode(xNode);
105 
106         TestEnvironment tEnv = new TestEnvironment(xNode);
107 
108         tEnv.addObjRelation("OBJNAME", "toolkit.MutableTreeDataModel");
109         log.println("ImplementationName: " + utils.getImplName(oObj));
110 
111         tEnv.addObjRelation("XTreeNode_DisplayValue", sDisplayValue);
112         tEnv.addObjRelation("XTreeNode_ExpandedGraphicURL", sExpandedGraphicURL);
113         tEnv.addObjRelation("XTreeNode_CollapsedGraphicURL", sCollapsedGraphicURL);
114         tEnv.addObjRelation("XTreeNode_NodeGraphicURL", sNodeGraphicURL);
115 
116         tEnv.addObjRelation("XMutableTreeNode_NodeToAppend",
117                             mXTreeDataModel.createNode("XMutableTreeNode_NodeToAppend", true));
118 
119         tEnv.addObjRelation("XMutableTreeNodeCreator", new XMutableTreeNodeCreator(){
120             public XMutableTreeNode createNode(String name){
121                 return mXTreeDataModel.createNode(name, true);
122             }
123         });
124 
125         return tEnv;
126     } // finish method getTestEnvironment
127 
128     private void fillNode( XMutableTreeNode xNode ){
129 
130         if( xNode.getChildCount() == 0 )
131         {
132             String sParentPath = (String) xNode.getDataValue();
133 
134             String officeUserPath = utils.getOfficeUserPath(mxMSF);
135             Object fileacc = null;
136             try {
137                 fileacc = mxMSF.createInstance("com.sun.star.comp.ucb.SimpleFileAccess");
138             } catch (com.sun.star.uno.Exception ex) {
139                 ex.printStackTrace();
140             }
141             XSimpleFileAccess sA = (XSimpleFileAccess)
142                             UnoRuntime.queryInterface(XSimpleFileAccess.class,fileacc);
143 
144 
145             dirlist(officeUserPath, xNode);
146         }
147     }
148 
149     private void dirlist(String dir, XMutableTreeNode xNode){
150 
151         Object fileacc = null;
152         try {
153             fileacc = mxMSF.createInstance("com.sun.star.comp.ucb.SimpleFileAccess");
154         } catch (com.sun.star.uno.Exception ex) {
155             ex.printStackTrace();
156         }
157         XSimpleFileAccess sfa = (XSimpleFileAccess)
158                         UnoRuntime.queryInterface(XSimpleFileAccess.class,fileacc);
159         XMutableTreeNode xChildNode = null;
160         try {
161             xChildNode = mXTreeDataModel.createNode(dir.substring(dir.lastIndexOf("/")+1, dir.length()), sfa.isFolder(dir));
162             xChildNode.setDataValue(dir);
163             boolean test = sfa.isFolder(dir);
164             if (sfa.isFolder(dir)){
165                 xChildNode.setExpandedGraphicURL( "private:graphicrepository/sd/res/triangle_down.png");
166                 xChildNode.setCollapsedGraphicURL("private:graphicrepository/sd/res/triangle_right.png");
167                 String[] children = sfa.getFolderContents(dir, true);
168                 if (children != null){
169                     for (int i=0; i<children.length; i++) {
170                         // Get filename of file or directory
171                         String filename = children[i];
172                         dirlist( filename , xChildNode);
173                     }
174                 }
175             }
176             else{
177                 xChildNode.setNodeGraphicURL( "private:graphicrepository/sw/imglst/nc20010.png");
178             }
179         } catch (CommandAbortedException ex) {
180             ex.printStackTrace();
181         } catch (com.sun.star.uno.Exception ex) {
182             ex.printStackTrace();
183         }
184 
185         try {
186             xNode.appendChild( xChildNode );
187         } catch (com.sun.star.lang.IllegalArgumentException ex) {
188             ex.printStackTrace();
189         }
190     }
191 } // finish class UnoControlListBoxModel
192