Java eclipse中特定于项目的首选项页面
首先,我按照这里的说明进行操作: 但我仍然无法在Eclipse中创建特定于项目的首选项页面 此外,我需要访问工作区中所有现有项目的所有自定义首选项值。我如何访问它 我的尝试: 我在plugin.xml中添加了以下扩展:Java eclipse中特定于项目的首选项页面,java,eclipse,eclipse-plugin,Java,Eclipse,Eclipse Plugin,首先,我按照这里的说明进行操作: 但我仍然无法在Eclipse中创建特定于项目的首选项页面 此外,我需要访问工作区中所有现有项目的所有自定义首选项值。我如何访问它 我的尝试: 我在plugin.xml中添加了以下扩展: <extension point="org.eclipse.ui.propertyPages"> <page class="com.test.PropertiesPage" id="com.test.projectPropertiesP
<extension point="org.eclipse.ui.propertyPages">
<page class="com.test.PropertiesPage"
id="com.test.projectPropertiesPage" name="%appHandle">
<enabledWhen>
<and>
<instanceof
value="org.eclipse.core.resources.IFile">
</instanceof>
<adapt type="org.eclipse.core.resources.IResource" >
<test property="org.eclipse.core.resources.projectNature"
value="com.test.runtime.nature" />
</adapt>
</and>
</enabledWhen>
</page>
</extension>
)。但首选项页面未加载或未显示任何内容。在这种情况下我能做什么
public abstract class PropertiesPage extends PropertyPage {
/*** Name of resource property for the selection of workbench or project settings ***/
public static final String USEPROJECTSETTINGS = "Use Project Settings"; //$NON-NLS-1$
private static final String FALSE = "false"; //$NON-NLS-1$
private static final String TRUE = "true"; //$NON-NLS-1$
// Additional buttons for property pages
private Button useWorkspaceSettingsButton,
useProjectSettingsButton,
configureButton;
// Overlay preference store for property pages
private PropertyStore propertyStore;
// The image descriptor of this pages title image
private ImageDescriptor image;
// Cache for page id
private String pageId;
// Container for subclass controls
private Composite contents;
/**
* Constructor
*/
public PropertiesPage() {
super();
}
/**
* Constructor
* @param title - title string
*/
public PropertiesPage(String title) {
super();
setTitle(title);
}
/**
* Constructor
* @param title - title string
* @param image - title image
*/
public PropertiesPage(String title, ImageDescriptor image) {
super();
setTitle(title);
setImageDescriptor(image);
this.image = image;
}
/**
* Returns the id of the current preference page as defined in plugin.xml
* Subclasses must implement.
*
* @return - the qualifier
*/
protected abstract String getPageId();
/**
* Returns true if this instance represents a property page
* @return - true for property pages, false for preference pages
*/
public boolean isPropertyPage() {
return getElement() != null;
}
/**
* We need to implement createContents method. In case of property pages we insert two radio buttons
* and a push button at the top of the page. Below this group we create a new composite for the contents
* created by subclasses.
*
* @see org.eclipse.jface.preference.PreferencePage#createContents(org.eclipse.swt.widgets.Composite)
*/
protected Control createContents(Composite parent) {
if (isPropertyPage())
createSelectionGroup(parent);
contents = new Composite(parent, SWT.NONE);
GridLayout layout = new GridLayout();
layout.marginHeight = 0;
layout.marginWidth = 0;
contents.setLayout(layout);
contents.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
return contents;
}
/**
* Creates and initializes a selection group with two choice buttons and one push button.
* @param parent - the parent composite
*/
private void createSelectionGroup(Composite parent) {
Composite comp = new Composite(parent, SWT.NONE);
GridLayout layout = new GridLayout(2, false);
layout.marginHeight = 0;
layout.marginWidth = 0;
comp.setLayout(layout);
comp.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
Composite radioGroup = new Composite(comp, SWT.NONE);
radioGroup.setLayout(new GridLayout());
radioGroup.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
useWorkspaceSettingsButton = createRadioButton(radioGroup, Messages.getString("Use workspace setting")); //$NON-NLS-1$
useProjectSettingsButton = createRadioButton(radioGroup, Messages.getString("Use project setting")); //$NON-NLS-1$
configureButton = new Button(comp, SWT.PUSH);
configureButton.setText(Messages.getString("Configure workspace setting")); //$NON-NLS-1$
configureButton.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
configureWorkspaceSettings();
}
});
// Set workspace/project radio buttons
try {
String use =
((IResource) getElement()).getPersistentProperty(
new QualifiedName(pageId, USEPROJECTSETTINGS));
if (TRUE.equals(use)) {
useProjectSettingsButton.setSelection(true);
configureButton.setEnabled(false);
} else
useWorkspaceSettingsButton.setSelection(true);
} catch (CoreException e) {
useWorkspaceSettingsButton.setSelection(true);
}
}
/**
* Convenience method creating a radio button
* @param parent - the parent composite
* @param label - the button label
* @return - the new button
*/
private Button createRadioButton(Composite parent, String label) {
final Button button = new Button(parent, SWT.RADIO);
button.setText(label);
button.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
configureButton.setEnabled(
button == useWorkspaceSettingsButton);
setControlsEnabled();
}
});
return button;
}
/**
* In case of property pages we create a new PropertyStore as local overlay store.
* After all controls have been create, we enable/disable these controls
*
* @see org.eclipse.jface.preference.PreferencePage#createControl()
*/
public void createControl(Composite parent) {
// Special treatment for property pages
if (isPropertyPage()) {
// Cache the page id
pageId = getPageId();
// Create an overlay preference store and fill it with properties
propertyStore =
new PropertyStore(
(IResource) getElement(),
super.getPreferenceStore(),
pageId);
// Set overlay store as current preference store
}
super.createControl(parent);
// Update enablement of all subclass controls
if (isPropertyPage())
setControlsEnabled();
}
/*
* Returns in case of property pages the overlay store - otherwise the standard preference store
* @see org.eclipse.jface.preference.PreferencePage#getPreferenceStore()
*/
public IPreferenceStore getPreferenceStore() {
if (isPropertyPage())
return propertyStore;
return super.getPreferenceStore();
}
/**
* Enables or disables the controls of this page
*/
private void setControlsEnabled() {
boolean enabled = useProjectSettingsButton.getSelection();
setControlsEnabled(enabled);
}
/**
* Enables or disables the controls of this page
* Subclasses may override.
*
* @param enabled - true if controls shall be enabled
*/
protected void setControlsEnabled(boolean enabled) {
setControlsEnabled(contents, enabled);
}
/**
* Enables or disables a tree of controls starting at the specified root.
* We spare tabbed notebooks and pagebooks to allow for user navigation.
*
* @param root - the root composite
* @param enabled - true if controls shall be enabled
*/
private void setControlsEnabled(Composite root, boolean enabled) {
Control[] children = root.getChildren();
for (int i = 0; i < children.length; i++) {
Control child = children[i];
if (!(child instanceof CTabFolder) && !(child instanceof TabFolder) && !(child instanceof PageBook))
child.setEnabled(enabled);
if (child instanceof Composite)
setControlsEnabled((Composite) child, enabled);
}
}
/**
* We override the performOk method. In case of property pages
* we save the state of the radio buttons.
*
* @see org.eclipse.jface.preference.IPreferencePage#performOk()
*/
public boolean performOk() {
boolean result = super.performOk();
if (result && isPropertyPage()) {
// Save state of radiobuttons in project properties
IResource resource = (IResource) getElement();
try {
String value =
(useProjectSettingsButton.getSelection()) ? TRUE : FALSE;
resource.setPersistentProperty(
new QualifiedName(pageId, USEPROJECTSETTINGS),
value);
} catch (CoreException e) {
}
}
return result;
}
/**
* We override the performDefaults method. In case of property pages we
* switch back to the workspace settings and disable the page controls.
*
* @see org.eclipse.jface.preference.PreferencePage#performDefaults()
*/
protected void performDefaults() {
if (isPropertyPage()) {
useWorkspaceSettingsButton.setSelection(true);
useProjectSettingsButton.setSelection(false);
configureButton.setEnabled(true);
setControlsEnabled();
}
super.performDefaults();
}
/**
* Creates a new preferences page and opens it
*/
protected void configureWorkspaceSettings() {
try {
// create a new instance of the current class
IPreferencePage page =
(IPreferencePage) this.getClass().newInstance();
page.setTitle(getTitle());
page.setImageDescriptor(image);
// and show it
showPreferencePage(pageId, page);
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
/**
* Show a single preference pages
* @param id - the preference page identification
* @param page - the preference page
*/
protected void showPreferencePage(String id, IPreferencePage page) {
final IPreferenceNode targetNode = new PreferenceNode(id, page);
PreferenceManager manager = new PreferenceManager();
manager.addToRoot(targetNode);
final PreferenceDialog dialog =
new PreferenceDialog(getControl().getShell(), manager);
BusyIndicator.showWhile(getControl().getDisplay(), new Runnable() {
public void run() {
dialog.create();
dialog.setMessage(targetNode.getLabelText());
dialog.open();
}
});
}
}
公共抽象类PropertiesPage扩展了PropertyPage{
/***用于选择工作台或项目设置的资源属性的名称***/
公共静态最终字符串USEPROJECTSETTINGS=“使用项目设置”//$NON-NLS-1$
私有静态最终字符串FALSE=“FALSE”/$NON-NLS-1$
私有静态最终字符串TRUE=“TRUE”/$NON-NLS-1$
//属性页的其他按钮
专用按钮使用工作空间设置按钮,
使用项目设置按钮,
配置按钮;
//属性页的覆盖首选项存储
私人地产店地产店;
//此页面标题图像的图像描述符
私有图像描述符图像;
//页面id的缓存
私有字符串pageId;
//子类控件的容器
私有复合内容;
/**
*建造师
*/
公共财产页(){
超级();
}
/**
*建造师
*@param title-标题字符串
*/
公共属性页(字符串标题){
超级();
片名(片名);
}
/**
*建造师
*@param title-标题字符串
*@param image-标题图像
*/
公共属性页(字符串标题、图像描述符图像){
超级();
片名(片名);
setImageDescriptor(图像);
这个图像=图像;
}
/**
*返回plugin.xml中定义的当前首选项页面的id
*子类必须实现。
*
*@return-限定符
*/
受保护的抽象字符串getPageId();
/**
*如果此实例表示属性页,则返回true
*@return-属性页为true,首选项页为false
*/
公共布尔值isPropertyPage(){
返回getElement()!=null;
}
/**
*我们需要实现createContents方法。对于属性页,我们插入两个单选按钮
*页面顶部有一个按钮。在这个组下面,我们为内容创建一个新的组合
*由子类创建。
*
*@see org.eclipse.jface.preference.PreferencePage#createContents(org.eclipse.swt.widgets.Composite)
*/
受保护的控件createContents(复合父级){
如果(isPropertyPage())
createSelectionGroup(父级);
内容=新的复合材料(母体,SWT.NONE);
GridLayout=新的GridLayout();
layout.marginHeight=0;
layout.marginWidth=0;
contents.setLayout(布局);
setLayoutData(新的GridData(GridData.FILL_HORIZONTAL));
返回内容;
}
/**
*使用两个选择按钮和一个按钮创建并初始化选择组。
*@param parent-父组合
*/
私有void createSelectionGroup(复合父级){
复合复合材料=新复合材料(母体,SWT.NONE);
GridLayout=新的GridLayout(2,false);
layout.marginHeight=0;
layout.marginWidth=0;
组件设置布局(布局);
组件setLayoutData(新GridData(GridData.FILL_HORIZONTAL));
复合射线组=新复合材料(复合材料,SWT.无);
setLayout(新的GridLayout());
radioGroup.setLayoutData(新的GridData(GridData.FILL_HORIZONTAL));
useWorkspaceSettingsButton=createRadioButton(radioGroup,Messages.getString(“使用工作区设置”);//$NON-NLS-1$
useProjectSettingsButton=createRadioButton(radioGroup,Messages.getString(“使用项目设置”);//$NON-NLS-1$
配置按钮=新按钮(组件、开关按钮);
configureButton.setText(Messages.getString(“配置工作区设置”);//$NON-NLS-1$
configureButton.addSelectionListener(新建SelectionAdapter(){
公共无效WidgeSelected(SelectionEvent e){
配置工作空间设置();
}
});
//设置工作区/项目单选按钮
试一试{
字符串使用=
((IResource)getElement()).getPersistentProperty(
新的限定名称(pageId,USEPROJECTSETTINGS));
if(TRUE.equals(use)){
使用ProjectSettingsButton.setSelection(true);
configureButton.setEnabled(错误);
}否则
使用WorkspaceSettingsButton.setSelection(true);
}捕获(COREE){
使用WorkspaceSettingsButton.setSelection(true);
}
}
/**
*创建单选按钮的简便方法
*@param parent-父组合
*@param标签-按钮标签
*@return-新按钮
*/
专用按钮createRadioButton(复合父级,字符串标签){
最终按钮=新按钮(父按钮、SWT.收音机);
按钮.setText(标签);
addSelectionListener(新建SelectionAdapter(){
公共无效WidgeSelected(SelectionEvent e){
configureButton.setEnabled(
按钮==使用工作空间设置按钮);
setControlsEnabled();
}
});
返回按钮;
}
/**
*对于属性页,我们创建一个新的PropertyStore作为本地覆盖存储。
*创建所有控件后,我们将启用/禁用这些控件
*
*@see org.eclipse.jface.preference.PreferencePage#createControl()
*/
公共void createControl(复合父级){
//属性页的特殊处理
如果(isPropertyPage()){