SWT(JFace) Wizard(Eclipse插件编程必备)

演示代码如下:

HotelReservation.java

代码如下:

package swt_jface.demo12;
import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.jface.window.ApplicationWindow;
import org.eclipse.jface.wizard.WizardDialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;
public class HotelReservation extends ApplicationWindow {
protected Control createContents(Composite parent) {
Button button = new Button(parent, SWT.PUSH);
button.setText("Make a reservation");
button.addListener(SWT.Selection, new Listener() {
public void handleEvent(Event event) {
ReservationWizard wizard = new ReservationWizard();

WizardDialog dialog = new WizardDialog(getShell(), wizard);
dialog.setBlockOnOpen(true);
int returnCode = dialog.open();
if(returnCode == Dialog.OK)
System.out.println(wizard.data);
else
System.out.println("Cancelled");
}
});
return button;
}
public HotelReservation(Shell parentShell) {
super(parentShell);
}
public static void main(String[] args) {
HotelReservation reservation = new HotelReservation(null);
reservation.setBlockOnOpen(true);
reservation.open();
}
}
package swt_jface.demo12;
import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.jface.window.ApplicationWindow;
import org.eclipse.jface.wizard.WizardDialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;
public class HotelReservation extends ApplicationWindow {
    protected Control createContents(Composite parent) {
        Button button = new Button(parent, SWT.PUSH);
        button.setText("Make a reservation");
        button.addListener(SWT.Selection, new Listener() {
            public void handleEvent(Event event) {
                ReservationWizard wizard = new ReservationWizard();

WizardDialog dialog = new WizardDialog(getShell(), wizard);
                dialog.setBlockOnOpen(true);
                int returnCode = dialog.open();
                if(returnCode == Dialog.OK)
                    System.out.println(wizard.data);
                else
                    System.out.println("Cancelled");
            }
        });
        return button;
    }
    public HotelReservation(Shell parentShell) {
        super(parentShell);
    }
    public static void main(String[] args) {
        HotelReservation reservation = new HotelReservation(null);
        reservation.setBlockOnOpen(true);
        reservation.open();
    }
}

CustomerInfoPage.java

代码如下:

