📄 wordpreference.java
字号:
package edit4.preference;
import java.util.StringTokenizer;
import org.eclipse.jface.preference.PreferencePage;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.*;
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.IWorkbenchPreferencePage;
import edit4.Edit4Plugin;
public class WordPreference
extends PreferencePage
implements IWorkbenchPreferencePage {
//The list that displays the current bad words
private List badWordList;
//The newEntryText is the text where new bad words are specified
private Text newEntryText;
private static String PREFERENCE_DELIMITER = ";";
private static String VALUE_IDENTIFIER = "=";
private static String ENTRY_DELIMITER = ",";
private static String KEY_DELIMITER = ";";
//The identifiers for the preferences
public static final String BAD_WORDS_PREFERENCE = "badwords";
public static final String HIGHLIGHT_PREFERENCE = "highlight";
//The default values for the preferences
public static final String DEFAULT_BAD_WORDS = "bug;bogus;hack;";
public static final int DEFAULT_HIGHLIGHT = SWT.COLOR_BLUE;
/*
* @see PreferencePage#createContents(Composite)
*/
protected Control createContents(Composite parent) {
Composite entryTable = new Composite(parent, SWT.NULL);
//Create a data that takes up the extra space in the dialog .
GridData data = new GridData(GridData.FILL_HORIZONTAL);
data.grabExcessHorizontalSpace = true;
entryTable.setLayoutData(data);
GridLayout layout = new GridLayout();
entryTable.setLayout(layout);
//Add in a dummy label for spacing
new Label(entryTable,SWT.NONE);
badWordList = new List(entryTable, SWT.BORDER);
badWordList.setItems(getBadWordsPreference());
//Create a data that takes up the extra space in the dialog and spans both columns.
data = new GridData(GridData.FILL_BOTH);
badWordList.setLayoutData(data);
Composite buttonComposite = new Composite(entryTable,SWT.NULL);
GridLayout buttonLayout = new GridLayout();
buttonLayout.numColumns = 2;
buttonComposite.setLayout(buttonLayout);
//Create a data that takes up the extra space in the dialog and spans both columns.
data = new GridData(GridData.FILL_BOTH | GridData.VERTICAL_ALIGN_BEGINNING);
buttonComposite.setLayoutData(data);
Button addButton = new Button(buttonComposite, SWT.PUSH | SWT.CENTER);
addButton.setText("加入到列表"); //$NON-NLS-1$
addButton.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent event) {
badWordList.add(newEntryText.getText(), badWordList.getItemCount());
}
});
newEntryText = new Text(buttonComposite, SWT.BORDER);
//Create a data that takes up the extra space in the dialog .
data = new GridData(GridData.FILL_HORIZONTAL);
data.grabExcessHorizontalSpace = true;
newEntryText.setLayoutData(data);
Button removeButton = new Button(buttonComposite, SWT.PUSH | SWT.CENTER);
removeButton.setText("删除选择"); //$NON-NLS-1$
removeButton.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent event) {
badWordList.remove(badWordList.getSelectionIndex());
}
});
data = new GridData();
data.horizontalSpan = 2;
removeButton.setLayoutData(data);
return entryTable;
}
/*
* @see IWorkbenchPreferencePage#init(IWorkbench)
*/
public void init(IWorkbench workbench) {
//Initialize the preference store we wish to use
setPreferenceStore(Edit4Plugin.getDefault().getPreferenceStore());
}
/**
* Performs special processing when this page's Restore Defaults button has been pressed.
* Sets the contents of the nameEntry field to
* be the default
*/
protected void performDefaults() {
badWordList.setItems(getDefaultBadWordsPreference());
}
/**
* Method declared on IPreferencePage. Save the
* author name to the preference store.
*/
public boolean performOk() {
setBadWordsPreference(badWordList.getItems());
return super.performOk();
}
public String[] getDefaultBadWordsPreference() {
return convert(DEFAULT_BAD_WORDS
//getPreferenceStore().getDefaultString(DEFAULT_BAD_WORDS)
);
}
/**
* Return the bad words preference as an array of
* Strings.
* @return String[]
*/
public String[] getBadWordsPreference() {
return convert(getPreferenceStore().getString(BAD_WORDS_PREFERENCE));
}
/**
* Convert the supplied PREFERENCE_DELIMITER delimited
* String to a String array.
* @return String[]
*/
private String[] convert(String preferenceValue) {
StringTokenizer tokenizer =
new StringTokenizer(preferenceValue, PREFERENCE_DELIMITER);
int tokenCount = tokenizer.countTokens();
String[] elements = new String[tokenCount];
for (int i = 0; i < tokenCount; i++) {
elements[i] = tokenizer.nextToken();
}
return elements;
}
/**
* Set the bad words preference
* @param String [] elements - the Strings to be
* converted to the preference value
*/
public void setBadWordsPreference(String[] elements) {
StringBuffer buffer = new StringBuffer();
for (int i = 0; i < elements.length; i++) {
buffer.append(elements[i]);
buffer.append(PREFERENCE_DELIMITER);
}
getPreferenceStore().setValue(BAD_WORDS_PREFERENCE, buffer.toString());
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -