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