001: // The contents of this file are subject to the Mozilla Public License Version
002: // 1.1
003: //(the "License"); you may not use this file except in compliance with the
004: //License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
005: //
006: //Software distributed under the License is distributed on an "AS IS" basis,
007: //WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
008: //for the specific language governing rights and
009: //limitations under the License.
010: //
011: //The Original Code is "The Columba Project"
012: //
013: //The Initial Developers of the Original Code are Frederik Dietz and Timo
014: // Stich.
015: //Portions created by Frederik Dietz and Timo Stich are Copyright (C) 2003.
016: //
017: //All Rights Reserved.
018:
019: package org.columba.core.gui.base;
020:
021: import java.awt.BorderLayout;
022: import java.awt.Color;
023: import java.awt.Dimension;
024: import java.awt.Font;
025: import java.awt.GridLayout;
026: import java.awt.event.ActionEvent;
027: import java.awt.event.ActionListener;
028: import java.awt.event.KeyEvent;
029: import java.io.IOException;
030: import java.net.URL;
031:
032: import javax.swing.BorderFactory;
033: import javax.swing.JButton;
034: import javax.swing.JComponent;
035: import javax.swing.JDialog;
036: import javax.swing.JFrame;
037: import javax.swing.JPanel;
038: import javax.swing.JScrollPane;
039: import javax.swing.JTextPane;
040: import javax.swing.KeyStroke;
041: import javax.swing.SwingConstants;
042: import javax.swing.UIManager;
043: import javax.swing.text.html.HTMLEditorKit;
044: import javax.swing.text.html.StyleSheet;
045:
046: import org.columba.core.resourceloader.GlobalResourceLoader;
047:
048: /**
049: * Dialg showing information to the user. This can be either a URL to document
050: * or a string.
051: *
052: * @author fdietz
053: */
054: public class InfoViewerDialog extends JDialog implements ActionListener {
055: protected JButton helpButton;
056:
057: protected JButton closeButton;
058:
059: protected JTextPane textPane;
060:
061: /**
062: * Creates a new info dialog showing the specified string.
063: */
064: public InfoViewerDialog(String message) {
065: this ();
066: HTMLEditorKit editorKit = new HTMLEditorKit();
067: StyleSheet styles = new StyleSheet();
068:
069: Font font = UIManager.getFont("Label.font");
070: String name = font.getName();
071: int size = font.getSize();
072: String css = "<style type=\"text/css\"><!--p {font-family:\""
073: + name + "\"; font-size:\"" + size + "pt\"}--></style>";
074: styles.addRule(css);
075: editorKit.setStyleSheet(styles);
076:
077: textPane.setEditorKit(editorKit);
078: textPane.setText(message);
079: setVisible(true);
080: }
081:
082: /**
083: * Creates a new info dialog showing the contents of the given URL.
084: */
085: public InfoViewerDialog(URL url) throws IOException {
086: this ();
087: textPane.setPage(url);
088: setVisible(true);
089: }
090:
091: /**
092: * Internal constructor used by the two public ones.
093: */
094: protected InfoViewerDialog() {
095: super (new JFrame(), "Info", true);
096: initComponents();
097: pack();
098: setLocationRelativeTo(null);
099: }
100:
101: protected void initComponents() {
102: JPanel mainPanel = new JPanel(new BorderLayout());
103: mainPanel.setBorder(BorderFactory.createEmptyBorder(12, 12, 12,
104: 12));
105: getContentPane().add(mainPanel);
106:
107: // centerpanel
108: textPane = new JTextPane();
109: textPane.setEditable(false);
110:
111: JScrollPane scrollPane = new JScrollPane(textPane);
112: scrollPane.setPreferredSize(new Dimension(450, 300));
113: scrollPane.getViewport().setBackground(Color.white);
114:
115: mainPanel.add(scrollPane);
116:
117: JPanel bottomPanel = new JPanel(new BorderLayout());
118: bottomPanel.setBorder(new SingleSideEtchedBorder(
119: SwingConstants.TOP));
120:
121: JPanel buttonPanel = new JPanel(new GridLayout(1, 2, 6, 0));
122: buttonPanel.setBorder(BorderFactory.createEmptyBorder(12, 12,
123: 12, 12));
124:
125: ButtonWithMnemonic closeButton = new ButtonWithMnemonic(
126: GlobalResourceLoader.getString("global", "global",
127: "close"));
128: closeButton.setActionCommand("CLOSE");
129: closeButton.addActionListener(this );
130: buttonPanel.add(closeButton);
131:
132: ButtonWithMnemonic helpButton = new ButtonWithMnemonic(
133: GlobalResourceLoader.getString("global", "global",
134: "help"));
135:
136: //TODO (@author fdietz): associate help with button and root pane
137: buttonPanel.add(helpButton);
138: bottomPanel.add(buttonPanel, BorderLayout.EAST);
139: getContentPane().add(bottomPanel, BorderLayout.SOUTH);
140: getRootPane().setDefaultButton(closeButton);
141: getRootPane().registerKeyboardAction(this , "CLOSE",
142: KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0),
143: JComponent.WHEN_IN_FOCUSED_WINDOW);
144: }
145:
146: public void actionPerformed(ActionEvent e) {
147: String action = e.getActionCommand();
148:
149: if (action.equals("CLOSE")) {
150: setVisible(false);
151: }
152: }
153: }
|