//Copyright 2012-2019 Keysight Technologies
//
//Licensed under the Apache License, Version 2.0 (the "License");
//you may not use this file except in compliance with the License.
//You may obtain a copy of the License at
//
//http://www.apache.org/licenses/LICENSE-2.0
//
//Unless required by applicable law or agreed to in writing, software
//distributed under the License is distributed on an "AS IS" BASIS,
//WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//See the License for the specific language governing permissions and
//limitations under the License.
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Net;
using System.Net.NetworkInformation;
using System.Xml.Linq;
namespace OpenTap.Plugins.PluginDevelopment
{
//
// This example shows how to create a custom annotation for an IPAddress type
// It will provide a way to convert from/to IPAddress/string, a way of showing parsing errors,
// and a way of showing suggested values to the user.
//
// To create an annotation, two things are needed. The annotation itself and an IAnnotator implementation.
// The IAnnotator is used to annotate data with the created annotations.
//
// Annotation types used here:
// IStringValueAnnotation - Provides a way to convert from value to a string. This is used by the GUI to create a textbox.
// IErrorAnnotation - Provides a way to display data parsing errors to the user.
// ISuggestedValuesAnnotation - Provides a way to suggest values to the user.
// Other Annotation types available:
// IAvailableValuesAnnotation, IMultiSelect - These are for dropdowns.
// IAccessAnnotation - Read-Only and hide functionality.
// ICollectionAnnotation - collections of things. (advanced)
// IMembersAnnotation - members of objects. (advanced)
// IOwnedAnnotation - annotation that has Read/Write functionality. (advanced)
// IMethodAnnotation - Functionality that can be invoked e.g using a button.
// IValueDescriptionAnnotation, IStringExampleAnnotation - Used for improved tooltips.
//
/// Annotate an IPAddress
public class IPAnnotation : IStringValueAnnotation, IErrorAnnotation, ISuggestedValuesAnnotation
{
private AnnotationCollection annotations;
public IPAnnotation(AnnotationCollection annotations)
{
this.annotations = annotations;
}
/// Implementing IStringValueAnnotation.Value.
public string Value
{
get
{
// convert from the value to a string.
var ip = (IPAddress) annotations.Get().Value;
if (ip == null) return "";
return ip.ToString();
}
set
{
try
{
IPAddress ip = IPAddress.Parse(value);
annotations.Get().Value = ip;
error = null;
}
catch(Exception e)
{
error = e.Message;
}
}
}
public string error;
// Implementing IErrorAnnotation using this property.
public IEnumerable Errors
{
get { return error == null ? Array.Empty() : new string[] { error }; }
}
/// Implementing ISuggestedValuesAnnotation.
public IEnumerable SuggestedValues
{
get
{
yield return IPAddress.IPv6Loopback.ToString();
yield return IPAddress.IPv6Any.ToString();
yield return IPAddress.IPv6None.ToString();
yield return IPAddress.Parse("127.0.0.1").ToString();
}
}
}
// To fully support IPaddresses, we also need to be able to serialize/deserialize them.
// This plugin class takes care of that.
public class IpAdressSerializer : ITapSerializerPlugin
{
public double Order => 5;
public bool Deserialize(XElement node, ITypeData t, Action