Thursday, April 25, 2013

AutoCompletionTestFieldDemo using Java

import java.awt.*;
import java.awt.event.*;
import java.util.*;

import javax.swing.*;
import javax.swing.event.*;
/**
 * Example for AutoCompletionTestFieldDemo
 * @author R.Amirtharaj
 */
public class AutoCompletionTestFieldDemo {

    public static void main(String[] args) throws Exception {
        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        JFrame frame = new JFrame();
        frame.setTitle("Auto Completion Test");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setBounds(200, 200, 500, 400);

        ArrayList items = new ArrayList();
        // To get Locale
        Locale[] locales = Locale.getAvailableLocales();
        for (int i = 0; i < locales.length; i++) {
            String item = locales[i].getDisplayName();
            items.add(item);
        }
        JTextField txtInput = new JTextField();

        // set autocmplete
        setAutoComplete(txtInput, items);
        txtInput.setColumns(30);
        frame.getContentPane().setLayout(new FlowLayout());
        frame.getContentPane().add(txtInput, BorderLayout.NORTH);
        frame.setVisible(true);
    }

    private static boolean isAdjusting(JComboBox cbInput) {
        if (cbInput.getClientProperty("is_adjusting") instanceof Boolean) {
            return (Boolean) cbInput.getClientProperty("is_adjusting");
        }
        return false;
    }

    private static void setAdjusting(JComboBox cbInput, boolean adjusting) {
        cbInput.putClientProperty("is_adjusting", adjusting);
    }

    public static void setAutoComplete(final JTextField txtInput, final ArrayList items) {
        final DefaultComboBoxModel model = new DefaultComboBoxModel();
        final JComboBox cbInput = new JComboBox(model) {
            public Dimension getPreferredSize() {
                return new Dimension(super.getPreferredSize().width, 0);
            }
        };
        setAdjusting(cbInput, false);
        for (String item : items) {
            model.addElement(item);
        }
        cbInput.setSelectedItem(null);
        cbInput.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if (!isAdjusting(cbInput)) {
                    if (cbInput.getSelectedItem() != null) {
                        txtInput.setText(cbInput.getSelectedItem().toString());
                    }
                }
            }
        });

        txtInput.addKeyListener(new KeyAdapter() {

            @Override
            public void keyPressed(KeyEvent e) {
                setAdjusting(cbInput, true);
                if (e.getKeyCode() == KeyEvent.VK_SPACE) {
                    if (cbInput.isPopupVisible()) {
                        e.setKeyCode(KeyEvent.VK_ENTER);
                    }
                }
                if (e.getKeyCode() == KeyEvent.VK_ENTER || e.getKeyCode() == KeyEvent.VK_UP || e.getKeyCode() == KeyEvent.VK_DOWN) {
                    e.setSource(cbInput);
                    cbInput.dispatchEvent(e);
                    if (e.getKeyCode() == KeyEvent.VK_ENTER) {
                        // User may enter Other than list
                        try {
                            txtInput.setText(cbInput.getSelectedItem().toString());
                        }
                        catch(Exception ex)
                        {
                        }
                        cbInput.setPopupVisible(false);
                    }
                }
                if (e.getKeyCode() == KeyEvent.VK_ESCAPE) {
                    cbInput.setPopupVisible(false);
                }
                setAdjusting(cbInput, false);
            }
        });

        txtInput.getDocument().addDocumentListener(new DocumentListener() {
            public void insertUpdate(DocumentEvent e) {
                updateList();
            }

            public void removeUpdate(DocumentEvent e) {
                updateList();
            }

            public void changedUpdate(DocumentEvent e) {
                updateList();
            }

            private void updateList() {
                setAdjusting(cbInput, true);
                model.removeAllElements();
                String input = txtInput.getText();
                if (!input.isEmpty()) {
                    for (String item : items) {
                        if (item.toLowerCase().startsWith(input.toLowerCase())) {
                            model.addElement(item);
                        }
                    }
                }
                cbInput.hidePopup();
                cbInput.setPopupVisible(model.getSize() > 0);
                setAdjusting(cbInput, false);
            }
        });
        txtInput.setLayout(new BorderLayout());
        txtInput.add(cbInput, BorderLayout.SOUTH);
    }
}

Monday, April 22, 2013

The Seven Phases of the Systems Development Life Cycle (SDLC):

The Seven Phases of the Systems Development Life Cycle :

1. Information Gathering : In this phase Bussiness Analyst will gather all the information from customer requirement and prepare BRS/CRS/URS(Buiseness/Customer/User Requirement Specification) document. Mainly this done by Buisness Anlayst as they are the one who are completely involved in SDLC.

2. Analysis : In this phase Senior Business Analyst will prepare SRS(Software Requirement Specification). The Features and functions are put in the project are determined.

3. Design : Actual flow of the project is done here. Deciding upto logical functionality, ER Diagram , Flow chart etc are made. Cheif Architect will prepare HLD and LLD(High Level Document and Low Level Document)
  • HLD : Defines the overall Hierarchy of the function.Example : Login into Net Banking. From the root level to the least level is defines in HLD.
  • LLD: Internal logic of the project is defined. Focuses on the internal functionality of the program. Each and every module logic is defined.

4. Code : Based on the design document , small-small modules are prepared . All the modules are summed together to form a .exe. Unit testing , integration testing & white box testing comes in this phase.       

