001: //The contents of this file are subject to the Mozilla Public License Version 1.1
002: //(the "License"); you may not use this file except in compliance with the
003: //License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
004: //
005: //Software distributed under the License is distributed on an "AS IS" basis,
006: //WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
007: //for the specific language governing rights and
008: //limitations under the License.
009: //
010: //The Original Code is "The Columba Project"
011: //
012: //The Initial Developers of the Original Code are Frederik Dietz and Timo
013: // Stich.
014: //Portions created by Frederik Dietz and Timo Stich are Copyright (C) 2003.
015: //
016: //All Rights Reserved.
017: package org.columba.mail.gui.message.viewer;
018:
019: import java.awt.BorderLayout;
020: import java.awt.Color;
021: import java.awt.Image;
022:
023: import javax.swing.BorderFactory;
024: import javax.swing.ImageIcon;
025: import javax.swing.JComponent;
026: import javax.swing.JLabel;
027: import javax.swing.JPanel;
028:
029: import org.columba.core.resourceloader.ImageLoader;
030: import org.columba.mail.folder.IMailbox;
031: import org.columba.mail.gui.frame.MailFrameMediator;
032: import org.columba.mail.gui.message.filter.SecurityStatusEvent;
033: import org.columba.mail.gui.message.filter.SecurityStatusListener;
034: import org.columba.mail.parser.text.HtmlParser;
035: import org.columba.mail.util.MailResourceLoader;
036:
037: /**
038: * IViewer displays security status information.
039: *
040: * @author fdietz
041: *
042: */
043: public class SecurityStatusViewer extends JPanel implements
044: ICustomViewer, SecurityStatusListener {
045:
046: public static final int DECRYPTION_SUCCESS = 0;
047:
048: public static final int DECRYPTION_FAILURE = 1;
049:
050: public static final int VERIFICATION_SUCCESS = 2;
051:
052: public static final int VERIFICATION_FAILURE = 3;
053:
054: public static final int NO_KEY = 4;
055:
056: public static final int NOOP = 5;
057:
058: protected JLabel icon;
059:
060: protected JLabel text;
061:
062: protected JPanel left;
063:
064: private boolean visible;
065:
066: public SecurityStatusViewer() {
067: super ();
068:
069: setLayout(new BorderLayout());
070:
071: left = new JPanel();
072: left.setLayout(new BorderLayout());
073: left.setBorder(BorderFactory.createEmptyBorder(0, 5, 5, 5));
074:
075: icon = new JLabel();
076: left.add(icon, BorderLayout.NORTH);
077:
078: add(left, BorderLayout.WEST);
079: text = new JLabel();
080: add(text, BorderLayout.CENTER);
081:
082: setValue(SecurityStatusViewer.NOOP, "");
083:
084: updateUI();
085:
086: visible = false;
087: }
088:
089: public void updateUI() {
090: super .updateUI();
091:
092: setBorder(new MessageBorder(new Color(255, 255, 60), 1, true));
093:
094: Color color = new Color(255, 255, 160);
095:
096: setBackground(color);
097:
098: if (icon != null) {
099: 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 message) {
112: ImageIcon 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(
121: "menu", "mainframe", "security_decrypt_success"),
122: message));
123:
124: break;
125: }
126:
127: case SecurityStatusViewer.DECRYPTION_FAILURE: {
128: image = ImageLoader.getMiscIcon("signature-bad.png");
129: icon.setToolTipText(MailResourceLoader.getString("menu",
130: "mainframe", "security_encrypt_fail"));
131: text.setText(transformToHTML(MailResourceLoader.getString(
132: "menu", "mainframe", "security_encrypt_fail"),
133: message));
134:
135: break;
136: }
137:
138: case SecurityStatusViewer.VERIFICATION_SUCCESS: {
139: image = ImageLoader.getMiscIcon("signature-ok.png");
140: icon.setToolTipText(MailResourceLoader.getString("menu",
141: "mainframe", "security_verify_success"));
142: text.setText(transformToHTML(MailResourceLoader.getString(
143: "menu", "mainframe", "security_verify_success"),
144: message));
145:
146: break;
147: }
148:
149: case SecurityStatusViewer.VERIFICATION_FAILURE: {
150: image = ImageLoader.getMiscIcon("signature-bad.png");
151:
152: icon.setToolTipText(MailResourceLoader.getString("menu",
153: "mainframe", "security_verify_fail"));
154: text.setText(transformToHTML(MailResourceLoader.getString(
155: "menu", "mainframe", "security_verify_fail"),
156: message));
157:
158: break;
159: }
160:
161: case SecurityStatusViewer.NO_KEY: {
162: image = ImageLoader.getMiscIcon("signature-nokey.png");
163: icon.setToolTipText(MailResourceLoader.getString("menu",
164: "mainframe", "security_verify_nokey"));
165: text.setText(transformToHTML(MailResourceLoader.getString(
166: "menu", "mainframe", "security_verify_nokey"),
167: message));
168:
169: break;
170: }
171:
172: case SecurityStatusViewer.NOOP: {
173: text.setText("");
174: icon.setIcon(null);
175:
176: break;
177: }
178: }
179:
180: if (image != null) {
181: // scale image
182: image = new ImageIcon(image.getImage().getScaledInstance(
183: 16, 16, Image.SCALE_SMOOTH));
184:
185: icon.setIcon(image);
186: }
187:
188: updateUI();
189: }
190:
191: protected String transformToHTML(String title, String message) {
192: // convert special characters
193: String html = null;
194:
195: if (message != null) {
196: html = HtmlParser.substituteSpecialCharacters(message);
197: }
198:
199: StringBuffer buf = new StringBuffer();
200:
201: buf.append("<html><body><p>");
202: // buf.append("<b>" + title + "</b><br>");
203: buf.append(title + "<br>");
204: buf.append(html);
205: buf.append("</p></body></html>");
206:
207: return buf.toString();
208: }
209:
210: /**
211: * @see org.columba.mail.gui.message.viewer.IViewer#view(IMailbox,
212: * java.lang.Object, org.columba.mail.gui.frame.MailFrameMediator)
213: */
214: public void view(IMailbox folder, Object uid,
215: MailFrameMediator mediator) throws Exception {
216:
217: }
218:
219: /**
220: * @see org.columba.mail.gui.message.viewer.IViewer#getView()
221: */
222: public JComponent getView() {
223: return this ;
224: }
225:
226: /**
227: * @see org.columba.mail.gui.message.filter.SecurityStatusListener#statusUpdate(org.columba.mail.gui.message.filter.SecurityStatusEvent)
228: */
229: public void statusUpdate(SecurityStatusEvent event) {
230: String message = event.getMessage();
231: int status = event.getStatus();
232:
233: setValue(status, message);
234:
235: if (status == NOOP)
236: visible = false;
237: else
238: visible = true;
239: }
240:
241: /**
242: * @see org.columba.mail.gui.message.viewer.IViewer#isVisible()
243: */
244: public boolean isVisible() {
245: return visible;
246: }
247:
248: /**
249: * @see org.columba.mail.gui.message.viewer.IViewer#updateGUI()
250: */
251: public void updateGUI() throws Exception {
252:
253: }
254: }
|