You are on page 1of 5

package com.queppelin.shopdekho.form; import java.util.

Vector; import import import import import import import import import import import import import import import import import com.queppelin.shopdekho.exception.ShopDekhoServerConnectionException; com.queppelin.shopdekho.helper.Product; com.queppelin.shopdekho.helper.ProductModel; com.queppelin.shopdekho.main.ShopDekhoMidlet; com.queppelin.shopdekho.renderer.ProductContainerListRenderer; com.sun.lwuit.Button; com.sun.lwuit.Command; com.sun.lwuit.Component; com.sun.lwuit.Dialog; com.sun.lwuit.List; com.sun.lwuit.TextArea; com.sun.lwuit.TextField; com.sun.lwuit.animations.CommonTransitions; com.sun.lwuit.events.ActionEvent; com.sun.lwuit.events.ActionListener; com.sun.lwuit.events.FocusListener; com.sun.lwuit.layouts.BoxLayout;

public class HomeForm extends AbstractForm { /* Declaring all the UI components */ private final TextField searchItem = new TextField(20); private final Button searchButton = new Button("Search Product"); private List heavyDiscountProducts=new List(); private AbstractForm form=this; /*To keep the track of the product first downloaded tried with static bu t * as there are so many products so out of memory error or exception was there */ //public static Product []homeProduct; private Product []homeProduct; /* Midlet for shop dekho */ ShopDekhoMidlet shopDekhoMidlet; /* variable for Getting value of search Text Box */ public String searchedItem = ""; ProductModel model=new ProductModel(); public HomeForm(final ShopDekhoMidlet shopDekhoMidlet, String search) { super(shopDekhoMidlet); this.shopDekhoMidlet = shopDekhoMidlet; if (search !=null) { searchItem.setText(search); } try { //new ProductModel().getProduct(search); Vector vectorModel=model.getProduct(search); homeProduct=new Product[vectorModel.size()]; for(int i=0;i<vectorModel.size();i++) { homeProduct[i]=(Product)vectorModel.elementAt(i) ; heavyDiscountProducts.addItem(homeProduct[i]);

//System.out.println(jsonProduct.getStri ng("productID")+jsonProduct.getString("name")+jsonProduct.getString("price")+jso nProduct.getString("actualprice")+jsonProduct.getString("image")); } /*Checking if the search results did not match w ith user search term*/ if(vectorModel.size()==0) { final Dialog errorDialog = new Dialog("N o product found"); errorDialog.setTransitionOutAnimator(Com monTransitions .createFade(500)); errorDialog.addCommand(new Command("OK") { public void actionPerformed(Acti onEvent ae) { errorDialog.dispose(); } }); errorDialog.show(); } } catch(ShopDekhoServerConnectionException jse) { final Dialog errorDialog = new Dialog(); errorDialog.setTransitionOutAnimator(CommonTrans itions .createFade(500)); TextArea txtError=new TextArea("Please allow int ernet connection to run the application..Application will close"); errorDialog.addComponent(txtError); txtError.setEditable(false); errorDialog.addCommand(new Command("OK"){ public void actionPerformed(ActionEvent ae) { midlet.notifyDestroyed(); } }); errorDialog.show(); } //this.setFocused(heavyDiscountProducts); /* Call to adding UI components function */ addComponents(); } public void keyPressed(int keyCode) { super.keyPressed(keyCode); if(keyCode==-6) { if(this.getCommandCount())

//

//UIManager.getInstance().getLookAndFeel().setReverseSof tButtons(false); //Display.getInstance().setThirdSoftButton(false); for(int i=0;i<this.getCommandCount();i++) System.out.println(i+"="+this.getCommand(i)); System.out.println(this.getSoftButtonCount()); } else { //default:Display.getInstance().setThirdSoftButton(true) ; } } /* Functions for adding UI components */ protected void addComponents() { this.setLayout(new BoxLayout(BoxLayout.Y_AXIS)); searchButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) { searchedItem = searchItem.getText().toString(); System.out.println("In search button event"); if (searchedItem == null searchedItem.length( ) == 0) { System.out.println("Nothing entered in s earch Term"); Dialog errorDialog = new Dialog("Enter s earch criteria"); errorDialog.setTransitionOutAnimator(Com monTransitions .createFade(500)); errorDialog.setTimeout(1500l); errorDialog.show(); } else { //calling method to search an item //searchItem(searchedItem); midlet.setBoolTransition(false); new HomeForm(shopDekhoMidlet, searchedIt em).show(); midlet.setBoolTransition(true); } } }); searchButton.addFocusListener(new FocusListener() { public void focusLost(Component arg0) { searchButton.setNextFocusDown(heavyDiscountProdu cts); } public void focusGained(Component arg0) { } });

this.addComponent(searchItem); addExitButton(); this.addCommand(new Command("Browse Catalogue") { public void actionPerformed(ActionEvent ae) { System.out.println("Browse Catalogue"); BrowseCatalogueForm.formName="Home"; new BrowseCatalogueForm(shopDekhoMidlet,null).sh ow(); } }); this.addCommand(new Command("View Cart") { public void actionPerformed(ActionEvent ae) { System.out.println("View Cart Pressed"); new CartForm(shopDekhoMidlet,form).show(); } }); this.addComponent(searchButton); this.setScrollable(false); searchButton.setNextFocusDown(heavyDiscountProducts); heavyDiscountProducts .setListCellRenderer(new ProductContainerListRen derer(getWidth())); /*for (int i = 0; i < productList.length; i++) { heavyDiscountProducts.addItem(productList[i]); }*/ heavyDiscountProducts.addFocusListener(new FocusListener() { public void focusLost(Component arg0) { } public void focusGained(Component arg0) { heavyDiscountProducts.setSelectedIndex(0); } }); this.addComponent(heavyDiscountProducts); heavyDiscountProducts.setNextFocusDown(searchItem); heavyDiscountProducts.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) { System.out.println("Select Button was pressed"); int itemIndex = heavyDiscountProducts .getSelectedIndex(); System.out.println(itemIndex+"Reference"+heavyDi scountProducts.getSelectedItem());

Product productSelected =(Product) heavyDiscount Products.getSelectedItem(); System.out.println("Succes for selected product" ); new DetailedDescriptionProductForm(shopD ekhoMidlet,productSelected).show();

} }); }

You might also like