这是一个插件控件的开发示例,实现了一个基于ztree的参数树控件。
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

146 lines
4.0 KiB

package com.fr.plugin.widget.ztree.core;
import com.fanruan.api.cal.CalculatorKit;
import com.fanruan.api.util.GeneralKit;
import com.fanruan.api.xml.XmlKit;
import com.fr.plugin.widget.ztree.tools.ZTreeHelper;
//需要用子类传入时用CalculatorProvider出错
import com.fr.script.Calculator;
//open
import com.fr.stable.xml.XMLPrintWriter;
import com.fr.stable.xml.XMLableReader;
//放下
import com.fr.json.JSONArray;
import com.fr.json.JSONException;
import com.fr.json.JSONObject;
//接口
import com.fr.data.Dictionary;
import com.fr.stable.script.NameSpace;
import com.fr.stable.xml.XMLable;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
/**
* Created by richie on 15/11/17.
*/
public class ZTreeNode implements XMLable {
public static final int ROOT_LEVEL = -1;
public static final String XML_TAG = "ZTreeNode";
private Dictionary dictionary;
private ZTreeNode child;
public ZTreeNode() {
}
public ZTreeNode(Dictionary dictionary) {
this.dictionary = dictionary;
}
public Dictionary getDictionary() {
return dictionary;
}
public ZTreeNode getChild() {
return child;
}
public void setChild(ZTreeNode child) {
this.child = child;
}
public ZTreeNode findNode(int level) {
return findNode(level, ROOT_LEVEL);
}
public ZTreeNode findNode(int level, int current) {
if (current < level) {
current++;
if (child == null) {
return null;
} else {
return child.findNode(level, current);
}
} else {
return this;
}
}
public int getLevelCount() {
int count = 1;
ZTreeNode node = getChild();
while (node != null) {
count++;
node = node.getChild();
}
return count;
}
public JSONArray toJSONArray(Calculator c, int level, String text, String value, String pv, boolean checked) throws JSONException {
Map<String, Object> layerMap = new HashMap<String, Object>();
if (pv != null) {
String[] parentValues = pv.split("\\$\\$");
for (int i = 0, len = parentValues.length; i < len; i ++) {
layerMap.put(GeneralKit.objectToString(i + 1), parentValues[i]);
}
}
layerMap.put(GeneralKit.objectToString(level + 1), value);
NameSpace ns = CalculatorKit.createTreeLayerNameSpace(layerMap);
c.pushNameSpace(ns);
JSONArray data = new JSONArray();
Iterator entryIt = dictionary.entrys(c);
while (entryIt.hasNext()) {
Dictionary.MV mv = (Dictionary.MV) entryIt.next();
JSONObject jo = new JSONObject();
jo.put("text", mv.getView());
jo.put("value", mv.getModel());
jo.put("pv", ZTreeHelper.join(new String[]{pv, value}, "$$"));
jo.put("isParent", getChild() != null);
jo.put("checked", checked);
data.put(jo);
}
c.removeNameSpace(ns);
return data;
}
@Override
public void readXML(XMLableReader reader) {
if (reader.isChildNode()) {
String tagName = reader.getTagName();
if (tagName.equals(Dictionary.XML_TAG)) {
dictionary = (Dictionary) XmlKit.readXMLable(reader);
} else if (tagName.equals(ZTreeNode.XML_TAG)) {
child = (ZTreeNode) XmlKit.readXMLable(reader);
}
}
}
@Override
public void writeXML(XMLPrintWriter writer) {
if (dictionary != null) {
XmlKit.writeXMLable(writer, dictionary, Dictionary.XML_TAG);
}
if (child != null) {
XmlKit.writeXMLable(writer, child, ZTreeNode.XML_TAG);
}
}
@Override
public Object clone() throws CloneNotSupportedException {
ZTreeNode cloned = (ZTreeNode) super.clone();
cloned.dictionary = (Dictionary) dictionary.clone();
cloned.child = (ZTreeNode) child.clone();
return cloned;
}
}