Implement the UI Controller Class
In this chapter you will learn how to implement a class for controlling the plug-in user interface. This class acts as a kind of intermediary between the plug-in UI and the class for setting and retrieving the settings values (see Retrieve the Settings Values).
Add the Class for Controlling the User Interface
Add new class to your project and call it e.g. IdenticalVerifierUIPage.cs
. This class contains a number of overriding methods that are required for controlling the user interface, which you created previously (see Implement the User Interface). This class needs to reference the namespace 'Sdl.Core.Settings** and Sdl.Verification.Api. Moreover, the new class needs to be derived from AbstractSettingsPage.
This class needs to be preceded by the following annotation, which makes it an extension class, which is referenced in the plug-in manifest (see also Create a New Project).
[GlobalVerifierSettingsPage(
Id = "Identical Settings Definition ID",
Name = "Context to check",
Description = "The display code of the context for which the length check should be performed.",
HelpTopic = "")]
Note that there are various strings to provide in the annotation, such as the plug-in page name and description. The plug-in page name corresponds to the link that is clicked in the user interface of Trados Studio to display the corresponding page. Then declare the following private members:
- _Control: This member is derived from the user interface class (see Implement the User Interface); We use this member to control the actual user interface (e.g. when the user clicks the buttons OK, Reset to Defaults, etc. in the UI)
- _ControlSettings: This member is derived from our
IdenticalVerifierSettings
class (see Retrieve the Settings Values); it is used to access the setting values of the verifier plug-in. The skeleton for your new class should look as shown below:
using Sdl.Core.Settings;
using Sdl.Verification.Api;
namespace Verification.Sdk.IdenticalCheck
{
[GlobalVerifierSettingsPage(
Id = "Identical Settings Definition ID",
Name = "Context to check",
Description = "The display code of the context for which the length check should be performed.",
HelpTopic = "")]
class IdenticalVerifierUIPage : AbstractSettingsPage
{
private IdenticalVerifierUI _Control;
private IdenticalVerifierSettings _ControlSettings;
}
}
Access the Verifier Settings
Start by adding the following private members, which are derived from the actual plug-in UI (see Implement the User Interface) and the verification settings class (see Retrieve the Settings Values):
private IdenticalVerifierUI _Control;
private IdenticalVerifierSettings _ControlSettings;
In the next step, add the following member, which is used to get a handle on the plug-in user interface (i.e. the control).
public override object GetControl()
{
_ControlSettings = ((ISettingsBundle)DataSource).GetSettingsGroup<IdenticalVerifierSettings>();
_ControlSettings.BeginEdit();
if (_Control == null)
{
_Control = new IdenticalVerifierUI();
}
return _Control;
}
Add the Functionality to Control the UI
Now you need to add a number of members that control the user interface. These methods are implemented to do the following:
- initialize the user interface
- load the settings that were configured in a previous session
- determine what happens when the user clicks the OK button, thereby saving the configured settings
- define what happens when the user clicks Reset to Defaults, thus restoring the settings to their default values
- moves away from the settings page, thus cancelling the settings page operation
Below you see examples of the members for controlling the user interface. The following member controls what happens when the user clicks the Reset to Defaults button, which is provided by the framework:
public override void ResetToDefaults()
{
_ControlSettings.CheckContext.Reset();
_Control.ContextToCheck = _ControlSettings.CheckContext;
}
The following member controls what happens when the user clicks the OK button, thereby committing the settings that were configured on the plug-in settings page:
public override void Save()
{
_ControlSettings.CheckContext.Value = _Control.ContextToCheck;
}
The member below is used to control what happens when the user clicks Cancel, thereby not saving the settings:
public override void Cancel()
{
_ControlSettings.CancelEdit();
}
Another required member of the abstract settings page base class is used to properly dispose of the user control:
public override void Dispose()
{
_Control.Dispose();
}
Putting it All Together
The complete class for controlling the settings page should now as shown below:
using Sdl.Core.Settings;
using Sdl.Verification.Api;
namespace Verification.Sdk.IdenticalCheck
{
/// <summary>
/// This is the extension class that displays and controls the plug-in user interface,
/// in which the verification setting(s) can be specified. This class is responsible for
/// e.g. saving the setting(s) configured in the UI, for resetting the values to their defaults,
/// and for properly disposing of the UI control.
/// </summary>
#region "Declaration"
[GlobalVerifierSettingsPage(
Id = "Identical Settings Definition ID",
Name = "Context to check",
Description = "The display code of the context for which the length check should be performed.",
HelpTopic = "")]
#endregion
class IdenticalVerifierUIPage : AbstractSettingsPage
{
#region "PrivateMembers"
private IdenticalVerifierUI _Control;
private IdenticalVerifierSettings _ControlSettings;
#endregion
#region "control"
// Return the UI control.
#region "GetControl"
public override object GetControl()
{
_ControlSettings = ((ISettingsBundle)DataSource).GetSettingsGroup<IdenticalVerifierSettings>();
_ControlSettings.BeginEdit();
if (_Control == null)
{
_Control = new IdenticalVerifierUI();
}
return _Control;
}
#endregion
// Load data from the settings into the UI control.
#region "OnActivate"
public override void OnActivate()
{
_Control.ContextToCheck = _ControlSettings.CheckContext;
}
#endregion
// Reset the values on the UI control.
#region "ResetToDefaults"
public override void ResetToDefaults()
{
_ControlSettings.CheckContext.Reset();
_Control.ContextToCheck = _ControlSettings.CheckContext;
}
#endregion
public override bool ValidateInput()
{
return _Control.ValidateChildren();
}
// Save the values from the UI into settings class.
#region "Save"
public override void Save()
{
_ControlSettings.CheckContext.Value = _Control.ContextToCheck;
}
#endregion
// Call EndEdit after all changes have been saved in the Save() call.
#region "AfterSave"
public override void AfterSave()
{
_ControlSettings.EndEdit();
}
#endregion
// Cancel any pending changes.
#region "Cancel"
public override void Cancel()
{
_ControlSettings.CancelEdit();
}
#endregion
// Properly dispose of the control.
#region "Dispose"
public override void Dispose()
{
_Control.Dispose();
}
#endregion
#endregion
}
}