-
Notifications
You must be signed in to change notification settings - Fork 2
/
ManagementWindow.xaml.cs
339 lines (292 loc) · 13.3 KB
/
ManagementWindow.xaml.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.IO;
using System.Xml;
using System.Windows.Controls.Primitives;
namespace MyFa
{
public partial class ManagementWindow : Window
{
private string path;
private XmlDocument cardXml;
private Dictionary<string, Card> cards;
private List<CardForDataGrid> cardsForDataGrid;
private System.Windows.Forms.OpenFileDialog importFileDialog;
public ManagementWindow(string newPath)
{
InitializeComponent();
path = newPath;
cardXml = new XmlDocument();
cardXml.Load(path);
XmlNodeList tmpCards = cardXml.SelectNodes(@"//card");
cardsForDataGrid = new List<CardForDataGrid>();
cards = new Dictionary<string, Card>();
XmlNodeList tmpTranslations;
List<string> tmpTranslationsList;
foreach (XmlNode tmpCard in tmpCards)
{
tmpTranslations = cardXml.SelectNodes(@"/cards/card[@word = '" + tmpCard.Attributes["word"].Value + @"']/translations/translation");
tmpTranslationsList = new List<string>();
foreach (XmlNode tmpTranslation in tmpTranslations) tmpTranslationsList.Add(tmpTranslation.Attributes["word"].Value);
cards.Add(tmpCard.Attributes["word"].Value, new Card() {
Mode = (CardMode)Int32.Parse(tmpCard.Attributes["mode"].Value),
Word = tmpCard.Attributes["word"].Value,
Translations = tmpTranslationsList,
Shown = Int32.Parse(tmpCard.Attributes["shown"].Value),
Tested = Int32.Parse(tmpCard.Attributes["tested"].Value)
});
cardsForDataGrid.Add(cards[tmpCard.Attributes["word"].Value].Data);
}
List<string> tmpModes = new List<string>() { "Untouched", "Learning", "Testing", "Done" };
ModeComboBox.ItemsSource = tmpModes;
CardGrid.ItemsSource = cardsForDataGrid;
Loaded += new RoutedEventHandler(OnLoad);
}
private void OnLoad(object sender, RoutedEventArgs e)
{
MinWidth = Width;
MinHeight = Height;
SizeToContent = System.Windows.SizeToContent.Manual;
this.Left = Owner.Left + (Owner.ActualWidth / 2) - (this.ActualWidth / 2);
this.Top = Owner.Top + (Owner.ActualHeight / 2) - (this.ActualHeight / 2);
}
private void ImportClick(object sender, RoutedEventArgs e)
{
importFileDialog = new System.Windows.Forms.OpenFileDialog();
importFileDialog.Multiselect = false;
importFileDialog.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
importFileDialog.Filter = "XML Files (*.xml)|*.xml|All Files (*.*)|*.*";
importFileDialog.FilterIndex = 0;
importFileDialog.RestoreDirectory = false;
if (importFileDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
XmlDocument tmpXml = new XmlDocument();
tmpXml.Load(importFileDialog.OpenFile());
XmlNodeList tmpCards = tmpXml.SelectNodes(@"//card");
XmlNodeList tmpTranslations;
List<string> tmpTranslationsList;
string tmpWord;
string[] separatedTranslations;
foreach (XmlNode tmpCard in tmpCards)
{
tmpWord = tmpCard.SelectSingleNode(@"word").InnerText.ToLower().Trim();
tmpTranslations = tmpCard.SelectNodes(@"meanings/meaning/translations/word");
tmpTranslationsList = new List<string>();
foreach (XmlNode tmpTranslation in tmpTranslations)
{
separatedTranslations = tmpTranslation.InnerText.ToLower().Split(',');
foreach (string tmpTranslationWord in separatedTranslations) tmpTranslationsList.Add(tmpTranslationWord.Trim());
}
if (!cards.ContainsKey(tmpWord))
{
cards.Add(tmpWord, new Card()
{
Mode = CardMode.Untouched,
Word = tmpWord,
Translations = tmpTranslationsList,
Shown = 0,
Tested = 0
});
cardsForDataGrid.Add(cards[tmpWord].Data);
}
else
{
cardsForDataGrid.Remove(cards[tmpWord].Data);
cards[tmpWord].Translations = cards[tmpWord].Translations.Concat(tmpTranslationsList).Distinct().ToList();
cards[tmpWord].Mode = CardMode.Untouched;
cardsForDataGrid.Add(cards[tmpWord].Data);
}
}
CardGrid.ItemsSource = null;
CardGrid.ItemsSource = cardsForDataGrid;
}
}
private void SelectAllClick(object sender, RoutedEventArgs e)
{
if (CardGrid.SelectedItems.Count == CardGrid.Items.Count) CardGrid.SelectedItems.Clear();
else CardGrid.SelectAll();
}
private void AddNewClick(object sender, RoutedEventArgs e)
{
CardWindow cw = new CardWindow(new Card() {
Mode = 0,
Word = "",
Translations = new List<string>(),
Shown = 0,
Tested = 0
});
cw.Owner = this;
if (cw.ShowDialog() == true)
{
cards.Add(cw.TheCard.Word, cw.TheCard);
cardsForDataGrid.Add(cw.TheCard.Data);
CardGrid.ItemsSource = null;
CardGrid.ItemsSource = cardsForDataGrid;
CardGrid.SelectedItem = cw.TheCard.Data;
}
}
private void EditClick(object sender, RoutedEventArgs e)
{
if (CardGrid.SelectedItems.Count == 1)
{
string word = ((CardForDataGrid)CardGrid.SelectedItem).Word;
Card oldCard = cards[word];
cardsForDataGrid.Remove(cards[word].Data);
cards.Remove(word);
CardWindow cw = new CardWindow(oldCard);
cw.Owner = this;
if (cw.ShowDialog() == true) oldCard = cw.TheCard;
cards.Add(oldCard.Word, oldCard);
cardsForDataGrid.Add(oldCard.Data);
CardGrid.ItemsSource = null;
CardGrid.ItemsSource = cardsForDataGrid;
CardGrid.SelectedItem = oldCard.Data;
}
}
private void DeleteClick(object sender, RoutedEventArgs e)
{
if (CardGrid.SelectedItems.Count > 0 && MessageBox.Show("Delete card" + (CardGrid.SelectedItems.Count > 1 ? "s" : "") + "?", "MyFa Cards - Delete", MessageBoxButton.YesNo, MessageBoxImage.Warning, MessageBoxResult.No) == MessageBoxResult.Yes)
{
foreach (CardForDataGrid selectedCard in CardGrid.SelectedItems)
{
cards.Remove(selectedCard.Word.ToLower());
cardsForDataGrid.Remove(selectedCard);
}
CardGrid.ItemsSource = null;
CardGrid.ItemsSource = cardsForDataGrid;
}
}
private void SaveClick(object sender, RoutedEventArgs e)
{
XmlTextWriter writer = new XmlTextWriter(File.Create(path), Encoding.UTF8);
writer.Formatting = Formatting.Indented;
writer.WriteProcessingInstruction("xml", "version='1.0' encoding='UTF-8'");
writer.WriteStartElement("cards");
foreach (Card tmpCard in cards.Values)
{
writer.WriteStartElement("card");
writer.WriteAttributeString("word", tmpCard.Word);
writer.WriteAttributeString("mode", ((int)tmpCard.Mode).ToString());
writer.WriteAttributeString("shown", tmpCard.Shown.ToString());
writer.WriteAttributeString("tested", tmpCard.Tested.ToString());
writer.WriteStartElement("translations");
foreach (string tmpTranslation in tmpCard.Translations)
{
writer.WriteStartElement("translation");
writer.WriteAttributeString("word", tmpTranslation);
writer.WriteEndElement();
}
writer.WriteEndElement();
writer.WriteEndElement();
}
writer.WriteEndElement();
writer.Close();
DialogResult = true;
}
private void CancelClick(object sender, RoutedEventArgs e)
{
DialogResult = false;
}
/*private void CheckFiles()
{
Path = Environment.GetFolderPath(Environment.SpecialFolder.Personal) + @"\MyFa Cards";
if (!Directory.Exists(Path)) Directory.CreateDirectory(Path);
FileInfo[] tmpFI = (new DirectoryInfo(Path)).GetFiles("*.xml");
SelectFile.Items.Clear();
string lastOpenedPath = "";
if (File.Exists(Environment.GetFolderPath(Environment.SpecialFolder.Personal) + @"\MyFa Cards\LastOpened.txt"))
{
StreamReader sr = new StreamReader(Environment.GetFolderPath(Environment.SpecialFolder.Personal) + @"\MyFa Cards\LastOpened.txt");
lastOpenedPath = sr.ReadLine();
sr.Close();
}
SelectFile.SelectedIndex = 0;
foreach (FileInfo tmpFile in tmpFI)
{
XmlDocument tmp = new XmlDocument();
tmp.Load(Environment.GetFolderPath(Environment.SpecialFolder.Personal) + @"\MyFa Cards\" + tmpFile.Name);
if (tmp.SelectNodes(@"//card[@word != '' and @mode and @shown and @tested]").Count == 0) continue;
ComboBoxItem tmpItem = new ComboBoxItem();
tmpItem.Content = tmpFile.Name;
SelectFile.Items.Add(tmpItem);
if (tmpFile.Name == lastOpenedPath) SelectFile.SelectedIndex = tmpFI.ToList().IndexOf(tmpFile);
}
if (SelectFile.Items.Count == 0)
{
OpenButton.IsEnabled = false;
OpenButton.Filter = Brushes.White;
MessageBox.Show("No any card collection in correct XML format found! Import them, please!");
}
else
{
OpenButton.Filter = Brushes.Green;
OpenButton.IsEnabled = true;
}
}*/
}
public class CardForDataGrid
{
public string Mode { get; set; }
public string Word { get; set; }
public string Translations { get; set; }
public string Shown { get; set; }
public string Tested { get; set; }
}
public class Card
{
private CardMode mode;
public CardMode Mode { get { return mode; } set { mode = value; Convert(); } }
private string word;
public string Word { get { return word; } set { word = value; Convert(); } }
private List<string> translations;
public List<string> Translations { get { return translations; } set { translations = value; Convert(); } }
private int shown;
public int Shown { get { return shown; } set { shown = value; Convert(); } }
private int tested;
public int Tested { get { return tested; } set { tested = value; Convert(); } }
private CardForDataGrid data;
public CardForDataGrid Data { get { return data; } private set { data = value; } }
/*public Card(Card copy)
{
Mode = copy.Mode;
Word = copy.Word;
Translations = copy.Translations;
Shown = copy.Shown;
Tested = copy.Tested;
Data = copy.Data;
}*/
public void Convert()
{
string tmpString = "";
if (Translations != null && Translations.Count > 0)
{
foreach (string translation in Translations) tmpString += translation + ", ";
tmpString = tmpString.Remove(tmpString.Length - 2);
}
Data = new CardForDataGrid() {
Mode = Mode.ToString(),
Word = Word,
Translations = tmpString,
Shown = Shown.ToString(),
Tested = Tested.ToString()
};
}
}
public enum CardMode
{
Untouched,
Learning,
Testing,
Done
}
}