001/* 002 * jPOS Project [http://jpos.org] 003 * Copyright (C) 2000-2026 jPOS Software SRL 004 * 005 * This program is free software: you can redistribute it and/or modify 006 * it under the terms of the GNU Affero General Public License as 007 * published by the Free Software Foundation, either version 3 of the 008 * License, or (at your option) any later version. 009 * 010 * This program is distributed in the hope that it will be useful, 011 * but WITHOUT ANY WARRANTY; without even the implied warranty of 012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 013 * GNU Affero General Public License for more details. 014 * 015 * You should have received a copy of the GNU Affero General Public License 016 * along with this program. If not, see <http://www.gnu.org/licenses/>. 017 */ 018 019package org.jpos.ui.factory; 020 021import org.jdom2.Element; 022import org.jpos.ui.UI; 023import org.jpos.ui.UIFactory; 024 025import javax.swing.*; 026import javax.swing.event.HyperlinkEvent; 027import javax.swing.event.HyperlinkListener; 028import javax.swing.text.html.HTMLDocument; 029import javax.swing.text.html.HTMLFrameHyperlinkEvent; 030 031/** 032 * UIFactory that builds a Swing HTML browser/editor pane. 033 * 034 * @author Alejandro Revilla 035 * 036 * Creates an html browser/editor 037 * i.e: 038 * <pre> 039 * <html editable="false" follow-links="true" scrollable="true"> 040 * http://jpos.org 041 * </html> 042 * </pre> 043 * @see org.jpos.ui.UIFactory 044 */ 045public class HtmlFactory implements UIFactory { 046 /** Default constructor for {@link UIFactory} discovery. */ 047 public HtmlFactory() {} 048 public JComponent create (UI ui, Element e) { 049 try { 050 JEditorPane editorPane = new JEditorPane (e.getText()); 051 editorPane.setEditable ( 052 "true".equals (e.getAttributeValue ("editable")) 053 ); 054 if ("true".equals (e.getAttributeValue ("follow-links"))) 055 editorPane.addHyperlinkListener (new Listener ()); 056 return editorPane; 057 } catch (Exception ex) { 058 return new JLabel (ex.getMessage()); 059 } 060 } 061 static class Listener implements HyperlinkListener { 062 public void hyperlinkUpdate(HyperlinkEvent e) { 063 if (e.getEventType() == HyperlinkEvent.EventType.ACTIVATED) { 064 JEditorPane pane = (JEditorPane) e.getSource(); 065 if (e instanceof HTMLFrameHyperlinkEvent) { 066 HTMLFrameHyperlinkEvent evt = (HTMLFrameHyperlinkEvent)e; 067 HTMLDocument doc = (HTMLDocument)pane.getDocument(); 068 doc.processHTMLFrameHyperlinkEvent(evt); 069 } else { 070 try { 071 pane.setPage(e.getURL()); 072 } catch (Throwable t) { 073 t.printStackTrace(); 074 } 075 } 076 } 077 } 078 } 079} 080