tkuhn1l6 | e4eb4cd | 2019-05-16 11:06:07 +0200 | [diff] [blame] | 1 | using System; |
| 2 | using System.Collections.Generic; |
| 3 | using System.Linq; |
| 4 | using System.Text; |
| 5 | using System.Threading.Tasks; |
| 6 | using System.Windows; |
| 7 | using System.Windows.Controls; |
| 8 | using System.Windows.Input; |
| 9 | |
| 10 | /* Copyright (c) 2018-2019 Festo AG & Co. KG <https://www.festo.com/net/de_de/Forms/web/contact_international>, author: Michael Hoffmeister |
| 11 | This software is licensed under the Eclipse Public License - v 2.0 (EPL-2.0) (see https://www.eclipse.org/org/documents/epl-2.0/EPL-2.0.txt). |
| 12 | The browser functionality is under the cefSharp license (see https://raw.githubusercontent.com/cefsharp/CefSharp/master/LICENSE). |
| 13 | The JSON serialization is under the MIT license (see https://github.com/JamesNK/Newtonsoft.Json/blob/master/LICENSE.md). */ |
| 14 | |
| 15 | namespace AasxPackageExplorer |
| 16 | { |
| 17 | // |
| 18 | // Modify Repo |
| 19 | // |
| 20 | |
| 21 | public class ModifyRepo |
| 22 | { |
| 23 | // some types for LambdaAction |
| 24 | public class LambdaAction { } |
| 25 | public class LambdaActionNone : LambdaAction { } |
| 26 | public class LambdaActionRedrawEntity : LambdaAction { } |
| 27 | public class LambdaActionRedrawAllElements : LambdaAction |
| 28 | { |
| 29 | public object NextFocus = null; |
| 30 | public bool? IsExpanded = null; |
| 31 | public LambdaActionRedrawAllElements(object nextFocus, bool? isExpanded = true) |
| 32 | { |
| 33 | this.NextFocus = nextFocus; |
| 34 | this.IsExpanded = isExpanded; |
| 35 | } |
| 36 | } |
| 37 | public class LambdaActionContentsChanged : LambdaAction { } |
| 38 | public class LambdaActionContentsTakeOver : LambdaAction { } |
| 39 | |
| 40 | // some flags for the main application |
| 41 | public List<LambdaAction> WishForOutsideAction = new List<LambdaAction>(); |
| 42 | |
| 43 | public class RepoItem |
| 44 | { |
| 45 | public Control control = null; |
| 46 | public Func<object, LambdaAction> setValueLambda = null; |
| 47 | public object originalValue = null; |
| 48 | } |
| 49 | |
| 50 | private Dictionary<Control, RepoItem> items = new Dictionary<Control, RepoItem>(); |
| 51 | |
| 52 | |
| 53 | public Control RegisterControl(Control c, Func<object, LambdaAction> setValue) |
| 54 | { |
| 55 | // add item |
| 56 | var it = new RepoItem(); |
| 57 | it.control = c; |
| 58 | it.setValueLambda = setValue; |
| 59 | items.Add(c, it); |
| 60 | |
| 61 | // put callbacks accordingly |
| 62 | if (c is TextBox) |
| 63 | { |
| 64 | var tb = c as TextBox; |
| 65 | it.originalValue = "" + tb.Text; |
| 66 | tb.TextChanged += Tb_TextChanged; |
| 67 | tb.KeyUp += Tb_KeyUp; |
| 68 | } |
| 69 | |
| 70 | if (c is Button) |
| 71 | { |
| 72 | var b = c as Button; |
| 73 | b.Click += B_Click; |
| 74 | } |
| 75 | |
| 76 | if (c is ComboBox) |
| 77 | { |
| 78 | var cb = c as ComboBox; |
| 79 | it.originalValue = "" + cb.Text; |
| 80 | cb.AddHandler(System.Windows.Controls.Primitives.TextBoxBase.TextChangedEvent, |
| 81 | new System.Windows.Controls.TextChangedEventHandler(Tb_TextChanged)); |
| 82 | if (!cb.IsEditable) |
| 83 | // we need this event |
| 84 | cb.SelectionChanged += Cb_SelectionChanged; |
| 85 | if (cb.IsEditable) |
| 86 | // add this for comfort |
| 87 | cb.KeyUp += Tb_KeyUp; |
| 88 | } |
| 89 | |
| 90 | if (c is CheckBox) |
| 91 | { |
| 92 | var cb = c as CheckBox; |
| 93 | it.originalValue = cb.IsChecked; |
| 94 | cb.Checked += Cb_Checked; |
| 95 | cb.Unchecked += Cb_Checked; |
| 96 | } |
| 97 | |
| 98 | return c; |
| 99 | } |
| 100 | |
| 101 | private void Cb_SelectionChanged(object sender, SelectionChangedEventArgs e) |
| 102 | { |
| 103 | // sender shall be in dictionary |
| 104 | if (sender is Control && items.ContainsKey(sender as Control)) |
| 105 | { |
| 106 | var it = items[sender as Control]; |
| 107 | if (it.control is ComboBox && it.setValueLambda != null) |
| 108 | it.setValueLambda((string)(it.control as ComboBox).SelectedItem); |
| 109 | |
| 110 | // contents changed |
| 111 | WishForOutsideAction.Add(new LambdaActionContentsTakeOver()); |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | private void Cb_Checked(object sender, RoutedEventArgs e) |
| 116 | { |
| 117 | // sender shall be in dictionary |
| 118 | if (sender is Control && items.ContainsKey(sender as Control)) |
| 119 | { |
| 120 | var it = items[sender as Control]; |
| 121 | if (it.control is CheckBox && it.setValueLambda != null) |
| 122 | it.setValueLambda((bool)(it.control as CheckBox).IsChecked); |
| 123 | |
| 124 | // contents changed |
| 125 | WishForOutsideAction.Add(new LambdaActionContentsTakeOver()); |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | private void B_Click(object sender, RoutedEventArgs e) |
| 130 | { |
| 131 | // sender shall be in dictionary |
| 132 | if (sender is Control && items.ContainsKey(sender as Control)) |
| 133 | { |
| 134 | var it = items[sender as Control]; |
| 135 | if (it.control is Button) |
| 136 | { |
| 137 | var action = it.setValueLambda(null); |
| 138 | if (action != null) |
| 139 | WishForOutsideAction.Add(action); |
| 140 | } |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | private void Tb_TextChanged(object sender, TextChangedEventArgs e) |
| 145 | { |
| 146 | // sender shall be in dictionary |
| 147 | if (sender is Control && items.ContainsKey(sender as Control)) |
| 148 | { |
| 149 | var it = items[sender as Control]; |
| 150 | if (it.control is TextBox && it.setValueLambda != null) |
| 151 | it.setValueLambda((it.control as TextBox).Text); |
| 152 | if (it.control is ComboBox && it.setValueLambda != null) |
| 153 | it.setValueLambda((it.control as ComboBox).Text); |
| 154 | |
| 155 | // contents changed |
| 156 | WishForOutsideAction.Add(new LambdaActionContentsChanged()); |
| 157 | } |
| 158 | } |
| 159 | |
| 160 | private void Tb_KeyUp(object sender, KeyEventArgs e) |
| 161 | { |
| 162 | if (e.Key == Key.Enter) |
| 163 | { |
| 164 | e.Handled = true; |
| 165 | // send a take over |
| 166 | WishForOutsideAction.Add(new LambdaActionContentsTakeOver()); |
| 167 | } |
| 168 | } |
| 169 | |
| 170 | public void Clear() |
| 171 | { |
| 172 | items.Clear(); |
| 173 | } |
| 174 | |
| 175 | public void CallUndoChanges() |
| 176 | { |
| 177 | foreach (var it in items.Values) |
| 178 | { |
| 179 | if (it.control != null && it.originalValue != null) |
| 180 | { |
| 181 | if (it.control is TextBox) |
| 182 | { |
| 183 | (it.control as TextBox).Text = it.originalValue as string; |
| 184 | } |
| 185 | } |
| 186 | |
| 187 | // contents changed |
| 188 | WishForOutsideAction.Add(new LambdaActionContentsTakeOver()); |
| 189 | } |
| 190 | } |
| 191 | } |
| 192 | } |