PageRenderTime 97ms CodeModel.GetById 83ms app.highlight 11ms RepoModel.GetById 1ms 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
  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
 22package infoviewer.workaround;
 23
 24import infoviewer.InfoViewer.ArrowKeyHandler;
 25
 26import java.awt.event.KeyEvent;
 27import java.io.IOException;
 28import java.io.InputStream;
 29import java.net.HttpURLConnection;
 30import java.net.URL;
 31import java.net.URLConnection;
 32
 33import javax.accessibility.AccessibleContext;
 34import javax.swing.JEditorPane;
 35
 36import org.gjt.sp.jedit.Buffer;
 37import org.gjt.sp.jedit.MiscUtilities;
 38import org.gjt.sp.jedit.jEdit;
 39import org.gjt.sp.util.Log;
 40
 41/**
 42 * this is workaround class for JEditorPane. It avoids a bug in accessibility
 43 * support.
 44 */
 45public class EnhancedJEditorPane extends JEditorPane
 46{
 47	ArrowKeyHandler handler = null;
 48
 49	public EnhancedJEditorPane()
 50	{
 51		super();
 52	}
 53
 54	public EnhancedJEditorPane(String type, String text)
 55	{
 56		super(type, text);
 57	}
 58
 59	public EnhancedJEditorPane(String url) throws IOException
 60	{
 61		super(url);
 62	}
 63
 64	public EnhancedJEditorPane(URL initialPage) throws IOException
 65	{
 66		super(initialPage);
 67	}
 68
 69	protected void ProcessKeyEvent(KeyEvent ke)
 70	{
 71		if (handler != null)
 72			handler.processKeyEvent(ke);
 73		else 
 74			super.processKeyEvent(ke);
 75		
 76	}
 77
 78	protected InputStream getStream(URL page) throws IOException
 79	{
 80		Buffer buffer = getBuffer(page);
 81
 82		if (buffer != null)
 83		{
 84			String text = buffer.getText(0, buffer.getLength());
 85			// InputStream in = new
 86			// java.io.StringBufferInputStream(text);
 87			InputStream in = new java.io.ByteArrayInputStream(text.getBytes());
 88			Log.log(Log.DEBUG, this, "getStream(): got stream from jEdit buffer: "
 89				+ buffer);
 90
 91			// determine and set content type of buffer:
 92			// String type = getContentType(in);
 93			String type = getContentType(page);
 94
 95			if (type != null)
 96				setContentType(type);
 97
 98			return in;
 99		}
100
101		return super.getStream(page);
102	}
103
104	/**
105	 * Check whether the url is already opened by jEdit and if true, return
106	 * its Buffer.
107	 * 
108	 * @param page
109	 *                the URL.
110	 * @return the jEdit buffer, of null, if there is no open buffer with
111	 *         this URL.
112	 */
113	private Buffer getBuffer(URL page)
114	{
115		String path = page.toString();
116
117		// cut off reference part:
118		int pos = path.indexOf('#');
119		if (pos >= 0)
120			path = path.substring(0, pos);
121
122		// cut off query part:
123		pos = path.indexOf('?');
124		if (pos >= 0)
125			path = path.substring(0, pos);
126
127		// determine protocol:
128		String protocol = "file";
129		if (MiscUtilities.isURL(path))
130		{
131			protocol = MiscUtilities.getProtocolOfURL(path);
132			if (protocol.equals("file"))
133				path = path.substring(5);
134		}
135
136		// if file, canonize file url:
137		if (protocol.equals("file"))
138			path = MiscUtilities.constructPath(null, path);
139
140		return jEdit.getBuffer(path);
141	}
142
143	/**
144	 * Try to determine the content type of the url.
145	 */
146	private String getContentType(URL page) throws IOException
147	{
148		URLConnection conn = page.openConnection();
149		String type = conn.getContentType();
150
151		// need to disconnect http connections, because otherwise the
152		// URL
153		// would be cached somewhere... (dunno why)
154		if (conn instanceof HttpURLConnection)
155			((HttpURLConnection) conn).disconnect();
156		conn = null;
157
158		return type;
159	}
160
161	public void setArrowKeyHandler(ArrowKeyHandler akh)
162	{
163		handler = akh;
164
165	}
166
167	public AccessibleContext getAccessibleContext()
168	{
169		if (this.accessibleContext == null)
170			this.accessibleContext = new MyAccessibleJEditorPaneHTML();
171		return this.accessibleContext;
172	}
173
174	protected class MyAccessibleJEditorPaneHTML extends JEditorPane.AccessibleJEditorPaneHTML
175	{
176
177		public MyAccessibleJEditorPaneHTML()
178		{
179			super();
180		}
181
182		private static final long serialVersionUID = 3215023593258981917L;
183
184	}
185
186	private static final long serialVersionUID = 5302023859973568642L;
187
188}