summaryrefslogtreecommitdiffstats
path: root/core/modules/qemu/runvirt-plugin-qemu/src/main/java/org/openslx/runvirt/plugin/qemu/EditorRunner.java
blob: 6dbb26c5b96e7aaabdffaddbea3838111219fb7c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package org.openslx.runvirt.plugin.qemu;

import java.io.IOException;
import java.lang.ProcessBuilder.Redirect;
import java.util.concurrent.TimeUnit;

/**
 * Small wrapper around Process that will try to launch any sensible GUI
 * text editor on the given file.
 */
public class EditorRunner
{

	private static final String[] EDITORS = {
			"mousepad",
			"leafpad",
			"gedit",
			"gvim",
			"medit",
			"kate",
			"featherpad",
	};

	public static void open( String file )
	{
		for ( String editor : EDITORS ) {
			if ( open( editor, file ) )
				return;
		}
		return;
	}

	private static boolean open( String editor, String file )
	{
		ProcessBuilder pr = new ProcessBuilder( editor, file );
		pr.redirectError( Redirect.DISCARD );
		pr.redirectOutput( Redirect.DISCARD );
		try {
			final Process p = pr.start();
			p.waitFor( 1, TimeUnit.SECONDS );
			if ( !p.isAlive() )
				return false;
			p.waitFor();
		} catch ( IOException | InterruptedException e ) {
			return false;
		}
		return true;
	}

}