Save and open xml file with objects

Hi Support!

I am using this code example and I have 1 question:

http://www.nwoods.com/forum/UserUploads/20101223_100240_TitleBarSubGrap.txt

I am saving TitleBarSubGraphin in a xml file.
If I resize it, and save… after open see that LimitingSubGraphMarker size was changed. Is there a way to resolve this problem?

Thanks!!




Code to save:


	               GoXmlBindingTransformer.DefaultTracingEnabled = true; 
			GoXmlBindingTransformer t;


                       //Read-Save SubProcess
			t = new GoXmlBindingTransformer("MyTitleBarSubGraph ", new TitleBarSubGraph());
			t.HandlesChildren = true;   
			t.IdAttributeUsedForSharedObjects = true; 
			t.HandlesNamedPorts = true;  
			t.AddBinding("text", "Text");
			t.AddBinding("loc", "Location");
			t.AddBinding("size", "Size");
			rw.AddTransformer(t);

Try setting obj.AutoRescales = false on the Markers.

Hi Jake!

Is this the only way to do it? The result was not good.

The LimitingSubGraphMarker stay with the same size…But the GoSubGraph object ( SubProcess) size was ignored when user open file after set AutoRescales property.

In the file, the size is correct:

<subprocess id="0" Port="1" text="SubProcess" loc="220 80" size="415 275" />

After I inserted obj.AutoRescales = false ;

When user open file, the GoSubGraph object size was ignored !!

Code:

//SubProcessItem Class: SubProcess
[Serializable]
public class SubProcessItem : GoSubGraph
{
    //Variables
    private ResourceManager rm;

    public SubProcessItem()
    {
        //this.LabelSpot = GoObject.MiddleTop;  //  default
        this.CollapsedLabelSpot = GoObject.MiddleBottom;
        this.Corner = new SizeF(5, 5);

        this.TopLeftMargin = new SizeF(10, 5);
        this.BottomRightMargin = new SizeF(10, 10);

        this.CollapsedTopLeftMargin = new SizeF(10, 5);
        this.CollapsedBottomRightMargin = new SizeF(10, 2);

        this.Opacity = 50f;
        this.BackgroundColor = Color.LightGray;
        this.BorderPenColor = Color.Gray;

        this.Handle.Style = GoCollapsibleHandleStyle.PlusMinus;
        this.Handle.Brush = null;
        this.Handle.Bordered = false;

        GoObject obj = new LimitingSubGraphMarker();
        obj.Position = new PointF(10, 10);
        Add(obj);
        obj = new LimitingSubGraphMarker();
        obj.Position = new PointF(200, 200);
        Add(obj);
    }

    protected override GoText CreateLabel()
    {
        //Resources
        rm = new ResourceManager("constants", this.GetType().Assembly);

        GoText label = base.CreateLabel();
        label.Text = rm.GetString("PROCESS_DIAGRAM_SUBPROCESSITEM");
        label.Bold = false;
        //label.FontSize = 8;
        label.Alignment = GoObject.MiddleTop;
        label.AutoResizes = false;
        label.Multiline = false;
        label.StringTrimming = StringTrimming.EllipsisCharacter;
        return label;
    }

    protected override void PaintDecoration(Graphics g, GoView view)
    {
        base.PaintDecoration(g, view);
        if (this.IsExpanded)
        {
            RectangleF rect = ComputeBorder();
            float y = this.Label.Bottom + 2;
            GoShape.DrawLine(g, view, this.BorderPen, rect.X, y, rect.Right, y);
        }
    }

    public override void LayoutLabel()
    {
        GoText lab = this.Label;
        if (lab == null)
            return;
        RectangleF rect;
        // just let the label sit in place on a collapse
        if (this.IsExpanded)
        {
            // don't move label if it's the only thing left (besides the standard children)
            int numchildren = 0;
            if (this.Handle != null) numchildren++;
            if (this.Label != null) numchildren++;
            if (this.Port != null) numchildren++;
            if (this.CollapsedObject != null) numchildren++;
            if (numchildren == this.Count) return;

            // find bounds of meaningful children, excluding Label and ignoring Margin
            rect = ComputeInsideMargins(lab);
            PointF pt = new PointF(rect.X + rect.Width / 2, rect.Y - this.TopLeftMargin.Height - lab.Height);
            lab.SetSpotLocation(TopCenter, pt);
        }
    }