package swt_jface.demo12;
import org.eclipse.jface.wizard.WizardPage;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Text;
public class CustomerInfoPage extends WizardPage {

Text textName;
Text textPhone;
Text textEmail;
Text textAddress;
public CustomerInfoPage() {
super("CustomerInfo");
setTitle("Customer Information");
setPageComplete(false);
}
public void createControl(Composite parent) {

Composite composite = new Composite(parent, SWT.NULL);
composite.setLayout(new GridLayout(2, false));
new Label(composite, SWT.NULL).setText("Full name: ");
textName = new Text(composite, SWT.SINGLE | SWT.BORDER);
textName.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
new Label(composite, SWT.NULL).setText("Phone Number: ");
textPhone = new Text(composite, SWT.SINGLE | SWT.BORDER);
textPhone.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
new Label(composite, SWT.NULL).setText("Email address: ");
textEmail = new Text(composite, SWT.SINGLE | SWT.BORDER);
textEmail.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
new Label(composite, SWT.NULL).setText("Address: ");
textAddress = new Text(composite, SWT.MULTI | SWT.BORDER);
textAddress.setText("\r\n\r\n\r\n");
textAddress.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
Listener listener = new Listener() {
public void handleEvent(Event event) {
if (event.widget == null || !(event.widget instanceof Text)) return;
String string = ((Text) event.widget).getText();
if (event.widget == textName) {
((ReservationWizard) getWizard()).data.customerName = string;
} else if (event.widget == textPhone) {
((ReservationWizard) getWizard()).data.customerPhone = string;
} else if (event.widget == textEmail) {
if (string.indexOf('@') < 0) {
setErrorMessage("Invalid email address: " + string);
((ReservationWizard) getWizard()).data.customerEmail = null;
} else {
setErrorMessage(null);
((ReservationWizard) getWizard()).data.customerEmail = string;
}
} else if (event.widget == textAddress) {
((ReservationWizard) getWizard()).data.customerAddress = string;
}
ReservationData data = ((ReservationWizard) getWizard()).data;
if (data.customerName != null
&& data.customerPhone != null
&& data.customerEmail != null
&& data.customerAddress != null) {
setPageComplete(true);
} else {
setPageComplete(false);
}
}
};
textName.addListener(SWT.Modify, listener);
textPhone.addListener(SWT.Modify, listener);
textEmail.addListener(SWT.Modify, listener);
textAddress.addListener(SWT.Modify, listener);
if (getDialogSettings() != null && validDialogSettings()) {
textName.setText(getDialogSettings().get(ReservationWizard.KEY_CUSTOMER_NAME));
textPhone.setText(getDialogSettings().get(ReservationWizard.KEY_CUSTOMER_PHONE));
textEmail.setText(getDialogSettings().get(ReservationWizard.KEY_CUSTOMER_EMAIL));
textAddress.setText(getDialogSettings().get(ReservationWizard.KEY_CUSTOMER_ADDRESS));
}
setControl(composite);
}

private boolean validDialogSettings() {
if (getDialogSettings().get(ReservationWizard.KEY_CUSTOMER_NAME) == null
|| getDialogSettings().get(ReservationWizard.KEY_CUSTOMER_ADDRESS) == null
|| getDialogSettings().get(ReservationWizard.KEY_CUSTOMER_EMAIL) == null
|| getDialogSettings().get(ReservationWizard.KEY_CUSTOMER_PHONE) == null) return false;
return true;
}
}
package swt_jface.demo12;
import org.eclipse.jface.wizard.WizardPage;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Text;
public class CustomerInfoPage extends WizardPage {

Text textName;
    Text textPhone;
    Text textEmail;
    Text textAddress;
    public CustomerInfoPage() {
        super("CustomerInfo");
        setTitle("Customer Information");
        setPageComplete(false);
    }
    public void createControl(Composite parent) {

Composite composite = new Composite(parent, SWT.NULL);
        composite.setLayout(new GridLayout(2, false));
        new Label(composite, SWT.NULL).setText("Full name: ");
        textName = new Text(composite, SWT.SINGLE | SWT.BORDER);
        textName.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
        new Label(composite, SWT.NULL).setText("Phone Number: ");
        textPhone = new Text(composite, SWT.SINGLE | SWT.BORDER);
        textPhone.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
        new Label(composite, SWT.NULL).setText("Email address: ");
        textEmail = new Text(composite, SWT.SINGLE | SWT.BORDER);
        textEmail.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
        new Label(composite, SWT.NULL).setText("Address: ");
        textAddress = new Text(composite, SWT.MULTI | SWT.BORDER);
        textAddress.setText("\r\n\r\n\r\n");
        textAddress.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
        Listener listener = new Listener() {
            public void handleEvent(Event event) {
                if (event.widget == null || !(event.widget instanceof Text)) return;
                String string = ((Text) event.widget).getText();
                if (event.widget == textName) {
                    ((ReservationWizard) getWizard()).data.customerName = string;
                } else if (event.widget == textPhone) {
                    ((ReservationWizard) getWizard()).data.customerPhone = string;
                } else if (event.widget == textEmail) {
                    if (string.indexOf('@') < 0) {
                        setErrorMessage("Invalid email address: " + string);
                        ((ReservationWizard) getWizard()).data.customerEmail = null;
                    } else {
                        setErrorMessage(null);
                        ((ReservationWizard) getWizard()).data.customerEmail = string;
                    }
                } else if (event.widget == textAddress) {
                    ((ReservationWizard) getWizard()).data.customerAddress = string;
                }
                ReservationData data = ((ReservationWizard) getWizard()).data;
                if (data.customerName != null
                    && data.customerPhone != null
                    && data.customerEmail != null
                    && data.customerAddress != null) {
                    setPageComplete(true);
                } else {
                    setPageComplete(false);
                }
            }
        };
        textName.addListener(SWT.Modify, listener);
        textPhone.addListener(SWT.Modify, listener);
        textEmail.addListener(SWT.Modify, listener);
        textAddress.addListener(SWT.Modify, listener);
        if (getDialogSettings() != null && validDialogSettings()) {
                textName.setText(getDialogSettings().get(ReservationWizard.KEY_CUSTOMER_NAME));
                textPhone.setText(getDialogSettings().get(ReservationWizard.KEY_CUSTOMER_PHONE));
                textEmail.setText(getDialogSettings().get(ReservationWizard.KEY_CUSTOMER_EMAIL));
                textAddress.setText(getDialogSettings().get(ReservationWizard.KEY_CUSTOMER_ADDRESS));
        }
        setControl(composite);
    }

private boolean validDialogSettings() {
        if (getDialogSettings().get(ReservationWizard.KEY_CUSTOMER_NAME) == null
            || getDialogSettings().get(ReservationWizard.KEY_CUSTOMER_ADDRESS) == null
            || getDialogSettings().get(ReservationWizard.KEY_CUSTOMER_EMAIL) == null
            || getDialogSettings().get(ReservationWizard.KEY_CUSTOMER_PHONE) == null) return false;
        return true;
    }
}

FrontPage.java

代码如下:

