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