java swt 下拉列表_自定义SWT控件四之其它下拉框

packagecom.view.control.select;importjava.util.ArrayList;importjava.util.List;importorg.eclipse.swt.SWT;importorg.eclipse.swt.custom.CLabel;importorg.eclipse.swt.custom.ScrolledComposite;importorg.eclipse.swt.graphics.Rectangle;importorg.eclipse.swt.layout.GridData;importorg.eclipse.swt.layout.GridLayout;importorg.eclipse.swt.widgets.Composite;importorg.eclipse.swt.widgets.Event;importorg.eclipse.swt.widgets.Label;importorg.eclipse.swt.widgets.Listener;importorg.eclipse.swt.widgets.Shell;importcom.global.constant.Constants;importcom.util.CollectionUtil;importcom.util.FileUtil;importcom.util.StringUtil;importcom.view.control.DefinedControl;importcom.view.control.select.DefinedCommonSingleSelect.DefinedCommonSingleSelectEvent;importcom.view.control.select.DropDownBox.Data;importcom.view.swt.SWTResourceManager;importcom.view.swt.StyleTextLink;importcom.view.util.ImageUtil;/***

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

*

* 第一级为单选框(可带搜索功能)

* 第二级为单选框(可带搜索功能)

*

*

* 区分第一级拉框是否是带搜索功能,取决于firstSearchEvent 变量是否为null,不为null表示带搜索功能

* 区分第二级拉框是否是带搜索功能,取决于secondSearchEvent 变量是否为null,不为null表示带搜索功能

*

*

* 第一级下拉框可以通过 listener 方法 event的widget 使用getData("firstSelect") 获取下拉框本身

* 第二级下拉框可以通过 listener 方法 event的widget 使用getData("firstSelect") 获取第一个下拉框对象,通过getData("secondSelect")获取第二个下拉框本身

* deleteBtnListener 通用event的widget使用getData("firstSelect") 获取第一个下拉框对象,通过getData("secondSelect")获取第二个下拉框

*

*@versionV1.0*/

