插件开发工具库,推荐依赖该工具库。
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.
 
 

253 lines
7.4 KiB

package com.fanruan.api.report.form.category;
import com.fanruan.api.report.form.BaseWidget;
import com.fanruan.api.report.form.WidgetKit;
import com.fr.base.BaseXMLUtils;
import com.fr.base.iofile.IOFileAttrMarkReader;
import com.fr.form.ui.LayoutBorderStyle;
import com.fr.form.ui.PaddingMargin;
import com.fr.form.ui.RichStyleWidgetProvider;
import com.fr.general.Background;
import com.fr.general.act.BorderPacker;
import com.fr.general.xml.GeneralXMLTools;
import com.fr.json.JSONException;
import com.fr.json.JSONObject;
import com.fr.plugin.solution.sandbox.collection.PluginSandboxCollections;
import com.fr.stable.fun.IOFileAttrMark;
import com.fr.stable.script.CalculatorProvider;
import com.fr.stable.web.Repository;
import com.fr.stable.xml.XMLConstants;
import com.fr.stable.xml.XMLPrintWriter;
import com.fr.stable.xml.XMLReadable;
import com.fr.stable.xml.XMLableReader;
import com.fr.web.BaseHTMLWriterUtils;
import java.util.Map;
/**
* @author richie
* @version 10.0
* Created by richie on 2019/10/25
* 带边框和样式设置的控件基类
*/
public abstract class BaseBorderStyleWidget extends BaseWidget implements RichStyleWidgetProvider {
private BorderPacker borderStyle = WidgetKit.newLayoutBorderStyle();
private Background background;
private PaddingMargin margin = new PaddingMargin();
// widget对象添加额外的属性: 是否共享之类的
private Map<String, IOFileAttrMark> widgetAttrMarkMap;
public BaseBorderStyleWidget() {
}
/**
* 获取背景
*/
public Background getBackground() {
return background;
}
/**
* 设置主体背景
*/
public void setBackground(Background background) {
this.background = background;
}
/**
* 获取边框样式
*/
public BorderPacker getBorderStyle() {
return borderStyle;
}
/**
* 设置边框样式
*/
public void setBorderStyle(BorderPacker borderStyle) {
this.borderStyle = borderStyle;
// 主体的背景也都是放在borderStyle
setBackground(borderStyle.getBackground());
}
/**
* 获取边距
*/
public PaddingMargin getMargin() {
return margin;
}
/**
* 设置边距
*/
public void setMargin(PaddingMargin margin) {
if (!canCurrentMarginAvailable(margin)) {
throw new UnsupportedOperationException();
} else {
this.margin = margin;
}
}
/**
* 清除边距, 默认的是(1,1,1,1).
*/
public void clearMargin() {
this.margin = new PaddingMargin(0, 0, 0, 0);
}
/**
* 给widget对象添加额外的属性
*
* @param attrMark 额外的属性对象
*/
public void addWidgetAttrMark(IOFileAttrMark attrMark) {
if (attrMark == null) {
return;
}
if (widgetAttrMarkMap == null) {
widgetAttrMarkMap = PluginSandboxCollections.newSandboxMap();
}
widgetAttrMarkMap.put(attrMark.xmlTag(), attrMark);
}
/**
* 根据标签获取widget的属性对象
*
* @param tagName 标签名
* @param <T> 对象类型
* @return 属性对象
*/
public <T extends IOFileAttrMark> T getWidgetAttrMark(String tagName) {
if (widgetAttrMarkMap == null) {
return null;
}
IOFileAttrMark mark = widgetAttrMarkMap.get(tagName);
return (T) mark;
}
/**
* 当前设置的边距值是否可用,若设置后有控件达到最小尺寸则返回false
*
* @param margin 边距
* @return 可用则返回true
*/
public abstract boolean canCurrentMarginAvailable(PaddingMargin margin);
/**
* 设置边框样式
*/
public void setBorderStyleNoBackground(LayoutBorderStyle borderStyle) {
this.borderStyle = borderStyle;
}
@Override
public void mixinJSON(Repository repo, CalculatorProvider c, JSONObject jo) {
if (PaddingMargin.need(margin)) {
margin.createJSONConfig(jo);
}
if (this.background != null) {
createBackgroundJson(repo, jo);
}
if (this.borderStyle != null) {
borderStyle.mixinJSON(repo, c, jo);
}
if (widgetAttrMarkMap != null) {
for (Map.Entry<String, IOFileAttrMark> entry : widgetAttrMarkMap.entrySet()) {
jo.put(entry.getKey(), entry.getValue().createJSONConfig());
}
}
}
private void createBackgroundJson(Repository repo, JSONObject jo) throws JSONException {
if (repo.getDevice().isMobile()) {
jo.put("widgetBackground", background.toJSONObject(repo));
} else {
jo.put("widgetBackground", BaseHTMLWriterUtils.jsonBackground(background, repo));
}
}
@Override
public void readXML(XMLableReader reader) {
super.readXML(reader);
if (reader.isChildNode()) {
String tagName = reader.getTagName();
if (PaddingMargin.XML_TAG.equals(tagName)) {
PaddingMargin newMargin = new PaddingMargin();
reader.readXMLObject(newMargin);
this.margin = newMargin;
} else if (XMLConstants.Border_TAG.equals(tagName)) {
final LayoutBorderStyle border = new LayoutBorderStyle();
reader.readXMLObject(new XMLReadable() {
public void readXML(XMLableReader reader) {
reader.readXMLObject(border);
}
});
this.setBorderStyle(border);
} else if (XMLConstants.Background_TAG.equals(tagName)) {
this.background = BaseXMLUtils.readBackground(reader);
} else {
readExtra(reader);
}
}
}
@Override
public void writeXML(XMLPrintWriter writer) {
super.writeXML(writer);
if (this.margin != null) {
this.margin.writeXML(writer);
}
if (this.borderStyle != null) {
this.borderStyle.writeXML(writer);
}
if (this.background != null) {
BaseXMLUtils.writeBackground(writer, this.background);
}
writeExtra(writer);
}
private void writeExtra(XMLPrintWriter writer) {
if (widgetAttrMarkMap != null) {
for (Map.Entry<String, IOFileAttrMark> entry : widgetAttrMarkMap.entrySet()) {
GeneralXMLTools.writeXMLable(writer, entry.getValue(), entry.getKey());
}
}
}
protected void readExtra(XMLableReader reader) {
addWidgetAttrMark(IOFileAttrMarkReader.read(reader));
}
@Override
public Object clone() throws CloneNotSupportedException {
BaseBorderStyleWidget cloned = (BaseBorderStyleWidget) super.clone();
if (this.margin != null) {
cloned.margin = (PaddingMargin) this.margin.clone();
}
if (widgetAttrMarkMap != null) {
Map<String, IOFileAttrMark> clonedMap = PluginSandboxCollections.newSandboxMap();
for (Map.Entry<String, IOFileAttrMark> entry : widgetAttrMarkMap.entrySet()) {
clonedMap.put(entry.getKey(), entry.getValue().clone());
}
cloned.widgetAttrMarkMap = clonedMap;
}
if (this.background != null) {
cloned.background = (Background) this.background.clone();
}
cloned.borderStyle = (LayoutBorderStyle) this.borderStyle.clone();
return cloned;
}
}