    public override void LayoutHandle()
    {
        if (!this.IsExpanded) return;
        GoSubGraphHandle h = this.Handle;
        if (h != null)
        {
            RectangleF rect = ComputeInsideMargins(h);
            // top-right, inside margin
            PointF pt = new PointF(rect.Right, rect.Y - this.TopLeftMargin.Height);
            if (this.Label != null) pt.Y = this.Label.Location.Y;

            h.SetSpotLocation(TopRight, pt);
        }
    }

    // Make sure the collapsed subgraph body (including any CollapsedObject)
    // is positioned to the left of the handle.
    protected override RectangleF ComputeCollapsedRectangle(SizeF s)
    {
        PointF hpos = ComputeReferencePoint();
        return new RectangleF(hpos.X + this.Handle.Width - s.Width, hpos.Y, s.Width, s.Height);
    }

    public override PointF Location
    {
        get
        {
            if (this.Label != null)
                return this.Label.Position;
            else
                return this.Position;
        }
        set
        {
            if (this.Label != null)
            {
                // we move the node, which substitutes presumably unnecessary calls to LayoutChildren with calls to MoveChildren
                SizeF off = GoTool.SubtractPoints(this.Label.Position, this.Position);
                this.Position = new PointF(value.X - off.Width, value.Y - off.Height);
            }
            else
            {
                this.Position = value;
            }
        }
    }

    public override void DoResize(GoView view, RectangleF origRect, PointF newPoint,int whichHandle, GoInputState evttype, SizeF min, SizeF max)
    {
        base.DoResize(view, origRect, newPoint, whichHandle, evttype, min, max);
        LayoutPort();
        if (this.IsExpanded)
        {
            LayoutHandle();
        }
    }

    protected override GoPort CreatePort()
    {
        GoBoxPort bp = new GoBoxPort();
        bp.LinkPointsSpread = true;
        bp.Style = GoPortStyle.None;
        bp.IsValidTo = false;
        bp.IsValidFrom = false;
        return bp;

    }

    public override void LayoutPort()
    {
        GoPort p = this.Port;
        if (p != null && p.CanView())
        {
            RectangleF r = ComputeBorder();
            p.Bounds = r;
        }
    }

    public override RectangleF ComputeInsideMargins(GoObject ignore)
    {
        RectangleF rect = base.ComputeInsideMargins(ignore);

        // if width of subgraph changes, change width of label to correspond.
        if (this.Label != null && ignore == this.Label && this.State == GoSubGraphState.Expanded)
        {
            this.Label.Width = rect.Width;
        }
        if (this.Label != null && ignore == null && this.State == GoSubGraphState.Collapsed)
        {
            rect = this.Label.Bounds;
        }
        return rect;
    }

    // making collapsed nodes a fixed size (the size of the label)
    public override SizeF ComputeCollapsedSize(bool visible)
    {
        SizeF sz = base.ComputeCollapsedSize(visible);
        if (this.Label != null) sz = this.Label.Size;
        return sz;
    }

    public override bool OnSelectionDropped(GoObjectEventArgs evt, GoView view)
    {
        if (true)
        { //this.AllowDragInOut
            // add all selected objects to this BoxArea
            view.Selection.AddRange(AddCollection(view.Selection, true));
            return true;
        }
        //else {
        //  return false;
        //}
    }

