This repository has been archived by the owner on Sep 11, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
FileEdit.vb
262 lines (212 loc) · 7.43 KB
/
FileEdit.vb
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
Imports Microsoft.VisualBasic
Imports System
Imports System.Collections.Generic
Imports System.Linq
Imports DevExpress.XtraEditors
Imports DevExpress.XtraEditors.Drawing
Imports System.ComponentModel
Imports System.Windows.Forms
Imports System.IO
Imports System.Drawing
Imports DevExpress.Utils.Menu
Imports DevExpress.LookAndFeel
Imports DevExpress.XtraEditors.Popup
Imports DevExpress.Utils
Namespace CustomEditor
Friend Class FileEdit
Inherits PopupContainerEdit
Shared Sub New()
RepositoryItemFileEdit.Register()
End Sub
Public Sub New()
CreateContextMenu()
AddHandler ToolTipController.DefaultController.GetActiveObjectInfo, AddressOf tooltip_GetActiveObjectInfo
End Sub
Private Sub tooltip_GetActiveObjectInfo(ByVal sender As Object, ByVal e As ToolTipControllerGetActiveObjectInfoEventArgs)
If e.SelectedControl IsNot Me Then
Return
End If
Dim info As ToolTipControlInfo = Nothing
Dim text As String = ""
If EditValue IsNot Nothing Then
text = EditValue.ToString()
info = New ToolTipControlInfo(text, text)
End If
If info IsNot Nothing Then
e.Info = info
End If
End Sub
Private defaultDirectory As String = "c:\"
Private Sub InitializeOpenDialog()
openFileDialog_Renamed.InitialDirectory = defaultDirectory
openFileDialog_Renamed.Filter = "Text Files (*.txt)|*.txt|BMP Files (*.bmp)|*.bmp|PNG Files (*.png)|*.png|JPG Files (*.jpg)|*.jpg|GIF Files (*.gif)|*.gif|All files (*.*)|*.*"
End Sub
Protected Overrides Overloads Sub Dispose(ByVal disposing As Boolean)
RemoveHandler ToolTipController.DefaultController.GetActiveObjectInfo, AddressOf tooltip_GetActiveObjectInfo
MyBase.Dispose(disposing)
End Sub
Private menu As DXPopupMenu
Private Sub CreateContextMenu()
menu= New DXPopupMenu()
menu.MenuViewType = MenuViewType.Menu
menu.Items.Add(New DXMenuItem("Open file", AddressOf OpenFile))
menu.Items.Add(New DXMenuItem("Rename/Move to ... file", AddressOf RenameFile))
menu.Items.Add(New DXMenuItem("Copy file", AddressOf CopyFile))
menu.Items.Add(New DXMenuItem("Delete file", AddressOf DeleteFile))
SetMenuItemEnabledMode(False)
End Sub
Private Sub SetMenuItemEnabledMode(ByVal key As Boolean)
For Each m As DXMenuItem In menu.Items
m.Enabled = key
Next m
End Sub
Private skinMenuManager As SkinMenuManager
Protected Overrides Sub OnMouseUp(ByVal e As MouseEventArgs)
MyBase.OnMouseUp(e)
If e.Button = MouseButtons.Right Then
Dim lf As UserLookAndFeel = UserLookAndFeel.Default
If skinMenuManager Is Nothing Then
skinMenuManager = New SkinMenuManager(lf)
End If
CType(menu, IDXDropDownControl).Show(skinMenuManager, Me, e.Location)
End If
End Sub
Private Sub RetrieveInformation(ByVal info As FileInfo)
blobForm.InitializeFileInfo()
blobForm.FillLabelContent(info)
blobForm.DisplayOpenFilePreview()
End Sub
Protected Overrides Sub OnEditValueChanged()
MyBase.OnEditValueChanged()
If EditValue IsNot Nothing Then
If EditValue.ToString() <> "" Then
SetMenuItemEnabledMode(True)
Dim info As FileInfo = GetFileInfo()
If blobForm IsNot Nothing Then
RetrieveInformation(info)
End If
End If
End If
End Sub
Private Sub OpenFile(ByVal sender As Object, ByVal e As EventArgs)
Try
System.Diagnostics.Process.Start(GetFileName())
Catch ex As Exception
MessageBox.Show("Error: Could not read file from disk. Original error: " & ex.Message)
End Try
End Sub
Private fileDialog As SaveFileDialog
Private Sub RenameFile(ByVal sender As Object, ByVal e As EventArgs)
Dim oldFileName As String = GetFileName()
If fileDialog Is Nothing Then
fileDialog = New SaveFileDialog()
End If
fileDialog.Title = "Rename/Move to ... "
If GetOpenSaveDialogResult() Then
File.Move(oldFileName, fileDialog.FileName)
End If
End Sub
Private Sub SetFileEditValue(ByVal newFileName As String)
Me.EditValue = newFileName
End Sub
Private Sub CopyFile(ByVal sender As Object, ByVal e As EventArgs)
Dim oldFileName As String = GetFileName()
If fileDialog Is Nothing Then
fileDialog = New SaveFileDialog()
End If
fileDialog.Title = "Copy"
If GetOpenSaveDialogResult() Then
File.Copy(oldFileName, fileDialog.FileName, True)
End If
End Sub
Private Function GetOpenSaveDialogResult() As Boolean
Dim oldFileName As String = GetFileName()
fileDialog.FileName = oldFileName
fileDialog.DefaultExt = Path.GetExtension(oldFileName)
If fileDialog.ShowDialog() = DialogResult.OK Then
Dim newFileName As String = fileDialog.FileName
fileDialog.AddExtension = True
SetFileEditValue(newFileName)
Return True
End If
Return False
End Function
Private Sub DeleteFile(ByVal sender As Object, ByVal e As EventArgs)
Try
Dim result As DialogResult = MessageBox.Show("Are you sure you want to delete this file?", "Information", MessageBoxButtons.YesNo, MessageBoxIcon.Information)
If result = DialogResult.Yes Then
File.Delete(GetFileName())
MessageBox.Show("File " & GetFileName() & " was successfully deleted!", "Information")
Me.EditValue = Nothing
SetMenuItemEnabledMode(False)
End If
Catch ex As Exception
MessageBox.Show("Error: Could not delete file from disk. Original error: " & ex.Message)
End Try
End Sub
Public Overrides ReadOnly Property EditorTypeName() As String
Get
Return RepositoryItemFileEdit.FileEditName
End Get
End Property
<DesignerSerializationVisibility(DesignerSerializationVisibility.Content)> _
Public Shadows ReadOnly Property Properties() As RepositoryItemFileEdit
Get
Return TryCast(MyBase.Properties, RepositoryItemFileEdit)
End Get
End Property
Protected Overrides Sub OnClickButton(ByVal buttonInfo As EditorButtonObjectInfoArgs)
MyBase.OnClickButton(buttonInfo)
If buttonInfo.Button.Tag IsNot Nothing Then
If buttonInfo.Button.Tag.ToString() = "OpenFileDialog" Then
OpenFileDialog()
End If
End If
End Sub
Protected Friend Function GetFileName() As String
Return If((EditValue Is Nothing), defaultDirectory, Me.EditValue.ToString())
End Function
Private openFileDialog_Renamed As OpenFileDialog
Private Sub OpenFileDialog()
If openFileDialog_Renamed Is Nothing Then
openFileDialog_Renamed = New OpenFileDialog()
InitializeOpenDialog()
End If
openFileDialog_Renamed.InitialDirectory = GetFileName()
If openFileDialog_Renamed.ShowDialog() = DialogResult.OK Then
Dim fileName As String = openFileDialog_Renamed.FileName
SetFileEditValue(fileName)
End If
End Sub
Private Function GetFileInfo() As FileInfo
Return New FileInfo(GetFileName())
End Function
Protected Overrides Function CalcPopupFormSize() As Size
If EditValue IsNot Nothing Then
If EditValue.ToString() = "" Then
blobForm.ClearPopupContainerContent()
End If
End If
Return blobForm.CalcBlobPopupFormSize()
End Function
Private blobForm As CustomPopupForm
Protected Overrides Function GetPopupForm() As PopupBaseForm
If blobForm Is Nothing Then
blobForm = New CustomPopupForm(Me)
If EditValue IsNot Nothing Then
If EditValue.ToString() <> "" Then
RetrieveInformation(GetFileInfo())
Else
blobForm.ClearPopupContainerContent()
End If
End If
End If
Return blobForm
End Function
Protected Overrides ReadOnly Property PopupForm() As PopupBaseForm
Get
Return blobForm
End Get
End Property
End Class
End Namespace