2 * This file is part of the LibreOffice project.
4 * This Source Code Form is subject to the terms of the Mozilla Public
5 * License, v. 2.0. If a copy of the MPL was not distributed with this
6 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 * This file incorporates work covered by the following license notice:
10 * Licensed to the Apache Software Foundation (ASF) under one or more
11 * contributor license agreements. See the NOTICE file distributed
12 * with this work for additional information regarding copyright
13 * ownership. The ASF licenses this file to you under the Apache
14 * License, Version 2.0 (the "License"); you may not use this file
15 * except in compliance with the License. You may obtain a copy of
16 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
22 import java
.io
.FileFilter
;
23 import java
.util
.ArrayList
;
25 import com
.sun
.star
.frame
.XDesktop
;
26 import com
.sun
.star
.frame
.XFrame
;
27 import com
.sun
.star
.lang
.XComponent
;
28 import com
.sun
.star
.lang
.XMultiServiceFactory
;
29 import com
.sun
.star
.uno
.UnoRuntime
;
30 import com
.sun
.star
.datatransfer
.clipboard
.*;
31 import com
.sun
.star
.datatransfer
.*;
33 public class SysUtils
{
35 public static String
getJavaPath() {
36 String cp
= System
.getProperty("java.class.path");
37 String jh
= System
.getProperty("java.home");
38 String fs
= System
.getProperty("file.separator");
39 jh
= jh
+ fs
+ "bin" + fs
;
40 jh
= jh
+ "java -classpath "+cp
;
44 static ArrayList
<String
> files
= new ArrayList
<String
>();
46 public static Object
[] traverse( String afileDirectory
) {
48 File fileDirectory
= new File(afileDirectory
);
49 // Testing, if the file is a directory, and if so, it throws an exception
50 if ( !fileDirectory
.isDirectory() ) {
51 throw new IllegalArgumentException(
52 "not a directory: " + fileDirectory
.getName()
56 // Getting all files and directories in the current directory
57 File
[] entries
= fileDirectory
.listFiles(
59 public boolean accept( File pathname
) {
65 // Iterating for each file and directory
66 for ( int i
= 0; i
< entries
.length
; ++i
) {
67 // Testing, if the entry in the list is a directory
68 if ( entries
[ i
].isDirectory() ) {
69 // Recursive call for the new directory
70 traverse( entries
[ i
].getAbsolutePath() );
72 // adding file to List
74 // Composing the URL by replacing all backslashs
75 String stringUrl
= "file:///"
76 + entries
[ i
].getAbsolutePath().replace( '\\', '/' );
79 catch( Exception exception
) {
80 exception
.printStackTrace();
85 return files
.toArray();
88 public static XComponent
getActiveComponent(XMultiServiceFactory msf
) {
91 Object desk
= msf
.createInstance("com.sun.star.frame.Desktop");
92 XDesktop xDesk
= UnoRuntime
.queryInterface(XDesktop
.class,desk
);
93 ac
= xDesk
.getCurrentComponent();
94 } catch (com
.sun
.star
.uno
.Exception e
) {
95 System
.out
.println("Couldn't get active Component");
100 public static XFrame
getActiveFrame(XMultiServiceFactory msf
) {
102 Object desk
= msf
.createInstance("com.sun.star.frame.Desktop");
103 XDesktop xDesk
= UnoRuntime
.queryInterface(XDesktop
.class,desk
);
104 return xDesk
.getCurrentFrame();
105 } catch (com
.sun
.star
.uno
.Exception e
) {
106 System
.out
.println("Couldn't get active Component");
113 * Tries to obtain text data from cliboard if such one exists.
114 * The method iterates through all 'text/plain' supported data
115 * flavors and returns the first non-null String value.
117 * @param msf MultiserviceFactory
118 * @return First found string clipboard contents or null if no
119 * text contents were found.
120 * @throws com.sun.star.uno.Exception if system clipboard is not accessible.
122 public static String
getSysClipboardText(XMultiServiceFactory msf
)
123 throws com
.sun
.star
.uno
.Exception
{
125 XClipboard xCB
= UnoRuntime
.queryInterface
126 (XClipboard
.class, msf
.createInstance
127 ("com.sun.star.datatransfer.clipboard.SystemClipboard"));
129 XTransferable xTrans
= xCB
.getContents();
131 DataFlavor
[] dfs
= xTrans
.getTransferDataFlavors();
133 for (int i
= 0; i
< dfs
.length
; i
++) {
134 if (dfs
[i
].MimeType
.startsWith("text/plain")) {
135 Object data
= xTrans
.getTransferData(dfs
[i
]);
136 if (data
!= null && data
instanceof String
) {
137 return (String
) data
;