1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 package org.openoffice.accessibility.awb;
23 
24 import javax.swing.JFrame;
25 import javax.swing.JScrollPane;
26 import javax.swing.JEditorPane;
27 import javax.swing.JButton;
28 import java.net.URL;
29 import javax.swing.event.HyperlinkListener;
30 import javax.swing.event.HyperlinkEvent;
31 import java.net.MalformedURLException;
32 import java.io.IOException;
33 import java.io.File;
34 import java.awt.event.WindowAdapter;
35 import java.awt.event.WindowEvent;
36 import java.awt.GridBagLayout;
37 import java.awt.GridBagConstraints;
38 import java.awt.event.ActionListener;
39 import java.util.LinkedList;
40 
41 public class HelpWindow
42     implements ActionListener
43 {
Instance()44     public static synchronized HelpWindow Instance ()
45     {
46         if (maInstance == null)
47             maInstance = new HelpWindow();
48         return maInstance;
49     }
50 
loadFile(String sFilename)51     public void loadFile (String sFilename)
52     {
53         File aFile = new File (sFilename);
54         try
55         {
56             loadURL (aFile.toURL());
57         }
58         catch (MalformedURLException e)
59         {
60             e.printStackTrace (System.err);
61         }
62     }
loadURL(String sURL)63     public void loadURL (String sURL)
64     {
65         try
66         {
67             loadURL (new URL (sURL));
68         }
69         catch (MalformedURLException e)
70         {
71             e.printStackTrace (System.err);
72         }
73     }
74 
75 
76 
77 
loadURL(URL aURL)78     public void loadURL (URL aURL)
79     {
80         maHistory.addLast (aURL);
81         selectHistoryPage (maHistory.size()-1);
82         maFrame.toFront ();
83     }
84 
85 
86 
87 
HelpWindow()88     private HelpWindow ()
89     {
90         try
91         {
92             maCurrentHistoryEntry = -1;
93             maHistory = new LinkedList();
94 
95             maFrame = new JFrame ();
96             maFrame.addWindowListener (new WindowAdapter ()
97                 {
98                     public void windowClosing (WindowEvent e)
99                     {
100                         maInstance = null;
101                     }
102                 });
103             maContent = createContentWidget();
104 
105             maFrame.getContentPane().setLayout (new GridBagLayout());
106             GridBagConstraints aConstraints = new GridBagConstraints ();
107             aConstraints.gridx = 0;
108             aConstraints.gridy = 0;
109             aConstraints.gridwidth = 3;
110             aConstraints.weightx = 1;
111             aConstraints.weighty = 1;
112             aConstraints.fill = GridBagConstraints.BOTH;
113             maFrame.getContentPane().add (new JScrollPane (maContent), aConstraints);
114 
115             aConstraints = new GridBagConstraints();
116             aConstraints.gridx = 0;
117             aConstraints.gridy = 1;
118             maPrevButton = new JButton ("Prev");
119             maFrame.getContentPane().add (maPrevButton, aConstraints);
120             maPrevButton.addActionListener (this);
121 
122             aConstraints = new GridBagConstraints();
123             aConstraints.gridx = 1;
124             aConstraints.gridy = 1;
125             maNextButton = new JButton ("Next");
126             maFrame.getContentPane().add (maNextButton, aConstraints);
127             maNextButton.addActionListener (this);
128 
129             aConstraints = new GridBagConstraints();
130             aConstraints.gridx = 2;
131             aConstraints.gridy = 1;
132             aConstraints.anchor = GridBagConstraints.EAST;
133             JButton aButton = new JButton ("Close");
134             maFrame.getContentPane().add (aButton, aConstraints);
135             aButton.addActionListener (this);
136 
137             maFrame.setSize (600,400);
138             maFrame.setVisible (true);
139         }
140         catch (Exception e)
141         {}
142     }
143 
actionPerformed(java.awt.event.ActionEvent e)144     public void actionPerformed (java.awt.event.ActionEvent e)
145     {
146         if (e.getActionCommand().equals("Prev"))
147         {
148             selectHistoryPage (maCurrentHistoryEntry - 1);
149         }
150         else if (e.getActionCommand().equals("Next"))
151         {
152             selectHistoryPage (maCurrentHistoryEntry + 1);
153         }
154         else if (e.getActionCommand().equals("Close"))
155         {
156             maFrame.dispose ();
157             maInstance = null;
158         }
159     }
160 
createContentWidget()161     private JEditorPane createContentWidget ()
162     {
163         JEditorPane aContent = new JEditorPane ();
164         aContent.setEditable (false);
165         aContent.addHyperlinkListener (new HyperlinkListener()
166             {
167                 public void hyperlinkUpdate (HyperlinkEvent e)
168                 {
169                     if (e.getEventType() == HyperlinkEvent.EventType.ACTIVATED)
170                         HelpWindow.Instance().loadURL (e.getURL());
171                 }
172             });
173         return aContent;
174     }
175 
selectHistoryPage(int i)176     private void selectHistoryPage (int i)
177     {
178         if (i < 0)
179             i = 0;
180         else if (i >= maHistory.size()-1)
181             i = maHistory.size()-1;
182         if (i != maCurrentHistoryEntry)
183         {
184             URL aURL = (URL)maHistory.get (i);
185             try
186             {
187                 maContent.setPage (aURL);
188             }
189             catch (java.io.IOException ex)
190             {
191                 ex.printStackTrace(System.err);
192             }
193 
194             maCurrentHistoryEntry = i;
195         }
196 
197         maPrevButton.setEnabled (maCurrentHistoryEntry > 0);
198         maNextButton.setEnabled (maCurrentHistoryEntry < maHistory.size()-1);
199     }
200 
201     private static HelpWindow maInstance = null;
202     private JFrame maFrame;
203     private JEditorPane maContent;
204     private LinkedList maHistory;
205     private int maCurrentHistoryEntry;
206     private JButton maPrevButton;
207     private JButton maNextButton;
208 }
209