001: /**
002: * Copyright 2004 Carlos Silva A.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: *
016: */package com.csa.lgantt.view;
017:
018: import java.awt.BorderLayout;
019: import java.io.IOException;
020: import java.net.URL;
021:
022: import javax.swing.JEditorPane;
023: import javax.swing.JFrame;
024: import javax.swing.JScrollPane;
025: import javax.swing.JViewport;
026: import javax.swing.event.HyperlinkEvent;
027: import javax.swing.event.HyperlinkListener;
028: import javax.swing.text.html.HTMLDocument;
029: import javax.swing.text.html.HTMLFrameHyperlinkEvent;
030:
031: import com.csa.lgantt.Messages;
032:
033: /**
034: * HelpWindow despliega el contenido de la ayuda HTML en una ventana.
035: *
036: * <p>$Date: 2006/09/14 08:14:17 $</p>
037: * @version $Revision: 1.1 $
038: * @author Carlos Silva
039: */
040: public class HelpWindow extends JFrame {
041: /**
042: *
043: */
044: private static final long serialVersionUID = 3256436989241735473L;
045: JEditorPane html;
046:
047: /**
048: * Inicializa y muestra la ventana de ayuda a partir de
049: * un archivo obtenido usando getResource
050: * @param path
051: */
052: public HelpWindow(String path) {
053: super (Messages.getString("help.title")); //$NON-NLS-1$
054:
055: URL url = null;
056: try {
057: url = HelpWindow.class.getResource(path);
058: //ClassLoader.getSystemResource(path);
059: } catch (Exception e) {
060: System.err.println(Messages
061: .getString("help.error.open.failed") + path); //$NON-NLS-1$
062: url = null;
063: }
064: initURL(url);
065: setSize(500, 480);
066: }
067:
068: /**
069: * Crea una ventana de ayuda a partir de un URL
070: * @param url
071: */
072: public HelpWindow(URL url) {
073: super (Messages.getString("help.title")); //$NON-NLS-1$
074: initURL(url);
075: setSize(600, 440);
076: }
077:
078: private void initURL(URL url) {
079: try {
080:
081: if (url != null) {
082: html = new JEditorPane(url);
083: html.setEditable(false);
084: html.addHyperlinkListener(createHyperLinkListener());
085: JScrollPane scroller = new JScrollPane();
086: JViewport vp = scroller.getViewport();
087: vp.add(html);
088: getContentPane().add(scroller, BorderLayout.CENTER);
089: }
090: } catch (Exception e) {
091: System.out
092: .println(Messages.getString("help.error.init") + e); //$NON-NLS-1$
093: }
094:
095: }
096:
097: public HyperlinkListener createHyperLinkListener() {
098: return new HyperlinkListener() {
099: public void hyperlinkUpdate(HyperlinkEvent e) {
100: if (e.getEventType() == HyperlinkEvent.EventType.ACTIVATED) {
101: if (e instanceof HTMLFrameHyperlinkEvent) {
102: ((HTMLDocument) html.getDocument())
103: .processHTMLFrameHyperlinkEvent((HTMLFrameHyperlinkEvent) e);
104: } else {
105: try {
106: html.setPage(e.getURL());
107: } catch (IOException ioe) {
108: System.out
109: .println(Messages
110: .getString("help.error.follow.link") + ioe); //$NON-NLS-1$
111: }
112: }
113: }
114: }
115: };
116: }
117:
118: /**
119: * Funcion de test
120: * @param args
121: */
122: public static void main(String[] args) {
123: HelpWindow h = new HelpWindow("res/help/kbd.html");
124: h.setVisible(true);
125: }
126: }
|