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