public class DefinedFromAddLinkageSingleSelect extendsDefinedControl {/****内容容器*****/

privateComposite addComposite;/****显示名称控件****/

privateCLabel name;/****选中内容显示的文本区域 + 下拉图标 总宽度(两级)****/

private int chooseWidth = 323;private int chooseHeight = 32;/*****显示名称**********/

privateString nameText;/*****设置显示名称控件的宽度*****/

private int nameWidth = 100;/****每个下拉框的中每行的宽度****/

private intcomboRowWidth;private int comboRowHeight = 0;privateLabel addImg ;/*****点击第二级下拉中的小删除按钮发生的额外事件*****/

privateListener deleteBtnListener;/****联动事件*****/

privateDefinedFromAddLinkageSingleSelectEvent linkageSelectEvent;/****是否将value也显示在下拉框中,呈现效果为(display(value))****/

private booleanshowValue;private List addRowCompositeList = new ArrayList<>();//****以下为第一级下拉框不带搜索功能需要使用的变量***************// /****第一级下拉框选择范围内容(若第一级下拉框带搜索功能,不需要传递该值,应实现搜索方法,通过搜索方法填充第一级下拉框选择范围)***/

private List firstSelectedList = new ArrayList();//****结束**************///****以下为第一级下拉框带搜索功能的用法*****// /***是否异步分页加载**/

private booleanfirstAsynchronous;/***搜索框中的默认提示*****/

privateString firstDefaultMultiSearchMentionHint;/**链接事件******/

privateStyleTextLink.StyleTextLinkEvent firstLinkEvent;privateDefinedSearchSingleSelect.SearchSingleSelectEvent firstSearchEvent;//************结束*********************//

//*******第一级下拉框公用变量***********// /****第一级搜索框默认内容********/

private ListfirstDefaultSelectValueList;/****选中第一级搜索框触发的事件*******/

privateListener firsetSelectListener;privateDefinedCommonSingleSelectEvent firstDropdownBeforeEvent;//************结束*********************//

//****以下为第二级下拉框带搜索功能的用法*****// /*** 是否异步分页加载 **/

private booleansecondAsynchronous;/*** 搜索框中的默认提示 *****/

privateString secondDefaultMultiSearchMentionHint;/**链接事件 ******/

privateStyleTextLink.StyleTextLinkEvent secondLinkEvent;privateDefinedSearchSingleSelect.SearchSingleSelectEvent secondSearchEvent;//************结束*********************//

//*******第二级下拉框公用变量***********// /**** 选中第二级搜索框触发的事件 *******/

privateListener secondSelectListener;privateDefinedCommonSingleSelectEvent secondDropdownBeforeEvent;//************结束*********************//

public DefinedFromAddLinkageSingleSelect(Composite parent, String nameText, intchooseWidth,

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, intchooseWidth,intchooseHeight, DefinedFromAddLinkageSingleSelectEvent linkageSelectEvent) {this(parent, nameText, chooseWidth, linkageSelectEvent);this.nameWidth =nameTextWidth;this.chooseHeight =chooseHeight;

}

@Overridepublic voidpaint() {

generateAddComposite(this.parent);

}private voidgenerateAddComposite(Composite contentComposite){/****显示添加一行(第一行24像素高,后面每一行 chooseHeight高)****/addComposite= newComposite(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= newCLabel(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= newLabel(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,newListener(){

@Overridepublic voidhandleEvent(Event event) {

generateAddRowComposite(addComposite,null);

reLayout();

}

});if(CollectionUtil.isNotEmpty(firstDefaultSelectValueList)){for(DropDownBox.Data firstDefaultSelectValue:firstDefaultSelectValueList){

generateAddRowComposite(addComposite,firstDefaultSelectValue);

}

}

reLayout();

}/*** 绘制每行联动下拉框*@paramcontentComposite

*@paramdefaultValue*/

private voidgenerateAddRowComposite(Composite contentComposite,DropDownBox.Data defaultValue){

Composite addRowComposite= newComposite(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= newCLabel(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 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 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 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 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= newLabel(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,newListener(){

@Overridepublic voidhandleEvent(Event event) {

addRowComposite.dispose();

addRowCompositeList.remove(addRowComposite);

reLayout();

}

});

addRowComposite.setData("firstSelect", firstSelector);

addRowComposite.setData("secondSelect", secondSelector);

Label mentionLabel= newLabel(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 voidreLayout(){

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 instanceofShell)){

parentComposite.layout(true);

contentComposite=parentComposite;

parentComposite=parentComposite.getParent();

}if(parentComposite instanceofScrolledComposite){

((ScrolledComposite)parentComposite).setMinSize(contentComposite.computeSize(SWT.DEFAULT, SWT.DEFAULT));

}

}/***

联动事件

*@versionV1.0*/

public interfaceDefinedFromAddLinkageSingleSelectEvent{/*****根据第一个下拉框选择的内容动态生成第二个框选择的内容(仅在第二个下拉框不带搜索功能时需要实现该方法)******/ListgetSecondData(DropDownBox.Data firstValue);/*****根据第一个获取其默认已经配置的内容*****/DropDownBox.Data getSecondDefaultValue(DropDownBox.Data firstValue);

}public intgetChooseWidth() {returnchooseWidth;

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

}public intgetChooseHeight() {returnchooseHeight;

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

}public intgetNameWidth() {returnnameWidth;

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

}public intgetComboRowWidth() {returncomboRowWidth;

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

}public intgetComboRowHeight() {returncomboRowHeight;

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

}public booleanisShowValue() {returnshowValue;

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

}public ListgetFirstSelectedList() {returnfirstSelectedList;

}public void setFirstSelectedList(ListfirstSelectedList) {this.firstSelectedList =firstSelectedList;

}public booleanisFirstAsynchronous() {returnfirstAsynchronous;

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

}publicString getFirstDefaultMultiSearchMentionHint() {returnfirstDefaultMultiSearchMentionHint;

}public voidsetFirstDefaultMultiSearchMentionHint(String firstDefaultMultiSearchMentionHint) {this.firstDefaultMultiSearchMentionHint =firstDefaultMultiSearchMentionHint;

}public ListgetFirstDefaultSelectValueList() {returnfirstDefaultSelectValueList;

}public void setFirstDefaultSelectValueList(ListfirstDefaultSelectValueList) {this.firstDefaultSelectValueList =firstDefaultSelectValueList;

}public booleanisSecondAsynchronous() {returnsecondAsynchronous;

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

}publicString getSecondDefaultMultiSearchMentionHint() {returnsecondDefaultMultiSearchMentionHint;

}public voidsetSecondDefaultMultiSearchMentionHint(String secondDefaultMultiSearchMentionHint) {this.secondDefaultMultiSearchMentionHint =secondDefaultMultiSearchMentionHint;

}publicListener getSecondSelectListener() {returnsecondSelectListener;

}public voidsetSecondSelectListener(Listener secondSelectListener) {this.secondSelectListener =secondSelectListener;

}public voidsetNameText(String nameText) {this.nameText =nameText;

}public voidsetDeleteBtnListener(Listener deleteBtnListener) {this.deleteBtnListener =deleteBtnListener;

}public voidsetLinkageSelectEvent(DefinedFromAddLinkageSingleSelectEvent linkageSelectEvent) {this.linkageSelectEvent =linkageSelectEvent;

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

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

}public voidsetFirsetSelectListener(Listener firsetSelectListener) {this.firsetSelectListener =firsetSelectListener;

}public voidsetFirstDropdownBeforeEvent(DefinedCommonSingleSelectEvent firstDropdownBeforeEvent) {this.firstDropdownBeforeEvent =firstDropdownBeforeEvent;

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

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

}public voidsetSecondDropdownBeforeEvent(DefinedCommonSingleSelectEvent secondDropdownBeforeEvent) {this.secondDropdownBeforeEvent =secondDropdownBeforeEvent;

}public ListgetAddRowCompositeList() {returnaddRowCompositeList;

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值