KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > columba > core > gui > base > InfoViewerDialog


1 // The contents of this file are subject to the Mozilla Public License Version
2
// 1.1
3
//(the "License"); you may not use this file except in compliance with the
4
//License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
5
//
6
//Software distributed under the License is distributed on an "AS IS" basis,
7
//WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
8
//for the specific language governing rights and
9
//limitations under the License.
10
//
11
//The Original Code is "The Columba Project"
12
//
13
//The Initial Developers of the Original Code are Frederik Dietz and Timo
14
// Stich.
15
//Portions created by Frederik Dietz and Timo Stich are Copyright (C) 2003.
16
//
17
//All Rights Reserved.
18

19 package org.columba.core.gui.base;
20
21 import java.awt.BorderLayout JavaDoc;
22 import java.awt.Color JavaDoc;
23 import java.awt.Dimension JavaDoc;
24 import java.awt.Font JavaDoc;
25 import java.awt.GridLayout JavaDoc;
26 import java.awt.event.ActionEvent JavaDoc;
27 import java.awt.event.ActionListener JavaDoc;
28 import java.awt.event.KeyEvent JavaDoc;
29 import java.io.IOException JavaDoc;
30 import java.net.URL JavaDoc;
31
32 import javax.swing.BorderFactory JavaDoc;
33 import javax.swing.JButton JavaDoc;
34 import javax.swing.JComponent JavaDoc;
35 import javax.swing.JDialog JavaDoc;
36 import javax.swing.JFrame JavaDoc;
37 import javax.swing.JPanel JavaDoc;
38 import javax.swing.JScrollPane JavaDoc;
39 import javax.swing.JTextPane JavaDoc;
40 import javax.swing.KeyStroke JavaDoc;
41 import javax.swing.SwingConstants JavaDoc;
42 import javax.swing.UIManager JavaDoc;
43 import javax.swing.text.html.HTMLEditorKit JavaDoc;
44 import javax.swing.text.html.StyleSheet JavaDoc;
45
46 import org.columba.core.resourceloader.GlobalResourceLoader;
47
48 /**
49  * Dialg showing information to the user. This can be either a URL to document
50  * or a string.
51  *
52  * @author fdietz
53  */

54 public class InfoViewerDialog extends JDialog JavaDoc implements ActionListener JavaDoc {
55     protected JButton JavaDoc helpButton;
56
57     protected JButton JavaDoc closeButton;
58
59     protected JTextPane JavaDoc textPane;
60
61     /**
62      * Creates a new info dialog showing the specified string.
63      */

64     public InfoViewerDialog(String JavaDoc message) {
65         this();
66         HTMLEditorKit JavaDoc editorKit = new HTMLEditorKit JavaDoc();
67         StyleSheet JavaDoc styles = new StyleSheet JavaDoc();
68
69         Font JavaDoc font = UIManager.getFont("Label.font");
70         String JavaDoc name = font.getName();
71         int size = font.getSize();
72         String JavaDoc css = "<style type=\"text/css\"><!--p {font-family:\"" + name
73                 + "\"; font-size:\"" + size + "pt\"}--></style>";
74         styles.addRule(css);
75         editorKit.setStyleSheet(styles);
76
77         textPane.setEditorKit(editorKit);
78         textPane.setText(message);
79         setVisible(true);
80     }
81
82     /**
83      * Creates a new info dialog showing the contents of the given URL.
84      */

85     public InfoViewerDialog(URL JavaDoc url) throws IOException JavaDoc {
86         this();
87         textPane.setPage(url);
88         setVisible(true);
89     }
90
91     /**
92      * Internal constructor used by the two public ones.
93      */

94     protected InfoViewerDialog() {
95         super(new JFrame JavaDoc(), "Info", true);
96         initComponents();
97         pack();
98         setLocationRelativeTo(null);
99     }
100
101     protected void initComponents() {
102         JPanel JavaDoc mainPanel = new JPanel JavaDoc(new BorderLayout JavaDoc());
103         mainPanel.setBorder(BorderFactory.createEmptyBorder(12, 12, 12, 12));
104         getContentPane().add(mainPanel);
105
106         // centerpanel
107
textPane = new JTextPane JavaDoc();
108         textPane.setEditable(false);
109
110         JScrollPane JavaDoc scrollPane = new JScrollPane JavaDoc(textPane);
111         scrollPane.setPreferredSize(new Dimension JavaDoc(450, 300));
112         scrollPane.getViewport().setBackground(Color.white);
113
114         mainPanel.add(scrollPane);
115
116         JPanel JavaDoc bottomPanel = new JPanel JavaDoc(new BorderLayout JavaDoc());
117         bottomPanel.setBorder(new SingleSideEtchedBorder(SwingConstants.TOP));
118
119         JPanel JavaDoc buttonPanel = new JPanel JavaDoc(new GridLayout JavaDoc(1, 2, 6, 0));
120         buttonPanel.setBorder(BorderFactory.createEmptyBorder(12, 12, 12, 12));
121
122         ButtonWithMnemonic closeButton = new ButtonWithMnemonic(
123                 GlobalResourceLoader.getString("global", "global", "close"));
124         closeButton.setActionCommand("CLOSE");
125         closeButton.addActionListener(this);
126         buttonPanel.add(closeButton);
127
128         ButtonWithMnemonic helpButton = new ButtonWithMnemonic(
129                 GlobalResourceLoader.getString("global", "global", "help"));
130
131         //TODO (@author fdietz): associate help with button and root pane
132
buttonPanel.add(helpButton);
133         bottomPanel.add(buttonPanel, BorderLayout.EAST);
134         getContentPane().add(bottomPanel, BorderLayout.SOUTH);
135         getRootPane().setDefaultButton(closeButton);
136         getRootPane().registerKeyboardAction(this, "CLOSE",
137                 KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0),
138                 JComponent.WHEN_IN_FOCUSED_WINDOW);
139     }
140
141     public void actionPerformed(ActionEvent JavaDoc e) {
142         String JavaDoc action = e.getActionCommand();
143
144         if (action.equals("CLOSE")) {
145             setVisible(false);
146         }
147     }
148 }
Popular Tags