1 59 60 61 package swingwtx.swing; 62 63 import java.io.IOException ; 64 import java.net.URL ; 65 import java.util.Iterator ; 66 import java.util.Vector ; 67 68 import org.eclipse.swt.SWT; 69 import org.eclipse.swt.SWTError; 70 import org.eclipse.swt.browser.Browser; 71 import org.eclipse.swt.browser.LocationEvent; 72 import org.eclipse.swt.browser.LocationListener; 73 import org.eclipse.swt.browser.StatusTextEvent; 74 import org.eclipse.swt.browser.StatusTextListener; 75 76 import swingwt.awt.Color; 77 import swingwtx.swing.text.AttributeSet; 78 import swingwtx.swing.text.BadLocationException; 79 import swingwtx.swing.text.Document; 80 import swingwtx.swing.text.EditorKit; 81 import swingwtx.swing.text.Element; 82 import swingwtx.swing.text.Position; 83 import swingwtx.swing.text.Segment; 84 import swingwtx.swing.text.StyleConstants; 85 86 public class JEditorPane extends swingwtx.swing.text.JTextComponent { 87 88 protected Browser ppeer = null; 89 protected String pUrl = ""; 90 protected Color selectionColor = Color.BLACK; 91 92 protected Vector hyperListeners = new Vector (); 93 94 public JEditorPane() {} 95 public JEditorPane(String url) throws IOException { pUrl = url; } 96 public JEditorPane(String type, String text) { pText = text; } 97 public JEditorPane(URL url) throws IOException { pUrl = url.toString(); } 98 99 public String getText() { return pText; } 100 public void setText(String text) { pText = text; try { handleText(); } catch (Exception e) { e.printStackTrace(); } } 101 public String getContentType() { return "text/html"; } 102 public void setContentType(String contentType) { } 103 public boolean isEditable() { return false; } 104 public void setEditable(boolean b) { } 105 public void setPage(URL url) throws IOException { 106 String newUrl = url.toString(); 108 if (pUrl.equals(newUrl)) return; 109 pUrl = newUrl; 110 SwingUtilities.invokeAsync(new Runnable () { 111 public void run() { 112 if (SwingWTUtils.isSWTControlAvailable(ppeer)) 113 ppeer.setUrl(pUrl); 114 } 115 }); 116 } 117 public void setPage(String url) throws IOException { 118 setPage(new URL (url)); 119 } 120 public void addHyperlinkListener(swingwtx.swing.event.HyperlinkListener l) { hyperListeners.add(l); } 121 public void removeHyperlinkListener(swingwtx.swing.event.HyperlinkListener l) { hyperListeners.remove(l); } 122 123 public void setCaretPosition(int pos) { } 124 public int getCaretPosition() { return 0; } 125 public int getSelectionStart() { return 0; } 126 public int getSelectionEnd() { return 0; } 127 public EditorKit getEditorKit() { return null; } 128 public void setEditorKit(EditorKit k) { } 129 public void setSelectionColor(Color color) { this.selectionColor = color; } 130 public Color getSelectionColor(){ return selectionColor; } 131 132 136 private void handleText() throws IOException { 137 setPage(SwingWTUtils.stringToTempFile(pText.getBytes(), "html")); 138 } 139 140 143 protected swingwt.awt.Dimension calculatePreferredSize() { 144 swingwt.awt.Dimension size = new swingwt.awt.Dimension(300, 200); 146 setSize(size); 147 return size; 148 } 149 150 155 public void setSwingWTParent(swingwt.awt.Container parent) throws Exception { 156 descendantHasPeer = true; 157 try { 158 159 ppeer = new Browser(parent.getComposite(), SWT.NONE); 160 161 if (!pText.equals("")) handleText(); 163 if (!pUrl.equals("")) ppeer.setUrl(pUrl); 164 165 registerHyperLinkEvents(); 166 167 peer = ppeer; 168 this.parent = parent; 169 } 170 catch (SWTError e) { 171 System.out.println("SwingWT JEditorPane/SWT Browser requires at least Mozilla 1.4 to operate."); 172 System.out.println("If you have it installed, then you need to create the file /etc/gre.conf"); 173 System.out.println("and in that file, put the following:\n"); 174 System.out.println("[VERSION]"); 175 System.out.println("GRE_PATH=MOZILLA_PATH\n"); 176 System.out.println("Where VERSION is the version of Mozilla you have (eg: 1.4)"); 177 System.out.println("and MOZILLA_PATH is the path to Mozilla - eg: /usr/lib/mozilla-1.4"); 178 } 179 } 180 181 185 protected void registerHyperLinkEvents() { 186 ppeer.addLocationListener(new LocationListener() { 187 public void changed(LocationEvent event) { 188 processHyperlinkEvent(event.location, swingwtx.swing.event.HyperlinkEvent.EventType.ACTIVATED); 189 } 190 public void changing(LocationEvent event) {} 191 }); 192 ppeer.addStatusTextListener(new StatusTextListener() { 193 public void changed(StatusTextEvent event) { 194 processHyperlinkEvent(event.text, swingwtx.swing.event.HyperlinkEvent.EventType.ENTERED); 195 } 196 }); 197 } 198 199 protected void processHyperlinkEvent(String location, swingwtx.swing.event.HyperlinkEvent.EventType eventType) { 200 URL url = null; 202 try { url = new URL (location); } catch (Exception e) {} 203 swingwtx.swing.event.HyperlinkEvent e = new swingwtx.swing.event.HyperlinkEvent(this, eventType, url, location); 204 Iterator i = hyperListeners.iterator(); 205 while (i.hasNext()) { 206 ((swingwtx.swing.event.HyperlinkListener) i.next()).hyperlinkUpdate(e); 207 } 208 } 209 210 public void setDocument(Document newdoc) { 211 System.err.println("Setting document is NOT implemented yet!"); 212 } 213 214 private EditorPaneHTMLDoc doc = new EditorPaneHTMLDoc(); 215 216 220 private class EditorPaneHTMLDocPosition implements Position { 221 private int offset = 0; 222 public EditorPaneHTMLDocPosition(int pos) { offset = pos; } 223 public int getOffset() { return offset; } 224 } 225 226 237 private class EditorPaneHTMLDoc implements Document { 238 239 String docText=""; 240 241 public Position getEndPosition() { 242 return new EditorPaneHTMLDocPosition(docText.length()); 243 } 244 public Position getStartPosition() { 245 return new EditorPaneHTMLDocPosition(0); 246 } 247 248 251 public void remove(int i, int j) { 252 StringBuffer buffer = new StringBuffer (docText); 253 buffer.replace(i, j, ""); 254 docText = buffer.toString(); 255 convertText(); 256 } 257 258 private void convertText() { 259 pText = "<HTML><BODY>"+docText+"<A name=\"1\"/></BODY></HTML>"; 260 try { 261 handleText(); 262 } 263 catch (IOException e) { 264 e.printStackTrace(); 265 } 266 } 267 268 271 public void insertString(int i, String string, AttributeSet as) { 272 273 StringBuffer buffer = new StringBuffer (docText); 274 StringBuffer nb = new StringBuffer (string); 275 Color foreground=(Color) as.getAttribute(StyleConstants.ColorConstants.Foreground); 276 int index = SwingWTUtils.getStringBufferIndexOf(nb, ">"); 277 while (index >-1) { 278 nb.replace(index, index+1, ">"); 279 index = SwingWTUtils.getStringBufferIndexOf(nb, ">"); 280 } 281 index = SwingWTUtils.getStringBufferIndexOf(nb, "<"); 282 while (index >-1){ 283 nb.replace(index, index+1, "<"); 284 SwingWTUtils.getStringBufferIndexOf(nb, "<"); 285 } 286 287 SwingWTUtils.getStringBufferIndexOf(nb, "http://"); 288 while (index > -1) { 289 int end = Math.min(SwingWTUtils.getStringBufferIndexOf(nb, " ", index) ,SwingWTUtils.getStringBufferIndexOf(nb, "\n", index) ); 290 if(end==-1) 291 end =nb.length()-1; 292 String url=nb.substring(index, end); 293 nb.replace(index, end, "<A HREF=\""+url+"\" " + 294 ">"+url+"</A> "); 296 index = SwingWTUtils.getStringBufferIndexOf(nb, "http://", end + (end-index) + 32 ); 297 } 298 299 if (foreground!=null){ 300 nb.insert(0, "<font color=\""+encodeColor(foreground)+"\">"); 301 nb.append("</font> "); 302 } 303 index = SwingWTUtils.getStringBufferIndexOf(nb, "\n"); 304 while (index >-1){ 305 nb.replace(index, index+1, "<br> "); 306 SwingWTUtils.getStringBufferIndexOf(nb, "\n"); 307 } 308 309 if(i<=0) 310 buffer.append(nb.toString()); 311 else 312 buffer.insert(i,nb.toString()); 313 docText = buffer.toString(); 314 convertText(); 315 316 } 317 318 private String encodeColor(Color foreground) { 319 return "#"+Integer.toHexString(foreground.getRed())+Integer.toHexString(foreground.getGreen())+Integer.toHexString(foreground.getBlue()); 320 } 321 322 public void addDocumentListener(swingwtx.swing.event.DocumentListener listener) { } 323 public void addUndoableEditListener(swingwtx.swing.event.UndoableEditListener listener) { } 324 public Position createPosition(int offs) throws BadLocationException { return new EditorPaneHTMLDocPosition(offs); } 325 public Element getDefaultRootElement() { return null; } 326 public int getLength() {return docText.length(); } 327 public Object getProperty(Object key) { return null; } 328 public Element[] getRootElements() { return null; } 329 public String getText(int offset, int length) throws BadLocationException { 330 String ret = null; 331 try { 332 ret = docText.substring(offset, offset+length); 333 } 334 catch (StringIndexOutOfBoundsException e) { throw new BadLocationException("Invalid range", offset); } 335 return ret; 336 } 337 public void getText(int offset, int length, Segment txt) throws BadLocationException { 338 String ret = null; 339 try { 340 ret = docText.substring(offset, offset+length); 341 } 342 catch (StringIndexOutOfBoundsException e) { throw new BadLocationException("Invalid range", offset); } 343 txt.array = docText.toCharArray(); 344 txt.offset = offset; 345 txt.count = length; 346 } 347 public void putProperty(Object key, Object value) { } 348 public void removeDocumentListener(swingwtx.swing.event.DocumentListener listener) { } 349 public void removeUndoableEditListener(swingwtx.swing.event.UndoableEditListener listener) { } 350 public void render(Runnable r) { } 351 352 } 353 354 public Document getDocument() { 355 return doc; 356 } 357 358 } 359 | Popular Tags |