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.accessibility.XAccessibleContext;
30 import com.sun.star.awt.XControl;
31 import com.sun.star.awt.XControlContainer;
32 import com.sun.star.awt.XControlModel;
33 import com.sun.star.awt.XDialog;
34 import com.sun.star.awt.XToolkit;
35 import com.sun.star.awt.XWindow;
36 import com.sun.star.awt.tree.XMutableTreeDataModel;
37 import com.sun.star.awt.tree.XMutableTreeNode;
38 import com.sun.star.awt.tree.XTreeControl;
39 import com.sun.star.awt.tree.XTreeNode;
40 import com.sun.star.beans.XPropertySet;
41 import com.sun.star.container.XNameContainer;
42 import com.sun.star.lang.XMultiServiceFactory;
43 import com.sun.star.text.XTextDocument;
44 import com.sun.star.ucb.CommandAbortedException;
45 import com.sun.star.ucb.XSimpleFileAccess;
46 import com.sun.star.uno.AnyConverter;
47 import com.sun.star.uno.Type;
48 import com.sun.star.uno.UnoRuntime;
49 import com.sun.star.uno.XInterface;
50 
51 import java.io.PrintWriter;
52 import java.util.Comparator;
53 
54 import lib.StatusException;
55 import lib.TestCase;
56 import lib.TestEnvironment;
57 import lib.TestParameters;
58 import util.PropertyName;
59 import util.SOfficeFactory;
60 import util.UITools;
61 import util.utils;
62 
63 
64 public class UnoTreeControl extends TestCase {
65     private static XTextDocument xTextDoc;
66     private static XMutableTreeDataModel mXTreeDataModel;
67     private static XMultiServiceFactory mxMSF;
68     private static PrintWriter log;
69     private static boolean debug = false;
70 
71     protected void initialize(TestParameters Param, PrintWriter log) {
72         this.log = log;
73         debug = Param.getBool(PropertyName.DEBUG_IS_ACTIVE);
74 
75         SOfficeFactory SOF = SOfficeFactory.getFactory(
76             (XMultiServiceFactory) Param.getMSF());
77 
78         try {
79             log.println("creating a textdocument");
80             xTextDoc = SOF.createTextDoc(null);
81         } catch (com.sun.star.uno.Exception e) {
82             // Some exception occures.FAILED
83             e.printStackTrace(log);
84             throw new StatusException("Couldn't create document", e);
85         }
86     }
87 
88     protected void cleanup(TestParameters tParam, PrintWriter log) {
89 //        log.println("    disposing xTextDoc ");
90 //
91 //        util.DesktopTools.closeDoc(xTextDoc);
92     }
93 
94     protected TestEnvironment createTestEnvironment(TestParameters Param,
95         PrintWriter log) {
96         String sTreeControlName = "UnoTreeControl-Test";
97         mxMSF = (XMultiServiceFactory) Param.getMSF();
98         XInterface oObj = null;
99         XMutableTreeNode xNode = null;
100 
101         try {
102 
103             mXTreeDataModel = (XMutableTreeDataModel )
104                 UnoRuntime.queryInterface(XMutableTreeDataModel.class,
105                 mxMSF.createInstance("com.sun.star.awt.tree.MutableTreeDataModel"));
106 
107             xNode = mXTreeDataModel.createNode("UnoTreeControl", false);
108 
109             xNode.setDataValue( "UnoTreeControl");
110             xNode.setExpandedGraphicURL( "private:graphicrepository/sd/res/triangle_down.png");
111             xNode.setCollapsedGraphicURL( "private:graphicrepository/sd/res/triangle_right.png");
112 
113             fillNode(xNode);
114 
115             mXTreeDataModel.setRoot(xNode);
116 
117             XControlModel xDialogModel = (XControlModel)
118                 UnoRuntime.queryInterface(XControlModel.class,
119                 mxMSF.createInstance("com.sun.star.awt.UnoControlDialogModel"));
120 
121             XPropertySet xDialogPropertySet = (XPropertySet) UnoRuntime.queryInterface(XPropertySet.class, xDialogModel);
122             xDialogPropertySet.setPropertyValue( "PositionX",	new Integer(50) );
123             xDialogPropertySet.setPropertyValue( "PositionY",	new Integer(50) );
124             xDialogPropertySet.setPropertyValue( "Width",		new Integer(256) );
125             xDialogPropertySet.setPropertyValue( "Height",		new Integer(256) );
126             xDialogPropertySet.setPropertyValue( "Title",		"Tree Control Test");
127 
128             XMultiServiceFactory xDialogMSF = (XMultiServiceFactory)
129                 UnoRuntime.queryInterface(XMultiServiceFactory.class, xDialogModel);
130 
131             XControlModel  xTreeControlModel = (XControlModel)
132                 UnoRuntime.queryInterface(XControlModel.class,
133                 xDialogMSF.createInstance("com.sun.star.awt.tree.TreeControlModel"));
134 
135             XPropertySet XTreeControlModelSet = (XPropertySet) UnoRuntime.queryInterface(XPropertySet.class, xTreeControlModel);
136 
137             XTreeControlModelSet.setPropertyValue( "SelectionType",com.sun.star.view.SelectionType.NONE);
138             XTreeControlModelSet.setPropertyValue( "PositionX",		new Integer(3 ));
139             XTreeControlModelSet.setPropertyValue( "PositionY",		new Integer(3 ));
140             XTreeControlModelSet.setPropertyValue( "Width",			new Integer(253));
141             XTreeControlModelSet.setPropertyValue( "Height",		new Integer(253) );
142             XTreeControlModelSet.setPropertyValue( "DataModel",		mXTreeDataModel );
143             XTreeControlModelSet.setPropertyValue( "ShowsRootHandles",new Boolean (false));
144             XTreeControlModelSet.setPropertyValue( "ShowsHandles",	new Boolean (false));
145             XTreeControlModelSet.setPropertyValue( "RootDisplayed",	new Boolean (true));
146             XTreeControlModelSet.setPropertyValue( "Editable",		new Boolean (true ));
147 
148             XNameContainer xDialogModelContainer = (XNameContainer)
149             UnoRuntime.queryInterface(XNameContainer.class, xDialogModel);
150 
151             xDialogModelContainer.insertByName( sTreeControlName, xTreeControlModel);
152 
153             XControl xDialogControl = (XControl)
154             UnoRuntime.queryInterface(XControl.class,
155                 mxMSF.createInstance("com.sun.star.awt.UnoControlDialog"));
156 
157             xDialogControl.setModel( xDialogModel );
158 
159             XToolkit xToolkit = (XToolkit) UnoRuntime.queryInterface(XToolkit.class,
160                         mxMSF.createInstance("com.sun.star.awt.Toolkit" ));
161 
162         	xDialogControl.createPeer( xToolkit, null );
163 
164             // get the peers of the sub controls from the dialog peer container
165             XControlContainer xDialogContainer = (XControlContainer)
166             UnoRuntime.queryInterface(XControlContainer.class ,xDialogControl);
167 
168             XTreeControl xTreeControl = (XTreeControl)
169             UnoRuntime.queryInterface(XTreeControl.class, xDialogContainer.getControl( sTreeControlName ));
170 
171             xTreeControl.expandNode(xNode);
172             oObj = xTreeControl;
173 
174         	XDialog xDialog = (XDialog) UnoRuntime.queryInterface(XDialog.class, xDialogControl);
175 
176             execurteDialog aDialog = new execurteDialog(xDialog);
177 
178             aDialog.start();
179 
180 //            xDialog.execute();
181 
182         } catch (com.sun.star.uno.Exception ex) {
183             ex.printStackTrace();
184         }
185         TestEnvironment tEnv = new TestEnvironment(oObj);
186 
187         tEnv.addObjRelation("XTreeControl_Node", xNode);
188 
189         //com.sun.star.view.XSelectionSupplier
190         try {
191 
192             System.out.println("count of children: " + xNode.getChildCount());
193             tEnv.addObjRelation("Selections", new Object[]{xNode.getChildAt(0), xNode});
194         } catch (com.sun.star.lang.IndexOutOfBoundsException ex) {
195             log.println("ERROR: could not add object relation 'Selections' because 'xNode.getChildAt(1) failed: " +
196                         ex.toString());
197         }
198 
199         tEnv.addObjRelation("Comparer",
200                             new Comparator() {
201             public int compare(Object o1, Object o2) {
202                 XMutableTreeNode xNode1 = (XMutableTreeNode) UnoRuntime.queryInterface(
203                                         XMutableTreeNode.class, o1);
204                 XTreeNode xNode2a = null;
205                 try {
206                     xNode2a = (XTreeNode) AnyConverter.toObject(new Type(XTreeNode.class), o2);
207                 } catch (com.sun.star.lang.IllegalArgumentException ex) {
208                     ex.printStackTrace();
209                 }
210 
211                 XMutableTreeNode xNode2 = (XMutableTreeNode) UnoRuntime.queryInterface(
212                                         XMutableTreeNode.class, xNode2a);
213 
214                 if (((String) xNode1.getDataValue()).equals((String)xNode2.getDataValue())) {
215                     return 0;
216                 }
217 
218                 return -1;
219             }
220         });
221         System.out.println("ImplementationName: " + utils.getImplName(oObj));
222 
223         return tEnv;
224     } // finish method getTestEnvironment
225 
226     private void fillNode( XMutableTreeNode xNode ){
227 
228         if( xNode.getChildCount() == 0 )
229         {
230             String sParentPath = (String) xNode.getDataValue();
231 
232             String officeUserPath = utils.getOfficeUserPath(mxMSF);
233             Object fileacc = null;
234             try {
235                 fileacc = mxMSF.createInstance("com.sun.star.comp.ucb.SimpleFileAccess");
236             } catch (com.sun.star.uno.Exception ex) {
237                 ex.printStackTrace();
238             }
239             XSimpleFileAccess sA = (XSimpleFileAccess)
240                             UnoRuntime.queryInterface(XSimpleFileAccess.class,fileacc);
241 
242 
243             dirlist(officeUserPath, xNode);
244         }
245     }
246 
247     private void dirlist(String dir, XMutableTreeNode xNode){
248 
249         Object fileacc = null;
250         try {
251             fileacc = mxMSF.createInstance("com.sun.star.comp.ucb.SimpleFileAccess");
252         } catch (com.sun.star.uno.Exception ex) {
253             ex.printStackTrace();
254         }
255         XSimpleFileAccess sfa = (XSimpleFileAccess)
256                         UnoRuntime.queryInterface(XSimpleFileAccess.class,fileacc);
257         XMutableTreeNode xChildNode = null;
258         try {
259             xChildNode = mXTreeDataModel.createNode(dir.substring(dir.lastIndexOf("/")+1, dir.length()), sfa.isFolder(dir));
260             xChildNode.setDataValue(dir);
261             boolean test = sfa.isFolder(dir);
262             if (sfa.isFolder(dir)){
263                 xChildNode.setExpandedGraphicURL( "private:graphicrepository/sd/res/triangle_down.png");
264                 xChildNode.setCollapsedGraphicURL("private:graphicrepository/sd/res/triangle_right.png");
265                 String[] children = sfa.getFolderContents(dir, true);
266                 if (children != null){
267                     for (int i=0; i<children.length; i++) {
268                         // Get filename of file or directory
269                         String filename = children[i];
270                         dirlist( filename , xChildNode);
271                     }
272                 }
273             }
274             else{
275                 xChildNode.setNodeGraphicURL( "private:graphicrepository/sw/imglst/nc20010.png");
276             }
277         } catch (CommandAbortedException ex) {
278             ex.printStackTrace();
279         } catch (com.sun.star.uno.Exception ex) {
280             ex.printStackTrace();
281         }
282 
283         try {
284             xNode.appendChild( xChildNode );
285         } catch (com.sun.star.lang.IllegalArgumentException ex) {
286             ex.printStackTrace();
287         }
288     }
289 
290     private class execurteDialog extends Thread{
291         private XDialog mXDialog;
292 
293         public execurteDialog(XDialog xDialog){
294             mXDialog = xDialog;
295         }
296 
297         public void run() {
298             mXDialog.endExecute();
299         }
300     }
301 
302 
303 } // finish class UnoControlRadioButton
304