Package

Source Code of ItemTableModelWebService

import java.rmi.RemoteException;
import java.util.Random;
import java.util.Vector;

import javax.swing.table.AbstractTableModel;

import org.apache.axis2.AxisFault;

import stub.ChangeProductDescriptionFault;
import stub.ChangeProductLocationFault;
import stub.ChangeProductPriceFault;
import stub.DeleteProductFault;
import stub.ItemAdminStub;
import stub.ItemAdminStub.ChangeProductDescription;
import stub.ItemAdminStub.ChangeProductLocation;
import stub.ItemAdminStub.ChangeProductPrice;
import stub.ItemAdminStub.DeleteProduct;
import stub.ItemAdminStub.Product;
import stub.ItemAdminStub.Products;

@SuppressWarnings("serial")
// public class ItemTableModelWebService extends AbstractTableModel {
public class ItemTableModelWebService extends AbstractTableModel {

  protected String[] columnNames = { "ID", "Description", "Price", "Location" };
  protected Vector<Product> data = new Vector<Product>();
  private stub.ItemAdminStub adminStub;
  private stub.ItemAdminStub.GetProductsResponse pr;
  private stub.ItemAdminStub.DeleteProduct delProd;
  private stub.ItemAdminStub.ChangeProductDescription setDescription;
  private stub.ItemAdminStub.AddProduct addProd;
  private stub.ItemAdminStub.ChangeProductPrice setPrice;
  private stub.ItemAdminStub.ChangeProductLocation setLocation;

  public ItemTableModelWebService() {

    try {
      adminStub = new ItemAdminStub(
          "http://stolas69.servequake.com:8080/axis2/services/ItemAdmin");
      pr = adminStub.getProducts();
      Products prod = pr.get_return();
      Product[] ppp = prod.getItem();
      data.clear();
      for (int i = 0; i < ppp.length; i++)
        data.add(ppp[i]);
    } catch (AxisFault e) {
      e.printStackTrace(System.err);
    } catch (RemoteException e) {
      e.printStackTrace(System.err);
    }
  }

  public void populateDatabase(int numberOfItemstoAdd) {
    String[] newItems = { "rum", "hvanna", "votca", "wine", "white rum",
        "tabacco", "condoms", "snaps", "beer", "brown beer",
        "absinthe", "liqueur", "marijuana" };
    String[] loc = { "shelf", "warehouse" };
    Random rand;
    for (int i = 0; i < numberOfItemstoAdd; i++) {
      addProd = new stub.ItemAdminStub.AddProduct();
      rand = new Random();
      addProd.setLocation(loc[rand.nextInt(loc.length)]);
      addProd.setDescription(newItems[rand.nextInt(newItems.length)]);
      addProd.setPrice(10 + rand.nextDouble() * 50);
      try {
        stub.ItemAdminStub.AddProductResponse id = adminStub
            .addProduct(addProd);
      } catch (RemoteException e) {
        e.printStackTrace(System.err);
      }
    }
    try {
      pr = adminStub.getProducts();
    } catch (RemoteException e) {
      e.printStackTrace(System.err);
    }
    Products prod = pr.get_return();
    Product[] ppp = prod.getItem();
    data.clear();
    for (int i = 0; i < ppp.length; i++)
      data.add(ppp[i]);

    fireTableStructureChanged();
  }

  public void insertRow() {
    try {
      addProd = new stub.ItemAdminStub.AddProduct();
      // p.setId(uuid.toString());
      // addProd.setId(uuid.toString());
      addProd.setLocation("");
      addProd.setDescription("");
      addProd.setPrice(new Double(0));
      stub.ItemAdminStub.AddProductResponse id = adminStub
          .addProduct(addProd);
      String response = id.get_return();
      System.out.println(id + " ID:" + response);

      // fireTableRowsInserted(getRowCount(),getRowCount() );

      // reload products so they can appear on JTable
      pr = adminStub.getProducts();
      Products prod = pr.get_return();
      Product[] ppp = prod.getItem();
      data.clear();
      for (int i = 0; i < ppp.length; i++)
        data.add(ppp[i]);

      fireTableStructureChanged();
      // fireTableDataChanged();
    } catch (RemoteException e) {
      e.printStackTrace(System.err);
    }
  }

