首页 > 代码库 > 5.29 diagramEditor中的save

5.29 diagramEditor中的save

public class DiagramEditor extends GraphicalEditorWithFlyoutPalette implements IPersistableEditor{


public
void doSave(IProgressMonitor monitor) { // TODO Auto-generated method stub performSave(monitor, getEditorInput()); getCommandStack().markSaveLocation(); } @Override public void doSaveAs() { // TODO Auto-generated method stub Shell shell = getSite().getShell(); final IEditorInput input = getEditorInput(); SaveAsDialog dialog = new SaveAsDialog(shell); IFile original = (input instanceof IFileEditorInput) ? ((IFileEditorInput) input).getFile(): null; if (original != null){ dialog.setOriginalFile(original);} dialog.create(); int result = dialog.open(); 返回的是cancel或者ok if (result == Window.CANCEL) { return; } IPath filePath = dialog.getResult(); IFile file = null; if (filePath == null) { return; } else { file = ResourcesPlugin.getWorkspace().getRoot().getFile(filePath); } if (input.exists()) { } else { dialog.setErrorMessage(null); dialog.setMessage(WARN_FILEEXIST, IMessageProvider.WARNING); return; } if(file.exists()){ }else{ try { file.create(ResourceURLHelper.getNullInputStream(), true, null); } catch (CoreException e) { // TODO Auto-generated catch block e.printStackTrace(); } } DiagramEditorInput newInput = new DiagramEditorInput(file.getLocation()); performSave(getProgressMonitor(),newInput); } @Override public boolean isSaveAsAllowed() { // TODO Auto-generated method stub return true; } 这个才是核心程序!重要private void performSave(IProgressMonitor monitor, IEditorInput input) { if (diagram == null) return; diagram.doSaveXML(); Document xmlTree = diagram.getDiagramDoc(); if (xmlTree == null) return; IFile file = ResourceUtil.getFile(getEditorInput()); OutputFormat format = OutputFormat.createPrettyPrint(); try { FileWriter fileWriter; fileWriter = new FileWriter((String) input.getAdapter(Path.class)); XMLWriter writer = new XMLWriter(fileWriter, format); writer.write(xmlTree); writer.close(); if (file != null) { file.refreshLocal(IResource.DEPTH_ZERO, new NullProgressMonitor()); } } catch (IOException e) { e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } } @Override public void commandStackChanged(EventObject event) { // TODO Auto-generated method stub firePropertyChange(IEditorPart.PROP_DIRTY); super.commandStackChanged(event); } @Override public boolean isDirty() { // TODO Auto-generated method stub return getCommandStack().isDirty(); }
}

这里调用了diagram.doSaveXML(),所以接下来看diagrammodel的dosavexml()方法

public class DiagramModel extends AbstractModel {
   这些个都是diagram中包含的元素吗?
private List<Object> children = new ArrayList<Object>(); private List<Object> ports = new ArrayList<Object>(); private List<Object> texts = new ArrayList<Object>(); private List<Object> containers = new ArrayList<Object>(); private List<Object> portOps = new ArrayList<Object>(); private List<Object> refers = new ArrayList<Object>();
private
ComponentInstanceModel component; 一直没搞懂这个componentInstanceModel和portInstanceModel是用来干嘛的。 private DiagramHeadingModel diagramHead; 这个model也没搞懂是用来干嘛的。 private String name; private Document diagramDoc; private Element rootElement,diagramElement,diagramExElement; public DiagramModel() { super(); name=""; initModel(); } public void initModel(){ setName("Diagram"); setDiagramDoc(DocumentHelper.createDocument()); setRootElement(getDiagramDoc().addElement(ModelType.ELE_ROOT)); setDiagramElement(getRootElement().addElement(ModelType.ELE_DIAGRAM)); setDiagramExElement(getRootElement().addElement(ModelType.ELE_DIAGRAMEX));???? } @Override public void doSaveXML() { // TODO Auto-generated method stub this.addDiagramAttribute(ModelType.ATR_NAME, name); getDiagramHead().doSaveXML(); getComponent().doSaveXML(); for(int i=0;i<getPorts().size();i++){ ((ElementModel)getPorts().get(i)).doSaveXML(); } for(int i=0;i<getTexts().size();i++){ ((ElementModel)getTexts().get(i)).doSaveXML(); } } protected void addDiagramAttribute(String name,String value){ if(getDiagramElement()==null){ return; } getDiagramElement().addAttribute(name, value); } protected void removeDiagramAttribute(String name){ if(getDiagramElement()==null){ return; } Attribute atr = getDiagramElement().attribute(name); if(atr != null){ getDiagramElement().remove(atr); } } }
1. getDiagramHead().doSaveXML();

public class DiagramHeadingModel extends ElementModel {
        public DiagramHeadingModel() {
        super(ConstantResourceFactory.LABEL_DIAGRAMHEAD_MODEL,
                ConstantResourceFactory.ID_DIAGRAMHEAD_MODEL,
                ConstantResourceFactory.SIZE_DIAGRAMHEAD);
        // TODO Auto-generated constructor stub
        definition="";
        myCtype="";
        myReturn="";
        mySystem="";
        diagramType="";
        isRetShow=false;
        isSysShow=false;
        isRunsOnShow=false;
    }
        public void doSaveXML() {
        // TODO Auto-generated method stub
        super.doSaveXML();
        
        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);
        }
    }
}

因为ComponentInstanceModel继承自portElementModel
所以先看portElementModel的dosaveXML()

public abstract class PortElementModel extends ElementModel
{
    public PortElementModel(String label, String modelID, Dimension size) {
        super(label, modelID, size);
        // TODO Auto-generated constructor stub
        name="";
        type="";
    }
    public void doSaveXML() {
         // TODO Auto-generated method stub
        super.doSaveXML();
        this.addXMLElementAttribute(ModelType.ELE_NAME, name);
        this.addXMLElementAttribute(ModelType.ELE_TYPE, type);
    }  
}
getComponentInstance.doSaveXML();
{
public ComponentInstanceModel() {
        super(ConstantResourceFactory.LABEL_COMPONENTINSTANCE_MODEL,
                ConstantResourceFactory.ID_COMPONENTINSTANCE_MODEL,
                ConstantResourceFactory.SIZE_PORT_ELEMENT);
        // TODO Auto-generated constructor stub
        diagramType = "";
    }
public void doSaveXML() {
        // TODO Auto-generated method stub
        super.doSaveXML();        
        
        for(int i=0;i<children.size();i++){
            ((ElementModel)children.get(i)).doSaveXML();  子元素的dosavexml()
        }
        
        if(tail!=null)
             tail.doSaveXML();这里的tail是elementmodel,所以调用的是elementmodel.dosavexml()
    }
}