KickJava   Java API By Example, From Geeks To Geeks.

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


1 package net.suberic.pooka.gui.crypto;
2
3 import javax.swing.*;
4 import java.awt.*;
5 import java.awt.event.ActionListener JavaDoc;
6 import java.security.Key JavaDoc;
7 import java.awt.event.ActionEvent JavaDoc;
8 import java.awt.event.ActionListener JavaDoc;
9
10 import net.suberic.util.VariableBundle;
11 import net.suberic.util.gui.IconManager;
12 import net.suberic.pooka.Pooka;
13 import net.suberic.crypto.EncryptionKey;
14 import net.suberic.pooka.gui.NewMessageProxy;
15 import net.suberic.pooka.gui.NewMessageCryptoInfo;
16
17
18 /**
19  * Displays the cryptography status for a new message.
20  */

21 public class NewMessageCryptoDisplay extends JPanel implements CryptoStatusDisplay {
22   
23   JButton mSignatureKeyButton = null;
24   JButton mEncryptionKeyButton = null;
25
26   JToggleButton mSignatureEnabledButton = null;
27   JToggleButton mEncryptionEnabledButton = null;
28
29   JList mAttachKeysList = null;
30
31   NewMessageProxy proxy = null;
32
33   /**
34    * A JPanel that shows the encryption status of this message.
35    */

36   public NewMessageCryptoDisplay(NewMessageProxy nmp) {
37     super();
38
39     proxy = nmp;
40
41     this.setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
42     /*
43     Box cryptoBox = new Box(BoxLayout.Y_AXIS);
44     Box cryptoLabelBox = new Box(BoxLayout.X_AXIS);
45
46     cryptoLabelBox.add(new JLabel(Pooka.getProperty("NewMessageCryptoPanel.encryptionKey.label", "Encryption Key")));
47     
48     cryptoLabelBox.add(Box.createHorizontalStrut(5));
49     JButton clearEncryptionButton = createClearEncryptionButton();
50
51     cryptoLabelBox.add(clearEncryptionButton);
52     
53     cryptoLabelBox.add(Box.createHorizontalGlue());
54
55     createEncryptionButton();
56     
57     cryptoBox.add(cryptoLabelBox);
58
59     Box cryptoButtonBox = Box.createHorizontalBox();
60     cryptoButtonBox.add(mEncryptionKeyButton);
61     cryptoButtonBox.add(Box.createHorizontalGlue());
62     cryptoBox.add(cryptoButtonBox);
63     
64     Box signatureBox = new Box(BoxLayout.Y_AXIS);
65     Box signatureLabelBox = new Box(BoxLayout.X_AXIS);
66
67     signatureLabelBox.add(new JLabel(Pooka.getProperty("NewMessageCryptoPanel.signatureKey.label", "Signature Key")));
68     
69     signatureLabelBox.add(Box.createHorizontalStrut(5));
70
71     JButton clearSignatureButton = createClearSignatureButton();
72     signatureLabelBox.add(clearSignatureButton);
73
74     signatureLabelBox.add(Box.createHorizontalGlue());
75     
76     createSignatureButton();
77     
78     signatureBox.add(signatureLabelBox);
79     
80     Box signatureButtonBox = Box.createHorizontalBox();
81     signatureButtonBox.add(mSignatureKeyButton);
82     signatureButtonBox.add(Box.createHorizontalGlue());
83     signatureBox.add(signatureButtonBox);
84     */

85
86     Box cryptoBox = new Box(BoxLayout.X_AXIS);
87
88     cryptoBox.add(new JLabel(Pooka.getProperty("NewMessageCryptoPanel.encryptionKey.label", "Encryption Key")));
89     
90     cryptoBox.add(Box.createHorizontalStrut(5));
91
92     mEncryptionEnabledButton = createEncryptionEnabledButton();
93
94     createEncryptionButton();
95
96     cryptoBox.add(mEncryptionKeyButton);
97
98     cryptoBox.add(Box.createHorizontalGlue());
99
100     cryptoBox.add(mEncryptionEnabledButton);
101     
102     Box signatureBox = new Box(BoxLayout.X_AXIS);
103
104     signatureBox.add(new JLabel(Pooka.getProperty("NewMessageCryptoPanel.signatureKey.label", "Signature Key")));
105     
106     signatureBox.add(Box.createHorizontalStrut(5));
107
108     mSignatureEnabledButton = createSignatureEnabledButton();
109
110     createSignatureButton();
111
112     signatureBox.add(mSignatureKeyButton);
113
114     signatureBox.add(Box.createHorizontalGlue());
115
116     signatureBox.add(mSignatureEnabledButton);
117     
118     this.add(cryptoBox);
119     this.add(signatureBox);
120
121   }
122
123   /**
124    * Creates an Encryption Button.
125    */

126   public void createEncryptionButton() {
127     mEncryptionKeyButton = new JButton();
128     mEncryptionKeyButton.addActionListener(proxy.getAction("message-select-crypt-key"));
129     updateEncryptionButton();
130   }
131   
132   /**
133    * Updates the enryption button.
134    */

135   public void updateEncryptionButton() {
136     Runnable JavaDoc runMe = new Runnable JavaDoc() {
137     public void run() {
138       Key JavaDoc current = getEncryptionKey();
139       if (current == null)
140         mEncryptionKeyButton.setText(Pooka.getProperty("NewMessageCryptoPanel.encryptionKey.none", "< Not Encrypted >"));
141       else
142         mEncryptionKeyButton.setText(current instanceof EncryptionKey ? ((EncryptionKey)current).getDisplayAlias() : current.toString());
143     }
144       };
145
146     if (SwingUtilities.isEventDispatchThread())
147       runMe.run();
148     else
149       SwingUtilities.invokeLater(runMe);
150   }
151
152   /**
153    * Creates a ToggleButton for enabling/disabling encryption.
154    */

155   public JToggleButton createEncryptionEnabledButton() {
156     IconManager iconManager = Pooka.getUIFactory().getIconManager();
157     
158     ImageIcon keyIcon = iconManager.getIcon(Pooka.getProperty("NewMessageCryptoDisplay.keyIcon", "Key"));
159     ImageIcon noKeyIcon = iconManager.getIcon(Pooka.getProperty("NewMessageCryptoDisplay.noKeyIcon", "NoKey"));
160     if (keyIcon != null && noKeyIcon != null) {
161       JToggleButton returnValue = new JToggleButton(noKeyIcon, proxy.getCryptoInfo().getEncryptMessage() != NewMessageCryptoInfo.CRYPTO_NO);
162       
163       returnValue.setSelectedIcon(keyIcon);
164       
165       returnValue.addActionListener(new ActionListener JavaDoc() {
166       public void actionPerformed(ActionEvent JavaDoc e) {
167         boolean nowSelected = mEncryptionEnabledButton.isSelected();
168         
169         mEncryptionKeyButton.setEnabled(nowSelected);
170         if (nowSelected)
171           proxy.getCryptoInfo().setEncryptMessage(NewMessageCryptoInfo.CRYPTO_YES);
172         else
173           proxy.getCryptoInfo().setEncryptMessage(NewMessageCryptoInfo.CRYPTO_NO);
174       }
175     });
176       
177       returnValue.setSize(keyIcon.getIconHeight(), keyIcon.getIconWidth());
178       returnValue.setPreferredSize(new java.awt.Dimension JavaDoc(keyIcon.getIconHeight(), keyIcon.getIconWidth()));
179       return returnValue;
180     }
181     
182     return null;
183   }
184   
185   /**
186    * Creates a button which clears the encryption key.
187    */

188   public JButton createClearEncryptionButton() {
189     JButton returnValue = new JButton(Pooka.getProperty("NewMessageCryptoPanel.clearButton.label", "Clear Key"));
190     returnValue.addActionListener(proxy.getAction("message-clear-encrypt"));
191     return returnValue;
192   }
193
194   /**
195    * Returns the current encryption key.
196    */

197   public Key JavaDoc getEncryptionKey() {
198     return proxy.getCryptoInfo().getEncryptionKey();
199   }
200
201   /**
202    * Sets the current encryption key.
203    */

204   public void setEncryptionKey(Key JavaDoc pEncryptionKey) {
205     proxy.getCryptoInfo().setEncryptionKey(pEncryptionKey);
206     updateEncryptionButton();
207   }
208
209   /**
210    * Creates an Signature Button.
211    */

212   public void createSignatureButton() {
213     mSignatureKeyButton = new JButton();
214     mSignatureKeyButton.addActionListener(proxy.getAction("message-select-sig-key"));
215     updateSignatureButton();
216   }
217   
218   /**
219    * Creates a ToggleButton for enabling/disabling the signature.
220    */

221   public JToggleButton createSignatureEnabledButton() {
222     IconManager iconManager = Pooka.getUIFactory().getIconManager();
223     
224     ImageIcon keyIcon = iconManager.getIcon(Pooka.getProperty("NewMessageCryptoDisplay.keyIcon", "Key"));
225     ImageIcon noKeyIcon = iconManager.getIcon(Pooka.getProperty("NewMessageCryptoDisplay.noKeyIcon", "NoKey"));
226     if (keyIcon != null && noKeyIcon != null) {
227       JToggleButton returnValue = new JToggleButton(noKeyIcon, proxy.getCryptoInfo().getSignMessage() != NewMessageCryptoInfo.CRYPTO_NO);
228       
229       returnValue.setSelectedIcon(keyIcon);
230
231       returnValue.addActionListener(new ActionListener JavaDoc() {
232       public void actionPerformed(ActionEvent JavaDoc e) {
233         boolean nowSelected = mSignatureEnabledButton.isSelected();
234         mSignatureKeyButton.setEnabled(nowSelected);
235         if (nowSelected)
236           proxy.getCryptoInfo().setSignMessage(NewMessageCryptoInfo.CRYPTO_YES);
237         else
238           proxy.getCryptoInfo().setSignMessage(NewMessageCryptoInfo.CRYPTO_NO);
239       }
240     });
241       returnValue.setSize(new java.awt.Dimension JavaDoc(keyIcon.getIconHeight(), keyIcon.getIconWidth()));
242       returnValue.setPreferredSize(new java.awt.Dimension JavaDoc(keyIcon.getIconHeight(), keyIcon.getIconWidth()));
243
244       return returnValue;
245     }
246
247     return null;
248   }
249
250   /**
251    * Updates the enryption button.
252    */

253   public void updateSignatureButton() {
254     Runnable JavaDoc runMe = new Runnable JavaDoc() {
255     public void run() {
256       Key JavaDoc current = getSignatureKey();
257       if (current == null)
258         mSignatureKeyButton.setText(Pooka.getProperty("NewMessageCryptoPanel.signatureKey.none", "< Not Signed >"));
259       else
260         mSignatureKeyButton.setText(current instanceof EncryptionKey ? ((EncryptionKey)current).getDisplayAlias() : current.toString()); }
261       };
262
263     if (SwingUtilities.isEventDispatchThread())
264       runMe.run();
265     else
266       SwingUtilities.invokeLater(runMe);
267   }
268
269   /**
270    * Creates a button which clears the signature key.
271    */

272   public JButton createClearSignatureButton() {
273     JButton returnValue = new JButton(Pooka.getProperty("NewMessageCryptoPanel.clearButton.label", "Clear Key"));
274     returnValue.addActionListener(proxy.getAction("message-clear-signature"));
275     return returnValue;
276   }
277
278   /**
279    * Returns the current encryption key.
280    */

281   public Key JavaDoc getSignatureKey() {
282     return proxy.getCryptoInfo().getSignatureKey();
283   }
284
285   /**
286    * Sets the current encryption key.
287    */

288   public void setSignatureKey(Key JavaDoc pSignatureKey) {
289     proxy.getCryptoInfo().setSignatureKey(pSignatureKey);
290     updateSignatureButton();
291   }
292
293   /**
294    * Sets whether we're going to encrypt or not.
295    */

296   public void setEncryptMessage(int encryptValue) {
297     proxy.getCryptoInfo().setEncryptMessage(encryptValue);
298   }
299
300   /**
301    * Sets whether we're going to sign or not.
302    */

303   public void setSignMessage(int signValue) {
304     proxy.getCryptoInfo().setSignMessage(signValue);
305   }
306
307   /**
308    * Attaches an encryption key.
309    */

310   public void attachEncryptionKey(Key JavaDoc cryptKey) {
311     proxy.getCryptoInfo().attachEncryptionKey(cryptKey);
312   }
313
314   /**
315    * Removes an encryption key.
316    */

317   public void removeEncryptionKey(Key JavaDoc cryptKey) {
318     proxy.getCryptoInfo().removeEncryptionKey(cryptKey);
319   }
320
321   /**
322    * Updates the encryption information.
323    */

324   public void cryptoUpdated(int newSignatureStatus, int newEncryptionStatus) {
325
326   }
327   
328   /**
329    * Updates the encryption information.
330    */

331   public void cryptoUpdated(net.suberic.pooka.MessageCryptoInfo cryptoInfo) {
332     
333   }
334
335
336 }
337
Popular Tags