Java 将org.eclipse.swt.layout.GridData中的文本框替换为DateChooserCombo

Java 将org.eclipse.swt.layout.GridData中的文本框替换为DateChooserCombo,java,eclipse,datagrid,swt,dynamic-controls,Java,Eclipse,Datagrid,Swt,Dynamic Controls,我使用GridData在SWT中创建了一个对话框,它具有: 标签1:组合框 标签2:TextBox1 标签3:TextBox2 复选框1标签4 复选框2标签5 根据ComoBox选择的值,我需要用DateChooserCombo替换TextBox2,或者用TextBox2替换DateChooserCombo 下面是我的类实现,方法createTextBox在加载对话框时创建TextBox2。setDatePicker调用组合框值的更改。请在下面的课堂上提供帮助,帮助我们如何实现这一目标。提前谢谢

我使用GridData在SWT中创建了一个对话框,它具有:

标签1:组合框
标签2:TextBox1
标签3:TextBox2

复选框1标签4
复选框2标签5

根据ComoBox选择的值,我需要用DateChooserCombo替换TextBox2,或者用TextBox2替换DateChooserCombo

下面是我的类实现,方法createTextBox在加载对话框时创建TextBox2。setDatePicker调用组合框值的更改。请在下面的课堂上提供帮助,帮助我们如何实现这一目标。提前谢谢

public class ConfgElementInsertDialog extends DialogEx {
    private Text typeName;
    private Text defaultValueText;
    private Combo templateType;
    private Button isKeyCheckBox;
    private Button isEditableCheckBox;
    private Button toDisplayCheckBox;
    private Button okButton;
    private boolean isInstanceConfig;
    private List<String> configElementName;
    private boolean isKey = false;
    private boolean toDisplay = false;
    private boolean isEditable = false;
    private String label;
    private String name;
    private String defaultVal;
    private boolean isEdit;
    private Text configElementNameText;
    private List<String> dateItem = Arrays.asList("effectiveDate", "endDate", "premiumEffectiveDate ","premiumEndDate");
    private boolean isDatePicker;
    private Date defaulDatetVal;
    DateChooserCombo enumDateChooser;
    Group templateTypeGroup;


    public ConfgElementInsertDialog (Shell shell) {
        super(shell);
        setShellStyle(getShellStyle() | SWT.RESIZE);
    }

    public ConfgElementInsertDialog (Shell shell, boolean isInstanceconfig,
            boolean isEdit) {
        super(shell);
        this.isInstanceConfig = isInstanceconfig;
        this.isEdit = isEdit;
        setShellStyle(getShellStyle() | SWT.RESIZE);
    }

    protected void configureShell(Shell shell) {
        super.configureShell(shell);
        shell.setSize(300, 300);
        if (!isEdit)
            shell.setText(TableTemplateConfigConstants.ADD_CONFIG_DIALOG);
        else
            shell.setText(TableTemplateConfigConstants.EDIT_CONFIG_DIALOG);

    }

