This repository was archived by the owner on May 10, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathfrmDBEditor.cs
More file actions
256 lines (232 loc) · 9.09 KB
/
Copy pathfrmDBEditor.cs
File metadata and controls
256 lines (232 loc) · 9.09 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
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
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Note_Profiler
{
/// <summary>
/// An editor for basic database tables such as "Note Type" and "Author".
/// </summary>
public partial class frmDBEditor : Form
{
DBAdapter db;
DataGridViewCellStyle cellStyleModified = new DataGridViewCellStyle();
DataGridViewCellStyle cellStyleNew = new DataGridViewCellStyle();
public enum EditType { Author, NoteType };
EditType DBType;
/// <summary>
/// Init the interface.
/// </summary>
/// <param name="dbAdapter">The DB adapter to connect to (passed from the main form).</param>
/// <param name="type">The type of DB to display (Author or NoteType)</param>
public frmDBEditor(DBAdapter dbAdapter, EditType type)
{
InitializeComponent();
db = dbAdapter;
DBType = type;
cellStyleModified.Font = new Font(gridviewDB.Font, FontStyle.Bold);
cellStyleModified.ForeColor = Color.Blue;
cellStyleNew.Font = new Font(gridviewDB.Font, FontStyle.Bold);
cellStyleNew.ForeColor = Color.Green;
}
/// <summary>
/// Loads the appropriate database and sets the correct labels.
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void frmManageAuthors_Load(object sender, EventArgs e)
{
switch (DBType)
{
case EditType.Author:
gridviewDB.DataSource = db.AuthorsTable;
Text = "Manage Authors";
grpDataGridView.Text = "Authors Database:";
break;
case EditType.NoteType:
gridviewDB.DataSource = db.NoteTypeTable;
Text = "Manage Note Types";
grpDataGridView.Text = "Note Type Database:";
break;
}
InvalidateGridview();
chkID.Checked = Properties.Settings.Default.ShowID;
chkSort.Checked = Properties.Settings.Default.ShowSort;
}
#region Interface
#region Buttons
private void btnMoveUp_Click(object sender, EventArgs e)
{
MoveRows(true);
}
private void btnMoveDown_Click(object sender, EventArgs e)
{
MoveRows(false);
}
/// <summary>
/// Revert the DB to the last saved version.
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnRevert_Click(object sender, EventArgs e)
{
DialogResult yesno = MessageBox.Show(this, "This will undo all your changes!\nAre you sure?", "Are you sure?", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
if (yesno == DialogResult.Yes)
{
db.ReloadAll();
}
}
private void btnApply_Click(object sender, EventArgs e)
{
SaveChanges();
}
#endregion
#region GridView
/// <summary>
/// Enables or Disables the "up/down" buttons based on how many items are selected.
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void gridviewDB_SelectionChanged(object sender, EventArgs e)
{
if (gridviewDB.SelectedRows.Count > 0)
{
btnMoveDown.Enabled = gridviewDB.SelectedRows[gridviewDB.SelectedRows.Count - 1].Index < gridviewDB.RowCount - 2;
btnMoveUp.Enabled = gridviewDB.SelectedRows[gridviewDB.SelectedRows.Count - 1].Index > 0;
}
else
{
btnMoveDown.Enabled = false;
btnMoveUp.Enabled = false;
}
}
/// <summary>
/// Formats the DB table.
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void gridviewDB_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
{
gridviewDB.Columns[0].AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;
gridviewDB.Columns[0].ReadOnly = true;
gridviewDB.Columns[1].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
}
/// <summary>
/// Fills the ID and Sort field automatically when creating a new row
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void gridviewDB_RowEnter(object sender, DataGridViewCellEventArgs e)
{
if (e.RowIndex == gridviewDB.NewRowIndex)
{
int nextID = DBType == EditType.Author ? db.NextAuthor : db.NextNoteType;
if (nextID == -1)
nextID = gridviewDB.Rows.Count;
gridviewDB.Rows[e.RowIndex].Cells[0].Value = nextID;
if (DBType == EditType.NoteType)
{
gridviewDB.Rows[e.RowIndex].Cells["Sort"].Value = nextID;
}
}
}
private void gridviewDB_UserAddedRow(object sender, DataGridViewRowEventArgs e)
{
e.Row.DefaultCellStyle = cellStyleModified;
}
private void gridviewDB_CellEndEdit(object sender, DataGridViewCellEventArgs e)
{
gridviewDB.Rows[e.RowIndex].DefaultCellStyle = cellStyleModified;
}
#endregion
#region Checkboxes
/// <summary>
/// Save checkbox states to the settings for cross-session consistency.
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void chkSort_CheckedChanged(object sender, EventArgs e)
{
Properties.Settings.Default.ShowSort = chkSort.Checked;
Properties.Settings.Default.Save();
InvalidateGridview();
}
/// <summary>
/// Save checkbox states to the settings for cross-session consistency.
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void chkID_CheckedChanged(object sender, EventArgs e)
{
Properties.Settings.Default.ShowID = chkID.Checked;
Properties.Settings.Default.Save();
InvalidateGridview();
}
#endregion
/// <summary>
/// Loads the Sort and ID fields based on the settings.
/// </summary>
private void InvalidateGridview()
{
gridviewDB.Columns["Id"].Visible = Properties.Settings.Default.ShowID;
gridviewDB.Columns["Sort"].Visible = Properties.Settings.Default.ShowSort;
}
#endregion
private void SaveChanges()
{
DialogResult yesno = MessageBox.Show(this, "Once saved, changes can not be undone!\nAre you sure?", "Are you sure?", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
if (yesno == DialogResult.Yes)
{
db.SaveAll();
db.ReloadAll();
}
((frmMain)Owner).InvalidateDatabase();
}
/// <summary>
/// Moves rows (changes the Sort values)
/// </summary>
/// <param name="up">True if up was clicked, false if down.</param>
private void MoveRows(bool up)
{
if (gridviewDB.SelectedRows.Count > 0)
{
List<int> indices = new List<int>();
List<int> IDs = new List<int>();
foreach (DataGridViewRow row in gridviewDB.SelectedRows)
{
indices.Add(row.Index);
}
indices.Sort();
if (!up)
indices.Reverse();
for (int i = 0; i < indices.Count; i++)
{
int first = Convert.ToInt32(gridviewDB.Rows[indices[i]].Cells["ID"].Value);
IDs.Add(first);
int second = Convert.ToInt32(gridviewDB.Rows[indices[i] + (up ? -1 : 1)].Cells["ID"].Value);
if (DBType == EditType.NoteType)
db.SwapNoteTyes(first, second);
else
db.SwapAuthorType(first, second);
}
db.SaveAll();
db.ReloadAll();
gridviewDB.ClearSelection();
gridviewDB.CurrentCell = null;
foreach (int id in IDs)
{
DataGridViewRow row = gridviewDB.Rows.Cast<DataGridViewRow>().Where(r => Convert.ToInt32(r.Cells["ID"].Value) == id).FirstOrDefault();
if (row != null)
{
row.Selected = true;
}
}
}
}
}
}