Package net.laubenberger.tyr.view

Source Code of net.laubenberger.tyr.view.PanelXHtml

/*
* Copyright (c) 2010-2011 by Stefan Laubenberger.
*
* Tyr is free software: you can redistribute it and/or modify
* it under the terms of the General Public License v2.0.
*
* Tyr is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
*
* See the GNU General Public License for more details:
* <http://www.gnu.org/licenses>
*
* This distribution is available at:
* <http://code.google.com/p/tyr/>
* <http://dev.laubenberger.net/tyr/>
*
* Contact information:
* Stefan Laubenberger
* Bullingerstrasse 53
* CH-8004 Zuerich
*
* <http://www.laubenberger.net>
*
* <laubenberger@gmail.com>
*/
package net.laubenberger.tyr.view;

import java.awt.BorderLayout;
import java.awt.GridBagConstraints;
import java.awt.event.MouseListener;
import java.io.DataInputStream;
import java.io.IOException;
import java.net.URL;
import javax.swing.JScrollPane;
import javax.swing.ScrollPaneConstants;
import net.laubenberger.bogatyr.helper.HelperIO;
import net.laubenberger.bogatyr.helper.HelperLog;
import net.laubenberger.bogatyr.helper.launcher.LauncherBrowser;
import net.laubenberger.bogatyr.helper.launcher.LauncherFile;
import net.laubenberger.bogatyr.helper.launcher.LauncherMail;
import net.laubenberger.bogatyr.view.swing.Panel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.xhtmlrenderer.extend.TextRenderer;
import org.xhtmlrenderer.simple.FSScrollPane;
import org.xhtmlrenderer.simple.XHTMLPanel;
import org.xhtmlrenderer.swing.BasicPanel;
import org.xhtmlrenderer.swing.LinkListener;

/**
* PanelXHtml <strong>Note:</strong> This class needs <a
* href="https://xhtmlrenderer.dev.java.net/">XHTML renderer</a>
*
* @author Stefan Laubenberger
* @version 0.7.5 (20110121)
* @since 0.4.0
*/
public class PanelXHtml extends Panel {
  private static final long serialVersionUID = -141939191654227033L;

  static final Logger log = LoggerFactory.getLogger(PanelXHtml.class);

  private final URL url;
  final XHTMLPanel xhtmlPanel = new XHTMLPanel();

  public PanelXHtml(final URL url) {
    super(new BorderLayout());
    if (log.isTraceEnabled()) log.trace(HelperLog.constructor(url));

    this.url = url;

    createLayout();
  }

  /*
   * Private methods
   */

  private void createLayout() {

    final GridBagConstraints gbc = new GridBagConstraints();
    gbc.fill = GridBagConstraints.BOTH;
    gbc.weightx = 1.0D;
    gbc.weighty = 1.0D;
    gbc.gridx = 0;
    gbc.gridy = 0;

    xhtmlPanel.getSharedContext().getTextRenderer().setSmoothingThreshold(0.0F);
    xhtmlPanel.getSharedContext().getTextRenderer().setSmoothingLevel(TextRenderer.HIGH);
    xhtmlPanel.setDocument(url.toString());

    // Catch own defined links
    final MouseListener[] lls = xhtmlPanel.getMouseListeners();
    for (final MouseListener listener : lls) {
      if (listener instanceof LinkListener) {
        xhtmlPanel.removeMouseListener(listener);
      }
    }

    xhtmlPanel.addMouseTrackingListener(new LinkListener() {
      @Override
      public void linkClicked(final BasicPanel panel, final String uri) {
        if (uri.startsWith("#internal")) { //$NON-NLS-1$
          final String document = uri.split(":")[1]; //$NON-NLS-1$

          xhtmlPanel.setDocument(getClass().getClassLoader().getResource(document).toString());

          revalidate();
          repaint();
        } else if (uri.startsWith("#open")) { //$NON-NLS-1$
          final String document = uri.split(":")[1]; //$NON-NLS-1$
          final String extension = HelperIO.getFileExtension(document);

          final DataInputStream in = new DataInputStream(getClass().getClassLoader().getResourceAsStream(document));
          try {
            LauncherFile.open(in, extension);
          } catch (IOException ex) {
            log.error("Could not launch an application for the given file", ex); //$NON-NLS-1$
          } finally {
            try {
              in.close();
            } catch (IOException ex) {
              // do nothing
              if (log.isWarnEnabled()) log.warn("Could not close stream", ex); //$NON-NLS-1$
            }
          }
        } else if (uri.startsWith("#mail")) { //$NON-NLS-1$
          final String mail = uri.split(":")[1]; //$NON-NLS-1$
          try {
            LauncherMail.mail(mail);
          } catch (Exception ex) {
            log.error("Could not launch mail application", ex); //$NON-NLS-1$
          }
        } else if (uri.startsWith("#web")) { //$NON-NLS-1$
          final String web = uri.split(":")[1]; //$NON-NLS-1$

          try {
            LauncherBrowser.browse(web);
          } catch (Exception ex) {
            log.error("Could not launch browser", ex); //$NON-NLS-1$
          }
        } else {
          log.error("Unsupported link type pressed " + uri); //$NON-NLS-1$
        }
      }
    });

    final JScrollPane scroll = new FSScrollPane(xhtmlPanel);
    scroll.setWheelScrollingEnabled(true);
    scroll.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
    scroll.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED);

    add(scroll, BorderLayout.CENTER);
  }
}
TOP

Related Classes of net.laubenberger.tyr.view.PanelXHtml

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.