    protected Control createDialogArea(Composite parent) {
        Composite child = new Composite(parent, SWT.FILL);
        child.setLayout(new GridLayout(1, false));
        child.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));

        templateTypeGroup = new Group(child, SWT.FILL);
        templateTypeGroup.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true,
                true));
        templateTypeGroup.setLayout(new GridLayout(3, false));

        GridData data = new GridData(SWT.None, SWT.FILL, false, false, 2, 1);
        data.widthHint = 100;
        if(isInstanceConfig)
            templateTypeGroup.setText(TableTemplateConfigConstants.ADD_CONFIG_GROUP_INSTANCE_TEXT);
        else
            templateTypeGroup.setText(TableTemplateConfigConstants.ADD_CONFIG_GROUP_TEMPLATE_TEXT);

        Label label1 = new Label(templateTypeGroup, SWT.NULL);
        label1.setText(TableTemplateConfigConstants.NAME_LABEL);
        if (!isEdit) {
            templateType = new Combo(templateTypeGroup, SWT.V_SCROLL);
            templateType.setLayoutData(data);

            templateType.setItems(getConfigElementName().toArray(
                    new String[getConfigElementName().size()]));
            templateType.addSelectionListener(new SelectionAdapter() {
                @Override
                public void widgetSelected(SelectionEvent e) {
                    if (templateType.getSelection() != null
                            && !templateType.getItem(templateType.getSelectionIndex()).isEmpty()) {
                        setName(templateType.getItem(templateType.getSelectionIndex()));                        
                        setDatePicker(templateType.getItem(templateType.getSelectionIndex()),templateTypeGroup);
                        okButton.setEnabled(true);
                    } else
                        okButton.setEnabled(false);

                }
            });
        } else {
            configElementNameText = new Text(templateTypeGroup, SWT.BORDER);
            configElementNameText.setLayoutData(data);
            configElementNameText.setEnabled(false);
            if(getName()!=null){
            configElementNameText.setText(getName());
            setName(getName());
            setDatePicker(getName(),templateTypeGroup);
            }
        }

        Label label = new Label(templateTypeGroup, SWT.NULL);
        label.setText(TableTemplateConfigConstants.LABEL_TEXT);

        typeName = new Text(templateTypeGroup, SWT.BORDER);
        typeName.setLayoutData(data);
        typeName.addModifyListener(new ModifyListener() {

            @Override
            public void modifyText(ModifyEvent e) {
                setLabel(typeName.getText());

            }
        });


        createTextBox(templateTypeGroup, data);

