-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclsRibbonEvents.cls
More file actions
277 lines (233 loc) · 9.13 KB
/
Copy pathclsRibbonEvents.cls
File metadata and controls
277 lines (233 loc) · 9.13 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
VERSION 1.0 CLASS
BEGIN
MultiUse = -1 'True
END
Attribute VB_Name = "clsRibbonEvents"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = False
Attribute VB_Exposed = True
' ==========================================
' CLASE DE EVENTOS DEL RIBBON
' ==========================================
' Esta clase envuelve el objeto IRibbonUI y gestiona
' su ciclo de vida con proteccion y logging.
'
' CAMBIOS IMPORTANTES:
' - ribbonUI ahora es privado con Property Get
' - mWasEverInitialized es informativo, no bloquea
' ==========================================
'@Folder "2-Servicios.Excel.Ribbon"
Option Explicit
Private Const MODULE_NAME As String = "clsRibbonEvents"
' ==========================================
' RIBBON UI Y ESTADO
' ==========================================
Private mribbonUI As IRibbonUI
Private mIsRecovering As Boolean ' Flag para evitar recursion durante recuperacion
Private mWasEverInitialized As Boolean ' Flag para distinguir "nunca inicializado" de "perdido"
' ==========================================
' EVENTOS DE APLICACION
' ==========================================
Public Event GenerarGraficosDesdeCurvasRto()
Public Event InvertirEjes()
Public Event FormatearCGASING()
Public Event Configurador()
Public Event NuevaOportunidad()
Public Event ReplaceWithNamesInValidations()
' ==========================================
' PROPERTY GET/SET PARA ribbonUI (PROTEGIDO)
' ==========================================
'@Description: Obtiene el puntero al Ribbon (puede ser Nothing)
Public Property Get ribbonUI() As IRibbonUI
Set ribbonUI = mribbonUI
End Property
' ==========================================
' INICIALIZACION Y LIMPIEZA
' ==========================================
Private Sub Class_Initialize()
LogInfo MODULE_NAME, "[Class_Initialize]"
mWasEverInitialized = False
mIsRecovering = False
End Sub
Private Sub Class_Terminate()
LogInfo MODULE_NAME, "[Class_Terminate]"
End Sub
Public Sub Init(ByRef ribbonObj As IRibbonUI)
LogDebug MODULE_NAME, "[Init] - Recibiendo puntero IRibbonUI"
Set mribbonUI = ribbonObj
If ribbonObj Is Nothing Then
LogWarning MODULE_NAME, "[Init] - Se paso Nothing como puntero"
Else
LogDebug MODULE_NAME, "[Init] - Puntero establecido correctamente"
End If
mWasEverInitialized = True
End Sub
Public Sub StopEvents()
LogDebug MODULE_NAME, "[StopEvents] - Liberando puntero"
Set mribbonUI = Nothing
End Sub
Public Sub OnGenerarGraficosDesdeCurvasRto()
RaiseEvent GenerarGraficosDesdeCurvasRto
End Sub
Public Sub OnInvertirEjes()
RaiseEvent InvertirEjes
End Sub
Public Sub OnFormatearCGASING()
RaiseEvent FormatearCGASING
End Sub
Public Sub OnConfigurador()
RaiseEvent Configurador
End Sub
Public Sub OnNuevaOportunidad()
RaiseEvent NuevaOportunidad
End Sub
Public Sub OnReplaceWithNamesInValidations()
RaiseEvent ReplaceWithNamesInValidations
End Sub
'@Description: Activa una pestaña del Ribbon por id
'@Scope: Friend (uso exclusivo desde clsAplicacion)
'@ArgumentDescriptions: tabId | id del tab a activar
'@Category: UI / Ribbon
Friend Sub ActivarTab(tabId As String)
On Error Resume Next
If IsRibbonUIAvailable() Then
mribbonUI.ActivateTab tabId
End If
End Sub
Public Function GetRibbonControlEnabled(control As IRibbonControl) As Boolean
Dim enabled As Boolean
Select Case control.id
Case "btnGenerarGraficos" ' GetGraficoEnabled
enabled = EsFicheroOportunidad() And EsValidoGenerarGrafico()
Case "btnInvertirSeries" ' GetInvertirEjesEnabled
enabled = EsFicheroOportunidad()
If enabled Then enabled = App.bChartActive ' comprueba que hay un grafico seleccionado
If enabled Then enabled = EsValidoInvertirEjes()
' HAY QUE PASAR A ESTO: el orquestador determina el estado del ribbon:
' ... PERO DE MOMENTO, NO ESTA TERMINADO DE IMPLEMENTAR clsExcelFile y clsFileManager
' enabled = App.bCanInvertAxes
Case "btnCGASING" ' GetCGASINGEnabled
enabled = EsFicheroOportunidad()
If enabled Then enabled = IsDefaultCGasIngSheet()
Case "btnNuevaOp" ' GetNuevaOportunidadEnabled
enabled = True
End Select
GetRibbonControlEnabled = enabled
'InvalidarControl control.id ' esto ESTA DE SOBRA!!
LogDebug MODULE_NAME, "[GetRibbonControlEnabled] - ¿" & control.id & "?: " & CBool(enabled)
End Function
'@Description: Invalida todo el Ribbon, forzando recarga de callbacks
'@Note: Intenta recuperacion automatica si el Ribbon esta perdido
Public Sub InvalidarRibbon()
On Error GoTo ErrHandler
' Verificar si el Ribbon esta disponible
If Not IsRibbonUIAvailable() Then
' IMPORTANTE: Solo intentar recuperacion si el ribbon FUE inicializado alguna vez
' Esto evita que durante Class_Initialize se intente recuperar un ribbon que
' aun no ha sido configurado (el callback RibbonOnLoad aun no ha terminado)
If Not mWasEverInitialized Then
LogDebug MODULE_NAME, "[InvalidarRibbon] Ribbon aun no inicializado, omitiendo"
Exit Sub
End If
' Intentar recuperacion automatica (solo si no estamos ya recuperando)
If Not mIsRecovering Then
LogWarning MODULE_NAME, "[InvalidarRibbon] Ribbon perdido, intentando recuperacion..."
If TryAutoRecover() Then
LogDebug MODULE_NAME, "[InvalidarRibbon] Recuperacion exitosa"
Else
LogError MODULE_NAME, "[InvalidarRibbon] Recuperacion fallida"
Exit Sub
End If
Else
LogDebug MODULE_NAME, "[InvalidarRibbon] Recuperacion en curso, omitiendo"
Exit Sub
End If
End If
' Invalidar el Ribbon
mribbonUI.Invalidate
Exit Sub
ErrHandler:
LogError MODULE_NAME, "[InvalidarRibbon] Error", Err.Description
' No propagar el error, solo loguear
End Sub
'@Description: Invalida un control especifico del Ribbon
'@Note: Intenta recuperacion automatica si el Ribbon esta perdido
Public Sub InvalidarControl(idControl As String)
On Error GoTo ErrHandler
' Verificar si el Ribbon esta disponible
If Not IsRibbonUIAvailable() Then
' IMPORTANTE: Solo intentar recuperacion si el ribbon FUE inicializado alguna vez
If Not mWasEverInitialized Then
LogDebug MODULE_NAME, "[InvalidarControl] Ribbon aun no inicializado, omitiendo: " & idControl
Exit Sub
End If
' Intentar recuperacion automatica
If Not mIsRecovering Then
LogWarning MODULE_NAME, "[InvalidarControl] Ribbon perdido, intentando recuperacion..."
If Not TryAutoRecover() Then
LogError MODULE_NAME, "[InvalidarControl] Recuperacion fallida para control: " & idControl
Exit Sub
End If
Else
Exit Sub
End If
End If
' Invalidar el control
mribbonUI.InvalidateControl idControl
Exit Sub
ErrHandler:
LogError MODULE_NAME, "[InvalidarControl] Error en control " & idControl, , Err.Description
End Sub
'@Description: Verifica si el objeto ribbonUI esta disponible y funcional
Private Function IsRibbonUIAvailable() As Boolean
If mribbonUI Is Nothing Then IsRibbonUIAvailable = False: Exit Function
On Error Resume Next
Dim testType As String
testType = TypeName(mribbonUI)
If Err.Number <> 0 Then
IsRibbonUIAvailable = False
Err.Clear
ElseIf testType = "Nothing" Or testType = "Empty" Then
IsRibbonUIAvailable = False
Else
IsRibbonUIAvailable = True
End If
End Function
'@Description: Intenta recuperar el Ribbon automaticamente
Private Function TryAutoRecover() As Boolean
On Error GoTo ErrHandler
mIsRecovering = True
LogInfo MODULE_NAME, "[TryAutoRecover] - Iniciando"
' Usar el modulo de recuperacion
TryAutoRecover = TryRecoverRibbon()
mIsRecovering = False
Exit Function
ErrHandler:
LogError MODULE_NAME, "[TryAutoRecover] - Error", Err.Number, Err.Description
mIsRecovering = False
TryAutoRecover = False
End Function
'@Description: Indica si el Ribbon esta en proceso de recuperacion
Public Property Get IsRecovering() As Boolean
IsRecovering = mIsRecovering
End Property
'@Description: Diagnostico rapido del estado del Ribbon
Public Function GetQuickDiagnostics() As String
Dim info As String
info = "RibbonUI: "
If Not IsRibbonUIAvailable() Then
info = info & "Nothing"
Else
On Error Resume Next
info = info & TypeName(mribbonUI)
If Err.Number <> 0 Then
info = info & "Corrupted"
Err.Clear
End If
On Error GoTo 0
End If
info = info & " | WasInit: " & mWasEverInitialized
info = info & " | Recovering: " & mIsRecovering
GetQuickDiagnostics = info
End Function