--- /dev/null
+// ServerBrowser.java\r
+// $Id: ServerBrowser.java,v 1.1 2010/06/15 12:21:49 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.gui; \r
+\r
+import javax.swing.ImageIcon;\r
+import javax.swing.JFrame;\r
+import javax.swing.JLabel;\r
+import javax.swing.JMenuBar;\r
+import javax.swing.JMenuItem;\r
+import javax.swing.JMenu;\r
+import javax.swing.JPanel;\r
+import javax.swing.JDialog;\r
+import javax.swing.BorderFactory;\r
+import javax.swing.JOptionPane;\r
+import javax.swing.UIManager;\r
+\r
+import java.awt.BorderLayout;\r
+import java.awt.Font;\r
+import java.awt.Cursor;\r
+import java.awt.Dimension;\r
+import java.awt.Container;\r
+import java.awt.GridLayout;\r
+import java.awt.CardLayout;\r
+import java.awt.event.WindowAdapter;\r
+import java.awt.event.WindowEvent;\r
+import java.awt.event.ActionListener;\r
+import java.awt.event.ActionEvent;\r
+\r
+import java.util.Properties;\r
+\r
+import java.net.URL;\r
+import java.net.MalformedURLException;\r
+\r
+import org.w3c.jigsaw.admin.AdminContext;\r
+import org.w3c.jigsaw.admin.RemoteAccessException;\r
+import org.w3c.jigsaw.admin.RemoteResource;\r
+\r
+import org.w3c.jigadmin.PropertyManager;\r
+import org.w3c.jigadmin.RemoteResourceWrapper;\r
+import org.w3c.jigadmin.gui.Message;\r
+import org.w3c.jigadmin.gui.slist.ServerList;\r
+import org.w3c.jigadmin.gui.slist.ServerListModel;\r
+import org.w3c.jigadmin.gui.slist.ServerListListener;\r
+import org.w3c.jigadmin.editors.ServerEditorInterface;\r
+import org.w3c.jigadmin.editors.ServerEditorFactory;\r
+import org.w3c.jigadmin.editors.FramedResourceHelper;\r
+import org.w3c.jigadmin.widgets.DnDJPanel;\r
+import org.w3c.jigadmin.widgets.Icons;\r
+\r
+class WindowCloser extends WindowAdapter {\r
+\r
+ protected static int windows = 0;\r
+\r
+ public synchronized static void close(JFrame frame) {\r
+ windows--;\r
+ frame.setVisible(false);\r
+ frame.dispose();\r
+ if (windows < 0)\r
+ System.exit(0);\r
+ }\r
+\r
+ public void windowClosing(WindowEvent e) {\r
+ close((JFrame)e.getWindow());\r
+ }\r
+\r
+}\r
+\r
+class ServerMenu extends JMenuBar implements ActionListener {\r
+\r
+ ServerBrowser browser = null;\r
+\r
+ protected String getAdminURL() {\r
+ String url = JOptionPane.showInputDialog(this, \r
+ "Admin server URL",\r
+ "Open",\r
+ JOptionPane.PLAIN_MESSAGE);\r
+ if ((url != null) && (! url.startsWith("http://")))\r
+ url = "http://"+url;\r
+ return url;\r
+ }\r
+\r
+ public void actionPerformed(ActionEvent evt) {\r
+ String command = evt.getActionCommand();\r
+ if (command.equals("open")) {\r
+ String adminurl = getAdminURL();\r
+ if (adminurl == null)\r
+ return;\r
+ try {\r
+ URL url = new URL(adminurl);\r
+ browser.open(url);\r
+ } catch (MalformedURLException ex) {\r
+ JOptionPane.showMessageDialog(this, \r
+ adminurl+" is not a valid URL",\r
+ "Invalid URL",\r
+ JOptionPane.ERROR_MESSAGE);\r
+ }\r
+ } else if(command.equals("new")) {\r
+ String adminurl = getAdminURL();\r
+ if (adminurl == null)\r
+ return;\r
+ URL url = null;\r
+ try {\r
+ url = new URL(adminurl);\r
+ } catch (MalformedURLException ex) {\r
+ JOptionPane.showMessageDialog(this, \r
+ adminurl+" is not a valid URL",\r
+ "Invalid URL",\r
+ JOptionPane.ERROR_MESSAGE);\r
+ }\r
+ JFrame frame = new JFrame("Server Browser: " + adminurl);\r
+ ServerBrowser sb = new ServerBrowser(frame);\r
+ frame.getContentPane().add(sb, BorderLayout.CENTER);\r
+ frame.setVisible(true);\r
+ sb.open(url);\r
+ WindowCloser.windows++;\r
+ } else if(command.equals("close")) {\r
+ JFrame cont = browser.frame;\r
+ cont.setVisible(false);\r
+ cont.dispose();\r
+ WindowCloser.windows--;\r
+ if (WindowCloser.windows < 0)\r
+ System.exit(0);\r
+ } else if(command.equals("quit")) {\r
+ JFrame cont = browser.frame;\r
+ cont.setVisible(false);\r
+ cont.dispose();\r
+ WindowCloser.windows = 0;\r
+ System.exit(0);\r
+ } else if (command.equals("about")) {\r
+ AboutJigAdmin.show(this);\r
+ }\r
+ }\r
+\r
+ ServerMenu(ServerBrowser browser) {\r
+ super();\r
+ this.browser = browser;\r
+ JMenu server = new JMenu("JigAdmin");\r
+ add(server);\r
+ JMenuItem about = new JMenuItem("About JigAdmin");\r
+ about.setActionCommand("about");\r
+ about.addActionListener(this);\r
+ server.add( about );\r
+ server.addSeparator();\r
+ JMenuItem open = new JMenuItem("Open");\r
+ open.setActionCommand("open");\r
+ open.addActionListener(this);\r
+ server.add( open );\r
+ JMenuItem newOpen = new JMenuItem("Open in new window");\r
+ newOpen.setActionCommand("new");\r
+ newOpen.addActionListener(this);\r
+ server.add( newOpen );\r
+ server.addSeparator();\r
+ JMenuItem close = new JMenuItem("Close window");\r
+ close.setActionCommand("close");\r
+ close.addActionListener(this);\r
+ server.add( close );\r
+ JMenuItem quit = new JMenuItem("Exit");\r
+ quit.setActionCommand("quit");\r
+ quit.addActionListener(this);\r
+ server.add( quit );\r
+ }\r
+}\r
+\r
+/**\r
+ * The ServerBrowser.\r
+ * @version $Revision: 1.1 $\r
+ * @author Benoît Mahé (bmahe@w3.org)\r
+ */\r
+\r
+public class ServerBrowser extends JPanel implements ServerListListener {\r
+\r
+ private class Openner extends Thread {\r
+\r
+ URL adminURL = null;\r
+\r
+ public void run() {\r
+ setAdminURL(adminURL);\r
+ }\r
+\r
+ private Openner(URL adminURL) {\r
+ this.adminURL = adminURL;\r
+ }\r
+ }\r
+\r
+ protected JFrame frame = null;\r
+ protected JDialog popup = null;\r
+ protected JPanel serverPanel = null;\r
+ protected ServerList serverList = null;\r
+\r
+ private AdminContext admin = null;\r
+\r
+ RemoteResourceWrapper rootResource = null;\r
+\r
+ /**\r
+ * Constructor.\r
+ * @param frame the JFrame containing ServerBrowser\r
+ * @param ac The Admin context\r
+ * @see org.w3c.jigsaw.admin.AdminContext\r
+ */\r
+ public ServerBrowser(JFrame frame, AdminContext ac) {\r
+ //set our variables\r
+ this.frame = frame;\r
+ this.admin = ac;\r
+ //update our frame.\r
+ frame.addWindowListener(new WindowCloser());\r
+ frame.setJMenuBar(this.getMenuBar());\r
+ frame.setSize(800, 600);\r
+ //initialization\r
+ initialize();\r
+ //build the interface\r
+ build();\r
+ }\r
+\r
+ /**\r
+ * Constructor.\r
+ * @param frame the JFrame containing ServerBrowser\r
+ */\r
+ protected ServerBrowser(JFrame frame) {\r
+ this.frame = frame;\r
+ frame.addWindowListener(new WindowCloser());\r
+ frame.setJMenuBar(this.getMenuBar());\r
+ frame.setSize(800, 600);\r
+ }\r
+\r
+ /**\r
+ * Get the frame of ServerBrowser.\r
+ * @return a JFrame instance\r
+ */\r
+ public JFrame getFrame() {\r
+ return frame;\r
+ }\r
+\r
+ /**\r
+ * Open the given URL (in another thread)\r
+ * @param admin the URL of the admin server.\r
+ */\r
+ protected void open(URL admin) {\r
+ (new Openner(admin)).start();\r
+ }\r
+\r
+ /**\r
+ * Set the admin server URL.\r
+ * @param adminurl The admin server URL.\r
+ */\r
+ protected void setAdminURL(URL adminurl) {\r
+ try {\r
+ setCursor(Cursor.WAIT_CURSOR);\r
+ frame.invalidate();\r
+ this.removeAll();\r
+ this.admin = new AdminContext(adminurl);\r
+ initialize(); \r
+ build();\r
+ RemoteResourceWrapper rrw = serverList.getModel().\r
+ getServer(ServerListModel.ADMIN_SERVER_NAME);\r
+ serverSelected(ServerListModel.ADMIN_SERVER_NAME, rrw);\r
+ frame.validate();\r
+ setCursor(Cursor.DEFAULT_CURSOR);\r
+ frame.setTitle("Server Browser : "+adminurl);\r
+ } catch (RemoteAccessException ex) {\r
+ JOptionPane.showMessageDialog(this, \r
+ ex.getMessage(),\r
+ "RemoteAccessException",\r
+ JOptionPane.ERROR_MESSAGE);\r
+ }\r
+ }\r
+\r
+ /**\r
+ * Initialize SErverBrowser.\r
+ */\r
+ protected void initialize() {\r
+ //check authorization\r
+ boolean authorized = false;\r
+ RemoteResource rr = null;\r
+ while (!authorized) {\r
+ try {\r
+ authorized = true;\r
+ admin.initialize();\r
+ } catch (RemoteAccessException ex) {\r
+ if(ex.getMessage().equals("Unauthorized")) {\r
+ authorized = false;\r
+ } else {\r
+ Message.showErrorMessage(this, ex);\r
+ }\r
+ } finally {\r
+ if(!authorized) {\r
+ popupPasswdDialog("admin");\r
+ }\r
+ }\r
+ }\r
+ try {\r
+ rr = admin.getAdminResource();\r
+ } catch (RemoteAccessException ex) { \r
+ // Unable to connect for whatever reason... exit!\r
+ ex.printStackTrace();\r
+ System.exit(0);\r
+ }\r
+ rootResource = new RemoteResourceWrapper(rr, this);\r
+ //build interface\r
+ }\r
+\r
+ /**\r
+ * Build the interface.\r
+ */\r
+ protected void build() {\r
+ serverPanel = new DnDJPanel(new CardLayout());\r
+ serverPanel.setBorder(BorderFactory.createEmptyBorder(0,2,0,0));\r
+ serverList = getServerList();\r
+\r
+ if (serverList == null)\r
+ return;\r
+ serverList.addServerListListener(this);\r
+\r
+ String servers[] = serverList.getModel().getServers();\r
+ for (int i = 0 ; i < servers.length ; i++) {\r
+ String server = servers[i];\r
+ RemoteResourceWrapper rrw = \r
+ serverList.getModel().getServer(server);\r
+ //no way to get the component from the cardLayout :(\r
+ ServerEditorInterface editor = \r
+ ServerEditorFactory.getServerEditor(server, this, rrw);\r
+ serverPanel.add(server, editor.getComponent());\r
+ }\r
+\r
+ JPanel serverListPanel = new JPanel( new BorderLayout() );\r
+ serverListPanel.add(serverList, BorderLayout.NORTH);\r
+ serverListPanel.setBorder(BorderFactory.createEmptyBorder(1,0,0,0));\r
+\r
+ setLayout(new BorderLayout());\r
+ add(serverListPanel, BorderLayout.WEST);\r
+ add(serverPanel, BorderLayout.CENTER);\r
+ setBorder(BorderFactory.createLoweredBevelBorder());\r
+ }\r
+\r
+ /**\r
+ * Show the configuration of the given server\r
+ * @param name The server name\r
+ * @param rrw The server RemoteResourceWrapper\r
+ */\r
+ public void serverSelected(String name, RemoteResourceWrapper rrw) {\r
+ setCursor(Cursor.WAIT_CURSOR);\r
+ ((CardLayout)serverPanel.getLayout()).show(serverPanel, name);\r
+ ServerEditorFactory.updateServerEditor(name, this, rrw);\r
+ setCursor(Cursor.DEFAULT_CURSOR);\r
+ }\r
+\r
+ /**\r
+ * Get the ServerList.\r
+ * @return a ServerList instance\r
+ * @see org.w3c.jigadmin.gui.slist.ServerList\r
+ */\r
+ protected ServerList getServerList() {\r
+ boolean authorized = false;\r
+ ServerList list = null;\r
+\r
+ while (! authorized) {\r
+ try {\r
+ authorized = true;\r
+ list = new ServerList(getRootWrapper(), Icons.serverIcon);\r
+ } catch (RemoteAccessException ex) {\r
+ if(ex.getMessage().equals("Unauthorized")) {\r
+ authorized = false;\r
+ } else {\r
+ Message.showErrorMessage(this, ex);\r
+ }\r
+ } finally {\r
+ if(!authorized) {\r
+ popupPasswdDialog("admin");\r
+ }\r
+ }\r
+ }\r
+ return list;\r
+ }\r
+\r
+ /**\r
+ * give the Root Resource of the browser\r
+ */\r
+ public RemoteResourceWrapper getRootWrapper() {\r
+ return rootResource;\r
+ }\r
+\r
+ /**\r
+ * Should I retry?\r
+ * @param ex the RemoteAccessException that occured\r
+ * @return a boolean\r
+ */\r
+ public boolean shouldRetry(RemoteAccessException ex) {\r
+ if(ex.getMessage().equals("Unauthorized")) {\r
+ popupPasswdDialog("admin");\r
+ return true;\r
+ } else {\r
+ Message.showErrorMessage(this, ex);\r
+ return false;\r
+ }\r
+ }\r
+\r
+ /**\r
+ * Set The Cursor.\r
+ * @param cursor The Cursor type\r
+ */\r
+ public void setCursor(int cursor) {\r
+ frame.setCursor(Cursor.getPredefinedCursor(cursor));\r
+ }\r
+\r
+ /**\r
+ * Popup a dialog that allows the user to enter his username/password.\r
+ * @param name the realm name\r
+ */\r
+ public void popupPasswdDialog(String name) {\r
+ if (popup == null) {\r
+ AuthPanel ap = new AuthPanel(this, name);\r
+ popup = new JDialog(frame, "Authorization for JigAdmin", false);\r
+ Container cont = popup.getContentPane();\r
+ cont.setLayout(new GridLayout(1,1));\r
+ cont.add(ap);\r
+ popup.setSize(new Dimension(300, 220));\r
+ popup.show();\r
+ ap.getFocus();\r
+ while(! ap.waitForCompletion());\r
+ }\r
+ }\r
+\r
+ /**\r
+ * Popup a dialog that allows the user to edit some resource properties.\r
+ * @param rrw the RemoteResourceWrapper of the resource to edit.\r
+ */\r
+ public void popupResource(RemoteResourceWrapper rrw) {\r
+ try {\r
+ setCursor(Cursor.WAIT_CURSOR);\r
+ JDialog popres = new JDialog(frame, "Edit Resource", false);\r
+ FramedResourceHelper helper = new FramedResourceHelper();\r
+ popres.setJMenuBar(helper.getMenuBar(popres));\r
+ PropertyManager pm = PropertyManager.getPropertyManager();\r
+ Properties props = pm.getEditorProperties(rrw);\r
+ helper.initialize(rrw, props);\r
+ Container cont = popres.getContentPane();\r
+\r
+ cont.setLayout(new GridLayout(1,1));\r
+ cont.add(helper.getComponent());\r
+ popres.setSize(new Dimension(500, 550));\r
+ popres.setLocationRelativeTo(this);\r
+ popres.show();\r
+ setCursor(Cursor.DEFAULT_CURSOR);\r
+ } catch (RemoteAccessException ex) {\r
+ Message.showErrorMessage(this, ex);\r
+ }\r
+ }\r
+\r
+ /**\r
+ * Dispose the popupPasswdDialog and close the frame.\r
+ * @param oK if true dispose omly the dialog.\r
+ */\r
+ protected void dispose(boolean Ok) {\r
+ if (! Ok) {\r
+ WindowCloser.close(frame);\r
+ popup.dispose();\r
+ popup = null;\r
+ } else if (popup != null) {\r
+ popup.dispose();\r
+ popup = null;\r
+ }\r
+ }\r
+\r
+ /**\r
+ * Returns the AdminContext.\r
+ * @return an AdminContext instance\r
+ * @see org.w3c.jigsaw.admin.AdminContext\r
+ */\r
+ protected AdminContext getAdminContext() {\r
+ return admin;\r
+ }\r
+\r
+ /**\r
+ * Get the ServerBrowser MenuBar.\r
+ * @return a JMenuBar instance.\r
+ */\r
+ public JMenuBar getMenuBar() {\r
+ return new ServerMenu(this);\r
+ }\r
+\r
+ public static void unBoldSpecificFonts() {\r
+ Font f = new Font("Dialog",Font.PLAIN,12); \r
+\r
+ UIManager.put("Button.font",f); \r
+ UIManager.put("CheckBox.font",f); \r
+ UIManager.put("CheckBoxMenuItem.font",f); \r
+ UIManager.put("ComboBox.font",f); \r
+ UIManager.put("DesktopIcon.font",f); \r
+\r
+ UIManager.put("InternalFrame.font",f); \r
+ UIManager.put("InternalFrame.titleFont",f); \r
+ UIManager.put("Label.font",f); \r
+ UIManager.put("Menu.font",f); \r
+ UIManager.put("MenuBar.font",f); \r
+\r
+ UIManager.put("MenuItem.font",f); \r
+ UIManager.put("ProgressBar.font",f); \r
+ UIManager.put("RadioButton.font",f); \r
+ UIManager.put("RadioButtonMenuItem.font",f); \r
+ UIManager.put("TabbedPane.font",f); \r
+\r
+ UIManager.put("TitledBorder.font",f); \r
+ UIManager.put("ToggleButton.font",f); \r
+ UIManager.put("ToolBar.font",f); \r
+ } \r
+\r
+ /**\r
+ * Run ServerBrowser.\r
+ */\r
+ public static void main(String args[]) {\r
+ String baseURL=null;\r
+ String jigadmRoot = null;\r
+ \r
+ for (int i = 0 ; i < args.length ; i++) {\r
+ if (args[i].equals("-root")) {\r
+ Properties p = System.getProperties();\r
+ jigadmRoot = args[++i];\r
+ p.put(PropertyManager.ROOT_P, jigadmRoot);\r
+ System.setProperties(p);\r
+ } else {\r
+ baseURL = args[i];\r
+ }\r
+ }\r
+ if (baseURL == null)\r
+ baseURL = "http://localhost:8009/";\r
+ URL bu = null; \r
+ try {\r
+ bu = new URL (baseURL);\r
+ if (bu.getFile().length() == 0) {\r
+ bu = new URL(bu, "/");\r
+ }\r
+ } catch (MalformedURLException ex) {\r
+ System.err.println("Invalid URL : "+baseURL);\r
+ }\r
+ if (bu != null) {\r
+ try {\r
+ unBoldSpecificFonts();\r
+ AdminContext ac = new AdminContext(bu);\r
+ JFrame frame = new JFrame("Server Browser: " + baseURL);\r
+ ServerBrowser sb = new ServerBrowser(frame, ac);\r
+ frame.getContentPane().add(sb, BorderLayout.CENTER);\r
+ frame.setVisible(true);\r
+ } catch (RemoteAccessException raex) {\r
+ raex.printStackTrace();\r
+ }\r
+ }\r
+ }\r
+}\r