KickJava   Java API By Example, From Geeks To Geeks.

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


1 package net.suberic.pooka.gui.crypto;
2
3 import net.suberic.util.gui.*;
4 import net.suberic.util.VariableBundle;
5
6 import javax.swing.*;
7 import java.awt.Color JavaDoc;
8
9 import java.util.Hashtable JavaDoc;
10
11 /**
12  * This defines a UI component which may be built dynamically using a
13  * set of properties in a VariableBundle, and then may have the Actions
14  * associated with the individual buttons/menu items/whatevers updated
15  * dynamically to reflect the new values.
16  *
17  * In general, the format for the properties which define a ConfigurableUI
18  * component are as follows:
19  *
20  * MenuBar=File:Edit:Mail:Window:Help
21  *
22  * MenuBar.File=NewFolder:NewMail:OpenFolder:OpenMessage:Close:SaveAs:Print:Exit
23  * MenuBar.File.Label=File
24  *
25  * MenuBar.File.NewFolder.Action=folder-new
26  * MenuBar.File.NewFolder.Image=images/New.gif
27  * MenuBar.File.NewFolder.KeyBinding=F
28  * MenuBar.File.NewFolder.Label=New Folder
29  *
30  * where MenuBar would be the name of the 'root' configuration property,
31  * 'MenuBar.File' is the first submenu, and 'MenuBar.File.NewFolder' is
32  * the first actual 'button' configured. On the NewFolder MenuItem, the
33  * 'Action' is the name of the Action which will be run, and is the
34  * central part of the configuration. The rest (Image, KeyBinding, and
35  * Label) just control how the item is displayed and invoked. The
36  * 'KeyBinding' and 'Label' items should probably be put in a localized
37  * file if you want to internationalize your application.
38  */

