Revision 42959

View differences:

trunk/org.gvsig.desktop/org.gvsig.desktop.framework/org.gvsig.andami/src/main/java/org/gvsig/andami/PluginsManager.java
24 24
package org.gvsig.andami;
25 25

  
26 26
import java.io.File;
27
import java.net.URI;
27 28
import java.util.Iterator;
28 29
import java.util.List;
29 30
import static org.gvsig.andami.Launcher.appName;
......
39 40
public interface PluginsManager {
40 41

  
41 42
    public String getApplicationName();
42
    
43

  
43 44
    /**
44 45
     * Return the associated pluginServices to the extension class passed as
45 46
     * parameter.
......
256 257

  
257 258
    /**
258 259
     * Gets the temporary folder of the plugins framework.
259
     * 
260
     * @return the temp folder 
260
     *
261
     * @return the temp folder
261 262
     */
262 263
    public File getTempFolder();
263
    
264

  
264 265
    /**
265 266
     * Get the file name in the temporary folder of the plugins framework.
266 267
     * @param name
267
     * @return 
268
     * @return
268 269
     */
269 270
    public File getTempFile(String name);
270
    
271

  
271 272
    /**
272 273
     * Create a unique file name in the temporary folder of the plugins framework.
273
     * 
274
     * @param name base name used for create the unique file name 
274
     *
275
     * @param name base name used for create the unique file name
275 276
     * @param sufix to add the the unique file name
276
     * @return 
277
     * @return
277 278
     */
278 279
    public File getTempFile(String name, String sufix);
279
    
280

  
281
    /**
282
     * Browses to a given URI
283
     * @param uri
284
     * @return
285
     */
286
    public boolean desktopBrowse(URI uri);
287

  
288
    /**
289
     * Open a given file
290
     * @param file
291
     * @return
292
     */
293
    public boolean desktopOpen(File file);
294

  
295

  
280 296
}
trunk/org.gvsig.desktop/org.gvsig.desktop.framework/org.gvsig.andami/src/main/java/org/gvsig/andami/impl/DesktopApi.java
1
package org.gvsig.andami.impl;
2

  
3
/*
4
 * Based on portions of code of MightyPork, http://www.ondrovo.com/
5
 * extracted from :
6
 * http://stackoverflow.com/questions/18004150/desktop-api-is-not-supported-on-the-current-platform
7
 */
