001    package com.sptci.echo2;
002    
003    import nextapp.echo2.app.Button;
004    import nextapp.echo2.app.Component;
005    import nextapp.echo2.app.Grid;
006    import nextapp.echo2.app.Label;
007    import nextapp.echo2.app.PasswordField;
008    import nextapp.echo2.app.Row;
009    import nextapp.echo2.app.SplitPane;
010    import nextapp.echo2.app.TextField;
011    import nextapp.echo2.app.WindowPane;
012    
013    import com.sptci.echo2.Configuration;
014    import com.sptci.echo2.Utilities;
015    
016    /**
017     * A view component that is used to display login controls to the
018     * user.
019     * 
020     * <p>The following shows usage of this class.  Since the associated
021     * {@link LoginPaneController} is abstract this component cannot be directly
022     * instantiated.  It will be instantiated through the sub-class of
023     * LoginPaneController.</p>
024     * 
025     * <pre>
026     *   import com.sptci.echo2.LoginPane;
027     * 
028     *     ...
029     *     // MyLoginController is your sub-class of LoginPaneController
030     *     MyLoginController controller = new MyLoginController();
031     *     LoginPane pane = controller.getView();
032     * </pre>
033     * 
034     * <p>Copyright 2006 Sans Pareil Technologies, Inc.</p>
035     * 
036     * @author Rakesh Vidyadharan 2006-11-23
037     * @version $Id: LoginPane.java 3129 2007-04-23 22:37:20Z rakesh $
038     */
039    public class LoginPane extends WindowPane
040    {
041      /**
042       * The name of the {@link #userName} field.
043       *
044       * {@value}
045       */
046      public static final String USERNAME = "userName";
047    
048      /**
049       * The name of the {@link #password} field.
050       *
051       * {@value}
052       */
053      public static final String PASSWORD = "password";
054    
055      /**
056       * The name of the {@link #login} field used to trigger the login process.
057       *
058       * {@value}
059       */
060      public static final String LOGIN = "login";
061    
062      /**
063       * The component to use to capture user/account name information.
064       */
065      protected TextField userName;
066    
067      /**
068       * The component to use to capture password information.
069       */
070      protected PasswordField password;
071    
072      /**
073       * The button used to trigger a login action.
074       */
075      protected Button login;
076    
077      /**
078       * The controller for this view.
079       */
080      protected LoginPaneController controller;
081    
082      /**
083       * Initialise the view using the specified {@link #controller}.
084       *
085       * <p><b>Note:</b> After creating a new instance of this view component,
086       * {@link #setController} and {@link #initComponents} must be invoked
087       * in the above order.</p>
088       */
089      protected LoginPane()
090      {
091        setStyleName( getClass().getName() );
092        setTitle( Configuration.getString( this, "title" ) );
093        setDefaultCloseOperation( WindowPane.DO_NOTHING_ON_CLOSE );
094      }
095    
096      /**
097       * Initialise the components of this pane.
098       *
099       * @see #createComponent
100       * @see #createLogin
101       */
102      protected void initComponents()
103      {
104        Grid grid = new Grid();
105        grid.setStyleName( getClass().getName() + ".grid" );
106    
107        createComponent( USERNAME, grid );
108        createComponent( PASSWORD, grid );
109        createLogin( grid );
110    
111        add( grid );
112        controller.getApplication().setFocusedComponent( userName );
113      }
114    
115      /**
116       * Initialise the specified component and add to the specified container
117       * component.
118       * 
119       * @param field The field that is to be initialised.
120       * @param component The parent container to which the field is to be
121       *   added.
122       * @see LoginPaneController#getListener
123       */
124      protected void createComponent( String field, Component component )
125      {
126        component.add( Utilities.createLabel( getClass().getName(), field ) );
127    
128        component.add( Utilities.createTextField( getClass().getName(),
129              field, controller.getListener( field ), this ) );
130      }
131    
132      /**
133       * Initialise the {@link #login} component and add to the specified
134       * container component.
135       * 
136       * @param component The parent container to which the field is to be
137       *   added.
138       * @see LoginPaneController#getListener
139       */
140      protected void createLogin( Component component )
141      {
142        component.add( Utilities.createButton( getClass().getName(),
143              LOGIN, controller.getListener( LOGIN ), this ) );
144      }
145    
146      /**
147       * Return the text entered into {@link #userName}.
148       *
149       * @return The value entered into the component.
150       */
151      public String getUserName()
152      {
153        return userName.getText();
154      }
155    
156      /**
157       * Return the text entered into {@link #password}.
158       *
159       * @return The value entered into the component.
160       */
161      public String getPassword()
162      {
163        return password.getText();
164      }
165      
166      /**
167       * Returns {@link #controller}.
168       *
169       * @return The value/reference of/to controller.
170       */
171      public LoginPaneController getController()
172      {
173        return controller;
174      }
175      
176      /**
177       * Set {@link #controller}.
178       *
179       * @param controller The value to set.
180       */
181      protected void setController( LoginPaneController controller )
182      {
183        this.controller = controller;
184      }
185    }