|
| 1 | +// Copyright (c) 2014 AlphaSierraPapa for the SharpDevelop Team |
| 2 | +// |
| 3 | +// Permission is hereby granted, free of charge, to any person obtaining a copy of this |
| 4 | +// software and associated documentation files (the "Software"), to deal in the Software |
| 5 | +// without restriction, including without limitation the rights to use, copy, modify, merge, |
| 6 | +// publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons |
| 7 | +// to whom the Software is furnished to do so, subject to the following conditions: |
| 8 | +// |
| 9 | +// The above copyright notice and this permission notice shall be included in all copies or |
| 10 | +// substantial portions of the Software. |
| 11 | +// |
| 12 | +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, |
| 13 | +// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR |
| 14 | +// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE |
| 15 | +// FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR |
| 16 | +// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
| 17 | +// DEALINGS IN THE SOFTWARE. |
| 18 | + |
| 19 | +using System; |
| 20 | +using System.Linq; |
| 21 | +using System.Windows; |
| 22 | +using System.Windows.Controls; |
| 23 | +using System.Windows.Controls.Primitives; |
| 24 | +using ICSharpCode.NRefactory.CSharp; |
| 25 | + |
| 26 | +namespace CSharpBinding.FormattingStrategy |
| 27 | +{ |
| 28 | + /// <summary> |
| 29 | + /// Offers an attached property to bind a formatting option to a ComboBox. |
| 30 | + /// </summary> |
| 31 | + public static class FormattingOptionBinding |
| 32 | + { |
| 33 | + public static readonly DependencyProperty ContainerProperty = |
| 34 | + DependencyProperty.RegisterAttached("Container", typeof(CSharpFormattingOptionsContainer), |
| 35 | + typeof(FormattingOptionBinding), |
| 36 | + new FrameworkPropertyMetadata(OnContainerPropertyChanged)); |
| 37 | + public static readonly DependencyProperty OptionProperty = |
| 38 | + DependencyProperty.RegisterAttached("Option", typeof(string), typeof(FormattingOptionBinding), |
| 39 | + new FrameworkPropertyMetadata(OnOptionPropertyChanged)); |
| 40 | + |
| 41 | + public static CSharpFormattingOptionsContainer GetContainer(Selector element) |
| 42 | + { |
| 43 | + return (CSharpFormattingOptionsContainer) element.GetValue(ContainerProperty); |
| 44 | + } |
| 45 | + |
| 46 | + public static void SetContainer(Selector element, CSharpFormattingOptionsContainer enumType) |
| 47 | + { |
| 48 | + element.SetValue(ContainerProperty, enumType); |
| 49 | + } |
| 50 | + |
| 51 | + public static string GetOption(Selector element) |
| 52 | + { |
| 53 | + return (string) element.GetValue(OptionProperty); |
| 54 | + } |
| 55 | + |
| 56 | + public static void SetOption(Selector element, string enumType) |
| 57 | + { |
| 58 | + element.SetValue(OptionProperty, enumType); |
| 59 | + } |
| 60 | + |
| 61 | + static void OnContainerPropertyChanged(DependencyObject o, DependencyPropertyChangedEventArgs e) |
| 62 | + { |
| 63 | + // No op? |
| 64 | + } |
| 65 | + |
| 66 | + static void OnOptionPropertyChanged(DependencyObject o, DependencyPropertyChangedEventArgs e) |
| 67 | + { |
| 68 | + string option = e.NewValue as string; |
| 69 | + ComboBox comboBox = o as ComboBox; |
| 70 | + if ((option != null) && (comboBox != null)) { |
| 71 | + // Add "default" entry in ComboBox |
| 72 | + // TODO Add located resource! |
| 73 | + comboBox.Items.Add(new ComboBoxItem { Content = "(default)", Tag = null }); |
| 74 | + comboBox.SelectedIndex = 0; |
| 75 | + |
| 76 | + CSharpFormattingOptionsContainer container = GetContainer(comboBox); |
| 77 | + if (container != null) { |
| 78 | + Type optionType = container.GetOptionType(option); |
| 79 | + FillComboValues(comboBox, optionType); |
| 80 | + object currentValue = container.GetOption(option); |
| 81 | + comboBox.SelectedItem = comboBox.Items.OfType<ComboBoxItem>().FirstOrDefault(item => currentValue.Equals(item.Tag)); |
| 82 | + } |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + static void FillComboValues(ComboBox comboBox, Type type) |
| 87 | + { |
| 88 | + if (type == typeof(bool)) { |
| 89 | + FillBoolComboValues(comboBox); |
| 90 | + } else if (type == typeof(BraceStyle)) { |
| 91 | + FillBraceStyleComboValues(comboBox); |
| 92 | + } else if (type == typeof(PropertyFormatting)) { |
| 93 | + FillPropertyFormattingComboValues(comboBox); |
| 94 | + } else if (type == typeof(Wrapping)) { |
| 95 | + FillWrappingComboValues(comboBox); |
| 96 | + } else if (type == typeof(NewLinePlacement)) { |
| 97 | + FillNewLinePlacementComboValues(comboBox); |
| 98 | + } else if (type == typeof(UsingPlacement)) { |
| 99 | + FillUsingPlacementComboValues(comboBox); |
| 100 | + } else if (type == typeof(EmptyLineFormatting)) { |
| 101 | + FillEmptyLineFormattingComboValues(comboBox); |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + static void FillBoolComboValues(ComboBox comboBox) |
| 106 | + { |
| 107 | + // TODO Add located resources! |
| 108 | + comboBox.Items.Add(new ComboBoxItem { Content = "Yes", Tag = true }); |
| 109 | + comboBox.Items.Add(new ComboBoxItem { Content = "No", Tag = false }); |
| 110 | + } |
| 111 | + |
| 112 | + static void FillBraceStyleComboValues(ComboBox comboBox) |
| 113 | + { |
| 114 | + // TODO Add located resources! |
| 115 | + comboBox.Items.Add(new ComboBoxItem { Content = "Do not change", Tag = BraceStyle.DoNotChange }); |
| 116 | + comboBox.Items.Add(new ComboBoxItem { Content = "End of line", Tag = BraceStyle.EndOfLine }); |
| 117 | + comboBox.Items.Add(new ComboBoxItem { Content = "End of line without space", Tag = BraceStyle.EndOfLineWithoutSpace }); |
| 118 | + comboBox.Items.Add(new ComboBoxItem { Content = "Next line", Tag = BraceStyle.NextLine }); |
| 119 | + comboBox.Items.Add(new ComboBoxItem { Content = "Next line shifted", Tag = BraceStyle.NextLineShifted }); |
| 120 | + comboBox.Items.Add(new ComboBoxItem { Content = "Next line shifted 2", Tag = BraceStyle.NextLineShifted2 }); |
| 121 | + comboBox.Items.Add(new ComboBoxItem { Content = "Banner style", Tag = BraceStyle.BannerStyle }); |
| 122 | + } |
| 123 | + |
| 124 | + static void FillPropertyFormattingComboValues(ComboBox comboBox) |
| 125 | + { |
| 126 | + // TODO Add located resources! |
| 127 | + comboBox.Items.Add(new ComboBoxItem { Content = "Allow one line", Tag = PropertyFormatting.AllowOneLine }); |
| 128 | + comboBox.Items.Add(new ComboBoxItem { Content = "Force one line", Tag = PropertyFormatting.ForceOneLine }); |
| 129 | + comboBox.Items.Add(new ComboBoxItem { Content = "Force new line", Tag = PropertyFormatting.ForceNewLine }); |
| 130 | + } |
| 131 | + |
| 132 | + static void FillWrappingComboValues(ComboBox comboBox) |
| 133 | + { |
| 134 | + // TODO Add located resources! |
| 135 | + comboBox.Items.Add(new ComboBoxItem { Content = "Do not change", Tag = Wrapping.DoNotChange }); |
| 136 | + comboBox.Items.Add(new ComboBoxItem { Content = "Do not wrap", Tag = Wrapping.DoNotWrap }); |
| 137 | + comboBox.Items.Add(new ComboBoxItem { Content = "Wrap always", Tag = Wrapping.WrapAlways }); |
| 138 | + comboBox.Items.Add(new ComboBoxItem { Content = "Wrap if too long", Tag = Wrapping.WrapIfTooLong }); |
| 139 | + } |
| 140 | + |
| 141 | + static void FillNewLinePlacementComboValues(ComboBox comboBox) |
| 142 | + { |
| 143 | + // TODO Add located resources! |
| 144 | + comboBox.Items.Add(new ComboBoxItem { Content = "Do not care", Tag = NewLinePlacement.DoNotCare }); |
| 145 | + comboBox.Items.Add(new ComboBoxItem { Content = "New line", Tag = NewLinePlacement.NewLine }); |
| 146 | + comboBox.Items.Add(new ComboBoxItem { Content = "Same line", Tag = NewLinePlacement.SameLine }); |
| 147 | + } |
| 148 | + |
| 149 | + static void FillUsingPlacementComboValues(ComboBox comboBox) |
| 150 | + { |
| 151 | + // TODO Add located resources! |
| 152 | + comboBox.Items.Add(new ComboBoxItem { Content = "Top of file", Tag = UsingPlacement.TopOfFile }); |
| 153 | + comboBox.Items.Add(new ComboBoxItem { Content = "Inside namespace", Tag = UsingPlacement.InsideNamespace }); |
| 154 | + } |
| 155 | + |
| 156 | + static void FillEmptyLineFormattingComboValues(ComboBox comboBox) |
| 157 | + { |
| 158 | + // TODO Add located resources! |
| 159 | + comboBox.Items.Add(new ComboBoxItem { Content = "Do not change", Tag = EmptyLineFormatting.DoNotChange }); |
| 160 | + comboBox.Items.Add(new ComboBoxItem { Content = "Indent", Tag = EmptyLineFormatting.Indent }); |
| 161 | + comboBox.Items.Add(new ComboBoxItem { Content = "Do not indent", Tag = EmptyLineFormatting.DoNotIndent }); |
| 162 | + } |
| 163 | + } |
| 164 | +} |
0 commit comments