Source code: com/sshtools/sshterm/BannerDialog.java
1 /**
2 * Sshtools - SSHTerm
3 *
4 * Copyright (C) 2002 Lee David Painter
5 *
6 * Written by: 2002 Lee David Painter <lee@sshtools.com>
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 */
22 package com.sshtools.sshterm;
23
24 import java.awt.event.*;
25 import java.awt.*;
26 import javax.swing.*;
27 import com.sshtools.j2ssh.ui.*;
28
29 /**
30 * <p>Displays an authentication banner</p>
31 *
32 *@author Richard Pernavas (<A HREF="mailto:richard@sshtools.com">
33 * richard@sshtools.com</A> )
34 *@author Brett Smith
35 *@created 31 August 2002
36 *@version $Id: BannerDialog.java,v 1.4 2002/12/23 18:11:57 martianx Exp $
37 */
38
39 class BannerDialog extends JDialog {
40
41 // Statics
42 final static String BANNER_ICON = "/com/sshtools/sshterm/largebanner.png";
43
44 // Private instance variables
45 private JTextArea text;
46
47 /**
48 * Constructs the BannerDialog object
49 *
50 *@param bannerText The SSH banner message text
51 */
52 public BannerDialog(String bannerText) {
53 super((Frame)null, "SSH Authentication - Banner Message", true);
54 init(bannerText);
55 }
56
57 /**
58 * Constructs the BannerDialog object
59 *
60 *@param parent Parent frame
61 *@param bannerText The SSH banner message text
62 */
63 public BannerDialog(Frame parent, String bannerText) {
64 super(parent, "SSH Authentication - Banner Message", true);
65 init(bannerText);
66 }
67
68 /**
69 * Constructs the BannerDialog object
70 *
71 *@param parent Parent dialog
72 *@param bannerText The SSH banner message text
73 */
74 public BannerDialog(Dialog parent, String bannerText) {
75 super(parent, "SSH Authentication - Banner Message", true);
76 init(bannerText);
77 }
78
79 /**
80 * Initialise the dialog
81 */
82 void init(String bannerText) {
83 try {
84 jbInit();
85 } catch (Exception e) {
86 e.printStackTrace();
87 }
88
89 //
90 setText(bannerText);
91 }
92
93 /**
94 * Convenience method to work out the parent window given a component and
95 * show the dialog
96 *
97 * @param parent parent component
98 * @param text banner text
99 */
100 public static void showBannerDialog(Component parent, String bannerText) {
101 Window w = (Window)SwingUtilities.getAncestorOfClass(Window.class, parent);
102 BannerDialog dialog = null;
103 if(w instanceof Frame)
104 dialog = new BannerDialog((Frame)w, bannerText);
105 else if(w instanceof Dialog)
106 dialog = new BannerDialog((Dialog)w, bannerText);
107 else
108 dialog = new BannerDialog(bannerText);
109 UIUtil.positionComponent(SwingConstants.CENTER, dialog);
110 dialog.toFront();
111 dialog.setVisible(true);
112 }
113
114 /**
115 * Set the text for the banner
116 *
117 * @param text the text to show in the banner
118 */
119 public void setText(String text) {
120 this.text.setText(text);
121 this.repaint();
122 }
123
124 /**
125 * Initializes the dialogs components
126 *
127 *@exception Exception
128 */
129 void jbInit() throws Exception {
130 setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
131
132 // Create the component to display the banner text
133 text = new JTextArea();
134 text.setBorder(BorderFactory.createEmptyBorder(4, 4, 4, 4));
135 /** @todo make optional - was this changed back? */
136 // text.setLineWrap(true);
137 text.setEditable(false);
138 Font f = new Font("MonoSpaced",
139 text.getFont().getStyle(), text.getFont().getSize());
140 text.setFont(f);
141 JScrollPane textScroller = new JScrollPane(text);
142
143 // Create the center banner panel
144 IconWrapperPanel centerPanel = new IconWrapperPanel(
145 new ResourceIcon(BANNER_ICON), textScroller);
146 centerPanel.setBorder(BorderFactory.createEmptyBorder(4, 4, 4, 4));
147
148 // Create the south button panel
149 JButton ok = new JButton("Ok");
150 ok.setMnemonic('o');
151 ok.setDefaultCapable(true);
152 ok.addActionListener(new ActionListener() {
153 public void actionPerformed(ActionEvent evt) {
154 // I presume this component is not reused?
155 dispose();
156 }
157 });
158 JPanel southPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
159 southPanel.add(ok);
160 getRootPane().setDefaultButton(ok);
161
162 // Build the main panel
163 getContentPane().setLayout(new BorderLayout());
164 getContentPane().add(centerPanel, BorderLayout.CENTER);
165 getContentPane().add(southPanel, BorderLayout.SOUTH);
166
167 //
168 setSize(500,245);
169 setResizable(false);
170 }
171 }