2、自定义下拉多选框

  1. package com.view.control.select;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.HashMap;
  5. import java.util.LinkedHashMap;
  6. import java.util.List;
  7. import java.util.Map;
  8.  
  9. import org.eclipse.swt.SWT;
  10. import org.eclipse.swt.custom.CLabel;
  11. import org.eclipse.swt.custom.ScrolledComposite;
  12. import org.eclipse.swt.events.DisposeEvent;
  13. import org.eclipse.swt.events.DisposeListener;
  14. import org.eclipse.swt.graphics.Image;
  15. import org.eclipse.swt.graphics.Rectangle;
  16. import org.eclipse.swt.layout.GridData;
  17. import org.eclipse.swt.layout.GridLayout;
  18. import org.eclipse.swt.widgets.Composite;
  19. import org.eclipse.swt.widgets.Control;
  20. import org.eclipse.swt.widgets.Event;
  21. import org.eclipse.swt.widgets.Label;
  22. import org.eclipse.swt.widgets.Listener;
  23. import org.eclipse.swt.widgets.Shell;
  24.  
  25. import com.global.constant.Constants;
  26. import com.util.CollectionUtil;
  27. import com.util.FileUtil;
  28. import com.view.control.DefinedControl;
  29. import com.view.swt.SWTResourceManager;
  30. import com.view.swt.SWTUtil;
  31. import com.view.util.ImageUtil;
  32.  
  33. /**
  34. * <p>
  35. * 多选下拉框的通用逻辑
  36. * </p>
  37. * @version V1.0
  38. */
  39. public class DefinedCommonMultiSelect extends DefinedControl {
  40.  
  41. /**** 其父框需要是gridLaout布局 *****/
  42. private Composite choseMaxComposite;
  43. /**** 选中内容显示的文本区域容器 *****/
  44. private Composite chooseComposite;
  45. /*** 下一个item所在的x坐标 ***/
  46. private int chooseItemNextX = 0;
  47. /*** 下一个item所在的y坐标 ******/
  48. private int chooseItemNextY = 0;
  49. private List<DropDownBox.Data> selectedList = new ArrayList<DropDownBox.Data>();
  50. /*** 选中内容显示的文本区域实际的宽度 ****/
  51. private int chooseRealWidth;
  52. private Map<DropDownBox.Data, Composite> selectedCompositeMap = new LinkedHashMap<DropDownBox.Data, Composite>();
  53. private Map<DropDownBox.Data, Label> comboImg = new HashMap<DropDownBox.Data, Label>();
  54. /**** 选中内容显示的文本区域 + 下拉图标 总宽度 ****/
  55. private int chooseWidth = 323;
  56. /**** 内容框中的每项的高度 *****/
  57. private int chooseItemHeight = 24;
  58. /**** 默认值 ***/
  59. private List<DropDownBox.Data> defaultValueList = new ArrayList<DropDownBox.Data>();
  60. private List<DefinedCommonMultiSelectEvent> itemHandlerListener;
  61. private Label img;
  62. private DropDownBox<DropDownBox.Data> dropDownBox;
  63. private Image CHECKBOX_NOR_ICON = ImageUtil.getImage(FileUtil.loadResourceFileAsStream(Constants.CHECKBOX_NOR_ICON));
  64. private Image CHECKBOX_SELF_ICON = ImageUtil.getImage(FileUtil.loadResourceFileAsStream(Constants.CHECKBOX_SELF_ICON));
  65.  
  66. public DefinedCommonMultiSelect(Composite parent, DropDownBox<DropDownBox.Data> dropDownBox) {
  67. super(parent);
  68. this.dropDownBox = dropDownBox;
  69. }
  70.  
  71. public DefinedCommonMultiSelect(Composite parent,int chooseWidth, int chooseItemHeight,
  72. DropDownBox<DropDownBox.Data> dropDownBox) {
  73. this(parent, dropDownBox);
  74. this.chooseWidth = chooseWidth;
  75. this.chooseItemHeight = chooseItemHeight;
  76. }
  77.  
  78. @Override
  79. public void paint() {
  80. this.chooseComposite = generateComposite(this.parent);
  81. }
  82.  
  83. /**
  84. * 创建显示下拉框的内容
  85. * @param contentComposite
  86. * @return
  87. */
  88. protected Composite generateComposite(Composite contentComposite) {
  89. choseMaxComposite = new Composite(contentComposite, SWT.NONE);
  90. GridData gd_contentComposite = new GridData(SWT.FILL, SWT.FILL, false, false, 1, 1);
  91. gd_contentComposite.widthHint = this.chooseWidth;
  92. choseMaxComposite.setLayoutData(gd_contentComposite);
  93. GridLayout grid = new GridLayout(2, false);
  94. grid.horizontalSpacing = 0;
  95. grid.marginHeight = 1;
  96. grid.marginWidth = 1;
  97. choseMaxComposite.setLayout(grid);
  98. choseMaxComposite.setBackground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
  99. SWTUtil.paintBorder(choseMaxComposite, SWTResourceManager.getColor(229, 229, 229));
  100. dropDownBox.setContentComposite(choseMaxComposite);
  101.  
  102. Composite text_container_composite = new Composite(choseMaxComposite, SWT.NONE);
  103. GridData gd_text_container_composite = new GridData(SWT.FILL, SWT.FILL, true, false, 1, 1);
  104. text_container_composite.setLayoutData(gd_text_container_composite);
  105. text_container_composite.setBackground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
  106. grid = new GridLayout(1, true);
  107. grid.horizontalSpacing = 0;
  108. grid.marginHeight = 2;
  109. grid.marginWidth = 2;
  110. text_container_composite.setLayout(grid);
  111.  
  112. Composite text_composite = new Composite(text_container_composite, SWT.NONE);
  113. text_composite.setData("rootParent", this.parent);
  114. GridData gd_text_composite = new GridData(SWT.FILL, SWT.FILL, false, false, 1, 1);
  115. chooseRealWidth = this.chooseWidth - 6 - 24;
  116. gd_text_composite.widthHint = chooseRealWidth;
  117. gd_text_composite.heightHint = this.chooseItemHeight + 4;
  118. text_composite.setLayoutData(gd_text_composite);
  119. text_composite.setBackground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
  120.  
  121. if (CollectionUtil.isNotEmpty(defaultValueList)) {
  122. List<DropDownBox.Data> dataList = new ArrayList<>();
  123. for (DropDownBox.Data defaultValue: defaultValueList) {
  124. addUniqueNickName(dataList,defaultValue);
  125. dataList.add(defaultValue);
  126. generateChooseItemComposite(text_composite, defaultValue);
  127. }
  128. }
  129. img = new Label(choseMaxComposite, SWT.NONE);
  130. GridData gd_img = new GridData(SWT.LEFT, SWT.CENTER, false, false, 1, 1);
  131. gd_img.widthHint = 24;
  132. gd_img.heightHint = 24;
  133. img.setLayoutData(gd_img);
  134. img.setImage(ImageUtil.getImage(FileUtil.loadResourceFileAsStream(Constants.ARROW_DOWN)));
  135. img.setBackground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
  136. img.setCursor(SWTResourceManager.getCursor(SWT.CURSOR_HAND));
  137. img.addListener(SWT.MouseDown, new Listener() {
  138. @Override
  139. public void handleEvent(Event event) {
  140. Composite comboComposite = dropDownBox.getComboComposite();
  141. if(comboComposite !=null && !comboComposite.isDisposed()){
  142. dropDownBox.comboDispose();
  143. }else{
  144. dropDownBox.comboPaint();
  145. }
  146. }
  147. });
  148. choseMaxComposite.layout(true);
  149. choseMaxComposite.addDisposeListener(new DisposeListener() {
  150. @Override
  151. public void widgetDisposed(DisposeEvent e) {
  152. dropDownBox.dispose();
  153. }
  154. });
  155. return text_composite;
  156. }
  157.  
  158. /**
  159. * 重命名data的昵称
  160. * @param dataList
  161. * @param defaultValue
  162. * @return
  163. */
  164. private boolean addUniqueNickName(List<DropDownBox.Data> dataList,DropDownBox.Data defaultValue){
  165. if(dataList.contains(defaultValue)){
  166. dataList.forEach(data->{
  167. if(data.getValue().equals(defaultValue.getValue()) && data.getNickname()>defaultValue.getNickname()) {
  168. defaultValue.setNickname(data.getNickname());
  169. }
  170. });
  171. defaultValue.setNickname(defaultValue.getNickname()+1);
  172. return true;
  173. }
  174. return false;
  175. }
  176.  
  177. public Label getImg() {
  178. return img;
  179. }
  180.  
  181. protected void generateChooseItemComposite(DropDownBox.Data data) {
  182. Composite containerComposite = generateChooseItemComposite(chooseComposite, data);
  183. Composite parentComposite = containerComposite.getParent();
  184. parentComposite.layout(true);
  185. parentComposite.getParent().layout(true);
  186. parentComposite.getParent().getParent().layout(true);
  187. parentComposite.getParent().getParent().getParent().layout(true);
  188. }
  189.  
  190. /**
  191. * 生成一个chooseItemComposite
  192. * @param text_composite
  193. * @return
  194. */
  195. private Composite generateChooseItemComposite(Composite text_composite, DropDownBox.Data data) {
  196. Composite containerComposite = new Composite(text_composite, SWT.NONE);
  197. containerComposite.setData("rootParent", this.parent);
  198. GridLayout gl_containerComposite = new GridLayout(1, true);
  199. gl_containerComposite.marginHeight = 2;
  200. gl_containerComposite.marginWidth = 2;
  201. gl_containerComposite.horizontalSpacing = 0;
  202. containerComposite.setLayout(gl_containerComposite);
  203. containerComposite.setBackground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
  204.  
  205. Composite itemComposite = new Composite(containerComposite, SWT.NONE);
  206. itemComposite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
  207. itemComposite.setBackground(SWTResourceManager.getColor(245, 245, 245));
  208. SWTUtil.paintBorder(itemComposite, SWTResourceManager.getBorderColor());
  209. GridLayout gl_itemComposite = new GridLayout(2, false);
  210. gl_itemComposite.marginHeight = 1;
  211. gl_itemComposite.marginWidth = 3;
  212. gl_itemComposite.horizontalSpacing = 2;
  213. itemComposite.setLayout(gl_itemComposite);
  214. itemComposite.setData("value", data);
  215.  
  216. CLabel label = new CLabel(itemComposite, SWT.NONE);
  217. label.setLayoutData(new GridData(SWT.FILL, SWT.FILL, false, true, 1, 1));
  218. label.setBackground(SWTResourceManager.getColor(245, 245, 245));
  219. label.setText(data.getDisplay());
  220. label.setAlignment(SWT.CENTER);
  221. label.setForeground(SWTResourceManager.getColor(51, 51, 51));
  222.  
  223. Label img = new Label(itemComposite, SWT.NONE);
  224. GridData gd_img = new GridData(SWT.CENTER, SWT.CENTER, false, false, 1, 1);
  225. gd_img.widthHint = 16;
  226. gd_img.heightHint = 16;
  227. img.setLayoutData(gd_img);
  228. img.setText("X");
  229. img.setBackground(SWTResourceManager.getColor(245, 245, 245));
  230. img.setCursor(SWTResourceManager.getCursor(SWT.CURSOR_HAND));
  231. img.addListener(SWT.MouseDown, new Listener() {
  232. @Override
  233. public void handleEvent(Event event) {
  234. disposeChooseItemComposite(data);
  235. }
  236. });
  237. itemComposite.layout(true);
  238.  
  239. int containerCompositeWidth = 24 + label.getBounds().width + 4;
  240. boolean change = false;
  241. containerComposite.setSize(containerCompositeWidth, this.chooseItemHeight + 4);
  242. if ((this.chooseRealWidth - this.chooseItemNextX - containerCompositeWidth) < 0) {
  243. this.chooseItemNextY += this.chooseItemHeight + 4;
  244. this.chooseItemNextX = 0;
  245. change = true;
  246. }
  247. containerComposite.setLocation(this.chooseItemNextX, this.chooseItemNextY);
  248. this.chooseItemNextX += containerCompositeWidth;
  249.  
  250. this.selectedList.add(data);
  251. this.selectedCompositeMap.put(data, containerComposite);
  252. containerComposite.layout(true);
  253. if (change) {
  254. ((GridData)text_composite.getLayoutData()).heightHint = this.chooseItemNextY + this.chooseItemHeight + 4;
  255. reLayoutAllParent(text_composite);
  256. dropDownBox.reLocation();
  257. }
  258.  
  259. if (CollectionUtil.isNotEmpty(itemHandlerListener)) {
  260. for (DefinedCommonMultiSelectEvent event : itemHandlerListener) {
  261. event.addItemEvent(data, containerComposite);
  262. }
  263. }
  264. return containerComposite;
  265. }
  266.  
  267. /**
  268. * 销毁一个选中文本框中的一项(同时将其在下拉框中的项置为未选中状态)
  269. * @param data
  270. */
  271. protected void disposeChooseItemComposite(DropDownBox.Data data) {
  272. selectedList.remove(data);
  273. Composite containerComposite = selectedCompositeMap.get(data);
  274. Composite beforeComposite = null;
  275. Composite beforecontainerComposite = null;
  276. Rectangle bounds;
  277. int nextX = containerComposite.getBounds().x;
  278. int nextY = containerComposite.getBounds().y;
  279. boolean deleteLast = true; // 要删除的是最后一个
  280. for (Composite leftContainerComposite : selectedCompositeMap.values()) {
  281. if (leftContainerComposite == containerComposite) {
  282. beforeComposite = leftContainerComposite;
  283. continue;
  284. }
  285. if (null != beforeComposite) {
  286. deleteLast = false;
  287. if ((this.chooseRealWidth - nextX - leftContainerComposite.getBounds().width) < 0) {
  288. leftContainerComposite.setLocation(0, nextY + this.chooseItemHeight + 4);
  289. } else {
  290. leftContainerComposite.setLocation(nextX, nextY);
  291. }
  292. leftContainerComposite.layout(true);
  293. bounds = leftContainerComposite.getBounds();
  294. nextX = bounds.x + bounds.width;
  295. nextY = bounds.y;
  296. beforeComposite = leftContainerComposite;
  297. } else {
  298. beforecontainerComposite = leftContainerComposite;
  299. }
  300. }
  301. boolean change = false;
  302. selectedCompositeMap.remove(data);
  303. if (deleteLast) {
  304. if (beforecontainerComposite == null) {
  305. nextX = 0;
  306. nextY = 0;
  307. } else {
  308. nextX = beforecontainerComposite.getBounds().x + beforecontainerComposite.getBounds().width;
  309. nextY = beforecontainerComposite.getBounds().y;
  310. }
  311. }
  312. chooseItemNextX = nextX;
  313. if (chooseItemNextY != nextY) {
  314. change = true;
  315. }
  316. chooseItemNextY = nextY;
  317. Composite parentComposite = containerComposite.getParent();
  318. Label imgLabel = comboImg.get(data);
  319. if(imgLabel != null && !imgLabel.isDisposed()){
  320. changeUnSelectCheckBox(imgLabel);
  321. }else{
  322. comboImg.remove(data);
  323. }
  324. containerComposite.dispose();
  325. if (change) {
  326. if (0 == chooseItemNextY) {
  327. ((GridData)parentComposite.getLayoutData()).heightHint = this.chooseItemHeight + 4;
  328. } else
  329. ((GridData)parentComposite.getLayoutData()).heightHint = chooseItemNextY + this.chooseItemHeight + 4;
  330. reLayoutAllParent(parentComposite);
  331. dropDownBox.reLocation();
  332. }
  333. if (CollectionUtil.isNotEmpty(itemHandlerListener)) {
  334. for (DefinedCommonMultiSelectEvent event : itemHandlerListener) {
  335. event.disposeItemEvent(data, parentComposite);
  336. }
  337. }
  338. }
  339.  
  340. /**
  341. * 获取当前的高度
  342. * @return
  343. */
  344. protected int getHeight() {
  345. return this.chooseItemNextY + this.chooseItemHeight + 4 + 6;
  346. }
  347.  
  348. protected Composite getChoseMaxComposite() {
  349. return choseMaxComposite;
  350. }
  351.  
  352. /**
  353. * 所有的重新绘制
  354. * @param composite
  355. */
  356. private void reLayoutAllParent(Composite composite) {
  357. Composite contentComposite = composite;
  358. while(contentComposite != this.parent){
  359. contentComposite.layout(true);
  360. contentComposite = contentComposite.getParent();
  361. }
  362. contentComposite.layout(true);
  363. Composite parentComposite = contentComposite.getParent();
  364. while(!(parentComposite instanceof ScrolledComposite) && !(parentComposite instanceof Shell)){
  365. parentComposite.layout(true);
  366. contentComposite = parentComposite;
  367. parentComposite = parentComposite.getParent();
  368. }
  369. if(parentComposite instanceof ScrolledComposite){
  370. ((ScrolledComposite)parentComposite).setMinSize(contentComposite.computeSize(SWT.DEFAULT, SWT.DEFAULT));
  371. }
  372. }
  373.  
  374. /**
  375. * 将checkbox变成选中状态
  376. * @param img
  377. */
  378. protected void changeSelectCheckBox(Label img) {
  379. if(img != null && !img.isDisposed()){
  380. img.setImage(CHECKBOX_SELF_ICON);
  381. img.setData("checked", true);
  382. }
  383. }
  384.  
  385. /**
  386. * 将checkbox变成未选中状态
  387. * @param img
  388. */
  389. protected void changeUnSelectCheckBox(Label img) {
  390. if(null != img && !img.isDisposed()){
  391. img.setImage(CHECKBOX_NOR_ICON);
  392. img.setData("checked", false);
  393. }
  394. }
  395.  
  396. /**
  397. * 销毁掉所有已选择的内容*/
  398. protected void disposeAllChooseItem() {
  399. while ((CollectionUtil.isNotEmpty(this.selectedList))) {
  400. this.disposeChooseItemComposite(this.selectedList.get(0));
  401. }
  402. }
  403.  
  404. private void changeItemSelection(CLabel itemLabel) {
  405. itemLabel.getParent().setBackground(SWTResourceManager.getColor(226, 235, 255));
  406. Control[] children = itemLabel.getParent().getChildren();
  407. for (Control child : children) {
  408. child.setBackground(SWTResourceManager.getColor(226, 235, 255));
  409. }
  410. }
  411.  
  412. protected void generateComboItemComposite(DropDownBox.Data data, Composite itemComposite) {
  413. GridLayout gl_itemComposite = new GridLayout(2,false);
  414. gl_itemComposite.verticalSpacing = 0;
  415. gl_itemComposite.horizontalSpacing = 5;
  416. itemComposite.setLayout(gl_itemComposite);
  417. Label img = new Label(itemComposite, SWT.NONE);
  418. GridData gd_img = new GridData(SWT.FILL,SWT.FILL,false,false,1,1);
  419. gd_img.widthHint = 24;
  420. gd_img.heightHint = 24;
  421. img.setLayoutData(gd_img);
  422. img.setData("value", data);
  423. if (this.selectedList.contains(data)) {
  424. changeSelectCheckBox(img);
  425. } else {
  426. changeUnSelectCheckBox(img);
  427. }
  428. img.setBackground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
  429. img.setCursor(SWTResourceManager.getCursor(SWT.CURSOR_HAND));
  430.  
  431. img.addListener(SWT.MouseDown, new Listener() {
  432.  
  433. @Override
  434. public void handleEvent(Event event) {
  435. Label img = (Label)event.widget;
  436. boolean select = (boolean)img.getData("checked");
  437. DropDownBox.Data data = (DropDownBox.Data)img.getData("value");
  438. if (select) {
  439. disposeChooseItemComposite(data);
  440. } else {
  441. generateChooseItemComposite(data);
  442. changeSelectCheckBox(img);
  443. }
  444. }
  445. });
  446. addDropDownCheckBoxImg(data, img);
  447.  
  448. CLabel itemLabel = new CLabel(itemComposite, SWT.NONE);
  449. if(dropDownBox.showValue){
  450. itemLabel.setText(data.getDisplay()+"("+data.getValue()+")");
  451. }else
  452. itemLabel.setText(data.getDisplay());
  453. itemLabel.setData("value", data);
  454. itemLabel.setBackground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
  455. GridData gd_itemLabel= new GridData(SWT.FILL,SWT.FILL,true,true,1,1);
  456. itemLabel.setLayoutData(gd_itemLabel);
  457. itemLabel.setCursor(SWTResourceManager.getCursor(SWT.CURSOR_HAND));
  458. itemLabel.addListener(SWT.MouseEnter, new Listener() {
  459.  
  460. @Override
  461. public void handleEvent(Event event) {
  462. changeItemSelection(itemLabel);
  463. }
  464. });
  465. itemLabel.addListener(SWT.MouseExit, new Listener() {
  466.  
  467. @Override
  468. public void handleEvent(Event event) {
  469. changeItemUnSelection(itemLabel);
  470. }
  471. });
  472. itemLabel.addListener(SWT.MouseDown, new Listener() {
  473.  
  474. @Override
  475. public void handleEvent(Event event) {
  476. Event newEvent = new Event();
  477. newEvent.widget = img;
  478. img.notifyListeners(SWT.MouseDown, newEvent);
  479. }
  480. });
  481. itemComposite.layout(true);
  482. }
  483.  
  484. private void changeItemUnSelection(CLabel itemLabel) {
  485. itemLabel.getParent().setBackground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
  486. Control[] children = itemLabel.getParent().getChildren();
  487. for (Control child : children) {
  488. child.setBackground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
  489. }
  490. }
  491.  
  492. protected void addItemHandlerListener(DefinedCommonMultiSelectEvent itemHandlerListener) {
  493. if (null == this.itemHandlerListener) {
  494. this.itemHandlerListener = new ArrayList<DefinedCommonMultiSelectEvent>();
  495. }
  496. this.itemHandlerListener.add(itemHandlerListener);
  497. }
  498.  
  499. public Composite getChooseComposite() {
  500. return chooseComposite;
  501. }
  502.  
  503. protected List<DropDownBox.Data> getSelectedList() {
  504. return selectedList;
  505. }
  506.  
  507. protected void addDropDownCheckBoxImg(DropDownBox.Data data, Label img) {
  508. comboImg.put(data, img);
  509. }
  510.  
  511. public void setSelectedList(List<DropDownBox.Data> selectedList) {
  512. this.selectedList = selectedList;
  513. }
  514.  
  515. public int getChooseWidth() {
  516. return chooseWidth;
  517. }
  518.  
  519. public void setChooseWidth(int chooseWidth) {
  520. this.chooseWidth = chooseWidth;
  521. }
  522.  
  523. public int getChooseItemHeight() {
  524. return chooseItemHeight;
  525. }
  526.  
  527. public void setChooseItemHeight(int chooseItemHeight) {
  528. this.chooseItemHeight = chooseItemHeight;
  529. }
  530.  
  531. public void setDefaultValueList(List<DropDownBox.Data> defaultValueList) {
  532. this.defaultValueList = defaultValueList;
  533. }
  534.  
  535. /**
  536. * 获取已经选择的所有值
  537. */
  538. @Override
  539. public Object getValue() {
  540. // TODO Auto-generated method stub
  541. return this.selectedList;
  542. }
  543.  
  544. /**
  545. * <p>
  546. * 销毁和添加一个选项发生的事件(主要用于保存后台数据时使用)
  547. * </p>
  548. * @version V1.0
  549. */
  550. public interface DefinedCommonMultiSelectEvent {
  551.  
  552. /**
  553. *
  554. * @param data
  555. * @param text_composite 输入框控件,该控件data属性key"rootParent",可以拿到该行控件
  556. */
  557. void disposeItemEvent(DropDownBox.Data data, Composite text_composite);
  558.  
  559. /**
  560. *
  561. * @param data
  562. * @param text_composite 为输入框中的一个item,该控件data属性key"rootParent",可以拿到该行控件
  563. */
  564. void addItemEvent(DropDownBox.Data data, Composite text_composite);
  565. }
  566.  
  567. }

