/// <summary>
        /// The initialization of a platform consumes 2 main dataDelivery sources.
        /// The settings is used for primary, startup information (like where 
        /// is the persistence DB file etc.) and the information inside the 
        /// persistence is than on used to create components etc.
        /// </summary>
        public bool Initialize(PlatformSettings platformSettings)
        {
            TracerHelper.TraceEntry();

            SystemMonitor.CheckThrow(_settings == null, "Platform already initialized.");

            lock (this)
            {
                _settings = platformSettings;
                if (_persistenceHelper == null)
                {
                    _persistenceHelper = CreatePlatformPersistenceHelper(platformSettings);
                }
            }

            if (_persistenceHelper == null)
            {
                return false;
            }

            LoadModules(_settings);

            if (PersistenceHelper.Count<Platform>(new MatchExpression("Name", this.Name)) == 0)
            {// This is a new platform.
                lock (this)
                {
                    _guid = Guid.NewGuid();
                }

                if (PersistenceHelper.Insert<Platform>(this, null) == false)
                {
                    SystemMonitor.Error("Failed to persist new platform [" + this.Name + "]");
                    return false;
                }
            }
            else
            {// This is existing.
                // Now try to load self from persistance storage.
                bool selectionResult = PersistenceHelper.SelectScalar<Platform>(this, new MatchExpression("Name", this.Name));

                if (selectionResult == false)
                {// Failed to load self from DB.
                    return false;
                }
            }

            lock (this)
            {
                if (_serializationData.ContainsValue("diagnosticsMode"))
                {
                    _settings.DiagnosticsMode = _serializationData.GetBoolean("diagnosticsMode");
                }

                if (_serializationData.ContainsValue("uiSerializationInfo"))
                {
                    // The main serialization dataDelivery stores the UI orderInfo.
                    _uiSerializationInfo = _serializationData.GetValue<SerializationInfoEx>("uiSerializationInfo");
                }

                if (_serializationData.ContainsValue("componentSpecificSerializationInfo"))
                {
                    // The main serialization dataDelivery stores the UI orderInfo.
                    _componentSpecificSerializationInfo = _serializationData.GetValue<SerializationInfoEx>("componentSpecificSerializationInfo");
                }
            }

            //_server = new Arbiter.TransportIntegrationServer(_platformUri);
            //Arbiter.AddClient(_server);

            GeneralHelper.FireAndForget(delegate()
            {// LoadFromFile components.
                // Registering of components is better done outside the lock,
                // since components may launch requests to platform at initializations.

                _isLoading = true;

                // Components are stored in the PlatformComponents database, they are being serialized and the entire object is
                // persisted in the DB, as well as the type information for it and a reference to the platform instance it belongs to.
                List<long> failedSerializationsIds = new List<long>();
                List<PlatformComponent> components = PersistenceHelper.SelectSerializedType<PlatformComponent>(
                    new MatchExpression("PlatformId", this.Id), "Data", null, failedSerializationsIds);

                SortedList<int, List<PlatformComponent>> componentsByLevel = GetComponentsByLevel(components);

                GatherMandatoryComponents(componentsByLevel);

                foreach (int level in componentsByLevel.Keys)
                {// Register lower level components first.
                    foreach (PlatformComponent component in componentsByLevel[level])
                    {
                        if (DoRegisterComponent(component, true) == false
                            && component.Id.HasValue && ComponentDeserializationFailedEvent != null)
                        {
                            ComponentDeserializationFailedEvent(component.Id.Value, component.GetType().Name);
                        }
                    }
                }

                // Handle failed deserializations.
                foreach (int id in failedSerializationsIds)
                {
                    string typeName = "Unknown";

                    try
                    {// Extract the type of this entry.
                        List<object[]> result = PersistenceHelper.SelectColumns<PlatformComponent>(
                            new MatchExpression("Id", id), new string[] { "Type" }, 1 );

                        Type type = Type.GetType(result[0][0].ToString());

                        if (type != null)
                        {
                            typeName = type.Name;
                        }
                    }
                    catch(Exception ex)
                    {
                        SystemMonitor.Error("Failed to extract type information [" + ex.Message + "].");
                    }

                    if (ComponentDeserializationFailedEvent != null)
                    {
                        ComponentDeserializationFailedEvent(id, typeName);
                    }
                }

                _isLoading = false;
            });

            return true;
        }
        /// <summary>
        /// Deserialization constructor.
        /// </summary>
        /// <param name="orderInfo"></param>
        /// <param name="context"></param>
        public void SaveState(SerializationInfoEx info, bool saveCustomObjects)
        {
            info.AddValue("Name", this.Name);
            info.AddValue("Height", this.Height);

            info.AddValue("_stateId", _stateId);
            info.AddValue("_titleFont", _titleFont);

            info.AddValue("_titleFontBrush", _titleFontBrush);
            info.AddValue("_fill", _fill);

            info.AddValue("_axisLabelsFont", _axisLabelsFont);
            info.AddValue("_xAxisLabelsFontBrush", _xAxisLabelsFontBrush);
            info.AddValue("_yAxisLabelsPosition", _yAxisLabelsPosition);
            info.AddValue("_yAxisLabelsFontBrush", _yAxisLabelsFontBrush);
            info.AddValue("_xAxisLabelsFormat", _xAxisLabelsFormat);
            info.AddValue("_yAxisLabelsFormat", _yAxisLabelsFormat);
            info.AddValue("_labelsFont", _labelsFont);

            info.AddValue("_labelsFontBrush", _labelsFontBrush);
            info.AddValue("_labelsFill", _labelsFill);
            info.AddValue("_labelsTopMargin", _labelsTopMargin);
            info.AddValue("_labelsMargin", _labelsMargin);
            info.AddValue("_showSeriesLabels", _showSeriesLabels);
            info.AddValue("_showClippingRectangle", _showClippingRectangle);
            info.AddValue("_unitUnificationOptimizationEnabled", _unitUnificationOptimizationEnabled);
            info.AddValue("_smoothingMode", _smoothingMode);
            info.AddValue("_defaultAbsoluteSelectionMargin", _defaultAbsoluteSelectionMargin);
            info.AddValue("_scrollMode", _scrollMode);
            info.AddValue("_rightMouseButtonSelectionMode", _rightMouseButtonSelectionMode);
            info.AddValue("_selectionPen", _selectionPen);
            info.AddValue("_selectionFill", _selectionFill);
            info.AddValue("_additionalDrawingSpaceAreaMarginLeft", _additionalDrawingSpaceAreaMarginLeft);
            info.AddValue("_additionalDrawingSpaceAreaMarginRight", _additionalDrawingSpaceAreaMarginRight);
            info.AddValue("_actualDrawingSpaceAreaMarginLeft", _actualDrawingSpaceAreaMarginLeft);
            info.AddValue("_actualDrawingSpaceAreaMarginTop", _actualDrawingSpaceAreaMarginTop);
            info.AddValue("_actualDrawingSpaceAreaMarginRight", _actualDrawingSpaceAreaMarginRight);
            info.AddValue("_actualDrawingSpaceAreaMarginBottom", _actualDrawingSpaceAreaMarginBottom);
            info.AddValue("_actualDrawingSpaceAreaBorderPen", _actualDrawingSpaceAreaBorderPen);
            info.AddValue("_actualDrawingSpaceAreaFill", _actualDrawingSpaceAreaFill);
            info.AddValue("_limitedView", _limitedView);
            info.AddValue("_seriesItemWidth", _seriesItemWidth);
            info.AddValue("_seriesItemMargin", _seriesItemMargin);

            info.AddValue("customObjectsSaved", saveCustomObjects);
            if (saveCustomObjects)
            {
                _customObjectsManager.SaveState(info);
            }

            info.AddValue("_actualSpaceGrid", _actualSpaceGrid);
            info.AddValue("_drawingSpaceGrid", _drawingSpaceGrid);

            info.AddValue("_chartName", _chartName);
            info.AddValue("_appearanceScheme", _appearanceScheme);
            info.AddValue("_autoScrollToEnd", _autoScrollToEnd);

            if (_maximumXZoom.HasValue)
            {
                info.AddValue("_maximumXZoom", _maximumXZoom.Value);
            }

            info.AddValue("_xAxisLabelSpacing", _xAxisLabelSpacing);
        }
 /// <summary>
 /// Deserialization constructor.
 /// </summary>
 /// <param name="orderInfo"></param>
 /// <param name="context"></param>
 public PlatformComponent(SerializationInfo info, StreamingContext context)
     : base(info, context)
 {
     _uiSerializationInfo = (SerializationInfoEx)info.GetValue("uiSerializationData", typeof(SerializationInfoEx));
 }
        /// <summary>
        /// Save state.
        /// </summary>
        /// <param name="state"></param>
        public void SaveState(SerializationInfoEx state)
        {
            if (_stringFilter != null)
            {
                state.AddValue("_stringFilter", _stringFilter);
            }

            if (_stringInputFilter != null)
            {
                state.AddValue("_stringInputFilter", _stringInputFilter);
            }
        }
        public void RestoreState(SerializationInfoEx info, bool restoreCustomObjects)
        {
            this.Name = info.GetString("Name");
            this.Height = info.GetInt32("Height");

            _stateId = info.GetValue<Guid>("_stateId");
            _titleFont = info.GetValue<Font>("_titleFont");

            _titleFontBrush = info.GetValue<Brush>("_titleFontBrush");
            _fill = info.GetValue<Brush>("_fill");
            _axisLabelsFont = info.GetValue<Font>("_axisLabelsFont");
            _xAxisLabelsFontBrush = info.GetValue<Brush>("_xAxisLabelsFontBrush");
            _yAxisLabelsPosition = info.GetValue<YAxisLabelPosition>("_yAxisLabelsPosition");
            _yAxisLabelsFontBrush = info.GetValue<Brush>("_yAxisLabelsFontBrush");

            _xAxisLabelsFormat = info.GetString("_xAxisLabelsFormat");
            _yAxisLabelsFormat = info.GetString("_yAxisLabelsFormat");

            _labelsFont = info.GetValue<Font>("_labelsFont");
            _labelsFontBrush = info.GetValue<Brush>("_labelsFontBrush");
            _labelsFill = info.GetValue<Brush>("_labelsFill");

            _labelsTopMargin = info.GetSingle("_labelsTopMargin");
            _labelsMargin = info.GetSingle("_labelsMargin");

            _showSeriesLabels = info.GetBoolean("_showSeriesLabels");
            _showClippingRectangle = info.GetBoolean("_showClippingRectangle");
            _unitUnificationOptimizationEnabled = info.GetBoolean("_unitUnificationOptimizationEnabled");

            _smoothingMode = info.GetValue<SmoothingMode>("_smoothingMode");
            _defaultAbsoluteSelectionMargin = info.GetSingle("_defaultAbsoluteSelectionMargin");

            _scrollMode = info.GetValue<ScrollModeEnum>("_scrollMode");
            _rightMouseButtonSelectionMode = info.GetValue<SelectionModeEnum>("_rightMouseButtonSelectionMode");

            _selectionPen = info.GetValue<Pen>("_selectionPen");
            _selectionFill = info.GetValue<Brush>("_selectionFill");

            _additionalDrawingSpaceAreaMarginLeft = info.GetInt32("_additionalDrawingSpaceAreaMarginLeft");
            _additionalDrawingSpaceAreaMarginRight = info.GetInt32("_additionalDrawingSpaceAreaMarginRight");

            _actualDrawingSpaceAreaMarginLeft = info.GetInt32("_actualDrawingSpaceAreaMarginLeft");
            _actualDrawingSpaceAreaMarginTop = info.GetInt32("_actualDrawingSpaceAreaMarginTop");
            _actualDrawingSpaceAreaMarginRight = info.GetInt32("_actualDrawingSpaceAreaMarginRight");
            _actualDrawingSpaceAreaMarginBottom = info.GetInt32("_actualDrawingSpaceAreaMarginBottom");

            _actualDrawingSpaceAreaBorderPen = info.GetValue<Pen>("_actualDrawingSpaceAreaBorderPen");
            _actualDrawingSpaceAreaFill = info.GetValue<Brush>("_actualDrawingSpaceAreaFill");
            _limitedView = info.GetBoolean("_limitedView");

            _seriesItemWidth = info.GetSingle("_seriesItemWidth");
            _seriesItemMargin = info.GetSingle("_seriesItemMargin");

            if (restoreCustomObjects && info.GetBoolean("customObjectsSaved"))
            {// Restore custom objects.
                _customObjectsManager.RestoreState(info);
            }
            else
            {// New clear custom objects.
                _customObjectsManager.Clear();
            }

            _actualSpaceGrid = info.GetValue<ChartGrid>("_actualSpaceGrid");
            _drawingSpaceGrid = info.GetValue<ChartGrid>("_drawingSpaceGrid");
            _chartName = info.GetString("_chartName");
            _appearanceScheme = info.GetValue<AppearanceSchemeEnum>("_appearanceScheme");

            _autoScrollToEnd = info.GetBoolean("_autoScrollToEnd");

            if (info.ContainsValue("_maximumXZoom"))
            {
                _maximumXZoom = info.GetSingle("_maximumXZoom");
            }
            else
            {
                _maximumXZoom = null;
            }

            _xAxisLabelSpacing = info.GetSingle("_xAxisLabelSpacing");

            if (AppearanceSchemeChangedEvent != null)
            {
                AppearanceSchemeChangedEvent(this, _appearanceScheme);
            }
        }
        /// <summary>
        /// Save state of controls (size, location, dock etc.)
        /// </summary>
        /// <param name="info"></param>
        public void SaveState(SerializationInfoEx info)
        {
            // Save panels.
            SavePanelState(info, panelTop);
            SavePanelState(info, panelBottom);
            SavePanelState(info, panelLeft);
            SavePanelState(info, panelRight);
            SavePanelState(info, panelCenter);

            // Save draggable controls.
            DockStyle[] values = (DockStyle[])Enum.GetValues(typeof(DockStyle));
            foreach (DockStyle style in values)
            {
                List<SerializationInfoEx> infos = new List<SerializationInfoEx>();
                foreach (DragControl control in GetAreaControls(style))
                {
                    //if (control.ControlContained == null)
                    //{// Skip empty drag controls.
                    //    continue;
                    //}

                    SerializationInfoEx controlInfo = new SerializationInfoEx();
                    control.SaveState(controlInfo);
                    infos.Add(controlInfo);
                }
                info.AddValue(style.ToString() + "Infos", infos);
            }
        }
        /// <summary>
        /// Load state.
        /// </summary>
        /// <param name="state"></param>
        public void LoadState(SerializationInfoEx state)
        {
            if (state.ContainsValue("_stringFilter"))
            {
                StringTracerFilter stringFilter = state.GetValue<StringTracerFilter>("_stringFilter");
                if (_stringFilter != null)
                {
                    _stringFilter.CopyDataFrom(stringFilter);
                }
            }

            if (state.ContainsValue("_stringInputFilter"))
            {
                StringTracerFilter stringInputFilter = state.GetValue<StringTracerFilter>("_stringInputFilter");
                if (_stringInputFilter != null)
                {
                    _stringInputFilter.CopyDataFrom(stringInputFilter);
                }
            }

            WinFormsHelper.DirectOrManagedInvoke(this, UpdateFiltersUI);
        }
 /// <summary>
 /// 
 /// </summary>
 /// <param name="info"></param>
 public void SaveState(SerializationInfoEx info)
 {
     info.AddValue("guid", _guid);
     info.AddValue("location", Location);
     info.AddValue("text", Text);
     info.AddValue("name", Name);
     info.AddValue("dock", Dock);
     info.AddValue("size", Size);
     info.AddValue("showDragStrip", _showDragStrip);
 }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="orderInfo"></param>
        public void RestoreState(SerializationInfoEx info)
        {
            ListEx<DynamicCustomObject> dynamicObjects = info.GetValue<ListEx<DynamicCustomObject>>("CustomObjectManager::_dynamicCustomObjects");
            ListEx<CustomObject> staticObjects = info.GetValue<ListEx<CustomObject>>("CustomObjectManager::_staticCustomObjects");

            foreach (DynamicCustomObject dynamicObject in dynamicObjects)
            {
                this.Add(dynamicObject);
            }

            foreach (CustomObject customObject in staticObjects)
            {
                this.Add(customObject);
            }
        }
        /// <summary>
        /// Call this to persist state data to controls. Controls contain their own settings (partial).
        /// </summary>
        public void SaveState(SerializationInfoEx info)
        {
            // Remove the checked control from drag controls, since it does not need to be stored.
            ChangeCheckedControl(null);

            // Clean of any leftover controls before persisting.
            foreach (DragControl control in GeneralHelper.EnumerableToArray<DragControl>(dragContainerControl.DragControls))
            {
                if (control.ControlContained == null && control != _checkedControlContainer)
                {
                    dragContainerControl.RemoveDragControl(control);
                }
            }

            // Also lets store the _checkedControlContainer
            if (_checkedControlContainer != null)
            {
                info.AddValue("combinedContainerControl.CheckedControlContainerGuid", _checkedControlContainer.Guid);
            }

            info.AddValue("combinedContainerControl.ShowComponentsTitles", toolStripButtonTitlesText.Checked);

            dragContainerControl.SaveState(info);

            foreach (ToolStripButton component in _tabbedButtonsControls.Keys)
            {
                PersistControlData(_tabbedButtonsControls[component], component);
            }

            foreach (CombinedHostingForm form in _floatingFormsControls.Keys)
            {
                PersistControlData(_floatingFormsControls[form], form);
            }

            foreach (DragControl dragControl in dragContainerControl.DragControls)
            {
                if (dragControl.ControlContained != null)
                {
                    PersistControlData((CommonBaseControl)dragControl.ControlContained, dragControl);
                }
            }
        }
 /// <summary>
 /// 
 /// </summary>
 /// <param name="info"></param>
 public void RestoreState(SerializationInfoEx info)
 {
     Guid = info.GetValue<Guid>("guid");
     Location = info.GetValue<Point>("location");
     Text = info.GetString("text");
     Name = info.GetString("name");
     Dock = info.GetValue<DockStyle>("dock");
     Size = info.GetValue<Size>("size");
     if (info.ContainsValue("showDragStrip"))
     {
         ShowDragStrip = info.GetBoolean("showDragStrip");
     }
 }
        /// <summary>
        /// 
        /// </summary>
        public void RestoreState(SerializationInfoEx info)
        {
            dragContainerControl.ClearControls();

            dragContainerControl.RestoreState(info);

            //dragContainerControl.ClearControls();

            if (info.ContainsValue("combinedContainerControl.ShowComponentsTitles"))
            {
                toolStripButtonTitlesText.Checked = info.GetBoolean("combinedContainerControl.ShowComponentsTitles");
            }

            // Establish the CheckedControlContainer
            if (info.ContainsValue("combinedContainerControl.CheckedControlContainerGuid"))
            {
                Guid guid = info.GetValue<Guid>("combinedContainerControl.CheckedControlContainerGuid");
                _checkedControlContainer = dragContainerControl.GetDragControlByGuid(guid);
                if (_checkedControlContainer != null)
                {
                    _checkedControlContainer.ShowDragStrip = false;
                }
            }
        }
 /// <summary>
 /// Save the state of the controls - placement, size etc.
 /// </summary>
 /// <param name="info"></param>
 /// <param name="panel"></param>
 void SavePanelState(SerializationInfoEx info, Panel panel)
 {
     info.AddValue(panel.Name + ".Width", panel.Width);
     info.AddValue(panel.Name + ".Height", panel.Height);
     info.AddValue(panel.Name + ".Visible", panel.Visible);
 }
        /// <summary>
        /// Restore state of the controls.
        /// </summary>
        /// <param name="info"></param>
        /// <param name="panel"></param>
        void RestorePanelState(SerializationInfoEx info, Panel panel)
        {
            if (info.ContainsValue(panel.Name + ".Width"))
            {
                panel.Width = info.GetInt32(panel.Name + ".Width");
            }

            if (info.ContainsValue(panel.Name + ".Height"))
            {
                panel.Height = info.GetInt32(panel.Name + ".Height");
            }

            if (info.ContainsValue(panel.Name + ".Visible"))
            {
                panel.Visible = info.GetBoolean(panel.Name + ".Visible");
            }
        }
        /// <summary>
        /// 
        /// </summary>
        public void RestoreState(SerializationInfoEx info)
        {
            Clear();

            lock (this)
            {
                foreach (ChartPane pane in GeneralHelper.EnumerableToList<ChartPane>(_panes))
                {
                    if (pane is SlaveChartPane)
                    {
                        this.RemoveSlavePane((SlaveChartPane)pane);
                    }
                }

                this.Name = info.GetString("Name");

                this.hScrollBar.Visible = info.GetBoolean("hScrollBar.Visible");
                this.vScrollBar.Visible = info.GetBoolean("vScrollBar.Visible");
                toolStripButtonShowScrollbars.Checked = this.hScrollBar.Visible;

                List<SerializationInfoEx> infos = info.GetValue<List<SerializationInfoEx>>("panesStates");
                for (int i = 0; i < infos.Count; i++)
                {
                    if (i == 0)
                    {
                        MasterPane.RestoreState(infos[0], true);
                    }
                    else
                    {
                        SlaveChartPane pane = CreateSlavePane("", SlaveChartPane.MasterPaneSynchronizationModeEnum.XAxis, 100);
                        pane.RestoreState(infos[i], true);
                    }
                }
            }
        }
 /// <summary>
 /// 
 /// </summary>
 /// <param name="orderInfo"></param>
 public void SaveState(SerializationInfoEx info)
 {
     info.AddValue("CustomObjectManager::_dynamicCustomObjects", _dynamicCustomObjects);
     info.AddValue("CustomObjectManager::_staticCustomObjects", _staticCustomObjects);
 }
        /// <summary>
        /// 
        /// </summary>
        public void SaveState(SerializationInfoEx info)
        {
            lock (this)
            {
                info.AddValue("Name", this.Name);
                info.AddValue("hScrollBar.Visible", this.hScrollBar.Visible);
                info.AddValue("vScrollBar.Visible", this.vScrollBar.Visible);

                List<SerializationInfoEx> infos = new List<SerializationInfoEx>();
                foreach (ChartPane pane in _panes)
                {
                    SerializationInfoEx paneInfo = new SerializationInfoEx();
                    pane.SaveState(paneInfo, true);
                    infos.Add(paneInfo);
                }

                info.AddValue("panesStates", infos);
            }
        }
        /// <summary>
        /// Restore state of controls. Controls are created and left invisible untill assigned.
        /// </summary>
        /// <param name="info"></param>
        public void RestoreState(SerializationInfoEx info)
        {
            // Clear off any existing drag control from any area.
            DockStyle[] values = (DockStyle[])Enum.GetValues(typeof(DockStyle));
            foreach (DockStyle style in values)
            {
                foreach (DragControl control in GetAreaControls(style))
                {
                    RemoveDragControl(control);
                }
            }

            // Restore draggable controls.
            foreach (DockStyle style in values)
            {
                string fieldName = style.ToString() + "Infos";
                if (info.ContainsValue(fieldName) == false)
                {
                    continue;
                }
                List<SerializationInfoEx> infos = info.GetValue<List<SerializationInfoEx>>(fieldName);
                foreach (SerializationInfoEx controlInfo in infos)
                {
                    DragControl control = new DragControl();
                    control.RestoreState(controlInfo);
                    control.Visible = false;
                    AddDragControl(control);

                    if (style != DockStyle.None)
                    {
                        DockControl(control, style);
                    }
                }
            }

            // Restore panes.
            RestorePanelState(info, panelTop);
            RestorePanelState(info, panelBottom);
            RestorePanelState(info, panelLeft);
            RestorePanelState(info, panelRight);
            RestorePanelState(info, panelCenter);
        }