package swt_jface.demo12;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import org.eclipse.jface.wizard.WizardPage;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.layout.RowLayout;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Listener;
public class FrontPage extends WizardPage {

Combo comboRoomTypes;
Combo comboArrivalYear;
Combo comboArrivalMonth;
Combo comboArrivalDay;
Combo comboDepartureYear;
Combo comboDepartureMonth;
Combo comboDepartureDay;

FrontPage() {
super("FrontPage");
setTitle("Your reservation information");
setDescription("Select the type of room and your arrival date & departure date");
}
public void createControl(Composite parent) {

Composite composite = new Composite(parent, SWT.NULL);
GridLayout gridLayout = new GridLayout(2, false);
composite.setLayout(gridLayout);
new Label(composite, SWT.NULL).setText("Arrival date: ");
Composite compositeArrival = new Composite(composite, SWT.NULL);
compositeArrival.setLayout(new RowLayout());
String[] months = new String[]{"Jan", "Feb", "Mar", "Apr", "May", "Jun",
"Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
};
Calendar calendar = new GregorianCalendar();
((ReservationWizard)getWizard()).data.arrivalDate = calendar.getTime();
comboArrivalMonth = new Combo(compositeArrival, SWT.BORDER | SWT.READ_ONLY);
for(int i=0; i<months.length; i++) comboArrivalMonth.add(months[i]);
comboArrivalMonth.select(calendar.get(Calendar.MONTH));
comboArrivalDay = new Combo(compositeArrival, SWT.BORDER | SWT.READ_ONLY);
for(int i=0; i<31; i++) comboArrivalDay.add("" + (i+1));
comboArrivalDay.select(calendar.get(Calendar.DAY_OF_MONTH)-1);
comboArrivalYear = new Combo(compositeArrival, SWT.BORDER | SWT.READ_ONLY);
for(int i=2004; i<2010; i++) comboArrivalYear.add("" + i);
comboArrivalYear.select(calendar.get(Calendar.YEAR)-2004);
calendar.add(Calendar.DATE, 1);
((ReservationWizard)getWizard()).data.departureDate = calendar.getTime();
new Label(composite, SWT.NULL).setText("Departure date: ");
Composite compositeDeparture = new Composite(composite, SWT.NULL | SWT.READ_ONLY);
compositeDeparture.setLayout(new RowLayout());
comboDepartureMonth = new Combo(compositeDeparture, SWT.NULL | SWT.READ_ONLY);
for(int i=0; i<months.length; i++) comboDepartureMonth.add(months[i]);
comboDepartureMonth.select(calendar.get(Calendar.MONTH));
comboDepartureDay = new Combo(compositeDeparture, SWT.NULL | SWT.READ_ONLY);
for(int i=0; i<31; i++) comboDepartureDay.add("" + (i+1));
comboDepartureDay.select(calendar.get(Calendar.DAY_OF_MONTH)-1);
comboDepartureYear = new Combo(compositeDeparture, SWT.NULL | SWT.READ_ONLY);
for(int i=2004; i<2010; i++) comboDepartureYear.add("" + i);
comboDepartureYear.select(calendar.get(Calendar.YEAR)-2004);
Label line = new Label(composite, SWT.SEPARATOR | SWT.HORIZONTAL);
GridData gridData = new GridData(GridData.FILL_HORIZONTAL);
gridData.horizontalSpan = 2;
line.setLayoutData(gridData);
new Label(composite, SWT.NULL).setText("Room type: ");
comboRoomTypes = new Combo(composite, SWT.BORDER | SWT.READ_ONLY);
comboRoomTypes.add("Standard room (rate: $198)");
comboRoomTypes.add("Deluxe room (rate: $298)");
comboRoomTypes.select(0);
Listener selectionListener = new Listener() {
public void handleEvent(Event event) {
int arrivalDay = comboArrivalDay.getSelectionIndex() + 1;
int arrivalMonth = comboArrivalMonth.getSelectionIndex();
int arrivalYear = comboArrivalYear.getSelectionIndex() + 2004;
int departureDay = comboDepartureDay.getSelectionIndex() + 1;
int departureMonth = comboDepartureMonth.getSelectionIndex();
int departureYear = comboDepartureYear.getSelectionIndex() + 2004;
setDates(arrivalDay, arrivalMonth, arrivalYear, departureDay, departureMonth, departureYear);
}
};

comboArrivalDay.addListener(SWT.Selection, selectionListener);
comboArrivalMonth.addListener(SWT.Selection, selectionListener);
comboArrivalYear.addListener(SWT.Selection, selectionListener);
comboDepartureDay.addListener(SWT.Selection, selectionListener);
comboDepartureMonth.addListener(SWT.Selection, selectionListener);
comboDepartureYear.addListener(SWT.Selection, selectionListener);
comboRoomTypes.addListener(SWT.Selection, new Listener() {
public void handleEvent(Event event) {
((ReservationWizard)getWizard()).data.roomType = comboRoomTypes.getSelectionIndex();
}
});
setControl(composite);
}

private void setDates(int arrivalDay, int arrivalMonth, int arrivalYear, int departureDay, int departureMonth, int departureYear) {
Calendar calendar = new GregorianCalendar();
calendar.set(Calendar.DAY_OF_MONTH, arrivalDay);
calendar.set(Calendar.MONTH, arrivalMonth);
calendar.set(Calendar.YEAR, arrivalYear);
Date arrivalDate = calendar.getTime();
calendar.set(Calendar.DAY_OF_MONTH, departureDay);
calendar.set(Calendar.MONTH, departureMonth);
calendar.set(Calendar.YEAR, departureYear);
Date departureDate = calendar.getTime();

System.out.println(arrivalDate + " - " + departureDate);
if(! arrivalDate.before(departureDate)) {
setErrorMessage("The arrival date is not before the departure date");
setPageComplete(false);
}else{
setErrorMessage(null);
setPageComplete(true);
((ReservationWizard)getWizard()).data.arrivalDate = arrivalDate;
((ReservationWizard)getWizard()).data.departureDate = departureDate;
}
}
}
package swt_jface.demo12;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import org.eclipse.jface.wizard.WizardPage;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.layout.RowLayout;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Listener;
public class FrontPage extends WizardPage {

Combo comboRoomTypes;
    Combo comboArrivalYear;
    Combo comboArrivalMonth;
    Combo comboArrivalDay;
    Combo comboDepartureYear;
    Combo comboDepartureMonth;
    Combo comboDepartureDay;

FrontPage() {
        super("FrontPage");
        setTitle("Your reservation information");
        setDescription("Select the type of room and your arrival date & departure date");
    }
    public void createControl(Composite parent) {

Composite composite = new Composite(parent, SWT.NULL);
        GridLayout gridLayout = new GridLayout(2, false);
        composite.setLayout(gridLayout);
        new Label(composite, SWT.NULL).setText("Arrival date: ");
        Composite compositeArrival = new Composite(composite, SWT.NULL);
        compositeArrival.setLayout(new RowLayout());
        String[] months = new String[]{"Jan", "Feb", "Mar", "Apr", "May", "Jun",
                "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
        };
        Calendar calendar = new GregorianCalendar();
        ((ReservationWizard)getWizard()).data.arrivalDate = calendar.getTime();
        comboArrivalMonth = new Combo(compositeArrival, SWT.BORDER | SWT.READ_ONLY);
        for(int i=0; i<months.length; i++) comboArrivalMonth.add(months[i]);
        comboArrivalMonth.select(calendar.get(Calendar.MONTH));
        comboArrivalDay = new Combo(compositeArrival, SWT.BORDER | SWT.READ_ONLY);
        for(int i=0; i<31; i++) comboArrivalDay.add("" + (i+1));
        comboArrivalDay.select(calendar.get(Calendar.DAY_OF_MONTH)-1);
        comboArrivalYear = new Combo(compositeArrival, SWT.BORDER | SWT.READ_ONLY);
        for(int i=2004; i<2010; i++) comboArrivalYear.add("" + i);
        comboArrivalYear.select(calendar.get(Calendar.YEAR)-2004);
        calendar.add(Calendar.DATE, 1);
        ((ReservationWizard)getWizard()).data.departureDate = calendar.getTime();
        new Label(composite, SWT.NULL).setText("Departure date: ");
        Composite compositeDeparture = new Composite(composite, SWT.NULL | SWT.READ_ONLY);
        compositeDeparture.setLayout(new RowLayout());
        comboDepartureMonth = new Combo(compositeDeparture, SWT.NULL | SWT.READ_ONLY);
        for(int i=0; i<months.length; i++) comboDepartureMonth.add(months[i]);
        comboDepartureMonth.select(calendar.get(Calendar.MONTH));
        comboDepartureDay = new Combo(compositeDeparture, SWT.NULL | SWT.READ_ONLY);
        for(int i=0; i<31; i++) comboDepartureDay.add("" + (i+1));
        comboDepartureDay.select(calendar.get(Calendar.DAY_OF_MONTH)-1);
        comboDepartureYear = new Combo(compositeDeparture, SWT.NULL | SWT.READ_ONLY);
        for(int i=2004; i<2010; i++) comboDepartureYear.add("" + i);
        comboDepartureYear.select(calendar.get(Calendar.YEAR)-2004);
        Label line = new Label(composite, SWT.SEPARATOR | SWT.HORIZONTAL);
        GridData gridData = new GridData(GridData.FILL_HORIZONTAL);
        gridData.horizontalSpan = 2;
        line.setLayoutData(gridData);
        new Label(composite, SWT.NULL).setText("Room type: ");
        comboRoomTypes = new Combo(composite, SWT.BORDER | SWT.READ_ONLY);
        comboRoomTypes.add("Standard room (rate: $198)");
        comboRoomTypes.add("Deluxe room (rate: $298)");
        comboRoomTypes.select(0);
        Listener selectionListener = new Listener() {
            public void handleEvent(Event event) {
                int arrivalDay = comboArrivalDay.getSelectionIndex() + 1;
                int arrivalMonth = comboArrivalMonth.getSelectionIndex();
                int arrivalYear = comboArrivalYear.getSelectionIndex() + 2004;
                int departureDay = comboDepartureDay.getSelectionIndex() + 1;
                int departureMonth = comboDepartureMonth.getSelectionIndex();
                int departureYear = comboDepartureYear.getSelectionIndex() + 2004;
                setDates(arrivalDay, arrivalMonth, arrivalYear, departureDay, departureMonth, departureYear);
            }
        };

comboArrivalDay.addListener(SWT.Selection, selectionListener);
        comboArrivalMonth.addListener(SWT.Selection, selectionListener);
        comboArrivalYear.addListener(SWT.Selection, selectionListener);
        comboDepartureDay.addListener(SWT.Selection, selectionListener);
        comboDepartureMonth.addListener(SWT.Selection, selectionListener);
        comboDepartureYear.addListener(SWT.Selection, selectionListener);
        comboRoomTypes.addListener(SWT.Selection, new Listener() {
            public void handleEvent(Event event) {
                ((ReservationWizard)getWizard()).data.roomType = comboRoomTypes.getSelectionIndex();
            }
        });
        setControl(composite);
    }

private void setDates(int arrivalDay, int arrivalMonth, int arrivalYear, int departureDay, int departureMonth, int departureYear) {
        Calendar calendar = new GregorianCalendar();
        calendar.set(Calendar.DAY_OF_MONTH, arrivalDay);
        calendar.set(Calendar.MONTH, arrivalMonth);
        calendar.set(Calendar.YEAR, arrivalYear);
        Date arrivalDate = calendar.getTime();
        calendar.set(Calendar.DAY_OF_MONTH, departureDay);
        calendar.set(Calendar.MONTH, departureMonth);
        calendar.set(Calendar.YEAR, departureYear);
        Date departureDate = calendar.getTime();

System.out.println(arrivalDate + " - " + departureDate);
        if(! arrivalDate.before(departureDate)) {
            setErrorMessage("The arrival date is not before the departure date");
            setPageComplete(false);
        }else{
            setErrorMessage(null);
            setPageComplete(true);
            ((ReservationWizard)getWizard()).data.arrivalDate = arrivalDate;
            ((ReservationWizard)getWizard()).data.departureDate = departureDate;
        }
    }
}