//      }
        isKeyCheckBox = new Button(templateTypeGroup, SWT.CHECK);
        isKeyCheckBox.setText(TableTemplateConfigConstants.REQUIRED_LABEL);
        isKeyCheckBox.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true,
                true, 1, 1));

        isKeyCheckBox.addSelectionListener(new SelectionAdapter() {
            @Override
            public void widgetSelected(SelectionEvent e) {
                if (isKeyCheckBox.getSelection())
                    setKey(true);
                else
                    setKey(false);
            }
        });

        isEditableCheckBox = new Button(templateTypeGroup, SWT.CHECK);
        isEditableCheckBox.setSelection(true);
        if (!isEdit)
            setEditable(true);
        if(getDefaultVal()==null || getDefaultVal().isEmpty())
            {isEditableCheckBox.setEnabled(false);

            }
        isEditableCheckBox.setText(TableTemplateConfigConstants.EDITABLE_LABEL);
        isEditableCheckBox.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true,
                true, 1, 1));
        isEditableCheckBox.addSelectionListener(new SelectionAdapter() {
            @Override
            public void widgetSelected(SelectionEvent e) {
                if (isEditableCheckBox.getSelection())
                    setEditable(true);
                else
                    setEditable(false);
            }
        });

        if (!isInstanceConfig) {
            toDisplayCheckBox = new Button(templateTypeGroup, SWT.CHECK);
            toDisplayCheckBox.setSelection(true);
            if(getDefaultVal()==null || getDefaultVal().isEmpty())
                toDisplayCheckBox.setEnabled(false);
            toDisplayCheckBox.setText(TableTemplateConfigConstants.DISPLAY_LABEL);
            toDisplayCheckBox.setLayoutData(new GridData(SWT.FILL, SWT.FILL,
                    true, true, 1, 1));
            toDisplayCheckBox.setSelection(true);
            if (!isEdit)
                setToDisplay(true);
            toDisplayCheckBox.addSelectionListener(new SelectionAdapter() {
                @Override
                public void widgetSelected(SelectionEvent e) {
                    if (toDisplayCheckBox.getSelection())
                        setToDisplay(true);
                    else
                        setToDisplay(false);
                }
            });
            if (isEdit) {
                toDisplayCheckBox.setSelection(isToDisplay());
            }
        }
        if (isEdit) {
            isKeyCheckBox.setSelection(isKey());
            if(getLabel()!=null)
            typeName.setText(getLabel());
            if(getDefaultVal()!=null)
                defaultValueText.setText(getDefaultVal());
            isEditableCheckBox.setSelection(isEditable());
        }
        return child;
    }

    protected void createButtonsForButtonBar(Composite parent) {
        super.createButtonsForButtonBar(parent);
        okButton = getButton(IDialogConstants.OK_ID);
        if(isEdit)
            okButton.setEnabled(true);
        else
        okButton.setEnabled(false);
    }

    public List<String> getConfigElementName() {
        return configElementName;
    }

    public void setConfigElementName(List<String> configElementName) {
        this.configElementName = configElementName;
    }

    public boolean isKey() {
        return isKey;
    }

    public void setKey(boolean isKey) {
        this.isKey = isKey;
    }

    public boolean isToDisplay() {
        return toDisplay;
    }

    public void setToDisplay(boolean toDisplay) {
        this.toDisplay = toDisplay;
    }

    public boolean isEditable() {
        return isEditable;
    }

    public void setEditable(boolean isEditable) {
        this.isEditable = isEditable;
    }

    public String getLabel() {
        return label;
    }

    public void setLabel(String label) {
        this.label = label;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
    public String getDefaultVal() {
        return defaultVal;
    }

    public void setDefaultVal(String defaultVal) {
        this.defaultVal = defaultVal;
    }

    public List<String> getListForDatePickere() {
        return dateItem;
    }

    public void setDatePicker(String value, Group templateTypeGroup) {
        if(dateItem.contains(value) && null != defaultValueText){
            Object grd = defaultValueText.getLayoutData();
            defaultValueText.dispose();
            defaultValueText = null;
            createDateControl(templateTypeGroup,grd);
        }else if(!dateItem.contains(value) && null != enumDateChooser){
            Object grd = enumDateChooser.getLayoutData();
            enumDateChooser.dispose();
            enumDateChooser = null;
            createTextBox(templateTypeGroup,enumDateChooser.getLayoutData());
        }
    }

    /**
     * @return the defaulDatetVal
     */
    public Date getDefaulDatetVal() {
        return defaulDatetVal;
    }

    /**
     * @param defaulDatetVal the defaulDatetVal to set
     */
    public void setDefaulDatetVal(Date defaulDatetVal) {
        this.defaulDatetVal = defaulDatetVal;
    }

    public void createTextBox(Composite templateTypeGroup, Object data){
        Label defaultLabel = new Label(templateTypeGroup, SWT.NULL);
        defaultLabel.setText(TableTemplateConfigConstants.DEFAULT_VAL_TEXT);

        defaultValueText = new Text(templateTypeGroup, SWT.BORDER);
        defaultValueText.setLayoutData(data);
        defaultValueText.addModifyListener(new ModifyListener() {

            @Override
            public void modifyText(ModifyEvent e) {
                setDefaultVal(defaultValueText.getText());
                if(!isEdit && (defaultValueText.getText()==null || defaultValueText.getText().isEmpty()))
                {
                    setToDisplay(true);
                    toDisplayCheckBox.setSelection(true);
                    toDisplayCheckBox.setEnabled(false);
                    setEditable(true);
                    isEditableCheckBox.setSelection(true);
                    isEditableCheckBox.setEnabled(false);

                }
                if(!isEdit && (defaultValueText.getText()!=null && !defaultValueText.getText().isEmpty()))
                {
                    isEditableCheckBox.setEnabled(true);
                    toDisplayCheckBox.setEnabled(true);
                }
            }
        });
    }

    public void createDateControl(Composite templateTypeGroup, Object data){
        Label defaultLbl = new Label(templateTypeGroup, SWT.NULL);
        defaultLbl.setText(TableTemplateConfigConstants.DEFAULT_VAL_TEXT);
        enumDateChooser = new DateChooserCombo(
                templateTypeGroup, SWT.BORDER | CDT.DROP_DOWN
                        | CDT.DATE_SHORT);
        enumDateChooser.setFormatter(new DateFormatter(
                ITableDataConstants.DATE_FORMAT_TABLE_VIEWER));
        enumDateChooser.setValue(new Date(System.currentTimeMillis()));
        enumDateChooser.setLayoutData((GridData)data);
        enumDateChooser.addModifyListener(new ModifyListener() {

            @Override
            public void modifyText(ModifyEvent e) {

                setDefaulDatetVal(enumDateChooser.getValue());
                if(!isEdit && (enumDateChooser.getValue()==null))
                {
                    setToDisplay(true);
                    toDisplayCheckBox.setSelection(true);
                    toDisplayCheckBox.setEnabled(false);
                    setEditable(true);
                    isEditableCheckBox.setSelection(true);
                    isEditableCheckBox.setEnabled(false);

                }
                if(!isEdit && (defaultValueText.getText()!=null && !defaultValueText.getText().isEmpty()))
                {
                    isEditableCheckBox.setEnabled(true);
                    toDisplayCheckBox.setEnabled(true);
                }
            }
        });
    }
}
public类ConfgElementInsertDialog扩展了DialogEx{
私有文本类型名;
私有文本默认值文本;
私有组合模板类型;
私有按钮isKeyCheckBox;
私有按钮IsEdit复选框;
私人按钮显示复选框;
私人按钮;
私有布尔isInstanceConfig;
私有列表configElementName;
私有布尔值isKey=false;
private boolean toDisplay=false;
私有布尔值isEditable=false;
私有字符串标签;
私有字符串名称;
私有字符串defaultVal;
私有布尔isEdit;
私有文本configElementNameText;
私有列表日期项=Arrays.asList(“生效日期”、“结束日期”、“PremiumeEffectiveDate”、“premiumEndDate”);
专用布尔值选择器;
私人日期Defauldateval;
日期选择器组合枚举日期选择器;
组模板类型组;
公共协商会议(壳牌公司){
超级(壳牌);
setShellStyle(getShellStyle()| SWT.RESIZE);
}
public ConfgElementInsertDialog(Shell,布尔值isInstanceconfig,
布尔值(isEdit){
超级(壳牌);
this.isInstanceConfig=isInstanceConfig;
this.isEdit=isEdit;
setShellStyle(getShellStyle()| SWT.RESIZE);
}
受保护的无效配置外壳(外壳){
super.configureShell(shell);
外壳。设置尺寸(300300);
如果(!isEdit)
setText(TableTemplateConfigConstants.ADD_CONFIG_对话框);
其他的
setText(TableTemplateConfigConstants.EDIT_CONFIG_对话框);
}
受保护的控件createDialogArea(复合父级){
组合子项=新组合(父项,SWT.FILL);
setLayout(新的GridLayout(1,false));
setLayoutData(新的GridData(SWT.FILL,SWT.FILL,true,true));
templateTypeGroup=新组(子组,SWT.FILL);
templateTypeGroup.setLayoutData(新的GridData(SWT.FILL,SWT.FILL,true,
是的);
setLayout(新的GridLayout(3,false));
GridData数据=新的GridData(SWT.None,SWT.FILL,false,false,2,1);
data.widthHint=100;
如果(isInstanceConfig)
templateTypeGroup.setText(TableTemplateConfigConstants.ADD_CONFIG_GROUP_INSTANCE_TEXT);
其他的
templateTypeGroup.setText(TableTemplateConfigConstants.ADD\u CONFIG\u GROUP\u TEMPLATE\u TEXT);
Label label1=新标签(templateTypeGroup,SWT.NULL);
label1.setText(TableTemplateConfigConstants.NAME\u标签);
如果(!isEdit){
templateType=新组合(templateTypeGroup,SWT.V_滚动);
templateType.setLayoutData(数据);
templateType.setItems(getConfigElementName().toArray(
新字符串[getConfigElementName().size()]);
templateType.addSelectionListener(新的SelectionAdapter(){
@凌驾
公共无效WidgeSelected(SelectionEvent e){
if(templateType.getSelection()!=null
&&!templateType.getItem(templateType.getSelectionIndex()).isEmpty()){
setName(templateType.getItem(templateType.getSelectionIndex());
setDatePicker(templateType.getItem(templateType.getSelectionIndex()),templateTypeGroup);
okButton.setEnabled(真);
}否则
okButton.setEnabled(假);
}
});
}否则{
configElementNameText=新文本(templateTypeGroup,SWT.BORDER);
configElementNameText.setLayoutData(数据);
configElementNameText.setEnabled(false);
如果(getName()!=null){
configElementNameText.setText(getName());
setName(getName());
setDatePicker(getName(),templateTypeGroup);
}
}
标签标签=新标签(templateTypeGroup,SWT.NULL);
label.setText(TableTemplateConfigConstants.label\u TEXT);
typeName=新文本(templateTypeGroup,SWT.BORDER);
typeName.setLayoutData(数据);
typeName.addModifyListener(新ModifyListener(){
@凌驾
公共无效修改文本(修改事件e){
setLabel(typeName.getText());
}
});
createTextBox(templateTypeGroup,数据);
//      }
isKeyCheckBox=新建按钮(templateTypeGroup,SWT.CHECK);
isKeyCheckBox.setText(TableTemplateConfigConstants.REQUIRED_标签);
setLayoutData(新网格数据(SWT.FILL、SWT.FILL、true、,
对,1,1);
isKeyCheckBox.addSelectionListener(新建SelectionAdapter(){
@凌驾
公共无效WidgeSelected(SelectionEvent e){
if(isKeyCheckBox.getSelection())
设置键(true);
其他的
设置键(假);
}
});
isEditableCheckBox=新建按钮(templateTypeGroup,SWT.CHECK);
isEditableCheckBox.setSelection(true);
如果(!isEdit)
可编辑设置(真);
如果(getDefaultVal()==null | | getDefaultVal().isEmpty())
{isEditableCheckBox.setEnabled(false);
}
isEditableCheckBox.setText(TableTemplateConfigConstants.EDITABLE_标签);
public void setControl(String value, Group templateTypeGroup) {
    Object grd = null;
    Point pt = null;
    Rectangle rct = null;   
    if(TableTemplateConfigConstants.dateItems.contains(value) && null != defaultValueText){
        grd = defaultValueText.getLayoutData();
        pt = defaultValueText.getLocation();
        rct = defaultValueText.getBounds();
        defaultValueText.dispose();
        cleanControls();
        createDateControl(templateTypeGroup,grd,pt,rct,value);}}


public void createDateControl(Composite templateTypeGroup, Object data, Point pt, Rectangle rectangle,String value){
    **enumDateChooser = new DateChooserCombo(
            templateTypeGroup, SWT.BORDER | CDT.DROP_DOWN
                    | CDT.DATE_SHORT);
    enumDateChooser.setFormatter(new DateFormatter(
            ITableDataConstants.DATE_FORMAT_TABLE_VIEWER));     
    enumDateChooser.setLayoutData((GridData)data);
    if(null != pt && null != rectangle){
        enumDateChooser.setLocation(pt);
        enumDateChooser.setBounds(rectangle);
    }
    enumDateChooser.setLocation(pt)**
    try {
    if(!isEdit){    


        if (TableTemplateConfigConstants.endDateLst.contains(value)) {
            enumDateChooser
                    .setValue(checkFormat(TableTemplateConfigConstants.endDate));
            DateFormat formatter = new SimpleDateFormat(TableTemplateConfigConstants.DATE_FORMAT_TABLE_VIEWER); 
            setDefaultVal(formatter.format(checkFormat(enumDateChooser.getText())));
        } else if (TableTemplateConfigConstants.effectiveDate
                .contains(value)) {
            enumDateChooser.setValue(new Date());
            DateFormat formatter = new SimpleDateFormat(TableTemplateConfigConstants.DATE_FORMAT_TABLE_VIEWER); 
            setDefaultVal(formatter.format(checkFormat(enumDateChooser.getText())));
        } else if (null != getDefaultVal() && !getDefaultVal().isEmpty()) {
            enumDateChooser.setValue(checkFormat(getDefaultVal()));
            setDefaultVal(value);
        }
    }else{
        enumDateChooser
        .setValue(checkFormat(getDefaultVal()));
    }
    } catch (ParseException e1) {
        PALogger.logError("Unable to parse date",
                PAEventCategory.OTHERS, Activator.PLUGIN_ID,e1);
    }

    setDateDecoration(false);
    enumDateChooser.addModifyListener(new ModifyListener() {

        @Override
        public void modifyText(ModifyEvent e) {             
            System.out.println(enumDateChooser.getText());
            if (null != enumDateChooser && !enumDateChooser.isDisposed()
                    ) {
                setDateDecoration(true);
            }
            setDateCheckBoxes();
        }
    });
}