DefinedCommonMultiSelect.java 为自定义多选下拉框公共部分

  1. package com.view.control.select;
  2.  
  3. import java.util.List;
  4.  
  5. import org.eclipse.swt.widgets.Composite;
  6.  
  7. import com.view.control.select.DefinedCommonMultiSelect.DefinedCommonMultiSelectEvent;
  8.  
  9. /**
  10. * <p>自定义多选择框(parent必须是gridLayout布局)</p>
  11. * @version V1.0
  12. */
  13. public class DefinedMultiSelect extends DropDownBox<DropDownBox.Data> {
  14. private DefinedCommonMultiSelect commonMultiSelect;
  15.  
  16. public DefinedMultiSelect(Composite parent, List<DropDownBox.Data> comboDataList, int comboRowWidth) {
  17. super(parent, comboDataList, comboRowWidth);
  18. commonMultiSelect = new DefinedCommonMultiSelect(parent,this);
  19. }
  20. public DefinedMultiSelect(Composite parent,List<Data> comboDataList,int chooseWidth,int chooseItemHeight) {
  21. this(parent,comboDataList,chooseWidth);
  22. commonMultiSelect = new DefinedCommonMultiSelect(parent,chooseWidth,chooseItemHeight,this);
  23. }
  24.  
  25. public void paint(){
  26. commonMultiSelect.paint();
  27. }
  28.  
  29. @Override
  30. protected void generateComboItemComposite(Data data, Composite itemComposite) {
  31. commonMultiSelect.generateComboItemComposite(data,itemComposite);
  32. }
  33.  
  34. public void addItemHandlerListener(DefinedCommonMultiSelectEvent itemHandlerListener) {
  35. commonMultiSelect.addItemHandlerListener(itemHandlerListener);
  36. }
  37.  
  38. public List<Data> getSelectedList() {
  39. return commonMultiSelect.getSelectedList();
  40. }
  41.  
  42. public void setSelectedList(List<Data> selectedList) {
  43. commonMultiSelect.setSelectedList(selectedList);
  44. }
  45.  
  46. public int getChooseWidth() {
  47. return commonMultiSelect.getChooseWidth();
  48. }
  49.  
  50. public void setChooseWidth(int chooseWidth) {
  51. commonMultiSelect.setChooseWidth(chooseWidth);
  52. }
  53.  
  54. public int getChooseItemHeight() {
  55. return commonMultiSelect.getChooseItemHeight();
  56. }
  57.  
  58. public void setChooseItemHeight(int chooseItemHeight) {
  59. commonMultiSelect.setChooseItemHeight(chooseItemHeight);
  60. }
  61.  
  62. public Composite getChoseMaxComposite() {
  63. return commonMultiSelect.getChoseMaxComposite();
  64. }
  65.  
  66. public Composite getChooseComposite() {
  67. return commonMultiSelect.getChooseComposite();
  68. }
  69.  
  70. public void setDefaultValueList(List<DropDownBox.Data> defaultValueList) {
  71. commonMultiSelect.setDefaultValueList(defaultValueList);
  72. }
  73.  
  74. public int getHeight(){
  75. return commonMultiSelect.getHeight();
  76. }
  77.  
  78. public void disposeAllChooseItem(){
  79. commonMultiSelect.disposeAllChooseItem();
  80. }
  81.  
  82. /**
  83. * 获取选择的值
  84. */
  85. @SuppressWarnings("unchecked")
  86. public List<DropDownBox.Data> getValue(){
  87. return (List<DropDownBox.Data>) commonMultiSelect.getValue();
  88. }
  89. }

