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