最上层的父类:
public interface IXmlModel {
public void initModel(); 初始化
public void doSaveXML(); 保存为xml文件
public void rebuildFromXml(Element e,List<Object> err); 反过程
}
AbstractModel继承IXmlModel。
ElementModel继承AbstractModel。
ElementModel的部分程序:
其中重写了initmodel()方法,set了四个东西:modelID(应该是模型本身的ID)、modelLable(应该是用户可以修改的名字)、XMLhead(是在xml文档中的标签么<name>这种?)、XMLmodel(暂时不知)
public abstract class ElementModel extends AbstractModel implements IDirectEditModel,IConstraint{
protected String xmlHead;
protected Element xmlModel;public String getXMLHead() {
return xmlHead;
}public void setXMLHead(String xmlHead) {
this.xmlHead = xmlHead;
}public Element getXMLModel() {
return xmlModel;
}public void setXMLModel(Element xmlModel) {
this.xmlModel = xmlModel;
}public void addXMLElementAttribute(String name,String value){
if(xmlModel==null){
return;
}
xmlModel.addAttribute(name, value);
}public void removeXMLElementAttribute(String name){
if(xmlModel==null){
return;
}Attribute atr = xmlModel.attribute(name);
if( atr != null){
xmlModel.remove(atr);
}}
public void doSaveXML(){
this.addXMLElementAttribute(ModelType.ATR_NAME, modelName);
this.addXMLElementAttribute(ModelType.ATR_ID, ID.toString());
}public void rebuildFromXml(Element e,List<Object> err) {
// TODO Auto-generated method stub
}
public void initModel(){
setID(ModelIDFactory.getNewModelID());
setModelName(getLabel());
setXMLHead(ModelType.getModelXMLHead(this));
setXMLModel(DocumentHelper.createElement(getXMLHead()));
}
}
根据我的理解,这个modeltype类应该目的是要通过elementmodel的类型来返回对应的xmlhead。
曾经在partfactory中用过这种 ,通过model的类型,来返回对应的editpart。
所以我需要考虑的问题是:
我有哪些elementmodel,需要定义哪些xmlhead。
public class ModelType {
public static final String HEAD_DIAGRAMHEAD = "DiagramHead";
//container model XML head
//container model separator XML head
//figure model XML head
//literal model XML head
//connection model XML head
public final static String HEAD_CONNECTION="Connect";
public final static String HEAD_FOUND=HEAD_CONNECTION+"FoundModel";
public final static String HEAD_CONMSGFROM=HEAD_CONNECTION+"MessageFrom";
public final static String HEAD_CONMSGTO=HEAD_CONNECTION+"MessageTo";
//port model XML head
//XML attribute Name
public final static String ATR_NAME="ModelName";
public final static String ATR_ID="ModelID";
//XML Element Name
public final static String ELE_ROOT="Root";
public final static String ELE_VALUE="Value";
public final static String ELE_NAME="Name";
public final static String ELE_TYPE="Type";
public static String getModelXMLHead(ElementModel model){
if(model instanceof ContainerElementModel)
return getContainerModelXMLHead(model);
else if(model instanceof FigureElementModel)
return getFigureModelXMLHead(model);
else if(model instanceof LiteralElementModel)
return getLiteralModelXMLHead(model);
else if(model instanceof ConnectionElementModel)
return getConnectioModelXMLHead(model);
else if(model instanceof PortElementModel)
return getPortModelXMLHead(model);
else if(model instanceof ContainerSeperatorModel)
return getSeparatorXMLHead(model);
else if(model instanceof DiagramHeadingModel)
return HEAD_DIAGRAMHEAD;return HEAD_ERROR;
}
}
上述的每一个getxxxModelXmlHead方法中,又分别根据子model的不同,返回对应的XmlHead。
继承elementModel的类有:
public abstract class ContainerElementModel extends ElementModel implements IContainer
public abstract class PortElementModel extends ElementModel
public abstract class LiteralElementModel extends ElementModel
public abstract class ConnectionElementModel extends ElementModel implements IShowHideLabel,IConnectMessage
public class DiagramHeadingModel extends ElementModel
public abstract class FigureElementModel extends ElementModel
1.首先看DiagramHeadingModel
public class DiagramHeadingModel extends ElementModel{
public void doSaveXML() {
// TODO Auto-generated method stub
super.doSaveXML(); 父类中是添加了ModelType.ATR_ID(属性名),和ModelType.ATR_NAME(属性显示名),这应该是所有的子elementmodel的公有属性。this.addXMLElementAttribute(ModelType.ATR_DIAGRAM,diagramType); 前者是属性名,后者是属性值。
this.addXMLElementAttribute(ModelType.ATR_DEFINITION,definition);后面这部分不知道做什么的。
if(isRunsOnShow()){
this.addXMLElementAttribute(ModelType.ATR_RUNSON,myCtype);
}
else{
this.removeXMLElementAttribute(ModelType.ATR_RUNSON);
}if(isRetShow()){
this.addXMLElementAttribute(ModelType.ATR_RETURN,myReturn);
}
else{
this.removeXMLElementAttribute(ModelType.ATR_RETURN);
}if(isSysShow()){
this.addXMLElementAttribute(ModelType.ATR_SYSTEM,mySystem);
}
else{
this.removeXMLElementAttribute(ModelType.ATR_SYSTEM);
}
}@Override
public void rebuildFromXml(Element e,List<Object> err) {
// TODO Auto-generated method stub
super.rebuildFromXml(e,err);if(e.attribute(ModelType.ATR_DIAGRAM) != null){
diagramType = e.attributeValue(ModelType.ATR_DIAGRAM);
}
else{
err.add(new CustomErrorInfo(
e,
CustomErrorInfo.NULL,
ModelType.ATR_DIAGRAM));
}if(e.attribute(ModelType.ATR_DEFINITION) != null){
definition = e.attributeValue(ModelType.ATR_DEFINITION);
}
else{
err.add(new CustomErrorInfo(
e,
CustomErrorInfo.NULL,
ModelType.ATR_DEFINITION));
}if(e.attribute(ModelType.ATR_RUNSON) != null){
myCtype = e.attributeValue(ModelType.ATR_RUNSON);
setRunsOnShow(true);
}
else{
setRunsOnShow(false);
}if(e.attribute(ModelType.ATR_RETURN) != null){
myReturn = e.attributeValue(ModelType.ATR_RETURN);
setRetShow(true);
}
else{
setRetShow(false);
}if(e.attribute(ModelType.ATR_SYSTEM) != null){
mySystem = e.attributeValue(ModelType.ATR_SYSTEM);
setSysShow(true);
}
else{
setSysShow(false);
}}
}
2.portelementmodel
public abstract class PortElementModel extends ElementModel{
private static final
IPropertyDescriptor[] descriptorComp = new IPropertyDescriptor[]{
new PropertyDescriptor(SELECT_LABEL,"Model Label"),
new PropertyDescriptor(SELECT_NAME,"Component Name"),
new TextPropertyDescriptor(SELECT_TYPE,"Component Type"),
};private static final
IPropertyDescriptor[] descriptorPort = new IPropertyDescriptor[]{
new PropertyDescriptor(SELECT_LABEL,"Model Label"),
new TextPropertyDescriptor(SELECT_NAME,"Port Name"),
new TextPropertyDescriptor(SELECT_TYPE,"Port Type"),
};
public IPropertyDescriptor[] getPropertyDescriptors() {
// TODO Auto-generated method stub
if(this instanceof ComponentInstanceModel){
return descriptorComp;
}
else if(this instanceof PortInstanceModel){
return descriptorPort;
}
return new IPropertyDescriptor[0];
}
public void doSaveXML() {
// TODO Auto-generated method stub
super.doSaveXML();
this.addXMLElementAttribute(ModelType.ELE_NAME, name);
this.addXMLElementAttribute(ModelType.ELE_TYPE, type);
}@Override
public void rebuildFromXml(Element e,List<Object> err) {
// TODO Auto-generated method stub
super.rebuildFromXml(e,err);if(e.attribute(ModelType.ELE_NAME) != null){
name = e.attributeValue(ModelType.ELE_NAME);
}
else{
err.add(new CustomErrorInfo(
e,
CustomErrorInfo.NULL,
ModelType.ELE_NAME));
}if(e.attribute(ModelType.ELE_TYPE) != null){
type = e.attributeValue(ModelType.ELE_TYPE);
}
else{
err.add(new CustomErrorInfo(e,CustomErrorInfo.NULL,ModelType.ELE_TYPE));
}
}}
5.28 xml