/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package basic.buy;
import basic.logger.SimpleLogger;
import com.icesoft.faces.context.effects.JavascriptContext;
import java.rmi.RemoteException;
import java.util.Scanner;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import javax.servlet.http.HttpServletRequest;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import javax.xml.rpc.ServiceException;
import javax.xml.rpc.holders.BooleanHolder;
import javax.xml.rpc.holders.StringHolder;
import org.apache.axis.client.Stub;
import org.icefaces.application.showcase.util.FacesUtils;
/**
*
* @author Admin
*/
public class TreeController {
private DefaultTreeModel model;
private SectionUserObject selectedUserObject;
private DefaultMutableTreeNode rootNode = null;
private DefaultMutableTreeNode firstLevel = null;
private int currentSectionID = 0;
private TreeTopController treeTop;
private TableController table;
private Stub stub = null;
private org.tempuri.IExhibitSrvbindingStub service;
private Boolean visible = true;
public TreeController() {
init();
}
private DefaultMutableTreeNode addNode(DefaultMutableTreeNode parent,
String title,
Section section) {
DefaultMutableTreeNode node = new DefaultMutableTreeNode();
SectionUserObject userObject = new SectionUserObject(node);
node.setUserObject(userObject);
userObject.setSection(section);
if (title != null) {
userObject.setText(title);
userObject.setLeaf(false);
userObject.setExpanded(true);
node.setAllowsChildren(true);
} else {
userObject.setText(section.getTitle());
userObject.setLeaf(true);
node.setAllowsChildren(false);
}
// finally add the node to the parent.
if (parent != null) {
parent.add(node);
}
return node;
}
protected void init() {
rootNode = addNode(null, "myRoot",
new Section(-1, "root", 0, 0, true, true));
model = new DefaultTreeModel(rootNode);
selectedUserObject = (SectionUserObject) rootNode.getUserObject();
selectedUserObject.setExpanded(true);
treeTop = new TreeTopController();
try {
stub = (Stub) (new org.tempuri.IExhibitSrvserviceLocator().getIExhibitSrvPort());
} catch (ServiceException ex) {
System.out.println("myError! When: " + "Creating stub." + " Reason: " + ex.toString());
}
service = (org.tempuri.IExhibitSrvbindingStub) stub;
table = new TableController(service,this);
createTree();
createTable();
}
private void createTree() {
//parses input parameters
FacesContext fc = FacesContext.getCurrentInstance();
ExternalContext ec = fc.getExternalContext();
HttpServletRequest rq = (HttpServletRequest) ec.getRequest();
//parses section_id
String sectionID = rq.getParameter("section_id");
if(sectionID!=null){
if(!sectionID.equals("")){
try{
int id=Integer.parseInt(sectionID);
currentSectionID=id;
}catch(NumberFormatException ex){
System.out.println(ex);
currentSectionID=0;
}
}else{
currentSectionID=0;
}
}else{
currentSectionID=0;
}
StringHolder s1 = new StringHolder();
StringHolder s2 = new StringHolder();
s2.value="";
StringHolder s3 = new StringHolder();
s3.value="";
BooleanHolder result = new BooleanHolder();
try {
service.navigateByBuyCatalogue(currentSectionID, s1, s2, s3, result);
} catch (RemoteException ex) {
SimpleLogger.logging(ex.toString());
}
createBasicTree(s3.value);
createTopTree(s2.value);
this.setVisible(true);
table.setVisible(false);
}
/**
* Basic tree
*/
public void createBasicTree(String tree) {
rootNode.removeAllChildren();
Scanner sc = new Scanner(tree);
while (sc.hasNextLine()) {
String line = sc.nextLine();
String[] t = line.split("\t");
boolean enabled;
if (t[5].equals("0")) {
enabled = false;
} else {
enabled = true;
}
Section item = new Section(Integer.parseInt(t[0]), t[1], Integer.parseInt(t[2]), Integer.parseInt(t[3]), Boolean.parseBoolean(t[4]), enabled);
if (item.getLevel() == 1) {
firstLevel = addNode(rootNode, item.getTitle(), item);
} else {
addNode(firstLevel, null, item);
}
}
}
/**
* Top tree
*/
public void createTopTree(String tree) {
treeTop.clear();
Scanner sc = new Scanner(tree);
while (sc.hasNextLine()) {
String line = sc.nextLine();
String[] t = line.split("\t");
boolean enabled;
if (t[5].equals("0")) {
enabled = false;
} else {
enabled = true;
}
Section item = new Section(Integer.parseInt(t[0]), t[1], Integer.parseInt(t[2]), Integer.parseInt(t[3]), Boolean.parseBoolean(t[4]), enabled);
treeTop.addNode(treeTop.getLast(), item.getTitle(), item);
}
}
private void createTable() {
//parses input parameters
FacesContext fc = FacesContext.getCurrentInstance();
ExternalContext ec = fc.getExternalContext();
HttpServletRequest rq = (HttpServletRequest) ec.getRequest();
//parses section_count
String showTable = rq.getParameter("show_table");
if (showTable != null) {
if (!showTable.equals("")) {
try {
boolean show = Boolean.parseBoolean(showTable);
String page = rq.getParameter("page");
if (page != null) {
if(!page.equals("")){
try{
int p=Integer.parseInt(page);
if(show){
SimpleLogger.logging("table");
table.createTable(currentSectionID, "\n\n", p);
this.setVisible(false);
table.setVisible(true);
}
}catch(NumberFormatException ex){
System.out.println(ex);
return;
}
}else{
return;
}
}else{
return;
}
} catch (NumberFormatException ex) {
System.out.println(ex);
return;
}
} else {
return;
}
} else {
return;
}
}
public DefaultTreeModel getModel() {
return model;
}
public void setModel(DefaultTreeModel model) {
this.model = model;
}
public TreeTopController getTreeTop() {
return treeTop;
}
public void setTreeTop(TreeTopController treeTop) {
this.treeTop = treeTop;
}
public TableController getTable() {
return table;
}
public void setTable(TableController table) {
this.table = table;
}
public Boolean getVisible() {
return visible;
}
public void setVisible(Boolean visible) {
this.visible = visible;
}
public int getCurrentSectionID() {
return currentSectionID;
}
}