39
40 public class CryptoButton extends JButton implements ConfigurableUI, CryptoStatusDisplay {
41
42   // the various icons
43
ImageIcon notEncryptedIcon;
44   ImageIcon uncheckedEncryptedIcon;
45   ImageIcon decryptedSuccessfullyIcon;
46   ImageIcon decryptedUnsuccessfullyIcon;
47   ImageIcon uncheckedSignedIcon;
48   ImageIcon signatureVerifiedIcon;
49   ImageIcon signatureBadIcon;
50   ImageIcon signatureFailedVerificationIcon;
51
52   // the various status colors
53
Color JavaDoc signedEncryptedColor = Color.MAGENTA;
54   Color JavaDoc signedColor = Color.GREEN;
55   Color JavaDoc encryptedColor = Color.BLUE;
56   Color JavaDoc uncheckedColor = Color.YELLOW;
57   Color JavaDoc failedColor = Color.RED;
58
59   // the current status
60
int currentCryptStatus = NOT_ENCRYPTED;
61   int currentSigStatus = NOT_SIGNED;
62
63   Action currentAction = null;
64
65   public CryptoButton () {
66     super();
67   }
68
69   /**
70    * This creates a new CryptoButton using the buttonID as the
71    * configuration key, and vars as the source for the values of all the
72    * properties.
73    *
74    * If buttonID doesn't exist in vars, then this returns an empty
75    * Button.
76    */

77   public CryptoButton(String JavaDoc buttonID, VariableBundle vars) {
78     super();
79     
80     configureComponent(buttonID, vars);
81   }
82
83   /**
84    * This configures the UI Component with the given ID and
85    * VariableBundle.
86    */

87   public void configureComponent(String JavaDoc key, VariableBundle vars) {
88
89     loadIcons(key, vars);
90
91     try {
92       this.setToolTipText(vars.getProperty(key+ ".ToolTip"));
93     } catch (java.util.MissingResourceException JavaDoc mre) {
94     }
95     
96     String JavaDoc cmd = vars.getProperty(key + ".Action", key);
97     
98     setActionCommand(cmd);
99     
100     cryptoUpdated(NOT_SIGNED, NOT_ENCRYPTED);
101   }
102
103   /**
104    * This loads all of the icons for this button.
105    */

106   public void loadIcons(String JavaDoc key, VariableBundle vars) {
107
108     /*
109      * this is going to have several images:
110      * Unchecked Encrypted
111      * Decrypted Successfully
112      * Decrypted Unsuccessfully
113      * Unchecked Signed
114      * Signature verified
115      * Signature bad
116      * Signature failed verification
117      * ...and maybe more.
118      */

119
120     try {
121       java.net.URL JavaDoc url =this.getClass().getResource(vars.getProperty(key + ".notEncrypted.Image"));
122       if (url != null)
123     notEncryptedIcon = new ImageIcon(url);
124     } catch (java.util.MissingResourceException JavaDoc mre) {
125       return;
126     }
127
128     try {
129       java.net.URL JavaDoc url =this.getClass().getResource(vars.getProperty(key + ".uncheckedEncrypted.Image"));
130       if (url != null)
131     uncheckedEncryptedIcon = new ImageIcon(url);
132     } catch (java.util.MissingResourceException JavaDoc mre) {
133       return;
134     }
135     
136     try {
137       java.net.URL JavaDoc url =this.getClass().getResource(vars.getProperty(key + ".decryptedSuccessfully.Image"));
138       if (url != null)
139     decryptedSuccessfullyIcon = new ImageIcon(url);
140     } catch (java.util.MissingResourceException JavaDoc mre) {
141       return;
142     }
143     
144     try {
145       java.net.URL JavaDoc url =this.getClass().getResource(vars.getProperty(key + ".decryptedUnsuccessfully.Image"));
146       if (url != null)
147     decryptedUnsuccessfullyIcon = new ImageIcon(url);
148     } catch (java.util.MissingResourceException JavaDoc mre) {
149       return;
150     }
151     
152     try {
153       java.net.URL JavaDoc url =this.getClass().getResource(vars.getProperty(key + ".uncheckedSigned.Image"));
154       if (url != null)
155     uncheckedSignedIcon = new ImageIcon(url);
156     } catch (java.util.MissingResourceException JavaDoc mre) {
157       return;
158     }
159     
160     try {
161       java.net.URL JavaDoc url =this.getClass().getResource(vars.getProperty(key + ".signatureVerified.Image"));
162       if (url != null)
163     signatureVerifiedIcon = new ImageIcon(url);
164     } catch (java.util.MissingResourceException JavaDoc mre) {
165       return;
166     }
167     
168     try {
169       java.net.URL JavaDoc url =this.getClass().getResource(vars.getProperty(key + ".signatureBad.Image"));
170       if (url != null)
171     signatureBadIcon = new ImageIcon(url);
172     } catch (java.util.MissingResourceException JavaDoc mre) {
173       return;
174     }
175     
176     try {
177       java.net.URL JavaDoc url =this.getClass().getResource(vars.getProperty(key + ".signatureFailedVerification.Image"));
178       if (url != null)
179     signatureFailedVerificationIcon = new ImageIcon(url);
180     } catch (java.util.MissingResourceException JavaDoc mre) {
181       return;
182     }
183     
184   }
185
186   /**
187    * This updates the Actions on the UI Component.
188    *
189    * The commands Hashtable is expected to be a table with the Action
190    * names as keys, and the Actions themselves as values.
191    */

192   public void setActive(Hashtable JavaDoc commands) {
193     if (currentAction != null) {
194       removeActionListener(currentAction);
195     }
196
197     try {
198       currentAction = (Action)commands.get(getActionCommand());
199     } catch (ClassCastException JavaDoc cce) {
200       currentAction = null;
201     }
202     
203     if (currentAction != null) {
204       addActionListener(currentAction);
205       setEnabled(true);
206     } else {
207       setEnabled(false);
208     }
209   }
210   
211
212   /**
213    * This updates the Actions on the UI Component.
214    *
215    */

216   public void setActive(Action[] newActions) {
217     Hashtable JavaDoc tmpHash = new Hashtable JavaDoc();
218     if (newActions != null && newActions.length > 0) {
219       for (int i = 0; i < newActions.length; i++) {
220     String JavaDoc cmdName = (String JavaDoc)newActions[i].getValue(Action.NAME);
221     tmpHash.put(cmdName, newActions[i]);
222       }
223     }
224     
225     setActive(tmpHash);
226   }
227   
228   /**
229    * Updates the encryption information.
230    */

231   public void cryptoUpdated(net.suberic.pooka.MessageCryptoInfo cryptoInfo) {
232
233     try {
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       
262       cryptoUpdated(sigStatus, cryptStatus);
263
264     } catch (javax.mail.MessagingException JavaDoc me) {
265       // ignore here.
266
}
267   }
268
269   /**
270    * Updates the encryption information.
271    */

272   public void cryptoUpdated(int newSignatureStatus, int newEncryptionStatus) {
273     currentCryptStatus = newEncryptionStatus;
274     currentSigStatus = newSignatureStatus;
275
276     if (currentCryptStatus == NOT_ENCRYPTED || currentCryptStatus == DECRYPTED_SUCCESSFULLY) {
277       if (currentSigStatus == NOT_SIGNED) {
278     if (currentCryptStatus == NOT_ENCRYPTED) {
279       setIcon(notEncryptedIcon);
280     } else {
281       setIcon(decryptedSuccessfullyIcon);
282       setToolBarColor(encryptedColor);
283     }
284       } else if (currentSigStatus == UNCHECKED_SIGNED) {
285     setIcon(uncheckedSignedIcon);
286       setToolBarColor(uncheckedColor);
287       } else if (currentSigStatus == SIGNATURE_VERIFIED) {
288     setIcon(signatureVerifiedIcon);
289     if (currentCryptStatus == NOT_ENCRYPTED)
290       setToolBarColor(signedColor);
291     else
292       setToolBarColor(signedEncryptedColor);
293       } else if (currentSigStatus == SIGNATURE_BAD) {
294     setIcon(signatureBadIcon);
295     setToolBarColor(failedColor);
296       } else if (currentSigStatus == SIGNATURE_FAILED_VERIFICATION) {
297     setIcon(signatureFailedVerificationIcon);
298     setToolBarColor(failedColor);
299       }
300     } else if (currentCryptStatus == UNCHECKED_ENCRYPTED) {
301       setIcon(uncheckedEncryptedIcon);
302       setToolBarColor(uncheckedColor);
303     } else if (currentCryptStatus == DECRYPTED_SUCCESSFULLY) {
304       setIcon(decryptedSuccessfullyIcon);
305       setToolBarColor(encryptedColor);
306     } else if (currentCryptStatus == DECRYPTED_UNSUCCESSFULLY) {
307       setIcon(decryptedUnsuccessfullyIcon);
308       setToolBarColor(failedColor);
309     } else {
310       setIcon(notEncryptedIcon);
311     }
312
313     repaint();
314   }
315
316   /**
317    * Sets the toolbar color.
318    */

319   public void setToolBarColor(Color JavaDoc newColor) {
320     /*
321     JToolBar tb = getToolBar();
322     if (tb != null) {
323       tb.setBackground(newColor);
324     } else {
325       System.err.println("toolbar = null.");
326     }
327     */

328     this.setBackground(newColor);
329   }
330
331   /**
332    * Gets the toolbar this Button belongs to.
333    */

334   public JToolBar getToolBar() {
335     try {
336       return (JToolBar) getAncestorOfClass(Class.forName("javax.swing.JToolBar"), this);
337     } catch (Exception JavaDoc e) {
338       e.printStackTrace();
339       return null;
340     }
341   }
342
343   /**
344    * Workaround for jdk 1.5 weirdness.
345    */

346   private java.awt.Container JavaDoc getAncestorOfClass(Class JavaDoc c, java.awt.Component JavaDoc comp) {
347     if(comp == null || c == null)
348       return null;
349     java.awt.Container JavaDoc parent = comp.getParent();
350     while(parent != null && !(c.isInstance(parent)))
351       parent = parent.getParent();
352     return parent;
353   }
354 }
355     
356
Popular Tags