import item.Admin;
import item.AdminHelper;
import item.product;
import item.unknownId;
import java.util.Properties;
import java.util.Vector;
import javax.swing.table.AbstractTableModel;
import org.omg.CORBA.ORB;
import org.omg.CosNaming.NamingContextExt;
import org.omg.CosNaming.NamingContextExtHelper;
import org.omg.CosNaming.NamingContextPackage.CannotProceed;
import org.omg.CosNaming.NamingContextPackage.InvalidName;
import org.omg.CosNaming.NamingContextPackage.NotFound;
@SuppressWarnings("serial")
public class ItemTableModelCORBA extends AbstractTableModel {
protected String[] columnNames = { "ID", "Description", "Price", "Location" };
// this is for CORBA
protected Vector<product> data = new Vector<product>();
private Admin items;
public ItemTableModelCORBA() {
System.out.println("Customer called ...");
try {
Properties props = new Properties();
props.put("org.omg.CORBA.ORBInitialPort", "900");
//props.put("org.omg.CORBA.ORBInitialHost", "87.104.136.229");
props.put("org.omg.CORBA.ORBInitialHost", "stolas69.servequake.com");
//props.setProperty("org.omg.CORBA.ORBInitialPort", "900");
//props.setProperty("org.omg.CORBA.ORBInitialHost", "stolas69.servequake.com");
String[] args = new String[0];
ORB orb = ORB.init(args, props);
org.omg.CORBA.Object object = orb
.resolve_initial_references("NameService");
NamingContextExt nc = NamingContextExtHelper.narrow(object);
org.omg.CORBA.Object adm = nc.resolve_str("admin");
items = AdminHelper.narrow(adm);
product[] products = new product[items.getProducts().length];
System.out.println(products.length);
products = items.getProducts();
for (int i = 0; i < products.length - 1; i++) {
System.out.println(products[i].id + ":"
+ products[i].description);
data.add(products[i]);
}
} catch (InvalidName e) {
e.printStackTrace(System.err);
} catch (NotFound e) {
e.printStackTrace(System.err);
} catch (CannotProceed e) {
e.printStackTrace(System.err);
} catch (org.omg.CORBA.ORBPackage.InvalidName 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.id;
case 1:
return i.description;
case 2:
return i.price;
case 3:
return i.location;
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:
i.description = (String) aValue;
try {
items.changeProductDescription(i.id, (String) aValue);
} catch (unknownId e) {
e.printStackTrace(System.err);
}
break;
case 2:
i.price = (Double) aValue;
try {
items.changeProductPrice(i.id, (Double) aValue);
} catch (unknownId e) {
e.printStackTrace(System.err);
}
break;
case 3:
i.location = (String) aValue;
try {
items.changeProductLocation(i.id, (String) aValue);
} catch (unknownId e) {
e.printStackTrace(System.err);
}
break;
default:
System.out.println("invalid index");
}
fireTableCellUpdated(rowIndex, columnIndex);
}
}