PaymentInfoPage.java

代码如下:

package swt_jface.demo12;
import org.eclipse.jface.wizard.WizardPage;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Text;
public class PaymentInfoPage extends WizardPage {

Combo comboCreditCardTypes;
Text textCreditCardNumber;
Text textCreditCardExpiration;

public PaymentInfoPage() {
super("PaymentInfo");
setTitle("Payment information");
setDescription("Please enter your credit card details");
setPageComplete(false);
}
public void createControl(Composite parent) {

Composite composite = new Composite(parent, SWT.NULL);
composite.setLayout(new GridLayout(2, false));
new Label(composite, SWT.NULL).setText("Credit card type: ");
comboCreditCardTypes = new Combo(composite, SWT.READ_ONLY | SWT.BORDER);
comboCreditCardTypes.add("American Express");
comboCreditCardTypes.add("Master Card");
comboCreditCardTypes.add("Visa");
comboCreditCardTypes.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
new Label(composite, SWT.NULL).setText("Credit card number: ");
textCreditCardNumber = new Text(composite, SWT.SINGLE | SWT.BORDER);
textCreditCardNumber.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
new Label(composite, SWT.NULL).setText("Expiration (MM/YY)");
textCreditCardExpiration = new Text(composite, SWT.SINGLE | SWT.BORDER);
textCreditCardExpiration.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
comboCreditCardTypes.addListener(SWT.Selection, new Listener() {
public void handleEvent(Event event) {
((ReservationWizard)getWizard()).data.creditCardType = comboCreditCardTypes.getSelectionIndex();
if(((ReservationWizard)getWizard()).data.creditCardNumber != null &&
((ReservationWizard)getWizard()).data.creditCardExpiration != null)
setPageComplete(true);
else
setPageComplete(false);
}
});

textCreditCardNumber.addListener(SWT.Modify, new Listener() {
public void handleEvent(Event event) {
((ReservationWizard)getWizard()).data.creditCardNumber = textCreditCardNumber.getText();
if(((ReservationWizard)getWizard()).data.creditCardNumber != null &&
((ReservationWizard)getWizard()).data.creditCardExpiration != null)
setPageComplete(true);
else
setPageComplete(false);
}
});

textCreditCardExpiration.addListener(SWT.Modify, new Listener() {
public void handleEvent(Event event) {
String text = textCreditCardExpiration.getText().trim();
if(text.length() == 5 && text.charAt(2) == '/') {
((ReservationWizard)getWizard()).data.creditCardExpiration = text;
setErrorMessage(null);
}else{
((ReservationWizard)getWizard()).data.creditCardExpiration = null;
setErrorMessage("Invalid expiration date: " + text);
}

if(((ReservationWizard)getWizard()).data.creditCardNumber != null &&
((ReservationWizard)getWizard()).data.creditCardExpiration != null)
setPageComplete(true);
else
setPageComplete(false);
}
});
setControl(composite);
}
}
package swt_jface.demo12;
import org.eclipse.jface.wizard.WizardPage;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Text;
public class PaymentInfoPage extends WizardPage {

Combo comboCreditCardTypes;
    Text textCreditCardNumber;
    Text textCreditCardExpiration;

public PaymentInfoPage() {
        super("PaymentInfo");
        setTitle("Payment information");
        setDescription("Please enter your credit card details");
        setPageComplete(false);
    }
    public void createControl(Composite parent) {

Composite composite = new Composite(parent, SWT.NULL);
        composite.setLayout(new GridLayout(2, false));
        new Label(composite, SWT.NULL).setText("Credit card type: ");
        comboCreditCardTypes = new Combo(composite, SWT.READ_ONLY | SWT.BORDER);
        comboCreditCardTypes.add("American Express");
        comboCreditCardTypes.add("Master Card");
        comboCreditCardTypes.add("Visa");
        comboCreditCardTypes.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
        new Label(composite, SWT.NULL).setText("Credit card number: ");
        textCreditCardNumber = new Text(composite, SWT.SINGLE | SWT.BORDER);
        textCreditCardNumber.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
        new Label(composite, SWT.NULL).setText("Expiration (MM/YY)");
        textCreditCardExpiration = new Text(composite, SWT.SINGLE | SWT.BORDER);
        textCreditCardExpiration.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
        comboCreditCardTypes.addListener(SWT.Selection, new Listener() {
            public void handleEvent(Event event) {
                ((ReservationWizard)getWizard()).data.creditCardType = comboCreditCardTypes.getSelectionIndex();
                if(((ReservationWizard)getWizard()).data.creditCardNumber != null &&
                ((ReservationWizard)getWizard()).data.creditCardExpiration != null)
                    setPageComplete(true);
                else
                    setPageComplete(false);
            }
        });

textCreditCardNumber.addListener(SWT.Modify, new Listener() {
            public void handleEvent(Event event) {
                ((ReservationWizard)getWizard()).data.creditCardNumber = textCreditCardNumber.getText();
                if(((ReservationWizard)getWizard()).data.creditCardNumber != null &&
                ((ReservationWizard)getWizard()).data.creditCardExpiration != null)
                    setPageComplete(true);
                else
                    setPageComplete(false);
            }
        });

textCreditCardExpiration.addListener(SWT.Modify, new Listener() {
            public void handleEvent(Event event) {
                String text = textCreditCardExpiration.getText().trim();
                if(text.length() == 5 && text.charAt(2) == '/') {
                    ((ReservationWizard)getWizard()).data.creditCardExpiration = text;
                    setErrorMessage(null);
                }else{
                    ((ReservationWizard)getWizard()).data.creditCardExpiration = null;
                    setErrorMessage("Invalid expiration date: " + text);
                }

if(((ReservationWizard)getWizard()).data.creditCardNumber != null &&
                ((ReservationWizard)getWizard()).data.creditCardExpiration != null)
                    setPageComplete(true);
                else
                    setPageComplete(false);
            }
        });
        setControl(composite);
    }
}

