1*ef39d40dSAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*ef39d40dSAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*ef39d40dSAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*ef39d40dSAndrew Rist  * distributed with this work for additional information
6*ef39d40dSAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*ef39d40dSAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*ef39d40dSAndrew Rist  * "License"); you may not use this file except in compliance
9*ef39d40dSAndrew Rist  * with the License.  You may obtain a copy of the License at
10*ef39d40dSAndrew Rist  *
11*ef39d40dSAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*ef39d40dSAndrew Rist  *
13*ef39d40dSAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*ef39d40dSAndrew Rist  * software distributed under the License is distributed on an
15*ef39d40dSAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*ef39d40dSAndrew Rist  * KIND, either express or implied.  See the License for the
17*ef39d40dSAndrew Rist  * specific language governing permissions and limitations
18*ef39d40dSAndrew Rist  * under the License.
19*ef39d40dSAndrew Rist  *
20*ef39d40dSAndrew Rist  *************************************************************/
21*ef39d40dSAndrew Rist 
22*ef39d40dSAndrew Rist 
23cdf0e10cSrcweir package mod._toolkit;
24cdf0e10cSrcweir 
25cdf0e10cSrcweir import com.sun.star.awt.tree.XMutableTreeDataModel;
26cdf0e10cSrcweir import com.sun.star.awt.tree.XMutableTreeNode;
27cdf0e10cSrcweir import com.sun.star.lang.XMultiServiceFactory;
28cdf0e10cSrcweir import com.sun.star.text.XTextDocument;
29cdf0e10cSrcweir import com.sun.star.ucb.CommandAbortedException;
30cdf0e10cSrcweir import com.sun.star.ucb.XSimpleFileAccess;
31cdf0e10cSrcweir import com.sun.star.uno.UnoRuntime;
32cdf0e10cSrcweir import com.sun.star.uno.XInterface;
33cdf0e10cSrcweir import ifc.awt.tree._XMutableTreeNode.XMutableTreeNodeCreator;
34cdf0e10cSrcweir 
35cdf0e10cSrcweir import java.io.PrintWriter;
36cdf0e10cSrcweir import lib.Status;
37cdf0e10cSrcweir import lib.StatusException;
38cdf0e10cSrcweir 
39cdf0e10cSrcweir import lib.TestCase;
40cdf0e10cSrcweir import lib.TestEnvironment;
41cdf0e10cSrcweir import lib.TestParameters;
42cdf0e10cSrcweir import util.PropertyName;
43cdf0e10cSrcweir import util.utils;
44cdf0e10cSrcweir 
45cdf0e10cSrcweir 
46cdf0e10cSrcweir public class MutableTreeNode extends TestCase {
47cdf0e10cSrcweir     private static XTextDocument xTextDoc;
48cdf0e10cSrcweir     private static XInterface oObj = null;
49cdf0e10cSrcweir     private static XMutableTreeDataModel mXTreeDataModel;
50cdf0e10cSrcweir     private static XMultiServiceFactory mxMSF;
51cdf0e10cSrcweir     private static PrintWriter log;
52cdf0e10cSrcweir     private static boolean debug = false;
53cdf0e10cSrcweir 
54cdf0e10cSrcweir     /**
55cdf0e10cSrcweir      * Creates StarOffice Writer document.
56cdf0e10cSrcweir      */
initialize(TestParameters tParam, PrintWriter log)57cdf0e10cSrcweir     protected void initialize(TestParameters tParam, PrintWriter log) {
58cdf0e10cSrcweir         this.log = log;
59cdf0e10cSrcweir         debug = tParam.getBool(PropertyName.DEBUG_IS_ACTIVE);
60cdf0e10cSrcweir         mxMSF = (XMultiServiceFactory) tParam.getMSF();
61cdf0e10cSrcweir //        log.println("creating a textdocument");
62cdf0e10cSrcweir //        xTextDoc = WriterTools.createTextDoc(mxMSF);
63cdf0e10cSrcweir     }
64cdf0e10cSrcweir 
65cdf0e10cSrcweir     /**
66cdf0e10cSrcweir      * Disposes StarOffice Writer document.
67cdf0e10cSrcweir      */
cleanup(TestParameters tParam, PrintWriter log)68cdf0e10cSrcweir     protected void cleanup(TestParameters tParam, PrintWriter log) {
69cdf0e10cSrcweir         log.println("    disposing xTextDoc ");
70cdf0e10cSrcweir 
71cdf0e10cSrcweir         util.DesktopTools.closeDoc(xTextDoc);
72cdf0e10cSrcweir     }
73cdf0e10cSrcweir 
createTestEnvironment(TestParameters Param, PrintWriter log)74cdf0e10cSrcweir     protected synchronized TestEnvironment createTestEnvironment(TestParameters Param,
75cdf0e10cSrcweir         PrintWriter log) {
76cdf0e10cSrcweir         XMutableTreeNode xNode;
77cdf0e10cSrcweir 
78cdf0e10cSrcweir         try {
79cdf0e10cSrcweir             mXTreeDataModel = (XMutableTreeDataModel) UnoRuntime.queryInterface(XMutableTreeDataModel.class,
80cdf0e10cSrcweir                 mxMSF.createInstance("com.sun.star.awt.tree.MutableTreeDataModel"));
81cdf0e10cSrcweir         } catch (com.sun.star.uno.Exception ex) {
82cdf0e10cSrcweir             throw new StatusException(Status.failed("ERROR: could not create instance of" +
83cdf0e10cSrcweir                 " 'com.sun.star.awt.tree.MutableTreeDataModel'"));
84cdf0e10cSrcweir         }
85cdf0e10cSrcweir 
86cdf0e10cSrcweir         xNode = mXTreeDataModel.createNode("UnoTreeControl", false);
87cdf0e10cSrcweir 
88cdf0e10cSrcweir         String sDisplayValue = "UnoTreeControl";
89cdf0e10cSrcweir         String sExpandedGraphicURL = "private:graphicrepository/sd/res/triangle_down.png";
90cdf0e10cSrcweir         String sCollapsedGraphicURL = "private:graphicrepository/sd/res/triangle_right.png";
91cdf0e10cSrcweir         String sNodeGraphicURL = "private:graphicrepository/sw/imglst/nc20010.png";
92cdf0e10cSrcweir 
93cdf0e10cSrcweir         xNode.setDisplayValue( sDisplayValue);
94cdf0e10cSrcweir         xNode.setDataValue(sDisplayValue);
95cdf0e10cSrcweir         xNode.setExpandedGraphicURL(sExpandedGraphicURL);
96cdf0e10cSrcweir         xNode.setCollapsedGraphicURL(sCollapsedGraphicURL);
97cdf0e10cSrcweir         xNode.setNodeGraphicURL(sNodeGraphicURL);
98cdf0e10cSrcweir         xNode.setHasChildrenOnDemand(true);
99cdf0e10cSrcweir 
100cdf0e10cSrcweir         fillNode(xNode);
101cdf0e10cSrcweir 
102cdf0e10cSrcweir         TestEnvironment tEnv = new TestEnvironment(xNode);
103cdf0e10cSrcweir 
104cdf0e10cSrcweir         tEnv.addObjRelation("OBJNAME", "toolkit.MutableTreeDataModel");
105cdf0e10cSrcweir         log.println("ImplementationName: " + utils.getImplName(oObj));
106cdf0e10cSrcweir 
107cdf0e10cSrcweir         tEnv.addObjRelation("XTreeNode_DisplayValue", sDisplayValue);
108cdf0e10cSrcweir         tEnv.addObjRelation("XTreeNode_ExpandedGraphicURL", sExpandedGraphicURL);
109cdf0e10cSrcweir         tEnv.addObjRelation("XTreeNode_CollapsedGraphicURL", sCollapsedGraphicURL);
110cdf0e10cSrcweir         tEnv.addObjRelation("XTreeNode_NodeGraphicURL", sNodeGraphicURL);
111cdf0e10cSrcweir 
112cdf0e10cSrcweir         tEnv.addObjRelation("XMutableTreeNode_NodeToAppend",
113cdf0e10cSrcweir                             mXTreeDataModel.createNode("XMutableTreeNode_NodeToAppend", true));
114cdf0e10cSrcweir 
115cdf0e10cSrcweir         tEnv.addObjRelation("XMutableTreeNodeCreator", new XMutableTreeNodeCreator(){
116cdf0e10cSrcweir             public XMutableTreeNode createNode(String name){
117cdf0e10cSrcweir                 return mXTreeDataModel.createNode(name, true);
118cdf0e10cSrcweir             }
119cdf0e10cSrcweir         });
120cdf0e10cSrcweir 
121cdf0e10cSrcweir         return tEnv;
122cdf0e10cSrcweir     } // finish method getTestEnvironment
123cdf0e10cSrcweir 
fillNode( XMutableTreeNode xNode )124cdf0e10cSrcweir     private void fillNode( XMutableTreeNode xNode ){
125cdf0e10cSrcweir 
126cdf0e10cSrcweir         if( xNode.getChildCount() == 0 )
127cdf0e10cSrcweir         {
128cdf0e10cSrcweir             String sParentPath = (String) xNode.getDataValue();
129cdf0e10cSrcweir 
130cdf0e10cSrcweir             String officeUserPath = utils.getOfficeUserPath(mxMSF);
131cdf0e10cSrcweir             Object fileacc = null;
132cdf0e10cSrcweir             try {
133cdf0e10cSrcweir                 fileacc = mxMSF.createInstance("com.sun.star.comp.ucb.SimpleFileAccess");
134cdf0e10cSrcweir             } catch (com.sun.star.uno.Exception ex) {
135cdf0e10cSrcweir                 ex.printStackTrace();
136cdf0e10cSrcweir             }
137cdf0e10cSrcweir             XSimpleFileAccess sA = (XSimpleFileAccess)
138cdf0e10cSrcweir                             UnoRuntime.queryInterface(XSimpleFileAccess.class,fileacc);
139cdf0e10cSrcweir 
140cdf0e10cSrcweir 
141cdf0e10cSrcweir             dirlist(officeUserPath, xNode);
142cdf0e10cSrcweir         }
143cdf0e10cSrcweir     }
144cdf0e10cSrcweir 
dirlist(String dir, XMutableTreeNode xNode)145cdf0e10cSrcweir     private void dirlist(String dir, XMutableTreeNode xNode){
146cdf0e10cSrcweir 
147cdf0e10cSrcweir         Object fileacc = null;
148cdf0e10cSrcweir         try {
149cdf0e10cSrcweir             fileacc = mxMSF.createInstance("com.sun.star.comp.ucb.SimpleFileAccess");
150cdf0e10cSrcweir         } catch (com.sun.star.uno.Exception ex) {
151cdf0e10cSrcweir             ex.printStackTrace();
152cdf0e10cSrcweir         }
153cdf0e10cSrcweir         XSimpleFileAccess sfa = (XSimpleFileAccess)
154cdf0e10cSrcweir                         UnoRuntime.queryInterface(XSimpleFileAccess.class,fileacc);
155cdf0e10cSrcweir         XMutableTreeNode xChildNode = null;
156cdf0e10cSrcweir         try {
157cdf0e10cSrcweir             xChildNode = mXTreeDataModel.createNode(dir.substring(dir.lastIndexOf("/")+1, dir.length()), sfa.isFolder(dir));
158cdf0e10cSrcweir             xChildNode.setDataValue(dir);
159cdf0e10cSrcweir             boolean test = sfa.isFolder(dir);
160cdf0e10cSrcweir             if (sfa.isFolder(dir)){
161cdf0e10cSrcweir                 xChildNode.setExpandedGraphicURL( "private:graphicrepository/sd/res/triangle_down.png");
162cdf0e10cSrcweir                 xChildNode.setCollapsedGraphicURL("private:graphicrepository/sd/res/triangle_right.png");
163cdf0e10cSrcweir                 String[] children = sfa.getFolderContents(dir, true);
164cdf0e10cSrcweir                 if (children != null){
165cdf0e10cSrcweir                     for (int i=0; i<children.length; i++) {
166cdf0e10cSrcweir                         // Get filename of file or directory
167cdf0e10cSrcweir                         String filename = children[i];
168cdf0e10cSrcweir                         dirlist( filename , xChildNode);
169cdf0e10cSrcweir                     }
170cdf0e10cSrcweir                 }
171cdf0e10cSrcweir             }
172cdf0e10cSrcweir             else{
173cdf0e10cSrcweir                 xChildNode.setNodeGraphicURL( "private:graphicrepository/sw/imglst/nc20010.png");
174cdf0e10cSrcweir             }
175cdf0e10cSrcweir         } catch (CommandAbortedException ex) {
176cdf0e10cSrcweir             ex.printStackTrace();
177cdf0e10cSrcweir         } catch (com.sun.star.uno.Exception ex) {
178cdf0e10cSrcweir             ex.printStackTrace();
179cdf0e10cSrcweir         }
180cdf0e10cSrcweir 
181cdf0e10cSrcweir         try {
182cdf0e10cSrcweir             xNode.appendChild( xChildNode );
183cdf0e10cSrcweir         } catch (com.sun.star.lang.IllegalArgumentException ex) {
184cdf0e10cSrcweir             ex.printStackTrace();
185cdf0e10cSrcweir         }
186cdf0e10cSrcweir     }
187cdf0e10cSrcweir } // finish class UnoControlListBoxModel
188