1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2000, 2010 Oracle and/or its affiliates.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * This file is part of OpenOffice.org.
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.
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).
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.
26 ************************************************************************/
31 import java
.io
.FileFilter
;
32 import java
.util
.ArrayList
;
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
.*;
42 public class SysUtils
{
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
;
53 static ArrayList files
= new ArrayList();
55 public static Object
[] traverse( String afileDirectory
) {
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()
65 // Getting all files and directories in the current directory
66 File
[] entries
= fileDirectory
.listFiles(
68 public boolean accept( File pathname
) {
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() );
81 // adding file to List
83 // Composing the URL by replacing all backslashs
84 String stringUrl
= "file:///"
85 + entries
[ i
].getAbsolutePath().replace( '\\', '/' );
88 catch( Exception exception
) {
89 exception
.printStackTrace();
94 return files
.toArray();
97 public static XComponent
getActiveComponent(XMultiServiceFactory msf
) {
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");
109 public static XFrame
getActiveFrame(XMultiServiceFactory msf
) {
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");
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.
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.
131 public static String
getSysClipboardText(XMultiServiceFactory msf
)
132 throws com
.sun
.star
.uno
.Exception
{
134 XClipboard xCB
= (XClipboard
) UnoRuntime
.queryInterface
135 (XClipboard
.class, msf
.createInstance
136 ("com.sun.star.datatransfer.clipboard.SystemClipboard"));
138 XTransferable xTrans
= xCB
.getContents();
140 DataFlavor
[] dfs
= xTrans
.getTransferDataFlavors();
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
;