Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Apr 28th, 2012  |  syntax: None  |  size: 3.62 KB  |  hits: 12  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. GXT - ComoboBox with Multi select feature
  2. package com.ui.test.client;
  3.  
  4.     import java.util.List;
  5.  
  6.     import com.extjs.gxt.ui.client.data.ModelData;
  7.     import com.extjs.gxt.ui.client.event.ComponentEvent;
  8.     import com.extjs.gxt.ui.client.event.WindowEvent;
  9.     import com.extjs.gxt.ui.client.event.WindowListener;
  10.     import com.extjs.gxt.ui.client.store.ListStore;
  11.     import com.extjs.gxt.ui.client.widget.CheckBoxListView;
  12.     import com.extjs.gxt.ui.client.widget.Dialog;
  13.     import com.extjs.gxt.ui.client.widget.form.TriggerField;
  14.     import com.extjs.gxt.ui.client.widget.layout.FillLayout;
  15.     import com.google.gwt.user.client.Element;
  16.  
  17.     public class MultiSelectComboBox extends TriggerField {
  18.  
  19.         private Dialog checkBoxListHolder;
  20.         private CheckBoxListView listView;
  21.         private ListStore store;
  22.  
  23.         private String delimiter = ",";
  24.         private boolean readOnly;
  25.  
  26.  
  27.         public MultiSelectComboBox() {
  28.             store = new ListStore();
  29.             listView = new CheckBoxListView();
  30.         }
  31.  
  32.  
  33.  
  34.  
  35.         @Override
  36.         protected void onTriggerClick(ComponentEvent ce) {
  37.             super.onTriggerClick(ce);
  38.             if(readOnly) {
  39.                 return;
  40.             }
  41.             checkBoxListHolder.setSize(getWidth(), 200);
  42.             listView.setWidth(getWidth());
  43.             checkBoxListHolder.setPosition(getAbsoluteLeft(),
  44.                     getAbsoluteTop() + getHeight());
  45.             if(checkBoxListHolder.isVisible()) {
  46.                 checkBoxListHolder.hide();
  47.             }
  48.             else {
  49.                 checkBoxListHolder.show();
  50.             }
  51.         }
  52.  
  53.  
  54.  
  55.  
  56.         @Override
  57.         protected void onRender(Element target, int index) {
  58.             super.onRender(target, index);
  59.  
  60.             checkBoxListHolder = new Dialog();
  61.             checkBoxListHolder.setClosable(false);
  62.             checkBoxListHolder.setHeaderVisible(false);
  63.             checkBoxListHolder.setFooter(false);
  64.             checkBoxListHolder.setFrame(false);
  65.             checkBoxListHolder.setResizable(false);
  66.             checkBoxListHolder.setAutoHide(false);
  67.             checkBoxListHolder.getButtonBar().setVisible(false);
  68.             checkBoxListHolder.setLayout(new FillLayout());
  69.             checkBoxListHolder.add(listView);
  70.             listView.setStore(store);
  71.  
  72.             checkBoxListHolder.addWindowListener(new WindowListener(){
  73.  
  74.                 @Override
  75.                 public void windowHide(WindowEvent we) {
  76.                     setValue(parseCheckedValues(listView));
  77.                 }
  78.  
  79.             });
  80.  
  81.         }
  82.  
  83.  
  84.  
  85.  
  86.         private String parseCheckedValues(CheckBoxListView checkBoxView) {
  87.             StringBuffer buf = new StringBuffer();
  88.             if(checkBoxView != null) {
  89.                 List selected = checkBoxView.getChecked();
  90.                 int index = 1, len = selected.size();
  91.                 for(D c : selected) {
  92.                     buf.append(c.get(listView.getDisplayProperty()));
  93.                     if(index  getListView() {
  94.             return listView;
  95.         }
  96.  
  97.         public void setListView(CheckBoxListView listView) {
  98.             this.listView = listView;
  99.         }
  100.  
  101.         public ListStore getStore() {
  102.             return store;
  103.         }
  104.  
  105.         public void setStore(ListStore store) {
  106.             this.store = store;
  107.         }
  108.  
  109.         public String getDelimiter() {
  110.             return delimiter;
  111.         }
  112.  
  113.         public void setDelimiter(String delimiter) {
  114.             this.delimiter = delimiter;
  115.         }
  116.  
  117.         public boolean isReadOnly() {
  118.             return readOnly;
  119.         }
  120.  
  121.         public void setReadOnly(boolean readOnly) {
  122.             this.readOnly = readOnly;
  123.         }
  124.  
  125.  
  126.     }