bump product version to 4.2.0.1
[LibreOffice.git] / toolkit / test / accessibility / HelpWindow.java
blob4d15114874ce21860eba52211e0a8e51398bf324
1 /*
2 * This file is part of the LibreOffice project.
4 * This Source Code Form is subject to the terms of the Mozilla Public
5 * License, v. 2.0. If a copy of the MPL was not distributed with this
6 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 * This file incorporates work covered by the following license notice:
10 * Licensed to the Apache Software Foundation (ASF) under one or more
11 * contributor license agreements. See the NOTICE file distributed
12 * with this work for additional information regarding copyright
13 * ownership. The ASF licenses this file to you under the Apache
14 * License, Version 2.0 (the "License"); you may not use this file
15 * except in compliance with the License. You may obtain a copy of
16 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
19 import javax.swing.JFrame;
20 import javax.swing.JScrollPane;
21 import javax.swing.JEditorPane;
22 import javax.swing.JButton;
23 import java.net.URL;
24 import javax.swing.event.HyperlinkListener;
25 import javax.swing.event.HyperlinkEvent;
26 import java.net.MalformedURLException;
27 import java.io.File;
28 import java.awt.event.WindowAdapter;
29 import java.awt.event.WindowEvent;
30 import java.awt.GridBagLayout;
31 import java.awt.GridBagConstraints;
32 import java.awt.event.ActionListener;
33 import java.util.LinkedList;
35 class HelpWindow
36 implements ActionListener
38 public static synchronized HelpWindow Instance ()
40 if (maInstance == null)
41 maInstance = new HelpWindow();
42 return maInstance;
45 public void loadFile (String sFilename)
47 File aFile = new File (sFilename);
48 try
50 loadURL (aFile.toURI().toURL());
52 catch (MalformedURLException e)
54 e.printStackTrace (System.err);
57 public void loadURL (String sURL)
59 try
61 loadURL (new URL (sURL));
63 catch (MalformedURLException e)
65 e.printStackTrace (System.err);
72 public void loadURL (URL aURL)
74 maHistory.addLast (aURL);
75 selectHistoryPage (maHistory.size()-1);
76 maFrame.toFront ();
82 private HelpWindow ()
84 try
86 maCurrentHistoryEntry = -1;
87 maHistory = new LinkedList<URL>();
89 maFrame = new JFrame ();
90 maFrame.addWindowListener (new WindowAdapter ()
92 public void windowClosing (WindowEvent e)
94 maInstance = null;
96 });
97 maContent = createContentWidget();
99 maFrame.getContentPane().setLayout (new GridBagLayout());
100 GridBagConstraints aConstraints = new GridBagConstraints ();
101 aConstraints.gridx = 0;
102 aConstraints.gridy = 0;
103 aConstraints.gridwidth = 3;
104 aConstraints.weightx = 1;
105 aConstraints.weighty = 1;
106 aConstraints.fill = GridBagConstraints.BOTH;
107 maFrame.getContentPane().add (new JScrollPane (maContent), aConstraints);
109 aConstraints = new GridBagConstraints();
110 aConstraints.gridx = 0;
111 aConstraints.gridy = 1;
112 maPrevButton = new JButton ("Prev");
113 maFrame.getContentPane().add (maPrevButton, aConstraints);
114 maPrevButton.addActionListener (this);
116 aConstraints = new GridBagConstraints();
117 aConstraints.gridx = 1;
118 aConstraints.gridy = 1;
119 maNextButton = new JButton ("Next");
120 maFrame.getContentPane().add (maNextButton, aConstraints);
121 maNextButton.addActionListener (this);
123 aConstraints = new GridBagConstraints();
124 aConstraints.gridx = 2;
125 aConstraints.gridy = 1;
126 aConstraints.anchor = GridBagConstraints.EAST;
127 JButton aButton = new JButton ("Close");
128 maFrame.getContentPane().add (aButton, aConstraints);
129 aButton.addActionListener (this);
131 maFrame.setSize (600,400);
132 maFrame.setVisible (true);
134 catch (Exception e)
138 public void actionPerformed (java.awt.event.ActionEvent e)
140 if (e.getActionCommand().equals("Prev"))
142 selectHistoryPage (maCurrentHistoryEntry - 1);
144 else if (e.getActionCommand().equals("Next"))
146 selectHistoryPage (maCurrentHistoryEntry + 1);
148 else if (e.getActionCommand().equals("Close"))
150 maFrame.dispose ();
151 maInstance = null;
155 private JEditorPane createContentWidget ()
157 JEditorPane aContent = new JEditorPane ();
158 aContent.setEditable (false);
159 aContent.addHyperlinkListener (new HyperlinkListener()
161 public void hyperlinkUpdate (HyperlinkEvent e)
163 if (e.getEventType() == HyperlinkEvent.EventType.ACTIVATED)
164 HelpWindow.Instance().loadURL (e.getURL());
167 return aContent;
170 private void selectHistoryPage (int i)
172 if (i < 0)
173 i = 0;
174 else if (i >= maHistory.size()-1)
175 i = maHistory.size()-1;
176 if (i != maCurrentHistoryEntry)
178 URL aURL = maHistory.get (i);
181 maContent.setPage (aURL);
183 catch (java.io.IOException ex)
185 ex.printStackTrace(System.err);
188 maCurrentHistoryEntry = i;
191 maPrevButton.setEnabled (maCurrentHistoryEntry > 0);
192 maNextButton.setEnabled (maCurrentHistoryEntry < maHistory.size()-1);
195 private static HelpWindow maInstance = null;
196 private JFrame maFrame;
197 private JEditorPane maContent;
198 private LinkedList<URL> maHistory;
199 private int maCurrentHistoryEntry;
200 private JButton maPrevButton;
201 private JButton maNextButton;