HelpWindow.java :  » Project-Management » lgantt » com » csa » lgantt » view » Java Open Source

Java Open Source » Project Management » lgantt 
lgantt » com » csa » lgantt » view » HelpWindow.java
/**
 *   Copyright 2004 Carlos Silva A.
 * 
 *   Licensed under the Apache License, Version 2.0 (the "License");
 *   you may not use this file except in compliance with the License. 
 *   You may obtain a copy of the License at  
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0
 * 
 *   Unless required by applicable law or agreed to in writing, software
 *   distributed under the License is distributed on an "AS IS" BASIS,
 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *   See the License for the specific language governing permissions and
 *   limitations under the License.
 * 
 */
package com.csa.lgantt.view;

import java.awt.BorderLayout;
import java.io.IOException;
import java.net.URL;

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

import com.csa.lgantt.Messages;


/**
 * HelpWindow despliega el contenido de la ayuda HTML en una ventana.
 * 
 * <p>$Date: 2006/09/14 08:14:17 $</p> 
 * @version $Revision: 1.1 $
 * @author Carlos Silva
 */
public class HelpWindow extends JFrame {
  /**
   * 
   */
  private static final long serialVersionUID = 3256436989241735473L;
  JEditorPane html;
  /**
   * Inicializa y muestra la ventana de ayuda a partir de 
   * un archivo obtenido usando getResource 
   * @param path
   */
  public HelpWindow(String path) {
    super(Messages.getString("help.title")); //$NON-NLS-1$

    URL url = null;    
    try {      
      url = HelpWindow.class.getResource(path); 
      //ClassLoader.getSystemResource(path);
    } catch (Exception e) {
      System.err.println(Messages.getString("help.error.open.failed") + path); //$NON-NLS-1$
      url = null;
    }
    initURL(url);
    setSize(500,480);
  }

  /**
   * Crea una ventana de ayuda a partir de un URL
   * @param url
   */
  public HelpWindow(URL url) {
    super(Messages.getString("help.title")); //$NON-NLS-1$
    initURL(url);
    setSize(600,440);
  }

  private void initURL(URL url) {
    try {

      if (url != null) {
        html = new JEditorPane(url);
        html.setEditable(false);
        html.addHyperlinkListener(createHyperLinkListener());
        JScrollPane scroller = new JScrollPane();
        JViewport vp = scroller.getViewport();
        vp.add(html);
        getContentPane().add(scroller, BorderLayout.CENTER);
      }
    } catch (Exception e) {
      System.out.println(Messages.getString("help.error.init") + e); //$NON-NLS-1$
    }
    
  }

  public HyperlinkListener createHyperLinkListener() {
    return new HyperlinkListener() {
      public void hyperlinkUpdate(HyperlinkEvent e) {
        if (e.getEventType() == HyperlinkEvent.EventType.ACTIVATED) {
          if (e instanceof HTMLFrameHyperlinkEvent) {
            ((HTMLDocument) html.getDocument()).processHTMLFrameHyperlinkEvent((HTMLFrameHyperlinkEvent) e);
          } else {
            try {
              html.setPage(e.getURL());
            } catch (IOException ioe) {
              System.out.println(Messages.getString("help.error.follow.link") + ioe); //$NON-NLS-1$
            }
          }
        }
      }
    };
  }
  
  /**
   * Funcion de test
   * @param args
   */
  public static void main(String[] args) {
    HelpWindow h = new HelpWindow("res/help/kbd.html");    
    h.setVisible(true);
  }
}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.