001 package gate.swing;
002
003 import java.awt.event.ActionEvent;
004 import java.io.IOException;
005 import java.net.URL;
006 import java.util.LinkedList;
007 import java.util.Vector;
008
009 import javax.swing.*;
010 import javax.swing.event.HyperlinkEvent;
011 import javax.swing.event.HyperlinkListener;
012 import javax.swing.text.html.HTMLDocument;
013 import javax.swing.text.html.HTMLFrameHyperlinkEvent;
014
015 import gate.event.StatusListener;
016 import gate.gui.MainFrame;
017 import gate.util.Err;
018
019 /**
020 * An enhanced version of {@link javax.swing.JEditorPane} that is able of
021 * handling hyperlinks from the HTML document displayed.
022 */
023 public class XJEditorPane extends JEditorPane {
024
025 public XJEditorPane(){
026 super();
027 init();
028 }
029
030 public XJEditorPane(String url) throws IOException{
031 super(url);
032 init();
033 }
034
035 public XJEditorPane(URL initialPage)throws IOException{
036 super(initialPage);
037 init();
038 }
039
040 protected void init(){
041 initLocalData();
042 initListeners();
043 }//protected void init()
044
045 protected void initLocalData(){
046 backUrls = new LinkedList();
047 forwardUrls = new LinkedList();
048 try{
049 backAction = new BackAction();
050 forwardAction = new ForwardAction();
051 }catch(IOException ioe){
052 Err.prln("Resource mising! Is your classpath OK?");
053 ioe.printStackTrace(Err.getPrintWriter());
054 }
055 }//protected void initLocalData()
056
057 protected void initListeners(){
058 addHyperlinkListener(new HyperlinkListener() {
059 public void hyperlinkUpdate(HyperlinkEvent e){
060 if (e.getEventType() == HyperlinkEvent.EventType.ACTIVATED){
061 if (e instanceof HTMLFrameHyperlinkEvent) {
062 HTMLFrameHyperlinkEvent evt = (HTMLFrameHyperlinkEvent)e;
063 HTMLDocument doc = (HTMLDocument)getDocument();
064 doc.processHTMLFrameHyperlinkEvent(evt);
065 }else{
066 try {
067 backUrls.addLast(getPage());
068 forwardUrls.clear();
069 setPage(e.getURL().toExternalForm());
070 }catch (Throwable t){
071 t.printStackTrace(Err.getPrintWriter());
072 }
073 }
074 }else if(e.getEventType() == HyperlinkEvent.EventType.ENTERED){
075 fireStatusChanged(e.getURL().toExternalForm());
076 }else if(e.getEventType() == HyperlinkEvent.EventType.EXITED){
077 fireStatusChanged("");
078 }
079 }//public void hyperlinkUpdate(HyperlinkEvent e)
080 });
081 }//protected void initListeners()
082
083 public Action getForwardAction(){
084 return forwardAction;
085 }
086
087 public Action getBackAction(){
088 return backAction;
089 }
090
091 public void setPage(URL page) throws IOException{
092 try{
093 super.setPage(page);
094 }catch(Exception e){
095 fireStatusChanged(e.toString());
096 e.printStackTrace(Err.getPrintWriter());
097 }
098 updateEnableState();
099 }
100
101 class ForwardAction extends AbstractAction{
102 private ForwardAction() throws IOException{
103 super("Forward", MainFrame.getIcon("forward"));
104 }
105
106 public void actionPerformed(ActionEvent e){
107 backUrls.addLast(getPage());
108 try{
109 setPage((URL)forwardUrls.removeFirst());
110 }catch(IOException ioe){
111 ioe.printStackTrace(Err.getPrintWriter());
112 }
113 }
114 }//class ForwardAction extends AbstractAction
115
116 class BackAction extends AbstractAction{
117 private BackAction() throws IOException{
118 super("Back", MainFrame.getIcon("back"));
119 }
120
121 public void actionPerformed(ActionEvent e){
122 forwardUrls.addFirst(getPage());
123 try{
124 setPage((URL)backUrls.removeLast());
125 }catch(IOException ioe){
126 ioe.printStackTrace(Err.getPrintWriter());
127 }
128 }
129 }//class ForwardAction extends AbstractAction
130
131
132 /**
133 * Updates the enabled/disabled state for the back/forward actions
134 */
135 protected void updateEnableState(){
136 forwardAction.setEnabled(!forwardUrls.isEmpty());
137 backAction.setEnabled(!backUrls.isEmpty());
138 }
139 public synchronized void removeStatusListener(StatusListener l) {
140 if (statusListeners != null && statusListeners.contains(l)) {
141 Vector v = (Vector) statusListeners.clone();
142 v.removeElement(l);
143 statusListeners = v;
144 }
145 }
146 public synchronized void addStatusListener(StatusListener l) {
147 Vector v = statusListeners == null ? new Vector(2) : (Vector) statusListeners.clone();
148 if (!v.contains(l)) {
149 v.addElement(l);
150 statusListeners = v;
151 }
152 }
153
154 protected LinkedList backUrls;
155 protected LinkedList forwardUrls;
156 protected Action backAction;
157 protected Action forwardAction;
158 private transient Vector statusListeners;
159 protected void fireStatusChanged(String e) {
160 if (statusListeners != null) {
161 Vector listeners = statusListeners;
162 int count = listeners.size();
163 for (int i = 0; i < count; i++) {
164 ((StatusListener) listeners.elementAt(i)).statusChanged(e);
165 }
166 }
167 }
168 }//public class XJEditorPane extends JEditorPane
|