KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > suberic > pooka > gui > propedit > KeySelectorPane


1 package net.suberic.pooka.gui.propedit;
2 import net.suberic.util.gui.propedit.*;
3 import net.suberic.util.VariableBundle;
4 import net.suberic.pooka.gui.filechooser.*;
5 import net.suberic.pooka.*;
6
7 import javax.swing.*;
8 import java.awt.event.ActionEvent JavaDoc;
9
10 import java.util.Set JavaDoc;
11 import java.util.Vector JavaDoc;
12
13 /**
14  * This displays the currently selected key (if any).
15  */

16
17 public class KeySelectorPane extends LabelValuePropertyEditor {
18
19   JLabel label;
20   JComboBox valueDisplay;
21
22   /**
23    * @param propertyName The property to be edited.
24    * @param template The property that will define the layout of the
25    * editor.
26    * @param manager The PropertyEditorManager that will manage the
27    * changes.
28    */

29   public void configureEditor(String JavaDoc propertyName, String JavaDoc template, String JavaDoc propertyBaseName, PropertyEditorManager newManager) {
30     configureBasic(propertyName, template, propertyBaseName, newManager);
31
32     if (debug) {
33       System.out.println("property is " + property + "; editorTemplate is " + editorTemplate);
34     }
35
36     label = createLabel();
37
38     valueDisplay = createKeyList(originalValue);
39
40     //valueDisplay.setPreferredSize(new java.awt.Dimension(150 - inputButton.getPreferredSize().width, valueDisplay.getMinimumSize().height));
41

42     this.add(label);
43     labelComponent = label;
44     JPanel tmpPanel = new JPanel(new java.awt.FlowLayout JavaDoc(java.awt.FlowLayout.LEFT, 0,0));
45     tmpPanel.add(valueDisplay);
46     tmpPanel.setPreferredSize(new java.awt.Dimension JavaDoc(Math.max(150, tmpPanel.getMinimumSize().width), valueDisplay.getMinimumSize().height));
47     valueComponent = tmpPanel;
48     this.add(tmpPanel);
49
50     updateEditorEnabled();
51
52   }
53
54   /**
55    * Creates a button that will bring up a way to select a folder.
56    */

57   public JComboBox createKeyList(String JavaDoc defaultValue) {
58     if (Pooka.isDebug())
59       System.out.println("creating keylist.");
60
61     String JavaDoc encryptionType = manager.getProperty(editorTemplate + ".encryptionType", "All");
62
63     if (encryptionType.equalsIgnoreCase("All"))
64       encryptionType = null;
65
66     String JavaDoc keyType = manager.getProperty(editorTemplate + ".keyType", "private");
67
68     Set JavaDoc keySet = null;
69
70     try {
71       if (keyType.equalsIgnoreCase("private"))
72         keySet = Pooka.getCryptoManager().privateKeyAliases(encryptionType);
73       else
74         keySet = Pooka.getCryptoManager().publicKeyAliases(encryptionType);
75     } catch (java.security.KeyStoreException JavaDoc kse) {
76       keySet = null;
77     }
78
79     Vector JavaDoc listModel = null;
80
81     if (keySet != null) {
82       listModel = new Vector JavaDoc(keySet);
83     } else {
84       listModel = new Vector JavaDoc();
85     }
86
87     if (originalValue != null && originalValue != "") {
88       if (! listModel.contains(originalValue))
89         listModel.add(originalValue);
90       JComboBox returnValue = new JComboBox(listModel);
91       returnValue.setSelectedItem(originalValue);
92
93       return returnValue;
94     } else {
95       JComboBox returnValue = new JComboBox(listModel);
96
97       return returnValue;
98     }
99   }
100
101   // as defined in net.suberic.util.gui.PropertyEditorUI
102

103   public void setValue() throws PropertyValueVetoException {
104     validateProperty();
105     if (Pooka.isDebug())
106       System.out.println("calling ksp.setValue. isEditorEnabled() = " + isEditorEnabled() + "; isChanged() = " + isChanged());
107
108     String JavaDoc newValue = (String JavaDoc) valueDisplay.getSelectedItem();
109     if (newValue == null)
110       newValue = "";
111
112     if (isEditorEnabled() && isChanged()) {
113       manager.setProperty(property, newValue);
114     }
115   }
116
117   public void validateProperty() throws PropertyValueVetoException {
118     String JavaDoc newValue = (String JavaDoc) valueDisplay.getSelectedItem();
119     if (newValue == null)
120       newValue = "";
121
122     if (isEditorEnabled()) {
123       firePropertyCommittingEvent(newValue);
124     }
125   }
126
127   public java.util.Properties JavaDoc getValue() {
128     java.util.Properties JavaDoc retProps = new java.util.Properties JavaDoc();
129
130     String JavaDoc newValue = (String JavaDoc) valueDisplay.getSelectedItem();
131     if (newValue == null)
132       newValue = "";
133
134     retProps.setProperty(property, newValue);
135
136     return retProps;
137   }
138
139   public void resetDefaultValue() {
140     valueDisplay.setSelectedItem(originalValue);
141   }
142
143   public boolean isChanged() {
144     return (!(originalValue.equals(valueDisplay.getSelectedItem())));
145   }
146
147   /**
148    * Run when the PropertyEditor may have changed enabled states.
149    */

150   protected void updateEditorEnabled() {
151     if (Pooka.isDebug())
152       System.out.println("calling ksp.updateEditorEnabled(). isEditorEnabled() = " + isEditorEnabled());
153
154     if (valueDisplay != null) {
155       valueDisplay.setEnabled(isEditorEnabled());
156     }
157   }
158
159 }
160
Popular Tags