1 /* 2 * This file is part of the LibreOffice project. 3 * 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/. 7 * 8 * This file incorporates work covered by the following license notice: 9 * 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 . 17 */ 18 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; 34 35 class HelpWindow 36 implements ActionListener 37 { Instance()38 public static synchronized HelpWindow Instance () 39 { 40 if (maInstance == null) 41 maInstance = new HelpWindow(); 42 return maInstance; 43 } 44 loadFile(String sFilename)45 public void loadFile (String sFilename) 46 { 47 File aFile = new File (sFilename); 48 try 49 { 50 loadURL (aFile.toURI().toURL()); 51 } 52 catch (MalformedURLException e) 53 { 54 e.printStackTrace (System.err); 55 } 56 } 57 loadURL(URL aURL)58 private void loadURL (URL aURL) 59 { 60 maHistory.addLast (aURL); 61 selectHistoryPage (maHistory.size()-1); 62 maFrame.toFront (); 63 } 64 65 66 67 HelpWindow()68 private HelpWindow () 69 { 70 try 71 { 72 maCurrentHistoryEntry = -1; 73 maHistory = new LinkedList<URL>(); 74 75 maFrame = new JFrame (); 76 maFrame.addWindowListener (new WindowAdapter () 77 { 78 @Override 79 public void windowClosing (WindowEvent e) 80 { 81 maInstance = null; 82 } 83 }); 84 maContent = createContentWidget(); 85 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); 95 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); 102 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); 109 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); 117 118 maFrame.setSize (600,400); 119 maFrame.setVisible (true); 120 } 121 catch (Exception e) 122 {} 123 } 124 actionPerformed(java.awt.event.ActionEvent e)125 public void actionPerformed (java.awt.event.ActionEvent e) 126 { 127 if (e.getActionCommand().equals("Prev")) 128 { 129 selectHistoryPage (maCurrentHistoryEntry - 1); 130 } 131 else if (e.getActionCommand().equals("Next")) 132 { 133 selectHistoryPage (maCurrentHistoryEntry + 1); 134 } 135 else if (e.getActionCommand().equals("Close")) 136 { 137 maFrame.dispose (); 138 maInstance = null; 139 } 140 } 141 createContentWidget()142 private JEditorPane createContentWidget () 143 { 144 JEditorPane aContent = new JEditorPane (); 145 aContent.setEditable (false); 146 aContent.addHyperlinkListener (new HyperlinkListener() 147 { 148 public void hyperlinkUpdate (HyperlinkEvent e) 149 { 150 if (e.getEventType() == HyperlinkEvent.EventType.ACTIVATED) 151 HelpWindow.Instance().loadURL (e.getURL()); 152 } 153 }); 154 return aContent; 155 } 156 selectHistoryPage(int i)157 private void selectHistoryPage (int i) 158 { 159 if (i < 0) 160 i = 0; 161 else if (i >= maHistory.size()-1) 162 i = maHistory.size()-1; 163 if (i != maCurrentHistoryEntry) 164 { 165 URL aURL = maHistory.get (i); 166 try 167 { 168 maContent.setPage (aURL); 169 } 170 catch (java.io.IOException ex) 171 { 172 ex.printStackTrace(System.err); 173 } 174 175 maCurrentHistoryEntry = i; 176 } 177 178 maPrevButton.setEnabled (maCurrentHistoryEntry > 0); 179 maNextButton.setEnabled (maCurrentHistoryEntry < maHistory.size()-1); 180 } 181 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; 189 } 190