HomeDigital EditionSys-Con RadioSearch Java Cd
Advanced Java AWT Book Reviews/Excerpts Client Server Corba Editorials Embedded Java Enterprise Java IDE's Industry Watch Integration Interviews Java Applet Java & Databases Java & Web Services Java Fundamentals Java Native Interface Java Servlets Java Beans J2ME Libraries .NET Object Orientation Observations/IMHO Product Reviews Scalability & Performance Security Server Side Source Code Straight Talking Swing Threads Using Java with others Wireless XML
 

"Persistent User Interface for Multi-User Applications"
Volume: 3 Issue: 8 p. 16

	

Listing 1: The SmartLogin Constructor.
 
private SmartLogin(String profile) { 
   this.profile = profile; 
   setTitle("Smart Login"); 
   setSize(400,200); 
   setLayout(new GridLayout(6,1)); 
   add(new Label("Name:")); 
   add(tName = new TextField()); 
   Panel p = new Panel(); 
   p.setLayout(new GridLayout(1, 3)); 
   p.add(cMan = new Checkbox("Manager")); 
   p.add(cDes = new Checkbox("Designer")); 
   p.add(cDev = new Checkbox("Developer")); 
   add(p); 
   add(new Label("Password:")); 
   add(tPassw = new TextField()); 
   SmartAdapter adapter = new SmartAdapter(this); 
   Panel q = new Panel(); 
   q.setLayout(new FlowLayout()); 
   q.add(bOk = new Button("    Ok    ")); 
   bOk.setActionCommand(OK); 
   bOk.addActionListener(adapter); 
   q.add(bCancel = new Button("  Cancel  ")); 
   bCancel.addActionListener(adapter); 
   add(q); 
   addWindowListener(adapter); 
} 

Listing 2: The SmartAdapter Class.
 
class SmartAdapter extends WindowAdapter 
      implements ActionListener, Serializable { 

SmartLogin sl; 

SmartAdapter(SmartLogin sl) { 
   this.sl = sl; 
} 

public void actionPerformed(ActionEvent e) { 
   if (SmartLogin.OK.equals(e.getActionCommand())) { 
      sl.login(); 
      sl.serialize(); 
   } else 
      sl.clearPassw(); 
   System.exit(0); 
} 

public void windowClosing(WindowEvent e) { 
   sl.clearPassw(); 
   System.exit(0); 
} 

} 
  

// SmartLogin.java 

import java.awt.*; 
import java.awt.event.*; 
import java.io.*; 

public class SmartLogin extends Frame 
{ 
    public  static final String OK = "OK"; 
    private transient String profile; 
    private TextField tName, tPassw; 
    private Checkbox  cMan, cDes, cDev; 
    private Button    bOk, bCancel; 

    // Creates the user interface 
    private SmartLogin(String profile) 
    { 
        this.profile = profile; 
        setTitle("Smart Login"); 
        setSize(400,200); 
        setLayout(new GridLayout(6,1)); 
        add(new Label("Name:")); 
        add(tName = new TextField()); 
        Panel p = new Panel(); 
        p.setLayout(new GridLayout(1, 3)); 
        p.add(cMan = new Checkbox("Manager")); 
        p.add(cDes = new Checkbox("Designer")); 
        p.add(cDev = new Checkbox("Developer")); 
        add(p); 
        add(new Label("Password:")); 
        add(tPassw = new TextField()); 
        SmartAdapter adapter = new SmartAdapter(this); 
        Panel q = new Panel(); 
        q.setLayout(new FlowLayout()); 
        q.add(bOk = new Button("    Ok    ")); 
        bOk.setActionCommand(OK); 
        bOk.addActionListener(adapter); 
        q.add(bCancel = new Button("  Cancel  ")); 
        bCancel.addActionListener(adapter); 
        add(q); 
        addWindowListener(adapter); 
    } 

    // Simulates a login 
    public void login() 
    { 
        System.out.println("Hello " + tName.getText()); 
        StringBuffer userType = new StringBuffer(); 
        if (cMan.getState()) 
            userType.append("manager "); 
        if (cDes.getState()) 
        { 
            if (userType.length() != 0) 
                userType.append("and "); 
            userType.append("designer "); 
        } 
        if (cDev.getState()) 
        { 
            if (userType.length() != 0) 
                userType.append("and "); 
            userType.append("developer "); 
        } 
        if (userType.length() == 0) 
            userType.append("simple user"); 
        System.out.println("You have logged as " + userType); 
    } 

    // Clear the password maintained in a private field of tPassw 
    public void clearPassw() 
    { 
        tPassw.setText(""); 
        System.gc(); 
    } 

    // Sets the profile member variable 
    public void setProfile(String profile) 
    { 
        this.profile = profile; 
    } 

    // Serialize the user interface of the application 
    public void serialize() 
    { 
        clearPassw(); 
        try 
        { 
            FileOutputStream out = new FileOutputStream(profile + 
".ser"); 
            ObjectOutputStream s = new ObjectOutputStream(out); 
            s.writeObject(this); 
            s.flush(); 
            s.close(); 
        } 
        catch (IOException e) 
            { System.out.println(e); } 
        catch (SecurityException e) 
            { System.out.println(e); } 
    } 

    // The main method 
    public static void main(String args[]) 
    { 
        SmartLogin sl; 
        String profile = args.length > 0 ? args[0] : "default"; 
        // Tries to deserialize the user interface 
        try 
        { 
            FileInputStream in = new FileInputStream(profile + ".ser"); 
            ObjectInputStream s = new ObjectInputStream(in); 
            sl = (SmartLogin) s.readObject(); 
            s.close(); 
            sl.setProfile(profile); 
            sl.show(); 
            return; 
        } 
        catch (FileNotFoundException e) 
            { } 
        catch (IOException e) 
            { System.out.println(e); } 
        catch (ClassNotFoundException e) 
            { System.out.println(e); } 
        catch (SecurityException e) 
            { System.out.println(e); } 
        // The deserialization failed 
        // A new SmartLogin instance will be created 
        sl = new SmartLogin(profile); 
        sl.show(); 
    } 

} 

class SmartAdapter extends WindowAdapter implements ActionListener, 
Serializable 
{ 
    SmartLogin sl; 

    SmartAdapter(SmartLogin sl) 
    { 
        this.sl = sl; 
    } 
  
    // This method is called when a button is pushed and released 
    public void actionPerformed(ActionEvent e) 
    { 
        if (SmartLogin.OK.equals(e.getActionCommand())) 
        { 
            sl.login(); 
            sl.serialize(); 
        } 
        else 
            sl.clearPassw(); 
        System.exit(0); 
    } 

    // This method is called when the window of the application is 
closed 
    public void windowClosing(WindowEvent e) 
    { 
        sl.clearPassw(); 
        System.exit(0); 
    } 

} 
  
      
 

All Rights Reserved
Copyright ©  2004 SYS-CON Media, Inc.
  E-mail: [email protected]

Java and Java-based marks are trademarks or registered trademarks of Sun Microsystems, Inc. in the United States and other countries. SYS-CON Publications, Inc. is independent of Sun Microsystems, Inc.