#region License Information /* HeuristicLab * Copyright (C) 2002-2014 Heuristic and Evolutionary Algorithms Laboratory (HEAL) * * This file is part of HeuristicLab. * * HeuristicLab is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * HeuristicLab is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with HeuristicLab. If not, see . */ #endregion using HeuristicLab.MainForm; using HeuristicLab.PluginInfrastructure; using System; using System.Windows.Forms; namespace HeuristicLab.Optimization.Networks.Views { [View("GenericPort View")] [Content(typeof(GenericPort), true)] [Content(typeof(IGenericPort), false)] public partial class GenericPortView : PortView { protected EntitySelectorDialog entitySelectorDialog; public new IGenericPort Content { get { return (IGenericPort)base.Content; } set { base.Content = value; } } public GenericPortView() { InitializeComponent(); errorProvider.SetIconAlignment(connectedPortView, ErrorIconAlignment.MiddleRight); errorProvider.SetIconPadding(connectedPortView, 2); } protected override void Dispose(bool disposing) { if (disposing) { if (entitySelectorDialog != null) entitySelectorDialog.Dispose(); if (components != null) components.Dispose(); } base.Dispose(disposing); } protected override void DeregisterContentEvents() { Content.InterfaceChanged -= Content_InterfaceChanged; Content.ConnectedPortChanged -= Content_ConnectedPortChanged; base.DeregisterContentEvents(); } protected override void RegisterContentEvents() { base.RegisterContentEvents(); Content.InterfaceChanged += Content_InterfaceChanged; Content.ConnectedPortChanged += Content_ConnectedPortChanged; } protected override void OnContentChanged() { base.OnContentChanged(); connectedPortView.Content = Content == null ? null : Content.ConnectedPort; portParameterCollectionView.Content = Content == null ? null : Content.Parameters; messageCollectionView.Content = Content == null ? null : Content.Messages; errorProvider.SetError(connectedPortView, ((Content == null) || Content.PortConnectionValid) ? string.Empty : "Port connection is not valid"); } protected override void SetEnabledStateOfControls() { base.SetEnabledStateOfControls(); sendMessageButton.Enabled = Content != null; connectedPortGroupBox.Enabled = Content != null && !ReadOnly; setConnectedPortButton.Enabled = Content != null && !ReadOnly; clearConnectedPortButton.Enabled = Content != null && Content.ConnectedPort != null && !ReadOnly; portParameterCollectionView.Enabled = Content != null && !ReadOnly; cloneConnectedPortParametersButton.Enabled = Content != null && Content.ConnectedPort != null && !ReadOnly; messageCollectionView.Enabled = Content != null && !ReadOnly; } protected virtual void Content_ConnectedPortChanged(object sender, EventArgs e) { if (InvokeRequired) Invoke(new EventHandler(Content_ConnectedPortChanged), sender, e); else { clearConnectedPortButton.Enabled = Content.ConnectedPort != null && !ReadOnly; cloneConnectedPortParametersButton.Enabled = Content.ConnectedPort != null && !ReadOnly; connectedPortView.Content = Content.ConnectedPort; } } protected virtual void Content_InterfaceChanged(object sender, EventArgs e) { if (InvokeRequired) Invoke(new EventHandler(Content_InterfaceChanged), sender, e); else { errorProvider.SetError(connectedPortView, Content.PortConnectionValid ? string.Empty : "Port connection is not valid"); } } protected virtual void sendMessageButton_Click(object sender, EventArgs e) { Content.SendMessageAsync(Content.PrepareMessage()); } protected virtual void setConnectedPortButton_Click(object sender, EventArgs e) { if (entitySelectorDialog == null) { entitySelectorDialog = new EntitySelectorDialog(); entitySelectorDialog.Caption = "Select Connected Port"; entitySelectorDialog.EntitySelector.Caption = "Available Ports"; } try { IEntity root = Content; while (root.Parent != null) root = root.Parent; entitySelectorDialog.EntitySelector.Configure( root, Content.ConnectedPort, typeof(IConnectedPort) ); if (entitySelectorDialog.ShowDialog(this) == DialogResult.OK) { Content.ConnectedPort = entitySelectorDialog.EntitySelector.SelectedEntity as IConnectedPort; } } catch (Exception ex) { ErrorHandling.ShowErrorDialog(this, ex); } } protected virtual void clearConnectedPortButton_Click(object sender, EventArgs e) { Content.ConnectedPort = null; } protected virtual void connectedPortView_DragEnterOver(object sender, DragEventArgs e) { e.Effect = DragDropEffects.None; var data = e.Data.GetData(HeuristicLab.Common.Constants.DragDropDataFormat) as IConnectedPort; if (!ReadOnly && (data != null) && (Content.CanConnectToPort(data))) { e.Effect = DragDropEffects.Link; } } protected virtual void connectedPortView_DragDrop(object sender, DragEventArgs e) { if (e.Effect != DragDropEffects.None) { var port = e.Data.GetData(HeuristicLab.Common.Constants.DragDropDataFormat) as IConnectedPort; Content.ConnectedPort = port; } } protected virtual void cloneConnectedPortParametersButton_Click(object sender, EventArgs e) { Content.CloneConnectedPortParameters(); } } }