merged tag ooo/DEV300_m102
[LibreOffice.git] / toolkit / test / accessibility / HelpWindow.java
blob74627ad997613b8bb973b30e9ca59889c198d382
1 import javax.swing.JFrame;
2 import javax.swing.JScrollPane;
3 import javax.swing.JEditorPane;
4 import javax.swing.JButton;
5 import java.net.URL;
6 import javax.swing.event.HyperlinkListener;
7 import javax.swing.event.HyperlinkEvent;
8 import java.net.MalformedURLException;
9 import java.io.IOException;
10 import java.io.File;
11 import java.awt.event.WindowAdapter;
12 import java.awt.event.WindowEvent;
13 import java.awt.GridBagLayout;
14 import java.awt.GridBagConstraints;
15 import java.awt.event.ActionListener;
16 import java.util.LinkedList;
18 class HelpWindow
19 implements ActionListener
21 public static synchronized HelpWindow Instance ()
23 if (maInstance == null)
24 maInstance = new HelpWindow();
25 return maInstance;
28 public void loadFile (String sFilename)
30 File aFile = new File (sFilename);
31 try
33 loadURL (aFile.toURL());
35 catch (MalformedURLException e)
37 e.printStackTrace (System.err);
40 public void loadURL (String sURL)
42 try
44 loadURL (new URL (sURL));
46 catch (MalformedURLException e)
48 e.printStackTrace (System.err);
55 public void loadURL (URL aURL)
57 maHistory.addLast (aURL);
58 selectHistoryPage (maHistory.size()-1);
59 maFrame.toFront ();
65 private HelpWindow ()
67 try
69 maCurrentHistoryEntry = -1;
70 maHistory = new LinkedList();
72 maFrame = new JFrame ();
73 maFrame.addWindowListener (new WindowAdapter ()
75 public void windowClosing (WindowEvent e)
77 maInstance = null;
79 });
80 maContent = createContentWidget();
82 maFrame.getContentPane().setLayout (new GridBagLayout());
83 GridBagConstraints aConstraints = new GridBagConstraints ();
84 aConstraints.gridx = 0;
85 aConstraints.gridy = 0;
86 aConstraints.gridwidth = 3;
87 aConstraints.weightx = 1;
88 aConstraints.weighty = 1;
89 aConstraints.fill = GridBagConstraints.BOTH;
90 maFrame.getContentPane().add (new JScrollPane (maContent), aConstraints);
92 aConstraints = new GridBagConstraints();
93 aConstraints.gridx = 0;
94 aConstraints.gridy = 1;
95 maPrevButton = new JButton ("Prev");
96 maFrame.getContentPane().add (maPrevButton, aConstraints);
97 maPrevButton.addActionListener (this);
99 aConstraints = new GridBagConstraints();
100 aConstraints.gridx = 1;
101 aConstraints.gridy = 1;
102 maNextButton = new JButton ("Next");
103 maFrame.getContentPane().add (maNextButton, aConstraints);
104 maNextButton.addActionListener (this);
106 aConstraints = new GridBagConstraints();
107 aConstraints.gridx = 2;
108 aConstraints.gridy = 1;
109 aConstraints.anchor = GridBagConstraints.EAST;
110 JButton aButton = new JButton ("Close");
111 maFrame.getContentPane().add (aButton, aConstraints);
112 aButton.addActionListener (this);
114 maFrame.setSize (600,400);
115 maFrame.setVisible (true);
117 catch (Exception e)
121 public void actionPerformed (java.awt.event.ActionEvent e)
123 if (e.getActionCommand().equals("Prev"))
125 selectHistoryPage (maCurrentHistoryEntry - 1);
127 else if (e.getActionCommand().equals("Next"))
129 selectHistoryPage (maCurrentHistoryEntry + 1);
131 else if (e.getActionCommand().equals("Close"))
133 maFrame.dispose ();
134 maInstance = null;
138 private JEditorPane createContentWidget ()
140 JEditorPane aContent = new JEditorPane ();
141 aContent.setEditable (false);
142 aContent.addHyperlinkListener (new HyperlinkListener()
144 public void hyperlinkUpdate (HyperlinkEvent e)
146 if (e.getEventType() == HyperlinkEvent.EventType.ACTIVATED)
147 HelpWindow.Instance().loadURL (e.getURL());
150 return aContent;
153 private void selectHistoryPage (int i)
155 if (i < 0)
156 i = 0;
157 else if (i >= maHistory.size()-1)
158 i = maHistory.size()-1;
159 if (i != maCurrentHistoryEntry)
161 URL aURL = (URL)maHistory.get (i);
164 maContent.setPage (aURL);
166 catch (java.io.IOException ex)
168 ex.printStackTrace(System.err);
171 maCurrentHistoryEntry = i;
174 maPrevButton.setEnabled (maCurrentHistoryEntry > 0);
175 maNextButton.setEnabled (maCurrentHistoryEntry < maHistory.size()-1);
178 private static HelpWindow maInstance = null;
179 private JFrame maFrame;
180 private JEditorPane maContent;
181 private LinkedList maHistory;
182 private int maCurrentHistoryEntry;
183 private JButton maPrevButton;
184 private JButton maNextButton;