KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > HelpWindow


1 import java.awt.*;
2 import java.awt.event.*;
3 import java.io.File JavaDoc;
4 import java.net.URL JavaDoc;
5 import javax.swing.*;
6 import javax.swing.border.*;
7 import javax.swing.event.*;
8
9 import java.io.IOException JavaDoc;
10
11 /** A window that displays html help.
12 * The help files are in a directory called help.
13 * The pages displayed depend on the language in use.
14 * The language specific help files are in sub-directories of the help
15 * directory named after the ISO language name, eg help/en/ .*/

16 public final class HelpWindow{
17     private static final int GAP=5; // Gap in pixels between components
18
private static final File JavaDoc HELP_DIR=new File JavaDoc("help"); // the root help file directory
19
private static final String JavaDoc DEFAULT="en"; // Default language
20

21     final JFrame frame;
22     //final JPanel btnPanel=new JPanel(new FlowLayout(FlowLayout.TRAILING,GAP,GAP));
23

24     //final JButton contentsBtn=new JButton(Lang.gs("contents"), Icons.CONTENTS);
25
//final JButton indexBtn=new JButton(Lang.gs("index"), Icons.INDEX);
26
//final JButton closeBtn=new JButton(Lang.gs("close"), Icons.CLOSE);
27

28     final JEditorPane html=new JEditorPane();
29     final JScrollPane scrollPane=new JScrollPane(html);
30     
31     final JEditorPane contents=new JEditorPane();
32     final JScrollPane contentsScrollPane=new JScrollPane(contents);
33     
34     final JSplitPane splitpane=new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,false,contentsScrollPane,scrollPane);
35     
36     private String JavaDoc currentLang="";
37     //final BtnClick click=new BtnClick();
38

39     /** Constructs a new help window.*/
40     public HelpWindow(){
41         frame=new JFrame(Lang.gs("help title"));
42         frame.setDefaultLookAndFeelDecorated(true);
43         frame.setIconImage(Icons.HELP.getImage());
44         frame.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
45         Container cp=frame.getContentPane();
46         cp.setLayout(new BorderLayout());
47         // Button Panel
48
//~ contentsBtn.setMnemonic(Lang.s2k("contents_mn"));
49
//~ indexBtn.setMnemonic(Lang.s2k("index_mn"));
50
//~ closeBtn.setMnemonic(Lang.s2k("close_mn"));
51
//~ contentsBtn.addActionListener(click);
52
//~ indexBtn.addActionListener(click);
53
//~ closeBtn.addActionListener(click);
54
//~ btnPanel.add(contentsBtn);
55
//~ btnPanel.add(indexBtn);
56
//~ btnPanel.add(closeBtn);
57
//~ btnPanel.setPreferredSize(btnPanel.getMinimumSize());
58
// Scroll pane
59
splitpane.setResizeWeight(0.3);
60         GUI.setScrollBars(scrollPane);
61         GUI.setScrollBars(contentsScrollPane);
62         html.setEditable(false);
63         html.setContentType("text/html");
64         html.addHyperlinkListener(new HyperLinkClick());
65         contents.setEditable(false);
66         contents.setContentType("text/html");
67         contents.addHyperlinkListener(new HyperLinkClick());
68         // add panel and pane to frame
69
//cp.add(btnPanel,BorderLayout.NORTH);
70
cp.add(splitpane,BorderLayout.CENTER);
71         frame.pack();
72         //load index page ready
73
//~ File lang=new File(HELP_DIR,WhisperIM.lang.getString("base_language_code")); // language sub directory
74
//~ if(!lang.isDirectory()){ // If doesnt exist or not a directory...
75
//~ lang=new File(HELP_DIR, DEFAULT); // set to default language
76
//~ }
77
//~ File page=new File(lang,"index.html");
78
//~ try{
79
//~ loadPage(new URL("file:"+page.getAbsolutePath()));
80

81         //~ }
82
//~ catch(Exception e){
83
//~ GUI.errorStop(null,e);
84
//~ }
85
frame.setSize(Form.d2);
86         frame.addKeyListener(new KeyAction());
87         frame.addMouseListener(new MouseAction());
88     }
89     
90     //~ /** Shows the help window and loads the contents page.
91
//~ * @param caller The frame that is showing the HelpWindow.*/
92
//~ public void show(Frame caller){
93
//~ show(caller, "intro.html");
94

95     //~ }
96