8

  
9
import java.awt.Desktop;
10
import java.io.File;
11
import java.io.IOException;
12
import java.net.URI;
13
import java.util.ArrayList;
14
import java.util.List;
15
import org.slf4j.Logger;
16
import org.slf4j.LoggerFactory;
17

  
18
public class DesktopApi {
19

  
20
    private static Logger logger = LoggerFactory.getLogger(DesktopApi.class);
21

  
22
    public static boolean browse(URI uri) {
23

  
24
        if ( browseDESKTOP(uri) ) {
25
            return true;
26
        }
27

  
28
        if ( openSystemSpecific(uri.toString()) ) {
29
            return true;
30
        }
31

  
32
        return false;
33
    }
34

  
35
    public static boolean open(File file) {
36

  
37
        if ( openDESKTOP(file) ) {
38
            return true;
39
        }
40

  
41
        if ( openSystemSpecific(file.getPath()) ) {
42
            return true;
43
        }
44

  
45
        return false;
46
    }
47

  
48
    public static boolean edit(File file) {
49

  
50
        if ( editDESKTOP(file) ) {
51
            return true;
52
        }
53

  
54
        if ( openSystemSpecific(file.getPath()) ) {
55
            return true;
56
        }
57

  
58
        return false;
59
    }
60

  
61
    private static boolean openSystemSpecific(String what) {
62

  
63
        EnumOS os = getOs();
64

  
65
        if (os.isLinux()) {
66
//            if (runCommand("gnome-open", "%s", what)) {
67
//                return true;
68
//            }
69
//            if (runCommand("kde-open", "%s", what)) {
70
//                return true;
71
//            }
72
            if (runCommand("xdg-open", "%s", what)) {
73
                return true;
74
            }
75
        }
76

  
77
        if ( os.isMac() ) {
78
            if ( runCommand("open", "%s", what) ) {
79
                return true;
80
            }
81
        }
82

  
83
        if ( os.isWindows() ) {
84
            if ( runCommand("explorer", "%s", what) ) {
85
                return true;
86
            }
87
        }
88

  
89
        return false;
90
    }
91

  
92
    private static boolean browseDESKTOP(URI uri) {
93

  
94
        logOut("Trying to use Desktop.getDesktop().browse() with " + uri.toString());
95
        try {
96
            if ( !Desktop.isDesktopSupported() ) {
97
                logErr("Platform is not supported.");
98
                return false;
99
            }
100

  
101
            if ( !Desktop.getDesktop().isSupported(Desktop.Action.BROWSE) ) {
102
                logErr("BROWSE is not supported.");
103
                return false;
104
            }
105

  
106
            Desktop.getDesktop().browse(uri);
107

  
108
            return true;
109
        } catch (Throwable t) {
110
            logErr("Error using desktop browse.", t);
111
            return false;
112
        }
113
    }
114

  
115
    private static boolean openDESKTOP(File file) {
116

  
117
        logOut("Trying to use Desktop.getDesktop().open() with " + file.toString());
118
        try {
119
            if ( !Desktop.isDesktopSupported() ) {
120
                logErr("Platform is not supported.");
121
                return false;
122
            }
123

  
124
            if ( !Desktop.getDesktop().isSupported(Desktop.Action.OPEN) ) {
125
                logErr("OPEN is not supported.");
126
                return false;
127
            }
128

  
129
            Desktop.getDesktop().open(file);
130

  
131
            return true;
132
        } catch (Throwable t) {
133
            logErr("Error using desktop open.", t);
134
            return false;
135
        }
136
    }
137

  
138
    private static boolean editDESKTOP(File file) {
139

  
140
        logOut("Trying to use Desktop.getDesktop().edit() with " + file);
141
        try {
142
            if ( !Desktop.isDesktopSupported() ) {
143
                logErr("Platform is not supported.");
144
                return false;
145
            }
146

  
147
            if ( !Desktop.getDesktop().isSupported(Desktop.Action.EDIT) ) {
148
                logErr("EDIT is not supported.");
149
                return false;
150
            }
151

  
152
            Desktop.getDesktop().edit(file);
153

  
154
            return true;
155
        } catch (Throwable t) {
156
            logErr("Error using desktop edit.", t);
157
            return false;
158
        }
159
    }
160

  
161
    private static boolean runCommand(String command, String args, String file) {
162

  
163
        logOut("Trying to exec:\n   cmd = " + command + "\n   args = " + args + "\n   %s = " + file);
164

  
165
        String[] parts = prepareCommand(command, args, file);
166

  
167
        try {
168
            Process p = Runtime.getRuntime().exec(parts);
169
            if ( p == null ) {
170
                return false;
171
            }
172

  
173
            try {
174
                int retval = p.exitValue();
175
                if ( retval == 0 ) {
176
                    logErr("Process ended immediately.");
177
                    return false;
178
                } else {
179
                    logErr("Process crashed.");
180
                    return false;
181
                }
182
            } catch (IllegalThreadStateException itse) {
183
                logErr("Process is running.");
184
                return true;
185
            }
186
        } catch (IOException e) {
187
            logErr("Error running command.", e);
188
            return false;
189
        }
190
    }
191

  
192
    private static String[] prepareCommand(String command, String args, String file) {
193

  
194
        List<String> parts = new ArrayList<String>();
195
        parts.add(command);
196

  
197
        if ( args != null ) {
198
            for ( String s : args.split(" ") ) {
199
                s = String.format(s, file); // put in the filename thing
200

  
201
                parts.add(s.trim());
202
            }
203
        }
204

  
205
        return parts.toArray(new String[parts.size()]);
206
    }
207

  
208
    private static void logErr(String msg, Throwable t) {
209
        logger.warn(msg,t);
210
    }
211

  
212
    private static void logErr(String msg) {
213
        logger.warn(msg);
214
    }
215

  
216
    private static void logOut(String msg) {
217
        logger.info(msg);
218
    }
219

  
220
    public static enum EnumOS {
221

  
222
        linux, macos, solaris, unknown, windows;
223

  
224
        public boolean isLinux() {
225

  
226
            return this == linux || this == solaris;
227
        }
228

  
229
        public boolean isMac() {
230

  
231
            return this == macos;
232
        }
233

  
234
        public boolean isWindows() {
235

  
236
            return this == windows;
237
        }
238
    }
239

  
240
    public static EnumOS getOs() {
241

  
242
        String s = System.getProperty("os.name").toLowerCase();
243

  
244
        if ( s.contains("win") ) {
245
            return EnumOS.windows;
246
        }
247

  
248
        if ( s.contains("mac") ) {
249
            return EnumOS.macos;
250
        }
251

  
252
        if ( s.contains("solaris") ) {
253
            return EnumOS.solaris;
254
        }
255

  
256
        if ( s.contains("sunos") ) {
257
            return EnumOS.solaris;
258
        }
259

  
260
        if ( s.contains("linux") ) {
261
            return EnumOS.linux;
262
        }
263

  
264
        if ( s.contains("unix") ) {
265
            return EnumOS.linux;
266
        } else {
267
            return EnumOS.unknown;
268
        }
269
    }
270
}
trunk/org.gvsig.desktop/org.gvsig.desktop.framework/org.gvsig.andami/src/main/java/org/gvsig/andami/impl/DefaultPluginsManager.java
26 26
import java.io.File;
27 27
import java.io.IOException;
28 28
import java.lang.reflect.InvocationTargetException;
29
import java.net.URI;
29 30
import java.util.ArrayList;
30 31
import java.util.Arrays;
31 32
import java.util.Collections;
......
410 411
        String fname = String.format("%s-%x%x%s", name,t,sufix);
411 412
        return new File(tempFolder,fname);
412 413
    }
413
    
414
    
414

  
415
    @Override
416
    public boolean desktopBrowse(URI uri){
417
        return DesktopApi.browse(uri);
418
    }
419

  
420
    @Override
421
    public boolean desktopOpen(File file){
422
        return desktopOpen(file);
423
    }
415 424
}

Also available in: Unified diff