/*
Get information from the user and save it to RegForm.Txt in the installation directory
Notes:
1. This panel should only be entered in the "After uninstaller created" field on the
Custom panel so we know where to store the information
2. You are welcome to adapt this class to meet your needs as long as you
only use it with the JExpress product
Copyright (C) 1998-2011 DeNova
*/
import java.awt.BorderLayout;
import java.awt.GridBagConstraints;
import java.io.File;
import java.io.FileOutputStream;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import com.denova.JExpress.Installer.CustomUninstaller;
import com.denova.JExpress.Installer.InstallPropertyNames;
import com.denova.ui.Fonts;
import com.denova.ui.GridBagControl;
import com.denova.ui.UiLayoutUtilities;
import com.denova.ui.UserNotices;
import com.denova.ui.WizardPanel;
import com.denova.util.PropertyList;
public class RegisterPanel extends WizardPanel
implements InstallPropertyNames {
private static final String UserName = "userName";
private static final String Company = "company";
private static final String EmailAddress = "emailAddress";
private static final String SerialNumber = "serialNumber";
private JTextField userName;
private JTextField company;
private JTextField emailAddress;
private JTextField serialNumber;
private JLabel hint;
public RegisterPanel ( PropertyList properties) {
// initialize WizardPanel superclass
super (properties);
addPanel();
}
/**
* Enter the panel.
*
* Do not call this method; only the wizard should call it.
*/
public void enter()
{
// there's nothing special to do because the WizardPanel
// will automatically move to the next panel when the user
// presses the Next button *if* all information is correct
}
public boolean isOk () {
boolean ok;
if ( allFieldsFilledIn () ) {
ok = true;
UiLayoutUtilities. update ( hint, "Saving registration" );
saveRegistration ();
}
// let the user know they need all the information
// and move them back to the panel where they can try again
else {
ok = false;
UserNotices un = new UserNotices();
un.noteError("You must fill in all fields.");
}
return ok;
}
boolean allFieldsFilledIn () {
return fieldFilledIn ( userName. getText () ) &&
fieldFilledIn ( company. getText () ) &&
fieldFilledIn ( emailAddress. getText () ) &&
fieldFilledIn ( serialNumber. getText () );
}
boolean fieldFilledIn ( String s ) {
return s. trim (). length () > 0;
}
void saveRegistration () {
// save the information
String applicationDirectory =
getPropertyList (). getProperty ( ApplicationDirectory, "" );
// only do this after we know where the installation directory is
if ( applicationDirectory != null ) {
// let's create a local property list that holds just the info we want
PropertyList list = new PropertyList();
// get the following properties from the control file
// and if any of the properties don't exist, then default them to blank fields
addProperty ( list, UserName, userName. getText () );
addProperty ( list, Company, company. getText () );
addProperty ( list, EmailAddress, emailAddress. getText () );
addProperty ( list, SerialNumber, serialNumber. getText () );
addProperty ( list, "packageName", getPropertyList (). getProperty ( PackageName, "" ) );
addProperty ( list, "packageVersion", getPropertyList (). getProperty ( PackageVersion, "" ));
// save the information to a text file
try {
File f = new File ( applicationDirectory, "regform.txt" );
FileOutputStream o = new FileOutputStream ( f );
// save the property list
list. save ( o, "Registration" );
// close the output stream
o. close ();
// append the appropriate commands to the uninstaller
CustomUninstaller customUninstaller = new CustomUninstaller ();
File formFile = new File ( applicationDirectory, "regform.txt" );
customUninstaller. deleteFile ( formFile. getPath () );
customUninstaller. append ( applicationDirectory );
}
// if anything goes wrong, let the user know
catch ( Exception e ) {
UserNotices un = new UserNotices();
un.noteError("Unable to save the registration information." );
}
}
}
void addProperty ( PropertyList list,
String attribute,
String propertyValue ) {
list. setProperty ( attribute,
propertyValue );
}
private void addPanel()
{
// set an outer border on the panel
final int Margin = 20;
setLayout ( new BorderLayout());
setBorder ( UiLayoutUtilities.getMarginBorder(Margin) );
// use DeNova's magic class that tames GridBag
GridBagConstraints labelConstraints = GridBagControl. getDefaultConstraints ();
labelConstraints. anchor = GridBagConstraints. CENTER;
GridBagConstraints textConstraints = GridBagControl. getDefaultConstraints ();
textConstraints. weightx = 1.0;
textConstraints. fill = GridBagConstraints. HORIZONTAL;
GridBagConstraints buttonConstraints = GridBagControl. getDefaultConstraints ();
buttonConstraints. fill = GridBagConstraints. HORIZONTAL;
JPanel p = new JPanel ();
GridBagControl gc = new GridBagControl ( p );
gc. addVerticalSpace ();
JLabel title = new JLabel ( "Registration Form" );
title.setFont(Fonts.Bold);
gc. endRow ( title );
gc. add ( labelConstraints, new JLabel ( "Contact name:" ) );
userName = new JTextField();
userName. setToolTipText ( "Your first and last name" );
userName. setEditable ( true );
gc. endRow( textConstraints, userName );
gc. add ( labelConstraints, new JLabel ( "Company:" ) );
company = new JTextField();
company. setToolTipText ( "Your company's name" );
company. setEditable ( true );
gc. endRow( textConstraints, company );
gc. add ( labelConstraints, new JLabel ( "E-mail address:" ) );
emailAddress = new JTextField();
emailAddress. setToolTipText ( "Your e-mail address, i.e., user@domain.com" );
emailAddress. setEditable ( true );
gc. endRow( textConstraints, emailAddress );
gc. add ( labelConstraints, new JLabel ( "Serial number:" ) );
serialNumber = new JTextField();
serialNumber. setToolTipText ( "Product's serial number" );
serialNumber. setEditable ( true );
gc. endRow( textConstraints, serialNumber );
hint = new JLabel ( "Please fill in all fields" );
gc. addCentered ( hint );
add ( p );
}
} // RegisterPanel