1
2
3
4
5
6
7
8
9
10
11
12
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
35
36
37
38
39
40 public class HelpFrame extends JFrame {
41
42
43 private static final String HOW_TO_COUNT_FILENAME = "howToCount";
44
45
46 private static final String PREF_REFERENCE_FILENAME = "prefReference";
47
48
49 private static final String COMMON_RULES_FILENAME = "commonRules";
50
51
52 private JTextPane textPane;
53
54
55 private WindowComponent currHelpEnum;
56
57
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
71 super.addWindowListener(new WindowAdapter() {
72
73 @Override
74 public void windowClosing(WindowEvent event) {
75 setVisible(false);
76 }
77 });
78
79 LocaleExt.registerComponent(new LocaleExt.LocaleExec() {
80
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
91
92
93
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 }