Skip to content

Commit

Permalink
Better multiplatform support
Browse files Browse the repository at this point in the history
- Support browser for Windows, Mac and Linux (Bug #1)
- Improved per os look and feel
  • Loading branch information
james2432 committed Sep 1, 2016
1 parent 16b21e5 commit 9466aac
Show file tree
Hide file tree
Showing 3 changed files with 148 additions and 2 deletions.
2 changes: 1 addition & 1 deletion OSVUploadr/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>ca.osmcanada</groupId>
<artifactId>OSVUploadr</artifactId>
<version>0.1.2-ALPHA</version>
<version>0.1.2.1-ALPHA</version>
<packaging>jar</packaging>
<dependencies>
<dependency>
Expand Down
7 changes: 6 additions & 1 deletion OSVUploadr/src/main/java/ca/osmcanada/osvuploadr/JPMain.java
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,10 @@ public class JPMain extends javax.swing.JPanel {
* Creates new form JPMain
*/
public JPMain() {
try{
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
}
catch(Exception ex){}
initComponents();
}

Expand All @@ -79,7 +83,8 @@ public String GetOSMUser() throws IOException{

final OAuth1RequestToken requestToken = service.getRequestToken();
String url = service.getAuthorizationUrl(requestToken);
java.awt.Desktop.getDesktop().browse(java.net.URI.create(url));
Helper.OpenBrowser(java.net.URI.create(url));
//java.awt.Desktop.getDesktop().browse(java.net.URI.create(url));
String pin_code = JOptionPane.showInputDialog(null, "Authorization window has opened, please paste authorization code below once authorized.\nWith out the \".\" at the end", "Authorization Required", JOptionPane.INFORMATION_MESSAGE);
final OAuth1AccessToken accessToken = service.getAccessToken(requestToken, pin_code);
final OAuthRequest request = new OAuthRequest(Verb.GET, BASE_URL + "api/0.6/user/details", service);
Expand Down
141 changes: 141 additions & 0 deletions OSVUploadr/src/main/java/ca/osmcanada/osvuploadr/Utils/Helper.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,41 @@
import com.drew.metadata.Metadata;
import com.drew.metadata.exif.ExifSubIFDDirectory;
import com.drew.metadata.exif.GpsDirectory;
import java.awt.Desktop;
import java.io.File;
import java.net.URI;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.ArrayList;
import java.util.List;
import java.io.IOException;

/**
*
* @author Jamie Nadeau
*/
public final class Helper {
public static enum EnumOS {
linux, macos, solaris, unknown, windows;

public boolean isLinux() {

return this == linux || this == solaris;
}


public boolean isMac() {

return this == macos;
}


public boolean isWindows() {

return this == windows;
}
}

public static long getFileTime(File f){
try{
Expand Down Expand Up @@ -72,4 +97,120 @@ public static ImageProperties getImageProperties(File f){
return null;
}

public static Boolean OpenBrowser(URI uri){
try{
boolean supportsBrowse = false;
if(!Desktop.isDesktopSupported()){
supportsBrowse = false;
if(!Desktop.getDesktop().isSupported(Desktop.Action.BROWSE)){
supportsBrowse = false;
}
}

if(supportsBrowse){
Desktop.getDesktop().browse(uri);
}
else{
EnumOS os = getOs();
if (os.isLinux()) {
if (runCommand("kde-open", "%s", uri.toString())) return true;
if (runCommand("gnome-open", "%s", uri.toString())) return true;
if (runCommand("xdg-open", "%s", uri.toString())) return true;
}

if (os.isMac()) {
if (runCommand("open", "%s", uri.toString())) return true;
}

if (os.isWindows()) {
if (runCommand("explorer", "%s", uri.toString())) return true;
}
}
}
catch(Exception ex){
Logger.getLogger(JPMain.class.getName()).log(Level.SEVERE, null, ex);
return false;
}
return false;
}

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

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

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

try {
Process p = Runtime.getRuntime().exec(parts);
if (p == null) return false;

try {
int retval = p.exitValue();
if (retval == 0) {
logOut("Process ended immediately.");
return false;
} else {
logOut("Process crashed.");
return false;
}
} catch (IllegalThreadStateException itse) {
logOut("Process is running.");
return true;
}
} catch (IOException e) {
Logger.getLogger(JPMain.class.getName()).log(Level.SEVERE, null, e);
return false;
}
}


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

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

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

parts.add(s.trim());
}
}

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

public static EnumOS getOs() {

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

if (s.contains("win")) {
return EnumOS.windows;
}

if (s.contains("mac")) {
return EnumOS.macos;
}

if (s.contains("solaris")) {
return EnumOS.solaris;
}

if (s.contains("sunos")) {
return EnumOS.solaris;
}

if (s.contains("linux")) {
return EnumOS.linux;
}

if (s.contains("unix")) {
return EnumOS.linux;
} else {
return EnumOS.unknown;
}
}
private static void logOut(String msg) {
System.out.println(msg);
}
}

0 comments on commit 9466aac

Please sign in to comment.