ReservationWizard.java

代码如下:

package swt_jface.demo12;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.Date;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.jface.dialogs.DialogSettings;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.operation.IRunnableWithProgress;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.jface.wizard.Wizard;
class ReservationData {

Date arrivalDate;
Date departureDate;
int roomType;
String customerName;
String customerPhone;
String customerEmail;
String customerAddress;
int creditCardType;
String creditCardNumber;
String creditCardExpiration;

public String toString() {
StringBuffer sb = new StringBuffer();
sb.append("* HOTEL ROOM RESERVATION DETAILS *\n");
sb.append("Arrival date:\t" + arrivalDate.toString() + "\n");
sb.append("Departure date:\t" + departureDate.toString() + "\n");
sb.append("Room type:\t" + roomType + "\n");
sb.append("Customer name:\t" + customerName + "\n");
sb.append("Customer email:\t" + customerEmail + "\n");
sb.append("Credit card no.:\t" + creditCardNumber + "\n");

return sb.toString();
}
}
public class ReservationWizard extends Wizard {

static final String DIALOG_SETTING_FILE = "C:/userInfo.xml";

static final String KEY_CUSTOMER_NAME = "customer-name";
static final String KEY_CUSTOMER_EMAIL = "customer-email";
static final String KEY_CUSTOMER_PHONE = "customer-phone";
static final String KEY_CUSTOMER_ADDRESS = "customer-address";

ReservationData data = new ReservationData();

public ReservationWizard() {
setWindowTitle("Hotel room reservation wizard");
setNeedsProgressMonitor(true);
setDefaultPageImageDescriptor(ImageDescriptor.createFromFile(null, "C:/icons/hotel.gif"));
DialogSettings dialogSettings = new DialogSettings("userInfo");
try {
dialogSettings.load(DIALOG_SETTING_FILE);
} catch (IOException e) {
e.printStackTrace();
}
setDialogSettings(dialogSettings);
}
public void addPages() {
addPage(new FrontPage());
addPage(new CustomerInfoPage());
addPage(new PaymentInfoPage());
}
public boolean performFinish() {
if(getDialogSettings() != null) {
getDialogSettings().put(KEY_CUSTOMER_NAME, data.customerName);
getDialogSettings().put(KEY_CUSTOMER_PHONE, data.customerPhone);
getDialogSettings().put(KEY_CUSTOMER_EMAIL, data.customerEmail);
getDialogSettings().put(KEY_CUSTOMER_ADDRESS, data.customerAddress);
try {
getDialogSettings().save(DIALOG_SETTING_FILE);
} catch (IOException e1) {
e1.printStackTrace();
}
}
try {
getContainer().run(true, true, new IRunnableWithProgress() {
public void run(IProgressMonitor monitor)
throws InvocationTargetException, InterruptedException {
monitor.beginTask("Store data", 100);
monitor.worked(40);

System.out.println(data);
Thread.sleep(2000);
monitor.done();
}
});
} catch (InvocationTargetException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
return true;
}
public boolean performCancel() {
boolean ans = MessageDialog.openConfirm(getShell(), "Confirmation", "Are you sure to cancel the task?");
if(ans)
return true;
else
return false;
}
}
package swt_jface.demo12;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.Date;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.jface.dialogs.DialogSettings;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.operation.IRunnableWithProgress;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.jface.wizard.Wizard;
class ReservationData {

Date arrivalDate;
    Date departureDate;
    int roomType;
    String customerName;
    String customerPhone;
    String customerEmail;
    String customerAddress;
    int creditCardType;
    String creditCardNumber;
    String creditCardExpiration;

public String toString() {
        StringBuffer sb = new StringBuffer();
        sb.append("* HOTEL ROOM RESERVATION DETAILS *\n");
        sb.append("Arrival date:\t" + arrivalDate.toString() + "\n");
        sb.append("Departure date:\t" + departureDate.toString() + "\n");
        sb.append("Room type:\t" + roomType + "\n");
        sb.append("Customer name:\t" + customerName + "\n");
        sb.append("Customer email:\t" + customerEmail + "\n");
        sb.append("Credit card no.:\t" + creditCardNumber + "\n");

return sb.toString();
    }
}
public class ReservationWizard extends Wizard {

static final String DIALOG_SETTING_FILE = "C:/userInfo.xml";

static final String KEY_CUSTOMER_NAME = "customer-name";
    static final String KEY_CUSTOMER_EMAIL = "customer-email";
    static final String KEY_CUSTOMER_PHONE = "customer-phone";
    static final String KEY_CUSTOMER_ADDRESS = "customer-address";

ReservationData data = new ReservationData();

public ReservationWizard() {
        setWindowTitle("Hotel room reservation wizard");
        setNeedsProgressMonitor(true);
        setDefaultPageImageDescriptor(ImageDescriptor.createFromFile(null, "C:/icons/hotel.gif"));
        DialogSettings dialogSettings = new DialogSettings("userInfo");
        try {
            dialogSettings.load(DIALOG_SETTING_FILE);
        } catch (IOException e) {
            e.printStackTrace();
        }        
        setDialogSettings(dialogSettings);
    }
    public void addPages() {
        addPage(new FrontPage());
        addPage(new CustomerInfoPage());
        addPage(new PaymentInfoPage());    
    }
    public boolean performFinish() {
        if(getDialogSettings() != null) {
            getDialogSettings().put(KEY_CUSTOMER_NAME, data.customerName);
            getDialogSettings().put(KEY_CUSTOMER_PHONE, data.customerPhone);
            getDialogSettings().put(KEY_CUSTOMER_EMAIL, data.customerEmail);
            getDialogSettings().put(KEY_CUSTOMER_ADDRESS, data.customerAddress);
            try {
                getDialogSettings().save(DIALOG_SETTING_FILE);
            } catch (IOException e1) {
                e1.printStackTrace();
            }
        }
        try {
            getContainer().run(true, true, new IRunnableWithProgress() {
                public void run(IProgressMonitor monitor)
                    throws InvocationTargetException, InterruptedException {
                    monitor.beginTask("Store data", 100);
                    monitor.worked(40);

System.out.println(data);
                    Thread.sleep(2000);
                    monitor.done();
                }
            });
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return true;
    }
    public boolean performCancel() {
        boolean ans = MessageDialog.openConfirm(getShell(), "Confirmation", "Are you sure to cancel the task?");
        if(ans)
            return true;
        else
            return false;
    }    
}

userInfo.xml

代码如下:

<?xml version="1.0" encoding="UTF-8"?>
<section name="userInfo">
<item value="お早
" key="customer-address"/>
<item value="123" key="customer-phone"/>
<item value="123" key="customer-name"/>
<item value="123@sina.com" key="customer-email"/>
</section>

(0)

相关推荐

