1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203
| package com.cap.tree.action;
import java.util.Hashtable;
import com.cc.framework.adapter.struts.ActionContext;
import com.cc.framework.adapter.struts.FormActionContext;
import com.cc.framework.common.Algorithm;
import com.cc.framework.common.CheckState;
import com.cc.framework.ui.control.ControlActionContext;
import com.cc.framework.ui.control.TreeControl;
import com.cc.framework.ui.model.Checkable;
import com.cc.framework.ui.model.TreeNodeDataModel;
import com.cc.framework.util.TreeHelp;
import com.cc.sample.action.CCAction;
import com.cc.sample.common.Forwards;
import com.cc.sample.common.Messages;
import com.cc.sample.dbaccess.DBProduct;
import com.cc.sample.presentation.dsp.ProductGroupDsp;
/**
* This Class is responsible to instantiate our TreeControl.
* This Class also implements the Methode which are called if
* an Event in our TreeControl occurs.
*
* @author <a href="mailto:gschulz@scc-gmbh.com">Gernot Schulz</a>
* @version $Revision: 1.7 $
*/
public class ProductTreeBrowseAction extends CCAction {
/**
* The name of the bean for our control in the session
*/
protected String BEANNAME;
/**
* Inner class which checks the checked items
*/
private static class CollectCheckedAlg implements Algorithm {
/**
* Collection for the checked items found
*/
private Hashtable items = new Hashtable();
/**
* Constructor
*/
public CollectCheckedAlg() {
super();
}
/**
* @see com.cc.framework.common.Algorithm#execute(String uniqueId, Object obj)
*/
public boolean execute(String uniqueId, Object obj) throws Exception {
if (obj instanceof Checkable) {
TreeNodeDataModel node = (TreeNodeDataModel) obj;
Checkable checkable = (Checkable) obj;
if (checkable.getCheckState() == CheckState.CHECKED.toInt()) {
items.put(node.getUniqueKey(), obj);
}
}
return true;
}
/**
* Returns the checked items
*/
public Hashtable getcheckedItems() {
return items;
}
}
// ----------------------------------
// methods
// ----------------------------------
/**
* Constructor for StocksBrowseAction.
*/
public ProductTreeBrowseAction() {
super();
BEANNAME = "products";
}
/**
* @see com.cc.framework.adapter.struts.FrameworkAction#doExecute(com.cc.framework.adapter.struts.ActionContext)
*/
public void doExecute(ActionContext ctx) throws Exception {
try {
refreshTree(ctx);
} catch (Throwable t) {
log.error("Error", t);
ctx.addError(Messages.ERROR_INIT_FORMBEAN, t);
}
// Dialog anzeigen
ctx.forwardToInput();
}
/**
* Initializ the Tree with the Display-Data
* @param ctx ActionContext
* @throws java.lang.Exception
*/
private void refreshTree(ActionContext ctx) throws Exception {
// In this Example we check if the Tree already
// exists, so we do not populte it again.
// So the Tree does not collaps if we reenter the Page
if (null != ctx.session().getAttribute(BEANNAME)) {
return;
}
// first we get the Data for our Tree
ProductGroupDsp data = DBProduct.fetch();
// secondly create the TreeControl and populate it
// with the Data to display
TreeControl products = new TreeControl();
products.setDataModel(data);
// third put the TreeControl into the Session-Object.
// Our Control is a statefull Object.
// Normaly you can use an Objectmanager or an other
// workflow Component that manage the Livecyle of the
// our TreeControl-Object.
ctx.session().setAttribute(BEANNAME, products);
}
// ------------------------------------------------
// Tree-Control Event Handler
// ------------------------------------------------
/**
* This Method is called when the TreeLabel is clicked
* In our Example we switch to the DetailView, which shows
* more Information about the node.
* @param ctx ControlActionContext
* @param key UniqueKey, as created in the Datamodel (e.g. the Primarykey)
*/
public void products_onDrilldown(ControlActionContext ctx, String key) throws Exception {
// highlight the node
((TreeControl) ctx.control()).select(key);
ctx.forwardByName(Forwards.DRILLDOWN, key);
}
/**
* This Method is call if a Node with an unknown number of
* Childs is clicked.
* @param ctx ControlActionContext
* @param key UniqueKey, as created in the Datamodel (e.g. the Primarykey)
*/
public void products_onExpandEx(ControlActionContext ctx, String key) throws Exception {
}
/**
* This Method is call if the save button is clicked
* @param ctx FormActionContext
*/
public void save_onClick(FormActionContext ctx) throws Exception {
Hashtable items = new Hashtable();
try {
TreeControl control = (TreeControl) ctx.session().getAttribute(BEANNAME);
CollectCheckedAlg collector = new CollectCheckedAlg();
TreeHelp.iterateNodes((TreeNodeDataModel)control.getDataModel(), collector);
items = collector.getcheckedItems();
// process items
} catch (Throwable t) {
log.error("Error", t);
ctx.addError(Messages.ERROR, t);
}
if (!ctx.hasErrors()) {
ctx.addGlobalMessage(Messages.MESSAGE, "Items checked: " + items.size());
}
// back to the jsp
ctx.forwardToInput();
}
} |
Partager