DefinedMultiSelect.java 多选下拉框(文本框 + 下拉弹出框)

  1. package com.view.control.select;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.List;
  5.  
  6. import org.eclipse.swt.SWT;
  7. import org.eclipse.swt.custom.CLabel;
  8. import org.eclipse.swt.custom.ScrolledComposite;
  9. import org.eclipse.swt.graphics.Rectangle;
  10. import org.eclipse.swt.layout.GridData;
  11. import org.eclipse.swt.layout.GridLayout;
  12. import org.eclipse.swt.widgets.Composite;
  13. import org.eclipse.swt.widgets.Event;
  14. import org.eclipse.swt.widgets.Label;
  15. import org.eclipse.swt.widgets.Listener;
  16. import org.eclipse.swt.widgets.Shell;
  17.  
  18. import com.global.constant.Constants;
  19. import com.util.CollectionUtil;
  20. import com.util.FileUtil;
  21. import com.util.StringUtil;
  22. import com.view.control.DefinedFormControl;
  23. import com.view.control.select.DefinedCommonMultiSelect.DefinedCommonMultiSelectEvent;
  24. import com.view.swt.SWTResourceManager;
  25. import com.view.util.ImageUtil;
  26.  
  27. /**
  28. * <p>多选下拉框不可编辑</p>
  29. * @version V1.0
  30. */
  31. public class DefinedFormMultiSelect extends DefinedFormControl{
  32. /****内容容器*****/
  33. private Composite contentComposite;
  34. /****显示名称控件****/
  35. private CLabel name;
  36. private DefinedMultiSelect multiSelect;
  37. /****选中内容显示的文本区域 + 下拉图标 总宽度****/
  38. private int chooseWidth = 323;
  39. /****内容框中的每项的高度*****/
  40. private int chooseItemHeight = 24;
  41. /*****显示名称**********/
  42. private String nameText;
  43. /*****设置显示名称控件的宽度*****/
  44. private int nameWidth = 100;
  45. /****默认值***/
  46. private List<DropDownBox.Data> defaultValueList = new ArrayList<>();
  47. private List<DropDownBox.Data> selectedList = new ArrayList<DropDownBox.Data>();
  48. private int comboRowWidth;
  49. private Listener helpListener;
  50. private Listener selectListener;
  51.  
  52. public Listener getSelectListener() {
  53. return selectListener;
  54. }
  55.  
  56. public void setSelectListener(Listener selectListener) {
  57. this.selectListener = selectListener;
  58. }
  59.  
  60. private List<DefinedCommonMultiSelectEvent> multiSelectEventList;
  61. private boolean showValue = false;
  62.  
  63. public DefinedFormMultiSelect(Composite parent,String nameText, List<DropDownBox.Data> comboDataList, int comboRowWidth) {
  64. super(parent);
  65. this.nameText = nameText;
  66. this.selectedList = comboDataList;
  67. this.comboRowWidth = comboRowWidth;
  68. }
  69.  
  70. public DefinedFormMultiSelect(Composite parent,String nameText,List<DropDownBox.Data> comboDataList,int chooseWidth,int nameWidth,int chooseItemHeight) {
  71. this(parent,nameText,comboDataList,chooseWidth);
  72. this.chooseWidth = chooseWidth;
  73. this.nameWidth = nameWidth;
  74. this.chooseItemHeight = chooseItemHeight;
  75. }
  76.  
  77. @Override
  78. public void paint() {
  79. contentComposite = new Composite(this.parent,SWT.NONE);
  80. GridData gd_contentComposite = new GridData(SWT.FILL, SWT.FILL, true, false, 1, 1);
  81. gd_contentComposite.widthHint = this.chooseWidth+nameWidth+2 + (this.helpListener != null? 24:0); //24为帮助图标宽度
  82. contentComposite.setLayoutData(gd_contentComposite);
  83. GridLayout gl_contentComposite = new GridLayout((this.helpListener != null? 4:3), false);
  84. gl_contentComposite.horizontalSpacing = 5;
  85. gl_contentComposite.verticalSpacing = 0;
  86. gl_contentComposite.marginHeight = 0;
  87. contentComposite.setLayout(gl_contentComposite);
  88. contentComposite.setBackground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
  89.  
  90. name = new CLabel(contentComposite,SWT.NONE);
  91. GridData gd_name = new GridData(SWT.RIGHT, SWT.FILL, false, true, 1, 1);
  92. gd_name.widthHint = nameWidth - 2;
  93. name.setLayoutData(gd_name);
  94. name.setAlignment(SWT.RIGHT);
  95. if(this.require){
  96. name.setImage(ImageUtil.getImage(FileUtil.loadResourceFileAsStream("images/asterisk.png")));
  97. }
  98. name.setText(nameText);
  99. name.setBackground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
  100.  
  101. multiSelect = new DefinedMultiSelect(contentComposite,this.selectedList,this.comboRowWidth);
  102. if(this.chooseWidth != 0){
  103. multiSelect.setChooseWidth(this.chooseWidth);
  104. }
  105. if(this.chooseItemHeight != 0){
  106. multiSelect.setChooseItemHeight(this.chooseItemHeight);
  107. }
  108. if(CollectionUtil.isNotEmpty(defaultValueList)){
  109. multiSelect.setDefaultValueList(defaultValueList);
  110. }
  111. multiSelect.setShowValue(showValue);
  112. multiSelect.addItemHandlerListener(new DefinedCommonMultiSelectEvent() {
  113. @Override
  114. public void disposeItemEvent(DropDownBox.Data data, Composite text_composite) {
  115. reLayoutAllParent(text_composite);
  116. }
  117.  
  118. @Override
  119. public void addItemEvent(DropDownBox.Data data, Composite text_composite) {
  120. reLayoutAllParent(text_composite);
  121. }
  122. });
  123. multiSelect.addItemHandlerListener(new DefinedCommonMultiSelectEvent(){
  124.  
  125. @Override
  126. public void disposeItemEvent(DropDownBox.Data data, Composite text_composite) {
  127. if(CollectionUtil.isEmpty(multiSelect.getValue()) && require){ //为空
  128. Composite composite = getMentionComposite();
  129. if(composite != null) {
  130. showErrorMention("不能为空",composite);
  131. }
  132.  
  133. }
  134. }
  135.  
  136. @Override
  137. public void addItemEvent(DropDownBox.Data data, Composite text_composite) {
  138. Composite composite = getMentionComposite();
  139. if(null != composite && !composite.isDisposed() && mention != null && !mention.isDisposed()){
  140. showNormalMention(composite);
  141. }
  142. }
  143.  
  144. });
  145.  
  146. if(CollectionUtil.isNotEmpty(multiSelectEventList)){
  147. for(DefinedCommonMultiSelectEvent event:multiSelectEventList){
  148. multiSelect.addItemHandlerListener(event);
  149. }
  150. }
  151.  
  152. multiSelect.paint();
  153.  
  154. if(this.helpListener != null){ //添加帮助图标
  155. Label help_img = new Label(contentComposite,SWT.NONE);
  156. help_img.setToolTipText("获取帮助");
  157. help_img.setBackground(SWTResourceManager.getWhiteColor());
  158. help_img.setImage(ImageUtil.getImage(FileUtil.loadResourceFileAsStream(Constants.HELP_NOR)));
  159. GridData gd_help_img = new GridData(SWT.FILL,SWT.FILL,false,false,1,1);
  160. gd_help_img.widthHint = 24;
  161. gd_help_img.heightHint = 24;
  162. help_img.setLayoutData(gd_help_img);
  163. help_img.setCursor(SWTResourceManager.getCursor(SWT.CURSOR_HAND));
  164. help_img.addListener(SWT.MouseDown, this.helpListener);
  165. help_img.addListener(SWT.MouseEnter, new Listener(){
  166. @Override
  167. public void handleEvent(Event event) {
  168. help_img.setImage(ImageUtil.getImage(FileUtil.loadResourceFileAsStream(Constants.HELP_HOVER)));
  169. }
  170. });
  171. help_img.addListener(SWT.MouseExit, new Listener(){
  172. @Override
  173. public void handleEvent(Event event) {
  174. help_img.setImage(ImageUtil.getImage(FileUtil.loadResourceFileAsStream(Constants.HELP_NOR)));
  175. }
  176. });
  177. }
  178.  
  179. mention = new Label(contentComposite,SWT.WRAP);
  180. GridData gd_mention = new GridData(SWT.LEFT, SWT.CENTER, false, true, 1, 1);
  181. if(super.mentionWidth != 0){
  182. gd_mention.widthHint = super.mentionWidth;
  183. }else{
  184. Rectangle bounds = this.parent.getBounds();
  185. if(bounds.width == 0){
  186. bounds = this.parent.getParent().getBounds();
  187. }
  188. gd_mention.widthHint = bounds.width - nameWidth - this.chooseWidth - 10;
  189. }
  190. mention.setLayoutData(gd_mention);
  191. mention.setBackground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
  192. showNormalMention(getMentionComposite());
  193. }
  194.  
  195. /**
  196. * 所有的重新绘制
  197. * @param composite
  198. */
  199. private void reLayoutAllParent(Composite composite){
  200. Composite contentComposite = composite;
  201. while(contentComposite != this.parent){
  202. contentComposite.layout(true);
  203. contentComposite = contentComposite.getParent();
  204. }
  205. contentComposite.layout(true);
  206. Composite parentComposite = contentComposite.getParent();
  207. while(!(parentComposite instanceof ScrolledComposite) && !(parentComposite instanceof Shell)){
  208. parentComposite.layout(true);
  209. contentComposite = parentComposite;
  210. parentComposite = parentComposite.getParent();
  211. }
  212. if(parentComposite instanceof ScrolledComposite){
  213. ((ScrolledComposite)parentComposite).setMinSize(contentComposite.computeSize(SWT.DEFAULT, SWT.DEFAULT));
  214. }
  215. }
  216.  
  217. public void setNameWidth(int nameWidth) {
  218. this.nameWidth = nameWidth;
  219. }
  220.  
  221. public Composite getContentComposite() {
  222. return contentComposite;
  223. }
  224.  
  225. public void setRequire(boolean require) {
  226. this.require = require;
  227. }
  228.  
  229. public void setMention(Label mention) {
  230. this.mention = mention;
  231. }
  232.  
  233. public void setDefaultMention(String defaultMention) {
  234. this.defaultMention = defaultMention;
  235. }
  236.  
  237. public int getChooseWidth() {
  238. return chooseWidth;
  239. }
  240.  
  241. public void setChooseWidth(int chooseWidth) {
  242. this.chooseWidth = chooseWidth;
  243. }
  244.  
  245. public int getChooseItemHeight() {
  246. return chooseItemHeight;
  247. }
  248.  
  249. public void setChooseItemHeight(int chooseItemHeight) {
  250. this.chooseItemHeight = chooseItemHeight;
  251. }
  252.  
  253. public boolean isValidResult() {
  254. return validResult;
  255. }
  256.  
  257. public void setValidResult(boolean validResult) {
  258. this.validResult = validResult;
  259. }
  260.  
  261. public String getDefaultMention() {
  262. return defaultMention;
  263. }
  264.  
  265. public List<DropDownBox.Data> getDefaultValueList() {
  266. return defaultValueList;
  267. }
  268.  
  269. public List<DropDownBox.Data> getSelectedList() {
  270. return selectedList;
  271. }
  272.  
  273. public void setNameText(String nameText) {
  274. this.nameText = nameText;
  275. }
  276.  
  277. public boolean isShowValue() {
  278. return showValue;
  279. }
  280.  
  281. public void setShowValue(boolean showValue) {
  282. this.showValue = showValue;
  283. }
  284.  
  285. public void setDefaultValueList(List<DropDownBox.Data> defaultValueList) {
  286. this.defaultValueList = defaultValueList;
  287. }
  288.  
  289. public void addItemHandlerListener(DefinedCommonMultiSelectEvent itemHandlerListener) {
  290. if(null == this.multiSelectEventList){
  291. this.multiSelectEventList = new ArrayList<DefinedCommonMultiSelectEvent>();
  292. }
  293. this.multiSelectEventList.add(itemHandlerListener);
  294. }
  295.  
  296. public void setHelpListener(Listener helpListener) {
  297. this.helpListener = helpListener;
  298. }
  299.  
  300. @Override
  301. public Composite getMentionComposite() {
  302. // TODO Auto-generated method stub
  303. if(null != multiSelect.getChooseComposite()){
  304. return multiSelect.getChooseComposite().getParent();
  305. }else{
  306. return null;
  307. }
  308. }
  309.  
  310. @Override
  311. public String getValue() {
  312. List<DropDownBox.Data> dataList = multiSelect.getValue();
  313. StringBuilder builder = new StringBuilder();
  314. for(DropDownBox.Data data:dataList){
  315. builder.append(data.getValue()).append(",");
  316. }
  317. if(StringUtil.isNotNullAndEmpty(builder.toString())){
  318. builder = builder.deleteCharAt(builder.length()-1);
  319. }
  320. return builder.toString();
  321. }
  322. }