  • SWT(JFace) Wizard(Eclipse插件编程必备)

    演示代码如下: HotelReservation.java 复制代码 代码如下: package swt_jface.demo12; import org.eclipse.jface.dialogs.Dialog; import org.eclipse.jface.window.ApplicationWindow; import org.eclipse.jface.wizard.WizardDialog; import org.eclipse.swt.SWT; import org.eclips

  • 让开发自动化 用 Eclipse 插件提高代码质量

    在 让开发自动化 的本期文章中,自动化专家 Paul Duvall 将带来一些关于 Eclipse 插件的例子,您可以安装.配置和使用这些静态分析插件,以便在开发生命周期的早期预防问题. 开发软件时,我的主要目标之一是:要么防止将缺陷引入代码库,要么限制缺陷的生存期:换言之,要尽早找到缺陷.很显然,越是了解如何编写更好的代码以及如何有效测试软件,就越能及早地捕捉到缺陷.我也很想要一张能发现潜在缺陷的安全之网. 在本系列 八月份 的那期文章中,我得出了这样的结论:将检验工具集成到构建过程(例如,使

  • SWT(JFace) FTP客户端实现

    Jar包一览: org.eclipse.jface_3.4.2.M20090107-0800.jar org.eclipse.swt.win32.win32.x86_3.4.1.v3452b.jar org.eclipse.core.commands_3.4.0.I20080509-2000.jar org.eclipse.core.runtime_3.4.0.v20080512.jar org.eclipse.equinox.common_3.4.0.v20080421-2006.jar or

  • SWT(JFace) 打印功能

    演示代码如下: 复制代码 代码如下: package swt_jface.demo11; import org.eclipse.swt.graphics.GC; import org.eclipse.swt.printing.PrintDialog; import org.eclipse.swt.printing.Printer; import org.eclipse.swt.printing.PrinterData; import org.eclipse.swt.widgets.Display

  • SWT JFace 小制作 文本阅读器

    代码如下: 复制代码 代码如下: package swt_jface.demo11; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.lang.reflect.InvocationTargetException; import org.eclipse.core.runtime.IProgressMonit

  • SWT(JFace)小制作 FileBrowser文件浏览

    代码如下: 复制代码 代码如下: package swt_jface.demo6; import java.io.File; import java.util.Date; import org.eclipse.jface.resource.ImageRegistry; import org.eclipse.swt.SWT; import org.eclipse.swt.graphics.Image; import org.eclipse.swt.layout.GridData; import o

  • SWT(JFace)小制作 BugTracker

    代码如下: 实现1: BugTracker.java 复制代码 代码如下: package swt_jface.demo6; import java.io.DataInputStream; import java.io.DataOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import j

  • SWT(JFace) 文本编辑器 实现代码

    代码如下: 实现1: BasicEditor.java 复制代码 代码如下: package swt_jface.demo5; import java.io.BufferedReader; import java.io.File; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import org.eclipse.jface.resource.ImageDescriptor; i

  • SWT(JFace)体验之GridLayout布局

    GridLayout布局 GridLayout 布局的功能非常强大,也是笔者常用的一种布局方式.GridLayout是网格式布局,它把父组件分成一个表格,默认情况下每个子组件占据一个单元格的空间,每个子组件按添加到父组件的顺序排列在表格中.GridLayout提供了很多的属性,可以灵活设置网格的信息.另外,GridLayout 布局提供了GridData类,子组件可以设置相应的GridData,例如 "dogPhoto.setLayoutData(gridData)",GridData

  • SWT(JFace)体验之StyledText类

    WrapLines.java 复制代码 代码如下: package swt_jface.demo4; import org.eclipse.swt.SWT; import org.eclipse.swt.layout.GridData; import org.eclipse.swt.layout.GridLayout; import org.eclipse.swt.widgets.Display; import org.eclipse.swt.widgets.Label; import org.

随机推荐