update dev300-m58
[ooovba.git] / qadevOOo / runner / util / SysUtils.java
blobdcc82b8c33bc297f33a655b7e7b1db546010cbc1
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: SysUtils.java,v $
10 * $Revision: 1.3 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 package util;
33 import java.io.File;
34 import java.io.FileFilter;
35 import java.util.ArrayList;
37 import com.sun.star.frame.XDesktop;
38 import com.sun.star.frame.XFrame;
39 import com.sun.star.lang.XComponent;
40 import com.sun.star.lang.XMultiServiceFactory;
41 import com.sun.star.uno.UnoRuntime;
42 import com.sun.star.datatransfer.clipboard.*;
43 import com.sun.star.datatransfer.*;
45 public class SysUtils {
47 public static String getJavaPath() {
48 String cp = (String) System.getProperty("java.class.path");
49 String jh = (String) System.getProperty("java.home");
50 String fs = (String) System.getProperty("file.separator");
51 jh = jh + fs + "bin" + fs;
52 jh = jh + "java -classpath "+cp;
53 return jh;
56 static ArrayList files = new ArrayList();
58 public static Object[] traverse( String afileDirectory ) {
60 File fileDirectory = new File(afileDirectory);
61 // Testing, if the file is a directory, and if so, it throws an exception
62 if ( !fileDirectory.isDirectory() ) {
63 throw new IllegalArgumentException(
64 "not a directory: " + fileDirectory.getName()
68 // Getting all files and directories in the current directory
69 File[] entries = fileDirectory.listFiles(
70 new FileFilter() {
71 public boolean accept( File pathname ) {
72 return true;
77 // Iterating for each file and directory
78 for ( int i = 0; i < entries.length; ++i ) {
79 // Testing, if the entry in the list is a directory
80 if ( entries[ i ].isDirectory() ) {
81 // Recursive call for the new directory
82 traverse( entries[ i ].getAbsolutePath() );
83 } else {
84 // adding file to List
85 try {
86 // Composing the URL by replacing all backslashs
87 String stringUrl = "file:///"
88 + entries[ i ].getAbsolutePath().replace( '\\', '/' );
89 files.add(stringUrl);
91 catch( Exception exception ) {
92 exception.printStackTrace();
97 return files.toArray();
100 public static XComponent getActiveComponent(XMultiServiceFactory msf) {
101 XComponent ac = null;
102 try {
103 Object desk = msf.createInstance("com.sun.star.frame.Desktop");
104 XDesktop xDesk = (XDesktop) UnoRuntime.queryInterface(XDesktop.class,desk);
105 ac = xDesk.getCurrentComponent();
106 } catch (com.sun.star.uno.Exception e) {
107 System.out.println("Couldn't get active Component");
109 return ac;
112 public static XFrame getActiveFrame(XMultiServiceFactory msf) {
113 try {
114 Object desk = msf.createInstance("com.sun.star.frame.Desktop");
115 XDesktop xDesk = (XDesktop) UnoRuntime.queryInterface(XDesktop.class,desk);
116 return xDesk.getCurrentFrame();
117 } catch (com.sun.star.uno.Exception e) {
118 System.out.println("Couldn't get active Component");
121 return null;
125 * Tries to obtain text data from cliboard if such one exists.
126 * The method iterates through all 'text/plain' supported data
127 * flavors and returns the first non-null String value.
129 * @param msf MultiserviceFactory
130 * @return First found string clipboard contents or null if no
131 * text contents were found.
132 * @throws com.sun.star.uno.Exception if system clipboard is not accessible.
134 public static String getSysClipboardText(XMultiServiceFactory msf)
135 throws com.sun.star.uno.Exception {
137 XClipboard xCB = (XClipboard) UnoRuntime.queryInterface
138 (XClipboard.class, msf.createInstance
139 ("com.sun.star.datatransfer.clipboard.SystemClipboard"));
141 XTransferable xTrans = xCB.getContents();
143 DataFlavor[] dfs = xTrans.getTransferDataFlavors();
145 for (int i = 0; i < dfs.length; i++) {
146 if (dfs[i].MimeType.startsWith("text/plain")) {
147 Object data = xTrans.getTransferData(dfs[i]);
148 if (data != null && data instanceof String) {
149 return (String) data;
154 return null;