View Javadoc
1   /**
2    * Copyright 2011, Aiki IT, FotoRenamer
3    * <p/>
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * <p/>
8    * http://www.apache.org/licenses/LICENSE-2.0
9    * <p/>
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package de.aikiit.fotorenamer.gui;
17  
18  import de.aikiit.fotorenamer.util.ComponentGaugeUtil;
19  import org.apache.logging.log4j.LogManager;
20  import org.apache.logging.log4j.Logger;
21  
22  import javax.swing.*;
23  import java.awt.*;
24  import java.awt.event.ActionEvent;
25  import java.awt.event.ActionListener;
26  import java.io.File;
27  
28  import static de.aikiit.fotorenamer.util.LocalizationHelper.getBundleString;
29  import static de.aikiit.fotorenamer.util.LocalizationHelper.getLanguage;
30  import static de.aikiit.fotorenamer.util.LocalizationHelper.getParameterizedBundleString;
31  
32  /**
33   * This class represents a help window component. It loads a HTML-page to show
34   * as application help.
35   *
36   * @author hirsch, 20.10.2003
37   * @version 2004-01-08
38   */
39  class HelpWindow extends JFrame implements ActionListener {
40      /**
41       * Logger for this class.
42       */
43      private static final Logger LOG = LogManager.getLogger(HelpWindow.class);
44      /**
45       * End button needs to be visible inside the class to perform application
46       * exit.
47       */
48      private JButton endButton = null;
49  
50      /**
51       * Relative location of the UI's help page.
52       */
53      private static final String HTML_HELP_LOCATION = File.separator + "html"
54              + File.separator;
55  
56      /**
57       * Creates a HelpWindow, initializes its components but does
58       * <strong>not</strong> show the window.
59       */
60      HelpWindow() {
61          SwingUtilities.invokeLater(this::init);
62      }
63  
64      /**
65       * Initialize UI-compontens and make them visible.
66       */
67      private void init() {
68          this.setTitle(getBundleString(
69                  "fotorenamer.ui.help.title"));
70          this.setResizable(false);
71          JPanel top = new JPanel(new FlowLayout());
72          JPanel bottom = new JPanel(new FlowLayout());
73  
74          JEditorPane textfield = new JEditorPane();
75          textfield.setContentType("text/html");
76          textfield.setEditable(false);
77          loadHelpContents(top, textfield);
78  
79          JScrollPane rollpanel = new JScrollPane(top);
80          rollpanel.setPreferredSize(new Dimension(350, 300));
81          rollpanel.setVerticalScrollBarPolicy(
82                  JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
83  
84          this.endButton = new JButton(getBundleString(
85                  "fotorenamer.ui.help.close"));
86          this.endButton.addActionListener(this);
87          this.endButton.setMnemonic(getBundleString("fotorenamer.ui.help.close.mnemonic").charAt(0));
88          bottom.add(this.endButton);
89  
90          this.getContentPane().setLayout(new BorderLayout());
91          this.getContentPane().add(rollpanel, BorderLayout.NORTH);
92          this.getContentPane().add(bottom, BorderLayout.CENTER);
93  
94          this.pack();
95          ComponentGaugeUtil.makeCentered(this);
96  
97          // let button get the focus, needs to be run in the and and async
98          SwingUtilities.invokeLater(() -> {
99              getRootPane().setDefaultButton(endButton);
100             endButton.requestFocusInWindow();
101         });
102 
103         LOG.debug("HelpWindow init done.");
104     }
105 
106     /**
107      * Load page contents depending on available languages (fallback is English).
108      *
109      * @param base      base panel.
110      * @param textfield where to put the contents to.
111      */
112     private void loadHelpContents(final JPanel base, final JEditorPane textfield) {
113         try {
114             String helpPageLocation = HTML_HELP_LOCATION;
115 
116             if (getLanguage().startsWith("de")) {
117                 helpPageLocation += "hilfe.html";
118             } else {
119                 helpPageLocation += "help.html";
120             }
121 
122             textfield.setPage(HelpWindow.class.getResource(helpPageLocation));
123             base.add(textfield);
124         } catch (Exception e) {
125             base.setLayout(new GridLayout(1, 1));
126             base.add(new JLabel(getParameterizedBundleString("fotorenamer.ui.help.error", e.getMessage(), e.getClass().getSimpleName())));
127         }
128     }
129 
130     /**
131      * Make this component react to close button.
132      *
133      * @param event Event to react on in this UI-component.
134      */
135     public final void actionPerformed(final ActionEvent event) {
136         if (event.getSource() == this.endButton) {
137             LOG.debug("Disabling visibility of helpWindow.");
138             setVisible(false);
139         }
140     }
141 }