| QuickHelpDialog.java |
1 /**
2 * Distribution License:
3 * JSword is free software; you can redistribute it and/or modify it under
4 * the terms of the GNU Lesser General Public License, version 2.1 as published by
5 * the Free Software Foundation. This program is distributed in the hope
6 * that it will be useful, but WITHOUT ANY WARRANTY; without even the
7 * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
8 * See the GNU Lesser General Public License for more details.
9 *
10 * The License is available on the internet at:
11 * http://www.gnu.org/copyleft/lgpl.html
12 * or by writing to:
13 * Free Software Foundation, Inc.
14 * 59 Temple Place - Suite 330
15 * Boston, MA 02111-1307, USA
16 *
17 * Copyright: 2005
18 * The copyright to this program is held by it's authors.
19 *
20 * ID: $Id: QuickHelpDialog.java 2107 2011-03-08 13:47:34Z dmsmith $
21 */
22 package org.crosswire.common.swing;
23
24 import java.awt.BorderLayout;
25 import java.awt.Color;
26 import java.awt.FlowLayout;
27 import java.awt.Frame;
28 import java.awt.Insets;
29 import java.awt.event.KeyAdapter;
30 import java.awt.event.KeyEvent;
31 import java.awt.event.WindowAdapter;
32 import java.awt.event.WindowEvent;
33
34 import javax.swing.JButton;
35 import javax.swing.JDialog;
36 import javax.swing.JEditorPane;
37 import javax.swing.JPanel;
38 import javax.swing.JScrollPane;
39 import javax.swing.text.html.HTMLEditorKit;
40
41 /**
42 * .
43 *
44 * @see gnu.lgpl.License for license details.<br>
45 * The copyright to this program is held by it's authors.
46 * @author Joe Walker [joe at eireneh dot com]
47 */
48 public class QuickHelpDialog extends JDialog {
49 /**
50 * This is the default constructor
51 */
52 public QuickHelpDialog(Frame owner, String title, String helpText) {
53 super(owner);
54
55 initialize();
56
57 txtHelp.setText(helpText);
58 this.setTitle(title);
59 }
60
61 /**
62 * This method initializes the GUI
63 */
64 private void initialize() {
65 actions = new ActionFactory(this);
66
67 txtHelp = new JEditorPane();
68 txtHelp.setEditable(false);
69 txtHelp.setEditorKit(new HTMLEditorKit());
70 txtHelp.setMargin(new Insets(5, 5, 0, 5));
71 txtHelp.addKeyListener(new KeyAdapter() {
72 /*
73 * (non-Javadoc)
74 *
75 * @see java.awt.event.KeyListener#keyTyped(java.awt.event.KeyEvent)
76 */
77 @Override
78 public void keyTyped(KeyEvent ev) {
79 close();
80 }
81 });
82
83 JScrollPane scrHelp = new CWScrollPane(txtHelp);
84 scrHelp.setBorder(null);
85
86 // TRANSLATOR: This is the text on an "OK" button.
87 JButton btnOK = new JButton(actions.addAction("OK", CWMsg.gettext("OK")));
88 JPanel pnlOK = new JPanel();
89 pnlOK.setLayout(new FlowLayout(FlowLayout.TRAILING));
90 pnlOK.add(btnOK, null);
91 pnlOK.setBackground(Color.WHITE);
92 pnlOK.setOpaque(true);
93
94 JPanel pnlHelp = new JPanel();
95 pnlHelp.setLayout(new BorderLayout());
96 pnlHelp.add(scrHelp, BorderLayout.CENTER);
97 pnlHelp.add(pnlOK, BorderLayout.SOUTH);
98
99 // TODO(joe): Make this more generic
100 this.setSize(650, 200);
101 this.setModal(true);
102 this.setContentPane(pnlHelp);
103 this.getRootPane().setDefaultButton(btnOK);
104 this.addWindowListener(new WindowAdapter() {
105 /*
106 * (non-Javadoc)
107 *
108 * @seejava.awt.event.WindowListener#windowClosing(java.awt.event.
109 * WindowEvent)
110 */
111 @Override
112 public void windowClosing(WindowEvent ev) {
113 close();
114 }
115 });
116 }
117
118 /*
119 * (non-Javadoc)
120 *
121 * @see java.awt.Component#setVisible(boolean)
122 */
123 @Override
124 public void setVisible(boolean visible) {
125 if (visible) {
126 GuiUtil.centerOnScreen(this);
127 }
128
129 super.setVisible(visible);
130 }
131
132 /**
133 * Someone clicked OK
134 */
135 public void doOK() {
136 close();
137 }
138
139 /**
140 *
141 */
142 public final void close() {
143 setVisible(false);
144 }
145
146 private transient ActionFactory actions;
147
148 private JEditorPane txtHelp;
149
150 /**
151 * Serialization ID
152 */
153 private static final long serialVersionUID = 3690752899747557426L;
154 }
155