이미지출처 : www.mobilefish.com
EclipseRCP 환경설정 페이지 사용하기(EclipseRCP Preference Page)
1. add (org.eclipse.ui.preferencePages)extension to plugin.xml
- plugin.xml파일에 환경설정 확장점을 추가해준다.
2. create new class that implements IWorkbenchPreferencePage
- IWorkbenchPreferencePage를 구현한 클래스를 생성한다.
3. If you need to adjust, you can make Preference constants and Preference initializer classes.
- 필요하다면, 환경설정 상수나, 초기화 클래스관련 클래스를 생성할 수 있습니다.
범위 : 기본, 환경, 인스턴스
scopes : default , configuration , instance
Preference.java
Code:
public class PreferencePage | |
extends FieldEditorPreferencePage | |
implements IWorkbenchPreferencePage { | |
| |
public PreferencePage() { | |
super(GRID); | |
setPreferenceStore(Plugin.getDefault().getPreferenceStore()); | |
setDescription("Preferences"); | |
} | |
| |
public void createFieldEditors() { | |
String[] filterExtension = { "*.file extension" }; | |
addField(new DirectoryFieldEditor(PreferenceConstants.DIR_PATH, "&Directory preference:", getFieldEditorParent())); | |
FileFieldEditor filePathPrefEditor = new FileFieldEditor(PreferenceConstants.FILE_PATH, | |
"&File Path preference:", getFieldEditorParent()); | |
filePathPrefEditor.setFileExtensions(filterExtension); | |
addField(filePathPrefEditor); | |
addField( | |
new BooleanFieldEditor( | |
PreferenceConstants.P_BOOLEAN, | |
"&An example of a boolean preference", | |
getFieldEditorParent())); | |
| |
addField(new RadioGroupFieldEditor( | |
PreferenceConstants.P_CHOICE, | |
"An example of a multiple-choice preference", | |
1, | |
new String[][] { { "&Choice 1", "choice1" }, { | |
"C&hoice 2", "choice2" } | |
}, getFieldEditorParent())); | |
addField( | |
new StringFieldEditor(PreferenceConstants.P_STRING, "A &text preference:", getFieldEditorParent())); | |
} | |
| |
/* (non-Javadoc) | |
* @see org.eclipse.ui.IWorkbenchPreferencePage#init(org.eclipse.ui.IWorkbench) | |
*/ | |
public void init(IWorkbench workbench) { | |
} | |
| |
} |
PreferenceConstants.java
Code:
public class PreferenceConstants { |
public static final String DIR_PATH = "dirPath"; |
public static final String FILE_PATH = "filePath"; |
|
public static final String P_PATH = "pathPreference"; |
public static final String P_BOOLEAN = "booleanPreference"; |
public static final String P_CHOICE = "choicePreference"; |
public static final String P_STRING = "stringPreference"; |
} |
PreferenceInitializer.java
Code:
public class PreferenceInitializer extends AbstractPreferenceInitializer { |
public void initializeDefaultPreferences() { |
IPreferenceStore store = Plugin.getDefault().getPreferenceStore(); |
store.setDefault(PreferenceConstants.DIR_PATH,"C:\\Program Files"); |
} |
} |
RadioGroupFieldEditor 사용팁
만약 아직 지원하지 않는 값을 사용자가 클릭했을 경우,
다음과 같이 propertyChange를 overriding하여 필드값을 Initializer에서 설정해준 값으로 불러와 변경시키는 방법을 사용 하면된다.
Code:
public void propertyChange(PropertyChangeEvent event) { | |
super.propertyChange(event); | |
if(event.getProperty().equals(FieldEditor.VALUE)) { | |
if(event.getSource() instanceof RadioGroupFieldEditor) { | |
if(!event.getNewValue().equals("available value")){ | |
Dialog.showWarningMessageDialog(getFieldEditorParent(), "Not available", "Sorry, Now available only for available value."); | |
RadioGroupFieldEditor radioGroupEditor = (RadioGroupFieldEditor) event.getSource(); | |
radioGroupEditor .loadDefault(); | |
} | |
} | |
} | |
} |
by 月風