home *** CD-ROM | disk | FTP | other *** search
/ The Net: Ultimate Internet Guide / WWLCD1.ISO / pc / java / in4wjcxu / src / como / sys / userpanel.java < prev   
Encoding:
Java Source  |  1996-08-14  |  4.2 KB  |  162 lines

  1. /*
  2. * @(#)UserPanel.java    1.0 95/11/09 Ulrich Gall & Jan Kautz
  3. *
  4. * Copyright (c) 1996 Ulrich Gall & Jan Kautz 
  5. * uhgall@cip.informatik.uni-erlangen.de
  6. * Hofmannstr. 48, D-91052 Erlangen, Germany, Fax: +49-9131-201358
  7. *
  8. * Permission to use, copy, and distribute this software
  9. * and its documentation for NON-COMMERCIAL purposes and without
  10. * fee is hereby granted provided that this copyright notice
  11. * appears in all copies. Please contact us for  further copyright 
  12. * and licensing information.
  13. *
  14. * WE MAKE NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
  15. * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
  16. * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  17. * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. WE SHALL NOT BE LIABLE FOR
  18. * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
  19. * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
  20. */
  21. package como.sys;
  22.  
  23.  
  24. import como.awt.*;
  25. import como.util.*;
  26.  
  27. import java.util.*;
  28. import java.awt.*;
  29.  
  30. /**
  31.  * This class displays and edits information about users. 
  32.  * All fields are displayed. Standard fields are displayed neatly.
  33.  */
  34.  
  35. public class UserPanel extends Panel
  36. {
  37.     boolean edit;
  38.     User user;
  39.     Button updatebutton;
  40.     Hashtable comps;
  41.     String okbuttontext;
  42.     Image map;
  43.     Panel addcomppanel;
  44.  
  45.     /**
  46.     * Constructs a new Panel from the specified user
  47.     */
  48.     public UserPanel(User u,String okbutton)
  49.     {
  50.         super();
  51.         edit = (okbutton.length() != 0);
  52.         okbuttontext = okbutton;
  53.         setUser(u);
  54.     }
  55.     public UserPanel(User u,String okbutton,Image mapimg)
  56.     {
  57.         super();
  58.         edit = (okbutton.length() != 0);
  59.         okbuttontext = okbutton;
  60.         map = mapimg;
  61.         setUser(u);
  62.     }
  63.     public UserPanel() {
  64.         super();
  65.         edit = false;
  66.         setUser(null);
  67.         }
  68.     public void setUser(User u)
  69.     {
  70.         user = u;
  71.         if (user != null) {
  72.         comps = new Hashtable();
  73.         removeAll();
  74.         setLayout(new BorderLayout());
  75.         addcomppanel = new Panel();
  76.         addcomppanel.setLayout(new VertLayout(VertLayout.STRETCH));    
  77.         add("North",addcomppanel);
  78.  
  79.         addComp("ID",User.ID,false);
  80.         if (!user.containsKey(User.NAME)) user.put(User.NAME,"Unknown");
  81.         addComp("Name",User.NAME,edit);
  82.         addComp("Nickname",User.NICK,false);
  83.         addComp("Host address",User.ADDRESS,false);
  84.         addComp("Color",User.COLOR,edit);
  85.         if (!user.containsKey(User.COMMENT)) user.put(User.COMMENT,"Unknown");
  86.         addComp("Comment",User.COMMENT,edit);
  87.         
  88.         if ((map != null) && user.containsKey(User.LOCATION)) {
  89.             ImageClick ic = new ImageClick(map);
  90.             if (user.get(User.LOCATION) instanceof Point) ic.setPos((Point)user.get(User.LOCATION));
  91.             ic.setEditable(edit);
  92.             add("Center",ic);
  93.             comps.put(ic,User.LOCATION);
  94.         }
  95.         
  96.         if (edit) {
  97.             updatebutton = new Button(okbuttontext);
  98.             add("South",updatebutton);
  99.         }
  100.         }
  101.     }
  102.     public void setEditable(boolean b) {
  103.     edit = b;
  104.         }
  105.     public void addComp(String desc,Object key,boolean e) {
  106.         if (user.containsKey(key)) {
  107.             Object o = user.get(key);    
  108.             if (o instanceof String) {
  109.                 Panel p = new Panel();
  110.                 p.setLayout(new BorderLayout());
  111.                 p.add("West",new Label(desc+": "));
  112.                 TextField tf = new TextField((String)o);
  113.                 tf.setEditable(e);
  114.                 comps.put(tf,key);
  115.                 p.add("Center",tf);
  116.                 addcomppanel.add(p);
  117.             }
  118.             else if (o instanceof Color) {
  119.                 if (e) {
  120.                     addcomppanel.add(new Label(desc));
  121.                     ColorSelector cs = new ColorSelector((Color)o);
  122.                     comps.put(cs,key);
  123.                     addcomppanel.add(cs);
  124.                 }
  125.                 else {
  126.                     Panel p = new Panel();
  127.                     p.setLayout(new BorderLayout());
  128.                     p.add("West",new Label(desc+": "));
  129.                     Canvas c = new Canvas();
  130.                     c.setBackground((Color)o);
  131.                     p.add("Center",c);
  132.                     addcomppanel.add(p);
  133.                 }
  134.             }
  135.         }
  136.     }
  137.     public User getUser() {
  138.         if (edit) {
  139.         Enumeration e = comps.keys(); // enumerates the components...
  140.         while(e.hasMoreElements()) {
  141.             Component c = (Component)e.nextElement();
  142.             Object key = comps.get(c);
  143.             if (c instanceof TextField) user.put(key,((TextField)c).getText());
  144.             if (c instanceof ColorSelector) user.put(key,((ColorSelector)c).getColor());
  145.             }
  146.             }
  147.         return user;
  148.         }
  149.     public boolean action(Event evt, Object arg) {
  150.         if (edit) {
  151.             if (evt.target == updatebutton) {
  152.                 getParent().postEvent(new Event(this,Event.ACTION_EVENT,getUser()));        
  153.                 return true;
  154.             }
  155.             Object key = comps.get(evt.target);
  156.             user.put(key,evt.arg);
  157.         }
  158.         return true;
  159.     }
  160.  
  161. }
  162.