|
| 1 | +using System; |
| 2 | +using System.IO; |
| 3 | +using System.Collections; |
| 4 | +using System.Collections.Generic; |
| 5 | +using System.Text; |
| 6 | +using System.Xml; |
| 7 | +using System.Xml.Serialization; |
| 8 | +using System.Windows.Forms; |
| 9 | + |
| 10 | +namespace KMZRebuilder |
| 11 | +{ |
| 12 | + [Serializable] |
| 13 | + public class Preferences: XMLSaved<Preferences> |
| 14 | + { |
| 15 | + [XmlArray("configuration")] |
| 16 | + [XmlArrayItem("property")] |
| 17 | + public List<Property> Properties; |
| 18 | + |
| 19 | + [XmlIgnore] |
| 20 | + public string this[string name] |
| 21 | + { |
| 22 | + get |
| 23 | + { |
| 24 | + LoadDefaults(); |
| 25 | + if (Properties.Count == 0) return ""; |
| 26 | + foreach (Property prop in Properties) |
| 27 | + if (prop.name == name) |
| 28 | + return prop.value; |
| 29 | + return ""; |
| 30 | + } |
| 31 | + set |
| 32 | + { |
| 33 | + LoadDefaults(); |
| 34 | + foreach (Property prop in Properties) |
| 35 | + if (prop.name == name) |
| 36 | + { |
| 37 | + prop.value = value; |
| 38 | + return; |
| 39 | + }; |
| 40 | + Properties.Add(new Property(name, value)); |
| 41 | + } |
| 42 | + } |
| 43 | + |
| 44 | + private void LoadDefaults() |
| 45 | + { |
| 46 | + if (Properties == null) |
| 47 | + { |
| 48 | + Properties = new List<Property>(); |
| 49 | + this["gpi_localization"] = "EN"; |
| 50 | + }; |
| 51 | + } |
| 52 | + |
| 53 | + public static Preferences Load() |
| 54 | + { |
| 55 | + string fName = KMZRebuilederForm.CurrentDirectory()+@"\KMZRebuilder.config"; |
| 56 | + if (File.Exists(fName)) |
| 57 | + { |
| 58 | + try { return Preferences.Load(fName); } catch { }; |
| 59 | + }; |
| 60 | + return new Preferences(); |
| 61 | + } |
| 62 | + |
| 63 | + public void Save() |
| 64 | + { |
| 65 | + string fName = KMZRebuilederForm.CurrentDirectory()+@"\KMZRebuilder.config"; |
| 66 | + this.LoadDefaults(); |
| 67 | + try { Preferences.Save(fName, this); } catch { }; |
| 68 | + } |
| 69 | + |
| 70 | + [Serializable] |
| 71 | + public class Property |
| 72 | + { |
| 73 | + [XmlAttribute] |
| 74 | + public string name; |
| 75 | + [XmlText] |
| 76 | + public string value; |
| 77 | + |
| 78 | + public Property() { } |
| 79 | + public Property(string name) { this.name = name; } |
| 80 | + public Property(string name, string value) { this.name = name; this.value = value; } |
| 81 | + |
| 82 | + public override string ToString() |
| 83 | + { |
| 84 | + return String.Format("{0}={1}", name, value); |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + public void ShowChangeDialog() |
| 89 | + { |
| 90 | + Form form = new Form(); |
| 91 | + form.StartPosition = FormStartPosition.CenterParent; |
| 92 | + form.MinimizeBox = false; |
| 93 | + form.MaximizeBox = false; |
| 94 | + form.ShowIcon = false; |
| 95 | + form.ShowInTaskbar = false; |
| 96 | + form.Width = 400; |
| 97 | + form.Height = 380; |
| 98 | + form.Text = "Preferences"; |
| 99 | + form.FormBorderStyle = FormBorderStyle.FixedDialog; |
| 100 | + Label lab = new Label(); |
| 101 | + form.Controls.Add(lab); |
| 102 | + lab.Text = "Double click on item or press Space to edit/change value:"; |
| 103 | + lab.AutoSize = true; |
| 104 | + lab.Left = 8; |
| 105 | + lab.Top = 5; |
| 106 | + ListBox lb = new ListBox(); |
| 107 | + form.Controls.Add(lb); |
| 108 | + lb.Width = form.Width - 26; |
| 109 | + lb.Left = 10; |
| 110 | + lb.Top = 25; |
| 111 | + lb.Height = form.Height - 90; |
| 112 | + lb.BorderStyle = BorderStyle.FixedSingle; |
| 113 | + foreach (Property prop in Properties) lb.Items.Add(prop); |
| 114 | + lb.DoubleClick += (delegate(object sender, EventArgs e) { OnChangeItem(lb); }); |
| 115 | + lb.KeyPress += (delegate(object sender, KeyPressEventArgs e) { if (e.KeyChar == (char)32) OnChangeItem(lb); }); |
| 116 | + Button okbtn = new Button(); |
| 117 | + form.Controls.Add(okbtn); |
| 118 | + okbtn.Left = form.Width / 2 - okbtn.Width / 2; |
| 119 | + okbtn.Top = lb.Top + lb.Height + 6; |
| 120 | + okbtn.Text = "OK"; |
| 121 | + okbtn.Click += (delegate(object sender, EventArgs e) { form.Close(); }); |
| 122 | + form.ShowDialog(); |
| 123 | + form.Dispose(); |
| 124 | + } |
| 125 | + |
| 126 | + private void OnChangeItem(ListBox lb) |
| 127 | + { |
| 128 | + int si = lb.SelectedIndex; |
| 129 | + if (si >= 0) |
| 130 | + { |
| 131 | + Property p = (Property)lb.Items[si]; |
| 132 | + string nval = p.value; |
| 133 | + if (InputBox.Show("Edit value", p.name + ":", ref nval) == DialogResult.OK) |
| 134 | + { |
| 135 | + p.value = nval.Trim(); |
| 136 | + this[p.name] = p.value; |
| 137 | + lb.Items.RemoveAt(si); |
| 138 | + lb.Items.Insert(si, p); |
| 139 | + lb.SetSelected(si, true); |
| 140 | + }; |
| 141 | + }; |
| 142 | + } |
| 143 | + } |
| 144 | +} |
0 commit comments