Saturday, January 8, 2011

Change default behaivior of typeAhead in a Ext-Gwt Combobox

Recently I encountered with a incident where I had to filter the list of a Ext-Gwt combo box so that the list get fetched with the options which 'contains' the typed characters. Typeahead in Ext-Gwt combo box can be enabled by setting typeahead attribute true.
combo.setTypeAhead(boolean typeAhead);
But the default behaviour of the typeahead is to fetch the combo box list with the options which 'starting' with the typed characters. I had to get my brain troubled in searching the resources on the internet over few hours. but I couldn't find the solution.

Later, Hasith Yaggahavita; the team leader found the solution by going through the Gwt combo box source. What he had done was to extend the Gwt combo box component and to override the protected method onTypeAhead() to do nothing. Then he had written a custom StoreFilter with the logic to filter options which 'contains' the typed characters.

A sample code for the above implementation would be as follows

import com.gwtext.client.widgets.form.ComboBox
import com.extjs.gxt.ui.client.data.BaseListLoader;
import com.extjs.gxt.ui.client.data.ListLoadResult;
import com.extjs.gxt.ui.client.data.ListLoader;
import com.extjs.gxt.ui.client.data.RpcProxy;
import com.extjs.gxt.ui.client.store.ListStore;
import com.extjs.gxt.ui.client.store.Store;
import com.extjs.gxt.ui.client.store.StoreFilter;

public class ContainsTypeAheadComboBox extends ComboBox {

    public ContainsTypeAheadComboBoxComponent() {
        setMinChars(1);
        setQueryDelay(300);
        setTriggerAction(TriggerAction.ALL);
    }

    public void setRpcProxy(RpcProxy> proxy) {
        ListLoader> loader = new BaseListLoader>(proxy);
        ListStore listStore = new ListStoreExt(loader);
        listStore.addFilter(new ContainsStoreFilter());
        setStore(listStore);
    }

    @Override
    protected void onTypeAhead() {
        //this method is overriden to avoid typeahead
        //feature selecting the first data item in to combo box text field
    }

    /**
     * The Class extending ListStore to ignore the 'beginsWith' behavior
     */
    class ListStoreExt extends ListStore {     

        public ListStoreExt(ListLoader> loader) {
            super(loader);
            this.filtersEnabled = true;
        }

        @Override
        public void filter(String property, String beginsWith)
        {
            // ignore the 'beginsWith' string since we filter for 'contains'
            super.filter(property);
        }
    }
  
    /**
     * The Class that filter according to 'contains' instead of 'beginswith'
     */
    class ContainsStoreFilter implements StoreFilter {

        public boolean select(Store store, ComboBox parent, ComboBox item, String property) {
            String v = getRawValue();
            if ( v == null || "".equals(v)) {
                return true;
            }
            if (item != null && item.getText() != null) {
                return item.getText().toLowerCase().indexOf(v.toLowerCase()) >= 0;
            }
            return false;
        }
    }
}

Reference Ext-Gwt combo box:
http://gwt-ext.com/docs/2.0.4/com/gwtext/client/widgets/form/ComboBox.html

What are the Symptoms of Swine Flu

As swine flu spreads fast throughout the country, it is important to know the symptoms of the disease so you can recognize it in yourself and others at an early stage. The following information is extracted from an email I got recently

Usually symptoms of swine flu have generally proved mild. However a small number of patients may develop more serious illness. Many of these people have other underlying health conditions, such as heart or lung disease, that put them at increased risk.

Typical symptoms are:

* A sudden fever - 100 degrees F or above
* A sudden cough

Other symptoms may include:

* Headaches
* Tiredness
* Chills
* Cough and sneezing
* Headache
* Weakness and fatigue
* Aching muscles and joints
* Sore throat
* Runny nose
* Diarrhea or stomach upset
* Loss of appetite

You must see a doctor immediately if:

* you have a serious existing illness that weakens your immune system, such as cancer
* you are pregnant
* you have a sick child under one
* your condition suddenly gets much worse
* your condition is still getting worse after seven days (five for a child)

High-risk groups
For most people, swine flu is a mild illness. Some people get better by staying in bed, drinking plenty of water and taking over-the-counter flu medication.

However, some groups of people are more at risk of serious illness if they catch swine flu, and will need to start taking antiviral medication as it is confirmed that they have it.

It is already known that you are particularly at risk if you have:

* chronic (long-term) lung disease,
* chronic heart disease,
* chronic kidney disease,
* chronic liver disease,
* chronic neurological disease (neurological disorders include motor neurone disease, multiple sclerosis and Parkinson's disease),
* immunosuppressant (whether caused by disease or treatment) or
* Diabetes mellitus.

Also at risk are:

* patients who have had drug treatment for asthma within the past three years,
* pregnant women,
* people aged 65 and older, and
* Young children under five.


It is vital that people in these higher-risk groups who catch swine flu get antiviral and start taking them as soon as possible.

As with any sort of influenza, how bad and how long the symptoms last will depend on treatment and the patient's individual circumstances.

Most cases reported in India have been relatively mild, with those affected starting to recover within a week. Persons with swine flu infection should be considered potentially contagious for up to 7 days following illness onset. Persons who continue to be ill longer than 7 days after illness onset should be considered potentially contagious until symptoms have resolved. Children, especially younger children, might potentially be contagious for longer periods.