Fixed design-time displaying of the language mappings

git-svn-id: http://svn.code.sf.net/p/greenshot/code/trunk@1777 7dccd23d-a4a3-4e1f-8c07-b4c1b4018ab4
This commit is contained in:
RKrom 2012-04-17 12:52:46 +00:00
commit 27910e584a
10 changed files with 656 additions and 750 deletions

View file

@ -25,6 +25,8 @@ using GreenshotPlugin.Core;
namespace GreenshotPlugin.Controls {
public class GreenshotComboBox : ComboBox, IGreenshotConfigBindable {
private Type enumType = null;
private Enum selectedEnum = null;
private string sectionName = "Core";
[Category("Greenshot"), DefaultValue("Core"), Description("Specifies the Ini-Section to map this control with.")]
public string SectionName {
@ -42,41 +44,51 @@ namespace GreenshotPlugin.Controls {
set;
}
/// <summary>
/// This is a method to popululate the ComboBox
/// with the items from the enumeration
/// </summary>
/// <param name="enumType">TEnum to populate with</param>
public void Populate(ILanguage language, Type enumType, object currentValue) {
var availableValues = Enum.GetValues(enumType);
this.Items.Clear();
string enumTypeName = enumType.Name;
foreach (var enumValue in availableValues) {
string enumKey = enumTypeName + "." + enumValue.ToString();
if (language.hasKey(enumKey)) {
string translation = language.GetString(enumKey);
this.Items.Add(translation);
} else {
this.Items.Add(enumValue.ToString());
}
}
public GreenshotComboBox() {
this.SelectedIndexChanged += delegate {
StoreSelectedEnum();
};
}
public void SetValue(Enum currentValue) {
if (currentValue != null) {
string selectedEnumKey = enumTypeName + "." + currentValue.ToString();
if (language.hasKey(selectedEnumKey)) {
this.SelectedItem = language.GetString(selectedEnumKey);
selectedEnum = currentValue;
string selectedEnumKey = enumType.Name + "." + currentValue.ToString();
if (Language.hasKey(selectedEnumKey)) {
this.SelectedItem = Language.GetString(selectedEnumKey);
} else {
this.SelectedItem = currentValue.ToString();
}
}
}
/// <summary>
/// This is a method to popululate the ComboBox
/// with the items from the enumeration
/// </summary>
/// <param name="enumType">TEnum to populate with</param>
public void Populate(Type enumType) {
// Store the enum-type, so we can work with it
this.enumType = enumType;
var availableValues = Enum.GetValues(enumType);
this.Items.Clear();
string enumTypeName = enumType.Name;
foreach (var enumValue in availableValues) {
string enumKey = enumTypeName + "." + enumValue.ToString();
if (Language.hasKey(enumKey)) {
string translation = Language.GetString(enumKey);
this.Items.Add(translation);
} else {
this.Items.Add(enumValue.ToString());
}
}
}
/// <summary>
/// Get the selected enum value from the combobox, uses generics
/// Store the selected value internally
/// </summary>
/// <param name="comboBox">Combobox to get the value from</param>
/// <returns>The generics value of the combobox</returns>
public object GetSelectedEnum(ILanguage language, Type enumType) {
private void StoreSelectedEnum() {
string enumTypeName = enumType.Name;
string selectedValue = this.SelectedItem as string;
var availableValues = Enum.GetValues(enumType);
@ -84,20 +96,27 @@ namespace GreenshotPlugin.Controls {
try {
returnValue = Enum.Parse(enumType, selectedValue);
return returnValue;
} catch (Exception) {
}
foreach (Enum enumValue in availableValues) {
string enumKey = enumTypeName + "." + enumValue.ToString();
if (language.hasKey(enumKey)) {
string translation = language.GetString(enumTypeName + "." + enumValue.ToString());
if (Language.hasKey(enumKey)) {
string translation = Language.GetString(enumTypeName + "." + enumValue.ToString());
if (translation.Equals(selectedValue)) {
return enumValue;
returnValue = enumValue;
}
}
}
return returnValue;
selectedEnum = (Enum)returnValue;
}
/// <summary>
/// Get the selected enum value from the combobox, uses generics
/// </summary>
/// <returns>The enum value of the combobox</returns>
public Enum GetSelectedEnum() {
return selectedEnum;
}
}
}

View file

@ -12,43 +12,66 @@ using System.IO;
namespace GreenshotPlugin.Controls {
public abstract class GreenshotForm : Form, IGreenshotLanguageBindable {
private static log4net.ILog LOG = log4net.LogManager.GetLogger(typeof(GreenshotForm));
protected ILanguage language;
private IComponentChangeService m_changeService;
private bool isLanguageSet = false;
private IDictionary<string, Control> designTimeControls;
[Category("Greenshot"), DefaultValue(null), Description("Specifies key of the language file to use when displaying the text.")]
public string LanguageKey {
get;
set;
}
protected abstract string LanguagePattern { get; }
/// <summary>
/// Code to initialize the language etc during design time
/// </summary>
protected void InitializeForDesigner() {
if (language == null && this.DesignMode) {
if (this.DesignMode) {
designTimeControls = new Dictionary<string, Control>();
try {
if (!IniConfig.IsInited) {
IniConfig.Init("greenshot", "greenshot");
}
ITypeResolutionService typeResService = GetService(typeof(ITypeResolutionService)) as ITypeResolutionService;
Assembly currentAssembly = this.GetType().Assembly;
string assemblyPath = typeResService.GetPathOfAssembly(currentAssembly.GetName());
string designTimeLanguagePath = Path.Combine(Path.GetDirectoryName(assemblyPath), "../../Languages/");
language = new LanguageContainer(LanguagePattern, designTimeLanguagePath);
string designTimeLanguagePath = Path.Combine(Path.GetDirectoryName(assemblyPath), "../../../Greenshot/Languages/");
string designTimePluginLanguagePath = Path.Combine(Path.GetDirectoryName(assemblyPath), "../../Languages/");
//MessageBox.Show(designTimeLanguagePath);
Language.AddLanguageFilePath(designTimeLanguagePath);
Language.AddLanguageFilePath(designTimePluginLanguagePath);
} catch (Exception ex) {
MessageBox.Show(ex.ToString());
}
}
}
/// <summary>
/// This override is only for the design-time of the form
/// </summary>
/// <param name="e"></param>
protected override void OnPaint(PaintEventArgs e) {
if (this.DesignMode) {
LOG.InfoFormat("OnPaint called from designer. Key={0}", LanguageKey);
if (!isLanguageSet) {
isLanguageSet = true;
try {
ApplyLanguage();
} catch (Exception ex) {
MessageBox.Show(ex.ToString());
}
}
}
base.OnPaint(e);
}
protected override void OnLoad(EventArgs e) {
if (!this.DesignMode) {
ApplyLanguage();
FillFields();
base.OnLoad(e);
} else {
LOG.Info("OnLoad called from designer.");
InitializeForDesigner();
base.OnLoad(e);
ApplyLanguage();
}
base.OnLoad(e);
}
/// <summary>
@ -65,8 +88,10 @@ namespace GreenshotPlugin.Controls {
base.OnClosed(e);
}
// This override allows the control to register event handlers for IComponentChangeService events
// at the time the control is sited, which happens only in design mode.
/// <summary>
/// This override allows the control to register event handlers for IComponentChangeService events
/// at the time the control is sited, which happens only in design mode.
/// </summary>
public override ISite Site {
get {
return base.Site;
@ -93,6 +118,7 @@ namespace GreenshotPlugin.Controls {
// Clear our the component change events to prepare for re-siting.
if (m_changeService != null) {
m_changeService.ComponentChanged -= new ComponentChangedEventHandler(OnComponentChanged);
m_changeService.ComponentAdded -= new ComponentEventHandler(OnComponentAdded);
}
}
@ -100,19 +126,37 @@ namespace GreenshotPlugin.Controls {
// Register the event handlers for the IComponentChangeService events
if (m_changeService != null) {
m_changeService.ComponentChanged += new ComponentChangedEventHandler(OnComponentChanged);
m_changeService.ComponentAdded += new ComponentEventHandler(OnComponentAdded);
}
}
/* This method handles the OnComponentChanged event to display a notification. */
/// <summary>
/// This method handles the OnComponentChanged event to display a notification.
/// </summary>
/// <param name="sender"></param>
/// <param name="ce"></param>
private void OnComponentChanged(object sender, ComponentChangedEventArgs ce) {
if (ce.Component != null && ((IComponent)ce.Component).Site != null && ce.Member != null) {
//OnUserChange("The " + ce.Member.Name + " member of the " + ((IComponent)ce.Component).Site.Name + " component has been changed.");
if ("LanguageKey".Equals(ce.Member.Name)) {
ApplyLanguage(ce.Component as Control, (string)ce.NewValue);
}
}
}
private void OnComponentAdded(object sender, ComponentEventArgs ce) {
if (ce.Component != null && ((IComponent)ce.Component).Site != null) {
Control control = ce.Component as Control;
if (control != null) {
LOG.DebugFormat("Control {0} added.", control.Name);
if (!designTimeControls.ContainsKey(control.Name)) {
designTimeControls.Add(control.Name, control);
} else {
designTimeControls[control.Name] = control;
}
}
}
}
// Clean up any resources being used.
protected override void Dispose(bool disposing) {
if (disposing) {
@ -121,53 +165,67 @@ namespace GreenshotPlugin.Controls {
base.Dispose(disposing);
}
protected void ApplyLanguage(Control applyTo) {
IGreenshotLanguageBindable languageBindable = applyTo as IGreenshotLanguageBindable;
if (languageBindable == null) {
LOG.DebugFormat("Not bindable: {0}", applyTo.Name);
return;
}
string languageKey = languageBindable.LanguageKey;
LOG.DebugFormat("Found language key '{0}' configured on control '{1}'", languageKey, applyTo.Name);
// Apply language text to the control
ApplyLanguage(applyTo, languageKey);
// Repopulate the combox boxes
if (typeof(IGreenshotConfigBindable).IsAssignableFrom(applyTo.GetType())) {
if (typeof(GreenshotComboBox).IsAssignableFrom(applyTo.GetType())) {
IGreenshotConfigBindable configBindable = applyTo as IGreenshotConfigBindable;
if (!string.IsNullOrEmpty(configBindable.SectionName) && !string.IsNullOrEmpty(configBindable.PropertyName)) {
IniSection section = IniConfig.GetIniSection(configBindable.SectionName);
if (section != null) {
GreenshotComboBox comboxBox = applyTo as GreenshotComboBox;
// Only update the language, so get the actual value and than repopulate
Enum currentValue = (Enum)comboxBox.GetSelectedEnum();
comboxBox.Populate(section.Values[configBindable.PropertyName].ValueType);
comboxBox.SetValue(currentValue);
}
}
}
}
}
/// <summary>
/// Apply all the language settings to the "Greenshot" Controls on this form
/// </summary>
protected void ApplyLanguage() {
if (language == null) {
MessageBox.Show("Language not set!! Please use 'language = Language.GetInstance()' in your form constructor!");
return;
}
LOG.DebugFormat("Applying language, using key {0}", LanguageKey);
// Set title of the form
if (!string.IsNullOrEmpty(LanguageKey)) {
this.Text = language.GetString(LanguageKey);
this.Text = Language.GetString(LanguageKey);
}
// Reset the text values for all GreenshotControls
foreach (FieldInfo field in this.GetType().GetFields(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance)) {
foreach (FieldInfo field in this.GetType().GetFields(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static | BindingFlags.Instance)) {
LOG.DebugFormat("Checking field {0}", field.Name);
if (!field.FieldType.IsSubclassOf(typeof(Control))) {
LOG.DebugFormat("No control: {0}", field.Name);
continue;
}
Object controlObject = field.GetValue(this);
if (controlObject == null) {
LOG.DebugFormat("No value: {0}", field.Name);
continue;
}
Control applyTo = controlObject as Control;
if (applyTo == null) {
// not a control
LOG.DebugFormat("No control: {0}", field.Name);
continue;
}
IGreenshotLanguageBindable languageBindable = applyTo as IGreenshotLanguageBindable;
if (languageBindable == null) {
continue;
}
string languageKey = languageBindable.LanguageKey;
// Apply language text to the control
ApplyLanguage(applyTo, languageKey);
// Repopulate the combox boxes
if (typeof(IGreenshotConfigBindable).IsAssignableFrom(field.FieldType)) {
if (typeof(GreenshotComboBox).IsAssignableFrom(field.FieldType)) {
IGreenshotConfigBindable configBindable = controlObject as IGreenshotConfigBindable;
if (!string.IsNullOrEmpty(configBindable.SectionName) && !string.IsNullOrEmpty(configBindable.PropertyName)) {
IniSection section = IniConfig.GetIniSection(configBindable.SectionName);
if (section != null) {
GreenshotComboBox comboxBox = controlObject as GreenshotComboBox;
// Only update the language, so get the actual value and than repopulate
object currentValue = comboxBox.GetSelectedEnum(language, section.Values[configBindable.PropertyName].ValueType);
comboxBox.Populate(language, section.Values[configBindable.PropertyName].ValueType, currentValue);
}
}
}
ApplyLanguage(applyTo);
}
if (DesignMode) {
foreach (Control designControl in designTimeControls.Values) {
ApplyLanguage(designControl);
}
}
}
@ -177,13 +235,18 @@ namespace GreenshotPlugin.Controls {
/// </summary>
protected void ApplyLanguage(Control applyTo, string languageKey) {
if (!string.IsNullOrEmpty(languageKey)) {
if (!language.hasKey(languageKey)) {
if (!Language.hasKey(languageKey)) {
LOG.WarnFormat("Wrong language key '{0}' configured for control '{1}'", languageKey, applyTo.Name);
MessageBox.Show(string.Format("Wrong language key '{0}' configured for control '{1}'", languageKey, applyTo.Name));
return;
}
applyTo.Text = language.GetString(languageKey);
applyTo.Text = Language.GetString(languageKey);
} else {
MessageBox.Show(string.Format("Greenshot control without language key: {0}", applyTo.Name));
if (this.DesignMode) {
MessageBox.Show(string.Format("Greenshot control without language key: {0}", applyTo.Name));
} else {
LOG.DebugFormat("Greenshot control without language key: {0}", applyTo.Name);
}
}
}
@ -213,7 +276,8 @@ namespace GreenshotPlugin.Controls {
textBox.Text = (string)section.Values[configBindable.PropertyName].Value;
} else if (typeof(GreenshotComboBox).IsAssignableFrom(field.FieldType)) {
GreenshotComboBox comboxBox = controlObject as GreenshotComboBox;
comboxBox.Populate(language, section.Values[configBindable.PropertyName].ValueType, (Enum)section.Values[configBindable.PropertyName].Value);
comboxBox.Populate(section.Values[configBindable.PropertyName].ValueType);
comboxBox.SetValue((Enum)section.Values[configBindable.PropertyName].Value);
}
}
}
@ -253,7 +317,7 @@ namespace GreenshotPlugin.Controls {
iniDirty = true;
} else if (typeof(GreenshotComboBox).IsAssignableFrom(field.FieldType)) {
GreenshotComboBox comboxBox = controlObject as GreenshotComboBox;
section.Values[configBindable.PropertyName].Value = comboxBox.GetSelectedEnum(language, section.Values[configBindable.PropertyName].ValueType);
section.Values[configBindable.PropertyName].Value = comboxBox.GetSelectedEnum();
}
}
}