Adding JMCR-Stable version
[Benchmarks_CSolver.git] / JMCR-Stable / real-world application / jigsaw / src / org / w3c / jigadmin / editors / AttributesHelper.java
diff --git a/JMCR-Stable/real-world application/jigsaw/src/org/w3c/jigadmin/editors/AttributesHelper.java b/JMCR-Stable/real-world application/jigsaw/src/org/w3c/jigadmin/editors/AttributesHelper.java
new file mode 100644 (file)
index 0000000..4d27b90
--- /dev/null
@@ -0,0 +1,379 @@
+// AttributeHelper.java\r
+// $Id: AttributesHelper.java,v 1.1 2010/06/15 12:25:54 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.editors ;\r
+\r
+import java.awt.Component;\r
+import java.awt.Color;\r
+import java.awt.ScrollPane;\r
+import java.awt.BorderLayout;\r
+import java.awt.GridBagLayout;\r
+import java.awt.GridBagConstraints;\r
+import java.awt.event.ActionListener;\r
+import java.awt.event.ActionEvent;\r
+import java.awt.event.MouseAdapter;\r
+import java.awt.event.MouseEvent;\r
+\r
+import javax.swing.JPanel;\r
+import javax.swing.JLabel;\r
+import javax.swing.JButton;\r
+import javax.swing.BorderFactory;\r
+import javax.swing.border.BevelBorder;\r
+import javax.swing.JScrollPane;\r
+\r
+import java.util.Properties;\r
+\r
+import org.w3c.jigadmin.RemoteResourceWrapper;\r
+import org.w3c.jigadmin.PropertyManager;\r
+import org.w3c.jigadm.editors.AttributeEditor;\r
+import org.w3c.jigadm.events.ResourceChangeEvent;\r
+\r
+import org.w3c.jigsaw.admin.RemoteResource;\r
+import org.w3c.jigsaw.admin.RemoteAccessException;\r
+\r
+import org.w3c.tools.resources.Attribute;\r
+import org.w3c.tools.resources.serialization.AttributeDescription;\r
+import org.w3c.tools.widgets.Utilities;\r
+\r
+/**\r
+ * The Attributes helper\r
+ * @version $Revision: 1.1 $\r
+ * @author  Benoît Mahé (bmahe@w3.org)\r
+ */\r
+public class AttributesHelper extends ResourceHelper {\r
+\r
+    /**\r
+     * Our internal ActionListener\r
+     */\r
+    ActionListener al = new ActionListener() {\r
+       public void actionPerformed(ActionEvent ae) {\r
+           if (ae.getActionCommand().equals("Reset"))\r
+               resetChanges();\r
+           else if (ae.getActionCommand().equals(COMMIT_L)) {\r
+               Thread commiter = new Thread() {\r
+                   public void run() {\r
+                       try {\r
+                           commitChanges();\r
+                       } catch (RemoteAccessException ex) {\r
+                           errorPopup("RemoteAccessException",ex);\r
+                       }\r
+                   }\r
+               };\r
+               setMessage("Committing...");\r
+               commiter.start();\r
+               setMessage("Commit done.");\r
+           } \r
+       }\r
+    };\r
+\r
+    /**\r
+     * Our internal MouseListener\r
+     */\r
+    MouseAdapter ma = new MouseAdapter() {\r
+       public void mouseEntered(MouseEvent e) {\r
+           Component comp = e.getComponent();\r
+           if (comp instanceof JButton) {\r
+               String action = ((JButton)comp).getActionCommand();\r
+               if (action.equals(COMMIT_L)) {\r
+                   setMessage("Commit the changes to the server.");\r
+               } else if (action.equals(RESET_L)) {\r
+                   setMessage("Reset changes");\r
+               }\r
+           }\r
+       }\r
+\r
+       public void mouseExited(MouseEvent e) {\r
+           setMessage("");\r
+       }\r
+    };\r
+\r
+    private RemoteResourceWrapper rrw = null;\r
+    private AttributeDescription[] a = null;\r
+    private AttributeEditor[] ae = null;\r
+    private boolean initialized = false;\r
+\r
+    protected static final String COMMIT_L = "Commit"; \r
+    protected static final String RESET_L = "Reset"; \r
+\r
+    JPanel widget;\r
+    JLabel message;\r
+\r
+    /**\r
+     * Set the message of the information Label.\r
+     * @param msg the message to display\r
+     */\r
+    public void setMessage(String msg) {\r
+       message.setText(msg);\r
+    }\r
+\r
+    /**\r
+     * Commit changes (if any)\r
+     * @exception RemoteAccessException if a remote access error occurs.\r
+     */\r
+    public void commitChanges()\r
+       throws RemoteAccessException\r
+    {\r
+       if(!initialized)\r
+           return;\r
+\r
+       int num = 0;\r
+        for(int i=0; i<ae.length; i++) {\r
+            if (ae[i].hasChanged())\r
+                num++;\r
+        }\r
+       boolean authorized;\r
+        String s[] = new String[num];\r
+       Object o[] = new Object[num];\r
+        num = 0;\r
+        for(int i=0; i<ae.length; i++) {\r
+            if (ae[i].hasChanged()) {\r
+                s[num] = a[i].getName();\r
+               o[num] = ae[i].getValue();\r
+               if(s[num].equals("identifier")) {\r
+                   // should send an event FIXME!!!!\r
+                   if(rrw.getBrowser() != null)\r
+                       rrw.getBrowser().renameNode(rrw, (String)o[num]);\r
+                   processEvent(new ResourceChangeEvent(rrw,\r
+                                                        "identifier",\r
+                                                        null,\r
+                                                        o[num]));\r
+               }\r
+               num++;\r
+           }\r
+       }\r
+       authorized = false;\r
+       while(!authorized) {\r
+           try {\r
+               authorized = true;\r
+               rrw.getResource().setValues(s, o);\r
+           } catch (RemoteAccessException ex) {\r
+               if(ex.getMessage().equals("Unauthorized")) {\r
+                   authorized = false;\r
+               } else {\r
+                   throw ex;\r
+               }\r
+           } finally {\r
+               if(!authorized) {\r
+                   rrw.getServerBrowser().popupPasswdDialog("admin");\r
+               }\r
+           }\r
+       }\r
+       clearChanged();\r
+       // FIXME propagate event\r
+    }\r
+       \r
+    /**\r
+     * tells if the edited resource in the helper has changed\r
+     * @return <strong>true</strong> if the values changed.\r
+     * to get more informations about what has changed, you can use the \r
+     * three methods below.\r
+     */\r
+    public boolean hasChanged() {\r
+       if(ae == null)\r
+           return false;\r
+       boolean changed = false;\r
+       for(int i=0; !changed && i<ae.length; i++) {\r
+           changed = ae[i].hasChanged();\r
+       }\r
+       return changed;\r
+    }\r
+\r
+    /**\r
+     * undo the not-yet-commited changes\r
+     */\r
+    public void resetChanges() {\r
+       if(ae == null)\r
+           return;\r
+\r
+       for(int i=0; i<ae.length; i++) {\r
+           if(ae[i].hasChanged())\r
+               ae[i].resetChanges();\r
+       }\r
+    }\r
+\r
+    /**\r
+     * set the current resource to be the original resource (ie: the\r
+     * hasChanged() method must return <strong>false</false> now.\r
+     * to do a "fine tuned" reset, use one of the three following method.\r
+     */\r
+    public void clearChanged() {\r
+       if(ae == null)\r
+           return;\r
+       for(int i=0; i<ae.length; i++) {\r
+           if (ae[i].hasChanged())\r
+               ae[i].clearChanged();\r
+       }\r
+    }\r
+\r
+    /**\r
+     * Get the AttributeHelper component\r
+     */\r
+    public Component getComponent() {\r
+       return widget;\r
+    }\r
+\r
+    /**\r
+     * Get the AttributeHelper title\r
+     */    \r
+    public final String getTitle() {\r
+       return "Attribute";\r
+    }\r
+\r
+    /**\r
+     * Constructor.\r
+     */\r
+    public AttributesHelper() {\r
+       widget = new JPanel(new BorderLayout());\r
+    }\r
+\r
+    /**\r
+     * initialize the helper\r
+     * @param r the ResourceWrapper containing the Resource edited with \r
+     * this helper\r
+     * @param p some Properties, used to fine-tune the helper\r
+     * @exception RemoteAccessException if a remote access error occurs.\r
+     */\r
+    public void initialize(org.w3c.jigadm.RemoteResourceWrapper r, \r
+                          Properties pr)\r
+       throws RemoteAccessException\r
+    {\r
+       if(initialized)\r
+           return;\r
+\r
+       RemoteResource rr;\r
+       AttributeDescription b[] = null;\r
+       String s[] = null;\r
+       int nbn = 0;\r
+       boolean authorized;\r
+       \r
+\r
+       this.rrw = (RemoteResourceWrapper)r;\r
+       rr = rrw.getResource();\r
+       authorized = false;\r
+       while(!authorized) {\r
+           try {\r
+               authorized = true;\r
+               b = rr.getAttributes();\r
+           } catch (RemoteAccessException ex) {\r
+               if(ex.getMessage().equals("Unauthorized")) {\r
+                   authorized = false;\r
+               } else {\r
+                   throw ex;\r
+               }\r
+           } finally {\r
+               if(!authorized) {\r
+                   rrw.getServerBrowser().popupPasswdDialog("admin");\r
+               }\r
+           }\r
+       }\r
+       // we select only the editable Attributes.\r
+       for(int i=0; i<b.length; i++)\r
+           if(b[i] == null)\r
+               nbn++;\r
+           else\r
+               if(!b[i].getAttribute().checkFlag(Attribute.EDITABLE))\r
+                   nbn++;\r
+       a  = new AttributeDescription[b.length-nbn];\r
+       ae = new AttributeEditor[a.length];\r
+       int j = 0;\r
+       for(int i=0; i<b.length; i++) {\r
+           if(b[i] != null && \r
+              b[i].getAttribute().checkFlag(Attribute.EDITABLE)) {\r
+               a[j++] = b[i];\r
+           }\r
+       }\r
+\r
+       // add all the attribute editors\r
+\r
+       JLabel l;\r
+       GridBagLayout gbl = new GridBagLayout();\r
+       GridBagConstraints gbc = new GridBagConstraints();\r
+       JPanel p = new JPanel(gbl);\r
+       gbc.fill = GridBagConstraints.HORIZONTAL;\r
+       gbc.weightx = 0;\r
+       gbc.weighty = 0;\r
+       gbc.insets = Utilities.insets4;\r
+       for(int i = 0 ; i < a.length ; i++) {\r
+           if(a[i] != null) {\r
+               PropertyManager pm = PropertyManager.getPropertyManager();\r
+               Properties attrProps = \r
+                   pm.getAttributeProperties(rrw, a[i].getAttribute());\r
+               String labelText = (String) attrProps.get("label");\r
+               if ( labelText == null )\r
+                   labelText = a[i].getName();\r
+               l = new JLabel(labelText, JLabel.RIGHT);\r
+               ae[i] = AttributeEditorFactory.getEditor(rrw, \r
+                                                        a[i].getAttribute());\r
+               authorized = false;\r
+               while(!authorized) {\r
+                   try {\r
+                       authorized = true;\r
+                       ae[i].initialize(rrw, a[i].getAttribute(), \r
+                                        a[i].getValue(), attrProps);\r
+                   } catch (RemoteAccessException ex) {\r
+                       if(ex.getMessage().equals("Unauthorized")) {\r
+                           authorized = false;\r
+                       } else {\r
+                           throw ex;\r
+                       }\r
+                   } finally {\r
+                       if(!authorized) {\r
+                           rrw.getServerBrowser().popupPasswdDialog("admin");\r
+                       }\r
+                   }\r
+               }\r
+               gbc.gridwidth = 1;\r
+               gbl.setConstraints(l, gbc);\r
+               p.add(l);\r
+               gbc.gridwidth = GridBagConstraints.REMAINDER;\r
+               gbl.setConstraints(ae[i].getComponent(), gbc);\r
+               p.add(ae[i].getComponent());\r
+           }\r
+       }\r
+\r
+       JScrollPane pwidget = new JScrollPane(p);\r
+       //pwidget.add(p);\r
+       widget.add("Center", pwidget);\r
+       // Now add the reset/commit button bar\r
+\r
+       JPanel  toolpane= new JPanel(new BorderLayout());\r
+       JButton commitb = new JButton(COMMIT_L);\r
+       JButton resetb  = new JButton(RESET_L);\r
+\r
+       commitb.addMouseListener(ma);\r
+       resetb.addMouseListener(ma);\r
+\r
+       commitb.addActionListener(al);\r
+       resetb.addActionListener(al);\r
+\r
+       message = new JLabel("", JLabel.CENTER);\r
+       message.setForeground(Color.white);\r
+       message.setBackground(Color.gray);\r
+\r
+       JPanel pmsg = new JPanel(new BorderLayout());\r
+       pmsg.setBorder(BorderFactory.createBevelBorder(BevelBorder.LOWERED));\r
+       pmsg.add("Center", message);\r
+\r
+       toolpane.add("West", commitb);\r
+       toolpane.add("Center", pmsg);\r
+       toolpane.add("East", resetb);\r
+\r
+       widget.add("South", toolpane);\r
+       // add information about the class of the resource edited\r
+\r
+       String classes[] = {""};\r
+       try {\r
+           classes = rr.getClassHierarchy();\r
+       } catch (RemoteAccessException ex) {\r
+           // big trouble but it may be temporary and this information\r
+           // is not vital, so just warn\r
+           ex.printStackTrace();\r
+       }\r
+       l = new JLabel("Class: " + classes[0], JLabel.CENTER);\r
+       l.setForeground(new Color(0,0,128));\r
+       widget.add("North", l);\r
+       initialized = true;\r
+    }\r
+}\r