码迷,mamicode.com
首页 > 其他好文 > 详细

5.29 diagramEditor中的save

时间:2014-06-04 20:04:59      阅读:397      评论:0      收藏:0      [点我收藏+]

标签:c   style   class   blog   code   java   

bubuko.com,布布扣

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(); }
}
bubuko.com,布布扣

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

bubuko.com,布布扣
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); } } }
bubuko.com,布布扣
bubuko.com,布布扣
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);
        }
    }
}
bubuko.com,布布扣
bubuko.com,布布扣

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

public abstract class PortElementModel extends ElementModel
{
bubuko.com,布布扣
    public PortElementModel(String label, String modelID, Dimension size) {
        super(label, modelID, size);
        // TODO Auto-generated constructor stub
        name="";
        type="";
    }
bubuko.com,布布扣
bubuko.com,布布扣
    public void doSaveXML() {
         // TODO Auto-generated method stub
        super.doSaveXML();
        this.addXMLElementAttribute(ModelType.ELE_NAME, name);
        this.addXMLElementAttribute(ModelType.ELE_TYPE, type);
    }  
bubuko.com,布布扣
}
bubuko.com,布布扣
bubuko.com,布布扣
getComponentInstance.doSaveXML();
{
bubuko.com,布布扣
public ComponentInstanceModel() {
        super(ConstantResourceFactory.LABEL_COMPONENTINSTANCE_MODEL,
                ConstantResourceFactory.ID_COMPONENTINSTANCE_MODEL,
                ConstantResourceFactory.SIZE_PORT_ELEMENT);
        // TODO Auto-generated constructor stub
        diagramType = "";
    }
bubuko.com,布布扣
bubuko.com,布布扣
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()
    }
bubuko.com,布布扣
}
bubuko.com,布布扣

 

 

5.29 diagramEditor中的save,布布扣,bubuko.com

5.29 diagramEditor中的save

标签:c   style   class   blog   code   java   

原文地址:http://www.cnblogs.com/liyuhui20093357/p/3759399.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!