Пример #1
0
  public static boolean openFolderAvailable() {
    if (Base.isWindows() || Base.isMacOS()) return true;

    if (Base.isLinux()) {
      // Assume that this is set to something valid
      if (preferences.get("launcher.linux", null) != null) {
        return true;
      }

      // Attempt to use gnome-open
      try {
        Process p = Runtime.getRuntime().exec(new String[] {"gnome-open"});
        p.waitFor();
        // Not installed will throw an IOException (JDK 1.4.2, Ubuntu
        // 7.04)
        preferences.put("launcher.linux", "gnome-open");
        return true;
      } catch (Exception e) {
      }

      // Attempt with kde-open
      try {
        Process p = Runtime.getRuntime().exec(new String[] {"kde-open"});
        p.waitFor();
        preferences.put("launcher.linux", "kde-open");
        return true;
      } catch (Exception e) {
      }
    }
    return false;
  }
Пример #2
0
  /**
   * Implements the other cross-platform headache of opening a folder in the machine's native file
   * browser.
   */
  public static void openFolder(File file) {
    try {
      String folder = file.getAbsolutePath();

      if (Base.isWindows()) {
        // doesn't work
        // Runtime.getRuntime().exec("cmd /c \"" + folder + "\"");

        // works fine on winxp, prolly win2k as well
        Runtime.getRuntime().exec("explorer \"" + folder + "\"");

        // not tested
        // Runtime.getRuntime().exec("start explorer \"" + folder +
        // "\"");

      } else if (Base.isMacOS()) {
        openURL(folder); // handles char replacement, etc

      } else if (Base.isLinux()) {
        String launcher = preferences.get("launcher.linux", null);
        if (launcher != null) {
          Runtime.getRuntime().exec(new String[] {launcher, folder});
        }
      }
    } catch (IOException e) {
      e.printStackTrace();
    }
  }
Пример #3
0
  /**
   * Implements the cross-platform headache of opening URLs TODO This code should be replaced by
   * PApplet.link(), however that's not a static method (because it requires an AppletContext when
   * used as an applet), so it's mildly trickier than just removing this method.
   */
  public static void openURL(String url) {
    // System.out.println("opening url " + url);
    try {
      if (Base.isWindows()) {
        // this is not guaranteed to work, because who knows if the
        // path will always be c:\progra~1 et al. also if the user has
        // a different browser set as their default (which would
        // include me) it'd be annoying to be dropped into ie.
        // Runtime.getRuntime().exec("c:\\progra~1\\intern~1\\iexplore "
        // + currentDir

        // the following uses a shell execute to launch the .html file
        // note that under cygwin, the .html files have to be chmodded
        // +x
        // after they're unpacked from the zip file. i don't know why,
        // and don't understand what this does in terms of windows
        // permissions. without the chmod, the command prompt says
        // "Access is denied" in both cygwin and the "dos" prompt.
        // Runtime.getRuntime().exec("cmd /c " + currentDir +
        // "\\reference\\" +
        // referenceFile + ".html");
        if (url.startsWith("http://")) {
          // open dos prompt, give it 'start' command, which will
          // open the url properly. start by itself won't work since
          // it appears to need cmd
          Runtime.getRuntime().exec("cmd /c start " + url);
        } else {
          // just launching the .html file via the shell works
          // but make sure to chmod +x the .html files first
          // also place quotes around it in case there's a space
          // in the user.dir part of the url
          Runtime.getRuntime().exec("cmd /c \"" + url + "\"");
        }

      } else if (Base.isMacOS()) {
        // com.apple.eio.FileManager.openURL(url);

        if (!url.startsWith("http://")) {
          // prepend file:// on this guy since it's a file
          url = "file://" + url;

          // replace spaces with %20 for the file url
          // otherwise the mac doesn't like to open it
          // can't just use URLEncoder, since that makes slashes into
          // %2F characters, which is no good. some might say
          // "useless"
          if (url.indexOf(' ') != -1) {
            StringBuffer sb = new StringBuffer();
            char c[] = url.toCharArray();
            for (int i = 0; i < c.length; i++) {
              if (c[i] == ' ') {
                sb.append("%20");
              } else {
                sb.append(c[i]);
              }
            }
            url = sb.toString();
          }
        }
        com.apple.mrj.MRJFileUtils.openURL(url);

      } else if (Base.isLinux()) {
        String launcher = preferences.get("launcher.linux", "gnome-open");
        if (launcher != null) {
          Runtime.getRuntime().exec(new String[] {launcher, url});
        }
      } else {
        String launcher = preferences.get("launcher", null);
        if (launcher != null) {
          Runtime.getRuntime().exec(new String[] {launcher, url});
        } else {
          Base.logger.warning("Unspecified platform, no launcher available.");
        }
      }

    } catch (IOException e) {
      Base.showWarning("Could not open URL", "An error occurred while trying to open\n" + url, e);
    }
  }