Adding JMCR-Stable version
[Benchmarks_CSolver.git] / JMCR-Stable / real-world application / jigsaw / src / org / w3c / jigadmin / attributes / PasswordAttributeEditor.java
diff --git a/JMCR-Stable/real-world application/jigsaw/src/org/w3c/jigadmin/attributes/PasswordAttributeEditor.java b/JMCR-Stable/real-world application/jigsaw/src/org/w3c/jigadmin/attributes/PasswordAttributeEditor.java
new file mode 100644 (file)
index 0000000..b8beaba
--- /dev/null
@@ -0,0 +1,279 @@
+// PasswordAttributeEditor.java\r
+// $Id: PasswordAttributeEditor.java,v 1.1 2010/06/15 12:20:45 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.jigadmin.attributes ;\r
+\r
+import javax.swing.JPanel;\r
+import javax.swing.JButton;\r
+import javax.swing.JPasswordField;\r
+import javax.swing.JLabel;\r
+import javax.swing.JDialog;\r
+import javax.swing.JFrame;\r
+import javax.swing.BorderFactory;\r
+\r
+import java.awt.BorderLayout;\r
+import java.awt.Component;\r
+import java.awt.Container;\r
+import java.awt.Dimension;\r
+import java.awt.GridLayout;\r
+import java.awt.GridBagLayout;\r
+import java.awt.GridBagConstraints;\r
+import java.awt.Insets;\r
+\r
+import java.awt.event.ActionListener;\r
+import java.awt.event.ActionEvent;\r
+\r
+import java.util.Properties;\r
+\r
+import org.w3c.tools.resources.Attribute;\r
+\r
+import org.w3c.jigsaw.admin.RemoteResource;\r
+import org.w3c.jigsaw.admin.RemoteAccessException;\r
+\r
+import org.w3c.jigadm.RemoteResourceWrapper;\r
+import org.w3c.jigadm.editors.AttributeEditor;\r
+\r
+class PasswordEditor extends JPanel {\r
+\r
+    protected PasswordAttributeEditor pae;\r
+    protected JPasswordField passwd;\r
+    protected JPasswordField verify;\r
+    protected String orig;\r
+\r
+    private PasswordAttributeEditor getEditor() {\r
+       return pae;\r
+    }\r
+\r
+    ActionListener al = new ActionListener() {\r
+       public void actionPerformed(ActionEvent ae) {\r
+           if( ae.getActionCommand().equals("Ok") || \r
+               ae.getSource().equals(verify)) {\r
+                   String pass  = new String(passwd.getPassword());\r
+                   String verif = new String(verify.getPassword());\r
+                   if(pass.equals(verif) && (! pass.equals(""))) {\r
+                       getEditor().setValue(pass);\r
+                       getEditor().dispose();\r
+                   } else {\r
+                       // popup an Error? FIXME\r
+                       passwd.requestFocus();\r
+                   }\r
+               } else if ( ae.getActionCommand().equals("Cancel")) {\r
+                   getEditor().dispose();\r
+               } else if(ae.getSource().equals(passwd)) {\r
+                   verify.requestFocus();\r
+               } \r
+       }\r
+    };\r
+\r
+    public PasswordEditor (PasswordAttributeEditor pae, String name) {\r
+\r
+       GridBagLayout gbl = new GridBagLayout();\r
+       GridBagConstraints gbc = new GridBagConstraints();\r
+       GridBagLayout mgbl = new GridBagLayout();\r
+       GridBagConstraints mgbc = new GridBagConstraints();\r
+       JLabel l;\r
+       JButton b;\r
+       JPanel p = new JPanel(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 JPasswordField(10);\r
+       passwd.setBorder(BorderFactory.createLoweredBevelBorder());\r
+       passwd.addActionListener(al);\r
+       verify = new JPasswordField(10);\r
+       verify.setBorder(BorderFactory.createLoweredBevelBorder());\r
+       verify.addActionListener(al);\r
+\r
+       // Construct the first block with the labels and textfields\r
+       if (name != null)\r
+           setBorder(BorderFactory.createTitledBorder("User: "+name));\r
+\r
+       l = new JLabel("Password: ", JLabel.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 JLabel("Verify: ", JLabel.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 JPanel(new GridLayout(1, 2, 20, 20));\r
+       b = new JButton("Ok");\r
+       b.addActionListener(al);\r
+       p.add(b);\r
+       b = new JButton("Cancel");\r
+       b.addActionListener(al);\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
+    protected String  name   = null;\r
+    protected JButton widget = null;\r
+    protected JFrame  frame  = null;\r
+\r
+    private String  origs   = null;\r
+    private String  current = null;\r
+    private JDialog 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 JDialog(frame, "Jigsaw Password Editor", false);\r
+           Container cont = popup.getContentPane();\r
+           cont.setLayout(new BorderLayout());\r
+           cont.add("Center", pe);\r
+           popup.setSize(new Dimension(300, 200));\r
+           popup.setLocationRelativeTo(widget);\r
+           popup.show();\r
+           pe.passwd.requestFocus();\r
+       }\r
+    }\r
+\r
+    protected void setText(String s) {\r
+       if(s.equals("")) {\r
+           widget.setText("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.setText(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
+       setText(current);\r
+    }\r
+\r
+    /**\r
+     * @see org.w3c.jigadm.editors.AttributeEditorInterface\r
+     */\r
+\r
+    public void resetChanges() {\r
+       current = new String(origs);\r
+       setText(current);\r
+    }\r
+\r
+    /**\r
+     * @see org.w3c.jigadm.editors.AttributeEditorInterface\r
+     */\r
+\r
+    public Object getValue() {\r
+       if ((current != null) && (current.length() > 0)) {\r
+           return current;\r
+       }\r
+       return null;\r
+    }\r
+\r
+    /**\r
+     * @see org.w3c.jigadm.editors.AttributeEditorInterface\r
+     */\r
+\r
+    public void setValue(Object o) {\r
+       current = o.toString();\r
+       setText(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 JButton();\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
+       frame = ((org.w3c.jigadmin.RemoteResourceWrapper)\r
+                w).getServerBrowser().getFrame();\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
+       setText(origs);\r
+       \r
+       ActionListener al = new ActionListener() {\r
+           public void actionPerformed(ActionEvent ae) {\r
+               popupDialog();\r
+           }\r
+       };\r
+       widget.addActionListener(al);\r
+    }\r
+}\r