Branch libreoffice-5-0-4
[LibreOffice.git] / toolkit / test / accessibility / HelpWindow.java
blob00c93406766f5e13483bfa9d0a1a6e29de94eb8a
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);
58 private void loadURL (URL aURL)
60 maHistory.addLast (aURL);
61 selectHistoryPage (maHistory.size()-1);
62 maFrame.toFront ();
68 private HelpWindow ()
70 try
72 maCurrentHistoryEntry = -1;
73 maHistory = new LinkedList<URL>();
75 maFrame = new JFrame ();
76 maFrame.addWindowListener (new WindowAdapter ()
78 @Override
79 public void windowClosing (WindowEvent e)
81 maInstance = null;
83 });
84 maContent = createContentWidget();
86 maFrame.getContentPane().setLayout (new GridBagLayout());
87 GridBagConstraints aConstraints = new GridBagConstraints ();
88 aConstraints.gridx = 0;
89 aConstraints.gridy = 0;
90 aConstraints.gridwidth = 3;
91 aConstraints.weightx = 1;
92 aConstraints.weighty = 1;
93 aConstraints.fill = GridBagConstraints.BOTH;
94 maFrame.getContentPane().add (new JScrollPane (maContent), aConstraints);
96 aConstraints = new GridBagConstraints();
97 aConstraints.gridx = 0;
98 aConstraints.gridy = 1;
99 maPrevButton = new JButton ("Prev");
100 maFrame.getContentPane().add (maPrevButton, aConstraints);
101 maPrevButton.addActionListener (this);
103 aConstraints = new GridBagConstraints();
104 aConstraints.gridx = 1;
105 aConstraints.gridy = 1;
106 maNextButton = new JButton ("Next");
107 maFrame.getContentPane().add (maNextButton, aConstraints);
108 maNextButton.addActionListener (this);
110 aConstraints = new GridBagConstraints();
111 aConstraints.gridx = 2;
112 aConstraints.gridy = 1;
113 aConstraints.anchor = GridBagConstraints.EAST;
114 JButton aButton = new JButton ("Close");
115 maFrame.getContentPane().add (aButton, aConstraints);
116 aButton.addActionListener (this);
118 maFrame.setSize (600,400);
119 maFrame.setVisible (true);
121 catch (Exception e)
125 public void actionPerformed (java.awt.event.ActionEvent e)
127 if (e.getActionCommand().equals("Prev"))
129 selectHistoryPage (maCurrentHistoryEntry - 1);
131 else if (e.getActionCommand().equals("Next"))
133 selectHistoryPage (maCurrentHistoryEntry + 1);
135 else if (e.getActionCommand().equals("Close"))
137 maFrame.dispose ();
138 maInstance = null;
142 private JEditorPane createContentWidget ()
144 JEditorPane aContent = new JEditorPane ();
145 aContent.setEditable (false);
146 aContent.addHyperlinkListener (new HyperlinkListener()
148 public void hyperlinkUpdate (HyperlinkEvent e)
150 if (e.getEventType() == HyperlinkEvent.EventType.ACTIVATED)
151 HelpWindow.Instance().loadURL (e.getURL());
154 return aContent;
157 private void selectHistoryPage (int i)
159 if (i < 0)
160 i = 0;
161 else if (i >= maHistory.size()-1)
162 i = maHistory.size()-1;
163 if (i != maCurrentHistoryEntry)
165 URL aURL = maHistory.get (i);
168 maContent.setPage (aURL);
170 catch (java.io.IOException ex)
172 ex.printStackTrace(System.err);
175 maCurrentHistoryEntry = i;
178 maPrevButton.setEnabled (maCurrentHistoryEntry > 0);
179 maNextButton.setEnabled (maCurrentHistoryEntry < maHistory.size()-1);
182 private static HelpWindow maInstance = null;
183 private JFrame maFrame;
184 private JEditorPane maContent;
185 private LinkedList<URL> maHistory;
186 private int maCurrentHistoryEntry;
187 private JButton maPrevButton;
188 private JButton maNextButton;