Pages

Thursday, June 7, 2012

Default selection in ExtJS combo boxes

Problem Statement: I have a combo box and the options are populated from a data store. In my case the date store is local data store. Only thing that I want is to make some option selected by default.


This is what my code looks like.

var statusStore = new Ext.data.ArrayStore({
    id: 0,
    fields: [
        'status',
        'label'
    ],
    data: [
           ["", 'All'],
           [true, 'Active'], 
           [false, 'Inactive']
          
    ]
});

items: [{                        
    mode:'local',
    hideLabel:true,    
    xtype: 'combo',
    id: 'filterStatus',
    editable:false,
    typeAhead: true,
    triggerAction: 'all',
    forceSelection: true,
    store: statusStore,
    displayField: 'label',
    width: 110,
    ctCls: 'MB'
}]

Problem Solution: One of the solution lies around using listeners. After the combo box is rendered, make the default selection. The one that works for me is as given in the code below.


var statusStore = new Ext.data.ArrayStore({
    id: 0,
    fields: [
        'status',
        'label'
    ],
    data: [
           ["", 'All'],
           [true, 'Active'], 
           [false, 'Inactive']
          
    ]
});

items: [{                        
    mode:'local',
    hideLabel:true,    
    xtype: 'combo',
    id: 'filterStatus',
    editable:false,
    typeAhead: true,
    triggerAction: 'all',
    forceSelection: true,
    store: statusStore,
    displayField: 'label',
    width: 110,
    ctCls: 'MB',
    listeners:{
        afterrender:function(){
            this.setValue(true);
            this.setRawValue('All');
        }
    }
}]

What I have done here is added a listener i.e. afterrender to make the default selection. The code above is self explanatory.

Enable/Disable button in ExtJS 3.2.1

Problem Statement: How to enable disable buttons in ExtJS 3.2.1?

Problem Solution:  ExtJs button is basically instance of Ext.Button class. There are two ways we can enable/disable
  1. Using config option
  2. Programmatically enabling & disabling
USING CONFIG OPTIONS
In order to set any button enabled or disabled, config option disabled can be set as true or false.


Ext.Button({
    text: 'Edit',
    id: 'edit',
    cls: 'edit_class',
    diasabled: 'true', // button is disabled
    listeners: {
    'click': function () {}                
    }
})

Ext.Button({
    text: 'Edit',
    id: 'edit',
    cls: 'edit_class',
    diasabled: 'false', // button is enabled
    listeners: {
    'click': function () {}                
    }
})

PROGRAMMATICALLY
Ext.getCmp('edit').setDisabled(true);

Ext.getCmp('edit').setDisabled(false);

Read more : Ext JS 3.2.1 API DOCS (http://extjs.cachefly.net/ext-3.2.1/docs/)

Java program to find host name and IP address of local and remote machine

Problem Statement: Many times we need to find the host name and IP address of local and the remote machines. Most importantly I want to do this with a Java program.

Solution: Here is the main class, I have named it as HostNameFinder and to test these methods I've another class called as HostNameFinderTest.

/**
 * 
 * Classname : HostNameFinder.java
 * <b>HostNameFinder<b><br> 
 * <p>
 *     This class helps to get the host name and the IP addresses of the local machine and 
 *     remote machines. It provides static methods that can be used to get the very specific
 *     information such as host name or the IP address.
 * </p>
 * 
 * @author Manohar Negi
 * @version 1.0
 * @since 1.0
 * @see
 * Copyright notice
 *
 * Revision History: 
 *
 * Date            By              Version        Comments
 * ---------------------------------------------------------------------------------
 * Aug 31, 2011    Manohar Negi    1.0            Initial Draft
 * Sep 01, 2011    Manohar Negi    1.1            Modified to include more methods
 *
 **/

package com.mani.util;

import java.net.InetAddress;
import java.net.UnknownHostException;

/**
 * <p>
 * <b>Functional Description:</b>
 * <p>
 * Some Description of the file
 * 
 * Created: Month, Day ,YYYY
 * 
 * @author
 * @version
 */
public class HostNameFinder {

    public static String getMyHostName() {
        String hostname = null;
        try {
            InetAddress addr = InetAddress.getLocalHost();
            hostname = addr.getHostName();
            System.out.println("Host Name = " + hostname);

        } catch (UnknownHostException e) {
            e.printStackTrace();
        }

        return hostname;
    }

    public static String getMyIPAddress() {
        String ipAddress = null;
        try {
            InetAddress addr = InetAddress.getLocalHost();
            String ipAddr = addr.getHostAddress();
            System.out.println("IP Address = " + ipAddr.toString());

        } catch (UnknownHostException e) {
            e.printStackTrace();
        }

        return ipAddress;
    }

    public static String getIPByAddress(String address) {
        String ipAddress = null;
        try {
            InetAddress addr = InetAddress.getByName(address);
            String ipAddr = addr.getHostAddress();
            System.out.println("IP Address = " + ipAddr.toString());

        } catch (UnknownHostException e) {
            e.printStackTrace();
        }

        return ipAddress;
    }

    public static String getHostNameByAdress(String address) {
        String hostname = null;
        try {
            InetAddress addr = InetAddress.getByName(address);
            hostname = addr.getHostName();
            System.out.println("Host Name = " + hostname);

        } catch (UnknownHostException e) {
            e.printStackTrace();
        }

        return hostname;
    }

}


Here is the test class.

/**
 * 
 * Classname : HostNameFinderTest.java
 * <b>HostNameFinderTest<b><br> 
 * <p>
 *     This class is to test the methods of HostNameFinder.class
 * </p>
 * 
 * @author Manohar Negi
 * @version 1.0
 * @since 1.0
 * @see
 * Copyright notice
 *
 * Revision History: 
 *
 * Date            By              Version        Comments
 * ---------------------------------------------------------------------------------
 * Aug 31, 2011    Manohar Negi    1.0            Initial Draft
 * Sep 01, 2011    Manohar Negi    1.1            Modified to include more methods
 *
 **/
package com.mani.util;

public class HotmNameFinderTest {

    public static void main(String[] args) {

        /* get the host name for local machine */
        HostNameFinder.getMyHostName();

        /* get the IP for local machine */
        HostNameFinder.getMyIPAddress();

        /* get the host name for localhost */
        HostNameFinder.getHostNameByAdress("localhost");

        /* get the IP for localhost */
        HostNameFinder.getIPByAddress("localhost");

        /* get the host name for www.google.com */
        HostNameFinder.getHostNameByAdress("www.google.com");

        /* get the IP for www.google.com */
        HostNameFinder.getIPByAddress("www.google.com");

    }

}


And here is the output that I got.

Host Name = mani-mac
IP Address = 192.168.2.4
Host Name = localhost
IP Address = 127.0.0.1
Host Name = www.google.com
IP Address = 74.125.236.83