DefinedFormMultiSelect.java 表单多选下拉框(标题 + 多选下拉框 + 提示 + 可选帮助)

带搜索功能的下拉框见下章节。

自定义SWT控件二之自定义多选下拉框的更多相关文章

  1. 自定义SWT控件一之自定义单选下拉框

    一.自定义下拉控件 自定义的下拉框,是自定义样式的,其中的下拉框使用的是独立的window,非复选框的下拉框双击单机其它区域或选择完之后,独立window构成的下拉框会自动消失. package co ...

  2. 自定义SWT控件六之自定义Tab

    6.自定义tab 本章节提供的自定义tab 分为两类 tab上带删除按钮和添加按钮,可删除tab和添加tab tab不可删除和添加 6.1 不可删除tab package com.view.contr ...

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

    5.自定义穿梭框 package com.view.control.shuttlebox; import java.util.ArrayList; import java.util.HashMap; ...

  4. 自定义SWT控件七之自定义Shell(可伸缩窗口)

    7.可伸缩窗口 该自定义窗口可以通过鼠标随意更改窗口大小 package com.hikvision.encapsulate.view.control.shell; import org.eclips ...

  5. EXTJS 4.2 资料 控件之Grid 行编辑绑定下拉框,并点一次触发一次事件

    主要代码: { header: '属性值', dataIndex: 'PropertyValueName', width: 130, editor: new Ext.form.field.ComboB ...

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

    4.其它下拉框 4.1 添加联动二级多选择框(有添加按钮和删除按钮) package com.view.control.select; import java.util.ArrayList; impo ...

  7. C# 如何定义让PropertyGrid控件显示[...]按钮,并且点击后以下拉框形式显示自定义控件编辑属性值

    关于PropertyGrid控件的详细用法请参考文献: 1.C# PropertyGrid控件应用心得 2.C#自定义PropertyGrid属性 首先定义一个要在下拉框显示的控件: using Sy ...

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

    3.搜索功能下拉弹出框 package com.view.control.select; import java.util.ArrayList; import java.util.LinkedList ...

  9. FineReport——JS二次开发(隐藏下拉框控件的倒三角)

    在对FR控件进行二次开发的过程中,需要自定义样式,比如下拉框控件带有自动检索的功能,但是又希望它的显示样式如同文本框一样,这时就需要隐藏多余的部分. 在对在线文档的查阅中可以发现很多选择器适用于多种控 ...

随机推荐

  1. centos6.5虚拟机配置Nat模式连接外网

    想来在虚拟机上搭点软件,于是乎就想让虚拟机连上外网,就用到了Nat模式,自己对网络了解不是太深,以至于配置联网花了一下午.总结下联网步骤. (1)点击虚拟网络编辑器 (2)注意以下几点标红处 (3)点 ...

  2. python统计字符串里每个字符的次数

    方法一: 推导式 dd="ewq4aewtaSDDSFDTFDSWQrtewtyufashas" print {i:dd.count(i) for i in dd} 方法二: co ...

  3. jmeter分析性能报告时的误区

    概述 我们用jmeter做性能测试,必然需要学会分析测试报告.但是初学者常常因为对概念的不清晰,最后被测试报告带到沟里去. 常见的误区 分析响应时间全用平均值 响应时间不和吞吐量挂钩 响应时间和吞吐量 ...

  4. flutter 如何实现文件读写(使用篇)

    flutter文件读写可以对磁盘文件进行操作,实现某些业务场景,那么我们开始来讲下这个文件读写操作. 使用的库插件(package) dart:io(用于数据处理) path_provider (用于 ...

  5. SPOJ MINSUB - Largest Submatrix(二分+单调栈)

    http://www.spoj.com/problems/MINSUB/en/ 题意:给出一个n*m的矩阵M,和一个面积k,要使得M的子矩阵M'的最小元素最大并且面积大于等于k,问子矩阵M'的最小元素 ...

  6. pod update更新error: RPC failed; curl 18 transfer closed with outstanding read data remaining

    1. pod update 的时候出现下边的错误 error: RPC failed; curl 18 transfer closed with outstanding read data remai ...

  7. Python的函数, 返回值, 参数

    1. 函数 函数是对功能的封装 语法: def 函数名(形参): 函数体(代码块,return) 调用: 函数名(实参) 2. 返回值 return:在函数执行的时候, 遇到return 就直接返回, ...

  8. 操作xml练习

    案例1:获取指定节点的内容 public void XmlTest() { string xmlFileName=AppDomain.CurrentDomain.BaseDirectory+" ...

  9. cogs 2320. [HZOI 2015]聪聪的世界题解

    2320. [HZOI 2015]聪聪的世界 时间限制:6 s   内存限制:512 MB [题目描述] 背景: 聪聪的性取向有问题. 题目描述: 聪聪遇到了一个难题: 给出一个序列a1…an,完成以 ...

  10. 【题解】P2078 朋友-C++

    题目传送门 这道题目就是一个模板并查集 但是!唯一不同的地方在于,这道题的编号有负数. C++的map你忘了吗!!!下表可以是任意类型. 所以把fa数组开成一个int->int的map就可以了 ...