    //Context Menu
    public override bool OnContextClick(GoInputEventArgs evt, GoView view)
    {
        GoDocument doc = this.Document;
        GenerateProcess GenProcess = doc as GenerateProcess;


        if ((evt.Buttons == MouseButtons.Right) && (GenProcess != null))
        {
            if ((GenProcess.ProcessDiagramBarManager != null) && (GenProcess.ProcessDiagramPopupMenu != null))
            {
                //Clear the contents of PopUp Menu.
                GenProcess.ProcessDiagramBarManager.Items.Clear();
                GenProcess.ProcessDiagramPopupMenu.LinksPersistInfo.Clear();

                SubProcessItem selSubProcess = this as SubProcessItem;
                if (selSubProcess != null)
                {
                    //Create Menu Item: Remove SubProcess Item 
                    BarButtonItem RemoveSubProcessItem = new BarButtonItem();
                    RemoveSubProcessItem.Caption = rm.GetString("PROCESSDIAGRAM_REMOVE");
                    RemoveSubProcessItem.ItemClick += new ItemClickEventHandler(this.RemoveSubprocessItem_Command);
                    GenProcess.ProcessDiagramBarManager.Items.Add(RemoveSubProcessItem);
                    GenProcess.ProcessDiagramPopupMenu.AddItem(RemoveSubProcessItem);
                }
            }

            if (GenProcess.ProcessDiagramPopupMenu.ItemLinks.Count > 0)
            {
                //Show popupMenuNodes
                GenProcess.ProcessDiagramPopupMenu.ShowPopup(Cursor.Position);
            }

            return true;
        }
        else
        {
            return false;
        }


    }
    //Context Menu events
    private void RemoveSubprocessItem_Command(Object sender, EventArgs e)
    {
        SubProcessItem selSubProcessItem = this as SubProcessItem;
        if (selSubProcessItem != null)
        {
            GoDocument doc = this.Document;
            doc.StartTransaction();
            doc.Remove(this);
            this.LayoutChildren(null);
            doc.FinishTransaction("Removed SubProcessItem");
        }
    }

}




[Serializable]
public class LimitingSubGraphMarker : GoRectangle
{
    public LimitingSubGraphMarker()
    {
        this.Printable = false;
        this.Resizable = false;
        this.Deletable = false;
        this.Copyable = false;
        this.Brush = null;
        this.PenColor = Color.Gray;

this.AutoRescales = false;

    }

    // can't get any selection handles
    public override void AddSelectionHandles(GoSelection sel, GoObject selectedObj) { }

    // only seen when the mouse is over it
    public override bool OnEnterLeave(GoObject from, GoObject to, GoView view)
    {
        if (from == this)
            this.Brush = null;
        if (to == this)
            this.BrushColor = Color.Gray;
        return true;
    }

}

I meant that you should set AutoRescales to false on the Markers, not the subgraph.

Hi Jake. Sorry for all questions, but for me, a newbie in your component, it´s difficult to identify some words that you say…

Please, see these example code in :

http://www.nwoods.com/forum/UserUploads/20101223_100240_TitleBarSubGrap.txt

I copied these class to use in my project.

When you say “<!–[if gte mso 9]><>
<w:Word>
<w:View>Normal</w:View>
<w:Zoom>0</w:Zoom>
<w:TrackMoves/>
<w:Trackatting/>
<w:HyphenationZone>21</w:HyphenationZone>
<w:PunctuationKerning/>
<w:ValidateAgainstSchemas/>
<w:SaveIfInvalid>false</w:SaveIfInvalid>
<w:IgnoreMixedContent>false</w:IgnoreMixedContent>
<w:AlwaysShowPlaceholderText>false</w:AlwaysShowPlaceholderText>
<w:DonotPromoteQF/>
<w:LidThemeOther>PT-BR</w:LidThemeOther>
<w:LidThemeAsian>X-NONE</w:LidThemeAsian>
<w:LidThemeComplex>X-NONE</w:LidThemeComplex>
<w:Compatibility>
<w:BreakWrappedTables/>
<w:SnapToGridInCell/>
<w:WrapTextWithPunct/>
<w:UseAsianBreakRules/>
<w:DontGrowAutofit/>
<w:SplitPgBreakAndark/>
<w:EnableKerning/>
<w:DontFlipMirrorIndents/>
<w:OverrideTableStyleHps/>
</w:Compatibility>
<m:mathPr>
<m:mathFont m:val=“Cambria Math”/>
<m:brkBin m:val=“before”/>
<m:brkBinSub m:val=”–“/>
<m:smallFrac m:val=“off”/>
<m:dispDef/>
<m:lMargin m:val=“0”/>
<m:rMargin m:val=“0”/>
<m:defJc m:val=“centerGroup”/>
<m:wrapIndent m:val=“1440”/>
<m:intLim m:val=“subSup”/>
<m:naryLim m:val=“undOvr”/>
</m:mathPr></w:Word>
</><![endif]–><!–[if gte mso 10]>
<>
/* Style Definitions */
table.MsoNormalTable
{mso-style-name:“Tabela normal”;
mso-tstyle-rowband-size:0;
mso-tstyle-colband-size:0;
mso-style-noshow:yes;
mso-style-priority:99;
mso-style-parent:”";
mso-padding-alt:0cm 5.4pt 0cm 5.4pt;
mso-para-margin:0cm;
mso-para-margin-bottom:.0001pt;
mso-pagination:widow-orphan;
font-size:11.0pt;
font-family:“Calibri”,“sans-serif”;
mso-ascii-font-family:Calibri;
mso-ascii-theme-font:minor-latin;
mso-hansi-font-family:Calibri;
mso-hansi-theme-font:minor-latin;
mso-fareast-language:EN-US;}

