PageRenderTime 50ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/bundles/plugins-trunk/InfoViewer/infoviewer/InfoViewerPlugin.java

#
Java | 456 lines | 364 code | 36 blank | 56 comment | 62 complexity | 82feb04c3041cf82fda0bba599f78c38 MD5 | raw file
Possible License(s): BSD-3-Clause, AGPL-1.0, Apache-2.0, LGPL-2.0, LGPL-3.0, GPL-2.0, CC-BY-SA-3.0, LGPL-2.1, GPL-3.0, MPL-2.0-no-copyleft-exception, IPL-1.0
  1. /*
  2. * InfoViewerPlugin.java - an info viewer plugin for jEdit
  3. * Copyright (C) 1999-2002 Dirk Moebius
  4. * based on the original jEdit HelpViewer by Slava Pestov.
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License
  8. * as published by the Free Software Foundation; either version 2
  9. * of the License, or any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  19. *
  20. * Updated for Jedit 4.3 on 2005-09-09 by Alan Ezust
  21. *
  22. */
  23. package infoviewer;
  24. import java.awt.Component;
  25. import java.awt.Frame;
  26. import java.awt.Window;
  27. import java.lang.reflect.Constructor;
  28. import java.lang.reflect.Method;
  29. import java.net.URL;
  30. import java.util.Vector;
  31. import javax.swing.JComponent;
  32. import javax.swing.JFrame;
  33. import org.gjt.sp.jedit.Buffer;
  34. import org.gjt.sp.jedit.EditPlugin;
  35. import org.gjt.sp.jedit.GUIUtilities;
  36. import org.gjt.sp.jedit.OperatingSystem;
  37. import org.gjt.sp.jedit.OptionGroup;
  38. import org.gjt.sp.jedit.View;
  39. import org.gjt.sp.jedit.jEdit;
  40. import org.gjt.sp.jedit.gui.DockableWindowManager;
  41. import org.gjt.sp.jedit.gui.OptionsDialog;
  42. import org.gjt.sp.jedit.help.HelpViewerInterface;
  43. import org.gjt.sp.jedit.help.HelpViewer;
  44. import org.gjt.sp.jedit.io.FileVFS;
  45. import org.gjt.sp.util.Log;
  46. public class InfoViewerPlugin extends EditPlugin
  47. {
  48. // begin EditPlugin implementation
  49. public void start()
  50. {
  51. }
  52. static private boolean firstTime = true;
  53. public static void showHelp()
  54. {
  55. if (jEdit.getBooleanProperty("infoviewer.useforhelp"))
  56. {
  57. View v = jEdit.getActiveView();
  58. DockableWindowManager dwm = v.getDockableWindowManager();
  59. dwm.showDockableWindow("helpviewer");
  60. if (firstTime)
  61. {
  62. JComponent dockable = dwm.getDockable("helpviewer");
  63. dockable.setVisible(true);
  64. HelpViewerInterface viewer = (HelpViewerInterface) dockable;
  65. viewer.gotoURL("welcome.html", false, -1);
  66. firstTime = false;
  67. }
  68. dwm.showDockableWindow("helpviewer");
  69. }
  70. else new HelpViewer();
  71. }
  72. // end EditPlugin implementation
  73. /**
  74. * Open selected text with preferred browser. The selected text should
  75. * be an URL.
  76. */
  77. public static void openSelectedText(View view)
  78. {
  79. String selection = view.getTextArea().getSelectedText();
  80. if (selection == null)
  81. GUIUtilities.error(view, "infoviewer.error.noselection", null);
  82. else
  83. openURL(view, selection);
  84. }
  85. /**
  86. * Open current jEdit buffer with preferred browser.
  87. */
  88. public static void openCurrentBuffer(View view)
  89. {
  90. Buffer buffer = view.getBuffer();
  91. String url = buffer.getPath();
  92. if (buffer.getVFS() instanceof FileVFS)
  93. url = "file:" + url;
  94. if (buffer.isDirty())
  95. {
  96. int result = GUIUtilities.confirm(view, "notsaved", new String[] { buffer
  97. .getName() }, javax.swing.JOptionPane.YES_NO_CANCEL_OPTION,
  98. javax.swing.JOptionPane.WARNING_MESSAGE);
  99. if (result == javax.swing.JOptionPane.YES_OPTION)
  100. buffer.save(view, null);
  101. else if (result != javax.swing.JOptionPane.NO_OPTION)
  102. return;
  103. }
  104. openURL(view, url);
  105. }
  106. /**
  107. * Open an URL with the preferred browser.
  108. *
  109. * @param view
  110. * where to display error messages.
  111. * @param url
  112. * the URL. If null or empty, open an empty browser
  113. * window.
  114. */
  115. public static void openURL(View view, String url)
  116. {
  117. String browsertype = jEdit.getProperty("infoviewer.browsertype");
  118. if (url == null)
  119. url = "";
  120. if (url.startsWith("jeditresource:"))
  121. browsertype = "internal";
  122. Log.log(Log.DEBUG, InfoViewerPlugin.class, "(" +
  123. browsertype + "): openURL: " + url);
  124. if ("external".equals(browsertype))
  125. openURLWithOtherBrowser(view, url);
  126. else if ("class".equals(browsertype))
  127. openURLWithJavaMethod(view, url);
  128. else if ("firefox".equals(browsertype))
  129. openURLWithFirefox(view, url);
  130. else
  131. openURLWithInfoViewer(view, url);
  132. }
  133. public static void openURLWithInfoViewer(View view, String url)
  134. {
  135. DockableWindowManager mgr = view.getDockableWindowManager();
  136. mgr.showDockableWindow("infoviewer");
  137. InfoViewer iv = (InfoViewer) mgr.getDockable("infoviewer");
  138. iv.gotoURL(url, true, 0);
  139. }
  140. public static void openURLWithFirefox(View view, String url)
  141. {
  142. String[] args = new String[4];
  143. if (OperatingSystem.isWindows())
  144. {
  145. args[0]="cmd";
  146. args[1]="/C";
  147. }
  148. else
  149. {
  150. args[0] = "sh";
  151. args[1] = "-c";
  152. }
  153. args[2] = "firefox";
  154. args[3] = url;
  155. execProcess(view, args);
  156. }
  157. public static void openURLWithOtherBrowser(View view, String url)
  158. {
  159. String cmd = jEdit.getProperty("infoviewer.otherBrowser");
  160. String[] args = convertCommandString(cmd, url);
  161. execProcess(view, args);
  162. }
  163. public static void openURLWithJavaMethod(View view, String url)
  164. {
  165. String clazz = jEdit.getProperty("infoviewer.class");
  166. String method = jEdit.getProperty("infoviewer.method");
  167. Class c = null;
  168. Object obj = null;
  169. try
  170. {
  171. c = Class.forName(clazz);
  172. }
  173. catch (Throwable e)
  174. {
  175. GUIUtilities.error(view, "infoviewer.error.classnotfound",
  176. new Object[] { clazz });
  177. return;
  178. }
  179. if (method == null || (method != null && method.length() == 0))
  180. {
  181. // no method: try to find URL or String or empty
  182. // constructor
  183. Constructor constr = null;
  184. try
  185. {
  186. constr = c.getConstructor(new Class[] { URL.class });
  187. if (constr != null)
  188. obj = constr.newInstance(new Object[] { new URL(url) });
  189. }
  190. catch (Exception ex)
  191. {
  192. Log.log(Log.DEBUG, InfoViewerPlugin.class, ex);
  193. }
  194. if (obj == null)
  195. {
  196. try
  197. {
  198. constr = c.getConstructor(new Class[] { String.class });
  199. if (constr != null)
  200. obj = constr.newInstance(new Object[] { url });
  201. }
  202. catch (Exception ex)
  203. {
  204. Log.log(Log.DEBUG, InfoViewerPlugin.class, ex);
  205. }
  206. }
  207. if (obj == null)
  208. {
  209. try
  210. {
  211. constr = c.getConstructor(new Class[0]);
  212. if (constr != null)
  213. obj = constr.newInstance(new Object[0]);
  214. }
  215. catch (Exception ex)
  216. {
  217. Log.log(Log.DEBUG, InfoViewerPlugin.class, ex);
  218. }
  219. }
  220. if (obj == null)
  221. {
  222. GUIUtilities.error(view, "infoviewer.error.classnotfound",
  223. new Object[] { clazz });
  224. return;
  225. }
  226. }
  227. else
  228. {
  229. // there is a method name:
  230. Method meth = null;
  231. boolean ok = false;
  232. try
  233. {
  234. meth = c.getDeclaredMethod(method, new Class[] { URL.class });
  235. if (meth != null)
  236. {
  237. obj = meth.invoke(null, new Object[] { new URL(url) });
  238. ok = true;
  239. }
  240. }
  241. catch (Exception ex)
  242. {
  243. Log.log(Log.DEBUG, InfoViewerPlugin.class, ex);
  244. }
  245. if (!ok)
  246. {
  247. try
  248. {
  249. meth = c.getDeclaredMethod(method,
  250. new Class[] { String.class });
  251. if (meth != null)
  252. {
  253. obj = meth.invoke(null, new Object[] { url });
  254. ok = true;
  255. }
  256. }
  257. catch (Exception ex)
  258. {
  259. Log.log(Log.DEBUG, InfoViewerPlugin.class, ex);
  260. }
  261. }
  262. if (!ok)
  263. {
  264. try
  265. {
  266. meth = c.getDeclaredMethod(method, new Class[0]);
  267. if (meth != null)
  268. {
  269. obj = meth.invoke(null, new Object[0]);
  270. ok = true;
  271. }
  272. }
  273. catch (Exception ex)
  274. {
  275. Log.log(Log.DEBUG, InfoViewerPlugin.class, ex);
  276. }
  277. }
  278. if (!ok)
  279. {
  280. GUIUtilities.error(view, "infoviewer.error.methodnotfound",
  281. new Object[] { clazz, method });
  282. return;
  283. }
  284. }
  285. if (obj != null)
  286. {
  287. if (obj instanceof Window)
  288. {
  289. ((Window) obj).setVisible(true);
  290. }
  291. else if (obj instanceof JComponent)
  292. {
  293. JFrame f = new JFrame("Infoviewer JWrapper");
  294. f.getContentPane().add((JComponent) obj);
  295. f.pack();
  296. f.setVisible(true);
  297. }
  298. else if (obj instanceof Component)
  299. {
  300. Frame f = new Frame("Infoviewer Wrapper");
  301. f.add((Component) obj);
  302. f.pack();
  303. f.setVisible(true);
  304. }
  305. }
  306. }
  307. /**
  308. * converts the command string, which may contain "$u" as placeholders
  309. * for an url, into an array of strings, tokenized at the space char.
  310. * Characters in the command string may be escaped with '\\', which in
  311. * the case of space prevents tokenization.
  312. *
  313. * @param command
  314. * the command string.
  315. * @param url
  316. * the URL, as String.
  317. * @return the space separated parts of the command string, as array of
  318. * Strings.
  319. */
  320. private static String[] convertCommandString(String command, String url)
  321. {
  322. Vector args = new Vector();
  323. StringBuffer arg = new StringBuffer();
  324. boolean foundDollarU = false;
  325. boolean inQuotes = false;
  326. int end = command.length() - 1;
  327. for (int i = 0; i <= end; i++)
  328. {
  329. char c = command.charAt(i);
  330. switch (c)
  331. {
  332. case '$':
  333. if (i == end)
  334. arg.append(c);
  335. else
  336. {
  337. char c2 = command.charAt(++i);
  338. if (c2 == 'u')
  339. {
  340. arg.append(url);
  341. foundDollarU = true;
  342. }
  343. else
  344. {
  345. arg.append(c);
  346. arg.append(c2);
  347. }
  348. }
  349. break;
  350. case '"':
  351. inQuotes = !inQuotes;
  352. break;
  353. case ' ':
  354. if (inQuotes)
  355. arg.append(c);
  356. else
  357. {
  358. String newArg = arg.toString().trim();
  359. if (newArg.length() > 0)
  360. args.addElement(newArg);
  361. arg = new StringBuffer();
  362. }
  363. break;
  364. case '\\': // quote char, only for backwards
  365. // compatibility
  366. if (i == end)
  367. arg.append(c);
  368. else
  369. {
  370. char c2 = command.charAt(++i);
  371. if (c2 != '\\')
  372. arg.append(c);
  373. arg.append(c2);
  374. }
  375. break;
  376. default:
  377. arg.append(c);
  378. break;
  379. }
  380. }
  381. String newArg = arg.toString().trim();
  382. if (newArg.length() > 0)
  383. args.addElement(newArg);
  384. if (!foundDollarU && url.length() > 0)
  385. args.addElement(url);
  386. String[] result = new String[args.size()];
  387. args.copyInto(result);
  388. for (int i = 0; i < result.length; i++)
  389. Log.log(Log.DEBUG, InfoViewerPlugin.class, "args[" + i + "]=" + result[i]);
  390. return result;
  391. }
  392. private static void execProcess(View view, String[] args)
  393. {
  394. try
  395. {
  396. Runtime.getRuntime().exec(args);
  397. }
  398. catch (Exception ex)
  399. {
  400. StringBuffer buf = new StringBuffer();
  401. for (int i = 0; i < args.length; i++)
  402. {
  403. buf.append(args[i]);
  404. buf.append('\n');
  405. }
  406. GUIUtilities.error(view, "infoviewer.error.invokeBrowser", new Object[] {
  407. ex, buf.toString() });
  408. }
  409. }
  410. }