用org.eclipse.swt.layout.GridData中的DateChooserCombo替换文本框

时间:2014-05-16 16:36:43

标签: java eclipse datagrid swt dynamic-controls

我使用GridData在SWT中创建了对话框:

Label1:ComboBox
Label2:TextBox1
Label3:TextBox2

checkbox1 Label4
checkbox2 Label5

根据ComoBox选择的值,我需要将TextBox2替换为DateChooserCombo或将DateChooserCombo替换为TextBox2。

下面是我的类实现,其方法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);
                }
            }
        });
    }
}

1 个答案:

答案 0 :(得分:0)

对组合框的选择更改事件实现的方法,即setControl(templateType.getItem(templateType.getSelectionIndex()),templateTypeGroup);

它在处理旧控件之前采用布局和显示细节,并通过传递旧控件的细节来创建新控件。

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();
        }
    });
}