简体   繁体   English

在首选项页面中获取“应用”按钮+ swt eclipse

[英]get apply button in preference page + swt eclipse

I have MyPreferencePage which extends PreferencePage. 我有MyPreferencePage,它扩展了PreferencePage。 Inside the PreferencePage there is a method getApplyButton() I am overriding that method to get the apply button. 在PreferencePage内部,有一个方法getApplyButton()我正在重写该方法以获取应用按钮。

I need the apply button because there are some validators that I put on the data in the preference dialog and till the all the data is not correct I dont want the apply button to be enabled. 我需要应用按钮,因为我在首选项对话框中的数据上放置了一些验证器,直到所有数据都不正确为止,我不希望启用应用按钮。

My code 我的密码

 public class DefaultColorsPreferencePage extends PreferencePage implements IWorkbenchPreferencePage {
     @Override
     protected Control createContents(Composite parent) {

    this.container = new Composite(parent, SWT.NONE);
    this.container.setLayout(new GridLayout(1, false));
    GridData gd_area = new GridData(SWT.FILL, SWT.FILL, true, true);
    this.container.setLayoutData(gd_area);

    this.defalutColoringGroup = new Group(container, SWT.NONE);
    this.defalutColoringGroup.setLayout(new GridLayout(1, false));
    this.defalutColoringGroup.setLayoutData(gd_area);
    this.defalutColoringGroup.setText(Constants.DESCRIPTION_TEXT);

    this.defaultColoringCheckBox = new Button(defalutColoringGroup, SWT.CHECK);
    this.defaultColoringCheckBox.setText(Constants.DEFAULT_COLORING_BUTTON_TEXT);

    errorLabel = new Label(defalutColoringGroup, SWT.NONE);
    errorLabel.setText("Expression is not valid, enter a valid expression and try again!");
    errorLabel.setForeground(Display.getCurrent().getSystemColor(SWT.COLOR_RED));
    errorLabel.setVisible(false);

    this.viewer = tableviewerComposite.createTableViewer(defalutColoringGroup);
    this.viewer.setContentProvider(new ArrayContentProvider());
    try {
        contentProvider = new ContentProvider();
        this.viewer.setInput(contentProvider.getScenarios());
    } catch (ParserConfigurationException | SAXException | IOException e) {
        e.printStackTrace();
    }
    return container;
    }

     @Override
     protected Button getApplyButton() {
        super.getApplyButton();
     }
 }

//Method to create columns of the table //创建表列的方法

     private void createTableColumns(final TableViewer viewer,final Composite defalutColoringGroup) {

    TableViewerColumn scenariosColumn = createTableViewerColumn(viewer,Constants.SCENARIOS_COLUMN_NAME,Constants.SCENARIOS_COLUMN_NUMBER);
    ScenariosLabelProvider scenariosLabelProvider = new ScenariosLabelProvider();
    scenariosColumn.setLabelProvider(scenariosLabelProvider);
    scenariosColumn.setEditingSupport(new ScenariosEditingSupport(viewer));

   //more columns 
  }

//Editing Support for column //编辑列的支持

public class ScenariosEditingSupport extends EditingSupport {

private final TableViewer viewer;
private final CellEditor editor;
private final DefaultColorsPreferencePage preferencePage;

public ScenariosEditingSupport(TableViewer viewer) {
    super(viewer);
    this.viewer = viewer;
    this.editor = new TextCellEditor(viewer.getTable());
    this.preferencePage = new DefaultColorsPreferencePage();
}

@Override
protected CellEditor getCellEditor(Object element) {
    return editor;
}

@Override
protected boolean canEdit(Object element) {
    return true;
}

@Override
protected Object getValue(Object element) {
    return ((Content) element).getExpression();
}

@Override
protected void setValue(Object element, Object changedExpression) {
    String expression  = String.valueOf(changedExpression);

    if(Repository.isExpressionValid(expression)){
        ((Content) element).setExpression(expression);
        viewer.update(element, null);
    }
    else{
        preferencePage.setValid(false);
        preferencePage.setErrorMessage("Expression is not valid, enter a valid expression and try again!");
        ((Content) element).setExpression(expression);
        viewer.update(element, null);
    }
 }
}

You don't access the Apply button to enable / disable the preference page. 您无法访问“应用”按钮来启用/禁用首选项页面。 Instead call the 而是致电

setValid(false);

method of PreferencePage to disable Apply and OK. 禁用“应用”和“确定”的PreferencePage方法。

Call setValid(true) when the page is OK. 页面正常时,调用setValid(true)

You might also want to call the setErrorMessage or setMessage methods to set a message while the page is invalid. 您可能还想在页面无效时调用setErrorMessagesetMessage方法来设置消息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM