自定义SWT控件四之其它下拉框

Posted sandyflower

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了自定义SWT控件四之其它下拉框相关的知识,希望对你有一定的参考价值。

4、其它下拉框

4.1 添加联动二级多选择框(有添加按钮和删除按钮)

package com.view.control.select;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.CLabel;
import org.eclipse.swt.custom.ScrolledComposite;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;

import com.global.constant.Constants;
import com.util.CollectionUtil;
import com.util.FileUtil;
import com.view.control.DefinedControl;
import com.view.control.select.DefinedCommonMultiSelect.DefinedCommonMultiSelectEvent;
import com.view.control.select.DropDownBox.Data;
import com.view.swt.SWTResourceManager;
import com.view.util.ImageUtil;

/**
 * <p>添加联动二级多选择框(有添加按钮和删除按钮)</p>
 * <p>
 *   第一级为单选框(目前不带搜索功能)<br>
 *   第二级为不带搜索功能的多选项框
 * </p>
 * @version V1.0
 */
public class DefinedFormAddLinkageMultiSelect extends DefinedControl {
    /****内容容器*****/
    private Composite addComposite;
    /****显示名称控件****/
    private CLabel name;
    /****选中内容显示的文本区域 + 下拉图标 总宽度****/
    private int chooseWidth = 323;
    private int chooseHeight = 32;
    /****内容框中的每项的高度*****/
    private int chooseItemHeight = 24;
    /*****显示名称**********/
    private String nameText;
    /*****设置显示名称控件的宽度*****/
    private int nameWidth = 100;
    private List<DropDownBox.Data> selectedList = new ArrayList<DropDownBox.Data>();
    /****每个下拉框的中每行的宽度****/
    private int comboRowWidth;
    private int comboRowHeight = 0;
    /*****主要是数据交互使用*******/
    private DefinedCommonMultiSelectEvent itemHandlerListener;
    private DefinedFormAddLinkageMultiSelectEvent linkageMultiSelectEvent;
    private Listener firsetSelectListener;
    private Label addImg ;
    private List<DropDownBox.Data> firstDefaultSelectValueList;
    private List<Composite> addRowCompositeList = new ArrayList<>();
    /*****点击第二级下拉中的小删除按钮发生的额外事件*****/
    private Listener deleteBtnListener;

    public DefinedFormAddLinkageMultiSelect(Composite parent,String nameText,List<DropDownBox.Data> firstSelectedList,int chooseWidth,DefinedFormAddLinkageMultiSelectEvent linkageMultiSelectEvent) {
        super(parent);
       this.nameText = nameText;
       this.selectedList = firstSelectedList;
       this.comboRowWidth = chooseWidth/2;
       this.chooseWidth = chooseWidth;
       this.linkageMultiSelectEvent = linkageMultiSelectEvent;
    }
    
    public DefinedFormAddLinkageMultiSelect(Composite parent,String nameText,List<DropDownBox.Data> firstSelectedList,int nameTextWidth,int chooseWidth,int chooseItemHeight,int chooseHeight,DefinedFormAddLinkageMultiSelectEvent linkageMultiSelectEvent) {
       this(parent,nameText,firstSelectedList,chooseWidth,linkageMultiSelectEvent);
       this.nameWidth = nameTextWidth;
       this.chooseItemHeight = chooseItemHeight;
       this.chooseHeight = chooseHeight;
    }

    @Override
    public void paint() {
        generateAddComposite(this.parent);
    }
    
    private void generateAddComposite(Composite contentComposite){
        /****显示添加一行(第一行24像素高,后面每一行 chooseHeight高)****/
        addComposite = new Composite(contentComposite,SWT.NONE);
        GridData gd_addComposite = new GridData(SWT.FILL, SWT.CENTER, true, true, 1, 1);
        addComposite.setLayoutData(gd_addComposite);
        GridLayout gl_addComposite = new GridLayout(2,false);
        gl_addComposite.horizontalSpacing = 5;
        gl_addComposite.verticalSpacing = 15;
        gl_addComposite.marginHeight = 0;
        gl_addComposite.marginWidth = 0;
        addComposite.setLayout(gl_addComposite);
        addComposite.setBackground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
        
        name = new CLabel(addComposite,SWT.NONE);
        GridData gd_addLabel = new GridData(SWT.RIGHT, SWT.FILL, false, false, 1, 1);
        gd_addLabel.widthHint = this.nameWidth;
        gd_addLabel.heightHint = 20;
        name.setLayoutData(gd_addLabel);
        name.setAlignment(SWT.RIGHT);
        name.setText(this.nameText);
        name.setBackground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
        name.setForeground(SWTResourceManager.getColor(51,51,51));
        
        addImg = new Label(addComposite,SWT.NONE);
        GridData gd_addImg = new GridData(SWT.LEFT, SWT.FILL, false, false, 1, 1);
        gd_addImg.widthHint = 24;
        gd_addImg.heightHint = 24;
        addImg.setLayoutData(gd_addImg);
        addImg.setImage(ImageUtil.getImage(FileUtil.loadResourceFileAsStream(Constants.ADD_ICON)));
        addImg.setBackground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
        addImg.setCursor(SWTResourceManager.getCursor(SWT.CURSOR_HAND));
        addImg.addListener(SWT.MouseDown, new Listener(){
            @Override
            public void handleEvent(Event event) {
                generateAddRowComposite(addComposite,null);
                reLayout();
            }
        });
        
        if(CollectionUtil.isNotEmpty(firstDefaultSelectValueList)){
            for(DropDownBox.Data firstDefaultSelectValue:firstDefaultSelectValueList){
                generateAddRowComposite(addComposite,firstDefaultSelectValue);
            }
        }
        reLayout();
    }
    
    private void reLayout(){
        Composite contentComposite = addComposite;
        while(contentComposite != this.parent){
            contentComposite.layout(true);
            contentComposite = contentComposite.getParent();
        }
        contentComposite.layout(true);
        Composite parentComposite = contentComposite.getParent();
        while(!(parentComposite instanceof ScrolledComposite) && !(parentComposite instanceof Shell)){
            parentComposite.layout(true);
            contentComposite = parentComposite;
            parentComposite = parentComposite.getParent();
        }
        if(parentComposite instanceof ScrolledComposite){
            ((ScrolledComposite)parentComposite).setMinSize(contentComposite.computeSize(SWT.DEFAULT, SWT.DEFAULT));
        }
    }
    
    
    /**
     * 
     * @param contentComposite   为addComposite(2列)
     */
    private void generateAddRowComposite(Composite contentComposite,DropDownBox.Data defaultValue){
        Composite addRowComposite = new Composite(contentComposite,SWT.NONE);
        GridData gd_addRowComposite = new GridData(SWT.FILL, SWT.FILL, false, false, 2, 1);
        gd_addRowComposite.heightHint = this.chooseHeight;
        addRowComposite.setLayoutData(gd_addRowComposite);
        
        GridLayout gl_addRowComposite = new GridLayout(5,false);
        gl_addRowComposite.horizontalSpacing = 5;
        gl_addRowComposite.verticalSpacing = 0;
        gl_addRowComposite.marginHeight = 0;
        gl_addRowComposite.marginWidth = 0;
        addRowComposite.setLayout(gl_addRowComposite);
        addRowComposite.setBackground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
        
        CLabel emptyLabel = new CLabel(addRowComposite,SWT.NONE);
        GridData gd_addLabel = new GridData(SWT.RIGHT, SWT.FILL, false, true, 1, 1);
        gd_addLabel.widthHint = this.nameWidth;
        emptyLabel.setLayoutData(gd_addLabel);
        emptyLabel.setBackground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
        
        DefinedSingleSelect firstSelect = new DefinedSingleSelect(addRowComposite,this.selectedList,this.chooseWidth /2,this.chooseHeight);
        List<DropDownBox.Data> secondDataList = new ArrayList<>();
        List<DropDownBox.Data> secondDefaultList = new ArrayList<DropDownBox.Data>();
        if(null != defaultValue){
            secondDataList = linkageMultiSelectEvent.getSecondData(defaultValue);
            secondDefaultList = linkageMultiSelectEvent.getSecondDefaultValueList(defaultValue);
        }
        DefinedMultiSelect multiSelect = new DefinedMultiSelect(addRowComposite,secondDataList,this.chooseWidth /2,this.chooseItemHeight );
        if(null != defaultValue){
            firstSelect.setDefaultValue(defaultValue);
        }
        if(CollectionUtil.isNotEmpty(secondDefaultList)){
            multiSelect.setDefaultValueList(secondDefaultList);
        }
        if(this.comboRowWidth != 0){
            firstSelect.setComboRowWidth(this.comboRowWidth );
            multiSelect.setComboRowWidth(this.comboRowWidth);
        }
        if(this.comboRowHeight != 0){
            firstSelect.setComboRowHeight(this.comboRowHeight);
            multiSelect.setComboRowHeight(this.comboRowHeight);
        }
        
        firstSelect.addSelectListener(new Listener(){
            @Override
            public void handleEvent(Event event) {
                CLabel itemLabel = (CLabel)event.widget;
                DropDownBox.Data data = (Data)itemLabel.getData("data");
                multiSelect.disposeAllChooseItem();
                List<DropDownBox.Data> secondDataList = linkageMultiSelectEvent.getSecondData(data);
                multiSelect.setComboDataList(secondDataList);
                itemLabel.setData("firstSelect", firstSelect);
            }
        });
        
        //主要用于控制样式
        multiSelect.addItemHandlerListener(new DefinedCommonMultiSelectEvent() {
    
            @Override
            public void disposeItemEvent(Data data, Composite text_composite) {
                ((GridData)addRowComposite.getLayoutData()).heightHint = multiSelect.getHeight();
                reLayout();
            }
            
            @Override
            public void addItemEvent(Data data, Composite text_composite) {
                ((GridData)addRowComposite.getLayoutData()).heightHint = multiSelect.getHeight();
                reLayout();
            }
        });
        if(null != itemHandlerListener){
            multiSelect.addItemHandlerListener(itemHandlerListener);
        }
        if(null != firsetSelectListener){
            firstSelect.addSelectListener(firsetSelectListener);
        }
        
        firstSelect.paint();
        firstSelect.getContentText().setData("firstSelect", firstSelect);

        multiSelect.paint();
        multiSelect.getChooseComposite().setData(firstSelect);
        
        Label deleteImg = new Label(addRowComposite,SWT.NONE);
        GridData gd_deleteImg = new GridData(SWT.LEFT, SWT.FILL, false, false, 1, 1);
        gd_deleteImg.widthHint = 24;
        gd_deleteImg.heightHint = 24;
        deleteImg.setLayoutData(gd_deleteImg);
        deleteImg.setImage(ImageUtil.getImage(FileUtil.loadResourceFileAsStream(Constants.DELETE_ICON)));
        deleteImg.setBackground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
        deleteImg.setCursor(SWTResourceManager.getCursor(SWT.CURSOR_HAND));
        deleteImg.setData("multiSelect", multiSelect);   //将删除按钮绑定到第一个下拉框上
        deleteImg.setData("firstSelect", firstSelect);   //将删除按钮绑定到第一个下拉框上
        if(this.deleteBtnListener != null){
            deleteImg.addListener(SWT.MouseDown,this.deleteBtnListener);
        }
        deleteImg.addListener(SWT.MouseDown, new Listener(){
            @Override
            public void handleEvent(Event event) {
                multiSelect.disposeAllChooseItem();
                addRowCompositeList.remove(addRowComposite);
                addRowComposite.dispose();
                reLayout();
            }
        });
        addRowComposite.setData("firstSelect", firstSelect);
        addRowComposite.setData("multiSelect", multiSelect);
        
        Label mentionLabel = new Label(addRowComposite,SWT.WRAP);
        GridData gd_mention = new GridData(SWT.LEFT, SWT.CENTER, false, true, 1, 1);
        Rectangle bounds = contentComposite.getBounds();
        if(bounds.width == 0){
            bounds = contentComposite.getParent().getParent().getBounds();
        }
        gd_mention.widthHint = bounds.width - this.nameWidth - this.chooseWidth -24-15;
        mentionLabel.setLayoutData(gd_mention);
        mentionLabel.setBackground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
        mentionLabel.setForeground(SWTResourceManager.getColor(51,51,51));
        addRowComposite.layout(true);
        addRowCompositeList.add(addRowComposite);
    }
    
    public interface DefinedFormAddLinkageMultiSelectEvent{
        /*****根据第一个下拉框选择的内容动态生成第二个框选择的内容******/
        List<DropDownBox.Data> getSecondData(DropDownBox.Data firstValue);
        /*****根据第一个获取其默认已经配置的内容*****/
        List<DropDownBox.Data> getSecondDefaultValueList(DropDownBox.Data firstValue);
    }
    
    public int getChooseWidth() {
        return chooseWidth;
    }
    
    public void setChooseWidth(int chooseWidth) {
        this.chooseWidth = chooseWidth;
    }
    
    public int getChooseHeight() {
        return chooseHeight;
    }
    
    public void setChooseHeight(int chooseHeight) {
        this.chooseHeight = chooseHeight;
    }
    
    public int getChooseItemHeight() {
        return chooseItemHeight;
    }
    
    public void setChooseItemHeight(int chooseItemHeight) {
        this.chooseItemHeight = chooseItemHeight;
    }
    
    public int getNameWidth() {
        return nameWidth;
    }
    
    public void setNameWidth(int nameWidth) {
        this.nameWidth = nameWidth;
    }
    
    public int getComboRowWidth() {
        return comboRowWidth;
    }
    
    public void setComboRowWidth(int comboRowWidth) {
        this.comboRowWidth = comboRowWidth;
    }
    
    public int getComboRowHeight() {
        return comboRowHeight;
    }
    
    public void setComboRowHeight(int comboRowHeight) {
        this.comboRowHeight = comboRowHeight;
    }
    
    public void setNameText(String nameText) {
        this.nameText = nameText;
    }
    
    /*****
     * 第二个多选择框在删除一个选择项和新增一个选择项发生的事件(主要是数据交互)
     * @param itemHandlerListener
     */
    public void setItemHandlerListener(DefinedCommonMultiSelectEvent itemHandlerListener) {
        this.itemHandlerListener = itemHandlerListener;
    }

    public void setLinkageMultiSelectEvent(DefinedFormAddLinkageMultiSelectEvent linkageMultiSelectEvent) {
        this.linkageMultiSelectEvent = linkageMultiSelectEvent;
    }

    public void setFirsetSelectListener(Listener firsetSelectListener) {
        this.firsetSelectListener = firsetSelectListener;
    }

    public void setFirstDefaultSelectValueList(List<DropDownBox.Data> firstDefaultSelectValueList) {
        this.firstDefaultSelectValueList = firstDefaultSelectValueList;
    }

    public List<DropDownBox.Data> getSelectedList() {
        return selectedList;
    }

    public void setDeleteBtnListener(Listener deleteBtnListener) {
        this.deleteBtnListener = deleteBtnListener;
    }

    public List<Composite> getAddRowCompositeList() {
        return addRowCompositeList;
    }
}

 4.2 二级联动下拉框,第二级带搜索功能

package com.view.control.select;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.CLabel;
import org.eclipse.swt.custom.ScrolledComposite;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;

import com.global.constant.Constants;
import com.util.CollectionUtil;
import com.util.FileUtil;
import com.util.StringUtil;
import com.view.control.DefinedControl;
import com.view.control.DefinedSearchText;
import com.view.control.select.DefinedCommonMultiSelect.DefinedCommonMultiSelectEvent;
import com.view.swt.SWTResourceManager;
import com.view.swt.StyleTextLink;
import com.view.util.ImageUtil;

/**
 * <p>联动二级搜索下拉框</p>
 *  * <p>
 *   第一级为单选框(目前不带搜索功能)<br>
 *   第二级为带搜索功能的多选项框
 * </p>
 * @version V1.0
 */
public class DefinedFormSearchAddLinkageMultiSelect extends DefinedControl {
    /****内容容器*****/
    private Composite addComposite;
    /****显示名称控件****/
    private CLabel name;
    /****选中内容显示的文本区域 + 下拉图标 总宽度****/
    private int chooseWidth = 323;
    private int chooseHeight = 32;
    /****内容框中的每项的高度*****/
    private int chooseItemHeight = 24;
    /*****显示名称**********/
    private String nameText;
    /*****设置显示名称控件的宽度*****/
    private int nameWidth = 100;
    private List<DropDownBox.Data> selectedList = new ArrayList<DropDownBox.Data>();
    /****每个下拉框的中每行的宽度****/
    private int comboRowWidth;
    private int comboRowHeight = 0;
    /*****主要是数据交互使用*******/
    private DefinedCommonMultiSelectEvent itemHandlerListener;
    private Listener firsetSelectListener;
    private Label addImg ;
    private List<DropDownBox.Data> firstDefaultSelectValueList;
    private List<Composite> addRowCompositeList = new ArrayList<>();
    /*****点击第二级下拉中的小删除按钮发生的额外事件*****/
    private Listener deleteBtnListener;
    /*****添加一行时出现的额外事件**********/
    private Listener addRowListener;
    private DefinedFormSearchAddLinkageMultiSelectEvent linkageMultiSelectEvent;
    private boolean asynchronous = false;
    private int pagePerNum = 10;
    private String defaultMultiSearchMentionHint;
    private boolean multiInitEmpty;
    private StyleTextLink.StyleTextLinkEvent linkEvent;
    private int mentionWidth;

    public DefinedFormSearchAddLinkageMultiSelect(Composite parent,String nameText,List<DropDownBox.Data> firstSelectedList,int chooseWidth) {
        super(parent);
       this.nameText = nameText;
       this.selectedList = firstSelectedList;
       this.comboRowWidth = chooseWidth/2;
       this.chooseWidth = chooseWidth;
    }
    
    public DefinedFormSearchAddLinkageMultiSelect(Composite parent,String nameText,List<DropDownBox.Data> firstSelectedList,int nameTextWidth,int chooseWidth,int chooseItemHeight,int chooseHeight,DefinedFormSearchAddLinkageMultiSelectEvent linkageMultiSelectEvent) {
       this(parent,nameText,firstSelectedList,chooseWidth);
       this.nameWidth = nameTextWidth;
       this.chooseItemHeight = chooseItemHeight;
       this.chooseHeight = chooseHeight;
       this.linkageMultiSelectEvent = linkageMultiSelectEvent;
    }

    @Override
    public void paint() {
        generateAddComposite(this.parent);
    }
    
    private void generateAddComposite(Composite contentComposite){
        /****显示添加一行(第一行24像素高,后面每一行 chooseHeight高)****/
        addComposite = new Composite(contentComposite,SWT.NONE);
        GridData gd_addComposite = new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1);
        addComposite.setLayoutData(gd_addComposite);
        GridLayout gl_addComposite = new GridLayout(2,false);
        gl_addComposite.horizontalSpacing = 5;
        gl_addComposite.verticalSpacing = 15;
        gl_addComposite.marginHeight = 0;
        gl_addComposite.marginWidth = 0;
        addComposite.setLayout(gl_addComposite);
        addComposite.setBackground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
        
        name = new CLabel(addComposite,SWT.NONE);
        GridData gd_addLabel = new GridData(SWT.RIGHT, SWT.FILL, false, false, 1, 1);
        gd_addLabel.widthHint = this.nameWidth;
        gd_addLabel.heightHint = 20;
        name.setLayoutData(gd_addLabel);
        name.setAlignment(SWT.RIGHT);
        name.setText(this.nameText);
        name.setBackground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
        name.setForeground(SWTResourceManager.getColor(51,51,51));
        
        addImg = new Label(addComposite,SWT.NONE);
        GridData gd_addImg = new GridData(SWT.LEFT, SWT.FILL, false, false, 1, 1);
        gd_addImg.widthHint = 24;
        gd_addImg.heightHint = 24;
        addImg.setLayoutData(gd_addImg);
        addImg.setImage(ImageUtil.getImage(FileUtil.loadResourceFileAsStream(Constants.ADD_ICON)));
        addImg.setBackground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
        addImg.setCursor(SWTResourceManager.getCursor(SWT.CURSOR_HAND));
        addImg.addListener(SWT.MouseDown, new Listener(){
            @Override
            public void handleEvent(Event event) {
                generateAddRowComposite(addComposite,null);
                reLayout();
            }
        });
        
        if(CollectionUtil.isNotEmpty(firstDefaultSelectValueList)){
            for(DropDownBox.Data firstDefaultSelectValue:firstDefaultSelectValueList){
                generateAddRowComposite(addComposite,firstDefaultSelectValue);
            }
        }
        reLayout();
    }
    
    private void reLayout(){
        Composite contentComposite = addComposite;
        while(contentComposite != this.parent){
            contentComposite.layout(true);
            contentComposite = contentComposite.getParent();
        }
        contentComposite.layout(true);
        Composite parentComposite = contentComposite.getParent();
        while(!(parentComposite instanceof ScrolledComposite) && !(parentComposite instanceof Shell)){
            parentComposite.layout(true);
            contentComposite = parentComposite;
            parentComposite = parentComposite.getParent();
        }
        if(parentComposite instanceof ScrolledComposite){
            ((ScrolledComposite)parentComposite).setMinSize(contentComposite.computeSize(SWT.DEFAULT, SWT.DEFAULT));
        }
    }
    
    /**
     * 
     * @param contentComposite   为addComposite(2列)
     */
    private void generateAddRowComposite(Composite contentComposite,DropDownBox.Data defaultValue){
        Composite addRowComposite = new Composite(contentComposite,SWT.NONE);
        GridData gd_addRowComposite = new GridData(SWT.FILL, SWT.FILL, false, false, 2, 1);
        gd_addRowComposite.heightHint = this.chooseHeight;
        addRowComposite.setLayoutData(gd_addRowComposite);
        
        GridLayout gl_addRowComposite = new GridLayout(5,false);
        gl_addRowComposite.horizontalSpacing = 5;
        gl_addRowComposite.verticalSpacing = 0;
        gl_addRowComposite.marginHeight = 0;
        gl_addRowComposite.marginWidth = 0;
        addRowComposite.setLayout(gl_addRowComposite);
        addRowComposite.setBackground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
        
        CLabel emptyLabel = new CLabel(addRowComposite,SWT.NONE);
        GridData gd_addLabel = new GridData(SWT.RIGHT, SWT.FILL, false, true, 1, 1);
        gd_addLabel.widthHint = this.nameWidth;
        emptyLabel.setLayoutData(gd_addLabel);
        emptyLabel.setBackground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
        
        DefinedSingleSelect firstSelect = new DefinedSingleSelect(addRowComposite,this.selectedList,this.chooseWidth /2,this.chooseHeight);
        DefinedSearchMultiSelect multiSelect = new DefinedSearchMultiSelect(addRowComposite,new DefinedSearchMultiSelect.SearchMultiSelectEvent(){

            @Override
            public DefinedSearchText.SearchPageResult<DropDownBox.Data> search(Event event, String searchKeys, int pageNum, int pagePerNum, List<Object> filters) {
                return linkageMultiSelectEvent.search(event, searchKeys, pageNum, pagePerNum, filters);
            }

            @Override
            public String verifySearchContent(String searchKey) {
                return linkageMultiSelectEvent.verifySearchContent(searchKey);
            }
            
        },this.chooseWidth /2,this.chooseItemHeight );
        multiSelect.setAsynchronous(asynchronous);
        multiSelect.setInitEmpty(this.multiInitEmpty);
        multiSelect.setLinkEvent(linkEvent);
        if(pagePerNum>0){
            multiSelect.setPagePerNum(pagePerNum);
        }

        if(StringUtil.isNotNullAndEmpty(defaultMultiSearchMentionHint)){
            multiSelect.setDefaultMentionHint(defaultMultiSearchMentionHint);
        }
        List<DropDownBox.Data> secondDefaultList = new ArrayList<DropDownBox.Data>();
        if(null != defaultValue){
            firstSelect.setDefaultValue(defaultValue);
            multiSelect.addSearchExternalFilter(defaultValue.getValue());
            multiSelect.setInitEmpty(false);
            secondDefaultList = linkageMultiSelectEvent.getSecondDefaultValueList(defaultValue);
        }
        if(CollectionUtil.isNotEmpty(secondDefaultList)){
            multiSelect.setDefaultValueList(secondDefaultList);
        }
        if(this.comboRowWidth != 0){
            firstSelect.setComboRowWidth(this.comboRowWidth );
            multiSelect.setComboRowWidth(this.comboRowWidth);
        }
        if(this.comboRowHeight != 0){
            firstSelect.setComboRowHeight(this.comboRowHeight);
            multiSelect.setComboRowHeight(this.comboRowHeight);
        }
        firstSelect.addSelectListener(new Listener(){
            @Override
            public void handleEvent(Event event) {
                CLabel itemLabel = (CLabel)event.widget;
                DropDownBox.Data data = (DropDownBox.Data)itemLabel.getData("data");
                multiSelect.disposeAllChooseItem();
                multiSelect.replaceSearchExternalFilter(data.getValue(), 0);
                multiSelect.setInitEmpty(false);
                itemLabel.setData("firstSelect", firstSelect);
            }
        });
        
        //主要用于控制样式
        multiSelect.addItemHandlerListener(new DefinedCommonMultiSelectEvent() {
            @Override
            public void disposeItemEvent(DropDownBox.Data data, Composite text_composite) {
                ((GridData)addRowComposite.getLayoutData()).heightHint = multiSelect.getHeight();
                reLayout();
            }
            
            @Override
            public void addItemEvent(DropDownBox.Data data, Composite text_composite) {
                ((GridData)addRowComposite.getLayoutData()).heightHint = multiSelect.getHeight();
                reLayout();
            }
        });
        if(null != itemHandlerListener){
            multiSelect.addItemHandlerListener(itemHandlerListener);
        }
        if(null != firsetSelectListener){
            firstSelect.addSelectListener(firsetSelectListener);
        }
        
        firstSelect.paint();
        firstSelect.getContentText().setData("firstSelect", firstSelect);

        multiSelect.paint();
        multiSelect.getChooseComposite().setData(firstSelect);
        
        Label deleteImg = new Label(addRowComposite,SWT.NONE);
        GridData gd_deleteImg = new GridData(SWT.LEFT, SWT.FILL, false, false, 1, 1);
        gd_deleteImg.widthHint = 24;
        gd_deleteImg.heightHint = 24;
        deleteImg.setLayoutData(gd_deleteImg);
        deleteImg.setImage(ImageUtil.getImage(FileUtil.loadResourceFileAsStream(Constants.DELETE_ICON)));
        deleteImg.setBackground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
        deleteImg.setCursor(SWTResourceManager.getCursor(SWT.CURSOR_HAND));
        deleteImg.setData("multiSelect", multiSelect);   //将多选框绑定到删除图标上
        deleteImg.setData("firstSelect", firstSelect);   //将第一个下拉框绑定到删除图标上
        if(this.deleteBtnListener != null){
            deleteImg.addListener(SWT.MouseDown,this.deleteBtnListener);
        }
        deleteImg.addListener(SWT.MouseDown, new Listener(){
            @Override
            public void handleEvent(Event event) {
                multiSelect.disposeAllChooseItem();
                addRowCompositeList.remove(addRowComposite);
                addRowComposite.dispose();
                reLayout();
            }
        });
        addRowComposite.setData("firstSelect", firstSelect);
        addRowComposite.setData("multiSelect", multiSelect);
        
        Label mentionLabel = new Label(addRowComposite,SWT.WRAP);
        GridData gd_mention = new GridData(SWT.LEFT, SWT.CENTER, false, true, 1, 1);
        if(this.mentionWidth != 0){
            gd_mention.widthHint = this.mentionWidth;
        }else{
            Rectangle bounds = contentComposite.getBounds();
            if(bounds.width == 0){
                bounds = contentComposite.getParent().getParent().getBounds();
            }
            gd_mention.widthHint = bounds.width - this.nameWidth - this.chooseWidth -24-15;
        }
        mentionLabel.setLayoutData(gd_mention);
        mentionLabel.setBackground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
        mentionLabel.setForeground(SWTResourceManager.getColor(51,51,51));
        addRowComposite.layout(true);
        addRowCompositeList.add(addRowComposite);
        if(addRowListener != null){
            Event addRowEvent = new Event();
            addRowEvent.widget = addRowComposite;
            addRowEvent.button = 1;
            addRowListener.handleEvent(addRowEvent);
        }
    }
    
    public interface DefinedFormSearchAddLinkageMultiSelectEvent{
        /*****根据第一个获取其默认已经配置的内容*****/
        List<DropDownBox.Data> getSecondDefaultValueList(DropDownBox.Data firstValue);
        DefinedSearchText.SearchPageResult<DropDownBox.Data> search(Event event,String searchKeys,int pageNum,int pagePerNum,List<Object> filters);
        String verifySearchContent(String searchKey);
    }
    
    public int getChooseWidth() {
        return chooseWidth;
    }
    
    public void setChooseWidth(int chooseWidth) {
        this.chooseWidth = chooseWidth;
    }
    
    public int getChooseHeight() {
        return chooseHeight;
    }
    
    public void setChooseHeight(int chooseHeight) {
        this.chooseHeight = chooseHeight;
    }
    
    public int getChooseItemHeight() {
        return chooseItemHeight;
    }
    
    public void setChooseItemHeight(int chooseItemHeight) {
        this.chooseItemHeight = chooseItemHeight;
    }
    
    public int getNameWidth() {
        return nameWidth;
    }
    
    public void setNameWidth(int nameWidth) {
        this.nameWidth = nameWidth;
    }
    
    public int getComboRowWidth() {
        return comboRowWidth;
    }
    
    public void setComboRowWidth(int comboRowWidth) {
        this.comboRowWidth = comboRowWidth;
    }
    
    public int getComboRowHeight() {
        return comboRowHeight;
    }
    
    public void setComboRowHeight(int comboRowHeight) {
        this.comboRowHeight = comboRowHeight;
    }
    
    public void setNameText(String nameText) {
        this.nameText = nameText;
    }
    
    /*****
     * 第二个多选择框在删除一个选择项和新增一个选择项发生的事件(主要是数据交互)
     * @param itemHandlerListener
     */
    public void setItemHandlerListener(DefinedCommonMultiSelectEvent itemHandlerListener) {
        this.itemHandlerListener = itemHandlerListener;
    }

    public void setLinkageMultiSelectEvent(DefinedFormSearchAddLinkageMultiSelectEvent linkageMultiSelectEvent) {
        this.linkageMultiSelectEvent = linkageMultiSelectEvent;
    }

    public void setFirsetSelectListener(Listener firsetSelectListener) {
        this.firsetSelectListener = firsetSelectListener;
    }

    public void setFirstDefaultSelectValueList(List<DropDownBox.Data> firstDefaultSelectValueList) {
        this.firstDefaultSelectValueList = firstDefaultSelectValueList;
    }

    public List<DropDownBox.Data> getSelectedList() {
        return selectedList;
    }

    /**
     * 设置删除整行的逻辑 其中event widget获取的是删除图标,通过删除图标获取第一个下拉框和第二个多选下拉框
     * @param deleteBtnListener
     */
    public void setDeleteBtnListener(Listener deleteBtnListener) {
        this.deleteBtnListener = deleteBtnListener;
    }

    public List<Composite> getAddRowCompositeList() {
        return addRowCompositeList;
    }
    public int getPagePerNum() {
        return pagePerNum;
    }

    public void setPagePerNum(int pagePerNum) {
        this.pagePerNum = pagePerNum;
    }

    public boolean isAsynchronous() {
        return asynchronous;
    }
    
    public void setAsynchronous(boolean asynchronous) {
        this.asynchronous = asynchronous;
    }

    public String getDefaultMultiSearchMentionHint() {
        return defaultMultiSearchMentionHint;
    }

    public void setDefaultMultiSearchMentionHint(String defaultMultiSearchMentionHint) {
        this.defaultMultiSearchMentionHint = defaultMultiSearchMentionHint;
    }
    
    public boolean isMultiInitEmpty() {
        return multiInitEmpty;
    }

    public void setMultiInitEmpty(boolean multiInitEmpty) {
        this.multiInitEmpty = multiInitEmpty;
    }

    public void setLinkEvent(StyleTextLink.StyleTextLinkEvent linkEvent) {
        this.linkEvent = linkEvent;
    }

    /**
     * 添加一行触发的事件
     * @param addRowListener event 中widget是行控件 Composite
     */
    public void setAddRowListener(Listener addRowListener) {
        this.addRowListener = addRowListener;
    }

    public int getMentionWidth() {
        return mentionWidth;
    }
    
    public void setMentionWidth(int mentionWidth) {
        this.mentionWidth = mentionWidth;
    }
}

4.3 带搜索功能的二级联动框

package com.view.control.select;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.CLabel;
import org.eclipse.swt.custom.ScrolledComposite;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;

import com.global.constant.Constants;
import com.util.CollectionUtil;
import com.util.FileUtil;
import com.util.StringUtil;
import com.view.control.DefinedControl;
import com.view.control.select.DefinedCommonSingleSelect.DefinedCommonSingleSelectEvent;
import com.view.control.select.DropDownBox.Data;
import com.view.swt.SWTResourceManager;
import com.view.swt.StyleTextLink;
import com.view.util.ImageUtil;

/**
 * <p>添加联动二级多选框(有添加删除按钮)</p>
  * <p>
 *   第一级为单选框(可带搜索功能)<br>
 *   第二级为单选框(可带搜索功能)
 * </p>
 * <p>
 *   区分第一级拉框是否是带搜索功能,取决于firstSearchEvent 变量是否为null,不为null表示带搜索功能<br>
 *   区分第二级拉框是否是带搜索功能,取决于secondSearchEvent 变量是否为null,不为null表示带搜索功能<br>
 * </p>
 * <p>
 *   第一级下拉框可以通过 listener 方法 event的widget 使用getData("firstSelect") 获取下拉框本身<br>
 *   第二级下拉框可以通过 listener 方法 event的widget 使用getData("firstSelect") 获取第一个下拉框对象,通过getData("secondSelect")获取第二个下拉框本身<br>
 *   deleteBtnListener 通用event的widget使用getData("firstSelect") 获取第一个下拉框对象,通过getData("secondSelect")获取第二个下拉框
 * <p>
 * @version V1.0
 */
public class DefinedFromAddLinkageSingleSelect extends DefinedControl {

    /****内容容器*****/
    private Composite addComposite;
    /****显示名称控件****/
    private CLabel name;
    /****选中内容显示的文本区域 + 下拉图标 总宽度(两级)****/
    private int chooseWidth = 323;
    private int chooseHeight = 32;
    /*****显示名称**********/
    private String nameText;
    /*****设置显示名称控件的宽度*****/
    private int nameWidth = 100;
    /****每个下拉框的中每行的宽度****/
    private int comboRowWidth;
    private int comboRowHeight = 0;
    private Label addImg ;
    /*****点击第二级下拉中的小删除按钮发生的额外事件*****/
    private Listener deleteBtnListener;
    /****联动事件*****/
    private DefinedFromAddLinkageSingleSelectEvent linkageSelectEvent;
    /****是否将value也显示在下拉框中,呈现效果为(display(value))****/
    private boolean showValue;
    private List<Composite> addRowCompositeList = new ArrayList<>();
    
    
    //****以下为第一级下拉框不带搜索功能需要使用的变量***************//
    /****第一级下拉框选择范围内容(若第一级下拉框带搜索功能,不需要传递该值,应实现搜索方法,通过搜索方法填充第一级下拉框选择范围)***/
    private List<DropDownBox.Data> firstSelectedList = new ArrayList<DropDownBox.Data>();
    //****结束**************/
    
    
    //****以下为第一级下拉框带搜索功能的用法*****//
    /***是否异步分页加载**/
    private boolean firstAsynchronous;
    /***搜索框中的默认提示*****/
    private String firstDefaultMultiSearchMentionHint;
    /**链接事件******/
    private StyleTextLink.StyleTextLinkEvent firstLinkEvent;
    private DefinedSearchSingleSelect.SearchSingleSelectEvent firstSearchEvent;
    // ************结束*********************//
    
    
    
    //*******第一级下拉框公用变量***********//
    /****第一级搜索框默认内容********/
    private List<DropDownBox.Data> firstDefaultSelectValueList;
    /****选中第一级搜索框触发的事件*******/
    private Listener firsetSelectListener;
    private DefinedCommonSingleSelectEvent firstDropdownBeforeEvent;
    // ************结束*********************//
    
    
    
    // ****以下为第二级下拉框带搜索功能的用法*****//
    /*** 是否异步分页加载 **/
    private boolean secondAsynchronous;
    /*** 搜索框中的默认提示 *****/
    private String secondDefaultMultiSearchMentionHint;
    /** 链接事件 ******/
    private StyleTextLink.StyleTextLinkEvent secondLinkEvent;
    private DefinedSearchSingleSelect.SearchSingleSelectEvent secondSearchEvent;
    // ************结束*********************//
    
    
    
    // *******第二级下拉框公用变量***********//
    /**** 选中第二级搜索框触发的事件 *******/
    private Listener secondSelectListener;
    private DefinedCommonSingleSelectEvent secondDropdownBeforeEvent;
    // ************结束*********************//

    public DefinedFromAddLinkageSingleSelect(Composite parent, String nameText, int chooseWidth,
            DefinedFromAddLinkageSingleSelectEvent linkageSelectEvent) {
        super(parent);
        this.nameText = nameText;
        this.comboRowWidth = chooseWidth / 2;
        this.chooseWidth = chooseWidth;
        this.linkageSelectEvent = linkageSelectEvent;
    }
    
    public DefinedFromAddLinkageSingleSelect(Composite parent, String nameText, int nameTextWidth, int chooseWidth,
            int chooseHeight, DefinedFromAddLinkageSingleSelectEvent linkageSelectEvent) {
        this(parent, nameText, chooseWidth, linkageSelectEvent);
        this.nameWidth = nameTextWidth;
        this.chooseHeight = chooseHeight;
    }
    
    @Override
    public void paint() {
        generateAddComposite(this.parent);
    }
    
    private void generateAddComposite(Composite contentComposite){
        /****显示添加一行(第一行24像素高,后面每一行 chooseHeight高)****/
        addComposite = new Composite(contentComposite,SWT.NONE);
        GridData gd_addComposite = new GridData(SWT.FILL, SWT.FILL, true, false, 1, 1);
        addComposite.setLayoutData(gd_addComposite);
        GridLayout gl_addComposite = new GridLayout(2,false);
        gl_addComposite.horizontalSpacing = 5;
        gl_addComposite.verticalSpacing = 15;
        gl_addComposite.marginHeight = 0;
        gl_addComposite.marginWidth = 0;
        addComposite.setLayout(gl_addComposite);
        addComposite.setBackground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
        
        name = new CLabel(addComposite,SWT.NONE);
        GridData gd_addLabel = new GridData(SWT.RIGHT, SWT.FILL, false, false, 1, 1);
        gd_addLabel.widthHint = this.nameWidth;
        gd_addLabel.heightHint = 20;
        name.setLayoutData(gd_addLabel);
        name.setAlignment(SWT.RIGHT);
        name.setText(this.nameText);
        name.setBackground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
        name.setForeground(SWTResourceManager.getColor(51,51,51));
        
        addImg = new Label(addComposite,SWT.NONE);
        GridData gd_addImg = new GridData(SWT.LEFT, SWT.FILL, false, false, 1, 1);
        gd_addImg.widthHint = 24;
        gd_addImg.heightHint = 24;
        addImg.setLayoutData(gd_addImg);
        addImg.setImage(ImageUtil.getImage(FileUtil.loadResourceFileAsStream(Constants.ADD_ICON)));
        addImg.setBackground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
        addImg.setCursor(SWTResourceManager.getCursor(SWT.CURSOR_HAND));
        addImg.addListener(SWT.MouseDown, new Listener(){
            @Override
            public void handleEvent(Event event) {
                generateAddRowComposite(addComposite,null);
                reLayout();
            }
        });
        
        if(CollectionUtil.isNotEmpty(firstDefaultSelectValueList)){
            for(DropDownBox.Data firstDefaultSelectValue:firstDefaultSelectValueList){
                generateAddRowComposite(addComposite,firstDefaultSelectValue);
            }
        }
        reLayout();
    }
    
    /**
     * 绘制每行联动下拉框
     * @param contentComposite
     * @param defaultValue
     */
    private void generateAddRowComposite(Composite contentComposite,DropDownBox.Data defaultValue){
        Composite addRowComposite = new Composite(contentComposite,SWT.NONE);
        GridData gd_addRowComposite = new GridData(SWT.FILL, SWT.FILL, false, false, 2, 1);
        gd_addRowComposite.heightHint = this.chooseHeight;
        addRowComposite.setLayoutData(gd_addRowComposite);
        
        GridLayout gl_addRowComposite = new GridLayout(5,false);
        gl_addRowComposite.horizontalSpacing = 5;
        gl_addRowComposite.verticalSpacing = 0;
        gl_addRowComposite.marginHeight = 0;
        gl_addRowComposite.marginWidth = 0;
        addRowComposite.setLayout(gl_addRowComposite);
        addRowComposite.setBackground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
        
        CLabel emptyLabel = new CLabel(addRowComposite,SWT.NONE);
        GridData gd_addLabel = new GridData(SWT.RIGHT, SWT.FILL, false, true, 1, 1);
        gd_addLabel.widthHint = this.nameWidth;
        emptyLabel.setLayoutData(gd_addLabel);
        emptyLabel.setBackground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
        
        //开始生成第一级下拉框对象
        DropDownBox<DropDownBox.Data> firstSelector = null;
        if(this.firstSearchEvent != null){
            //第一级下拉框带搜索功能
            DefinedSearchSingleSelect searchFirstSelector =  new DefinedSearchSingleSelect(addRowComposite,this.firstSearchEvent,this.chooseWidth /2,this.chooseHeight,showValue);
            if(null != defaultValue){
                searchFirstSelector.setDefaultValue(defaultValue);
            }
            if(StringUtil.isNotNullAndEmpty(this.firstDefaultMultiSearchMentionHint)){
                searchFirstSelector.setDefaultMentionHint(firstDefaultMultiSearchMentionHint);
            }
            searchFirstSelector.setAsynchronous(this.firstAsynchronous);
            searchFirstSelector.setLinkEvent(this.firstLinkEvent);
            if(null != this.firstDropdownBeforeEvent){
                searchFirstSelector.setDropdownBeforeEvent(this.firstDropdownBeforeEvent);
            }
            searchFirstSelector.addSelectListener(event->{
                CLabel itemLabel = (CLabel)event.widget;
                itemLabel.setData("firstSelect", searchFirstSelector);
            });
            firstSelector = searchFirstSelector;
        }else{
            //第一级下拉框不带搜索功能
            DefinedSingleSelect noSearchFirstSelector = new DefinedSingleSelect(addRowComposite,this.firstSelectedList,this.chooseWidth /2,this.chooseHeight,showValue);
            if(null != defaultValue){
                noSearchFirstSelector.setDefaultValue(defaultValue);
            }
            if(null != this.firstDropdownBeforeEvent){
                noSearchFirstSelector.setDropdownBeforeEvent(this.firstDropdownBeforeEvent);
            }
            noSearchFirstSelector.addSelectListener(event->{
                CLabel itemLabel = (CLabel)event.widget;
                itemLabel.setData("firstSelect", noSearchFirstSelector);
            });
            firstSelector = noSearchFirstSelector;
        }
        
        DropDownBox.Data secondDefaultValue = null;
        if(null != defaultValue){
            secondDefaultValue = linkageSelectEvent.getSecondDefaultValue(defaultValue);
        }
        DropDownBox<DropDownBox.Data> secondSelector = null;
        //定义第二个下拉框
        if(this.secondSearchEvent != null){
            //带搜索功能
            DefinedSearchSingleSelect searchSecondSelector =  new DefinedSearchSingleSelect(addRowComposite,this.secondSearchEvent,this.chooseWidth /2,this.chooseHeight,showValue);
            if(null != secondDefaultValue){
                searchSecondSelector.setDefaultValue(secondDefaultValue);
            }
            searchSecondSelector.getContentText().setData("secondSelect", secondSelector);
            searchSecondSelector.getContentText().setData("firstSelect", firstSelector);
            if(StringUtil.isNotNullAndEmpty(this.secondDefaultMultiSearchMentionHint)){
                searchSecondSelector.setDefaultMentionHint(secondDefaultMultiSearchMentionHint);
            }
            searchSecondSelector.setAsynchronous(this.secondAsynchronous);
            searchSecondSelector.setLinkEvent(this.secondLinkEvent);
            if(null != this.secondDropdownBeforeEvent){
                searchSecondSelector.setDropdownBeforeEvent(this.secondDropdownBeforeEvent);
            }
            firstSelector.addSelectListener(event->{
                CLabel itemLabel = (CLabel)event.widget;
                DropDownBox.Data data = (Data)itemLabel.getData("data");
                searchSecondSelector.replaceSearchExternalFilter(data.getValue(), 0);
                searchSecondSelector.setInitEmpty(false);
            });
            searchSecondSelector.addSelectListener(event->{
                CLabel itemLabel = (CLabel)event.widget;
                itemLabel.setData("firstSelect", searchSecondSelector.getContentComposite().getData("firstSelect"));
                itemLabel.setData("secondSelect", searchSecondSelector);
            });
            secondSelector = searchSecondSelector;
        }else{
            //不带搜索功能
            List<DropDownBox.Data> secondDataList = new ArrayList<>();
            if(null != defaultValue){
                secondDataList = linkageSelectEvent.getSecondData(defaultValue);
            }
            DefinedSingleSelect noSearchSecondSelector = new DefinedSingleSelect(addRowComposite,secondDataList,this.chooseWidth /2,this.chooseHeight,showValue);
            if(null != secondDefaultValue){
                noSearchSecondSelector.setDefaultValue(secondDefaultValue);
            }
            if(null != this.secondDropdownBeforeEvent){
                noSearchSecondSelector.setDropdownBeforeEvent(this.secondDropdownBeforeEvent);
            }
            secondSelector = noSearchSecondSelector;
            firstSelector.addSelectListener(event->{
                CLabel itemLabel = (CLabel)event.widget;
                DropDownBox.Data data = (Data)itemLabel.getData("data");
                List<DropDownBox.Data> secondList = this.linkageSelectEvent.getSecondData(data);
                noSearchSecondSelector.setComboDataList(secondList);
            });
            noSearchSecondSelector.addSelectListener(event->{
                CLabel itemLabel = (CLabel)event.widget;
                itemLabel.setData("firstSelect", noSearchSecondSelector.getContentComposite().getData("firstSelect"));
                itemLabel.setData("secondSelect", noSearchSecondSelector);
            });
        }

        if(this.comboRowWidth != 0){
            firstSelector.setComboRowWidth(this.comboRowWidth );
            secondSelector.setComboRowWidth(this.comboRowWidth );
        }
        if(this.comboRowHeight != 0){
            firstSelector.setComboRowHeight(this.comboRowHeight);
            secondSelector.setComboRowHeight(this.comboRowHeight);
        }

        if(null != firsetSelectListener){
            firstSelector.addSelectListener(firsetSelectListener);
        }
        if(null != secondSelectListener){
            secondSelector.addSelectListener(secondSelectListener);
        }

        firstSelector.paint();
        secondSelector.paint();
        firstSelector.getContentComposite().setData("firstSelect", firstSelector);
        secondSelector.getContentComposite().setData("firstSelect", firstSelector);
        secondSelector.getContentComposite().setData("secondSelect", secondSelector);
        
        Label deleteImg = new Label(addRowComposite,SWT.NONE);
        GridData gd_deleteImg = new GridData(SWT.LEFT, SWT.FILL, false, false, 1, 1);
        gd_deleteImg.widthHint = 24;
        gd_deleteImg.heightHint = 24;
        deleteImg.setLayoutData(gd_deleteImg);
        deleteImg.setImage(ImageUtil.getImage(FileUtil.loadResourceFileAsStream(Constants.DELETE_ICON)));
        deleteImg.setBackground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
        deleteImg.setCursor(SWTResourceManager.getCursor(SWT.CURSOR_HAND));
        deleteImg.setData("secondSelect", secondSelector);   //将第二下拉框绑定到删除图标上
        deleteImg.setData("firstSelect", firstSelector);   //将第一个下拉框绑定到删除图标上
        if(this.deleteBtnListener != null){
            deleteImg.addListener(SWT.MouseDown,this.deleteBtnListener);
        }
        deleteImg.addListener(SWT.MouseDown, new Listener(){
            @Override
            public void handleEvent(Event event) {
                addRowComposite.dispose();
                addRowCompositeList.remove(addRowComposite);
                reLayout();
            }
        });
        
        addRowComposite.setData("firstSelect", firstSelector);
        addRowComposite.setData("secondSelect", secondSelector);
        
        Label mentionLabel = new Label(addRowComposite,SWT.WRAP);
        GridData gd_mention = new GridData(SWT.LEFT, SWT.CENTER, false, true, 1, 1);
        Rectangle bounds = contentComposite.getBounds();
        if(bounds.width == 0){
            bounds = contentComposite.getParent().getParent().getBounds();
        }
        gd_mention.widthHint = bounds.width - this.nameWidth - this.chooseWidth -24-15;
        mentionLabel.setLayoutData(gd_mention);
        mentionLabel.setBackground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
        mentionLabel.setForeground(SWTResourceManager.getColor(51,51,51));
        addRowComposite.layout(true);
        addRowCompositeList.add(addRowComposite);
    }
    
    private void reLayout(){
        Composite contentComposite = addComposite;
        while(contentComposite != this.parent){
            contentComposite.layout(true);
            contentComposite = contentComposite.getParent();
        }
        contentComposite.layout(true);
        Composite parentComposite = contentComposite.getParent();
        while(!(parentComposite instanceof ScrolledComposite) && !(parentComposite instanceof Shell)){
            parentComposite.layout(true);
            contentComposite = parentComposite;
            parentComposite = parentComposite.getParent();
        }
        if(parentComposite instanceof ScrolledComposite){
            ((ScrolledComposite)parentComposite).setMinSize(contentComposite.computeSize(SWT.DEFAULT, SWT.DEFAULT));
        }
    }
    
    /**
     * <p>联动事件</p>
     * @version V1.0
     */
    public interface DefinedFromAddLinkageSingleSelectEvent{
        /*****根据第一个下拉框选择的内容动态生成第二个框选择的内容(仅在第二个下拉框不带搜索功能时需要实现该方法)******/
        List<DropDownBox.Data> getSecondData(DropDownBox.Data firstValue);
        /*****根据第一个获取其默认已经配置的内容*****/
        DropDownBox.Data getSecondDefaultValue(DropDownBox.Data firstValue);
    }

    public int getChooseWidth() {
        return chooseWidth;
    }

    public void setChooseWidth(int chooseWidth) {
        this.chooseWidth = chooseWidth;
    }

    public int getChooseHeight() {
        return chooseHeight;
    }

    public void setChooseHeight(int chooseHeight) {
        this.chooseHeight = chooseHeight;
    }

    public int getNameWidth() {
        return nameWidth;
    }

    public void setNameWidth(int nameWidth) {
        this.nameWidth = nameWidth;
    }

    public int getComboRowWidth() {
        return comboRowWidth;
    }

    public void setComboRowWidth(int comboRowWidth) {
        this.comboRowWidth = comboRowWidth;
    }

    public int getComboRowHeight() {
        return comboRowHeight;
    }

    public void setComboRowHeight(int comboRowHeight) {
        this.comboRowHeight = comboRowHeight;
    }

    public boolean isShowValue() {
        return showValue;
    }

    public void setShowValue(boolean showValue) {
        this.showValue = showValue;
    }

    public List<DropDownBox.Data> getFirstSelectedList() {
        return firstSelectedList;
    }

    public void setFirstSelectedList(List<DropDownBox.Data> firstSelectedList) {
        this.firstSelectedList = firstSelectedList;
    }

    public boolean isFirstAsynchronous() {
        return firstAsynchronous;
    }

    public void setFirstAsynchronous(boolean firstAsynchronous) {
        this.firstAsynchronous = firstAsynchronous;
    }

    public String getFirstDefaultMultiSearchMentionHint() {
        return firstDefaultMultiSearchMentionHint;
    }

    public void setFirstDefaultMultiSearchMentionHint(String firstDefaultMultiSearchMentionHint) {
        this.firstDefaultMultiSearchMentionHint = firstDefaultMultiSearchMentionHint;
    }

    public List<DropDownBox.Data> getFirstDefaultSelectValueList() {
        return firstDefaultSelectValueList;
    }

    public void setFirstDefaultSelectValueList(List<DropDownBox.Data> firstDefaultSelectValueList) {
        this.firstDefaultSelectValueList = firstDefaultSelectValueList;
    }

    public boolean isSecondAsynchronous() {
        return secondAsynchronous;
    }

    public void setSecondAsynchronous(boolean secondAsynchronous) {
        this.secondAsynchronous = secondAsynchronous;
    }

    public String getSecondDefaultMultiSearchMentionHint() {
        return secondDefaultMultiSearchMentionHint;
    }
    
    public void setSecondDefaultMultiSearchMentionHint(String secondDefaultMultiSearchMentionHint) {
        this.secondDefaultMultiSearchMentionHint = secondDefaultMultiSearchMentionHint;
    }

    public Listener getSecondSelectListener() {
        return secondSelectListener;
    }

    public void setSecondSelectListener(Listener secondSelectListener) {
        this.secondSelectListener = secondSelectListener;
    }

    public void setNameText(String nameText) {
        this.nameText = nameText;
    }

    public void setDeleteBtnListener(Listener deleteBtnListener) {
        this.deleteBtnListener = deleteBtnListener;
    }

    public void setLinkageSelectEvent(DefinedFromAddLinkageSingleSelectEvent linkageSelectEvent) {
        this.linkageSelectEvent = linkageSelectEvent;
    }

    public void setFirstLinkEvent(StyleTextLink.StyleTextLinkEvent firstLinkEvent) {
        this.firstLinkEvent = firstLinkEvent;
    }

    public void setFirstSearchEvent(DefinedSearchSingleSelect.SearchSingleSelectEvent firstSearchEvent) {
        this.firstSearchEvent = firstSearchEvent;
    }

    public void setFirsetSelectListener(Listener firsetSelectListener) {
        this.firsetSelectListener = firsetSelectListener;
    }

    public void setFirstDropdownBeforeEvent(DefinedCommonSingleSelectEvent firstDropdownBeforeEvent) {
        this.firstDropdownBeforeEvent = firstDropdownBeforeEvent;
    }

    public void setSecondLinkEvent(StyleTextLink.StyleTextLinkEvent secondLinkEvent) {
        this.secondLinkEvent = secondLinkEvent;
    }

    public void setSecondSearchEvent(DefinedSearchSingleSelect.SearchSingleSelectEvent secondSearchEvent) {
        this.secondSearchEvent = secondSearchEvent;
    }

    public void setSecondDropdownBeforeEvent(DefinedCommonSingleSelectEvent secondDropdownBeforeEvent) {
        this.secondDropdownBeforeEvent = secondDropdownBeforeEvent;
    }
    public List<Composite> getAddRowCompositeList() {
        return addRowCompositeList;
    }
}

4.4

package com.view.control.select;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;

import com.view.control.DefinedControl;

/**
 * <p>二级联动下拉框</p>
 * @version V1.0
 */
public class DefinedSelectedLingageSelector extends DefinedControl{
    /****内容容器*****/
    private Composite contentComposite;
    private DefinedSingleSelect selectFirst;
    private DefinedSingleSelect selectSecond;
    /****第一级下拉框选项所有内容******/
    private List<DropDownBox.Data> comboDataList; 
    private int firstComboWidth;
    private int comboRowWidth;
    private int contentWidth = 323;
    private int contentHeight = 32;
    private Listener firstSelectListener;
    private Listener secondSelectListener;
    private DropDownBox.Data defaultValue;
    private SecondSelectorGenerator secondSelectorGenerator;
    private DropDownBox.Data secondDefaultValue;
    
    
    public DefinedSelectedLingageSelector(Composite parent,List<DropDownBox.Data> comboDataList,int comboRowWidth,int firstComboWidth,SecondSelectorGenerator secondSelectorGenerator) {
        super(parent);
        this.comboDataList = comboDataList;
        this.comboRowWidth = comboRowWidth;
        this.firstComboWidth = firstComboWidth;
        this.secondSelectorGenerator = secondSelectorGenerator;
    }
    
    @Override
    public void paint() {
        contentComposite = new Composite(this.parent,SWT.NONE);
        contentComposite.setBounds(0, 0, contentWidth, contentHeight);
        GridLayout gl_contentComposite = new GridLayout(2,false);
        gl_contentComposite.horizontalSpacing = 5;
        gl_contentComposite.verticalSpacing = 0;
        gl_contentComposite.marginHeight = 0;
        contentComposite.setLayout(gl_contentComposite);
        
        selectFirst = new DefinedSingleSelect(contentComposite, comboDataList, comboRowWidth, this.contentHeight);
        List<DropDownBox.Data> secondDataList = new ArrayList<DropDownBox.Data>();
        if(null != defaultValue){
            selectFirst.setDefaultValue(defaultValue);
            secondDataList = secondSelectorGenerator.getSecondSelectorData(defaultValue.getValue());
        }
        selectFirst.addSelectListener(new Listener(){
            @Override
            public void handleEvent(Event event) {
                changeSecondSelect(selectFirst.getValue());
            }
        });
        if(this.firstSelectListener != null){
            selectFirst.addSelectListener(this.firstSelectListener);
        }
        selectFirst.paint();

        Composite secondSelectComposite = new Composite(contentComposite,SWT.NONE);
        GridData gd_secondSelectComposite = new GridData(SWT.LEFT, SWT.FILL, false, true, 1, 1);
        gd_secondSelectComposite.widthHint = this.contentWidth - this.firstComboWidth - 5;
        secondSelectComposite.setLayoutData(gd_secondSelectComposite);
        selectSecond = new DefinedSingleSelect(secondSelectComposite, secondDataList, this.contentWidth - this.firstComboWidth - 5, this.contentHeight);
        if(null != secondDefaultValue){
            selectSecond.setDefaultValue(secondDefaultValue);
        }
        if(this.secondSelectListener != null){
            selectFirst.addSelectListener(this.secondSelectListener);
        }
        selectSecond.paint();
    }
    
    private void changeSecondSelect(String defaultValue){
        List<DropDownBox.Data> secondDataList = secondSelectorGenerator.getSecondSelectorData(defaultValue);
        selectSecond.setComboDataList(secondDataList);
    }

    public static interface SecondSelectorGenerator{
        List<DropDownBox.Data> getSecondSelectorData(String firstValue);
    }
    
    public int getFirstComboWidth() {
        return firstComboWidth;
    }
    
    public void setFirstComboWidth(int firstComboWidth) {
        this.firstComboWidth = firstComboWidth;
    }
    
    public int getComboRowWidth() {
        return comboRowWidth;
    }
    
    public void setComboRowWidth(int comboRowWidth) {
        this.comboRowWidth = comboRowWidth;
    }
    
    public int getContentWidth() {
        return contentWidth;
    }
    
    public void setContentWidth(int contentWidth) {
        this.contentWidth = contentWidth;
    }

    public int getContentHeight() {
        return contentHeight;
    }
    
    public void setContentHeight(int contentHeight) {
        this.contentHeight = contentHeight;
    }
    
    public Composite getContentComposite() {
        return contentComposite;
    }
    
    public DefinedSingleSelect getSelectFirst() {
        return selectFirst;
    }
    
    public DefinedSingleSelect getSelectSecond() {
        return selectSecond;
    }
    
    public void setFirstSelectListener(Listener firstSelectListener) {
        this.firstSelectListener = firstSelectListener;
    }
    
    public void setSecondSelectListener(Listener secondSelectListener) {
        this.secondSelectListener = secondSelectListener;
    }
    
    public void setDefaultValue(DropDownBox.Data defaultValue) {
        this.defaultValue = defaultValue;
    }
    
    public void setSecondSelectorGenerator(SecondSelectorGenerator secondSelectorGenerator) {
        this.secondSelectorGenerator = secondSelectorGenerator;
    }

    public void setSecondDefaultValue(DropDownBox.Data secondDefaultValue) {
        this.secondDefaultValue = secondDefaultValue;
    }
}

4.5

package com.view.control.select;

import java.util.List;

import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;

import com.view.control.DefinedControl;
import com.view.control.DefinedText;
import com.view.swt.SWTResourceManager;
import com.view.swt.SWTUtil;

/**
 * <p>选择联动编辑框(parent可以不是GridLayout布局)</p>
 * @version V1.0
 */
public class DefinedSelectedLinkageEditor extends DefinedControl{
    /****内容容器*****/
    private Composite contentComposite;
    private DefinedSingleSelect selectFirst;
    /****编辑框*****/
    private DefinedText editor;
    private int contentWidth = 323;
    /****选择下拉框内容显示的Label的宽度,那么编辑框的宽度为 contentWidth - comboWidth*****/
    private int comboWidth = 100;
    /**********/
    private int contentHeight = 32;
    /*****编辑框中默认显示的内容*****/
    private String defaultTextMention;
    /*****下拉选择默认选择的值****/
    private DropDownBox.Data defaultComoValue;
    /*****编辑框中默认值*******/
    private String defaultTextValue;
    private List<DropDownBox.Data> comboDataList; 
    private int comboRowWidth;
    /***选中第一个下拉框第一个值发生的事件*****/
    private Listener firstSelectItemListener;
    
    
    public DefinedSelectedLinkageEditor(Composite parent, List<DropDownBox.Data> comboDataList, int comboRowWidth) {
        super(parent);
        this.comboDataList = comboDataList;
        this.comboRowWidth = comboRowWidth;
    }
    
    public DefinedSelectedLinkageEditor(Composite parent,List<DropDownBox.Data> comboDataList,int contentWidth,int comboWidth,int contentHeight) {
        this(parent,comboDataList,comboWidth);
        this.comboWidth = comboWidth;
        this.contentWidth = contentWidth;
        this.contentHeight = contentHeight;
    }
    
    @Override
    public void paint(){
        contentComposite = new Composite(this.parent,SWT.NONE);
        contentComposite.setBounds(0, 0, this.contentWidth, this.contentHeight);
        SWTUtil.paintBorder(contentComposite, SWTResourceManager.getControlBorderColor());
        contentComposite.setBackground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
        GridLayout gl_contentComposite = new GridLayout(2,false);
        gl_contentComposite.horizontalSpacing = 0;
        gl_contentComposite.verticalSpacing = 0;
        gl_contentComposite.marginHeight = 0;
        gl_contentComposite.marginWidth = 0;
        contentComposite.setLayout(gl_contentComposite);
        
        selectFirst = new DefinedSingleSelect(contentComposite,this.comboDataList,this.comboWidth,this.contentHeight);
        if(null != this.defaultComoValue){
            selectFirst.setDefaultValue(this.defaultComoValue);
        }
        selectFirst.setComboRowWidth(comboRowWidth);
        selectFirst.addSelectListener(new Listener(){
            @Override
            public void handleEvent(Event event) {
                editor.getContentText().setText("");
            }
        });
        if(null != firstSelectItemListener){
            selectFirst.addSelectListener(firstSelectItemListener);
        }
        selectFirst.paint();
        
        Composite composite = new Composite(contentComposite,SWT.NONE);
        GridData gd_composite = new GridData(SWT.RIGHT, SWT.FILL, false, true, 1, 1);
        gd_composite.widthHint = this.contentWidth - this.comboWidth;
        composite.setLayoutData(gd_composite);
        editor = new DefinedText(composite,SWT.NONE);
        editor.setTextHeight(this.contentHeight);
        editor.setTextWidth(this.contentWidth - this.comboWidth);
        editor.setDefalutValue(this.defaultTextValue);
        editor.setDefaultTextMention(defaultTextMention);
        editor.paint();
        
        contentComposite.layout(true);
    }

    public int getComboWidth() {
        return comboWidth;
    }

    public void setComboWidth(int comboWidth) {
        this.comboWidth = comboWidth;
    }

    public int getComboRowWidth() {
        return comboRowWidth;
    }

    public void setComboRowWidth(int comboRowWidth) {
        this.comboRowWidth = comboRowWidth;
    }

    public DefinedSingleSelect getSelectFirst() {
        return selectFirst;
    }

    public DefinedText getEditor() {
        return editor;
    }

    public void setContentWidth(int contentWidth) {
        this.contentWidth = contentWidth;
    }

    public void setContentHeight(int contentHeight) {
        this.contentHeight = contentHeight;
    }

    public void setDefaultTextMention(String defaultTextMention) {
        this.defaultTextMention = defaultTextMention;
    }

    public void setDefaultComoValue(DropDownBox.Data defaultComoValue) {
        this.defaultComoValue = defaultComoValue;
    }
    public void setDefaultComoValue(String defaultComoValue) {
        this.defaultComoValue = new DropDownBox.Data(defaultComoValue,defaultComoValue);
    }

    public void setDefaultTextValue(String defaultTextValue) {
        this.defaultTextValue = defaultTextValue;
    }

    public void setComboDataList(List<DropDownBox.Data> comboDataList) {
        this.comboDataList = comboDataList;
    }
    
    public void dispose(){
        this.contentComposite.dispose();
    }

    public void setFirstSelectItemListener(Listener firstSelectItemListener) {
        this.firstSelectItemListener = firstSelectItemListener;
    }
}

 

以上是关于自定义SWT控件四之其它下拉框的主要内容,如果未能解决你的问题,请参考以下文章

自定义SWT控件二之自定义多选下拉框

自定义SWT控件三之搜索功能下拉框

自定义SWT控件五之自定义穿梭框

SWT 按钮下拉控件

swt,jface,rcp

如何打造Android自定义的下拉列表框控件