-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathProjectExplorer.vb
More file actions
264 lines (213 loc) · 8.57 KB
/
ProjectExplorer.vb
File metadata and controls
264 lines (213 loc) · 8.57 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
Imports System.Collections.ObjectModel
Imports System.IO
Public Class ProjectExplorer
Inherits Explorer
Dim _projDir As String
Dim projFiles As New ObservableCollection(Of ProjFileInfo)
Public Shared CurrentProject As String
Public Event FileNameChanged(oldFileName As String, newFileName As String)
Public Event FileDeleted()
Public Property ProjectDirectory As String
Get
Return _projDir
End Get
Set()
Dim fileName = ""
If Value = "" OrElse Not (Directory.Exists(Value) OrElse File.Exists(Value)) Then
_projDir = Value
Title = "New Project Files"
projFiles.Clear()
Return
End If
If File.GetAttributes(Value) <> FileAttributes.Directory Then
fileName = Value
Value = Path.GetDirectoryName(Value)
End If
If Value.ToLower() = _projDir.ToLower() Then
If fileName <> "" Then
If Path.GetFileNameWithoutExtension(fileName).ToLower() = "global" Then
SelectItem(Path.Combine(_projDir, "Global.sb"))
Else
SelectItem(fileName)
End If
End If
Return
End If
_projDir = Value
CurrentProject = _projDir
Title = $"{GetProjectName(_projDir)} Project Files"
FreezListFiles = True
projFiles.Clear()
Dim files = Directory.GetFiles(_projDir, "*.xaml")
If files.Count > 0 Then
For Each f In files
Dim fName = Helper.GetFormNameFromXaml(f)
If fName = "" Then Continue For
projFiles.Add(New ProjFileInfo(f))
Next
End If
Dim globFile = Path.Combine(_projDir, "Global.sb")
projFiles.Add(New ProjFileInfo(globFile))
FreezListFiles = False
If fileName = "" Then
SelectedIndex = 0
Else
SelectItem(fileName)
End If
Designer.RecentDirectory = _projDir
End Set
End Property
Private Sub SelectItem(fileName As String)
Dim f = fileName.ToLower()
If SelectedFile.ToLower() = f Then Return
For i = 0 To projFiles.Count - 1
If projFiles(i).FilePath.ToLower() = f Then
FilesList.SelectedIndex = i
Return
End If
Next
If File.Exists(fileName) Then
projFiles.Add(New ProjFileInfo(fileName))
FilesList.SelectedIndex = projFiles.Count - 1
Else
FilesList.SelectedIndex = -1
End If
End Sub
Private Function GetProjectName(projDir As String) As Object
' The job of this function is to restore the normal case project name from the lower case one.
If projDir = "" Then Return "New"
Dim dir = Path.GetDirectoryName(projDir)
Dim dirs = Directory.GetDirectories(dir, Path.GetFileName(projDir))
Return "`" & Path.GetFileName(dirs(0)) & "`"
End Function
Protected Overrides ReadOnly Property ItemsSource As Specialized.INotifyCollectionChanged
Get
Return projFiles
End Get
End Property
Public ReadOnly Property SelectedFile As String
Get
If FilesList.SelectedIndex = -1 Then Return ""
Return CType(FilesList.SelectedItem, ProjFileInfo).FilePath
End Get
End Property
Protected Overrides Sub OnSelectionChanged()
Dim filePath = SelectedFile
If Path.GetFileNameWithoutExtension(filePath).ToLower() = "global" Then
Designer.SwitchTo(Helper.GlobalFileName)
Designer.CurrentPage.CodeFile = filePath
Else
Designer.SwitchTo(filePath)
End If
End Sub
Protected Overrides Sub OnDeleteItem()
If FilesList.SelectedIndex = -1 Then
Beep()
Return
End If
If MsgBox(
"This action will delete this form and its code files from the project folder and move them to the recycle bin. Are you sure you want to do that?",
vbYesNo Or MsgBoxStyle.Exclamation Or MsgBoxStyle.DefaultButton2,
"Warning"
) = MsgBoxResult.No Then
Return
End If
Dim i = FilesList.SelectedIndex
Dim projFileInfo = CType(FilesList.SelectedItem, ProjFileInfo)
Dim fileName = projFileInfo.FilePath
If File.Exists(fileName) Then
Try
FileIO.FileSystem.DeleteFile(fileName, FileIO.UIOption.OnlyErrorDialogs, FileIO.RecycleOption.SendToRecycleBin)
Catch ex As Exception
MsgBox(ex.Message)
End Try
End If
fileName = fileName.Substring(0, fileName.Length - 4) & "sb"
If File.Exists(fileName) Then
Try
FileIO.FileSystem.DeleteFile(fileName, FileIO.UIOption.OnlyErrorDialogs, FileIO.RecycleOption.SendToRecycleBin)
Catch ex As Exception
MsgBox(ex.Message)
End Try
End If
fileName &= ".gen"
If File.Exists(fileName) Then
Try
FileIO.FileSystem.DeleteFile(fileName, FileIO.UIOption.OnlyErrorDialogs, FileIO.RecycleOption.SendToRecycleBin)
Catch ex As Exception
MsgBox(ex.Message)
End Try
End If
projFiles.Remove(projFileInfo)
RaiseEvent FileDeleted()
Dim n = FilesList.Items.Count
If n = 0 Then Return
FilesList.SelectedIndex = If(i >= n, n - 1, i)
End Sub
Protected Overrides Function OnCommit(newName As String) As Boolean
Dim item = CType(FilesList.SelectedItem, ProjFileInfo)
If item.IsTheGlobalFile Then Return False
Dim oldFile = item.FilePath
Dim x = newName(0).ToString().ToUpper()
newName = x & If(newName.Length > 1, newName.Substring(1), "")
Dim newFile = Path.Combine(_projDir, newName & ".xaml")
Try
If newName.ToLower() = Path.GetFileNameWithoutExtension(oldFile).ToLower() Then Return True
If File.Exists(newFile) Then
MsgBox($"The project folder already contains a file named `{newName}`", MsgBoxStyle.OkOnly Or MsgBoxStyle.Critical)
Return False
End If
If File.Exists(oldFile) Then
File.Move(oldFile, newFile)
Dim progFileInfo = CType(FilesList.SelectedItem, ProjFileInfo)
Dim i = projFiles.IndexOf(progFileInfo)
projFiles.RemoveAt(i)
progFileInfo.FilePath = newFile
projFiles.Insert(i, progFileInfo)
oldFile = oldFile.Substring(0, oldFile.Length - 4) & "sb"
newFile = newFile.Substring(0, newFile.Length - 4) & "sb"
If File.Exists(oldFile) And Not File.Exists(newFile) Then
File.Move(oldFile, newFile)
End If
Dim oldGenFile = oldFile & ".gen"
Dim newGenFile = newFile & ".gen"
If File.Exists(oldGenFile) AndAlso Not File.Exists(newGenFile) Then
File.Move(oldGenFile, newGenFile)
End If
RaiseEvent FileNameChanged(oldFile, newFile)
Return True
Else
MsgBox($"The file `{oldFile}` doesn't exist in project folder", MsgBoxStyle.OkOnly Or MsgBoxStyle.Critical)
Return False
End If
Catch ex As Exception
MsgBox(ex.Message)
End Try
Return False
End Function
Protected Overrides Function OnBeginEdit() As Boolean
Return Not CType(FilesList.SelectedItem, ProjFileInfo).IsTheGlobalFile
End Function
Public Sub SelectedGlobalFile()
SelectItem(Path.Combine(_projDir, "Global.sb"))
End Sub
End Class
Public Class ProjFileInfo
Dim _filePath As String
Public Sub New(filePath As String)
_filePath = filePath
IsTheGlobalFile = Path.GetFileNameWithoutExtension(filePath).ToLower() = "global"
End Sub
Public Property FilePath As String
Get
Return _filePath
End Get
Set(value As String)
_filePath = value
End Set
End Property
Public ReadOnly Property IsTheGlobalFile As Boolean
Public Overrides Function ToString() As String
Return " ● " & Path.GetFileName(_filePath)
End Function
End Class