?? prefpageone.java
字號:
package ejfaceintegrationsample;
import java.io.IOException;
import org.eclipse.jface.preference.*;
import org.eclipse.swt.*;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.layout.*;
import org.eclipse.swt.widgets.*;
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.IWorkbenchPreferencePage;
import ejfaceintegrationsample.views.*;
public class PrefPageOne extends PreferencePage implements IWorkbenchPreferencePage{
//Names for preferences
private static final String SERVERADDR = "SERVERADDR";
private static final String USERNAME = "USERNAME";
private static final String USERPASSWORD = "USERPASSWORD";
//Text fields for user to enter preferences
private Text fieldOne;
private Text fieldTwo;
private Text fieldThree;
//Declare required component
Label label1, label2, label3;
Composite composite;
Button defaultbutton;
PreferenceStore preferenceStoreNBA,preferenceStoreMLB;
//creates the controls for this page
protected Control createContents(Composite parent){
composite = new Composite(parent, SWT.NONE);
composite.setLayout(new GridLayout(2,false));
preferenceStoreNBA = new PreferenceStore("nba"); //$NON-NLS-1$
try {
preferenceStoreNBA.load();
} catch (IOException e) {}
preferenceStoreMLB = new PreferenceStore("mlb"); //$NON-NLS-1$
try {
preferenceStoreMLB.load();
} catch (IOException e) {}
//Create three text fields
label1 = new Label(composite, SWT.LEFT);
label1.setText("Server address:");
label1.setBackground(new Color(composite.getDisplay(),0x66, 0xCC, 0xFF));
fieldOne = new Text(composite, SWT.SINGLE | SWT.BORDER);
fieldOne.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
label2 = new Label(composite, SWT.LEFT);
label2.setText("Username:");
fieldTwo = new Text(composite, SWT.SINGLE | SWT.BORDER);
fieldTwo.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
label3 = new Label(composite, SWT.LEFT);
label3.setText("Password:");
fieldThree = new Text(composite, SWT.SINGLE | SWT.BORDER | SWT.PASSWORD);
fieldThree.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
//Set the default value of these three text fields
SetDefaultValue();
//Get the value from preferenceStore and show in Text field
if(SampleView.NBAorMLB==1){
fieldOne .setText(preferenceStoreNBA.getString(SERVERADDR));
fieldTwo .setText(preferenceStoreNBA.getString(USERNAME));
fieldThree.setText(preferenceStoreNBA.getString(USERPASSWORD));
}else{
fieldOne .setText(preferenceStoreMLB.getString(SERVERADDR));
fieldTwo .setText(preferenceStoreMLB.getString(USERNAME));
fieldThree.setText(preferenceStoreMLB.getString(USERPASSWORD));
}
//Set the Restore button and perform restore to default value
//Once this button is clicked
defaultbutton = new Button(composite,SWT.PUSH | SWT.LEFT);
defaultbutton.setText("Restore to Default value");
defaultbutton.addSelectionListener(new SelectionListener(){
public void widgetSelected(SelectionEvent e) {;
performDefaults();
}
public void widgetDefaultSelected(SelectionEvent e) {
}});
return composite;
}
/**
* Called when user clicks Restore button
*/
protected void performDefaults(){
//Reset the fields to the defaults
if(SampleView.NBAorMLB==1){
fieldOne .setText(preferenceStoreNBA.getDefaultString(SERVERADDR));
fieldTwo .setText(preferenceStoreNBA.getDefaultString(USERNAME));
fieldThree.setText(preferenceStoreNBA.getDefaultString(USERPASSWORD));
}else{
fieldOne .setText(preferenceStoreMLB.getDefaultString(SERVERADDR));
fieldTwo .setText(preferenceStoreMLB.getDefaultString(USERNAME));
fieldThree.setText(preferenceStoreMLB.getDefaultString(USERPASSWORD));
}
}
/**
* Called when user clicks Apply or OK
*/
public boolean performOk(){
//Set the values from the fields
if(SampleView.NBAorMLB==1){
if(fieldOne != null && fieldOne .getCharCount() != 0) preferenceStoreNBA.setValue(SERVERADDR, fieldOne .getText());
if(fieldTwo != null && fieldTwo .getCharCount() != 0) preferenceStoreNBA.setValue(USERNAME, fieldTwo .getText());
if(fieldThree != null && fieldThree.getCharCount() != 0) preferenceStoreNBA.setValue(USERPASSWORD, fieldThree.getText());
try {
preferenceStoreNBA.save();
} catch (IOException e) {
return false;
}
}else{
if(fieldOne != null && fieldOne .getCharCount() != 0) preferenceStoreMLB.setValue(SERVERADDR, fieldOne .getText());
if(fieldTwo != null && fieldTwo .getCharCount() != 0) preferenceStoreMLB.setValue(USERNAME, fieldTwo .getText());
if(fieldThree != null && fieldThree.getCharCount() != 0) preferenceStoreMLB.setValue(USERPASSWORD, fieldThree.getText());
try {
preferenceStoreMLB.save();
} catch (IOException e) {
return false;
}
}
//Retrun true to allow dialog to close
return true;
}
/**
* Called when initially setting default value of each fields
*/
private void SetDefaultValue() {
if (fieldOne != null && fieldOne.getCharCount() == 0) {
if(SampleView.NBAorMLB ==1){
preferenceStoreNBA.setDefault(SERVERADDR, "www.nba.com");
}else{
preferenceStoreMLB.setDefault(SERVERADDR, "www.mlb.com");
}
}
if (fieldTwo != null && fieldTwo.getCharCount() == 0) {
if(SampleView.NBAorMLB ==1){
preferenceStoreNBA.setDefault(USERNAME, "NBA Admin");
}else{
preferenceStoreMLB.setDefault(USERNAME, "MLB Admin");
}
}
if (fieldThree != null && fieldThree.getCharCount() == 0) {
if(SampleView.NBAorMLB ==1){
preferenceStoreNBA.setDefault(USERPASSWORD, "nbaadmin");
}else{
preferenceStoreMLB.setDefault(USERPASSWORD, "mlbadmin");
}
}
}
/**
* Called when user clicks Cancel
*/
public boolean performCancel() {
composite.dispose();
this.dispose();
return true;
}
public void init(IWorkbench arg0) {
// TODO Auto-generated method stub
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -