Adding JMCR-Stable version
[Benchmarks_CSolver.git] / JMCR-Stable / real-world application / jigsaw / src / org / w3c / jigadmin / editors / RealmsServerHelper.java
diff --git a/JMCR-Stable/real-world application/jigsaw/src/org/w3c/jigadmin/editors/RealmsServerHelper.java b/JMCR-Stable/real-world application/jigsaw/src/org/w3c/jigadmin/editors/RealmsServerHelper.java
new file mode 100644 (file)
index 0000000..c247bdc
--- /dev/null
@@ -0,0 +1,416 @@
+// RealmsServerHelper.java\r
+// $Id: RealmsServerHelper.java,v 1.1 2010/06/15 12:25:52 smhuang Exp $\r
+// (c) COPYRIGHT MIT and INRIA, 1998.\r
+// Please first read the full copyright statement in file COPYRIGHT.html\r
+\r
+package org.w3c.jigadmin.editors;\r
+\r
+import javax.swing.JPanel;\r
+import javax.swing.JLabel;\r
+import javax.swing.JComboBox;\r
+import javax.swing.JScrollPane;\r
+import javax.swing.JButton;\r
+import javax.swing.JTextField;\r
+import javax.swing.JList;\r
+import javax.swing.ListSelectionModel;\r
+import javax.swing.BoxLayout;\r
+import javax.swing.BorderFactory;\r
+import javax.swing.border.TitledBorder;\r
+import javax.swing.event.ListSelectionListener;\r
+import javax.swing.event.ListSelectionEvent;\r
+\r
+import java.awt.Component;\r
+import java.awt.BorderLayout;\r
+import java.awt.Cursor;\r
+import java.awt.GridLayout;\r
+import java.awt.event.ActionListener;\r
+import java.awt.event.ActionEvent;\r
+\r
+import java.util.Properties;\r
+import java.util.Vector;\r
+\r
+import org.w3c.jigadmin.RemoteResourceWrapper;\r
+import org.w3c.jigadmin.PropertyManager;\r
+import org.w3c.jigadmin.gui.Message;\r
+\r
+import org.w3c.jigsaw.admin.RemoteAccessException;\r
+\r
+import org.w3c.tools.sorter.Sorter;\r
+import org.w3c.tools.widgets.Utilities;\r
+\r
+/**\r
+ * The server helper dedicated to the Realms\r
+ * @version $Revision: 1.1 $\r
+ * @author  Benoît Mahé (bmahe@w3.org)\r
+ */\r
+public class RealmsServerHelper extends JPanel \r
+    implements ServerHelperInterface\r
+{\r
+    protected final static String ADD_USER_AC  = "add_user";\r
+    protected final static String DEL_USER_AC  = "del_user";\r
+    protected final static String DEL_REALM_AC = "del_realm";\r
+\r
+    protected String                name     = null;\r
+    protected String                tooltip  = null;\r
+    protected RemoteResourceWrapper root     = null;\r
+    protected RemoteResourceWrapper realmrrw = null;\r
+    protected RemoteResourceWrapper userrrw  = null;\r
+    protected Vector                realms   = null;\r
+    protected Vector                users    = null;\r
+\r
+    protected JComboBox  combo      = null;\r
+    protected JPanel     usersPanel = null;\r
+    protected JPanel     userPanel  = null;\r
+    protected JList      usersList  = null;\r
+    protected JTextField userT      = null;\r
+\r
+    /**\r
+     * Our internal ActionListener\r
+     */\r
+    ActionListener al = new ActionListener() {\r
+       public void actionPerformed(ActionEvent e) {\r
+           Object obj = e.getSource();\r
+           if (obj == combo) {\r
+               String realm = (String)combo.getSelectedItem();\r
+               if (realms.contains(realm))\r
+               selectRealm(realm);\r
+               else if (realm.length() > 0)\r
+               addRealm(realm);\r
+           } else if (e.getActionCommand().equals(ADD_USER_AC) ||\r
+                      (obj == userT)) \r
+           {\r
+               String user = userT.getText();\r
+               if ((user.length() > 0) && (! users.contains(user)))\r
+               addUser(user);\r
+           } else if (e.getActionCommand().equals(DEL_USER_AC)) {\r
+               Thread thread = new Thread() {\r
+                   public void run() {\r
+                       deleteCurrentUser();\r
+                   }\r
+               };\r
+               thread.start();\r
+           } else if (e.getActionCommand().equals(DEL_REALM_AC)) {\r
+               Thread thread = new Thread() {\r
+                   public void run() {\r
+                       deleteCurrentRealm();\r
+                   }\r
+               };\r
+               thread.start();\r
+           }\r
+       }\r
+    };\r
+\r
+    /**\r
+     * Our internal ListSelectionListener\r
+     */\r
+    ListSelectionListener lsl = new ListSelectionListener() {\r
+       public void valueChanged(ListSelectionEvent e) {\r
+           if (! e.getValueIsAdjusting()) {\r
+               int idx = usersList.getSelectedIndex();\r
+               selectUser((String)users.elementAt(idx));\r
+           }\r
+       }    \r
+    };\r
+\r
+    /**\r
+     * Initialize this editor.\r
+     * @param name the editor name\r
+     * @param rrw the RemoteResourceWrapper wrapping the editor node.\r
+     * @param p the editor properties\r
+     */ \r
+    public void initialize(String name, \r
+                          RemoteResourceWrapper rrw, \r
+                          Properties p)\r
+    {\r
+       this.root    = rrw;\r
+       this.name    = name;\r
+       this.tooltip = (String) p.get(TOOLTIP_P);\r
+       build();\r
+    }\r
+\r
+    /**\r
+     * Build the interface\r
+     */\r
+    protected void build() {\r
+       removeAll();\r
+       String srealms[] = null;\r
+       try {\r
+           srealms = root.getResource().enumerateResourceIdentifiers();\r
+       } catch (RemoteAccessException ex) {\r
+           while (root.getServerBrowser().shouldRetry(ex)) {\r
+               try {\r
+                   srealms = \r
+                       root.getResource().enumerateResourceIdentifiers();\r
+                   break;\r
+               } catch (RemoteAccessException ex2) {\r
+                   ex = ex2;\r
+               }\r
+           }\r
+       }\r
+       if (srealms == null)\r
+           return;\r
+\r
+       Sorter.sortStringArray(srealms, true);\r
+       realms = new Vector(srealms.length);\r
+       for (int i = 0 ; i < srealms.length; i++) \r
+           realms.addElement(srealms[i]);\r
+\r
+       setLayout(new BorderLayout());\r
+\r
+       JPanel comboPanel = new JPanel();\r
+       combo = new JComboBox(realms);\r
+       combo.setEditable(true);\r
+       combo.addActionListener(al);\r
+\r
+       JLabel realmLabel1 = new JLabel("Enter the realm name or");\r
+        JLabel realmLabel2 = new JLabel("select one from the list:");\r
+        realmLabel1.setAlignmentX(CENTER_ALIGNMENT);\r
+        realmLabel2.setAlignmentX(CENTER_ALIGNMENT);\r
+\r
+       comboPanel.setLayout(new BoxLayout(comboPanel, BoxLayout.Y_AXIS));\r
+        comboPanel.add(realmLabel1);\r
+        comboPanel.add(realmLabel2);\r
+        comboPanel.add(combo);\r
+       TitledBorder border = BorderFactory.createTitledBorder("Realms");\r
+       border.setTitleFont(Utilities.mediumBoldFont);\r
+       comboPanel.setBorder(border);\r
+\r
+       usersPanel = new JPanel(new BorderLayout(4,4));\r
+       border = BorderFactory.createTitledBorder("Realm");\r
+       border.setTitleFont(Utilities.mediumBoldFont);\r
+       usersPanel.setBorder(border);\r
+\r
+       JPanel realmsPanel = new JPanel(new BorderLayout());\r
+       realmsPanel.add(comboPanel, BorderLayout.NORTH);\r
+       realmsPanel.add(usersPanel, BorderLayout.CENTER);\r
+\r
+       userPanel = new JPanel(new BorderLayout());\r
+       initUserPanel();\r
+\r
+       add(realmsPanel, BorderLayout.WEST);\r
+       add(userPanel, BorderLayout.CENTER);\r
+    }\r
+\r
+    /**\r
+     * Initialize the User Panel.\r
+     */\r
+    protected void initUserPanel() {\r
+       userPanel.removeAll();\r
+       userPanel.invalidate();\r
+       TitledBorder border = BorderFactory.createTitledBorder("User");\r
+       border.setTitleFont(Utilities.mediumBoldFont);\r
+       userPanel.setBorder(border);\r
+       userPanel.validate();\r
+    }\r
+\r
+    /**\r
+     * Select (and display) the given realm.\r
+     * @param realm the Realm to select\r
+     */\r
+    protected void selectRealm(String realm) {\r
+       initUserPanel();\r
+       usersPanel.removeAll();\r
+       users = null;\r
+       realmrrw = null;\r
+\r
+       try {\r
+           realmrrw = root.getChildResource(realm);\r
+       } catch (RemoteAccessException ex) {\r
+           while (root.getServerBrowser().shouldRetry(ex)) {\r
+               try {\r
+                   realmrrw = root.getChildResource(realm);\r
+                   break;\r
+               } catch (RemoteAccessException ex2) {\r
+                   ex = ex2;\r
+               }\r
+           }\r
+       }\r
+       \r
+       if (realmrrw == null)\r
+           return;\r
+\r
+       try {\r
+           String susers[] = \r
+               realmrrw.getResource().enumerateResourceIdentifiers();\r
+           Sorter.sortStringArray(susers, true);\r
+           users = new Vector(susers.length);\r
+           for (int i = 0 ; i < susers.length; i++) \r
+               users.addElement(susers[i]);\r
+       } catch (RemoteAccessException ex) {\r
+           Message.showErrorMessage(this, ex);\r
+       }\r
+\r
+       if (users == null)\r
+           return;\r
+\r
+       //graphics...\r
+       usersPanel.invalidate();\r
+       if (usersList != null)\r
+           usersList.removeListSelectionListener(lsl);\r
+       usersList = new JList(users);\r
+       usersList.addListSelectionListener(lsl);\r
+       usersList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);\r
+       usersList.setBorder(BorderFactory.createLoweredBevelBorder());\r
+\r
+       JButton addUserB = new JButton("Add user");\r
+       addUserB.setMargin(Utilities.insets0);\r
+       addUserB.setActionCommand(ADD_USER_AC);\r
+       addUserB.addActionListener(al);\r
+       \r
+       JButton delRealmB = new JButton("Delete "+realm);\r
+       delRealmB.setMargin(Utilities.insets0);\r
+       delRealmB.setActionCommand(DEL_REALM_AC);\r
+       delRealmB.addActionListener(al);\r
+\r
+       userT = new JTextField(8);\r
+       userT.addActionListener(al);\r
+\r
+       JPanel p1 = new JPanel(new BorderLayout(1,4));\r
+       p1.add(userT, BorderLayout.WEST);\r
+       p1.add(addUserB, BorderLayout.CENTER);\r
+       //p1.setBorder(BorderFactory.createEtchedBorder());\r
+\r
+       JPanel p2 = new JPanel(new GridLayout(2,1));\r
+       p2.add(p1);\r
+       p2.add(delRealmB);\r
+\r
+       TitledBorder border = \r
+           BorderFactory.createTitledBorder("Realm: "+realm);\r
+       border.setTitleFont(Utilities.mediumBoldFont);\r
+       usersPanel.setBorder(border);\r
+       usersPanel.add(new JScrollPane(usersList), BorderLayout.CENTER);\r
+       usersPanel.add(p2, BorderLayout.SOUTH);\r
+       usersPanel.validate();\r
+    }\r
+\r
+    /**\r
+     * Select (and display) the given user.\r
+     * @param realm the User to select\r
+     */\r
+    protected void selectUser(String user) {\r
+       try {\r
+           if (realmrrw == null) //no realm selected\r
+               return;\r
+           root.getServerBrowser().setCursor(Cursor.WAIT_CURSOR);\r
+           userrrw = realmrrw.getChildResource(user);\r
+           userPanel.removeAll();\r
+           userPanel.invalidate();\r
+           TitledBorder border = \r
+               BorderFactory.createTitledBorder("User: "+user);\r
+           border.setTitleFont(Utilities.mediumBoldFont);\r
+           userPanel.setBorder(border);\r
+           AttributesHelper helper = new AttributesHelper();\r
+           PropertyManager pm = PropertyManager.getPropertyManager();\r
+           Properties props = pm.getEditorProperties(userrrw);\r
+           helper.initialize(userrrw, props);\r
+\r
+           JButton delUserB = new JButton("Delete user "+user);\r
+           delUserB.setMargin(Utilities.insets0);\r
+           delUserB.setActionCommand(DEL_USER_AC);\r
+           delUserB.addActionListener(al);\r
+\r
+           userPanel.add(helper.getComponent(), BorderLayout.CENTER);\r
+           userPanel.add(delUserB, BorderLayout.SOUTH);\r
+           userPanel.validate();\r
+           root.getServerBrowser().setCursor(Cursor.DEFAULT_CURSOR);\r
+       } catch (RemoteAccessException ex) {\r
+           Message.showErrorMessage(this, ex);\r
+       }\r
+    }\r
+\r
+    /**\r
+     * Add a user to the current selected realm.\r
+     * @param user the user name\r
+     */\r
+    protected void addUser(String user) {\r
+       try {\r
+           if (realmrrw == null)\r
+               return;\r
+           realmrrw.getResource().\r
+               registerResource(user,\r
+                                "org.w3c.jigsaw.auth.AuthUser");\r
+           selectRealm((String)combo.getSelectedItem());\r
+       } catch (RemoteAccessException ex) {\r
+           Message.showErrorMessage(this, ex);\r
+       }\r
+    }\r
+\r
+    /**\r
+     * Delete the selected user.\r
+     */\r
+    protected void deleteCurrentUser() {\r
+       if (userrrw == null)\r
+           return;\r
+       try {\r
+           userrrw.getResource().delete();\r
+           selectRealm((String)combo.getSelectedItem());\r
+       } catch (RemoteAccessException ex) {\r
+           Message.showErrorMessage(this, ex);\r
+       }\r
+    }\r
+\r
+    /**\r
+     * Create a new Realm.\r
+     * @param realm The name of the new realm.\r
+     */\r
+    protected void addRealm(String realm) {\r
+       try {\r
+           root.getResource().\r
+               registerResource(realm,\r
+                                "org.w3c.jigsaw.auth.AuthRealm");\r
+           combo.invalidate();\r
+           combo.addItem(realm);\r
+           combo.validate();\r
+           selectRealm(realm);\r
+       } catch (RemoteAccessException ex) {\r
+           Message.showErrorMessage(this, ex);\r
+       }\r
+    }\r
+\r
+    /**\r
+     * Delete the current realm.\r
+     */\r
+    protected void deleteCurrentRealm() {\r
+       if (realmrrw == null) //no realm selected\r
+           return;\r
+       try {\r
+           realmrrw.getResource().delete();\r
+           invalidate();\r
+           build();\r
+           validate();\r
+       } catch (RemoteAccessException ex) {\r
+           Message.showErrorMessage(this, ex);\r
+       }\r
+    }\r
+\r
+    /**\r
+     * Get the helper name.\r
+     * @return a String instance\r
+     */\r
+    public String getName() {\r
+       return name;\r
+    }\r
+\r
+    /**\r
+     * Get the helper tooltip\r
+     * @return a String\r
+     */  \r
+    public String getToolTip() {\r
+       return tooltip;\r
+    }\r
+\r
+    /**\r
+     * Get the Component.\r
+     * @return a Component instance\r
+     */\r
+    public Component getComponent() {\r
+       return this;\r
+    }\r
+\r
+    /**\r
+     * Constructor.\r
+     */\r
+    public RealmsServerHelper() {\r
+    }\r
+\r
+}\r