From d1b5232898a822387d18c8846ef46de90b868b5f Mon Sep 17 00:00:00 2001 From: RKrom Date: Thu, 12 Apr 2012 14:44:21 +0000 Subject: [PATCH] Test with the ImgurPlugin and designer language support, the form needs to extend a simple class which extends GreenshotForm and implements the language pattern getter. Some day we can change this to be simpler when there is only one global Language object... git-svn-id: http://svn.code.sf.net/p/greenshot/code/trunk@1774 7dccd23d-a4a3-4e1f-8c07-b4c1b4018ab4 --- GreenshotImgurPlugin/Forms/ImgurForm.cs | 20 +++ .../Forms/SettingsForm.Designer.cs | 52 ++++--- GreenshotImgurPlugin/Forms/SettingsForm.cs | 36 +---- .../GreenshotImgurPlugin.csproj | 2 +- GreenshotImgurPlugin/ImgurConfiguration.cs | 10 -- GreenshotImgurPlugin/Language.cs | 4 +- GreenshotPlugin/Controls/GreenshotForm.cs | 133 +++++++++++++++--- GreenshotPlugin/Core/LanguageHelper.cs | 14 +- GreenshotPlugin/IniFile/IniConfig.cs | 6 + 9 files changed, 190 insertions(+), 87 deletions(-) create mode 100644 GreenshotImgurPlugin/Forms/ImgurForm.cs diff --git a/GreenshotImgurPlugin/Forms/ImgurForm.cs b/GreenshotImgurPlugin/Forms/ImgurForm.cs new file mode 100644 index 000000000..b630af7bd --- /dev/null +++ b/GreenshotImgurPlugin/Forms/ImgurForm.cs @@ -0,0 +1,20 @@ +using System; +using System.Collections.Generic; +using System.Text; +using GreenshotPlugin.Controls; +using GreenshotPlugin.Core; +using Greenshot.IniFile; +using System.ComponentModel.Design; +using System.Reflection; +using System.IO; +using System.Windows.Forms; + +namespace GreenshotImgurPlugin.Forms { + public class ImgurForm : GreenshotForm { + protected override string LanguagePattern { + get { + return Language.LANGUAGE_FILENAME_PATTERN; + } + } + } +} diff --git a/GreenshotImgurPlugin/Forms/SettingsForm.Designer.cs b/GreenshotImgurPlugin/Forms/SettingsForm.Designer.cs index e2e417329..10375ae12 100644 --- a/GreenshotImgurPlugin/Forms/SettingsForm.Designer.cs +++ b/GreenshotImgurPlugin/Forms/SettingsForm.Designer.cs @@ -46,19 +46,20 @@ namespace GreenshotImgurPlugin { /// private void InitializeComponent() { - this.buttonOK = new System.Windows.Forms.Button(); - this.buttonCancel = new System.Windows.Forms.Button(); - this.label_url = new System.Windows.Forms.Label(); - this.textBoxUrl = new System.Windows.Forms.TextBox(); - this.combobox_uploadimageformat = new System.Windows.Forms.ComboBox(); - this.label_upload_format = new System.Windows.Forms.Label(); - this.historyButton = new System.Windows.Forms.Button(); - this.checkbox_usepagelink = new System.Windows.Forms.CheckBox(); + this.buttonOK = new GreenshotPlugin.Controls.GreenshotButton(); + this.buttonCancel = new GreenshotPlugin.Controls.GreenshotButton(); + this.label_url = new GreenshotPlugin.Controls.GreenshotLabel(); + this.textBoxUrl = new GreenshotPlugin.Controls.GreenshotTextBox(); + this.combobox_uploadimageformat = new GreenshotPlugin.Controls.GreenshotComboBox(); + this.label_upload_format = new GreenshotPlugin.Controls.GreenshotLabel(); + this.historyButton = new GreenshotPlugin.Controls.GreenshotButton(); + this.checkbox_usepagelink = new GreenshotPlugin.Controls.GreenshotCheckBox(); this.SuspendLayout(); // // buttonOK // this.buttonOK.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); + this.buttonOK.LanguageKey = "OK"; this.buttonOK.Location = new System.Drawing.Point(222, 129); this.buttonOK.Name = "buttonOK"; this.buttonOK.Size = new System.Drawing.Size(75, 23); @@ -69,6 +70,7 @@ namespace GreenshotImgurPlugin { // buttonCancel // this.buttonCancel.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); + this.buttonCancel.LanguageKey = "CANCEL"; this.buttonCancel.Location = new System.Drawing.Point(303, 129); this.buttonCancel.Name = "buttonCancel"; this.buttonCancel.Size = new System.Drawing.Size(75, 23); @@ -78,6 +80,7 @@ namespace GreenshotImgurPlugin { // // label_url // + this.label_url.LanguageKey = "label_url"; this.label_url.Location = new System.Drawing.Point(12, 21); this.label_url.Name = "label_url"; this.label_url.Size = new System.Drawing.Size(84, 20); @@ -88,6 +91,8 @@ namespace GreenshotImgurPlugin { this.textBoxUrl.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right))); this.textBoxUrl.Location = new System.Drawing.Point(102, 21); this.textBoxUrl.Name = "textBoxUrl"; + this.textBoxUrl.PropertyName = "ImgurApiUrl"; + this.textBoxUrl.SectionName = "Imgur"; this.textBoxUrl.Size = new System.Drawing.Size(276, 20); this.textBoxUrl.TabIndex = 6; // @@ -97,20 +102,24 @@ namespace GreenshotImgurPlugin { this.combobox_uploadimageformat.FormattingEnabled = true; this.combobox_uploadimageformat.Location = new System.Drawing.Point(102, 47); this.combobox_uploadimageformat.Name = "combobox_uploadimageformat"; + this.combobox_uploadimageformat.PropertyName = "UploadFormat"; + this.combobox_uploadimageformat.SectionName = "Imgur"; this.combobox_uploadimageformat.Size = new System.Drawing.Size(276, 21); this.combobox_uploadimageformat.TabIndex = 8; // // label_upload_format // + this.label_upload_format.LanguageKey = "label_upload_format"; this.label_upload_format.Location = new System.Drawing.Point(12, 50); this.label_upload_format.Name = "label_upload_format"; this.label_upload_format.Size = new System.Drawing.Size(84, 20); this.label_upload_format.TabIndex = 9; - this.label_upload_format.Text = "Upload format"; + this.label_upload_format.Text = "Image format"; // // historyButton // this.historyButton.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left))); + this.historyButton.LanguageKey = "imgur_history"; this.historyButton.Location = new System.Drawing.Point(13, 129); this.historyButton.Name = "historyButton"; this.historyButton.Size = new System.Drawing.Size(75, 23); @@ -122,9 +131,12 @@ namespace GreenshotImgurPlugin { // checkbox_usepagelink // this.checkbox_usepagelink.AutoSize = true; - this.checkbox_usepagelink.Location = new System.Drawing.Point(102, 97); - this.checkbox_usepagelink.Name = "copyOriginalLinkCheck"; - this.checkbox_usepagelink.Size = new System.Drawing.Size(188, 17); + this.checkbox_usepagelink.LanguageKey = "use_page_link"; + this.checkbox_usepagelink.Location = new System.Drawing.Point(15, 97); + this.checkbox_usepagelink.Name = "checkbox_usepagelink"; + this.checkbox_usepagelink.PropertyName = "UsePageLink"; + this.checkbox_usepagelink.SectionName = "Imgur"; + this.checkbox_usepagelink.Size = new System.Drawing.Size(251, 17); this.checkbox_usepagelink.TabIndex = 13; this.checkbox_usepagelink.UseVisualStyleBackColor = true; // @@ -150,13 +162,13 @@ namespace GreenshotImgurPlugin { this.PerformLayout(); } - private System.Windows.Forms.Button historyButton; - private System.Windows.Forms.ComboBox combobox_uploadimageformat; - private System.Windows.Forms.Label label_upload_format; - private System.Windows.Forms.TextBox textBoxUrl; - private System.Windows.Forms.Label label_url; - private System.Windows.Forms.Button buttonCancel; - private System.Windows.Forms.Button buttonOK; - private System.Windows.Forms.CheckBox checkbox_usepagelink; + private GreenshotPlugin.Controls.GreenshotButton historyButton; + private GreenshotPlugin.Controls.GreenshotComboBox combobox_uploadimageformat; + private GreenshotPlugin.Controls.GreenshotLabel label_upload_format; + private GreenshotPlugin.Controls.GreenshotTextBox textBoxUrl; + private GreenshotPlugin.Controls.GreenshotLabel label_url; + private GreenshotPlugin.Controls.GreenshotButton buttonCancel; + private GreenshotPlugin.Controls.GreenshotButton buttonOK; + private GreenshotPlugin.Controls.GreenshotCheckBox checkbox_usepagelink; } } diff --git a/GreenshotImgurPlugin/Forms/SettingsForm.cs b/GreenshotImgurPlugin/Forms/SettingsForm.cs index 1f6945277..db63d7c80 100644 --- a/GreenshotImgurPlugin/Forms/SettingsForm.cs +++ b/GreenshotImgurPlugin/Forms/SettingsForm.cs @@ -22,26 +22,21 @@ using System; using System.Windows.Forms; using GreenshotImgurPlugin.Forms; using GreenshotPlugin.Core; +using GreenshotPlugin.Controls; namespace GreenshotImgurPlugin { /// /// Description of PasswordRequestForm. /// - public partial class SettingsForm : Form { - private ILanguage lang = Language.GetInstance(); - + public partial class SettingsForm : ImgurForm { public SettingsForm(ImgurConfiguration config) { + language = Language.GetInstance(); // // The InitializeComponent() call is required for Windows Forms designer support. // InitializeComponent(); this.Icon = GreenshotPlugin.Core.GreenshotResources.getGreenshotIcon(); - InitializeTexts(); - combobox_uploadimageformat.Items.Clear(); - foreach(OutputFormat format in Enum.GetValues(typeof(OutputFormat))) { - combobox_uploadimageformat.Items.Add(format.ToString()); - } ImgurUtils.LoadHistory(); if (config.runtimeImgurHistory.Count > 0) { @@ -51,31 +46,6 @@ namespace GreenshotImgurPlugin { } } - private void InitializeTexts() { - this.label_url.Text = lang.GetString(LangKey.label_url); - this.buttonOK.Text = lang.GetString(LangKey.OK); - this.buttonCancel.Text = lang.GetString(LangKey.CANCEL); - this.Text = lang.GetString(LangKey.settings_title); - this.label_upload_format.Text = lang.GetString(LangKey.label_upload_format); - this.checkbox_usepagelink.Text = lang.GetString(LangKey.use_page_link); - this.historyButton.Text = lang.GetString(LangKey.imgur_history); - } - - public string Url { - get {return textBoxUrl.Text;} - set {textBoxUrl.Text = value;} - } - - public string UploadFormat { - get {return combobox_uploadimageformat.Text;} - set {combobox_uploadimageformat.Text = value;} - } - - public bool UsePageLink { - get { return checkbox_usepagelink.Checked; } - set { checkbox_usepagelink.Checked = value; } - } - void ButtonOKClick(object sender, EventArgs e) { this.DialogResult = DialogResult.OK; } diff --git a/GreenshotImgurPlugin/GreenshotImgurPlugin.csproj b/GreenshotImgurPlugin/GreenshotImgurPlugin.csproj index 9904bf021..0c8ad766b 100644 --- a/GreenshotImgurPlugin/GreenshotImgurPlugin.csproj +++ b/GreenshotImgurPlugin/GreenshotImgurPlugin.csproj @@ -53,7 +53,7 @@ - + ImgurHistory.cs diff --git a/GreenshotImgurPlugin/ImgurConfiguration.cs b/GreenshotImgurPlugin/ImgurConfiguration.cs index 33b344ed4..7a1a48fe7 100644 --- a/GreenshotImgurPlugin/ImgurConfiguration.cs +++ b/GreenshotImgurPlugin/ImgurConfiguration.cs @@ -76,18 +76,8 @@ namespace GreenshotImgurPlugin { } finally { backgroundForm.CloseDialog(); } - settingsForm.Url = ImgurApiUrl; - settingsForm.UsePageLink = UsePageLink; - settingsForm.UploadFormat = UploadFormat.ToString(); DialogResult result = settingsForm.ShowDialog(); if (result == DialogResult.OK) { - if (!settingsForm.Url.Equals(ImgurApiUrl) || !settingsForm.UploadFormat.Equals(UploadFormat.ToString()) - || !!settingsForm.UsePageLink.Equals(UsePageLink)) { - ImgurApiUrl = settingsForm.Url; - UploadFormat = (OutputFormat)Enum.Parse(typeof(OutputFormat), settingsForm.UploadFormat.ToLower()); - UsePageLink = settingsForm.UsePageLink; - } - IniConfig.Save(); return true; } return false; diff --git a/GreenshotImgurPlugin/Language.cs b/GreenshotImgurPlugin/Language.cs index 2ad69ab6c..a9c18e95a 100644 --- a/GreenshotImgurPlugin/Language.cs +++ b/GreenshotImgurPlugin/Language.cs @@ -28,8 +28,8 @@ namespace GreenshotImgurPlugin { /// public class Language : LanguageContainer, ILanguage { private static ILanguage uniqueInstance; - private const string LANGUAGE_FILENAME_PATTERN = @"language_imgurplugin-*.xml"; - + public const string LANGUAGE_FILENAME_PATTERN = @"language_imgurplugin-*.xml"; + public static ILanguage GetInstance() { if(uniqueInstance == null) { uniqueInstance = new LanguageContainer(LANGUAGE_FILENAME_PATTERN); diff --git a/GreenshotPlugin/Controls/GreenshotForm.cs b/GreenshotPlugin/Controls/GreenshotForm.cs index ac1a270ce..3c61b8f2b 100644 --- a/GreenshotPlugin/Controls/GreenshotForm.cs +++ b/GreenshotPlugin/Controls/GreenshotForm.cs @@ -6,11 +6,14 @@ using System.Reflection; using GreenshotPlugin.Core; using Greenshot.IniFile; using System.ComponentModel; +using System.ComponentModel.Design; +using System.IO; namespace GreenshotPlugin.Controls { - public class GreenshotForm : Form , IGreenshotLanguageBindable { + public abstract class GreenshotForm : Form, IGreenshotLanguageBindable { private static log4net.ILog LOG = log4net.LogManager.GetLogger(typeof(GreenshotForm)); protected ILanguage language; + private IComponentChangeService m_changeService; [Category("Greenshot"), DefaultValue(null), Description("Specifies key of the language file to use when displaying the text.")] public string LanguageKey { @@ -18,13 +21,32 @@ namespace GreenshotPlugin.Controls { set; } - public GreenshotForm() : base() { + protected abstract string LanguagePattern { get; } + + protected void InitializeForDesigner() { + if (language == null && this.DesignMode) { + 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); + } catch (Exception ex) { + MessageBox.Show(ex.ToString()); + } + } } protected override void OnLoad(EventArgs e) { if (!this.DesignMode) { ApplyLanguage(); FillFields(); + } else { + InitializeForDesigner(); } base.OnLoad(e); } @@ -34,19 +56,78 @@ namespace GreenshotPlugin.Controls { /// /// protected override void OnClosed(EventArgs e) { - if (DialogResult == DialogResult.OK) { - LOG.Info("Form was closed with OK: storing field values."); - StoreFields(); + if (!this.DesignMode) { + if (DialogResult == DialogResult.OK) { + LOG.Info("Form was closed with OK: storing field values."); + StoreFields(); + } } 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. + public override ISite Site { + get { + return base.Site; + } + set { + // Clear any component change event handlers. + ClearChangeNotifications(); + + // Set the new Site value. + base.Site = value; + + m_changeService = (IComponentChangeService)GetService(typeof(IComponentChangeService)); + + // Register event handlers for component change events. + RegisterChangeNotifications(); + } + } + + private void ClearChangeNotifications() { + // The m_changeService value is null when not in design mode, + // as the IComponentChangeService is only available at design time. + m_changeService = (IComponentChangeService)GetService(typeof(IComponentChangeService)); + + // Clear our the component change events to prepare for re-siting. + if (m_changeService != null) { + m_changeService.ComponentChanged -= new ComponentChangedEventHandler(OnComponentChanged); + } + } + + private void RegisterChangeNotifications() { + // Register the event handlers for the IComponentChangeService events + if (m_changeService != null) { + m_changeService.ComponentChanged += new ComponentChangedEventHandler(OnComponentChanged); + } + } + + /* This method handles the OnComponentChanged event to display a notification. */ + 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); + } + } + } + + // Clean up any resources being used. + protected override void Dispose(bool disposing) { + if (disposing) { + ClearChangeNotifications(); + } + base.Dispose(disposing); + } + /// /// Apply all the language settings to the "Greenshot" Controls on this form /// protected void ApplyLanguage() { if (language == null) { - throw new ArgumentNullException("Language not set!! Please use 'language = Language.GetInstance()' in your form constructor!"); + MessageBox.Show("Language not set!! Please use 'language = Language.GetInstance()' in your form constructor!"); + return; } // Set title of the form if (!string.IsNullOrEmpty(LanguageKey)) { @@ -58,19 +139,21 @@ namespace GreenshotPlugin.Controls { continue; } Object controlObject = field.GetValue(this); - if (typeof(IGreenshotLanguageBindable).IsAssignableFrom(field.FieldType)) { - IGreenshotLanguageBindable languageBindable = controlObject as IGreenshotLanguageBindable; - if (!string.IsNullOrEmpty(languageBindable.LanguageKey)) { - if (!language.hasKey(languageBindable.LanguageKey)) { - LOG.WarnFormat("Wrong language key '{0}' configured for field '{1}'", languageBindable.LanguageKey, field.Name); - continue; - } - Control control = controlObject as Control; - control.Text = language.GetString(languageBindable.LanguageKey); - } else { - LOG.WarnFormat("Greenshot control without language key: {0}", field.Name); - } + if (controlObject == null) { + continue; } + Control applyTo = controlObject as Control; + if (applyTo == null) { + // not a control + 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)) { @@ -87,7 +170,21 @@ namespace GreenshotPlugin.Controls { } } } + } + /// + /// Apply the language text to supplied control + /// + protected void ApplyLanguage(Control applyTo, string languageKey) { + if (!string.IsNullOrEmpty(languageKey)) { + if (!language.hasKey(languageKey)) { + MessageBox.Show(string.Format("Wrong language key '{0}' configured for control '{1}'", languageKey, applyTo.Name)); + return; + } + applyTo.Text = language.GetString(languageKey); + } else { + MessageBox.Show(string.Format("Greenshot control without language key: {0}", applyTo.Name)); + } } /// diff --git a/GreenshotPlugin/Core/LanguageHelper.cs b/GreenshotPlugin/Core/LanguageHelper.cs index 32b8e6ad2..93de4faeb 100644 --- a/GreenshotPlugin/Core/LanguageHelper.cs +++ b/GreenshotPlugin/Core/LanguageHelper.cs @@ -74,7 +74,8 @@ namespace GreenshotPlugin.Core { private static string PAF_LANGUAGE_PATH = Path.Combine(APPLICATION_PATH, @"App\Greenshot\Languages"); private const string HELP_FILENAME_PATTERN = @"help-*.html"; private const string LANGUAGE_GROUPS_KEY = @"SYSTEM\CurrentControlSet\Control\Nls\Language Groups"; - private static string globalLanguage = null; + private static string globalLanguage = DEFAULT_LANGUAGE; + private string additionalPath = null; // Set if the designer is used private Dictionary strings = new Dictionary(); private List languages = new List(); @@ -106,7 +107,11 @@ namespace GreenshotPlugin.Core { instances.Add(this); } - public LanguageContainer(string filePattern) : this() { + public LanguageContainer(string filePattern) : this(filePattern, null) { + } + + public LanguageContainer(string filePattern, string additionalPath) { + this.additionalPath = additionalPath; LanguageFilePattern = filePattern; Load(); SetInstanceLanguage(globalLanguage); @@ -171,7 +176,7 @@ namespace GreenshotPlugin.Core { Dictionary identifiedLanguages = new Dictionary(); if (languages == null || languages.Count == 0) { - throw new FileNotFoundException("No language files found!"); + throw new FileNotFoundException(string.Format("No language files for {0} found!", wantedIETF)); } // Find selected languages in available languages @@ -265,6 +270,9 @@ namespace GreenshotPlugin.Core { } else { languageDirectories.Add(APPLICATIONDATA_LANGUAGE_PATH); } + if (additionalPath != null) { + languageDirectories.Add(additionalPath); + } languageDirectories.Add(STARTUP_LANGUAGE_PATH); foreach(string path in languageDirectories) { if (!Directory.Exists(path)) { diff --git a/GreenshotPlugin/IniFile/IniConfig.cs b/GreenshotPlugin/IniFile/IniConfig.cs index 261eac567..f29c4e9f0 100644 --- a/GreenshotPlugin/IniFile/IniConfig.cs +++ b/GreenshotPlugin/IniFile/IniConfig.cs @@ -61,6 +61,12 @@ namespace Greenshot.IniFile { WatchConfigFile(true); } + public static bool IsInited { + get { + return applicationName != null && configName != null; + } + } + public static void ForceIniInStartupPath() { if (portableCheckMade) { throw new Exception("ForceLocal should be called before any file is read");