5. Testing : Actuall system testing take place here.

6. Implementation : All sr.department will release the product .

7. Maintenance : In maintenance the maintenance team will change the software if necessary. 

Thursday, April 18, 2013

Java Collection loop throw elements

import java.util.*;

/**
 *
 * @author Amirtharaj
 */
public class CollectionLoopThroDemo {

    public static void main(String arg[]) {
        CollectionLoopThroDemo cd = new CollectionLoopThroDemo();

        List al = new ArrayList();
        al.add("C");
        al.add("A");
        al.add("B");
        cd.iterateList(al);

        Set setDet = new HashSet();
        setDet.add("B");
        setDet.add("A");
        setDet.add("C");
        cd.iterateSet(setDet);

        Map mapDet = new HashMap();
        mapDet.put("B", 2.0);
        mapDet.put("A", 1.0);
        mapDet.put("C", 3.0);
        cd.iterateMap(mapDet);

    }

    public void iterateList(List al) {
        //access via Iterator
        Iterator iterator = al.iterator();
        while (iterator.hasNext()) {
            String str = (String) iterator.next();
            System.out.println(str);
        }
        //access via new for-loop
        for (String str : al) {
            System.out.println(str);
        }
    }

    public void iterateSet(Set setDet) {

        //access via Iterator
        Iterator iterator = setDet.iterator();
        while (iterator.hasNext()) {
            String str = (String) iterator.next();
            System.out.println(str);
        }


        //access via new for-loop
        for (String str : setDet) {
            System.out.println(str);
        }
    }

    public void iterateMap(Map mapDet) {
        //access via Iterator
        Iterator iterator = mapDet.keySet().iterator();
        while (iterator.hasNext()) {
            String key = (String) iterator.next();
            Double value = (Double) mapDet.get(key);
            System.out.println(key + "," + value);
        }

        //access via new for-loop
        for (String key : mapDet.keySet()) {
            Double value = mapDet.get(key);
            System.out.println(key + "," + value);
        }

    }
}

Tuesday, April 2, 2013

To get Motherboard serial number, Hard disk serial number, IP Address and MAC Address from Java

import java.io.File;
import java.io.FileWriter;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.InetAddress;
import java.net.NetworkInterface;

public class MyPC {

    public static String getMotherBoardSerialNumber() {
        String result = "";
        try {
            File file = File.createTempFile("realhowto", ".vbs");
            file.deleteOnExit();
            FileWriter fw = new java.io.FileWriter(file);
            String vbs =
                    "Set objWMIService = GetObject(\"winmgmts:\\\\.\\root\\cimv2\")\n"
                    + "Set colItems = objWMIService.ExecQuery _ \n"
                    + "   (\"Select * from Win32_BaseBoard\") \n"
                    + "For Each objItem in colItems \n"
                    + "    Wscript.Echo objItem.SerialNumber \n"
                    + "    exit for  ' do the first cpu only! \n"
                    + "Next \n";

            fw.write(vbs);
            fw.close();
            Process p = Runtime.getRuntime().exec("cscript //NoLogo " + file.getPath());
            BufferedReader input =
                    new BufferedReader(new InputStreamReader(p.getInputStream()));
            String line;
            while ((line = input.readLine()) != null) {
                result += line;
            }
            input.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result.trim();
    }

    public static String getHardDiskSerialNumber(String drive) {
        String result = "";
        try {
            File file = File.createTempFile("realhowto", ".vbs");
            file.deleteOnExit();
            FileWriter fw = new java.io.FileWriter(file);

            String vbs = "Set objFSO = CreateObject(\"Scripting.FileSystemObject\")\n"
                    + "Set colDrives = objFSO.Drives\n"
                    + "Set objDrive = colDrives.item(\"" + drive + "\")\n"
                    + "Wscript.Echo objDrive.SerialNumber";  // see note
            fw.write(vbs);
            fw.close();
            Process p = Runtime.getRuntime().exec("cscript //NoLogo " + file.getPath());
            BufferedReader input =
                    new BufferedReader(new InputStreamReader(p.getInputStream()));
            String line;
            while ((line = input.readLine()) != null) {
                result += line;
            }
            input.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result.trim();
    }

    public static String getIPAddress() {
        String strIP = "127.0.0.1";
        try {
            InetAddress ip = InetAddress.getLocalHost();
            strIP = ip.getHostAddress();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return strIP;
    }

    public static String getMACAddress() {
        String strMAC = "127.0.0.1";
        try {
            InetAddress ip = InetAddress.getLocalHost();
            NetworkInterface network = NetworkInterface.getByInetAddress(ip);
            byte[] mac = network.getHardwareAddress();
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < mac.length; i++) {
                sb.append(String.format("%02X%s", mac[i], (i < mac.length - 1) ? "-" : ""));
            }
            strMAC = sb.toString();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return strMAC;
    }

    public static void main(String[] args) {
        System.out.println("Motherboard serial number : " + MyPC.getMotherBoardSerialNumber());
        System.out.println("Hard disk serial number : " + MyPC.getHardDiskSerialNumber("C"));
        System.out.println("IP Address : " + MyPC.getIPAddress());
        System.out.println("MAC Address : " + MyPC.getMACAddress());
    }
}