From b620abbcefa269379781d90ce34c538cd7be268e Mon Sep 17 00:00:00 2001 From: Unknown Date: Thu, 7 Jul 2011 13:06:08 -0700 Subject: phone control exploration for BCT, not integrated yet should be made into a plugin sometime --- BCT/TranslationPlugins/PhoneControlsPlugin.cs | 205 ++++++++++++++++++++++ BCT/TranslationPlugins/Properties/AssemblyInfo.cs | 36 ++++ BCT/TranslationPlugins/TranslationPlugin.cs | 10 ++ BCT/TranslationPlugins/TranslationPlugins.csproj | 55 ++++++ 4 files changed, 306 insertions(+) create mode 100644 BCT/TranslationPlugins/PhoneControlsPlugin.cs create mode 100644 BCT/TranslationPlugins/Properties/AssemblyInfo.cs create mode 100644 BCT/TranslationPlugins/TranslationPlugin.cs create mode 100644 BCT/TranslationPlugins/TranslationPlugins.csproj (limited to 'BCT/TranslationPlugins') diff --git a/BCT/TranslationPlugins/PhoneControlsPlugin.cs b/BCT/TranslationPlugins/PhoneControlsPlugin.cs new file mode 100644 index 00000000..d63b6489 --- /dev/null +++ b/BCT/TranslationPlugins/PhoneControlsPlugin.cs @@ -0,0 +1,205 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.IO; + +namespace TranslationPlugins { + public enum Visibility { Visible, Collapsed }; + + public enum Event { Click, Checked, Unchecked }; + + public class HandlerSignature { + public static string[] getParameterTypesForHandler(Event controlEvent) { + switch (controlEvent) { + case Event.Checked: + case Event.Unchecked: + case Event.Click: + return new string[] { "object", "System.WindowsRoutedventArgs" }; + default: + throw new NotImplementedException("Handlers for event: " + controlEvent + " not supported yet"); + } + } + // TODO it would be nice to be dynamic on handler names and parameters + // TODO for now you just have to know the handler signature for each event at load time, and for now we only handle a handful of default control events + public string Name; + public string[] ParameterTypes; + } + + class ControlInfoStructure { + public string Name; + public string ClassName; + public bool IsEnabled; + public Visibility Visible; + private IDictionary> handlers; + + public ControlInfoStructure() { + handlers = new Dictionary>(); + } + + public void setHandler(Event p, string handler) { + IList eventHandlers; + try { + eventHandlers = handlers[p]; + } catch (KeyNotFoundException) { + eventHandlers= new List(); + } + + HandlerSignature newHandler= new HandlerSignature(); + newHandler.Name= handler; + newHandler.ParameterTypes= HandlerSignature.getParameterTypesForHandler(p); + eventHandlers.Add(newHandler); + } + + public IList getHandlers(Event p) { + try { + return handlers[p]; + } catch (KeyNotFoundException) { + return new List(); + } + } + } + + class PageStructure { + public PageStructure() { + controlsInfo = new Dictionary(); + } + + public string PageClassName; + public string PageXAML; + + private IDictionary controlsInfo; + public ControlInfoStructure getControlInfo(string controlName) { + try { + return controlsInfo[controlName]; + } catch (KeyNotFoundException) { + return null; + } + } + + public void setControlInfo(string controlName, ControlInfoStructure controlInfo) { + controlsInfo[controlName] = controlInfo; + } + } + + public class PhoneControlsPlugin : TranslationPlugin { + // TODO this will probably need a complete rewrite once it is event based, and make it more push than pull + // TODO but it doesn't make sense right now to make it BCT or CCI aware + private static int CONFIG_LINE_FIELDS= 9; + private static int PAGE_CLASS_FIELD= 0; + private static int PAGE_XAML_FIELD= 1; + private static int CONTROL_CLASS_FIELD= 2; + private static int CONTROL_NAME_FIELD= 3; + private static int ENABLED_FIELD= 4; + private static int VISIBILITY_FIELD= 5; + private static int CLICK_HANDLER_FIELD= 6; + private static int CHECKED_HANDLER_FIELD= 7; + private static int UNCHECKED_HANDLER_FIELD = 8; + + private IDictionary pageStructureInfo; + + public PhoneControlsPlugin(string configFile) { + pageStructureInfo = new Dictionary(); + StreamReader fileStream = null; + try { + fileStream = new StreamReader(configFile); + } catch (Exception e) { + if (e is DirectoryNotFoundException || e is FileNotFoundException || e is IOException) { + // TODO log, I don't want to terminate BCT because of this + } else if (e is ArgumentException || e is ArgumentNullException) { + // TODO log, I don't want to terminate BCT because of this + } else { + throw; + } + } + + LoadControlStructure(fileStream); + + if (fileStream != null) + fileStream.Close(); + } + + private void LoadControlStructure(StreamReader configStream) { + // TODO it would be nice to have some kind of dynamic definition of config format + // TODO for now remember that config format is CSV + // TODO each line is ,,,,,,,, + // TODO check PhoneControlsExtractor.py + + // TODO the page.xaml value is saved with no directory information: if two pages exist with same name but different directories it will treat them as the same + // TODO I'm not handling this for now, and I won't be handling relative/absolute URI either for now + + try { + string pageClass, pageXAML, controlClass, controlName, enabled, visibility, clickHandler, checkedHandler, uncheckedHandler; + string configLine = configStream.ReadLine(); + string[] inputLine; + PageStructure pageStr; + ControlInfoStructure controlInfoStr; + + while (configLine != null) { + inputLine = configLine.Split(','); + + if (inputLine.Length != CONFIG_LINE_FIELDS) + throw new ArgumentException("Config input line contains wrong number of fields: " + inputLine.Length + ", expected " + CONFIG_LINE_FIELDS); + + pageClass = inputLine[PAGE_CLASS_FIELD]; + pageXAML = inputLine[PAGE_XAML_FIELD]; + controlClass = inputLine[CONTROL_CLASS_FIELD]; + controlName = inputLine[CONTROL_NAME_FIELD]; + enabled = inputLine[ENABLED_FIELD]; + visibility = inputLine[VISIBILITY_FIELD]; + clickHandler = inputLine[CLICK_HANDLER_FIELD]; + checkedHandler = inputLine[CHECKED_HANDLER_FIELD]; + uncheckedHandler = inputLine[UNCHECKED_HANDLER_FIELD]; + + try { + pageStr = pageStructureInfo[pageClass]; + } catch (KeyNotFoundException) { + pageStr = new PageStructure(); + pageStr.PageClassName = pageClass; + pageStr.PageXAML = pageXAML; + } + + controlInfoStr= pageStr.getControlInfo(controlName); + if (controlInfoStr == null) { + controlInfoStr = new ControlInfoStructure(); + controlInfoStr.Name = controlName; + controlInfoStr.ClassName = controlClass; + } + controlInfoStr.IsEnabled = Boolean.Parse(enabled); + controlInfoStr.Visible = visibility == "Collapsed" ? Visibility.Collapsed : Visibility.Visible; + controlInfoStr.setHandler(Event.Click, clickHandler); + controlInfoStr.setHandler(Event.Checked, checkedHandler); + controlInfoStr.setHandler(Event.Unchecked, uncheckedHandler); + + pageStr.setControlInfo(controlName, controlInfoStr); + configLine = configStream.ReadLine(); + + } + } catch (Exception) { + // TODO log, I don't want to terminate BCT because of this + } + } + + public void getControlsForPage(string pageClass) { + } + + public string getXAMLForPage(string pageClass) { + return pageStructureInfo[pageClass].PageXAML; + } + + public bool getIsEnabled(string pageClass, string controlName) { + return pageStructureInfo[pageClass].getControlInfo(controlName).IsEnabled; + } + + public Visibility getVisibility(string pageClass, string controlName) { + return pageStructureInfo[pageClass].getControlInfo(controlName).Visible; + } + + public IList getHandlers(string pageClass, string controlName, string eventName) { + if (eventName != "Checked" && eventName != "Unchecked" && eventName != "Click") + throw new NotImplementedException("Event " + eventName + " is not translated or defined for control " + controlName + " in page " + pageClass); + + return pageStructureInfo[pageClass].getControlInfo(controlName).getHandlers((Event) Event.Parse(typeof(Event), eventName)); + } + } +} diff --git a/BCT/TranslationPlugins/Properties/AssemblyInfo.cs b/BCT/TranslationPlugins/Properties/AssemblyInfo.cs new file mode 100644 index 00000000..8158eb08 --- /dev/null +++ b/BCT/TranslationPlugins/Properties/AssemblyInfo.cs @@ -0,0 +1,36 @@ +using System.Reflection; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; + +// General Information about an assembly is controlled through the following +// set of attributes. Change these attribute values to modify the information +// associated with an assembly. +[assembly: AssemblyTitle("TranslationPlugins")] +[assembly: AssemblyDescription("")] +[assembly: AssemblyConfiguration("")] +[assembly: AssemblyCompany("Microsoft")] +[assembly: AssemblyProduct("TranslationPlugins")] +[assembly: AssemblyCopyright("Copyright © Microsoft 2011")] +[assembly: AssemblyTrademark("")] +[assembly: AssemblyCulture("")] + +// Setting ComVisible to false makes the types in this assembly not visible +// to COM components. If you need to access a type in this assembly from +// COM, set the ComVisible attribute to true on that type. +[assembly: ComVisible(false)] + +// The following GUID is for the ID of the typelib if this project is exposed to COM +[assembly: Guid("c26f68f2-14fb-4a47-8707-b7de569865cc")] + +// Version information for an assembly consists of the following four values: +// +// Major Version +// Minor Version +// Build Number +// Revision +// +// You can specify all the values or you can default the Build and Revision Numbers +// by using the '*' as shown below: +// [assembly: AssemblyVersion("1.0.*")] +[assembly: AssemblyVersion("1.0.0.0")] +[assembly: AssemblyFileVersion("1.0.0.0")] diff --git a/BCT/TranslationPlugins/TranslationPlugin.cs b/BCT/TranslationPlugins/TranslationPlugin.cs new file mode 100644 index 00000000..e8907a09 --- /dev/null +++ b/BCT/TranslationPlugins/TranslationPlugin.cs @@ -0,0 +1,10 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; + +namespace TranslationPlugins { + public class TranslationPlugin { + // TODO ideally these should really work as plugins, possibly registering as callbacks to some BCT events. + } +} diff --git a/BCT/TranslationPlugins/TranslationPlugins.csproj b/BCT/TranslationPlugins/TranslationPlugins.csproj new file mode 100644 index 00000000..9a27e949 --- /dev/null +++ b/BCT/TranslationPlugins/TranslationPlugins.csproj @@ -0,0 +1,55 @@ + + + + Debug + AnyCPU + 8.0.30703 + 2.0 + {8C242D42-9714-440F-884D-F64F09E78C7B} + Library + Properties + TranslationPlugins + TranslationPlugins + v4.0 + 512 + + + true + full + false + bin\Debug\ + DEBUG;TRACE + prompt + 4 + + + pdbonly + true + bin\Release\ + TRACE + prompt + 4 + + + + + + + + + + + + + + + + + + \ No newline at end of file -- cgit v1.2.3