-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathForm1.cs
More file actions
200 lines (168 loc) · 5.7 KB
/
Form1.cs
File metadata and controls
200 lines (168 loc) · 5.7 KB
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
using System;
using System.Diagnostics;
using System.IO;
using System.Windows.Forms;
using Kajabity.Tools.Java;
namespace JavaPropertiesDemo
{
public partial class Form1 : Form
{
private readonly JavaProperties _properties = new JavaProperties();
private bool _changed;
private string _filename;
public Form1()
{
InitializeComponent();
}
private void newToolStripMenuItem_Click(object sender, EventArgs e)
{
CloseDocument(sender, e);
}
private void openToolStripMenuItem_Click(object sender, EventArgs e)
{
var dialog = new OpenFileDialog
{
DefaultExt = @"properties",
Title = @"Open Java Properties file",
Filter = @"Java Properties files (*.properties)|*.properties|All files (*.*)|*.*",
AddExtension = true,
CheckFileExists = true,
CheckPathExists = true,
ReadOnlyChecked = false
};
if (dialog.ShowDialog(this) == DialogResult.OK && CloseDocument(sender, e))
{
Stream stream = new FileStream(dialog.FileName, FileMode.Open);
_properties.Load(stream);
stream.Close();
UpdateDisplay();
}
}
private void saveToolStripMenuItem_Click(object sender, EventArgs e)
{
if (_filename is null)
{
saveAsToolStripMenuItem_Click(sender, e);
}
else
{
SaveDocument();
}
}
private void saveAsToolStripMenuItem_Click(object sender, EventArgs e)
{
var dialog = new SaveFileDialog
{
DefaultExt = @"properties",
Title = @"Save Java Properties file",
Filter = @"Java Properties files (*.properties)|*.properties|All files (*.*)|*.*",
FileName = _filename
};
if (dialog.ShowDialog(this) == DialogResult.OK)
{
_filename = dialog.FileName;
SaveDocument();
}
}
private void SaveDocument()
{
Stream stream = new FileStream(_filename, FileMode.Create);
_properties.Store(stream, true);
stream.Close();
_changed = false;
}
private void exitToolStripMenuItem_Click(object sender, EventArgs e)
{
Application.Exit();
}
private bool CloseDocument(object sender, EventArgs e)
{
if (_changed)
{
var result = MessageBox.Show(this,
@"Java Properties file " + _filename + @" has been modified!\n\nDo you want to save it?",
@"Java Properties Demo",
MessageBoxButtons.YesNoCancel,
MessageBoxIcon.Exclamation);
if (result == DialogResult.Yes)
{
saveToolStripMenuItem_Click(sender, e);
}
else if (result == DialogResult.Cancel)
{
return false;
}
}
_properties.Clear();
_changed = false;
_filename = null;
return true;
}
private void UpdateDisplay()
{
// Clear the list.
listView1.Items.Clear();
// Add the entries to the ListView.
foreach (var key in _properties.Keys)
{
var item = new ListViewItem(key);
var text = _properties.GetProperty(key);
item.SubItems.Add(new ListViewItem.ListViewSubItem(item, text));
listView1.Items.Add(item);
}
// Force a display update.
Refresh();
}
private void listView1_ItemActivate(object sender, EventArgs e)
{
if (listView1.SelectedItems.Count > 0)
{
var item = listView1.SelectedItems[0];
var dialog = new Form2
{
Text = @"Edit Entry",
EntryName = item.Text,
EntryValue = item.SubItems[1].Text
};
if (dialog.ShowDialog(this) == DialogResult.OK)
{
_properties.Remove(item.Text);
_properties.SetProperty(dialog.EntryName, dialog.EntryValue);
_changed = true;
UpdateDisplay();
}
}
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
if (!CloseDocument(sender, e))
{
e.Cancel = true;
}
}
private void insertToolStripMenuItem_Click(object sender, EventArgs e)
{
var dialog = new Form2
{
Text = @"Add Entry"
};
if (dialog.ShowDialog(this) == DialogResult.OK)
{
_properties.SetProperty(dialog.EntryName, dialog.EntryValue);
_changed = true;
UpdateDisplay();
}
}
private void goToArticleToolStripMenuItem_Click(object sender, EventArgs e)
{
Process.Start("https://www.kajabity.com");
}
private void deleteToolStripMenuItem_Click(object sender, EventArgs e)
{
}
private void listView1_SelectedIndexChanged(object sender, EventArgs e)
{
deleteToolStripMenuItem.Enabled = listView1.SelectedIndices.Count > 0;
}
}
}