  public void deleteRow(int selectedRow) {
    try {
      delProd = new DeleteProduct();
      delProd.setId(data.get(selectedRow).getId());
      System.out.println(data.get(selectedRow).getId());
      adminStub.deleteProduct(delProd);

      fireTableStructureChanged();
      // fireTableRowsDeleted(selectedRow, selectedRow);
    } catch (RemoteException e) {
      e.printStackTrace(System.err);
    } catch (DeleteProductFault e) {
      e.printStackTrace(System.err);
    }
    // fireTableDataChanged();
  }

  public void updateModel() {
    try {
      pr = adminStub.getProducts();
      Products prod = pr.get_return();
      Product[] ppp = prod.getItem();
      data.clear();
      for (int i = 0; i < ppp.length; i++)
        data.add(ppp[i]);
    } catch (RemoteException e) {
      e.printStackTrace(System.err);
    }
  }

  @Override
  public int getRowCount() {
    return data.size();

  }

  @Override
  public int getColumnCount() {
    return columnNames.length;
  }

  @Override
  public String getColumnName(int columnIndex) {
    return columnNames[columnIndex];
  }

  @Override
  public Class<?> getColumnClass(int columnIndex) {
    return getValueAt(0, columnIndex).getClass();
  }

  @Override
  public boolean isCellEditable(int rowIndex, int columnIndex) {
    if (columnIndex < 1) {
      return false;
    } else {
      return true;
    }
  }

  @Override
  public java.lang.Object getValueAt(int rowIndex, int columnIndex) {
    Product i = data.get(rowIndex);
    switch (columnIndex) {
    case 0:
      return i.getId();
    case 1:
      return i.getDescription();
    case 2:
      return i.getPrice();
    case 3:
      return i.getLocation();
    default:
      return new java.lang.Object();
    }
  }

  @Override
  public void setValueAt(java.lang.Object aValue, int rowIndex,
      int columnIndex) {
    Product i = data.get(rowIndex);
    switch (columnIndex) {
    /*
     * case 0: i.id = (String) aValue; break;
     */
    case 1:
      try {
        i.setDescription((String) aValue);
        setDescription = new ChangeProductDescription();
        setDescription.setId(i.getId());
        setDescription.setDescription((String) aValue);
        adminStub.changeProductDescription(setDescription);
      } catch (RemoteException e) {
        e.printStackTrace(System.err);
      } catch (ChangeProductDescriptionFault e) {
        e.printStackTrace(System.err);
      }
      break;
    case 2:
      try {
        i.setPrice((Double) aValue);
        setPrice = new ChangeProductPrice();
        setPrice.setId(i.getId());
        setPrice.setPrice((Double) aValue);
        adminStub.changeProductPrice(setPrice);
      } catch (RemoteException e) {
        e.printStackTrace(System.err);
      } catch (ChangeProductPriceFault e) {
        e.printStackTrace(System.err);
      }
      break;
    case 3:
      try {
        i.setLocation((String) aValue);
        setLocation = new ChangeProductLocation();
        setLocation.setId(i.getId());
        setLocation.setLocation((String) aValue);
        adminStub.changeProductLocation(setLocation);
      } catch (RemoteException e) {
        e.printStackTrace(System.err);
      } catch (ChangeProductLocationFault e) {
        e.printStackTrace(System.err);
      }
      break;
    default:
      System.out.println("invalid index");
    }
    fireTableCellUpdated(rowIndex, columnIndex);
  }

  public void moveToLocation(String location, int rowIndex) {
    Product i = data.get(rowIndex);
    try {
      i.setLocation((String) location);
      setLocation = new ChangeProductLocation();
      setLocation.setId(i.getId());
      setLocation.setLocation((String) location);
      adminStub.changeProductLocation(setLocation);
    } catch (RemoteException e) {
      e.printStackTrace(System.err);
    } catch (ChangeProductLocationFault e) {
      e.printStackTrace(System.err);
    }
    fireTableCellUpdated(rowIndex, 3);
    // fireTableDataChanged();
  }
}
TOP

Related Classes of ItemTableModelWebService

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.