1 /**************************************************************
2 *
3 * Licensed to the Apache Software Foundation (ASF) under one
4 * or more contributor license agreements. See the NOTICE file
5 * distributed with this work for additional information
6 * regarding copyright ownership. The ASF licenses this file
7 * to you under the Apache License, Version 2.0 (the
8 * "License"); you may not use this file except in compliance
9 * with the License. You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing,
14 * software distributed under the License is distributed on an
15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16 * KIND, either express or implied. See the License for the
17 * specific language governing permissions and limitations
18 * under the License.
19 *
20 *************************************************************/
21
22 package util;
23
24 import java.io.File;
25 import java.io.FileFilter;
26 import java.util.ArrayList;
27
28 import com.sun.star.frame.XDesktop;
29 import com.sun.star.frame.XFrame;
30 import com.sun.star.lang.XComponent;
31 import com.sun.star.lang.XMultiServiceFactory;
32 import com.sun.star.uno.UnoRuntime;
33 import com.sun.star.datatransfer.clipboard.*;
34 import com.sun.star.datatransfer.*;
35
36 public class SysUtils {
37
getJavaPath()38 public static String getJavaPath() {
39 String cp = (String) System.getProperty("java.class.path");
40 String jh = (String) System.getProperty("java.home");
41 String fs = (String) System.getProperty("file.separator");
42 jh = jh + fs + "bin" + fs;
43 jh = jh + "java -classpath "+cp;
44 return jh;
45 }
46
47 static ArrayList files = new ArrayList();
48
traverse( String afileDirectory )49 public static Object[] traverse( String afileDirectory ) {
50
51 File fileDirectory = new File(afileDirectory);
52 // Testing, if the file is a directory, and if so, it throws an exception
53 if ( !fileDirectory.isDirectory() ) {
54 throw new IllegalArgumentException(
55 "not a directory: " + fileDirectory.getName()
56 );
57 }
58
59 // Getting all files and directories in the current directory
60 File[] entries = fileDirectory.listFiles(
61 new FileFilter() {
62 public boolean accept( File pathname ) {
63 return true;
64 }
65 }
66 );
67
68 // Iterating for each file and directory
69 for ( int i = 0; i < entries.length; ++i ) {
70 // Testing, if the entry in the list is a directory
71 if ( entries[ i ].isDirectory() ) {
72 // Recursive call for the new directory
73 traverse( entries[ i ].getAbsolutePath() );
74 } else {
75 // adding file to List
76 try {
77 // Composing the URL by replacing all backslashes
78 String stringUrl = "file:///"
79 + entries[ i ].getAbsolutePath().replace( '\\', '/' );
80 files.add(stringUrl);
81 }
82 catch( Exception exception ) {
83 exception.printStackTrace();
84 }
85
86 }
87 }
88 return files.toArray();
89 }
90
getActiveComponent(XMultiServiceFactory msf)91 public static XComponent getActiveComponent(XMultiServiceFactory msf) {
92 XComponent ac = null;
93 try {
94 Object desk = msf.createInstance("com.sun.star.frame.Desktop");
95 XDesktop xDesk = (XDesktop) UnoRuntime.queryInterface(XDesktop.class,desk);
96 ac = xDesk.getCurrentComponent();
97 } catch (com.sun.star.uno.Exception e) {
98 System.out.println("Couldn't get active Component");
99 }
100 return ac;
101 }
102
getActiveFrame(XMultiServiceFactory msf)103 public static XFrame getActiveFrame(XMultiServiceFactory msf) {
104 try {
105 Object desk = msf.createInstance("com.sun.star.frame.Desktop");
106 XDesktop xDesk = (XDesktop) UnoRuntime.queryInterface(XDesktop.class,desk);
107 return xDesk.getCurrentFrame();
108 } catch (com.sun.star.uno.Exception e) {
109 System.out.println("Couldn't get active Component");
110 }
111
112 return null;
113 }
114
115 /**
116 * Tries to obtain text data from clipboard if such one exists.
117 * The method iterates through all 'text/plain' supported data
118 * flavors and returns the first non-null String value.
119 *
120 * @param msf MultiserviceFactory
121 * @return First found string clipboard contents or null if no
122 * text contents were found.
123 * @throws com.sun.star.uno.Exception if system clipboard is not accessible.
124 */
getSysClipboardText(XMultiServiceFactory msf)125 public static String getSysClipboardText(XMultiServiceFactory msf)
126 throws com.sun.star.uno.Exception {
127
128 XClipboard xCB = (XClipboard) UnoRuntime.queryInterface
129 (XClipboard.class, msf.createInstance
130 ("com.sun.star.datatransfer.clipboard.SystemClipboard"));
131
132 XTransferable xTrans = xCB.getContents();
133
134 DataFlavor[] dfs = xTrans.getTransferDataFlavors();
135
136 for (int i = 0; i < dfs.length; i++) {
137 if (dfs[i].MimeType.startsWith("text/plain")) {
138 Object data = xTrans.getTransferData(dfs[i]);
139 if (data != null && data instanceof String) {
140 return (String) data;
141 }
142 }
143 }
144
145 return null;
146 }
147 }
148
149 /* vim: set noet sw=4 ts=4: */
150