97     /** Disposes the HelpWindow.*/
98     public void dispose(){
99         frame.dispose();
100     }
101     
102     public JFrame getFrame(){
103         return frame;
104     }
105     
106     /** Shows the help window and displays the topic.
107     * The topic is a html page and optional heading,
108     * eg config.html or preferences.html#lookandfeel
109     * If the topic cannot be found, the contents page is loaded instead.
110     * @param caller The frame or dialog that is showing the HelpWindow
111     * @param topic The topic to load.*/

112     public static void show(Component caller, String JavaDoc topic){
113         if(WhisperIM.Help_Window==null){
114             WhisperIM.Help_Window=new HelpWindow();
115         }
116         WhisperIM.Help_Window.showPage(caller,topic);
117     }
118     
119     public void showPage(Component caller, String JavaDoc topic){
120         boolean dosize=false;
121         if(!frame.isVisible()){
122             frame.setLocationRelativeTo(caller);
123             dosize=true; // we want to size the window for the page
124
// we dont this if the window was already visible though
125
}
126         frame.show();
127         File JavaDoc lang=new File JavaDoc(HELP_DIR,currentLang); // language sub directory
128

129         final String JavaDoc userLang=WhisperIM.lang.getString("base_language_code");
130         if(!currentLang.equals(userLang)){
131             lang=new File JavaDoc(HELP_DIR,userLang); // language sub directory
132
if(!lang.isDirectory()){ // If doesnt exist or not a directory...
133
lang=new File JavaDoc(HELP_DIR, DEFAULT);// set to default language
134
currentLang=DEFAULT;
135             }
136             else{
137                 currentLang=userLang;
138             }
139             try{// load correct contents page
140
frame.setCursor(GUI.WAIT);
141                 contents.setPage(new URL JavaDoc("file:"+(new File JavaDoc(lang,"index2.html")).getAbsolutePath()));
142                 frame.setCursor(GUI.NORMAL);
143             }
144             catch(Exception JavaDoc e){
145                 GUI.showError(frame,"help title",null,e.getMessage());
146             }
147         }
148         
149         if(!lang.isDirectory()){ // If doesnt exist or not a directory...
150
lang=new File JavaDoc(HELP_DIR, DEFAULT); // set to default language
151
}
152
153         File JavaDoc page=new File JavaDoc(lang, topic);
154         try{
155             loadPage(new URL JavaDoc("file:"+page.getAbsolutePath()));
156             if(dosize){
157                 frame.setSize(Form.d2);
158             }
159         }
160         catch(Exception JavaDoc e){
161             GUI.errorStop(null,e);
162         }
163     }
164     
165     // Loads the page specified in url
166
private void loadPage(URL JavaDoc url){
167         Cursor c=frame.getCursor(); // get current cursor
168
html.setEnabled(false);
169         contents.setEnabled(false);
170         frame.getGlassPane().setVisible(true);
171         frame.setCursor(GUI.WAIT);
172         SwingUtilities.invokeLater(new loadThread(c, url));
173     }
174     
175     // Thread that loads page in the html editor pane
176
final class loadThread implements Runnable JavaDoc{
177         Cursor c;
178         URL JavaDoc url;
179         
180         public loadThread(Cursor c, URL JavaDoc url){
181             this.c=c;
182             this.url=url;
183         }
184         
185         public void run() {
186             javax.swing.text.Document JavaDoc doc=html.getDocument();
187             try {
188                 html.setPage(url); // set the page
189
frame.setCursor(GUI.NORMAL); // reset cursor
190
frame.getGlassPane().setVisible(false);
191                 html.setEnabled(true);
192                 contents.setEnabled(true);
193             }
194             catch (IOException JavaDoc ioe){
195                 frame.setCursor(GUI.NORMAL);// reset cursor
196
frame.getGlassPane().setVisible(false);
197                 html.setEnabled(true);
198                 contents.setEnabled(true);
199                 html.setDocument(doc); // revert to previos page
200

201                 JOptionPane.showMessageDialog(frame,Lang.gs("cnlp")+"\n\r"+url.toString(),Lang.gs("error"),
202                 JOptionPane.ERROR_MESSAGE); // show error message
203
}
204         }
205     }
206     
207     
208     
209     final class HyperLinkClick implements HyperlinkListener{
210         public void hyperlinkUpdate(HyperlinkEvent e){
211             if (e.getEventType()==HyperlinkEvent.EventType.ACTIVATED){
212                 loadPage(e.getURL());
213             }
214         }
215     }
216 }
Popular Tags