xref: /aoo41x/main/qadevOOo/runner/util/SysUtils.java (revision cdf0e10c)
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 
28 package util;
29 
30 import java.io.File;
31 import java.io.FileFilter;
32 import java.util.ArrayList;
33 
34 import com.sun.star.frame.XDesktop;
35 import com.sun.star.frame.XFrame;
36 import com.sun.star.lang.XComponent;
37 import com.sun.star.lang.XMultiServiceFactory;
38 import com.sun.star.uno.UnoRuntime;
39 import com.sun.star.datatransfer.clipboard.*;
40 import com.sun.star.datatransfer.*;
41 
42 public class SysUtils {
43 
44     public static String getJavaPath() {
45         String cp = (String) System.getProperty("java.class.path");
46         String jh = (String) System.getProperty("java.home");
47         String fs = (String) System.getProperty("file.separator");
48         jh = jh + fs + "bin" + fs;
49         jh = jh + "java -classpath "+cp;
50         return jh;
51     }
52 
53   static ArrayList files = new ArrayList();
54 
55   public static Object[] traverse( String afileDirectory ) {
56 
57     File fileDirectory = new File(afileDirectory);
58     // Testing, if the file is a directory, and if so, it throws an exception
59     if ( !fileDirectory.isDirectory() ) {
60       throw new IllegalArgumentException(
61       "not a directory: " + fileDirectory.getName()
62       );
63     }
64 
65     // Getting all files and directories in the current directory
66     File[] entries = fileDirectory.listFiles(
67     new FileFilter() {
68       public boolean accept( File pathname ) {
69         return true;
70       }
71     }
72     );
73 
74     // Iterating for each file and directory
75     for ( int i = 0; i < entries.length; ++i ) {
76       // Testing, if the entry in the list is a directory
77       if ( entries[ i ].isDirectory() ) {
78         // Recursive call for the new directory
79         traverse( entries[ i ].getAbsolutePath() );
80       } else {
81         // adding file to List
82         try {
83           // Composing the URL by replacing all backslashs
84           String stringUrl = "file:///"
85           + entries[ i ].getAbsolutePath().replace( '\\', '/' );
86           files.add(stringUrl);
87         }
88         catch( Exception exception ) {
89           exception.printStackTrace();
90         }
91 
92       }
93     }
94     return files.toArray();
95   }
96 
97   public static XComponent getActiveComponent(XMultiServiceFactory msf) {
98     XComponent ac = null;
99     try {
100         Object desk = msf.createInstance("com.sun.star.frame.Desktop");
101         XDesktop xDesk = (XDesktop) UnoRuntime.queryInterface(XDesktop.class,desk);
102         ac = xDesk.getCurrentComponent();
103     } catch (com.sun.star.uno.Exception e) {
104         System.out.println("Couldn't get active Component");
105     }
106     return ac;
107   }
108 
109   public static XFrame getActiveFrame(XMultiServiceFactory msf) {
110     try {
111         Object desk = msf.createInstance("com.sun.star.frame.Desktop");
112         XDesktop xDesk = (XDesktop) UnoRuntime.queryInterface(XDesktop.class,desk);
113         return xDesk.getCurrentFrame();
114     } catch (com.sun.star.uno.Exception e) {
115         System.out.println("Couldn't get active Component");
116     }
117 
118     return null;
119   }
120 
121   /**
122    * Tries to obtain text data from cliboard if such one exists.
123    * The method iterates through all 'text/plain' supported data
124    * flavors and returns the first non-null String value.
125    *
126    * @param msf MultiserviceFactory
127    * @return First found string clipboard contents or null if no
128    *    text contents were found.
129    * @throws com.sun.star.uno.Exception if system clipboard is not accessible.
130    */
131   public static String getSysClipboardText(XMultiServiceFactory msf)
132         throws com.sun.star.uno.Exception {
133 
134     XClipboard xCB = (XClipboard) UnoRuntime.queryInterface
135         (XClipboard.class, msf.createInstance
136         ("com.sun.star.datatransfer.clipboard.SystemClipboard"));
137 
138     XTransferable xTrans = xCB.getContents();
139 
140     DataFlavor[] dfs = xTrans.getTransferDataFlavors();
141 
142     for (int i = 0; i < dfs.length; i++) {
143         if (dfs[i].MimeType.startsWith("text/plain")) {
144             Object data = xTrans.getTransferData(dfs[i]);
145             if (data != null && data instanceof String) {
146                 return (String) data;
147             }
148         }
149     }
150 
151     return null;
152   }
153 }
154