-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathfrmOISMRUList.cs
332 lines (312 loc) · 12.7 KB
/
frmOISMRUList.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
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
/// +------------------------------------------------------------------------------------------------------------------------------+
/// ¦ TERMS OF USE: MIT License ¦
/// +------------------------------------------------------------------------------------------------------------------------------¦
/// ¦Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation ¦
/// ¦files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, ¦
/// ¦modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software¦
/// ¦is furnished to do so, subject to the following conditions: ¦
/// ¦ ¦
/// ¦The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.¦
/// ¦ ¦
/// ¦THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE ¦
/// ¦WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR ¦
/// ¦COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ¦
/// ¦ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ¦
/// +------------------------------------------------------------------------------------------------------------------------------+
/// #########
/// Note: the three letter "OIS" prefix used here is an acronym for "OfItselfSo.com" this softwares home website.
/// #########
///
namespace OISCommon
{
/// +=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=
/// +=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=
/// <summary>
/// Provides Most Recently Used File (MRU) List functionality
/// </summary>
/// <history>
/// 08 Aug 10 Cynic - Started
/// </history>
public partial class frmOISMRUList : frmOISBase
{
// this is the most we keep
public const int DEFAULT_MAXMRU_VALUES = 25;
private int maxMRUValues = DEFAULT_MAXMRU_VALUES;
public const string DEFAULT_NOFILES_NOTICE = "No files to display...";
/// +=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=
/// <summary>
/// Constructor
/// </summary>
/// <history>
/// 08 Aug 10 Cynic - Started
/// </history>
public frmOISMRUList()
{
InitializeComponent();
// set the default "no files" notice
this.listBoxFileList.Items.AddRange(new object[] {DEFAULT_NOFILES_NOTICE});
}
/// +=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=
/// <summary>
/// Gets/Sets the Max number of MRU values we accept. Will never accept
/// a negative number.
/// </summary>
/// <history>
/// 08 Aug 10 Cynic - Started
/// </history>
public int MaxMRUValues
{
get
{
if (maxMRUValues < 0) maxMRUValues = DEFAULT_MAXMRU_VALUES;
return maxMRUValues;
}
set
{
maxMRUValues = value;
if (maxMRUValues < 0) maxMRUValues = DEFAULT_MAXMRU_VALUES;
}
}
/// +=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=
/// <summary>
/// We are only permitted to keep maxMRUValues here. So we trim it as
/// necessary
/// </summary>
/// <history>
/// 08 Aug 10 Cynic - Started
/// </history>
private void TrimFileListToMRUMaxValues()
{
// first some checks
if (this.listBoxFileList.Items.Count == 0) return;
int numToTrim = this.listBoxFileList.Items.Count-MaxMRUValues;
// do we have to do anything?
if (numToTrim <= 0) return;
// yes we do, get the list
List<string> tmpList = FileList;
// drop them off the bottom
for (int index = FileList.Count - 1; index > FileList.Count - numToTrim; index--)
{
tmpList.RemoveAt(index);
}
// put it back
listBoxFileList.Items.Clear();
listBoxFileList.Items.AddRange(tmpList.ToArray());
}
/// +=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=
/// <summary>
/// Gets/Sets the filelist as a generic list of strings
/// </summary>
/// <history>
/// 08 Aug 10 Cynic - Started
/// </history>
public List<string> FileList
{
set
{
if (value == null) return;
listBoxFileList.Items.Clear();
listBoxFileList.Items.AddRange(value.ToArray());
TrimFileListToMRUMaxValues();
}
get
{
List<string> tmpList = new List<string>();
foreach(object strObj in listBoxFileList.Items)
{
if((strObj is string) == false) continue;
tmpList.Add((strObj as string));
}
return tmpList;
}
}
/// +=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=
/// <summary>
/// Adds a filename to the top of the list
/// </summary>
/// <history>
/// 08 Aug 10 Cynic - Started
/// </history>
public void AddFileNameToTop(string fileName)
{
int existingIndex;
// sanity check
if ((fileName == null) || (fileName.Length == 0)) return;
// get the list
List<string> tmpList = FileList;
// note the lambda here, way cool way to represent an anonymous method with an
// inline delegate. Yummy!
existingIndex = tmpList.FindIndex(s => s == fileName);
// did we find one?
if (existingIndex >= 0)
{
// yes we did, remove it then re-insert it
tmpList.RemoveAt(existingIndex);
}
// be sure to remove the DEFAULT_NOFILES_NOTICE also
existingIndex = tmpList.FindIndex(s => s == DEFAULT_NOFILES_NOTICE);
// did we find one?
if (existingIndex >= 0)
{
// yes we did, remove it then re-insert it
tmpList.RemoveAt(existingIndex);
}
// insert filename right at the top now
tmpList.Insert(0,fileName);
// put it back now
FileList = tmpList;
// make sure this is in sync
SyncSelectedFileName();
}
/// +=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=
/// <summary>
/// Removes a file from the list
/// </summary>
/// <history>
/// 08 Aug 10 Cynic - Started
/// </history>
public void RemoveFileName(string fileName)
{
// sanity check
if ((fileName == null) || (fileName.Length == 0)) return;
// get the list
List<string> tmpList = FileList;
// note the lambda here, way cool way to represent an anonymous method with an
// inline delegate. Yummy!
int existingIndex = tmpList.FindIndex(s => s == fileName);
// did we find one?
if (existingIndex <= 0)
{
// no we did not - no need to do anything
return;
}
// yes we did, remove it
tmpList.RemoveAt(existingIndex);
// do we have no file left?
if (tmpList.Count == 0)
{
// yes, we removed the last file, Put this in here
tmpList.Add(DEFAULT_NOFILES_NOTICE);
}
// put the list back now
FileList = tmpList;
}
/// +=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=
/// <summary>
/// Handles a click on the OK button.
/// </summary>
/// <history>
/// 08 Aug 10 Cynic - Started
/// </history>
private void buttonOk_Click(object sender, EventArgs e)
{
if ((SelectedFileName == null) || (SelectedFileName.Length == 0))
{
// just assume a cancel
DialogResult = DialogResult.Cancel;
}
else DialogResult = DialogResult.OK;
}
/// +=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=
/// <summary>
/// Handles a click on the Cancel button.
/// </summary>
/// <history>
/// 08 Aug 10 Cynic - Started
/// </history>
private void buttonCancel_Click(object sender, EventArgs e)
{
DialogResult = DialogResult.Cancel;
}
/// +=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=
/// <summary>
/// handles changes to the selected file
/// </summary>
/// <history>
/// 08 Aug 10 Cynic - Started
/// </history>
private void listBoxFileList_SelectedIndexChanged(object sender, EventArgs e)
{
SyncSelectedFileName();
}
/// +=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=
/// <summary>
/// Synchronized the selected filename
/// </summary>
/// <history>
/// 08 Aug 10 Cynic - Started
/// </history>
private void SyncSelectedFileName()
{
if (listBoxFileList.SelectedItem == null)
{
textBoxChosenFile.Text = "";
this.buttonOk.Enabled = false;
}
else
{
this.buttonOk.Enabled = true;
this.textBoxChosenFile.Text = listBoxFileList.SelectedItem.ToString();
}
// never let the DEFAULT_NOFILES_NOTICE go in this box
if (textBoxChosenFile.Text == DEFAULT_NOFILES_NOTICE)
{
textBoxChosenFile.Text = "";
this.buttonOk.Enabled = false;
}
}
/// +=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=
/// <summary>
/// Returns the selected file name. Will never return null
/// </summary>
/// <history>
/// 08 Aug 10 Cynic - Started
/// </history>
public string SelectedFileName
{
get
{
if (textBoxChosenFile.Text == null) return "";
return textBoxChosenFile.Text;
}
}
/// +=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=
/// <summary>
/// handles the form shown event
/// </summary>
/// <history>
/// 08 Aug 10 Cynic - Started
/// </history>
private void frmOISMRUList_Shown(object sender, EventArgs e)
{
SyncSelectedFileName();
}
/// +=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=
/// <summary>
/// handles a double click on a filename
/// </summary>
/// <history>
/// 31 Aug 10 Cynic - Started
/// </history>
void listBoxFileList_MouseDoubleClick(object sender, MouseEventArgs e)
{
// did we click on an item
int index = listBoxFileList.IndexFromPoint(e.X, e.Y);
if(index < 0) return;
SyncSelectedFileName();
// do we have selected filename, check this?
if(buttonOk.Enabled==true)
{
// yes we do, simulate an ok click
buttonOk_Click(this, new EventArgs());
}
}
}
}