KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > columba > mail > gui > message > viewer > SecurityStatusViewer


1 //The contents of this file are subject to the Mozilla Public License Version 1.1
2
//(the "License"); you may not use this file except in compliance with the
3
//License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
4
//
5
//Software distributed under the License is distributed on an "AS IS" basis,
6
//WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
7
//for the specific language governing rights and
8
//limitations under the License.
9
//
10
//The Original Code is "The Columba Project"
11
//
12
//The Initial Developers of the Original Code are Frederik Dietz and Timo
13
// Stich.
14
//Portions created by Frederik Dietz and Timo Stich are Copyright (C) 2003.
15
//
16
//All Rights Reserved.
17
package org.columba.mail.gui.message.viewer;
18
19 import java.awt.BorderLayout JavaDoc;
20 import java.awt.Color JavaDoc;
21 import java.awt.Image JavaDoc;
22
23 import javax.swing.BorderFactory JavaDoc;
24 import javax.swing.ImageIcon JavaDoc;
25 import javax.swing.JComponent JavaDoc;
26 import javax.swing.JLabel JavaDoc;
27 import javax.swing.JPanel JavaDoc;
28
29 import org.columba.core.resourceloader.ImageLoader;
30 import org.columba.mail.folder.IMailbox;
31 import org.columba.mail.gui.frame.MailFrameMediator;
32 import org.columba.mail.gui.message.filter.SecurityStatusEvent;
33 import org.columba.mail.gui.message.filter.SecurityStatusListener;
34 import org.columba.mail.parser.text.HtmlParser;
35 import org.columba.mail.util.MailResourceLoader;
36
37 /**
38  * IViewer displays security status information.
39  *
40  * @author fdietz
41  *
42  */

