Adding JMCR-Stable version
[Benchmarks_CSolver.git] / JMCR-Stable / real-world application / jigsaw / src / org / w3c / jigadm / editors / ResourceEditor.java
diff --git a/JMCR-Stable/real-world application/jigsaw/src/org/w3c/jigadm/editors/ResourceEditor.java b/JMCR-Stable/real-world application/jigsaw/src/org/w3c/jigadm/editors/ResourceEditor.java
new file mode 100644 (file)
index 0000000..4bfb3f3
--- /dev/null
@@ -0,0 +1,429 @@
+// ResourceEditor.java\r
+// $Id: ResourceEditor.java,v 1.1 2010/06/15 12:22:47 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.Canvas;\r
+import java.awt.Color;\r
+import java.awt.Component;\r
+import java.awt.Container;\r
+import java.awt.Dimension;\r
+import java.awt.Graphics;\r
+import java.awt.GridLayout;\r
+import java.awt.Image;\r
+import java.awt.Label;\r
+import java.awt.Panel;\r
+import java.awt.Toolkit;\r
+\r
+import java.awt.event.ActionEvent;\r
+import java.awt.event.ActionListener;\r
+import java.awt.event.ComponentEvent;\r
+import java.awt.event.MouseAdapter;\r
+import java.awt.event.MouseEvent;\r
+\r
+import java.util.EventObject;\r
+import java.util.Properties;\r
+import java.util.Vector;\r
+\r
+import org.w3c.jigadm.PropertyManager;\r
+import org.w3c.jigadm.RemoteResourceWrapper;\r
+\r
+import org.w3c.jigadm.events.ResourceChangeEvent;\r
+import org.w3c.jigadm.events.ResourceListener;\r
+\r
+import org.w3c.jigsaw.admin.RemoteAccessException;\r
+import org.w3c.jigsaw.admin.RemoteResource;\r
+\r
+import org.w3c.tools.widgets.BorderPanel;\r
+import org.w3c.tools.widgets.MessagePopup;\r
+\r
+public class ResourceEditor implements ResourceEditorInterface {\r
+\r
+    public static final String UNREMOVABLE_P = "unremovable";\r
+\r
+    class Alert extends Canvas {\r
+\r
+       Image alert = null;\r
+       int width  = 0;\r
+       int height = 0;\r
+\r
+       public Dimension getMinimumSize() {\r
+           return new Dimension(width+8, height+8);\r
+       }\r
+\r
+       public Dimension getPreferredSize() {\r
+           return new Dimension(width+8, height+8);\r
+       }\r
+\r
+       public boolean imageUpdate(Image img, int flaginfo,\r
+                                  int x, int y, int width, int height) {\r
+           initSize();\r
+           Container parent = getParent();\r
+           if(parent != null) \r
+               parent.doLayout();\r
+           return super.imageUpdate(img, flaginfo, x, y, width, height);\r
+       }\r
+\r
+       private void initSize() {\r
+           if (alert != null) {\r
+               width = alert.getWidth(this);\r
+               height = alert.getHeight(this);\r
+           }\r
+       }\r
+\r
+       public void paint(Graphics g) {\r
+           if (alert != null)\r
+               g.drawImage(alert, 0, 0, this);\r
+       }\r
+\r
+       Alert() {\r
+           PropertyManager pm = PropertyManager.getPropertyManager();\r
+           String alertPath = pm.getIconLocation("alert");\r
+           if (alertPath != null) {\r
+               alert = Toolkit.getDefaultToolkit().getImage(alertPath);\r
+               prepareImage(alert,this);\r
+           }\r
+           initSize();\r
+       }\r
+    }\r
+\r
+    class ButtonBarListener implements ActionListener {\r
+\r
+       class Switcher extends Thread {\r
+           Button b;\r
+\r
+            public void run() {\r
+               try {\r
+                   switchHelper(b);\r
+               } catch (RemoteAccessException ex) {\r
+                   // do fancy things FIXME\r
+               }\r
+           }\r
+           \r
+           Switcher(Button b) {\r
+               this.b = b;\r
+           }\r
+       }\r
+\r
+        class Deleter extends Thread {\r
+            public void run() {\r
+               delete();\r
+           }\r
+       }\r
+\r
+        class Reindexer extends Thread {\r
+            public void run() {\r
+               reindex();\r
+           }\r
+       }\r
+\r
+        public void actionPerformed(ActionEvent ae) {\r
+           if (ae.getActionCommand().equals(DELETE_L)) {\r
+               (new Deleter()).start();\r
+           } else if(ae.getActionCommand().equals(REINDEX_L)) {\r
+               setMessage("Reindexation started...");\r
+               (new Reindexer()).start();\r
+               setMessage("Reindexation done.");\r
+           } else {\r
+               (new Switcher((Button)ae.getSource())).start();\r
+           }\r
+       }\r
+    }\r
+\r
+    class MouseButtonListener extends MouseAdapter {\r
+\r
+       public void mouseEntered(MouseEvent e) {\r
+           Component comp = e.getComponent();\r
+           if (comp instanceof Button) {\r
+               String action = ((Button)comp).getActionCommand();\r
+               if (action.equals(DELETE_L)) {\r
+                   setMessage("Delete this resource!");\r
+               } else if (action.equals(REINDEX_L)) {\r
+                   setMessage("Reindex the children.");\r
+               }\r
+           }\r
+       }\r
+\r
+       public void mouseExited(MouseEvent e) {\r
+           setMessage("");\r
+       }\r
+    }\r
+\r
+    protected Vector rls = null;\r
+\r
+    Panel target = null;\r
+    Panel buttonp = null;\r
+    Panel editorp = null;\r
+    Button commitb = null;\r
+    Button resetb;\r
+    RemoteResource rr = null;\r
+    RemoteResourceWrapper rrw = null;\r
+    ResourceHelper rh[] = null;\r
+    protected Button b[] = null;\r
+    protected Label message = null;\r
+    Properties p;\r
+\r
+    protected static final String DELETE_L  = "Delete";\r
+    protected static final String REINDEX_L = "Reindex";\r
+\r
+    public void setMessage(String msg) {\r
+       message.setText(msg);\r
+    }\r
+\r
+    public void commitChanges()\r
+       throws RemoteAccessException\r
+    {\r
+       boolean authorized;\r
+       for(int i=0; i<rh.length; i++) {\r
+           if(rh[i].hasChanged()) {\r
+               authorized = false;\r
+               while (!authorized) {\r
+                   try {\r
+                       authorized = true;\r
+                       rh[i].commitChanges();\r
+                   } catch (RemoteAccessException ex) {\r
+                       if( ex.getMessage().equals("Unauthorized")) {\r
+                           authorized = false;\r
+                       } else {\r
+                           (new MessagePopup("Exception : ",\r
+                                             ex.getMessage())).show();\r
+                           throw ex; // another reason, throw it\r
+                       }\r
+                   } finally {\r
+                       if(!authorized) {\r
+                           rrw.getBrowser().popupDialog("admin");\r
+                       }\r
+                   }\r
+               }\r
+           }\r
+       }\r
+    }\r
+\r
+    public void resetChanges()\r
+       throws RemoteAccessException\r
+    {\r
+       for(int i=0; i<rh.length; i++) {\r
+           if(rh[i].hasChanged()) {\r
+               rh[i].resetChanges();\r
+           }\r
+       }\r
+    }\r
+\r
+    protected void switchHelper(Button toggled)\r
+       throws RemoteAccessException\r
+    {\r
+       for(int i=0; i< b.length; i++) {\r
+           boolean authorized;\r
+           if( toggled.equals(b[i])) {\r
+               PropertyManager pm = PropertyManager.getPropertyManager();\r
+               Properties props = \r
+                   pm.getHelperProperties(rrw, rh[i].getClass().getName());\r
+               authorized = false;\r
+               while (!authorized) {\r
+                   try {\r
+                       authorized = true;\r
+                       rh[i].initialize(rrw, props);\r
+                   } catch (RemoteAccessException ex) {\r
+                       if( ex.getMessage().equals("Unauthorized")) {\r
+                           authorized = false;\r
+                       } else {\r
+                           (new MessagePopup("Exception : ",\r
+                                             ex.getMessage())).show();\r
+                           throw ex; // another reason, throw it\r
+                       }\r
+                   } finally {\r
+                       if(!authorized) {\r
+                           rrw.getBrowser().popupDialog("admin");\r
+                       }\r
+                   }\r
+               }\r
+               editorp.setVisible(false);\r
+               editorp.removeAll();\r
+               editorp.add("Center", rh[i].getComponent());\r
+               editorp.validate();\r
+               editorp.setVisible(true);\r
+           }\r
+       }\r
+    }\r
+\r
+    protected void reindex() {\r
+       boolean authorized = false;\r
+       while (!authorized) {\r
+           try { \r
+               authorized = true;\r
+               rrw.getResource().reindex(true);\r
+           } catch (RemoteAccessException ex) {\r
+               if( ex.getMessage().equals("Unauthorized")) {\r
+                   authorized = false;\r
+               } else {\r
+                   (new MessagePopup("Exception : ",\r
+                                     ex.getMessage())).show();\r
+                   // must throw a fancy error\r
+               }\r
+           } finally {\r
+               if(!authorized) {\r
+                   rrw.getBrowser().popupDialog("admin");\r
+               }\r
+           }\r
+       }\r
+    }\r
+\r
+    protected void delete() {\r
+       boolean authorized = false;\r
+       while (!authorized) {\r
+           try { \r
+               authorized = true;\r
+               rrw.getResource().delete();\r
+           } catch (RemoteAccessException ex) {\r
+               if( ex.getMessage().equals("Unauthorized")) {\r
+                   authorized = false;\r
+               } else {\r
+                   (new MessagePopup("Exception : ",\r
+                                     ex.getMessage())).show();\r
+                   // must throw a fancy error\r
+               }\r
+           } finally {\r
+               if(!authorized) {\r
+                   rrw.getBrowser().popupDialog("admin");\r
+               }\r
+           }\r
+       }\r
+       processEvent(new ResourceChangeEvent(rrw.getFatherWrapper(),\r
+                                            "deleted", rrw, null));\r
+    }\r
+\r
+    public boolean hasChanged() {\r
+       for(int i=0; i<rh.length; i++) {\r
+           if(rh[i].hasChanged()) {\r
+               return true;\r
+           }\r
+       }\r
+       return false;\r
+    }\r
+\r
+    public void clearChanged() {\r
+       for(int i=0; i<rh.length; i++) {\r
+           if(rh[i].hasChanged()) {\r
+               rh[i].clearChanged();\r
+           }\r
+       }\r
+    }\r
+\r
+    public RemoteResource getValue() {\r
+       return rr;\r
+    }\r
+\r
+    public synchronized void addResourceListener(ResourceListener rl) {\r
+       if (rls == null)\r
+           rls = new Vector(2);\r
+       rls.addElement(rl);\r
+    }\r
+\r
+    public synchronized void removeResourceListener(ResourceListener rl) {\r
+       if ( rls != null ) \r
+           rls.removeElement(rl);\r
+    }\r
+\r
+    protected void processEvent(EventObject eo) {\r
+       Vector rls = null;\r
+       ResourceListener rl;\r
+       synchronized(this) {\r
+           if((this.rls != null) && (eo instanceof ResourceChangeEvent )) {\r
+               rls = (Vector) this.rls.clone();\r
+           } else {\r
+               return;\r
+           }\r
+       }\r
+       for(int i=0; i<rls.size(); i++) {\r
+           rl = (ResourceListener) rls.elementAt(i);\r
+           rl.resourceChanged((ResourceChangeEvent)eo);\r
+       }\r
+    }\r
+\r
+    /**\r
+     * initialize this editor\r
+     * @param rrw The RemoteResourceWrapper\r
+     * @param pr The properties\r
+     * @exception RemoteAccessException if a remote access error occurs.\r
+     */\r
+    public void initialize(RemoteResourceWrapper rrw, Properties p)\r
+       throws RemoteAccessException\r
+    {\r
+       int num = 0;\r
+       this.rrw = rrw;\r
+       rr = rrw.getResource();\r
+       ButtonBarListener bbl = new ButtonBarListener();\r
+       this.p = p;\r
+       target.removeAll();\r
+       BorderPanel ptarget = new BorderPanel(BorderPanel.OUT, 2);\r
+       ptarget.setLayout(new BorderLayout());\r
+       rh = ResourceHelperFactory.getHelpers(rrw);\r
+\r
+       PropertyManager pm = PropertyManager.getPropertyManager();\r
+       Properties props = pm.getEditorProperties(rrw);\r
+       if ((props == null) ||\r
+           (props.getProperty(UNREMOVABLE_P,\r
+                              "false").equalsIgnoreCase("false")))\r
+           {\r
+               MouseButtonListener mbl = new MouseButtonListener();\r
+               Panel pan = new Panel(new BorderLayout());\r
+               Button deleteb = new Button(DELETE_L);\r
+               deleteb.addActionListener(bbl);\r
+               deleteb.addMouseListener(mbl);\r
+               pan.add("West", deleteb);\r
+               \r
+               message = new Label("", Label.CENTER);\r
+               message.setBackground(Color.gray);\r
+               message.setForeground(Color.white);\r
+\r
+               BorderPanel pmsg = new BorderPanel(BorderPanel.IN, 2);\r
+               pmsg.setLayout(new BorderLayout());\r
+               pmsg.add(message);\r
+               pan.add("Center", pmsg);\r
+\r
+               if (rr.isDirectoryResource()) {\r
+                   Button reindexb = new Button(REINDEX_L);\r
+                   reindexb.addActionListener(bbl);\r
+                   reindexb.addMouseListener(mbl);\r
+                   pan.add("East", reindexb);\r
+               }\r
+               //target.setVisible(false);\r
+               ptarget.add("South", pan);\r
+           }\r
+       if(rh != null) {\r
+           //buttonp = new Panel(new GridLayout(1, rh.length+1));\r
+           buttonp = new BorderPanel(BorderPanel.OUT, 1);\r
+           buttonp.setLayout(new GridLayout(1, rh.length+1));\r
+           b = new Button[rh.length];\r
+           for(int i=0; i<rh.length; i++) {\r
+               b[i] = new Button(rh[i].getTitle());\r
+               buttonp.add(b[i]);\r
+               b[i].addActionListener(bbl);\r
+               if (rh[i].getTitle().equalsIgnoreCase("shortcut"))\r
+                   num = i;\r
+           }\r
+           editorp = new Panel(new BorderLayout());\r
+           //target.setVisible(false);\r
+           ptarget.add("North", buttonp);\r
+           ptarget.add("Center", editorp);\r
+           target.add("Center", ptarget);\r
+           target.validate();\r
+           target.setVisible(true);\r
+           switchHelper(b[num]);\r
+       } else {\r
+           target.add("Center", ptarget);\r
+           target.validate();\r
+           target.setVisible(true);\r
+       }\r
+    }\r
+\r
+    public ResourceEditor(Panel p) {\r
+       target = p;\r
+    }\r
+}\r
+\r
+\r