Extended SWT combo

Source: Internet
Author: User
SWT combo has two troubles:
  1. If the last selected content is selected, the addselectionlistener method is triggered.
  2. If setdata (string key, object Value) is used, the key value must be set. Index is usually used as the key value. However, if an item is deleted, its index is automatically-1 and cannot correspond to the key.

To solve this problem, a combo class is rewritten.
To Solve Problem 1, a new interface is written:

  1. Public interface selectionchangedlistener {
  2. /**
  3. * Similar to selectionlistener
  4. * @ Param event
  5. */
  6. Public void selectionchanged (selectionevent event );
  7. }

To Solve Problem 2, define a class to save the combo status

  1. Public class combostate {
  2. Private int selectionindex =-1;
  3. Public int getselectionindex (){
  4. Return selectionindex;
  5. }
  6. Public void setselectionindex (INT selectionindex ){
  7. This. selectionindex = selectionindex;
  8. }
  9. }

The combo class is as follows:

  1. Public class easycombo extends combo {
  2. Private combostate state;
  3. Private list <Object> indexdata = new jsonlist <Object> ();
  4. Private list <selectionchangedlistener> scls = new arraylist <selectionchangedlistener> ();
  5. Public easycombo (composite parent, int style ){
  6. Super (parent, style );
  7. This. setstate (New combostate ());
  8. }
  9. /**
  10. * Encapsulate selectionchangedlistener events
  11. */
  12. Public void addselectionchangedlistener (selectionchangedlistener listener ){
  13. If (scls. Size () = 0) {// The first add operation creates an addselectionlistener event.
  14. Addselectionlistener (New selectionlistener (){
  15. Public void widgetdefaselecselected (selectionevent e ){
  16. }
  17. Public void widgetselected (selectionevent e ){
  18. If (getstate (). getselectionindex ()! = Getselectionindex ()){
  19. Getstate (). setselectionindex (getselectionindex ());
  20. For (selectionchangedlistener listener: scls ){
  21. Listener. selectionchanged (E );
  22. }
  23. }
  24. }
  25. });
  26. }
  27. Scls. Add (listener );
  28. }
  29. Public void add (string text ){
  30. Super. Add (text );
  31. Indexdata. Add (null );
  32. }
  33. Public void add (string text, int index ){
  34. Super. Add (text, index );
  35. If (index> indexdata. Size ()){
  36. Indexdata. Add (null );
  37. } Else {
  38. Indexdata. Set (index, null );
  39. }
  40. }
  41. Public void remove (INT index ){
  42. Super. Remove (INDEX );
  43. Indexdata. Remove (INDEX );
  44. }
  45. Public void removeall (){
  46. Super. removeall ();
  47. Indexdata. Clear ();
  48. }
  49. /**
  50. * Set the value. Unlike setdata (string, object), it is remove sensitive. <br>
  51. * For example, if 0-> A 1-> B 2-> C deletes 1, 0-> A 1-> C <br>
  52. * Therefore, you can use a linked list to store the object values. When the remove operation is called, The linked list is also removed from a node.
  53. *
  54. * @ Param Index
  55. * @ Param Value
  56. */
  57. Public void setindexdata (INT index, object Value ){
  58. Indexdata. Set (index, value );
  59. }
  60. Public object getindexdata (INT index ){
  61. Return indexdata. Get (INDEX );
  62. }
  63. Public void setstate (combostate state ){
  64. This. State = State;
  65. }
  66. Public combostate getstate (){
  67. Return this. State;
  68. }
  69. /**
  70. * Note: you must write an empty method. Otherwise, an exception is reported.
  71. */
  72. Protected void checksubclass (){
  73. }
  74. }

The last is the test class:

  1. Public class testeasycombo {
  2. Public static void main (string [] ARGs ){
  3. Display display = New Display ();
  4. Shell shell = new shell (Display );
  5. Shell. settext ("test window ");
  6. Shell. setlayout (New filllayout ());
  7. Easycombo combo = new easycombo (shell, SWT. read_only );
  8. Combo. Add ("test0 ");
  9. Combo. setindexdata (0, "test 0 ");
  10. Combo. Add ("test1 ");
  11. Combo. setindexdata (1, "Test 1 ");
  12. Combo. Add ("Test2 ");
  13. Combo. setindexdata (2, "Test 2 ");
  14. System. Out. println ("index:" + combo. getindexdata (1 ));
  15. Combo. Remove (1 );
  16. System. Out. println ("index:" + combo. getindexdata (1 ));
  17. Combo. addselectionchangedlistener (New selectionchangedlistener (){
  18. Public void selectionchanged (selectionevent event ){
  19. System. Out. println ("selection changed! ");
  20. }
  21. });
  22. Shell. Pack ();
  23. Shell. open ();
  24. While (! Shell. isdisposed ()){
  25. If (! Display. readanddispatch ())
  26. Display. Sleep ();
  27. }
  28. }
  29. }

Now, the new class is complete.
It provides a selectionchangeevent event. When you select a combo item and it is not the one selected last time, selectionchangeevent will be triggered. In addition, I can obtain the corresponding value based on the index.

This is only one way to solve the problem. If combo is not extended (eclipse does not recommend inheriting SWT widgets), there are other solutions. Next

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.