Adding JMCR-Stable version
[Benchmarks_CSolver.git] / JMCR-Stable / real-world application / jigsaw / src / org / w3c / jigadmin / attributes / DateAttributeEditor.java
diff --git a/JMCR-Stable/real-world application/jigsaw/src/org/w3c/jigadmin/attributes/DateAttributeEditor.java b/JMCR-Stable/real-world application/jigsaw/src/org/w3c/jigadmin/attributes/DateAttributeEditor.java
new file mode 100644 (file)
index 0000000..42b513c
--- /dev/null
@@ -0,0 +1,332 @@
+// DateAttributeEditor.java\r
+// $Id: DateAttributeEditor.java,v 1.1 2010/06/15 12:20:46 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 java.awt.Component;\r
+import java.awt.GridLayout;\r
+import java.awt.BorderLayout;\r
+\r
+import java.awt.event.ActionListener;\r
+import java.awt.event.ActionEvent;\r
+\r
+import javax.swing.JPanel;\r
+import javax.swing.JLabel;\r
+import javax.swing.JButton;\r
+import javax.swing.ImageIcon;\r
+import javax.swing.BorderFactory;\r
+\r
+import java.util.Properties;\r
+import java.util.Date;\r
+import java.util.Calendar;\r
+\r
+import org.w3c.jigsaw.admin.RemoteAccessException;\r
+import org.w3c.jigsaw.admin.RemoteResource;\r
+\r
+import org.w3c.jigadmin.widgets.Icons;\r
+\r
+import org.w3c.jigadm.RemoteResourceWrapper;\r
+import org.w3c.jigadm.editors.AttributeEditor;\r
+\r
+import org.w3c.tools.resources.Attribute;\r
+import org.w3c.tools.widgets.Utilities;\r
+\r
+public class DateAttributeEditor extends AttributeEditor {\r
+\r
+    /** \r
+     * an inner ActionListener for the '+' and '-' Buttons\r
+     */\r
+\r
+    class DateActionListener implements ActionListener {\r
+       int field = 0;\r
+       \r
+        public void actionPerformed(ActionEvent ae) {\r
+           if(ae.getActionCommand().equals("+"))\r
+               updateValue(field, true);\r
+           else\r
+               updateValue(field, false);\r
+       }\r
+\r
+      DateActionListener(int f) {\r
+           field = f;\r
+       }\r
+    }\r
+\r
+    private Calendar c;\r
+    private Date origd;\r
+    Date currd;\r
+    JPanel widget;\r
+    private JLabel h, min, s, d, m, y;\r
+    private static final String smonth[] = {"Jan", "Feb", "Mar", "Apr",\r
+                                           "May", "Jun", "Jul", "Aug",\r
+                                           "Sep", "Oct", "Nov", "Dec" };\r
+\r
+    /**\r
+     * reset the strings in the Textfields according to the new date\r
+     */\r
+\r
+    private void updateFields() {\r
+       h.setText((new Integer(c.get(Calendar.HOUR_OF_DAY))).toString());\r
+       min.setText((new Integer(c.get(Calendar.MINUTE))).toString());\r
+       s.setText((new Integer(c.get(Calendar.SECOND))).toString());\r
+       d.setText((new Integer(c.get(Calendar.DAY_OF_MONTH))).toString());\r
+       m.setText(smonth[c.get(Calendar.MONTH)]);\r
+       y.setText((new Integer(c.get(Calendar.YEAR))).toString());\r
+    }\r
+\r
+    /**\r
+     * update the new date value, according to the field defined in\r
+     * the Calendar\r
+     * @see Calendar\r
+     * @param field the field of the Calendar to be modified\r
+     * @param plus a boolean which determine the change sign\r
+     */\r
+\r
+    protected void updateValue(int field, boolean plus) {\r
+       c.setTime(currd);\r
+       c.roll(field, plus);\r
+       currd = c.getTime();\r
+       // jdk 1.1 kludge\r
+       c.setTime(currd);\r
+       updateFields();\r
+    }\r
+\r
+    /**\r
+     * Tells if the edited value has changed\r
+     * @return true if the value changed.\r
+     */\r
+\r
+    public boolean hasChanged() {\r
+       return (!origd.equals(currd));\r
+    }\r
+\r
+    /**\r
+     * set the current value to be the original value, ie: changed\r
+     * must return <strong>false</strong> after a reset.\r
+     */\r
+\r
+    public void clearChanged() {\r
+       origd = currd;\r
+    }\r
+\r
+    /**\r
+     * reset the changes (if any)\r
+     */\r
+\r
+    public void resetChanges() {\r
+       currd = origd;\r
+       c.setTime(currd);\r
+       updateFields();\r
+    }\r
+\r
+    /**\r
+     * Get the current value of the edited value\r
+     * @return an object or <strong>null</strong> if the object was not\r
+     * initialized\r
+     */\r
+\r
+    public Object getValue() {\r
+       return new Long(currd.getTime());\r
+    }\r
+\r
+    /**\r
+     * Add a Listener to this editor.\r
+     * @param el a listener\r
+     */\r
+\r
+    public void setValue(Object o) {\r
+       if(o instanceof Date) {\r
+           currd = (Date)o;\r
+           c.setTime(currd);\r
+           updateFields();\r
+       }\r
+    }\r
+\r
+    /**\r
+     * get the Component created by the editor.\r
+     * @return a Component\r
+     */\r
+\r
+    public Component getComponent() {\r
+       return widget;\r
+    }\r
+\r
+    private JButton getUpButton(DateActionListener dae) {\r
+       JButton up = new JButton(Icons.arrowUpIcon);\r
+       up.addActionListener(dae);\r
+       up.setActionCommand("+");\r
+       up.setMargin(Utilities.insets0);\r
+       return up;\r
+    }\r
+\r
+    private JButton getDownButton(DateActionListener dae) {\r
+       JButton down = new JButton(Icons.arrowDownIcon);\r
+       down.addActionListener(dae);\r
+       down.setActionCommand("-");\r
+       down.setMargin(Utilities.insets0);\r
+       return down;\r
+    }\r
+\r
+    private JLabel getDateLabel() {\r
+       JLabel label = new JLabel(".");\r
+       label.setHorizontalAlignment(JLabel.CENTER);\r
+       return label;\r
+    }\r
+\r
+    public DateAttributeEditor() {\r
+       JButton pl, mi;\r
+       JPanel p, arrows;\r
+       DateActionListener dae;\r
+\r
+       widget = new JPanel(new GridLayout(2,1,1,1));\r
+\r
+       JPanel time = new JPanel(new GridLayout(1,3));\r
+       time.setBorder(BorderFactory.createEtchedBorder());\r
+\r
+       JPanel date = new JPanel(new GridLayout(1,3));\r
+       date.setBorder(BorderFactory.createEtchedBorder());\r
+\r
+       //TIME\r
+       // add the "hour" panel\r
+       h = getDateLabel();\r
+\r
+       dae = new DateActionListener(Calendar.HOUR_OF_DAY);\r
+       pl = getUpButton(dae);\r
+       mi = getDownButton(dae);\r
+\r
+       arrows = new JPanel(new GridLayout(2,1));\r
+       arrows.add(pl);\r
+       arrows.add(mi);\r
+       p = new JPanel(new BorderLayout());\r
+       p.add(h, "Center");\r
+       p.add(arrows, "East");\r
+       time.add(p);\r
+\r
+       // add the "min" panel\r
+       min = getDateLabel();\r
+\r
+       dae = new DateActionListener(Calendar.MINUTE);\r
+       pl = getUpButton(dae);\r
+       mi = getDownButton(dae);\r
+\r
+       arrows = new JPanel(new GridLayout(2,1));\r
+       arrows.add(pl);\r
+       arrows.add(mi);\r
+       p = new JPanel(new BorderLayout());\r
+       p.add(min, "Center");\r
+       p.add(arrows, "East");\r
+       time.add(p);\r
+\r
+       s = getDateLabel();\r
+\r
+       dae = new DateActionListener(Calendar.SECOND);\r
+       pl = getUpButton(dae);\r
+       mi = getDownButton(dae);\r
+\r
+       arrows = new JPanel(new GridLayout(2,1));\r
+       arrows.add(pl);\r
+       arrows.add(mi);\r
+       p = new JPanel(new BorderLayout());\r
+       p.add(s, "Center");\r
+       p.add(arrows, "East");\r
+       time.add(p);\r
+\r
+       //DATE\r
+       // add the "day" panel\r
+       d = getDateLabel();\r
+\r
+       dae = new DateActionListener(Calendar.DAY_OF_MONTH);\r
+       pl = getUpButton(dae);\r
+       mi = getDownButton(dae);\r
+\r
+       arrows = new JPanel(new GridLayout(2,1));\r
+       arrows.add(pl);\r
+       arrows.add(mi);\r
+       p = new JPanel(new BorderLayout());\r
+       p.add(d, "Center");\r
+       p.add(arrows, "East");\r
+       date.add(p);\r
+\r
+       // then the "Month" panel\r
+       m = getDateLabel();\r
+\r
+       dae = new DateActionListener(Calendar.MONTH);\r
+       pl = getUpButton(dae);\r
+       mi = getDownButton(dae);\r
+\r
+       arrows = new JPanel(new GridLayout(2,1));\r
+       arrows.add(pl);\r
+       arrows.add(mi);\r
+       p = new JPanel(new BorderLayout());\r
+       p.add(m, "Center");\r
+       p.add(arrows, "East");\r
+       date.add(p);\r
+\r
+       // then the "Year" panel\r
+       y = getDateLabel();\r
+\r
+       dae = new DateActionListener(Calendar.YEAR);\r
+       pl = getUpButton(dae);\r
+       mi = getDownButton(dae);\r
+\r
+       arrows = new JPanel(new GridLayout(2,1));\r
+       arrows.add(pl);\r
+       arrows.add(mi);\r
+       p = new JPanel(new BorderLayout());\r
+       p.add(y, "Center");\r
+       p.add(arrows, "East");\r
+       date.add(p);\r
+\r
+       widget.add(time);\r
+       widget.add(date);\r
+\r
+       c = Calendar.getInstance();\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
+\r
+   public void initialize(RemoteResourceWrapper w, Attribute a,  Object o,\r
+                         Properties p)\r
+       throws RemoteAccessException\r
+    {\r
+       RemoteResource r = w.getResource();\r
+       if(o == null) {\r
+           Date d = null;\r
+           try {\r
+               d = new Date(((Long)r.getValue(a.getName())).longValue());\r
+               \r
+               if(d == null)\r
+                   if(a.getDefault() != null)\r
+                       d = new Date(((Long) a.getDefault()).longValue());\r
+           } catch (Exception ex) {\r
+               // a fancy error?\r
+           }\r
+           if ( d != null ) {\r
+               origd = d;\r
+               c.setTime(d);\r
+               updateFields();\r
+           } else {\r
+               origd = new Date();\r
+           }\r
+           currd = origd;\r
+       } else {\r
+           if(o instanceof Long) {\r
+               origd = new Date(((Long)o).longValue());\r
+               c.setTime(origd);\r
+           }\r
+       }\r
+       updateFields();\r
+       currd = origd;\r
+    }\r
+}\r
+\r
+\r