-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathForm1.vb
More file actions
575 lines (504 loc) · 19.7 KB
/
Form1.vb
File metadata and controls
575 lines (504 loc) · 19.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
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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
Imports System.ComponentModel
Imports System.ComponentModel.Design
Imports System.Drawing.Imaging.Effects
Imports System.IO
Imports System.Net.NetworkInformation
Imports System.Runtime.CompilerServices
Imports System.Runtime.Serialization
Imports System.Security.Policy
Imports System.Text.RegularExpressions
Imports System.Threading
Public Class Form1
Private cardDeck As New List(Of String)
Private cardPos As Integer = 0
Private currentIndex As Integer = 0
Private programCardIndex As Integer = -1
Private Const MaxColumns As Integer = 80
Private Const ProgramCardPrefix As String = "#PROGRAM:"
Private lastOpenedFile As String = String.Empty
Private ReaderList As New List(Of Reader)
Private PunchList As New List(Of Punch)
Private Function GetReaderConfig() As List(Of String)
Dim rlist As New List(Of String)
If Not File.Exists("readers.cfg") Then
Dim fs As FileStream = File.Create("readers.cfg")
fs.Close()
End If
Using tr As New StreamReader("readers.cfg")
While Not tr.EndOfStream
rlist.Add(tr.ReadLine)
End While
End Using
Return rlist
End Function
Private Function GetPunchConfig() As List(Of String)
Dim rlist As New List(Of String)
If Not File.Exists("punches.cfg") Then
Dim fs As FileStream = File.Create("punches.cfg")
fs.Close()
End If
Using tr As New StreamReader("punches.cfg")
rlist.Add(tr.ReadLine)
End Using
Return rlist
End Function
Private Function ResetPunches(sl As List(Of String)) As List(Of Punch)
Dim newPunches As New List(Of Punch)
If sl.Count = 0 Then Return newPunches
For Each s As String In sl
If s IsNot Nothing Then
Dim myName As String = ""
Dim myConnection As String = ""
Dim sp As String() = s.Split("=")
myName = sp(0)
myConnection = sp(1)
Dim thisPunch As New Punch
thisPunch.Name = myName
thisPunch.ConnectionString = myConnection
newPunches.Add(thisPunch)
End If
Next
Return newPunches
End Function
Private Function ResetReaders(sl As List(Of String)) As List(Of Reader)
Dim newReaders As New List(Of Reader)
For Each s As String In sl
Dim myName As String = ""
Dim myConnection As String = ""
Dim myDefault As Boolean = False
Dim sp As String() = s.Split("=")
myName = sp(0)
myConnection = sp(1)
If sp(2) = "Y" Then
myDefault = True
Else
myDefault = False
End If
Dim thisReader As New Reader
thisReader.Name = myName
thisReader.ConnectionString = myConnection
thisReader.isDefault = myDefault
newReaders.Add(thisReader)
Next
Return newReaders
End Function
Private Sub SaveConfig()
Dim sl As New List(Of String)
For Each r As Reader In ReaderList
Dim IsDefault As String = "N"
If r.isDefault Then IsDefault = "Y" Else IsDefault = "N"
sl.Add($"{r.Name}={r.ConnectionString}={IsDefault}")
Next
Using tr As New StreamWriter("readers.cfg", False)
For Each s As String In sl
tr.WriteLine(s)
Next
End Using
sl.Clear()
For Each p As Punch In PunchList
sl.Add($"{p.Name}={p.ConnectionString}")
Next
Using tr As New StreamWriter("punches.cfg", False)
For Each s As String In sl
tr.WriteLine(s)
Next
End Using
End Sub
Private Sub KeypunchForm_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim RString As List(Of String) = GetReaderConfig()
ReaderList = ResetReaders(RString)
PopulateReaders()
Dim PString As List(Of String) = GetPunchConfig()
PunchList = ResetPunches(PString)
'Stop
ActivatePunches()
txtCard.MaxLength = MaxColumns
Try
Dim fontPath = Path.Combine(Application.StartupPath, "fonts", "Line Printer.ttf")
If File.Exists(fontPath) Then
Dim pfc As New Drawing.Text.PrivateFontCollection()
pfc.AddFontFile(fontPath)
ListBox1.Font = New Font(pfc.Families(0), 12)
Else
ListBox1.Font = New Font("Consolas", 10)
End If
Catch
ListBox1.Font = New Font("Consolas", 10)
End Try
sysTimer.Start()
UpdateDisplay()
btnNew.PerformClick()
End Sub
Private Sub btnNew_Click(sender As Object, e As EventArgs) Handles btnNew.Click
cardDeck.Clear()
cardDeck.Add(New String(""))
currentIndex = 0
programCardIndex = -1
lastOpenedFile = String.Empty
ListBox1.Items.Clear()
UpdateDisplay()
End Sub
Private Sub btnPrev_Click(sender As Object, e As EventArgs) Handles btnPrev.Click
If currentIndex > 0 Then
SaveCurrentCard()
currentIndex -= 1
UpdateDisplay()
End If
End Sub
Private Sub btnNext_Click(sender As Object, e As EventArgs) Handles btnNext.Click
SaveCurrentCard()
If currentIndex < cardDeck.Count - 1 Then
currentIndex += 1
Else
cardDeck.Add(New String(""))
currentIndex = cardDeck.Count - 1
End If
UpdateDisplay()
End Sub
Private Sub btnInsert_Click(sender As Object, e As EventArgs) Handles btnInsert.Click
SaveCurrentCard()
cardDeck.Insert(currentIndex, New String(""))
ListBox1.Items.Insert(currentIndex, New String(""))
UpdateDisplay()
End Sub
Private Sub btnDelete_Click(sender As Object, e As EventArgs) Handles btnDelete.Click
If cardDeck.Count > 1 Then
cardDeck.RemoveAt(currentIndex)
ListBox1.Items.RemoveAt(currentIndex)
If currentIndex >= cardDeck.Count Then currentIndex = cardDeck.Count - 1
UpdateDisplay()
End If
End Sub
Private Sub btnDuplicate_Click(sender As Object, e As EventArgs) Handles btnDup.Click
SaveCurrentCard()
cardDeck.Insert(currentIndex + 1, cardDeck(currentIndex))
ListBox1.Items.Insert(currentIndex + 1, cardDeck(currentIndex))
currentIndex += 1
UpdateDisplay()
End Sub
Private Sub SaveCurrentCard()
If currentIndex >= 0 AndAlso currentIndex < cardDeck.Count Then
Dim text As String = txtCard.Text
cardDeck(currentIndex) = text
Dim lbCount As Integer = ListBox1.Items.Count - 1
If currentIndex > lbCount Then
ListBox1.Items.Add(text)
Else
ListBox1.Items(currentIndex) = text
End If
End If
End Sub
Private Sub UpdateDisplay()
If currentIndex >= 0 AndAlso currentIndex < cardDeck.Count Then
Dim cardText = cardDeck(currentIndex)
If cardText.StartsWith(ProgramCardPrefix) Then
Else
txtCard.Text = cardText.TrimEnd
lblStatus.Text = $"Card {currentIndex + 1} / {cardDeck.Count}"
If (ListBox1.Items.Count - 1) >= currentIndex Then
ListBox1.SelectedIndex = currentIndex
Else
ListBox1.Items.Add("")
ListBox1.SelectedIndex = currentIndex
End If
End If
End If
End Sub
Private Sub btnSave_Click(sender As Object, e As EventArgs) Handles btnSave.Click
SaveCurrentCard()
If String.IsNullOrEmpty(lastOpenedFile) Then
btnSaveAs.PerformClick()
Else
File.WriteAllLines(lastOpenedFile, cardDeck)
Beep()
End If
End Sub
Private Sub btnSaveAs_Click(sender As Object, e As EventArgs) Handles btnSaveAs.Click
SaveCurrentCard()
Using sfd As New SaveFileDialog
sfd.Filter = "JCL Files (*.jcl)|*.jcl|Text Files (*.txt)|*.txt"
If sfd.ShowDialog = DialogResult.OK Then
File.WriteAllLines(sfd.FileName, cardDeck)
lastOpenedFile = sfd.FileName
End If
End Using
Beep()
End Sub
Private Sub btnOpen_Click(sender As Object, e As EventArgs) Handles btnOpen.Click
Using ofd As New OpenFileDialog
ofd.Filter = "JCL Files (*.jcl)|*.jcl|Text Files (*.txt)|*.txt"
If ofd.ShowDialog = DialogResult.OK Then
' Set current directory to the file's folder
Environment.CurrentDirectory = Path.GetDirectoryName(ofd.FileName)
Dim lines = File.ReadAllLines(ofd.FileName).ToList
cardDeck = lines.Select(Function(l) l).ToList
currentIndex = 0
lastOpenedFile = ofd.FileName
UpdateDisplay()
ListBox1.Items.Clear()
For Each s In cardDeck
ListBox1.Items.Add(s)
ListBox1.SelectedIndex = 0
Application.DoEvents()
Next
End If
End Using
End Sub
Private Sub txtCard_KeyPress(sender As Object, e As KeyPressEventArgs) Handles txtCard.KeyPress
Dim thisKey As Char = e.KeyChar
If capslock.Checked Then
thisKey = Char.ToUpper(thisKey)
End If
e.KeyChar = thisKey
cardPos = txtCard.SelectionStart
End Sub
Private Sub TextBox1_KeyDown(sender As Object, e As KeyEventArgs) Handles txtCard.KeyDown
If e.KeyCode = Keys.Tab Then
e.SuppressKeyPress = True ' prevent focus change
Dim tb As TextBox = CType(sender, TextBox)
Dim pos As Integer = tb.SelectionStart
' Calculate next 10-character boundary
Dim nextTabStop As Integer = ((pos \ 10) + 1) * 10
Dim spacesNeeded As Integer = nextTabStop - pos
' Check how many spaces are needed that don't overwrite existing text
Dim insertSpaces As String = ""
For i As Integer = 0 To spacesNeeded - 1
Dim index As Integer = pos + i
If index >= tb.TextLength OrElse tb.Text(index) = " "c Then
insertSpaces &= " "
Else
Exit For ' Stop if non-space character would be overwritten
End If
Next
' Insert the calculated number of safe spaces
tb.Text = tb.Text.Insert(pos, insertSpaces)
tb.SelectionStart = pos + insertSpaces.Length
End If
End Sub
Private Sub sysTimer_Tick(sender As Object, e As EventArgs) Handles sysTimer.Tick
Dim currentDeck As String = ""
posDisplay.Text = (txtCard.SelectionStart + 1)
If lastOpenedFile = "" Then
currentDeck = "New deck"
Else
currentDeck = lastOpenedFile
End If
Dim title As String = $"IBM 029 Simulator ({currentDeck})"
Me.Text = title
sysTimer.Start()
End Sub
Private Sub ListBox1_DoubleClick(sender As Object, e As EventArgs) Handles ListBox1.DoubleClick
Dim newIndex As Integer = ListBox1.SelectedIndex
currentIndex = newIndex
txtCard.Text = cardDeck(currentIndex)
UpdateDisplay()
End Sub
Private Sub ListBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ListBox1.SelectedIndexChanged
If (currentIndex < 0) Or (ListBox1.SelectedIndex < 0) Then Exit Sub
If ListBox1.SelectedIndex <> currentIndex Then
currentIndex = ListBox1.SelectedIndex
txtCard.Text = cardDeck(currentIndex)
UpdateDisplay()
End If
End Sub
Private Sub ReadersToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles ReadersToolStripMenuItem.Click
Dim rf As New ReaderForm
rf.Readers = ReaderList
rf.ShowDialog()
ReaderList = rf.Readers
SaveConfig()
PopulateReaders()
End Sub
Private Sub PopulateReaders()
deviceSelect.Items.Clear()
For Each r As Reader In ReaderList
deviceSelect.Items.Add(r.Name)
If r.isDefault Then
Dim thisIdx As Integer = deviceSelect.Items.IndexOf(r.Name)
deviceSelect.SelectedIndex = thisIdx
End If
Next
End Sub
Private Sub ActivatePunches()
For Each p As Punch In PunchList
p.Connect()
'Stop
Next
End Sub
Private Sub FakeButton_Click(sender As Object, e As EventArgs) Handles FakeButton.Click
btnNext.PerformClick()
End Sub
Private Function ProcessDeck(inDeck As List(Of String)) As List(Of String)
Dim outDeck As New List(Of String)
For Each line As String In inDeck
Dim trimmedLine As String = line.Trim()
If trimmedLine.StartsWith("#include ", StringComparison.OrdinalIgnoreCase) Then
Dim includePath As String = trimmedLine.Substring(9).Trim()
If File.Exists(includePath) Then
Try
Dim includedLines As List(Of String) =
ProcessDeck(File.ReadAllLines(includePath).ToList())
outDeck.AddRange(includedLines)
Catch ex As Exception
Debug.WriteLine($"Error including file '{includePath}': {ex.Message}")
End Try
Else
Debug.WriteLine($"Include file not found: {includePath}")
' Optionally: outDeck.Add(line)
End If
Else
outDeck.Add(line)
End If
Next
Return outDeck
End Function
Private Function ShowProcessedDeck(processedDeck As List(Of String)) As DialogResult
Dim outputForm As New Form With {
.Text = "Processed Deck Output",
.Width = 800,
.Height = 600,
.StartPosition = FormStartPosition.CenterParent,
.FormBorderStyle = FormBorderStyle.FixedDialog,
.MinimizeBox = False,
.MaximizeBox = False
}
' Output TextBox
Dim txtOutput As New TextBox With {
.Multiline = True,
.ScrollBars = ScrollBars.Both,
.Dock = DockStyle.Fill,
.Font = New Font("Consolas", 10),
.ReadOnly = True,
.WordWrap = False
}
txtOutput.Lines = processedDeck.ToArray()
' Button panel
Dim buttonPanel As New Panel With {
.Dock = DockStyle.Bottom,
.Height = 50
}
' Submit Button
Dim btnSubmit As New Button With {
.Text = "Submit",
.DialogResult = DialogResult.OK,
.Width = 100,
.Height = 30,
.Top = 10,
.Left = 560
}
' Cancel Button
Dim btnCancel As New Button With {
.Text = "Cancel",
.DialogResult = DialogResult.Cancel,
.Width = 100,
.Height = 30,
.Top = 10,
.Left = 670
}
' Add buttons to panel
buttonPanel.Controls.Add(btnSubmit)
buttonPanel.Controls.Add(btnCancel)
' Add controls to form
outputForm.Controls.Add(txtOutput)
outputForm.Controls.Add(buttonPanel)
' Default buttons
outputForm.AcceptButton = btnSubmit
outputForm.CancelButton = btnCancel
Return outputForm.ShowDialog(Me)
End Function
Private Sub submitButton_Click(sender As Object, e As EventArgs) Handles submitButton.Click
If cardDeck.Count = 1 Then
If cardDeck.Item(0).Trim = "" Then
MsgBox($"There are no cards in this deck.", MsgBoxStyle.OkOnly Or MsgBoxStyle.Critical, "Error")
Exit Sub
End If
End If
Dim outDeck As List(Of String) = ProcessDeck(cardDeck)
Dim okToRun As DialogResult = ShowProcessedDeck(outDeck)
If okToRun = DialogResult.OK Then
Dim thisDevice As String = deviceSelect.Text
Dim devList As IEnumerable(Of Reader)
devList = (From r As Reader In ReaderList Where r.Name.Trim = thisDevice.Trim Select r)
Dim thisReader = devList.FirstOrDefault
thisReader.Submit(outDeck)
MsgBox($"Deck sent to reader.", MsgBoxStyle.OkOnly Or MsgBoxStyle.Information, "Submit")
Else
MsgBox($"Submit cancelled.", MsgBoxStyle.OkOnly Or MsgBoxStyle.Critical, "Cancel job")
End If
End Sub
Private Sub btnFirst_Click(sender As Object, e As EventArgs) Handles btnFirst.Click
ListBox1.SelectedIndex = 0
End Sub
Private Sub btnLast_Click(sender As Object, e As EventArgs) Handles btnLast.Click
ListBox1.SelectedIndex = ListBox1.Items.Count - 1
End Sub
Private Sub PunchesToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles PunchesToolStripMenuItem.Click
Dim txt As String = $"Using this option will stop any punches" & vbCrLf & $"that are already defined." & vbCrLf &
$"Are you sure you want to do this?"
Dim repl As New DialogResult
repl = MsgBox(txt, MsgBoxStyle.YesNo Or MsgBoxStyle.Question, "Punch")
If repl = DialogResult.No Then
Exit Sub
End If
Dim pf As New PunchForm
pf.Punches = PunchList
pf.ShowDialog()
PunchList = pf.Punches
SaveConfig()
ActivatePunches()
End Sub
Private Sub ToolStripMenuItem3_Click(sender As Object, e As EventArgs) Handles ToolStripMenuItem3.Click
If txtCard.Text.Trim <> "" Then
' The card is not blank.
Dim repl As New DialogResult
repl = MsgBox("This card is not empty. Do you want to replace it with the include directive?",
MsgBoxStyle.YesNo Or MsgBoxStyle.Question, "Include")
If repl = DialogResult.No Then Exit Sub
End If
Dim ib As New includeBrowse
ib.ShowDialog()
If ib.DialogResult = DialogResult.OK Then
If ib.FileBrowserControl1.SelectedFile.Trim = "" Then
MsgBox($"No file chosen")
Else
MsgBox($"File {ib.FileBrowserControl1.SelectedFile}")
End If
End If
txtCard.Text = $"#INCLUDE {ib.SelectedFile}"
End Sub
End Class
Public Class TabAwareTextBox
Inherits TextBox
Public Sub New()
Me.AcceptsTab = True
Me.Multiline = True
Me.Font = New Font("Consolas", 10) ' Monospaced font helps align tabs
End Sub
Protected Overrides Function ProcessCmdKey(ByRef msg As Message, keyData As Keys) As Boolean
If keyData = Keys.Tab Then
HandleTabKey()
Return True ' Suppress default Tab behavior
End If
Return MyBase.ProcessCmdKey(msg, keyData)
End Function
Private Sub HandleTabKey()
Dim pos As Integer = Me.SelectionStart
Dim text = Me.Text
Dim nextTabStop As Integer = ((pos \ 5) + 1) * 5
Dim spacesNeeded As Integer = nextTabStop - pos
' Only insert spaces that won't overwrite non-space characters
Dim insertSpaces As New Text.StringBuilder()
For i As Integer = 0 To spacesNeeded - 1
Dim index As Integer = pos + i
If index >= text.Length OrElse text(index) = " "c Then
insertSpaces.Append(" "c)
Else
Exit For
End If
Next
If insertSpaces.Length > 0 Then
Me.Text = text.Insert(pos, insertSpaces.ToString())
Me.SelectionStart = pos + insertSpaces.Length
End If
End Sub
End Class