001: // ServerBrowser.java
002: // $Id: ServerBrowser.java,v 1.29 2000/08/16 21:37:31 ylafon Exp $
003: // (c) COPYRIGHT MIT and INRIA, 1998.
004: // Please first read the full copyright statement in file COPYRIGHT.html
005:
006: package org.w3c.jigadmin.gui;
007:
008: import javax.swing.ImageIcon;
009: import javax.swing.JFrame;
010: import javax.swing.JLabel;
011: import javax.swing.JMenuBar;
012: import javax.swing.JMenuItem;
013: import javax.swing.JMenu;
014: import javax.swing.JPanel;
015: import javax.swing.JDialog;
016: import javax.swing.BorderFactory;
017: import javax.swing.JOptionPane;
018: import javax.swing.UIManager;
019:
020: import java.awt.BorderLayout;
021: import java.awt.Font;
022: import java.awt.Cursor;
023: import java.awt.Dimension;
024: import java.awt.Container;
025: import java.awt.GridLayout;
026: import java.awt.CardLayout;
027: import java.awt.event.WindowAdapter;
028: import java.awt.event.WindowEvent;
029: import java.awt.event.ActionListener;
030: import java.awt.event.ActionEvent;
031:
032: import java.util.Properties;
033:
034: import java.net.URL;
035: import java.net.MalformedURLException;
036:
037: import org.w3c.jigsaw.admin.AdminContext;
038: import org.w3c.jigsaw.admin.RemoteAccessException;
039: import org.w3c.jigsaw.admin.RemoteResource;
040:
041: import org.w3c.jigadmin.PropertyManager;
042: import org.w3c.jigadmin.RemoteResourceWrapper;
043: import org.w3c.jigadmin.gui.Message;
044: import org.w3c.jigadmin.gui.slist.ServerList;
045: import org.w3c.jigadmin.gui.slist.ServerListModel;
046: import org.w3c.jigadmin.gui.slist.ServerListListener;
047: import org.w3c.jigadmin.editors.ServerEditorInterface;
048: import org.w3c.jigadmin.editors.ServerEditorFactory;
049: import org.w3c.jigadmin.editors.FramedResourceHelper;
050: import org.w3c.jigadmin.widgets.DnDJPanel;
051: import org.w3c.jigadmin.widgets.Icons;
052:
053: class WindowCloser extends WindowAdapter {
054:
055: protected static int windows = 0;
056:
057: public synchronized static void close(JFrame frame) {
058: windows--;
059: frame.setVisible(false);
060: frame.dispose();
061: if (windows < 0)
062: System.exit(0);
063: }
064:
065: public void windowClosing(WindowEvent e) {
066: close((JFrame) e.getWindow());
067: }
068:
069: }
070:
071: class ServerMenu extends JMenuBar implements ActionListener {
072:
073: ServerBrowser browser = null;
074:
075: protected String getAdminURL() {
076: String url = JOptionPane.showInputDialog(this ,
077: "Admin server URL", "Open", JOptionPane.PLAIN_MESSAGE);
078: if ((url != null) && (!url.startsWith("http://")))
079: url = "http://" + url;
080: return url;
081: }
082:
083: public void actionPerformed(ActionEvent evt) {
084: String command = evt.getActionCommand();
085: if (command.equals("open")) {
086: String adminurl = getAdminURL();
087: if (adminurl == null)
088: return;
089: try {
090: URL url = new URL(adminurl);
091: browser.open(url);
092: } catch (MalformedURLException ex) {
093: JOptionPane.showMessageDialog(this , adminurl
094: + " is not a valid URL", "Invalid URL",
095: JOptionPane.ERROR_MESSAGE);
096: }
097: } else if (command.equals("new")) {
098: String adminurl = getAdminURL();
099: if (adminurl == null)
100: return;
101: URL url = null;
102: try {
103: url = new URL(adminurl);
104: } catch (MalformedURLException ex) {
105: JOptionPane.showMessageDialog(this , adminurl
106: + " is not a valid URL", "Invalid URL",
107: JOptionPane.ERROR_MESSAGE);
108: }
109: JFrame frame = new JFrame("Server Browser: " + adminurl);
110: ServerBrowser sb = new ServerBrowser(frame);
111: frame.getContentPane().add(sb, BorderLayout.CENTER);
112: frame.setVisible(true);
113: sb.open(url);
114: WindowCloser.windows++;
115: } else if (command.equals("close")) {
116: JFrame cont = browser.frame;
117: cont.setVisible(false);
118: cont.dispose();
119: WindowCloser.windows--;
120: if (WindowCloser.windows < 0)
121: System.exit(0);
122: } else if (command.equals("quit")) {
123: JFrame cont = browser.frame;
124: cont.setVisible(false);
125: cont.dispose();
126: WindowCloser.windows = 0;
127: System.exit(0);
128: } else if (command.equals("about")) {
129: AboutJigAdmin.show(this );
130: }
131: }
132:
133: ServerMenu(ServerBrowser browser) {
134: super ();
135: this .browser = browser;
136: JMenu server = new JMenu("JigAdmin");
137: add(server);
138: JMenuItem about = new JMenuItem("About JigAdmin");
139: about.setActionCommand("about");
140: about.addActionListener(this );
141: server.add(about);
142: server.addSeparator();
143: JMenuItem open = new JMenuItem("Open");
144: open.setActionCommand("open");
145: open.addActionListener(this );
146: server.add(open);
147: JMenuItem newOpen = new JMenuItem("Open in new window");
148: newOpen.setActionCommand("new");
149: newOpen.addActionListener(this );
150: server.add(newOpen);
151: server.addSeparator();
152: JMenuItem close = new JMenuItem("Close window");
153: close.setActionCommand("close");
154: close.addActionListener(this );
155: server.add(close);
156: JMenuItem quit = new JMenuItem("Exit");
157: quit.setActionCommand("quit");
158: quit.addActionListener(this );
159: server.add(quit);
160: }
161: }
162:
163: /**
164: * The ServerBrowser.
165: * @version $Revision: 1.29 $
166: * @author Benoît Mahé (bmahe@w3.org)
167: */
168:
169: public class ServerBrowser extends JPanel implements ServerListListener {
170:
171: private class Openner extends Thread {
172:
173: URL adminURL = null;
174:
175: public void run() {
176: setAdminURL(adminURL);
177: }
178:
179: private Openner(URL adminURL) {
180: this .adminURL = adminURL;
181: }
182: }
183:
184: protected JFrame frame = null;
185: protected JDialog popup = null;
186: protected JPanel serverPanel = null;
187: protected ServerList serverList = null;
188:
189: private AdminContext admin = null;
190:
191: RemoteResourceWrapper rootResource = null;
192:
193: /**
194: * Constructor.
195: * @param frame the JFrame containing ServerBrowser
196: * @param ac The Admin context
197: * @see org.w3c.jigsaw.admin.AdminContext
198: */
199: public ServerBrowser(JFrame frame, AdminContext ac) {
200: //set our variables
201: this .frame = frame;
202: this .admin = ac;
203: //update our frame.
204: frame.addWindowListener(new WindowCloser());
205: frame.setJMenuBar(this .getMenuBar());
206: frame.setSize(800, 600);
207: //initialization
208: initialize();
209: //build the interface
210: build();
211: }
212:
213: /**
214: * Constructor.
215: * @param frame the JFrame containing ServerBrowser
216: */
217: protected ServerBrowser(JFrame frame) {
218: this .frame = frame;
219: frame.addWindowListener(new WindowCloser());
220: frame.setJMenuBar(this .getMenuBar());
221: frame.setSize(800, 600);
222: }
223:
224: /**
225: * Get the frame of ServerBrowser.
226: * @return a JFrame instance
227: */
228: public JFrame getFrame() {
229: return frame;
230: }
231:
232: /**
233: * Open the given URL (in another thread)
234: * @param admin the URL of the admin server.
235: */
236: protected void open(URL admin) {
237: (new Openner(admin)).start();
238: }
239:
240: /**
241: * Set the admin server URL.
242: * @param adminurl The admin server URL.
243: */
244: protected void setAdminURL(URL adminurl) {
245: try {
246: setCursor(Cursor.WAIT_CURSOR);
247: frame.invalidate();
248: this .removeAll();
249: this .admin = new AdminContext(adminurl);
250: initialize();
251: build();
252: RemoteResourceWrapper rrw = serverList.getModel()
253: .getServer(ServerListModel.ADMIN_SERVER_NAME);
254: serverSelected(ServerListModel.ADMIN_SERVER_NAME, rrw);
255: frame.validate();
256: setCursor(Cursor.DEFAULT_CURSOR);
257: frame.setTitle("Server Browser : " + adminurl);
258: } catch (RemoteAccessException ex) {
259: JOptionPane.showMessageDialog(this , ex.getMessage(),
260: "RemoteAccessException", JOptionPane.ERROR_MESSAGE);
261: }
262: }
263:
264: /**
265: * Initialize SErverBrowser.
266: */
267: protected void initialize() {
268: //check authorization
269: boolean authorized = false;
270: RemoteResource rr = null;
271: while (!authorized) {
272: try {
273: authorized = true;
274: admin.initialize();
275: } catch (RemoteAccessException ex) {
276: if (ex.getMessage().equals("Unauthorized")) {
277: authorized = false;
278: } else {
279: Message.showErrorMessage(this , ex);
280: }
281: } finally {
282: if (!authorized) {
283: popupPasswdDialog("admin");
284: }
285: }
286: }
287: try {
288: rr = admin.getAdminResource();
289: } catch (RemoteAccessException ex) {
290: // Unable to connect for whatever reason... exit!
291: ex.printStackTrace();
292: System.exit(0);
293: }
294: rootResource = new RemoteResourceWrapper(rr, this );
295: //build interface
296: }
297:
298: /**
299: * Build the interface.
300: */
301: protected void build() {
302: serverPanel = new DnDJPanel(new CardLayout());
303: serverPanel.setBorder(BorderFactory.createEmptyBorder(0, 2, 0,
304: 0));
305: serverList = getServerList();
306:
307: if (serverList == null)
308: return;
309: serverList.addServerListListener(this );
310:
311: String servers[] = serverList.getModel().getServers();
312: for (int i = 0; i < servers.length; i++) {
313: String server = servers[i];
314: RemoteResourceWrapper rrw = serverList.getModel()
315: .getServer(server);
316: //no way to get the component from the cardLayout :(
317: ServerEditorInterface editor = ServerEditorFactory
318: .getServerEditor(server, this , rrw);
319: serverPanel.add(server, editor.getComponent());
320: }
321:
322: JPanel serverListPanel = new JPanel(new BorderLayout());
323: serverListPanel.add(serverList, BorderLayout.NORTH);
324: serverListPanel.setBorder(BorderFactory.createEmptyBorder(1, 0,
325: 0, 0));
326:
327: setLayout(new BorderLayout());
328: add(serverListPanel, BorderLayout.WEST);
329: add(serverPanel, BorderLayout.CENTER);
330: setBorder(BorderFactory.createLoweredBevelBorder());
331: }
332:
333: /**
334: * Show the configuration of the given server
335: * @param name The server name
336: * @param rrw The server RemoteResourceWrapper
337: */
338: public void serverSelected(String name, RemoteResourceWrapper rrw) {
339: setCursor(Cursor.WAIT_CURSOR);
340: ((CardLayout) serverPanel.getLayout()).show(serverPanel, name);
341: ServerEditorFactory.updateServerEditor(name, this , rrw);
342: setCursor(Cursor.DEFAULT_CURSOR);
343: }
344:
345: /**
346: * Get the ServerList.
347: * @return a ServerList instance
348: * @see org.w3c.jigadmin.gui.slist.ServerList
349: */
350: protected ServerList getServerList() {
351: boolean authorized = false;
352: ServerList list = null;
353:
354: while (!authorized) {
355: try {
356: authorized = true;
357: list = new ServerList(getRootWrapper(),
358: Icons.serverIcon);
359: } catch (RemoteAccessException ex) {
360: if (ex.getMessage().equals("Unauthorized")) {
361: authorized = false;
362: } else {
363: Message.showErrorMessage(this , ex);
364: }
365: } finally {
366: if (!authorized) {
367: popupPasswdDialog("admin");
368: }
369: }
370: }
371: return list;
372: }
373:
374: /**
375: * give the Root Resource of the browser
376: */
377: public RemoteResourceWrapper getRootWrapper() {
378: return rootResource;
379: }
380:
381: /**
382: * Should I retry?
383: * @param ex the RemoteAccessException that occured
384: * @return a boolean
385: */
386: public boolean shouldRetry(RemoteAccessException ex) {
387: if (ex.getMessage().equals("Unauthorized")) {
388: popupPasswdDialog("admin");
389: return true;
390: } else {
391: Message.showErrorMessage(this , ex);
392: return false;
393: }
394: }
395:
396: /**
397: * Set The Cursor.
398: * @param cursor The Cursor type
399: */
400: public void setCursor(int cursor) {
401: frame.setCursor(Cursor.getPredefinedCursor(cursor));
402: }
403:
404: /**
405: * Popup a dialog that allows the user to enter his username/password.
406: * @param name the realm name
407: */
408: public void popupPasswdDialog(String name) {
409: if (popup == null) {
410: AuthPanel ap = new AuthPanel(this , name);
411: popup = new JDialog(frame, "Authorization for JigAdmin",
412: false);
413: Container cont = popup.getContentPane();
414: cont.setLayout(new GridLayout(1, 1));
415: cont.add(ap);
416: popup.setSize(new Dimension(300, 220));
417: popup.show();
418: ap.getFocus();
419: while (!ap.waitForCompletion())
420: ;
421: }
422: }
423:
424: /**
425: * Popup a dialog that allows the user to edit some resource properties.
426: * @param rrw the RemoteResourceWrapper of the resource to edit.
427: */
428: public void popupResource(RemoteResourceWrapper rrw) {
429: try {
430: setCursor(Cursor.WAIT_CURSOR);
431: JDialog popres = new JDialog(frame, "Edit Resource", false);
432: FramedResourceHelper helper = new FramedResourceHelper();
433: popres.setJMenuBar(helper.getMenuBar(popres));
434: PropertyManager pm = PropertyManager.getPropertyManager();
435: Properties props = pm.getEditorProperties(rrw);
436: helper.initialize(rrw, props);
437: Container cont = popres.getContentPane();
438:
439: cont.setLayout(new GridLayout(1, 1));
440: cont.add(helper.getComponent());
441: popres.setSize(new Dimension(500, 550));
442: popres.setLocationRelativeTo(this );
443: popres.show();
444: setCursor(Cursor.DEFAULT_CURSOR);
445: } catch (RemoteAccessException ex) {
446: Message.showErrorMessage(this , ex);
447: }
448: }
449:
450: /**
451: * Dispose the popupPasswdDialog and close the frame.
452: * @param oK if true dispose omly the dialog.
453: */
454: protected void dispose(boolean Ok) {
455: if (!Ok) {
456: WindowCloser.close(frame);
457: popup.dispose();
458: popup = null;
459: } else if (popup != null) {
460: popup.dispose();
461: popup = null;
462: }
463: }
464:
465: /**
466: * Returns the AdminContext.
467: * @return an AdminContext instance
468: * @see org.w3c.jigsaw.admin.AdminContext
469: */
470: protected AdminContext getAdminContext() {
471: return admin;
472: }
473:
474: /**
475: * Get the ServerBrowser MenuBar.
476: * @return a JMenuBar instance.
477: */
478: public JMenuBar getMenuBar() {
479: return new ServerMenu(this );
480: }
481:
482: public static void unBoldSpecificFonts() {
483: Font f = new Font("Dialog", Font.PLAIN, 12);
484:
485: UIManager.put("Button.font", f);
486: UIManager.put("CheckBox.font", f);
487: UIManager.put("CheckBoxMenuItem.font", f);
488: UIManager.put("ComboBox.font", f);
489: UIManager.put("DesktopIcon.font", f);
490:
491: UIManager.put("InternalFrame.font", f);
492: UIManager.put("InternalFrame.titleFont", f);
493: UIManager.put("Label.font", f);
494: UIManager.put("Menu.font", f);
495: UIManager.put("MenuBar.font", f);
496:
497: UIManager.put("MenuItem.font", f);
498: UIManager.put("ProgressBar.font", f);
499: UIManager.put("RadioButton.font", f);
500: UIManager.put("RadioButtonMenuItem.font", f);
501: UIManager.put("TabbedPane.font", f);
502:
503: UIManager.put("TitledBorder.font", f);
504: UIManager.put("ToggleButton.font", f);
505: UIManager.put("ToolBar.font", f);
506: }
507:
508: /**
509: * Run ServerBrowser.
510: */
511: public static void main(String args[]) {
512: String baseURL = null;
513: String jigadmRoot = null;
514:
515: for (int i = 0; i < args.length; i++) {
516: if (args[i].equals("-root")) {
517: Properties p = System.getProperties();
518: jigadmRoot = args[++i];
519: p.put(PropertyManager.ROOT_P, jigadmRoot);
520: System.setProperties(p);
521: } else {
522: baseURL = args[i];
523: }
524: }
525: if (baseURL == null)
526: baseURL = "http://localhost:8009/";
527: URL bu = null;
528: try {
529: bu = new URL(baseURL);
530: if (bu.getFile().length() == 0) {
531: bu = new URL(bu, "/");
532: }
533: } catch (MalformedURLException ex) {
534: System.err.println("Invalid URL : " + baseURL);
535: }
536: if (bu != null) {
537: try {
538: unBoldSpecificFonts();
539: AdminContext ac = new AdminContext(bu);
540: JFrame frame = new JFrame("Server Browser: " + baseURL);
541: ServerBrowser sb = new ServerBrowser(frame, ac);
542: frame.getContentPane().add(sb, BorderLayout.CENTER);
543: frame.setVisible(true);
544: } catch (RemoteAccessException raex) {
545: raex.printStackTrace();
546: }
547: }
548: }
549: }
|