43 public class SecurityStatusViewer extends JPanel JavaDoc implements ICustomViewer,
44         SecurityStatusListener {
45
46     public static final int DECRYPTION_SUCCESS = 0;
47
48     public static final int DECRYPTION_FAILURE = 1;
49
50     public static final int VERIFICATION_SUCCESS = 2;
51
52     public static final int VERIFICATION_FAILURE = 3;
53
54     public static final int NO_KEY = 4;
55
56     public static final int NOOP = 5;
57
58     protected JLabel JavaDoc icon;
59
60     protected JLabel JavaDoc text;
61
62     protected JPanel JavaDoc left;
63
64     private boolean visible;
65
66     public SecurityStatusViewer() {
67         super();
68
69         setLayout(new BorderLayout JavaDoc());
70
71         left = new JPanel JavaDoc();
72         left.setLayout(new BorderLayout JavaDoc());
73         left.setBorder(BorderFactory.createEmptyBorder(0, 5, 5, 5));
74
75         icon = new JLabel JavaDoc();
76         left.add(icon, BorderLayout.NORTH);
77
78         add(left, BorderLayout.WEST);
79         text = new JLabel JavaDoc();
80         add(text, BorderLayout.CENTER);
81
82         setValue(SecurityStatusViewer.NOOP, "");
83
84         updateUI();
85
86         visible = false;
87     }
88
89     public void updateUI() {
90         super.updateUI();
91
92         setBorder(new MessageBorder(new Color JavaDoc(255, 255, 60), 1, true));
93
94         Color JavaDoc color = new Color JavaDoc(255, 255, 160);
95
96         setBackground(color);
97
98         if (icon != null) {
99             icon.setBackground(color);
100         }
101
102         if (text != null) {
103             text.setBackground(color);
104         }
105
106         if (left != null) {
107             left.setBackground(color);
108         }
109     }
110
111     private void setValue(int value, String JavaDoc message) {
112         ImageIcon JavaDoc image = null;
113
114         switch (value) {
115         case SecurityStatusViewer.DECRYPTION_SUCCESS: {
116             image = ImageLoader.getMiscIcon("signature-ok.png");
117
118             icon.setToolTipText(MailResourceLoader.getString("menu",
119                     "mainframe", "security_decrypt_success"));
120             text.setText(transformToHTML(MailResourceLoader.getString("menu",
121                     "mainframe", "security_decrypt_success"), message));
122
123             break;
124         }
125
126         case SecurityStatusViewer.DECRYPTION_FAILURE: {
127             image = ImageLoader.getMiscIcon("signature-bad.png");
128             icon.setToolTipText(MailResourceLoader.getString("menu",
129                     "mainframe", "security_encrypt_fail"));
130             text.setText(transformToHTML(MailResourceLoader.getString("menu",
131                     "mainframe", "security_encrypt_fail"), message));
132
133             break;
134         }
135
136         case SecurityStatusViewer.VERIFICATION_SUCCESS: {
137             image = ImageLoader.getMiscIcon("signature-ok.png");
138             icon.setToolTipText(MailResourceLoader.getString("menu",
139                     "mainframe", "security_verify_success"));
140             text.setText(transformToHTML(MailResourceLoader.getString("menu",
141                     "mainframe", "security_verify_success"), message));
142
143             break;
144         }
145
146         case SecurityStatusViewer.VERIFICATION_FAILURE: {
147             image = ImageLoader.getMiscIcon("signature-bad.png");
148
149             icon.setToolTipText(MailResourceLoader.getString("menu",
150                     "mainframe", "security_verify_fail"));
151             text.setText(transformToHTML(MailResourceLoader.getString("menu",
152                     "mainframe", "security_verify_fail"), message));
153
154             break;
155         }
156
157         case SecurityStatusViewer.NO_KEY: {
158             image = ImageLoader.getMiscIcon("signature-nokey.png");
159             icon.setToolTipText(MailResourceLoader.getString("menu",
160                     "mainframe", "security_verify_nokey"));
161             text.setText(transformToHTML(MailResourceLoader.getString("menu",
162                     "mainframe", "security_verify_nokey"), message));
163
164             break;
165         }
166
167         case SecurityStatusViewer.NOOP: {
168             text.setText("");
169             icon.setIcon(null);
170
171             break;
172         }
173         }
174
175         if (image != null) {
176             // scale image
177
image = new ImageIcon JavaDoc(image.getImage().getScaledInstance(16, 16,
178                     Image.SCALE_SMOOTH));
179
180             icon.setIcon(image);
181         }
182
183         updateUI();
184     }
185
186     protected String JavaDoc transformToHTML(String JavaDoc title, String JavaDoc message) {
187         // convert special characters
188
String JavaDoc html = null;
189
190         if (message != null) {
191             html = HtmlParser.substituteSpecialCharacters(message);
192         }
193
194         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
195
196         buf.append("<html><body><p>");
197         // buf.append("<b>" + title + "</b><br>");
198
buf.append(title + "<br>");
199         buf.append(html);
200         buf.append("</p></body></html>");
201
202         return buf.toString();
203     }
204
205     /**
206      * @see org.columba.mail.gui.message.viewer.IViewer#view(IMailbox,
207      * java.lang.Object, org.columba.mail.gui.frame.MailFrameMediator)
208      */

209     public void view(IMailbox folder, Object JavaDoc uid, MailFrameMediator mediator)
210             throws Exception JavaDoc {
211
212     }
213
214     /**
215      * @see org.columba.mail.gui.message.viewer.IViewer#getView()
216      */

217     public JComponent JavaDoc getView() {
218         return this;
219     }
220
221     /**
222      * @see org.columba.mail.gui.message.filter.SecurityStatusListener#statusUpdate(org.columba.mail.gui.message.filter.SecurityStatusEvent)
223      */

224     public void statusUpdate(SecurityStatusEvent event) {
225         String JavaDoc message = event.getMessage();
226         int status = event.getStatus();
227
228         setValue(status, message);
229
230         if (status == NOOP)
231             visible = false;
232         else
233             visible = true;
234     }
235
236     /**
237      * @see org.columba.mail.gui.message.viewer.IViewer#isVisible()
238      */

239     public boolean isVisible() {
240         return visible;
241     }
242
243     /**
244      * @see org.columba.mail.gui.message.viewer.IViewer#updateGUI()
245      */

246     public void updateGUI() throws Exception JavaDoc {
247
248     }
249 }
250
Popular Tags