Package jpianotrain.gui

Source Code of jpianotrain.gui.InfoPanel

/* For License see bottom */
package jpianotrain.gui;

import static jpianotrain.util.ResourceKeys.HELP_PAGE;

import java.awt.Dimension;
import java.net.URL;
import java.util.Stack;

import javax.swing.JEditorPane;
import javax.swing.JScrollPane;
import javax.swing.event.HyperlinkEvent;
import javax.swing.event.HyperlinkListener;
import javax.swing.text.html.HTMLDocument;
import javax.swing.text.html.HTMLFrameHyperlinkEvent;

import jpianotrain.VersionInformation;
import jpianotrain.event.NavigationCallback;
import jpianotrain.event.NavigationListener;
import jpianotrain.util.ExternalURLHandler;
import jpianotrain.util.ResourceFactory;
import jpianotrain.util.ResourceKeys;

import org.apache.log4j.Logger;

/**
*
* @author  methke01
*/
@VersionInformation (
  lastChanged="$LastChangedDate: 2008-05-12 04:23:35 -0500 (Mon, 12 May 2008) $",
  authors={"Alexander Methke"},
  revision="$LastChangedRevision: 19 $",
  lastEditor="$LastChangedBy: onkobu $",
  id="$Id"
)
public class InfoPanel extends javax.swing.JPanel
            implements HyperlinkListener,
                   NavigationListener {
  private static final Logger log=Logger.getLogger(InfoPanel.class);
 
  /**
   * Creates a new InfoPanel with
   * HELP_PAGE displaying initially.
   */
  public InfoPanel() {
    this(HELP_PAGE);
  }

  /**
   * Creates new InfoPanel.
   *
   * @param pageKey Key of the resource to load initially.
   */
  public InfoPanel(ResourceKeys pageKey) {
    this(pageKey, false);
  }

  public InfoPanel(ResourceKeys pageKey,
           boolean hideControls) {
    initComponents(hideControls);
    helpEditor.setContentType("text/html");
    helpEditor.setEditable(false);
    helpEditor.addHyperlinkListener(this);
    navigationPanel.setNavigationListener(this);
    initValues(pageKey);
    setMinimumSize(new Dimension(150, 200));
    setPreferredSize(getMinimumSize());
  }

  protected void initValues(ResourceKeys pageKey) {
    navigationCallback.navigateBackwardStateChanged(false);
    navigationCallback.navigateForwardStateChanged(false);
    navigationCallback.navigateTopStateChanged(true);

    String resUrl=null;
    try {
      if (helpEditor.getPage()!=null) {
        backwardStack.push(helpEditor.getPage());
        navigationCallback.navigateBackwardStateChanged(true);
        forwardStack.clear();
        navigationCallback.navigateForwardStateChanged(false);
      }
      resUrl=ResourceFactory.getString(pageKey);
      URL u=ClassLoader.getSystemClassLoader().getResource(resUrl);
      helpEditor.setPage(u);
      if (topUrl==null) {
        topUrl=u;
      }
    } catch (Exception ex) {
      helpEditor.setText("<h2>Faild to load "+HELP_PAGE.getKey()+" ("+resUrl+")</h2>");
      ex.printStackTrace();
    }
  }

  /** This method is called from within the constructor to
   * initialize the form.
   * WARNING: Do NOT modify this code. The content of this method is
   * always regenerated by the Form Editor.
   */
    private void initComponents(boolean hideControls) {
        java.awt.GridBagConstraints gridBagConstraints;

        jScrollPane1 = new javax.swing.JScrollPane();
        helpEditor = new javax.swing.JEditorPane();
        navigationPanel = new NavigationPanel();

        setLayout(new java.awt.GridBagLayout());

        jScrollPane1.setEnabled(false);
        helpEditor.setEditable(false);
        jScrollPane1.setViewportView(helpEditor);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.weighty = 1.0;
        add(jScrollPane1, gridBagConstraints);

    if (!hideControls) {
      navigationPanel.setMaximumSize(new java.awt.Dimension(144, 32767));
      gridBagConstraints = new java.awt.GridBagConstraints();
      gridBagConstraints.gridx = 0;
      gridBagConstraints.gridy = 1;
      gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
      add(navigationPanel, gridBagConstraints);
    }
    }

// HyperlinkListener
  public void hyperlinkUpdate(HyperlinkEvent e) {
    if (e.getEventType() == HyperlinkEvent.EventType.ACTIVATED) {
      URL u=e.getURL();
      if (u.getProtocol().equals("http")) {
        log.debug("GUI tries to open external URL "+u);
        fireOpenExternalURL(u);
        return;
      }
      JEditorPane pane = (JEditorPane) e.getSource();
      if (e instanceof HTMLFrameHyperlinkEvent) {
        HTMLFrameHyperlinkEvent  evt = (HTMLFrameHyperlinkEvent)e;
        HTMLDocument doc = (HTMLDocument)pane.getDocument();
        doc.processHTMLFrameHyperlinkEvent(evt);
      } else {
        try {
          backwardStack.push(pane.getPage());
          navigationCallback.navigateBackwardStateChanged(true);
          forwardStack.clear();
          navigationCallback.navigateForwardStateChanged(false);
          pane.setPage(e.getURL());
        } catch (Throwable t) {
          t.printStackTrace();
        }
      }
    }
  }

// NavigationListener
  public void navigateForward() {
    if (forwardStack.size()==0) {
      navigationCallback.navigateForwardStateChanged(false);
      return;
    }
    URL u=forwardStack.pop();
    try {
      helpEditor.setPage(u);
      if (forwardStack.size()==0) {
        navigationCallback.navigateForwardStateChanged(false);
      }
      backwardStack.push(u);
      navigationCallback.navigateBackwardStateChanged(true);
    } catch (Exception ex) {
      ex.printStackTrace();
    }
  }

  /**
   * Go one step backward. Only possible, if there were
   * preceeding navigation events.
   */
  public void navigateBackward() {
    if (backwardStack.size()==0) {
      navigationCallback.navigateBackwardStateChanged(false);
      return;
    }

    URL u=backwardStack.pop();
    try {
      forwardStack.push(helpEditor.getPage());
      navigationCallback.navigateForwardStateChanged(true);
      helpEditor.setPage(u);
      if (backwardStack.size()==0) {
        navigationCallback.navigateBackwardStateChanged(false);
      }
    } catch (Exception ex) {
      ex.printStackTrace();
    }
  }

  /**
   * Go back to where the user started from.
   */
  public void navigateTop() {
    try {
      if (backwardStack.size()==0 ||
        !backwardStack.peek().equals(helpEditor.getPage())) {
        backwardStack.push(helpEditor.getPage());
        navigationCallback.navigateBackwardStateChanged(true);
      }

      helpEditor.setPage(topUrl);
      forwardStack.clear();
      navigationCallback.navigateForwardStateChanged(false);
    } catch (Exception ex) {
      ex.printStackTrace();
    }
  }

  /**
   * Sets the callback to notify the navigator about
   * history state changes.
   */
  public void setNavigationCallback(NavigationCallback cb) {
    navigationCallback=cb;
  }

  private void fireOpenExternalURL(URL u) {
    ExternalURLHandler.handle(u);
  }
 
  private NavigationCallback navigationCallback;

  private URL topUrl;

  private Stack<URL> forwardStack=new Stack<URL>();
  private Stack<URL> backwardStack=new Stack<URL>();

    private JEditorPane helpEditor;
    private JScrollPane jScrollPane1;
    private NavigationPanel navigationPanel;
}
/*
    Copyright (C) 2008  Alexander Methke

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program 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.

    You should have received a copy of the GNU General Public License
    along with this program (gplv3.txt).
*/
TOP

Related Classes of jpianotrain.gui.InfoPanel

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.