PageRenderTime 42ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/bundles/plugins-trunk/InfoViewer/infoviewer/workaround/EnhancedJEditorPane.java

#
Java | 188 lines | 105 code | 36 blank | 47 comment | 15 complexity | aa770d1cf398ff8da0d6d5493ba71b55 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. * EnhancedJEditorPane.java
  3. * Copyright (C) 2000-2002 Dirk Moebius
  4. *
  5. * :tabSize=4:indentSize=4:noTabs=true:maxLineLen=0:
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License
  9. * as published by the Free Software Foundation; either version 2
  10. * of the License, or any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  20. */
  21. package infoviewer.workaround;
  22. import infoviewer.InfoViewer.ArrowKeyHandler;
  23. import java.awt.event.KeyEvent;
  24. import java.io.IOException;
  25. import java.io.InputStream;
  26. import java.net.HttpURLConnection;
  27. import java.net.URL;
  28. import java.net.URLConnection;
  29. import javax.accessibility.AccessibleContext;
  30. import javax.swing.JEditorPane;
  31. import org.gjt.sp.jedit.Buffer;
  32. import org.gjt.sp.jedit.MiscUtilities;
  33. import org.gjt.sp.jedit.jEdit;
  34. import org.gjt.sp.util.Log;
  35. /**
  36. * this is workaround class for JEditorPane. It avoids a bug in accessibility
  37. * support.
  38. */
  39. public class EnhancedJEditorPane extends JEditorPane
  40. {
  41. ArrowKeyHandler handler = null;
  42. public EnhancedJEditorPane()
  43. {
  44. super();
  45. }
  46. public EnhancedJEditorPane(String type, String text)
  47. {
  48. super(type, text);
  49. }
  50. public EnhancedJEditorPane(String url) throws IOException
  51. {
  52. super(url);
  53. }
  54. public EnhancedJEditorPane(URL initialPage) throws IOException
  55. {
  56. super(initialPage);
  57. }
  58. protected void ProcessKeyEvent(KeyEvent ke)
  59. {
  60. if (handler != null)
  61. handler.processKeyEvent(ke);
  62. else
  63. super.processKeyEvent(ke);
  64. }
  65. protected InputStream getStream(URL page) throws IOException
  66. {
  67. Buffer buffer = getBuffer(page);
  68. if (buffer != null)
  69. {
  70. String text = buffer.getText(0, buffer.getLength());
  71. // InputStream in = new
  72. // java.io.StringBufferInputStream(text);
  73. InputStream in = new java.io.ByteArrayInputStream(text.getBytes());
  74. Log.log(Log.DEBUG, this, "getStream(): got stream from jEdit buffer: "
  75. + buffer);
  76. // determine and set content type of buffer:
  77. // String type = getContentType(in);
  78. String type = getContentType(page);
  79. if (type != null)
  80. setContentType(type);
  81. return in;
  82. }
  83. return super.getStream(page);
  84. }
  85. /**
  86. * Check whether the url is already opened by jEdit and if true, return
  87. * its Buffer.
  88. *
  89. * @param page
  90. * the URL.
  91. * @return the jEdit buffer, of null, if there is no open buffer with
  92. * this URL.
  93. */
  94. private Buffer getBuffer(URL page)
  95. {
  96. String path = page.toString();
  97. // cut off reference part:
  98. int pos = path.indexOf('#');
  99. if (pos >= 0)
  100. path = path.substring(0, pos);
  101. // cut off query part:
  102. pos = path.indexOf('?');
  103. if (pos >= 0)
  104. path = path.substring(0, pos);
  105. // determine protocol:
  106. String protocol = "file";
  107. if (MiscUtilities.isURL(path))
  108. {
  109. protocol = MiscUtilities.getProtocolOfURL(path);
  110. if (protocol.equals("file"))
  111. path = path.substring(5);
  112. }
  113. // if file, canonize file url:
  114. if (protocol.equals("file"))
  115. path = MiscUtilities.constructPath(null, path);
  116. return jEdit.getBuffer(path);
  117. }
  118. /**
  119. * Try to determine the content type of the url.
  120. */
  121. private String getContentType(URL page) throws IOException
  122. {
  123. URLConnection conn = page.openConnection();
  124. String type = conn.getContentType();
  125. // need to disconnect http connections, because otherwise the
  126. // URL
  127. // would be cached somewhere... (dunno why)
  128. if (conn instanceof HttpURLConnection)
  129. ((HttpURLConnection) conn).disconnect();
  130. conn = null;
  131. return type;
  132. }
  133. public void setArrowKeyHandler(ArrowKeyHandler akh)
  134. {
  135. handler = akh;
  136. }
  137. public AccessibleContext getAccessibleContext()
  138. {
  139. if (this.accessibleContext == null)
  140. this.accessibleContext = new MyAccessibleJEditorPaneHTML();
  141. return this.accessibleContext;
  142. }
  143. protected class MyAccessibleJEditorPaneHTML extends JEditorPane.AccessibleJEditorPaneHTML
  144. {
  145. public MyAccessibleJEditorPaneHTML()
  146. {
  147. super();
  148. }
  149. private static final long serialVersionUID = 3215023593258981917L;
  150. }
  151. private static final long serialVersionUID = 5302023859973568642L;
  152. }