View Javadoc

1   /**
2    * This program is free software: you can redistribute it and/or modify
3    * it under the terms of the GNU General Public License as published by
4    * the Free Software Foundation, version 3.
5    *
6    * This program is distributed in the hope that it will be useful,
7    * but WITHOUT ANY WARRANTY; without even the implied warranty of
8    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9    * GNU General Public License for more details.
10   *
11   * You should have received a copy of the GNU General Public License
12   * along with this program. If not, see <http://www.gnu.org/licenses/>.
13   */
14  
15  package net.curre.prefcount.gui;
16  
17  import java.awt.BorderLayout;
18  import java.awt.Dimension;
19  import java.awt.Insets;
20  import java.awt.event.WindowAdapter;
21  import java.awt.event.WindowEvent;
22  import java.io.IOException;
23  import java.io.InputStream;
24  import javax.swing.JFrame;
25  import javax.swing.JScrollPane;
26  import javax.swing.JTextPane;
27  
28  import net.curre.prefcount.App;
29  import net.curre.prefcount.PrefCountRegistry;
30  import net.curre.prefcount.gui.type.WindowComponent;
31  import net.curre.prefcount.util.LocaleExt;
32  
33  /**
34   * Object of this class represents a frame with help infomation.
35   * <p/>
36   * Created date: Jun 18, 2008
37   *
38   * @author Yevgeny Nyden
39   */
40  public class HelpFrame extends JFrame {
41  
42    /** Name of the how to count help file (w/o extention). */
43    private static final String HOW_TO_COUNT_FILENAME = "howToCount";
44  
45    /** Name of the preference reference help file (w/o extention). */
46    private static final String PREF_REFERENCE_FILENAME = "prefReference";
47  
48    /** Name of the common rules help file (w/o extention). */
49    private static final String COMMON_RULES_FILENAME = "commonRules";
50  
51    /** Reference to the text pane object. */
52    private JTextPane textPane;
53  
54    /** Current help type enum. */
55    private WindowComponent currHelpEnum;
56  
57    /** Constructs a new <code>HelpFrame</code> object. */
58    public HelpFrame() {
59  
60      super.setLayout(new BorderLayout());
61  
62      this.textPane = new JTextPane();
63      this.textPane.setMargin(new Insets(0, 20, 20, 10));
64      this.textPane.setEditable(false);
65  
66      JScrollPane scrollPane = new JScrollPane(this.textPane);
67      super.setPreferredSize(new Dimension(600, 600));
68      super.add(scrollPane, BorderLayout.CENTER);
69  
70      // close action only makes the frame not visible
71      super.addWindowListener(new WindowAdapter() {
72        /** {@inheritDoc} */
73        @Override
74        public void windowClosing(WindowEvent event) {
75          setVisible(false);
76        }
77      });
78  
79      LocaleExt.registerComponent(new LocaleExt.LocaleExec() {
80        /** {@inheritDoc} */
81        public void doChange() {
82          if (HelpFrame.this.isVisible() && HelpFrame.this.currHelpEnum != null) {
83            refreshText(HelpFrame.this.currHelpEnum);
84          }
85        }
86      }, "HELP_FRAME_KEY");
87    }
88  
89    /**
90     * Refreshes the title and the text.
91     *
92     * @param itemEnum enum that represents type of help.
93     * @throws IllegalArgumentException for an unsupported type of help.
94     */
95    public void refreshText(WindowComponent itemEnum) {
96  
97      final String title = LocaleExt.getString(itemEnum.getTextKey());
98      super.setTitle(title);
99  
100     String fileName;
101     switch (itemEnum) {
102       case HELP_COUNT_ACTION:
103       case HELP_COUNT_ACTION2:
104         fileName = HOW_TO_COUNT_FILENAME;
105         break;
106 
107       case HELP_PREF_ACTION:
108       case HELP_PREF_ACTION2:
109         fileName = PREF_REFERENCE_FILENAME;
110         break;
111 
112       case HELP_COMMON_ACTION:
113       case HELP_COMMON_ACTION2:
114         fileName = COMMON_RULES_FILENAME;
115         break;
116 
117       default:
118         throw new IllegalArgumentException("Unsupported help type: " + itemEnum);
119     }
120 
121     this.currHelpEnum = itemEnum;
122 
123     try {
124       this.textPane.setContentType("text/rtf");
125       String localeStr = PrefCountRegistry.getCurrentLocale().getLocale().getLanguage();
126       InputStream resource = App.class.getResourceAsStream("help/" + localeStr + "/" + fileName + ".rtf");
127       this.textPane.read(resource, null);
128       resource.close();
129 
130     } catch (IOException e) {
131       this.textPane.setText(LocaleExt.getString("pref.countHelp.error"));
132     }
133 
134     super.pack();
135     super.setVisible(true);
136   }
137 
138 }