-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEditView.xaml.cs
232 lines (198 loc) · 6.2 KB
/
EditView.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
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
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.Navigation;
using System.Windows.Shapes;
using System.Xml;
using System.Xml.Linq;
namespace IndoorFinder
{
/// <summary>
/// Interaction logic for EditView.xaml
/// </summary>
public partial class EditView : UserControl
{
private BuildingList _buildingList { get; set; }
public EditView()
{
InitializeComponent();
_buildingList = new BuildingList();
listBoxBuilding.ItemsSource = _buildingList.GetBuildings().Select(C => C.Name);
}
private void buttonAdd_Click(object sender, RoutedEventArgs e)
{
_buildingList.AddBuilding(textBoxBuliding.Text, textBoxMap.Text, textBoxPath.Text);
listBoxBuilding.ItemsSource = null;
listBoxBuilding.ItemsSource = _buildingList.GetBuildings().Select(C => C.Name);
}
private void listBoxBuilding_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
try
{
int buildingIndex = listBoxBuilding.SelectedIndex;
Building thisBuilding = _buildingList.GetBuildings()[buildingIndex];
listBoxMap.ItemsSource = null;
listBoxMap.ItemsSource = thisBuilding.GetMaps().Select(C => C.Name);
}
catch (ArgumentOutOfRangeException)
{
}
}
private void buttonSave_Click(object sender, RoutedEventArgs e)
{
if (MessageBox.Show("Are you sure?", "Save", MessageBoxButton.YesNo) == MessageBoxResult.Yes)
{
_buildingList.WriteXml();
}
}
}
public class BuildingList
{
private ObservableCollection<Building> _buildings { get; set; }
public BuildingList()
{
_buildings = new ObservableCollection<Building>();
loadXml();
}
private void loadXml()
{
XElement root;
try
{
root = XElement.Load("Buildings.xml");
}
catch
{
MessageBox.Show("No initial xml file.");
return;
}
foreach (var building in root.Elements("Building"))
{
Building thisBuilding = new Building((string)building.Attribute("Name"));
_buildings.Add(thisBuilding);
foreach (var map in building.Elements("Map"))
{
thisBuilding.AddMap((string)map.Attribute("Name"), (string)map.Attribute("Path"));
}
}
}
public ObservableCollection<Building> GetBuildings()
{
return _buildings;
}
public void AddBuilding(string buildingName, string mapName, string mapPath)
{
Building thisBuilding = IsExisted(buildingName);
if (thisBuilding == null)
{
thisBuilding = new Building(buildingName);
_buildings.Add(thisBuilding);
}
thisBuilding.AddMap(mapName, mapPath);
}
private Building IsExisted(string buildingName)
{
foreach (Building building in _buildings)
{
if (building.Name == buildingName)
{
return building;
}
}
return null;
}
public void WriteXml()
{
XmlDocument xmlBuildings = new XmlDocument();
XmlElement building_List = xmlBuildings.CreateElement("Building_List");
xmlBuildings.AppendChild(building_List);
foreach (Building building in _buildings)
{
building_List.AppendChild(building.BuildingXml(xmlBuildings));
}
xmlBuildings.Save("Buildings.xml");
}
}
public class Building
{
private List<Map> _maps { get; set; }
public string Name { get; }
public Building(string buildingName)
{
_maps = new List<Map>();
Name = buildingName;
}
public List<Map> GetMaps()
{
return _maps;
}
private Map IsExisted(string mapName)
{
foreach (Map map in _maps)
{
if (map.Name == mapName)
{
return map;
}
}
return null;
}
public void AddMap(string mapName, string mapPath)
{
Map thisMap = IsExisted(mapName);
if (thisMap == null)
{
thisMap = new Map(mapName);
_maps.Add(thisMap);
}
thisMap.ChangePath(mapPath);
}
public void DeleteMap(string mapName)
{
}
public XmlElement BuildingXml(XmlDocument document)
{
XmlElement building = document.CreateElement("Building");
building.SetAttribute("Name", this.Name);
foreach (Map map in _maps)
{
building.AppendChild(map.MapXml(document));
}
return building;
}
}
public class Map
{
public string Name { get; }
public string Mode { get; set; }
private string _path { get; set; }
private Image _image { get; set; }
public List<Beacon> Beacons { get; }
public Map(string name)
{
Beacons = new List<Beacon>();
Name = name;
}
public void ChangePath(string path)
{
_path = path;
}
public XmlElement MapXml(XmlDocument document)
{
XmlElement map = document.CreateElement("Map");
map.SetAttribute("Name", this.Name);
map.SetAttribute("Path", this._path);
return map;
}
}
}