001: // PasswordAttributeEditor.java
002: // $Id: PasswordAttributeEditor.java,v 1.4 2000/08/16 21:37:29 ylafon Exp $
003: // (c) COPYRIGHT MIT and INRIA, 1997.
004: // Please first read the full copyright statement in file COPYRIGHT.html
005:
006: package org.w3c.jigadmin.attributes;
007:
008: import javax.swing.JPanel;
009: import javax.swing.JButton;
010: import javax.swing.JPasswordField;
011: import javax.swing.JLabel;
012: import javax.swing.JDialog;
013: import javax.swing.JFrame;
014: import javax.swing.BorderFactory;
015:
016: import java.awt.BorderLayout;
017: import java.awt.Component;
018: import java.awt.Container;
019: import java.awt.Dimension;
020: import java.awt.GridLayout;
021: import java.awt.GridBagLayout;
022: import java.awt.GridBagConstraints;
023: import java.awt.Insets;
024:
025: import java.awt.event.ActionListener;
026: import java.awt.event.ActionEvent;
027:
028: import java.util.Properties;
029:
030: import org.w3c.tools.resources.Attribute;
031:
032: import org.w3c.jigsaw.admin.RemoteResource;
033: import org.w3c.jigsaw.admin.RemoteAccessException;
034:
035: import org.w3c.jigadm.RemoteResourceWrapper;
036: import org.w3c.jigadm.editors.AttributeEditor;
037:
038: class PasswordEditor extends JPanel {
039:
040: protected PasswordAttributeEditor pae;
041: protected JPasswordField passwd;
042: protected JPasswordField verify;
043: protected String orig;
044:
045: private PasswordAttributeEditor getEditor() {
046: return pae;
047: }
048:
049: ActionListener al = new ActionListener() {
050: public void actionPerformed(ActionEvent ae) {
051: if (ae.getActionCommand().equals("Ok")
052: || ae.getSource().equals(verify)) {
053: String pass = new String(passwd.getPassword());
054: String verif = new String(verify.getPassword());
055: if (pass.equals(verif) && (!pass.equals(""))) {
056: getEditor().setValue(pass);
057: getEditor().dispose();
058: } else {
059: // popup an Error? FIXME
060: passwd.requestFocus();
061: }
062: } else if (ae.getActionCommand().equals("Cancel")) {
063: getEditor().dispose();
064: } else if (ae.getSource().equals(passwd)) {
065: verify.requestFocus();
066: }
067: }
068: };
069:
070: public PasswordEditor(PasswordAttributeEditor pae, String name) {
071:
072: GridBagLayout gbl = new GridBagLayout();
073: GridBagConstraints gbc = new GridBagConstraints();
074: GridBagLayout mgbl = new GridBagLayout();
075: GridBagConstraints mgbc = new GridBagConstraints();
076: JLabel l;
077: JButton b;
078: JPanel p = new JPanel(gbl);
079:
080: this .pae = pae;
081: gbc.fill = GridBagConstraints.HORIZONTAL;
082: gbc.weightx = 0;
083: gbc.weighty = 0;
084: mgbc.fill = GridBagConstraints.NONE;
085: mgbc.weightx = 0;
086: mgbc.weighty = 0;
087: mgbc.insets = new Insets(16, 10, 16, 5);
088: setLayout(mgbl);
089: passwd = new JPasswordField(10);
090: passwd.setBorder(BorderFactory.createLoweredBevelBorder());
091: passwd.addActionListener(al);
092: verify = new JPasswordField(10);
093: verify.setBorder(BorderFactory.createLoweredBevelBorder());
094: verify.addActionListener(al);
095:
096: // Construct the first block with the labels and textfields
097: if (name != null)
098: setBorder(BorderFactory.createTitledBorder("User: " + name));
099:
100: l = new JLabel("Password: ", JLabel.RIGHT);
101: gbc.gridwidth = 1;
102: gbl.setConstraints(l, gbc);
103: p.add(l);
104: gbc.gridwidth = GridBagConstraints.REMAINDER;
105: gbl.setConstraints(passwd, gbc);
106: p.add(passwd);
107:
108: l = new JLabel("Verify: ", JLabel.RIGHT);
109: gbc.gridwidth = 1;
110: gbl.setConstraints(l, gbc);
111: p.add(l);
112: gbc.gridwidth = GridBagConstraints.REMAINDER;
113: gbl.setConstraints(verify, gbc);
114: p.add(verify);
115: mgbc.gridwidth = GridBagConstraints.REMAINDER;
116: mgbl.setConstraints(p, mgbc);
117: add(p);
118:
119: // and now the usual button bar
120: p = new JPanel(new GridLayout(1, 2, 20, 20));
121: b = new JButton("Ok");
122: b.addActionListener(al);
123: p.add(b);
124: b = new JButton("Cancel");
125: b.addActionListener(al);
126: p.add(b);
127: mgbl.setConstraints(p, mgbc);
128: add(p);
129: }
130:
131: public PasswordEditor(PasswordAttributeEditor pae) {
132: this (pae, null);
133: }
134: }
135:
136: public class PasswordAttributeEditor extends AttributeEditor {
137:
138: protected String name = null;
139: protected JButton widget = null;
140: protected JFrame frame = null;
141:
142: private String origs = null;
143: private String current = null;
144: private JDialog popup = null;
145:
146: // get rid of the password editor
147:
148: protected void dispose() {
149: if (popup != null) {
150: popup.dispose();
151: popup = null;
152: }
153: }
154:
155: // pops up a new editor
156:
157: protected void popupDialog() {
158: if (popup == null) {
159: PasswordEditor pe = new PasswordEditor(this , name);
160: popup = new JDialog(frame, "Jigsaw Password Editor", false);
161: Container cont = popup.getContentPane();
162: cont.setLayout(new BorderLayout());
163: cont.add("Center", pe);
164: popup.setSize(new Dimension(300, 200));
165: popup.setLocationRelativeTo(widget);
166: popup.show();
167: pe.passwd.requestFocus();
168: }
169: }
170:
171: protected void setText(String s) {
172: if (s.equals("")) {
173: widget.setText("WARNING: No password, click to edit");
174: } else {
175: char c[] = new char[s.length()];
176: for (int i = 0; i < s.length(); i++)
177: c[i] = '*';
178: widget.setText(new String(c));
179: }
180: }
181:
182: /**
183: * @see org.w3c.jigadm.editors.AttributeEditorInterface
184: */
185:
186: public boolean hasChanged() {
187: return !origs.equals(current);
188: }
189:
190: /**
191: * @see org.w3c.jigadm.editors.AttributeEditorInterface
192: */
193:
194: public void clearChanged() {
195: origs = new String(current);
196: setText(current);
197: }
198:
199: /**
200: * @see org.w3c.jigadm.editors.AttributeEditorInterface
201: */
202:
203: public void resetChanges() {
204: current = new String(origs);
205: setText(current);
206: }
207:
208: /**
209: * @see org.w3c.jigadm.editors.AttributeEditorInterface
210: */
211:
212: public Object getValue() {
213: if ((current != null) && (current.length() > 0)) {
214: return current;
215: }
216: return null;
217: }
218:
219: /**
220: * @see org.w3c.jigadm.editors.AttributeEditorInterface
221: */
222:
223: public void setValue(Object o) {
224: current = o.toString();
225: setText(current);
226: }
227:
228: /*
229: * @see org.w3c.jigadm.editors.AttributeEditor
230: */
231:
232: public Component getComponent() {
233: return widget;
234: }
235:
236: public PasswordAttributeEditor() {
237: widget = new JButton();
238: origs = "";
239: }
240:
241: /**
242: * Initialize the editor
243: * @param w the ResourceWrapper father of the attribute
244: * @param a the Attribute we are editing
245: * @param o the value of the above attribute
246: * @param p some Properties, used to fine-tune the editor
247: * @exception RemoteAccessException if a remote access error occurs.
248: */
249: public void initialize(RemoteResourceWrapper w, Attribute a,
250: Object o, Properties p) throws RemoteAccessException {
251: frame = ((org.w3c.jigadmin.RemoteResourceWrapper) w)
252: .getServerBrowser().getFrame();
253: RemoteResource r = w.getResource();
254: name = (String) r.getValue("identifier");
255: if (o == null) {
256: String v = null;
257: v = (String) r.getValue(a.getName());
258: if (v == null)
259: if (a.getDefault() != null)
260: v = a.getDefault().toString();
261: if (v != null) {
262: origs = v;
263: }
264: } else {
265: origs = o.toString();
266: }
267: current = origs;
268: setText(origs);
269:
270: ActionListener al = new ActionListener() {
271: public void actionPerformed(ActionEvent ae) {
272: popupDialog();
273: }
274: };
275: widget.addActionListener(al);
276: }
277: }
|