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="[email protected]" key="customer-email"/> 
</section>

     
JDBC入门教程(备java基础,oracle,mysql,javaee)
企业级分布式搜索平台Solr教学视频从入门到精通视频课程
一头扎进httpclient企业跨域技术从零基础到大神实战案例
速学领悟ELK Stack入门实战Elasticsearch深入浅出视频
一头扎进JMS之ActiveMQ消息中间件实战视频教程
Lucene教学视频从入门到项目实战(备java基础,javase。javaee)
基于SOA 思想下的Webservice多层架构实战ATM项目实战  ...2
基于SOA 思想下的WebService从入门到上手企业开发实战小案例
一头扎进基于CXF搭建WebService(RestfulWebService与基于SOAP的WebService混合方案)
Spring整合WebService框架CXF的使用学会完整Webservice服务高效发布与调用
价值上万基于SOA 思想下的WebService多层架构实战用户管理系统  ...2
全套javaSE大牛毕经之路基础+进阶+实战(价值476元)
2017最新视频全套javaSE视频教程(小白程序员入门教程)视频+课堂笔记+源码)
JAVA开发之大型互联网企业级分布式通信 RMI及JMS学习以及深入讲解 
基于SOA 思想下的WebService多层架构入门到精通(课件+源码)
JAVA开发之大型互联网企业高并发架构Tomcat服务器性能优化
Activiti工作流框架从入门到大神企业开发实例讲解与OA项目实战视频课程
JAVA架构师系列课程分布式缓存技术Redis权威指南
JAVA架构师系列之消息中间件RocketMQ入门视频课程
Java Web整合开发实战:基于Struts 2+Hibernate+Spring(课件+源码+视频)

原文地址:https://www.cnblogs.com/winifredfs/p/10164118.html

时间: 2024-08-09 08:04:12

SWT(JFace) Wizard(Eclipse插件编程必备)的相关文章

Java程序员25个必备的Eclipse插件

原文:http://www.fromdev.com/2012/01/25-best-free-eclipse-plug-ins-for-java.html "工欲善其事, 必先利器". 这里列举了25个常用的提高Java程序员开发效率的Eclipse插件.  StackOverflow上也有两个类似的"我最喜爱的eclipse插件推荐", 我觉得这篇文章差不多是基于这个推荐:http://stackoverflow.com/questions/2826/do-you

RCP开发浅谈之SWT,JFACE

RCP开发浅谈之SWT,JFACE SWT 什么是SWT? SWT全名是Standard Widget Toolkit是一个开源的GUI编程框架,我们每一个java开发者,在学习java开发的时候都会接触到awt以及swing这两个图形库,与awt,swing两个图形库不同,swt的优势体现于底层调用本地的图形库,大大提高了运行速度(损失了一定跨平台性).SWT的一个很重要的一点,一个控件并不是单独存在的,而是存在于父控件中.这样当父控件disposed后,子控件也一定很disposed了.每一

使用Eclipse&Ant编译hadoop2.x的eclipse插件

注意:以下操作基于Hadoop-1.2.1,Eclipse Luna 1.下载插件源码包 https://github.com/winghc/hadoop2x-eclipse-plugin 2.新建eclisep java工程,将压缩包中以下目录的内容复制到project中 hadoop2x-eclipse-plugin-master.zip\hadoop2x-eclipse-plugin-master\src\contrib\eclipse-plugin 3.增加依赖库 其中hadoop-2.

hadoop eclipse插件生成

hadoop eclipse插件生成 做了一年的hadoop开发.还没有自动生成过eclipse插件,一直都是在网上下载别人的用,今天有时间,就把这段遗憾补回来,自己生成一下,废话不说,開始了. 本文着重介绍Eclipse插件的生成.配置过程.常见错误的解放方法以及使用Eclipse插件測试执行wordcount演示样例. 一.环境说明 本列中的hadoop eclipse插件通过eclipse生成(未使用命令生成是由于用命令生成插件过程中发生的一些问题.不easy查看和改动,用eclipse非

Tools # 图标素材、浏览器兼容测试、 eclipse 插件、

本文主题: 图标素材.浏览器兼容测试. eclipse 插件. jquery插件.Android工具 Android 上的 10 款 Web 开发应用工具 - 开源中国社区http://www.oschina.net/news/19793/top-10-web-development-apps-for-android-devices/ 图标 http://findicons.com/icon/88640/java?id=88771 浏览器测试 http://html5test.com/ ecli

Hadoop-1.2.1 Eclipse插件编译

Eclipse开发过程连接Hadoop集群环境,需要安装Hadoop插件.Hadoop源码包中有Eclipse插件源代码,需要自己动手编译. 环境:Hadoop 1.2.1 & Eclipse Kepler & Windows 7 & JAVA 7 1.生成插件源码 解压Hadoop安装包,找到src\contrib\eclipse-plugin 2.修改配置 修改${HADOOP_HOME}/src/contrib/目录下的build-contrib.xml文件,增加两行: &l

hadoop1.2 eclipse插件编译

目录说明 在编译之前,我们需要先下载后hadoop 1.2.1的源码文件,并解压到合适的位置.目录结构如下: Eclipse: D:\eclipse Hadoop: D:\hadoop-1.2.1 Step1导入 Hadoop-eclipse 插件工程 1. 下载hadoop-1.2.1.tar.gz,并解压缩到 D盘根目录下2. 在 Eclipse 中选择 File->Import->General/Existing Projectsinto Workspace 导入Hadoop的Eclip

Eclipse插件终极攻略(一):基本概念介绍

在这个系列的第一部分里,将对Eclipse和插件的概要.插件开发的基本概念.OSGi和SWT进行简单介绍. 1.Eclipse的架构 Eclipse被作为java的IDE(集成开发环境)被广泛的应用,但是从本质上看Eclipse是一个整合了各种开发工具的平台.因此,它采用了可以自由的增加各种功能的插件架构技术.Eclipse平台的基本架构如图1-1所示. 图1-1 Eclipse的架构 在这里,在最底层位置的是作为Eclipse插件架构基干的OSGi运行时.虽然在早期的Eclipse版本中已经

用 Eclipse 插件提高代码质量

如果能在构建代码前发现代码中潜在的问题会怎么样呢?很有趣的是,Eclipse 插件中就有这样的工具,比如 JDepend 和 CheckStyle,它们能帮您在软件问题暴露前发现这些问题.在 让开发自动化 的本期文章中,自动化专家 Paul Duvall 将带来一些关于 Eclipse 插件的例子,您可以安装.配置和使用这些静态分析插件,以便在开发生命周期的早期预防问题. 关于本系列 作为一名开发人员,我们的工作就是为终端用户将过程自动化:然而,我们当中有很多人却忽视了将我们自己的开发过程自动化