KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > suberic > pooka > gui > crypto > CryptoPanel


1 package net.suberic.pooka.gui.crypto;
2
3 import javax.swing.*;
4 import java.awt.*;
5 import java.awt.event.ActionListener JavaDoc;
6
7 import net.suberic.util.VariableBundle;
8 import net.suberic.util.gui.IconManager;
9 import net.suberic.pooka.Pooka;
10
11 /**
12  * Displays the current cryptography status for this Message.
13  */

14 public class CryptoPanel extends JPanel implements CryptoStatusDisplay {
15   
16   JButton encryptionButton;
17   JButton signatureButton;
18   JButton importKeysButton;
19
20   // the various icons
21
static ImageIcon notEncryptedIcon;
22   static ImageIcon uncheckedEncryptedIcon;
23   static ImageIcon decryptedSuccessfullyIcon;
24   static ImageIcon decryptedUnsuccessfullyIcon;
25   static ImageIcon notSignedIcon;
26   static ImageIcon uncheckedSignedIcon;
27   static ImageIcon signatureVerifiedIcon;
28   static ImageIcon signatureBadIcon;
29   static ImageIcon signatureFailedVerificationIcon;
30
31   static ImageIcon importKeysIcon;
32
33   // the various tooltips
34
static String JavaDoc notEncryptedTooltip;
35   static String JavaDoc uncheckedEncryptedTooltip;
36   static String JavaDoc decryptedSuccessfullyTooltip;
37   static String JavaDoc decryptedUnsuccessfullyTooltip;
38   static String JavaDoc notSignedTooltip;
39   static String JavaDoc uncheckedSignedTooltip;
40   static String JavaDoc signatureVerifiedTooltip;
41   static String JavaDoc signatureBadTooltip;
42   static String JavaDoc signatureFailedVerificationTooltip;
43
44   // the various status colors
45
static Color signedEncryptedColor = Color.MAGENTA;
46   static Color signedColor = Color.GREEN;
47   static Color encryptedColor = Color.BLUE;
48   static Color uncheckedColor = Color.YELLOW;
49   static Color failedColor = Color.RED;
50
51   static boolean iconsLoaded = false;
52   static boolean tooltipsLoaded = false;
53
54   // the current status
55
int currentCryptStatus = NOT_ENCRYPTED;
56   int currentSigStatus = NOT_SIGNED;
57
58   /**
59    * A JPanel that shows the encryption status of this message.
60    */

61   public CryptoPanel() {
62     super();
63     if (! iconsLoaded) {
64       Class JavaDoc thisClass = this.getClass();
65       synchronized(thisClass) {
66     if (! iconsLoaded) {
67       loadIcons("CryptoPanel", thisClass, Pooka.getResources());
68       iconsLoaded = true;
69     }
70     if (! tooltipsLoaded) {
71       loadTooltips("CryptoPanel", Pooka.getResources());
72       tooltipsLoaded = true;
73     }
74       }
75     }
76
77     this.setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
78
79     encryptionButton = createEncryptionButton();
80     signatureButton = createSignatureButton();
81
82     this.add(encryptionButton);
83     this.add(signatureButton);
84   }
85
86   /**
87    * Creates an Encryption Button.
88    */

89   public JButton createEncryptionButton() {
90     JButton returnValue = new JButton();
91     if (notEncryptedIcon != null)
92       returnValue.setIcon(notEncryptedIcon);
93     returnValue.setSize(25,25);
94     returnValue.setPreferredSize(new java.awt.Dimension JavaDoc(25,25));
95     returnValue.setMaximumSize(new java.awt.Dimension JavaDoc(25,25));
96     return returnValue;
97   }
98   
99   /**
100    * Creates a Signature Button.
101    */

102   public JButton createSignatureButton() {
103     JButton returnValue = new JButton();
104     if (notSignedIcon != null)
105       returnValue.setIcon(notSignedIcon);
106     returnValue.setPreferredSize(new java.awt.Dimension JavaDoc(25,25));
107     returnValue.setMaximumSize(new java.awt.Dimension JavaDoc(25,25));
108     returnValue.setSize(25,25);
109     return returnValue;
110   }
111
112   /**
113    * Creates an Import Keys Button.
114    */

115   public JButton createImportKeysButton() {
116     JButton returnValue = new JButton();
117     if (importKeysIcon != null)
118       returnValue.setIcon(importKeysIcon);
119     returnValue.setPreferredSize(new java.awt.Dimension JavaDoc(25,25));
120     returnValue.setMaximumSize(new java.awt.Dimension JavaDoc(25,25));
121     returnValue.setSize(25,25);
122     return returnValue;
123   }
124   
125   /**
126    * Updates the action on the given button.
127    */

128   public void updateAction(JButton button, Action a) {
129     ActionListener JavaDoc[] listeners = button.getActionListeners();
130     for (int i = 0; i < listeners.length; i++) {
131       button.removeActionListener(listeners[i]);
132     }
133     
134     button.addActionListener(a);
135   }
136
137   /**
138    * Updates the encryption information.
139    */

140   public void cryptoUpdated(int newSignatureStatus, int newEncryptionStatus) {
141     cryptoUpdated(newSignatureStatus, newEncryptionStatus, null);
142   }
143
144   /**
145    * Updates the encryption information.
146    */

147   public void cryptoUpdated(int newSignatureStatus, int newEncryptionStatus, net.suberic.pooka.gui.MessageProxy proxy) {
148     if (newSignatureStatus != currentSigStatus) {
149       currentSigStatus = newSignatureStatus;
150
151       if (currentSigStatus == NOT_SIGNED) {
152     signatureButton.setIcon(notSignedIcon);
153     signatureButton.setToolTipText(notSignedTooltip);
154     if (proxy != null) {
155       Action checkSigAction = proxy.getAction("message-signature-status");
156       if (checkSigAction != null)
157         updateAction(signatureButton, checkSigAction);
158     }
159
160       } else if (currentSigStatus == UNCHECKED_SIGNED) {
161     if (proxy != null) {
162       Action checkSigAction = proxy.getAction("message-check-signature");
163       if (checkSigAction != null)
164         updateAction(signatureButton, checkSigAction);
165     }
166     signatureButton.setIcon(uncheckedSignedIcon);
167     signatureButton.setToolTipText(uncheckedSignedTooltip);
168       } else if (currentSigStatus == SIGNATURE_VERIFIED) {
169     signatureButton.setIcon(signatureVerifiedIcon);
170     signatureButton.setToolTipText(signatureVerifiedTooltip);
171     if (proxy != null) {
172       Action checkSigAction = proxy.getAction("message-signature-status");
173       if (checkSigAction != null)
174         updateAction(signatureButton, checkSigAction);
175     }
176       } else if (currentSigStatus == SIGNATURE_BAD) {
177     signatureButton.setIcon(signatureBadIcon);
178     signatureButton.setToolTipText(signatureBadTooltip);
179     if (proxy != null) {
180       Action checkSigAction = proxy.getAction("message-signature-status");
181       if (checkSigAction != null)
182         updateAction(signatureButton, checkSigAction);
183     }
184       }
185     }
186
187     if (newEncryptionStatus != currentCryptStatus) {
188       currentCryptStatus = newEncryptionStatus;
189
190       if (currentCryptStatus == UNCHECKED_ENCRYPTED) {
191     encryptionButton.setIcon(uncheckedEncryptedIcon);
192     encryptionButton.setToolTipText(uncheckedEncryptedTooltip);
193     if (proxy != null) {
194       Action decryptAction = proxy.getAction("message-decrypt");
195       if (decryptAction != null)
196         updateAction(encryptionButton, decryptAction);
197     }
198       } else if (currentCryptStatus == DECRYPTED_SUCCESSFULLY) {
199     encryptionButton.setIcon(decryptedSuccessfullyIcon);
200     encryptionButton.setToolTipText(decryptedSuccessfullyTooltip);
201     if (proxy != null) {
202       Action decryptAction = proxy.getAction("message-encryption-status");
203       if (decryptAction != null)
204         updateAction(encryptionButton, decryptAction);
205     }
206       } else if (currentCryptStatus == DECRYPTED_UNSUCCESSFULLY) {
207     encryptionButton.setIcon(decryptedUnsuccessfullyIcon);
208     encryptionButton.setToolTipText(decryptedUnsuccessfullyTooltip);
209     if (proxy != null) {
210       Action decryptAction = proxy.getAction("message-encryption-status");
211       if (decryptAction != null)
212         updateAction(encryptionButton, decryptAction);
213     }
214       } else {
215     encryptionButton.setIcon(notEncryptedIcon);
216     encryptionButton.setToolTipText(notEncryptedTooltip);
217     if (proxy != null) {
218       Action decryptAction = proxy.getAction("message-encryption-status");
219       if (decryptAction != null)
220         updateAction(encryptionButton, decryptAction);
221     }
222       }
223       repaint();
224     }
225   }
226
227   /**
228    * Updates the encryption information.
229    */

230   public void cryptoUpdated(net.suberic.pooka.MessageCryptoInfo cryptoInfo) {
231
232     try {
233       
234       int sigStatus = NOT_SIGNED;
235       int cryptStatus = NOT_ENCRYPTED;
236       
237       if (cryptoInfo.isSigned()) {
238     if (cryptoInfo.hasCheckedSignature()) {
239       if (cryptoInfo.isSignatureValid()) {
240         sigStatus = SIGNATURE_VERIFIED;
241       } else {
242         sigStatus = SIGNATURE_BAD;
243       }
244     } else {
245       sigStatus = UNCHECKED_SIGNED;
246     }
247       }
248
249       if (cryptoInfo.isEncrypted()) {
250     if (cryptoInfo.hasTriedDecryption()) {
251       if (cryptoInfo.isDecryptedSuccessfully()) {
252         cryptStatus = DECRYPTED_SUCCESSFULLY;
253       } else {
254         cryptStatus = DECRYPTED_UNSUCCESSFULLY;
255       }
256     } else {
257       cryptStatus = UNCHECKED_ENCRYPTED;
258     }
259       }
260       
261       net.suberic.pooka.gui.MessageProxy proxy = null;
262       net.suberic.pooka.MessageInfo info = cryptoInfo.getMessageInfo();
263       if (info != null)
264     proxy = info.getMessageProxy();
265
266       cryptoUpdated(sigStatus, cryptStatus, proxy);
267
268     } catch (javax.mail.MessagingException JavaDoc me) {
269       // ignore here.
270
}
271   }
272
273   /**
274    * This loads all of the icons for this button.
275    */

276   static void loadIcons(String JavaDoc key, Class JavaDoc thisClass, VariableBundle vars) {
277
278     /*
279      * this is going to have several images:
280      * Unchecked Encrypted
281      * Decrypted Successfully
282      * Decrypted Unsuccessfully
283      * Unchecked Signed
284      * Signature verified
285      * Signature bad
286      * Signature failed verification
287      * ...and maybe more.
288      */

289
290     IconManager iconManager = Pooka.getUIFactory().getIconManager();
291     
292     notEncryptedIcon = iconManager.getIcon(Pooka.getProperty(key + ".notEncryptedIcon", "UnLock"));
293     uncheckedEncryptedIcon = iconManager.getIcon(Pooka.getProperty(key + ".uncheckedEncryptedIcon", "Lock"));
294     decryptedSuccessfullyIcon = iconManager.getIcon(Pooka.getProperty(key + ".decryptedSuccessfullyIcon", "OpenLock"));
295     decryptedUnsuccessfullyIcon = iconManager.getIcon(Pooka.getProperty(key + ".decryptedUnsuccessfullyIcon", "Bomb"));
296     uncheckedSignedIcon = iconManager.getIcon(Pooka.getProperty(key + ".uncheckedSignedIcon", "Draw"));
297     notSignedIcon = iconManager.getIcon(Pooka.getProperty(key + ".notSignedIcon", "EnvelopeOpen"));
298     signatureVerifiedIcon = iconManager.getIcon(Pooka.getProperty(key + ".signatureVerifiedIcon", "Check"));
299     signatureBadIcon = iconManager.getIcon(Pooka.getProperty(key + ".signatureBadIcon", "Caution"));
300     signatureFailedVerificationIcon = iconManager.getIcon(Pooka.getProperty(key + ".signatureFailedVerificationIcon", "Caution"));
301   }
302
303   /**
304    * This loads all of the tooltips for this button.
305    */

306   static void loadTooltips(String JavaDoc key, VariableBundle vars) {
307
308     /*
309      * this is going to have several tooltips:
310      * Unchecked Encrypted
311      * Decrypted Successfully
312      * Decrypted Unsuccessfully
313      * Unchecked Signed
314      * Signature verified
315      * Signature bad
316      * Signature failed verification
317      * ...and maybe more.
318      */

319
320
321     notEncryptedTooltip = vars.getProperty(key + ".notEncrypted.Tooltip", "NotEncrypted");
322     
323     uncheckedEncryptedTooltip = vars.getProperty(key + ".uncheckedEncrypted.Tooltip", "Encrypted Message");
324     decryptedSuccessfullyTooltip = vars.getProperty(key + ".decryptedSuccessfully.Tooltip", "Message Decrypted with Key ");
325     decryptedUnsuccessfullyTooltip = vars.getProperty(key + ".decryptedUnsuccessfully.Tooltip", "Message Failed Decryption");
326
327     uncheckedSignedTooltip = vars.getProperty(key + ".uncheckedSigned.Tooltip");
328     notSignedTooltip = vars.getProperty(key + ".notSigned.Tooltip", "Not Signed");
329     signatureVerifiedTooltip = vars.getProperty(key + ".signatureVerified.Tooltip", "Signature Verified with Key ");
330     signatureBadTooltip = vars.getProperty(key + ".signatureBad.Tooltip", "Signature Failed Verification by Key ");
331     signatureFailedVerificationTooltip = vars.getProperty(key + ".signatureFailedVerification.Tooltip", "Unable to Verfify Signature");
332   }
333
334 }
335     
336
Popular Tags