001: package net.suberic.pooka.gui.crypto;
002:
003: import javax.swing.*;
004: import java.awt.*;
005: import java.awt.event.ActionListener;
006:
007: import net.suberic.util.VariableBundle;
008: import net.suberic.util.gui.IconManager;
009: import net.suberic.pooka.Pooka;
010:
011: /**
012: * Displays the current cryptography status for this Message.
013: */
014: public class CryptoPanel extends JPanel implements CryptoStatusDisplay {
015:
016: JButton encryptionButton;
017: JButton signatureButton;
018: JButton importKeysButton;
019:
020: // the various icons
021: static ImageIcon notEncryptedIcon;
022: static ImageIcon uncheckedEncryptedIcon;
023: static ImageIcon decryptedSuccessfullyIcon;
024: static ImageIcon decryptedUnsuccessfullyIcon;
025: static ImageIcon notSignedIcon;
026: static ImageIcon uncheckedSignedIcon;
027: static ImageIcon signatureVerifiedIcon;
028: static ImageIcon signatureBadIcon;
029: static ImageIcon signatureFailedVerificationIcon;
030:
031: static ImageIcon importKeysIcon;
032:
033: // the various tooltips
034: static String notEncryptedTooltip;
035: static String uncheckedEncryptedTooltip;
036: static String decryptedSuccessfullyTooltip;
037: static String decryptedUnsuccessfullyTooltip;
038: static String notSignedTooltip;
039: static String uncheckedSignedTooltip;
040: static String signatureVerifiedTooltip;
041: static String signatureBadTooltip;
042: static String signatureFailedVerificationTooltip;
043:
044: // the various status colors
045: static Color signedEncryptedColor = Color.MAGENTA;
046: static Color signedColor = Color.GREEN;
047: static Color encryptedColor = Color.BLUE;
048: static Color uncheckedColor = Color.YELLOW;
049: static Color failedColor = Color.RED;
050:
051: static boolean iconsLoaded = false;
052: static boolean tooltipsLoaded = false;
053:
054: // the current status
055: int currentCryptStatus = NOT_ENCRYPTED;
056: int currentSigStatus = NOT_SIGNED;
057:
058: /**
059: * A JPanel that shows the encryption status of this message.
060: */
061: public CryptoPanel() {
062: super ();
063: if (!iconsLoaded) {
064: Class this Class = this .getClass();
065: synchronized (this Class) {
066: if (!iconsLoaded) {
067: loadIcons("CryptoPanel", this Class, Pooka
068: .getResources());
069: iconsLoaded = true;
070: }
071: if (!tooltipsLoaded) {
072: loadTooltips("CryptoPanel", Pooka.getResources());
073: tooltipsLoaded = true;
074: }
075: }
076: }
077:
078: this .setLayout(new BoxLayout(this , BoxLayout.Y_AXIS));
079:
080: encryptionButton = createEncryptionButton();
081: signatureButton = createSignatureButton();
082:
083: this .add(encryptionButton);
084: this .add(signatureButton);
085: }
086:
087: /**
088: * Creates an Encryption Button.
089: */
090: public JButton createEncryptionButton() {
091: JButton returnValue = new JButton();
092: if (notEncryptedIcon != null)
093: returnValue.setIcon(notEncryptedIcon);
094: returnValue.setSize(25, 25);
095: returnValue.setPreferredSize(new java.awt.Dimension(25, 25));
096: returnValue.setMaximumSize(new java.awt.Dimension(25, 25));
097: return returnValue;
098: }
099:
100: /**
101: * Creates a Signature Button.
102: */
103: public JButton createSignatureButton() {
104: JButton returnValue = new JButton();
105: if (notSignedIcon != null)
106: returnValue.setIcon(notSignedIcon);
107: returnValue.setPreferredSize(new java.awt.Dimension(25, 25));
108: returnValue.setMaximumSize(new java.awt.Dimension(25, 25));
109: returnValue.setSize(25, 25);
110: return returnValue;
111: }
112:
113: /**
114: * Creates an Import Keys Button.
115: */
116: public JButton createImportKeysButton() {
117: JButton returnValue = new JButton();
118: if (importKeysIcon != null)
119: returnValue.setIcon(importKeysIcon);
120: returnValue.setPreferredSize(new java.awt.Dimension(25, 25));
121: returnValue.setMaximumSize(new java.awt.Dimension(25, 25));
122: returnValue.setSize(25, 25);
123: return returnValue;
124: }
125:
126: /**
127: * Updates the action on the given button.
128: */
129: public void updateAction(JButton button, Action a) {
130: ActionListener[] listeners = button.getActionListeners();
131: for (int i = 0; i < listeners.length; i++) {
132: button.removeActionListener(listeners[i]);
133: }
134:
135: button.addActionListener(a);
136: }
137:
138: /**
139: * Updates the encryption information.
140: */
141: public void cryptoUpdated(int newSignatureStatus,
142: int newEncryptionStatus) {
143: cryptoUpdated(newSignatureStatus, newEncryptionStatus, null);
144: }
145:
146: /**
147: * Updates the encryption information.
148: */
149: public void cryptoUpdated(int newSignatureStatus,
150: int newEncryptionStatus,
151: net.suberic.pooka.gui.MessageProxy proxy) {
152: if (newSignatureStatus != currentSigStatus) {
153: currentSigStatus = newSignatureStatus;
154:
155: if (currentSigStatus == NOT_SIGNED) {
156: signatureButton.setIcon(notSignedIcon);
157: signatureButton.setToolTipText(notSignedTooltip);
158: if (proxy != null) {
159: Action checkSigAction = proxy
160: .getAction("message-signature-status");
161: if (checkSigAction != null)
162: updateAction(signatureButton, checkSigAction);
163: }
164:
165: } else if (currentSigStatus == UNCHECKED_SIGNED) {
166: if (proxy != null) {
167: Action checkSigAction = proxy
168: .getAction("message-check-signature");
169: if (checkSigAction != null)
170: updateAction(signatureButton, checkSigAction);
171: }
172: signatureButton.setIcon(uncheckedSignedIcon);
173: signatureButton.setToolTipText(uncheckedSignedTooltip);
174: } else if (currentSigStatus == SIGNATURE_VERIFIED) {
175: signatureButton.setIcon(signatureVerifiedIcon);
176: signatureButton
177: .setToolTipText(signatureVerifiedTooltip);
178: if (proxy != null) {
179: Action checkSigAction = proxy
180: .getAction("message-signature-status");
181: if (checkSigAction != null)
182: updateAction(signatureButton, checkSigAction);
183: }
184: } else if (currentSigStatus == SIGNATURE_BAD) {
185: signatureButton.setIcon(signatureBadIcon);
186: signatureButton.setToolTipText(signatureBadTooltip);
187: if (proxy != null) {
188: Action checkSigAction = proxy
189: .getAction("message-signature-status");
190: if (checkSigAction != null)
191: updateAction(signatureButton, checkSigAction);
192: }
193: }
194: }
195:
196: if (newEncryptionStatus != currentCryptStatus) {
197: currentCryptStatus = newEncryptionStatus;
198:
199: if (currentCryptStatus == UNCHECKED_ENCRYPTED) {
200: encryptionButton.setIcon(uncheckedEncryptedIcon);
201: encryptionButton
202: .setToolTipText(uncheckedEncryptedTooltip);
203: if (proxy != null) {
204: Action decryptAction = proxy
205: .getAction("message-decrypt");
206: if (decryptAction != null)
207: updateAction(encryptionButton, decryptAction);
208: }
209: } else if (currentCryptStatus == DECRYPTED_SUCCESSFULLY) {
210: encryptionButton.setIcon(decryptedSuccessfullyIcon);
211: encryptionButton
212: .setToolTipText(decryptedSuccessfullyTooltip);
213: if (proxy != null) {
214: Action decryptAction = proxy
215: .getAction("message-encryption-status");
216: if (decryptAction != null)
217: updateAction(encryptionButton, decryptAction);
218: }
219: } else if (currentCryptStatus == DECRYPTED_UNSUCCESSFULLY) {
220: encryptionButton.setIcon(decryptedUnsuccessfullyIcon);
221: encryptionButton
222: .setToolTipText(decryptedUnsuccessfullyTooltip);
223: if (proxy != null) {
224: Action decryptAction = proxy
225: .getAction("message-encryption-status");
226: if (decryptAction != null)
227: updateAction(encryptionButton, decryptAction);
228: }
229: } else {
230: encryptionButton.setIcon(notEncryptedIcon);
231: encryptionButton.setToolTipText(notEncryptedTooltip);
232: if (proxy != null) {
233: Action decryptAction = proxy
234: .getAction("message-encryption-status");
235: if (decryptAction != null)
236: updateAction(encryptionButton, decryptAction);
237: }
238: }
239: repaint();
240: }
241: }
242:
243: /**
244: * Updates the encryption information.
245: */
246: public void cryptoUpdated(
247: net.suberic.pooka.MessageCryptoInfo cryptoInfo) {
248:
249: try {
250:
251: int sigStatus = NOT_SIGNED;
252: int cryptStatus = NOT_ENCRYPTED;
253:
254: if (cryptoInfo.isSigned()) {
255: if (cryptoInfo.hasCheckedSignature()) {
256: if (cryptoInfo.isSignatureValid()) {
257: sigStatus = SIGNATURE_VERIFIED;
258: } else {
259: sigStatus = SIGNATURE_BAD;
260: }
261: } else {
262: sigStatus = UNCHECKED_SIGNED;
263: }
264: }
265:
266: if (cryptoInfo.isEncrypted()) {
267: if (cryptoInfo.hasTriedDecryption()) {
268: if (cryptoInfo.isDecryptedSuccessfully()) {
269: cryptStatus = DECRYPTED_SUCCESSFULLY;
270: } else {
271: cryptStatus = DECRYPTED_UNSUCCESSFULLY;
272: }
273: } else {
274: cryptStatus = UNCHECKED_ENCRYPTED;
275: }
276: }
277:
278: net.suberic.pooka.gui.MessageProxy proxy = null;
279: net.suberic.pooka.MessageInfo info = cryptoInfo
280: .getMessageInfo();
281: if (info != null)
282: proxy = info.getMessageProxy();
283:
284: cryptoUpdated(sigStatus, cryptStatus, proxy);
285:
286: } catch (javax.mail.MessagingException me) {
287: // ignore here.
288: }
289: }
290:
291: /**
292: * This loads all of the icons for this button.
293: */
294: static void loadIcons(String key, Class this Class,
295: VariableBundle vars) {
296:
297: /*
298: * this is going to have several images:
299: * Unchecked Encrypted
300: * Decrypted Successfully
301: * Decrypted Unsuccessfully
302: * Unchecked Signed
303: * Signature verified
304: * Signature bad
305: * Signature failed verification
306: * ...and maybe more.
307: */
308:
309: IconManager iconManager = Pooka.getUIFactory().getIconManager();
310:
311: notEncryptedIcon = iconManager.getIcon(Pooka.getProperty(key
312: + ".notEncryptedIcon", "UnLock"));
313: uncheckedEncryptedIcon = iconManager.getIcon(Pooka.getProperty(
314: key + ".uncheckedEncryptedIcon", "Lock"));
315: decryptedSuccessfullyIcon = iconManager.getIcon(Pooka
316: .getProperty(key + ".decryptedSuccessfullyIcon",
317: "OpenLock"));
318: decryptedUnsuccessfullyIcon = iconManager.getIcon(Pooka
319: .getProperty(key + ".decryptedUnsuccessfullyIcon",
320: "Bomb"));
321: uncheckedSignedIcon = iconManager.getIcon(Pooka.getProperty(key
322: + ".uncheckedSignedIcon", "Draw"));
323: notSignedIcon = iconManager.getIcon(Pooka.getProperty(key
324: + ".notSignedIcon", "EnvelopeOpen"));
325: signatureVerifiedIcon = iconManager.getIcon(Pooka.getProperty(
326: key + ".signatureVerifiedIcon", "Check"));
327: signatureBadIcon = iconManager.getIcon(Pooka.getProperty(key
328: + ".signatureBadIcon", "Caution"));
329: signatureFailedVerificationIcon = iconManager.getIcon(Pooka
330: .getProperty(key + ".signatureFailedVerificationIcon",
331: "Caution"));
332: }
333:
334: /**
335: * This loads all of the tooltips for this button.
336: */
337: static void loadTooltips(String key, VariableBundle vars) {
338:
339: /*
340: * this is going to have several tooltips:
341: * Unchecked Encrypted
342: * Decrypted Successfully
343: * Decrypted Unsuccessfully
344: * Unchecked Signed
345: * Signature verified
346: * Signature bad
347: * Signature failed verification
348: * ...and maybe more.
349: */
350:
351: notEncryptedTooltip = vars.getProperty(key
352: + ".notEncrypted.Tooltip", "NotEncrypted");
353:
354: uncheckedEncryptedTooltip = vars.getProperty(key
355: + ".uncheckedEncrypted.Tooltip", "Encrypted Message");
356: decryptedSuccessfullyTooltip = vars.getProperty(key
357: + ".decryptedSuccessfully.Tooltip",
358: "Message Decrypted with Key ");
359: decryptedUnsuccessfullyTooltip = vars.getProperty(key
360: + ".decryptedUnsuccessfully.Tooltip",
361: "Message Failed Decryption");
362:
363: uncheckedSignedTooltip = vars.getProperty(key
364: + ".uncheckedSigned.Tooltip");
365: notSignedTooltip = vars.getProperty(key + ".notSigned.Tooltip",
366: "Not Signed");
367: signatureVerifiedTooltip = vars.getProperty(key
368: + ".signatureVerified.Tooltip",
369: "Signature Verified with Key ");
370: signatureBadTooltip = vars.getProperty(key
371: + ".signatureBad.Tooltip",
372: "Signature Failed Verification by Key ");
373: signatureFailedVerificationTooltip = vars.getProperty(key
374: + ".signatureFailedVerification.Tooltip",
375: "Unable to Verfify Signature");
376: }
377:
378: }
|