001: /*
002: * <copyright>
003: *
004: * Copyright 2001-2004 BBNT Solutions, LLC
005: * under sponsorship of the Defense Advanced Research Projects
006: * Agency (DARPA).
007: *
008: * You can redistribute this software and/or modify it under the
009: * terms of the Cougaar Open Source License as published on the
010: * Cougaar Open Source Website (www.cougaar.org).
011: *
012: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
013: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
014: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
015: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
016: * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
017: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
018: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
019: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
020: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
021: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
022: * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
023: *
024: * </copyright>
025: */
026: package org.cougaar.tools.csmart.ui;
027:
028: import org.cougaar.tools.csmart.ui.viewer.CSMART;
029: import org.cougaar.util.log.Logger;
030:
031: import javax.swing.*;
032: import javax.swing.event.HyperlinkEvent;
033: import javax.swing.event.HyperlinkListener;
034: import javax.swing.text.AttributeSet;
035: import javax.swing.text.Document;
036: import javax.swing.text.html.HTML;
037: import javax.swing.text.html.HTMLDocument;
038: import java.awt.*;
039: import java.awt.event.WindowAdapter;
040: import java.awt.event.WindowEvent;
041: import java.io.IOException;
042: import java.net.URL;
043:
044: public class Browser extends JFrame implements HyperlinkListener {
045: private static final Dimension DEFAULT_SIZE = new Dimension(600,
046: 800);
047: private static final String BROWSER_TITLE = "CSMART Help Browser";
048: private static Browser singleton;
049: private Logger log;
050:
051: private JTextPane text = new JTextPane() {
052: public void scrollToReference(String ref) {
053: super .scrollToReference(ref);
054: selectReference(ref);
055: }
056: };
057: private JScrollPane scroll = new JScrollPane();
058:
059: private Browser() {
060: super (BROWSER_TITLE);
061: log = CSMART.createLogger(getClass().getName());
062: scroll.setViewport(new JViewport() {
063: public void scrollRectToVisible(Rectangle r) {
064: Dimension extent = getExtentSize();
065: r = new Rectangle(r.x, r.y, r.width, extent.height);
066: super .scrollRectToVisible(r);
067: }
068: });
069: scroll.setViewportView(text);
070: getContentPane().add(scroll);
071: addWindowListener(new WindowAdapter() {
072: public void windowClosing(WindowEvent e) {
073: dispose();
074: }
075: });
076: text.setEditable(false);
077: scroll.setPreferredSize(DEFAULT_SIZE);
078: pack();
079: show();
080: }
081:
082: public void dispose() {
083: clearSingleton();
084: super .dispose();
085: }
086:
087: private HTMLDocument.Iterator getIterator(HTML.Tag tag) {
088: Document doc = text.getDocument();
089: if (doc instanceof HTMLDocument) {
090: HTMLDocument html = (HTMLDocument) doc;
091: return html.getIterator(tag);
092: }
093: return null;
094: }
095:
096: private void selectReference(String ref) {
097: HTMLDocument.Iterator i = getIterator(HTML.Tag.A);
098: if (i != null) {
099: for (; i.isValid(); i.next()) {
100: AttributeSet a = i.getAttributes();
101: String nm = (String) a
102: .getAttribute(HTML.Attribute.NAME);
103: if ((nm != null) && nm.equals(ref)) {
104: // found a matching reference in the document.
105: text.select(i.getStartOffset(), i.getEndOffset());
106: }
107: }
108: }
109: }
110:
111: public void setURL(URL url) {
112: try {
113: text.setPage(url);
114: toFront();
115: } catch (IOException ioe) {
116: if (log.isErrorEnabled()) {
117: log.error("Exception", ioe);
118: }
119: }
120: }
121:
122: public void setText(String data) {
123: text.setText(data);
124: toFront();
125: }
126:
127: public void hyperlinkUpdate(HyperlinkEvent e) {
128: if (e.getEventType() == HyperlinkEvent.EventType.ACTIVATED) {
129: setURL(e.getURL());
130: }
131: }
132:
133: private static synchronized void clearSingleton() {
134: singleton = null;
135: }
136:
137: public static synchronized void setPage(URL url) {
138: if (singleton == null) {
139: singleton = new Browser();
140: } else {
141: singleton.setState(JFrame.NORMAL);
142: }
143: singleton.setURL(url);
144: }
145:
146: public static synchronized void setPage(String text) {
147: if (singleton == null) {
148: singleton = new Browser();
149: } else {
150: singleton.setState(JFrame.NORMAL);
151: }
152: singleton.setText(text);
153: }
154: }
|