Adding JMCR-Stable version
[Benchmarks_CSolver.git] / JMCR-Stable / real-world application / jigsaw / src / org / w3c / jigadm / editors / PasswordAttributeEditor.java
diff --git a/JMCR-Stable/real-world application/jigsaw/src/org/w3c/jigadm/editors/PasswordAttributeEditor.java b/JMCR-Stable/real-world application/jigsaw/src/org/w3c/jigadm/editors/PasswordAttributeEditor.java
new file mode 100644 (file)
index 0000000..0e16931
--- /dev/null
@@ -0,0 +1,274 @@
+// PasswordAttributeEditor.java\r
+// $Id: PasswordAttributeEditor.java,v 1.1 2010/06/15 12:22:44 smhuang Exp $\r
+// (c) COPYRIGHT MIT and INRIA, 1997.\r
+// Please first read the full copyright statement in file COPYRIGHT.html\r
+\r
+package org.w3c.jigadm.editors ;\r
+\r
+import java.awt.BorderLayout;\r
+import java.awt.Button;\r
+import java.awt.Color;\r
+import java.awt.Component;\r
+import java.awt.Container;\r
+import java.awt.Dimension;\r
+import java.awt.Frame;\r
+import java.awt.GridBagConstraints;\r
+import java.awt.GridBagLayout;\r
+import java.awt.GridLayout;\r
+import java.awt.Image;\r
+import java.awt.Insets;\r
+import java.awt.Label;\r
+import java.awt.Panel;\r
+import java.awt.TextComponent;\r
+import java.awt.TextField;\r
+import java.awt.Window;\r
+\r
+import java.awt.event.ActionEvent;\r
+import java.awt.event.ActionListener;\r
+\r
+import java.util.EventObject;\r
+import java.util.Properties;\r
+\r
+import org.w3c.tools.resources.Attribute;\r
+\r
+import org.w3c.jigsaw.admin.RemoteAccessException;\r
+import org.w3c.jigsaw.admin.RemoteResource;\r
+\r
+import org.w3c.jigadm.RemoteResourceWrapper;\r
+\r
+class PasswordEditor extends Panel implements ActionListener {\r
+\r
+    protected PasswordAttributeEditor pae;\r
+    protected TextField passwd;\r
+    protected TextField verify;\r
+    protected String orig;\r
+    protected Image img;\r
+\r
+    public void actionPerformed(ActionEvent ae) {\r
+       if( ae.getActionCommand().equals("Ok") || \r
+           ae.getSource().equals(verify)) {\r
+           if(passwd.getText().equals(verify.getText())\r
+              && !passwd.getText().equals("")) {\r
+               pae.setValue(passwd.getText());\r
+               pae.dispose();\r
+           } else {\r
+               // popup an Error? FIXME\r
+               passwd.requestFocus();\r
+           }\r
+       } else if ( ae.getActionCommand().equals("Cancel")) {\r
+           pae.dispose();\r
+       } else if(ae.getSource().equals(passwd)) {\r
+           verify.requestFocus();\r
+       } \r
+    }\r
+\r
+    public PasswordEditor (PasswordAttributeEditor pae, String name) {\r
+       GridBagLayout gbl = new GridBagLayout();\r
+       GridBagConstraints gbc = new GridBagConstraints();\r
+       GridBagLayout mgbl = new GridBagLayout();\r
+       GridBagConstraints mgbc = new GridBagConstraints();\r
+       Label l;\r
+       Button b;\r
+       Panel p = new Panel(gbl);\r
+\r
+       this.pae = pae;\r
+       gbc.fill = GridBagConstraints.HORIZONTAL;\r
+       gbc.weightx = 0;\r
+       gbc.weighty = 0;\r
+       mgbc.fill = GridBagConstraints.NONE;\r
+       mgbc.weightx = 0;\r
+       mgbc.weighty = 0;\r
+       mgbc.insets = new Insets(16, 10, 16, 5);\r
+       setLayout(mgbl);\r
+       passwd = new TextField(10);\r
+       passwd.setEchoChar('*');\r
+       passwd.addActionListener(this);\r
+       verify = new TextField(10);\r
+       verify.setEchoChar('*');\r
+       verify.addActionListener(this);\r
+\r
+       // Construct the first block with the labels and textfields\r
+       if (name != null) {\r
+           l = new Label("User: ", Label.RIGHT);\r
+           gbc.gridwidth = 1;\r
+           gbl.setConstraints(l, gbc);\r
+           p.add(l);\r
+           l = new Label(name);\r
+           gbc.gridwidth = GridBagConstraints.REMAINDER;\r
+           gbl.setConstraints(l, gbc);\r
+           p.add(l);\r
+       }\r
+       l = new Label("Password: ", Label.RIGHT);\r
+       gbc.gridwidth = 1;\r
+       gbl.setConstraints(l, gbc);\r
+       p.add(l);\r
+       gbc.gridwidth = GridBagConstraints.REMAINDER;\r
+       gbl.setConstraints(passwd, gbc);\r
+       p.add(passwd);\r
+\r
+       l = new Label("Verify: ", Label.RIGHT);\r
+       gbc.gridwidth = 1;\r
+       gbl.setConstraints(l, gbc);\r
+       p.add(l);\r
+       gbc.gridwidth = GridBagConstraints.REMAINDER;\r
+       gbl.setConstraints(verify, gbc);\r
+       p.add(verify);\r
+       mgbc.gridwidth = GridBagConstraints.REMAINDER;\r
+       mgbl.setConstraints(p, mgbc);\r
+       add(p);\r
+       \r
+       // and now the usual button bar\r
+       p = new Panel(new GridLayout(1, 2, 20, 20));\r
+       b = new Button("Ok");\r
+       b.addActionListener(this);\r
+       p.add(b);\r
+       b = new Button("Cancel");\r
+       b.addActionListener(this);\r
+       p.add(b);\r
+       mgbl.setConstraints(p, mgbc);\r
+       add(p);\r
+    }\r
+\r
+    public PasswordEditor (PasswordAttributeEditor pae) {\r
+       this(pae, null);\r
+    }\r
+}\r
+\r
+public class PasswordAttributeEditor extends AttributeEditor {\r
+\r
+  class PasswordEditorListener implements ActionListener {\r
+       \r
+        public void actionPerformed(ActionEvent ae) {\r
+           popupDialog();\r
+       }\r
+\r
+    }\r
+\r
+    protected String name = null;\r
+    private String origs;\r
+    private String current;\r
+    Button  widget;\r
+    private Frame popup = null;\r
+\r
+    // get rid of the password editor\r
+\r
+    protected void dispose() {\r
+       if(popup != null) {\r
+           popup.dispose();\r
+           popup = null;\r
+       }\r
+    }\r
+\r
+    // pops up a new editor\r
+\r
+    protected void popupDialog() {\r
+       if(popup == null) {\r
+           PasswordEditor pe = new PasswordEditor(this, name);\r
+           popup = new Frame("Jigsaw Password Editor");\r
+           popup.setBackground(Color.lightGray);\r
+           popup.setSize(new Dimension(300, 200));\r
+           popup.setLayout(new BorderLayout());\r
+           popup.add("Center", pe);\r
+           popup.show();\r
+           pe.passwd.requestFocus();\r
+       }\r
+    }\r
+\r
+    protected void setLabel(String s) {\r
+       if(s.equals("")) {\r
+           widget.setLabel("WARNING: No password, click to edit");\r
+       } else {\r
+           char c[] = new char[s.length()];\r
+           for(int i=0; i<s.length(); i++)\r
+               c[i] = '*';\r
+           widget.setLabel(new String(c));\r
+       }\r
+    }\r
+\r
+    /**\r
+     * @see org.w3c.jigadm.editors.AttributeEditorInterface\r
+     */\r
+\r
+    public boolean hasChanged() {\r
+       return !origs.equals(current);\r
+    }\r
+\r
+    /**\r
+     * @see org.w3c.jigadm.editors.AttributeEditorInterface\r
+     */\r
+\r
+    public void clearChanged() {\r
+       origs = new String(current);\r
+       setLabel(current);\r
+    }\r
+\r
+    /**\r
+     * @see org.w3c.jigadm.editors.AttributeEditorInterface\r
+     */\r
+\r
+    public void resetChanges() {\r
+       current = new String(origs);\r
+       setLabel(current);\r
+    }\r
+\r
+    /**\r
+     * @see org.w3c.jigadm.editors.AttributeEditorInterface\r
+     */\r
+\r
+    public Object getValue() {\r
+       return current;\r
+    }\r
+\r
+    /**\r
+     * @see org.w3c.jigadm.editors.AttributeEditorInterface\r
+     */\r
+\r
+    public void setValue(Object o) {\r
+       current = o.toString();\r
+       setLabel(current);\r
+    }\r
+\r
+    /*\r
+     * @see org.w3c.jigadm.editors.AttributeEditor\r
+     */\r
+\r
+    public Component getComponent() {\r
+       return widget;\r
+    }\r
+\r
+    public PasswordAttributeEditor() {\r
+       widget = new Button();\r
+       origs = "";\r
+    }\r
+\r
+    /**\r
+     * Initialize the editor\r
+     * @param w the ResourceWrapper father of the attribute\r
+     * @param a the Attribute we are editing\r
+     * @param o the value of the above attribute\r
+     * @param p some Properties, used to fine-tune the editor\r
+     * @exception RemoteAccessException if a remote access error occurs.\r
+     */\r
+    public void initialize(RemoteResourceWrapper w, Attribute a,  Object o,\r
+                          Properties p)\r
+       throws RemoteAccessException\r
+    {\r
+       RemoteResource r = w.getResource();\r
+       name = (String) r.getValue("identifier");\r
+       if(o == null) {\r
+           String v = null;\r
+           v = (String) r.getValue(a.getName());\r
+           if(v == null)\r
+               if(a.getDefault() != null)\r
+                   v = a.getDefault().toString();\r
+           if ( v != null ) {\r
+               origs = v;\r
+           } \r
+       } else {\r
+           origs = o.toString();\r
+       }\r
+       current = origs;\r
+       setLabel(origs);\r
+       widget.addActionListener(new PasswordEditorListener());\r
+    }\r
+}\r