Adding JMCR-Stable version
[Benchmarks_CSolver.git] / JMCR-Stable / real-world application / jigsaw / src / org / w3c / jigadmin / editors / ServerEditor.java
diff --git a/JMCR-Stable/real-world application/jigsaw/src/org/w3c/jigadmin/editors/ServerEditor.java b/JMCR-Stable/real-world application/jigsaw/src/org/w3c/jigadmin/editors/ServerEditor.java
new file mode 100644 (file)
index 0000000..8c5d564
--- /dev/null
@@ -0,0 +1,179 @@
+// ServerEditor.java\r
+// $Id: ServerEditor.java,v 1.1 2010/06/15 12:25:54 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 java.awt.GridLayout;\r
+import java.awt.Font;\r
+import java.awt.Component;\r
+import java.awt.BorderLayout;\r
+\r
+import javax.swing.JPanel;\r
+import javax.swing.JLabel;\r
+import javax.swing.BorderFactory;\r
+import javax.swing.JTabbedPane;\r
+import javax.swing.border.TitledBorder;\r
+import javax.swing.event.ChangeListener;\r
+\r
+import java.util.Properties;\r
+\r
+import org.w3c.jigadmin.RemoteResourceWrapper;\r
+import org.w3c.jigadmin.widgets.DnDTabbedPane;\r
+import org.w3c.jigadmin.events.ResourceActionSource;\r
+import org.w3c.jigadmin.events.ResourceActionListener;\r
+\r
+import org.w3c.jigsaw.admin.RemoteResource;\r
+import org.w3c.jigsaw.admin.RemoteAccessException;\r
+\r
+import org.w3c.tools.widgets.Utilities;\r
+import org.w3c.tools.sorter.Sorter;\r
+\r
+/**\r
+ * The server editor.\r
+ * @version $Revision: 1.1 $\r
+ * @author  Benoît Mahé (bmahe@w3.org)\r
+ */\r
+public class ServerEditor extends JPanel implements ServerEditorInterface {\r
+\r
+    private class Retryer extends Thread {\r
+       \r
+       RemoteAccessException ex = null;\r
+\r
+       public void run() {\r
+           while (server.getServerBrowser().shouldRetry(ex)) {\r
+               try {\r
+                   initializeServerHelpers();\r
+                   break;\r
+               } catch (RemoteAccessException ex2) {\r
+                   ex = ex2;\r
+               }\r
+           }\r
+           build();\r
+       }\r
+\r
+       private Retryer(RemoteAccessException ex) {\r
+           this.ex = ex;\r
+       }\r
+    }\r
+\r
+    protected RemoteResourceWrapper server     = null;\r
+    protected String                name       = null;\r
+    protected ServerHelperInterface shelpers[] = null;\r
+\r
+    /**\r
+     * Reload the server configuration.\r
+     * @param server the new server wrapper\r
+     */\r
+    public void setServer(RemoteResourceWrapper server) {\r
+       this.server = server;\r
+       try {\r
+           initializeServerHelpers();\r
+           build();\r
+       } catch (RemoteAccessException ex) {\r
+           (new Retryer(ex)).start();\r
+       }\r
+    }\r
+\r
+    /**\r
+     * Get the component.\r
+     * @return a Component\r
+     */\r
+    public Component getComponent() {\r
+       return this;\r
+    }\r
+\r
+    /**\r
+     * initialize the server helpers.\r
+     * @exception RemoteAccessException if a remote error occurs\r
+     */\r
+    protected void initializeServerHelpers()\r
+       throws RemoteAccessException\r
+    {\r
+       shelpers = null;\r
+       //use ServerHelperFactory....\r
+       RemoteResource rr = server.getResource();\r
+       String names[] = rr.enumerateResourceIdentifiers();\r
+       Sorter.sortStringArray(names, true);\r
+       shelpers = new ServerHelperInterface[names.length];\r
+       for (int i = 0 ; i < names.length ; i++) {\r
+           RemoteResourceWrapper rrw = server.getChildResource(names[i]);\r
+           shelpers[i] = ServerHelperFactory.getServerHelper(names[i], rrw);\r
+       }\r
+\r
+       for (int i = 0 ; i < shelpers.length ; i++) {\r
+           if (shelpers[i] instanceof ResourceActionSource) {\r
+               ResourceActionSource source = \r
+                   (ResourceActionSource)shelpers[i]; \r
+               for (int j = 0 ; j < shelpers.length ; j++) {\r
+                   if (j != i) {\r
+                       if (shelpers[j] instanceof ResourceActionListener) {\r
+                           ResourceActionListener listener =\r
+                               (ResourceActionListener) shelpers[j];\r
+                           source.addResourceActionListener(listener);\r
+                       }\r
+                   }\r
+               }\r
+           }\r
+       }\r
+    }\r
+\r
+    /**\r
+     * Build the interface.\r
+     */\r
+    protected void build() {\r
+       removeAll();\r
+       invalidate();\r
+       setLayout(new BorderLayout());\r
+       TitledBorder border = BorderFactory.createTitledBorder(name);\r
+       border.setTitleFont(Utilities.reallyBigBoldFont);\r
+       setBorder(border);\r
+       //tabbedpane\r
+       JTabbedPane tabbedPane = new DnDTabbedPane();\r
+       if (shelpers == null)\r
+           return;\r
+\r
+       for (int i = 0 ; i < shelpers.length ; i++) {\r
+           if (shelpers[i] instanceof ChangeListener)\r
+               tabbedPane.addChangeListener((ChangeListener)shelpers[i]);\r
+\r
+           if (shelpers[i] instanceof ControlServerHelper) {\r
+               //add it on top of the panel\r
+               add(shelpers[i].getComponent(), BorderLayout.NORTH);\r
+           } else {\r
+               String name = shelpers[i].getName().replace('_',' ');\r
+               char chars[] = name.toCharArray();\r
+               chars[0] = Character.toUpperCase(chars[0]);\r
+               String helperName = new String(chars);\r
+               tabbedPane.addTab(helperName, null, \r
+                                 shelpers[i].getComponent(),\r
+                                 shelpers[i].getToolTip());\r
+           }\r
+       }\r
+       tabbedPane.setSelectedIndex(0);\r
+       add(tabbedPane, BorderLayout.CENTER);\r
+       validate();\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.server = rrw;\r
+       this.name   = name;\r
+    }\r
+\r
+    public ServerEditor() {\r
+       //for new Isntance\r
+    }\r
+\r
+   \r
+    \r
+}\r