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 import javax.swing.JFrame;
23 import javax.swing.JScrollPane;
24 import javax.swing.JEditorPane;
25 import javax.swing.JButton;
26 import java.net.URL;
27 import javax.swing.event.HyperlinkListener;
28 import javax.swing.event.HyperlinkEvent;
29 import java.net.MalformedURLException;
30 import java.io.IOException;
31 import java.io.File;
32 import java.awt.event.WindowAdapter;
33 import java.awt.event.WindowEvent;
34 import java.awt.GridBagLayout;
35 import java.awt.GridBagConstraints;
36 import java.awt.event.ActionListener;
37 import java.util.LinkedList;
38 
39 class HelpWindow
40     implements ActionListener
41 {
Instance()42     public static synchronized HelpWindow Instance ()
43     {
44         if (maInstance == null)
45             maInstance = new HelpWindow();
46         return maInstance;
47     }
48 
loadFile(String sFilename)49     public void loadFile (String sFilename)
50     {
51         File aFile = new File (sFilename);
52         try
53         {
54             loadURL (aFile.toURL());
55         }
56         catch (MalformedURLException e)
57         {
58             e.printStackTrace (System.err);
59         }
60     }
loadURL(String sURL)61     public void loadURL (String sURL)
62     {
63         try
64         {
65             loadURL (new URL (sURL));
66         }
67         catch (MalformedURLException e)
68         {
69             e.printStackTrace (System.err);
70         }
71     }
72 
73 
74 
75 
loadURL(URL aURL)76     public void loadURL (URL aURL)
77     {
78         maHistory.addLast (aURL);
79         selectHistoryPage (maHistory.size()-1);
80         maFrame.toFront ();
81     }
82 
83 
84 
85 
HelpWindow()86     private HelpWindow ()
87     {
88         try
89         {
90             maCurrentHistoryEntry = -1;
91             maHistory = new LinkedList();
92 
93             maFrame = new JFrame ();
94             maFrame.addWindowListener (new WindowAdapter ()
95                 {
96                     public void windowClosing (WindowEvent e)
97                     {
98                         maInstance = null;
99                     }
100                 });
101             maContent = createContentWidget();
102 
103             maFrame.getContentPane().setLayout (new GridBagLayout());
104             GridBagConstraints aConstraints = new GridBagConstraints ();
105             aConstraints.gridx = 0;
106             aConstraints.gridy = 0;
107             aConstraints.gridwidth = 3;
108             aConstraints.weightx = 1;
109             aConstraints.weighty = 1;
110             aConstraints.fill = GridBagConstraints.BOTH;
111             maFrame.getContentPane().add (new JScrollPane (maContent), aConstraints);
112 
113             aConstraints = new GridBagConstraints();
114             aConstraints.gridx = 0;
115             aConstraints.gridy = 1;
116             maPrevButton = new JButton ("Prev");
117             maFrame.getContentPane().add (maPrevButton, aConstraints);
118             maPrevButton.addActionListener (this);
119 
120             aConstraints = new GridBagConstraints();
121             aConstraints.gridx = 1;
122             aConstraints.gridy = 1;
123             maNextButton = new JButton ("Next");
124             maFrame.getContentPane().add (maNextButton, aConstraints);
125             maNextButton.addActionListener (this);
126 
127             aConstraints = new GridBagConstraints();
128             aConstraints.gridx = 2;
129             aConstraints.gridy = 1;
130             aConstraints.anchor = GridBagConstraints.EAST;
131             JButton aButton = new JButton ("Close");
132             maFrame.getContentPane().add (aButton, aConstraints);
133             aButton.addActionListener (this);
134 
135             maFrame.setSize (600,400);
136             maFrame.setVisible (true);
137         }
138         catch (Exception e)
139         {}
140     }
141 
actionPerformed(java.awt.event.ActionEvent e)142     public void actionPerformed (java.awt.event.ActionEvent e)
143     {
144         if (e.getActionCommand().equals("Prev"))
145         {
146             selectHistoryPage (maCurrentHistoryEntry - 1);
147         }
148         else if (e.getActionCommand().equals("Next"))
149         {
150             selectHistoryPage (maCurrentHistoryEntry + 1);
151         }
152         else if (e.getActionCommand().equals("Close"))
153         {
154             maFrame.dispose ();
155             maInstance = null;
156         }
157     }
158 
createContentWidget()159     private JEditorPane createContentWidget ()
160     {
161         JEditorPane aContent = new JEditorPane ();
162         aContent.setEditable (false);
163         aContent.addHyperlinkListener (new HyperlinkListener()
164             {
165                 public void hyperlinkUpdate (HyperlinkEvent e)
166                 {
167                     if (e.getEventType() == HyperlinkEvent.EventType.ACTIVATED)
168                         HelpWindow.Instance().loadURL (e.getURL());
169                 }
170             });
171         return aContent;
172     }
173 
selectHistoryPage(int i)174     private void selectHistoryPage (int i)
175     {
176         if (i < 0)
177             i = 0;
178         else if (i >= maHistory.size()-1)
179             i = maHistory.size()-1;
180         if (i != maCurrentHistoryEntry)
181         {
182             URL aURL = (URL)maHistory.get (i);
183             try
184             {
185                 maContent.setPage (aURL);
186             }
187             catch (java.io.IOException ex)
188             {
189                 ex.printStackTrace(System.err);
190             }
191 
192             maCurrentHistoryEntry = i;
193         }
194 
195         maPrevButton.setEnabled (maCurrentHistoryEntry > 0);
196         maNextButton.setEnabled (maCurrentHistoryEntry < maHistory.size()-1);
197     }
198 
199     private static HelpWindow maInstance = null;
200     private JFrame maFrame;
201     private JEditorPane maContent;
202     private LinkedList maHistory;
203     private int maCurrentHistoryEntry;
204     private JButton maPrevButton;
205     private JButton maNextButton;
206 }
207