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 ; 8 9 import java.util.Hashtable ; 10 11 39 40 public class CryptoButton extends JButton implements ConfigurableUI, CryptoStatusDisplay { 41 42 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 Color signedEncryptedColor = Color.MAGENTA; 54 Color signedColor = Color.GREEN; 55 Color encryptedColor = Color.BLUE; 56 Color uncheckedColor = Color.YELLOW; 57 Color failedColor = Color.RED; 58 59 int currentCryptStatus = NOT_ENCRYPTED; 61 int currentSigStatus = NOT_SIGNED; 62 63 Action currentAction = null; 64 65 public CryptoButton () { 66 super(); 67 } 68 69 77 public CryptoButton(String buttonID, VariableBundle vars) { 78 super(); 79 80 configureComponent(buttonID, vars); 81 } 82 83 87 public void configureComponent(String key, VariableBundle vars) { 88 89 loadIcons(key, vars); 90 91 try { 92 this.setToolTipText(vars.getProperty(key+ ".ToolTip")); 93 } catch (java.util.MissingResourceException mre) { 94 } 95 96 String cmd = vars.getProperty(key + ".Action", key); 97 98 setActionCommand(cmd); 99 100 cryptoUpdated(NOT_SIGNED, NOT_ENCRYPTED); 101 } 102 103 106 public void loadIcons(String key, VariableBundle vars) { 107 108 119 120 try { 121 java.net.URL url =this.getClass().getResource(vars.getProperty(key + ".notEncrypted.Image")); 122 if (url != null) 123 notEncryptedIcon = new ImageIcon(url); 124 } catch (java.util.MissingResourceException mre) { 125 return; 126 } 127 128 try { 129 java.net.URL url =this.getClass().getResource(vars.getProperty(key + ".uncheckedEncrypted.Image")); 130 if (url != null) 131 uncheckedEncryptedIcon = new ImageIcon(url); 132 } catch (java.util.MissingResourceException mre) { 133 return; 134 } 135 136 try { 137 java.net.URL url =this.getClass().getResource(vars.getProperty(key + ".decryptedSuccessfully.Image")); 138 if (url != null) 139 decryptedSuccessfullyIcon = new ImageIcon(url); 140 } catch (java.util.MissingResourceException mre) { 141 return; 142 } 143 144 try { 145 java.net.URL url =this.getClass().getResource(vars.getProperty(key + ".decryptedUnsuccessfully.Image")); 146 if (url != null) 147 decryptedUnsuccessfullyIcon = new ImageIcon(url); 148 } catch (java.util.MissingResourceException mre) { 149 return; 150 } 151 152 try { 153 java.net.URL url =this.getClass().getResource(vars.getProperty(key + ".uncheckedSigned.Image")); 154 if (url != null) 155 uncheckedSignedIcon = new ImageIcon(url); 156 } catch (java.util.MissingResourceException mre) { 157 return; 158 } 159 160 try { 161 java.net.URL url =this.getClass().getResource(vars.getProperty(key + ".signatureVerified.Image")); 162 if (url != null) 163 signatureVerifiedIcon = new ImageIcon(url); 164 } catch (java.util.MissingResourceException mre) { 165 return; 166 } 167 168 try { 169 java.net.URL url =this.getClass().getResource(vars.getProperty(key + ".signatureBad.Image")); 170 if (url != null) 171 signatureBadIcon = new ImageIcon(url); 172 } catch (java.util.MissingResourceException mre) { 173 return; 174 } 175 176 try { 177 java.net.URL url =this.getClass().getResource(vars.getProperty(key + ".signatureFailedVerification.Image")); 178 if (url != null) 179 signatureFailedVerificationIcon = new ImageIcon(url); 180 } catch (java.util.MissingResourceException mre) { 181 return; 182 } 183 184 } 185 186 192 public void setActive(Hashtable commands) { 193 if (currentAction != null) { 194 removeActionListener(currentAction); 195 } 196 197 try { 198 currentAction = (Action)commands.get(getActionCommand()); 199 } catch (ClassCastException 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 216 public void setActive(Action[] newActions) { 217 Hashtable tmpHash = new Hashtable (); 218 if (newActions != null && newActions.length > 0) { 219 for (int i = 0; i < newActions.length; i++) { 220 String cmdName = (String )newActions[i].getValue(Action.NAME); 221 tmpHash.put(cmdName, newActions[i]); 222 } 223 } 224 225 setActive(tmpHash); 226 } 227 228 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 me) { 265 } 267 } 268 269 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 319 public void setToolBarColor(Color newColor) { 320 328 this.setBackground(newColor); 329 } 330 331 334 public JToolBar getToolBar() { 335 try { 336 return (JToolBar) getAncestorOfClass(Class.forName("javax.swing.JToolBar"), this); 337 } catch (Exception e) { 338 e.printStackTrace(); 339 return null; 340 } 341 } 342 343 346 private java.awt.Container getAncestorOfClass(Class c, java.awt.Component comp) { 347 if(comp == null || c == null) 348 return null; 349 java.awt.Container parent = comp.getParent(); 350 while(parent != null && !(c.isInstance(parent))) 351 parent = parent.getParent(); 352 return parent; 353 } 354 } 355 356 | Popular Tags |