PageRenderTime 62ms CodeModel.GetById 33ms RepoModel.GetById 0ms app.codeStats 0ms

/actors/src/org/geon/BrowserDisplay.java

https://github.com/Elblonko/kepler
Java | 319 lines | 161 code | 31 blank | 127 comment | 24 complexity | 1810e2a37e82af1105dfd47a82fa19bc MD5 | raw file
Possible License(s): GPL-2.0
  1. /*
  2. * Copyright (c) 2002-2010 The Regents of the University of California.
  3. * All rights reserved.
  4. *
  5. * '$Author: welker $'
  6. * '$Date: 2010-05-05 22:21:26 -0700 (Wed, 05 May 2010) $'
  7. * '$Revision: 24234 $'
  8. *
  9. * Permission is hereby granted, without written agreement and without
  10. * license or royalty fees, to use, copy, modify, and distribute this
  11. * software and its documentation for any purpose, provided that the above
  12. * copyright notice and the following two paragraphs appear in all copies
  13. * of this software.
  14. *
  15. * IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY
  16. * FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
  17. * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF
  18. * THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF
  19. * SUCH DAMAGE.
  20. *
  21. * THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
  22. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  23. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE
  24. * PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF
  25. * CALIFORNIA HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES,
  26. * ENHANCEMENTS, OR MODIFICATIONS.
  27. *
  28. */
  29. package org.geon;
  30. import java.io.File;
  31. import java.io.IOException;
  32. import java.lang.reflect.Method;
  33. import java.net.URI;
  34. import ptolemy.actor.TypedAtomicActor;
  35. import ptolemy.actor.TypedIOPort;
  36. import ptolemy.data.BooleanToken;
  37. import ptolemy.data.StringToken;
  38. import ptolemy.data.type.BaseType;
  39. import ptolemy.gui.MessageHandler;
  40. import ptolemy.kernel.CompositeEntity;
  41. import ptolemy.kernel.attributes.URIAttribute;
  42. import ptolemy.kernel.util.IllegalActionException;
  43. import ptolemy.kernel.util.NameDuplicationException;
  44. //////////////////////////////////////////////////////////////////////////
  45. //// BrowserDisplay
  46. /**
  47. * This actor displays a file or a URL using the BrowserLauncher class. The URL
  48. * to display is specified through the inputURL port.
  49. *
  50. * @UserLevelDocumentation This actor displays a file or a URL specified by
  51. * inputURL using the the appropriate application.
  52. * @author Efrat Jaeger
  53. * @version $Id: BrowserDisplay.java 24234 2010-05-06 05:21:26Z welker $
  54. * @since Ptolemy II 3.0.2
  55. */
  56. public class BrowserDisplay extends TypedAtomicActor {
  57. /**
  58. * Construct an actor with the given container and name.
  59. *
  60. * @param container
  61. * The container.
  62. * @param name
  63. * The name of this actor.
  64. * @exception IllegalActionException
  65. * If the actor cannot be contained by the proposed
  66. * container.
  67. * @exception NameDuplicationException
  68. * If the container already has an actor with this name.
  69. */
  70. public BrowserDisplay(CompositeEntity container, String name)
  71. throws IllegalActionException, NameDuplicationException {
  72. super(container, name);
  73. // fileOrURL = new FileParameter(this, "fileOrURL");
  74. inputURL = new TypedIOPort(this, "inputURL", true, false);
  75. inputURL.setTypeEquals(BaseType.STRING);
  76. trigger = new TypedIOPort(this, "trigger", true, false);
  77. trigger.setTypeEquals(BaseType.BOOLEAN);
  78. }
  79. // /////////////////////////////////////////////////////////////////
  80. // // ports and parameters ////
  81. // public FileParameter fileOrURL;
  82. /**
  83. * The file name or URL to be displayed.
  84. *
  85. * @UserLevelDocumentation The input file or URL to be displayed.
  86. */
  87. public TypedIOPort inputURL;
  88. /**
  89. * A trigger to invoke the actor.
  90. *
  91. * @UserLevelDocumentation This port is used to trigger the actor.
  92. */
  93. public TypedIOPort trigger;
  94. // /////////////////////////////////////////////////////////////////
  95. // // public methods ////
  96. /**
  97. * Display the input file or URL if it is other than null.
  98. *
  99. * @exception IllegalActionException
  100. * If there's no director.
  101. */
  102. public void fire() throws IllegalActionException {
  103. boolean val = false;
  104. for (int i = 0; i < trigger.getWidth(); i++) {
  105. if (trigger.hasToken(i)) {
  106. val = ((BooleanToken) trigger.get(i)).booleanValue();
  107. if (val == false) {
  108. System.out.println("value is false!!!");
  109. inputURL.get(0);
  110. return;
  111. } else
  112. System.out.println("TRUE!!!");
  113. }
  114. }
  115. try {
  116. System.out.println("Reading from the browser - val = " + val);
  117. StringToken fileToken = null;
  118. try {
  119. // Check whether a token has been consumed.
  120. fileToken = (StringToken) inputURL.get(0);
  121. } catch (Exception ex) {
  122. }
  123. if (fileToken != null) {
  124. strFileOrURL = fileToken.stringValue();
  125. // the following line replaces all '\\' (double backslashes)
  126. // with single forward slashes
  127. // this is sometimes needed on Windows platforms
  128. // Dan Higgins April 2006
  129. strFileOrURL = strFileOrURL.replaceAll("\\\\\\\\", "/");
  130. int lineEndInd = strFileOrURL.indexOf("\n");
  131. if (lineEndInd != -1) { // Read until the "\n".
  132. strFileOrURL = strFileOrURL.substring(0, lineEndInd);
  133. }
  134. if (!strFileOrURL.trim().toLowerCase().startsWith("http")) {
  135. File toDisplay = new File(strFileOrURL);
  136. if (!toDisplay.isAbsolute()) {
  137. // Try to resolve the base directory.
  138. URI modelURI = URIAttribute.getModelURI(this);
  139. if (modelURI != null) {
  140. URI newURI = modelURI.resolve(strFileOrURL);
  141. toDisplay = new File(newURI);
  142. strFileOrURL = toDisplay.getAbsolutePath();
  143. }
  144. }
  145. String canonicalPath = toDisplay.getCanonicalPath(); // Dan
  146. // Higgins
  147. // strFileOrURL = "file:///" + strFileOrURL;
  148. strFileOrURL = "file:///" + canonicalPath; // Dan Higgins
  149. }
  150. /*
  151. * else { // The file to display is a file attribute. URL url =
  152. * fileOrURL.asURL(); strFileOrURL = url.toString(); String
  153. * decoded = URLDecoder.decode(strFileOrURL); if
  154. * (decoded.toLowerCase().startsWith("file")) { url = new
  155. * URL(decoded); if (decoded.charAt(6) != '/') { // should be
  156. * "file://". strFileOrURL = decoded.substring(0, 6) +
  157. * decoded.substring(5); //url = new URL(file); } } }
  158. */
  159. BareBonesBrowserLaunch bl = new BareBonesBrowserLaunch(); // Dan
  160. // Higgins
  161. bl.openURL(strFileOrURL); // Dan Higgins
  162. // LaunchBrowser lb = new LaunchBrowser(); // Dan Higgins
  163. // lb.displayFileOrURL(strFileOrURL); // Dan Higgins
  164. } else {
  165. // There are no more tokens to consume.
  166. reFire = false;
  167. }
  168. } catch (Exception e) {
  169. MessageHandler.error("Error opening browser", e);
  170. }
  171. }
  172. /**
  173. * If there are no more URLs to display (the input token was null) returns
  174. * false.
  175. *
  176. * @exception IllegalActionException
  177. * If thrown by the super class.
  178. */
  179. public boolean postfire() throws IllegalActionException {
  180. return reFire;
  181. }
  182. /**
  183. * set reFire to true.
  184. */
  185. public void wrapup() {
  186. reFire = true;
  187. }
  188. // /////////////////////////////////////////////////////////////////
  189. // // inner classes ////
  190. /**
  191. * Launch the default browser from within java.
  192. */
  193. public class LaunchBrowser {
  194. public void displayFileOrURL(String strFileOrURL) {
  195. String cmd = null;
  196. try {
  197. if (isWindows()) {
  198. String cmdStr = "C:/Program Files/Internet Explorer/IEXPLORE.exe ";
  199. cmdStr += strFileOrURL;
  200. // cmd = _winAct + " " + _winFlag + " " + strFileOrURL;
  201. Process p = Runtime.getRuntime().exec(cmdStr);
  202. /*
  203. * try { p.waitFor(); } catch (Exception ex) {
  204. * MessageHandler.error("Error in waitFor", ex); }
  205. */
  206. } else {
  207. cmd = _unixAct + " " + _unixFlag + "(" + strFileOrURL + ")";
  208. Process p = Runtime.getRuntime().exec(cmd);
  209. try {
  210. int exitCode = p.waitFor();
  211. if (exitCode != 0) {
  212. cmd = _unixAct + " " + strFileOrURL;
  213. Runtime.getRuntime().exec(cmd);
  214. }
  215. } catch (InterruptedException ex) {
  216. MessageHandler.error("Error opening browser, cmd='"
  217. + cmd, ex);
  218. }
  219. }
  220. } catch (IOException ex) {
  221. MessageHandler.error("Error invoking browser, cmd=" + cmd, ex);
  222. }
  223. }
  224. public boolean isWindows() {
  225. String osName = System.getProperty("os.name");
  226. if (osName != null)
  227. return osName.startsWith(_winOS);
  228. else
  229. return false;
  230. }
  231. private static final String _winOS = "Windows";
  232. private static final String _winAct = "rundll32";
  233. private static final String _winFlag = "url.dll,FileProtocolHandler";
  234. private static final String _unixAct = "netscape";
  235. private static final String _unixFlag = "-remote openURL";
  236. }
  237. // added by Dan Higgins
  238. // ///////////////////////////////////////////////////////
  239. // Bare Bones Browser Launch //
  240. // Version 1.5 //
  241. // December 10, 2005 //
  242. // Supports: Mac OS X, GNU/Linux, Unix, Windows XP //
  243. // Example Usage: //
  244. // String url = "http://www.centerkey.com/"; //
  245. // BareBonesBrowserLaunch.openURL(url); //
  246. // Public Domain Software -- Free to Use as You Like //
  247. // ///////////////////////////////////////////////////////
  248. public class BareBonesBrowserLaunch {
  249. public void openURL(String url) {
  250. String osName = System.getProperty("os.name");
  251. try {
  252. if (osName.startsWith("Mac OS")) {
  253. Class fileMgr = Class.forName("com.apple.eio.FileManager");
  254. Method openURL = fileMgr.getDeclaredMethod("openURL",
  255. new Class[] { String.class });
  256. openURL.invoke(null, new Object[] { url });
  257. } else if (osName.startsWith("Windows"))
  258. Runtime.getRuntime().exec(
  259. "rundll32 url.dll,FileProtocolHandler " + url);
  260. else { // assume Unix or Linux
  261. String[] browsers = { "firefox", "opera", "konqueror",
  262. "epiphany", "mozilla", "netscape" };
  263. String browser = null;
  264. for (int count = 0; count < browsers.length
  265. && browser == null; count++)
  266. if (Runtime.getRuntime().exec(
  267. new String[] { "which", browsers[count] })
  268. .waitFor() == 0)
  269. browser = browsers[count];
  270. if (browser == null)
  271. throw new Exception("Could not find web browser");
  272. else
  273. Runtime.getRuntime()
  274. .exec(new String[] { browser, url });
  275. }
  276. } catch (Exception e) {
  277. System.out.println("error in BrowserLauncher - "
  278. + e.getLocalizedMessage());
  279. }
  280. }
  281. }
  282. // end added by Dan Higgins
  283. /** Represent the URL to be display. */
  284. private String strFileOrURL;
  285. /** Indicator that there are more tokens to consume. */
  286. private boolean reFire = true;
  287. }