<![endif]–><span style=“font-size: 12pt; font-family: “Times New Roman”,“serif”;”>Markers" … Is the LimitingSubGraphMarker class?

Because LimitingSubGraphMarker is a class responsable to configure a small rectangles that was added in GoSubGraph, to control subGraph size… or not??

So, I configured Autorescales = false:

        GoObject obj = new LimitingSubGraphMarker();
        obj.Position = new PointF(10, 10);
        obj.AutoRescales = false;
        Add(obj);

        obj = new LimitingSubGraphMarker();
        obj.Position = new PointF(200, 200);
        obj.AutoRescales = false;
        Add(obj);

Is it?

When you say “Markers” … Is the LimitingSubGraphMarker class?



yes… the two little squares that control the minimum size of the subgraph.



yes, the code looks fine. does that fix the XML restore problem?

→ does that fix the XML restore problem?
NO

You can do the same example to see the problem…I am sending the code that save/restore xml file… What should I do?
And the xml file was saved correctly, with a size expanded for example… The problem is …when user open xml file, this size “is cancelled” bacause of Autorescales=false. Very strange. Please, can you did a test with this class?
http://www.nwoods.com/forum/UserUploads/20101223_100240_TitleBarSubGrap.txt

Thanks!!

    //Save xml file
    public void StoreXml(FileStream ofile)
    {
        GoXmlWriter xw = new GoXmlWriter();
        InitReaderWriter(xw);
        xw.Objects = this;
        xw.Generate(ofile);
    }


    public static void InitReaderWriter(GoXmlReaderWriterBase rw)
    {
        GoXmlBindingTransformer.DefaultTracingEnabled = true;  // for debugging, check your Output window (trace listener)
        GoXmlBindingTransformer t;



        t = new GoXmlBindingTransformer("MyTitleBarSubGraph", new TitleBarSubGraph());
        t.HandlesChildren = true;   
        t.IdAttributeUsedForSharedObjects = true;  
        t.HandlesNamedPorts = true;  
        t.AddBinding("text", "Text");
        t.AddBinding("loc", "Location");
        t.AddBinding("size", "Size");
        rw.AddTransformer(t);

    }

    //Load xml file
    public static GenerateProcess LoadXml(Stream ifile)
    {
        GoXmlReader xr = new GoXmlReader();
        InitReaderWriter(xr);
        GenerateProcess doc = xr.Consume(ifile) as GenerateProcess;
        if (doc == null)
        {
            return null;
        }

        //Set Undo manager for this new document
        doc.UndoManager = new GoUndoManager();
        doc.UndoManager.Clear();

        return doc;
    }

You’re going to have to store the positions of the 2 Marker objects in the XML and restore them too.



Add get/set properties for them to the subgraph (for example LocationMarker1 and LocationMarker2) and then you’ll be able to add them in AddBinding.

ok, perfect!!!
Thanks!