<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * ContactPanel.java
 *
 * Created on Jan 8, 2010, 4:37:48 PM
 */

package usda.weru.weps.startup;

import java.awt.Component;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.RandomAccessFile;
import org.netbeans.spi.wizard.WizardPage;
import org.openide.util.Exceptions;
import usda.weru.util.About;
import usda.weru.util.ConfigData;

/**
 *
 * @author Joseph Levin &lt;joelevin@weru.ksu.edu&gt;
 */
public class UserPage extends WizardPage {
    private static final long serialVersionUID = 1L;

    private WelcomeWizard c_wizard;

    private boolean c_loaded;

    /** Creates new form ContactPanel */
    public UserPage() {
        initComponents();
     }

	/**
	 *
	 * @param wizard
	 */
	public UserPage(WelcomeWizard wizard){
        this();
        c_wizard = wizard;
       
        // for developer convenience
        // if file wepsdevname.txt exists in the user's home dir,
        // it can contain 2 lines of text: 1 for the name field and 1 for the email field
        try {
            RandomAccessFile developerNameFile = new RandomAccessFile (System.getProperty("user.home") + "/wepsdevname.txt", "r");
            String name = developerNameFile.readLine();
            String email = developerNameFile.readLine();
            if (name.length() &gt; 0) {
                nameField.setText(name);
                c_wizard.setValue(ConfigData.UserFullName, name);
            }
            if (email.length() &gt; 0) {
                emailField.setText(email);
                c_wizard.setValue(ConfigData.EmailSender, email);
            }
        } catch (FileNotFoundException ex) {
            // doesn't matter
        } catch (IOException ex) {
            // doesn't matter
        }
    }

	/**
	 *
	 * @return
	 */
	public static String getDescription(){
        return "User Information";
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // &lt;editor-fold defaultstate="collapsed" desc="Generated Code"&gt;//GEN-BEGIN:initComponents
    private void initComponents() {

        nameLabel = new javax.swing.JLabel();
        emailLabel = new javax.swing.JLabel();
        emailField = new javax.swing.JTextField();
        nameField = new javax.swing.JTextField();

        nameLabel.setFont(nameLabel.getFont().deriveFont(nameLabel.getFont().getStyle() &amp; ~java.awt.Font.BOLD));
        nameLabel.setText("Full name:");
        nameLabel.setFocusable(false);

        emailLabel.setFont(emailLabel.getFont().deriveFont(emailLabel.getFont().getStyle() &amp; ~java.awt.Font.BOLD));
        emailLabel.setText("Email address:");
        emailLabel.setFocusable(false);

        emailField.setFocusAccelerator('\t');

        nameField.setFocusAccelerator('\t');

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(emailLabel)
                    .addComponent(nameLabel))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(nameField, javax.swing.GroupLayout.DEFAULT_SIZE, 307, Short.MAX_VALUE)
                    .addComponent(emailField, javax.swing.GroupLayout.DEFAULT_SIZE, 307, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(nameLabel)
                    .addComponent(nameField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(emailLabel)
                    .addComponent(emailField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(243, Short.MAX_VALUE))
        );
    }// &lt;/editor-fold&gt;//GEN-END:initComponents


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTextField emailField;
    private javax.swing.JLabel emailLabel;
    private javax.swing.JTextField nameField;
    private javax.swing.JLabel nameLabel;
    // End of variables declaration//GEN-END:variables

	/**
	 *
	 */
	@Override
    protected void renderingPage() {
        if(c_loaded){
            //only need to load once
            return;
        }
        nameField.setText(c_wizard.getValue(ConfigData.UserFullName));
        //grab the first not null email setting
        emailField.setText(firstNotNull(true, c_wizard.getValue(ConfigData.EmailSender), c_wizard.getValue(ConfigData.MantisEmail)));
    }

	/**
	 *
	 * @param component
	 * @param event
	 * @return
	 */
	@Override
    protected String validateContents(Component component, Object event) {
        if(nameField.equals(component)){
            return firstNotNull(validateName(), validateEmail());
        }
        else if (emailField.equals(component)){
            return firstNotNull(validateEmail(), validateName());
        }
        else{
            return firstNotNull(validateName(), validateEmail());
        }
    }

    private String validateName(){
        String name = nameField.getText();
        String result = validateRequired("Full name", name);
        if(result == null){
            //set the value
            c_wizard.setValue(ConfigData.UserFullName, name);
        }
        return result;
    }

    private String validateRequired(String name, String value){
            boolean error = value== null || value.trim().length() == 0;
            return error ? (name + " is required.") : null;
    }

    private String validateEmail(){
        String email = emailField.getText();
        String result = firstNotNull(validateRequired("Email address", email), validateEmailAddress("Email address", email));
        if(result == null){
            //set the value, we have two email settings, might want to consolidate
            c_wizard.setValue(ConfigData.EmailSender, email);
            c_wizard.setValue(ConfigData.MantisEmail, email);
        }
        return result;
    }
    private String validateEmailAddress(String name, String email){
            boolean error = !email.contains("@") || !email.contains(".");
            return error ? (name + " is not valid.") : null;
    }


    private String firstNotNull(boolean trimToNull, String... ss){
        for(int i = 0; i &lt; ss.length; i++){
            String s = ss[i];
            if(s == null){
                continue;
            }
            s = s.trim();
            s = s.length() &gt; 0 ? s : null;
            ss[i] = s;
        }
        return firstNotNull(ss);
    }

    private String firstNotNull(String... ss){
        for (String s : ss){
            if (s != null){
                return s;
            }
        }
        return null;
    }


    


}
</pre></body></html>