forked from DeNardoLab/BehaviorDEPOT
-
Notifications
You must be signed in to change notification settings - Fork 0
/
BehDEPOT_v045App.m
117 lines (104 loc) · 4.21 KB
/
BehDEPOT_v045App.m
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
classdef BehDEPOT_v045App < handle
%
% Usage:
% app.BehDEPOT_v045App
properties
AppHandle;
AppCount = 0;
Output;
CurrClass;
end
properties (Constant)
AppPathBase = {'/BehDEPOT_v045'};
AppClass = 'BehDEPOT_v045App';
Increment = 1;
Decrement = 0;
Version = '13a';
end
properties (Dependent)
AppPath;
end
methods (Static)
function count = refcount(increment)
persistent AppCount;
if(isempty(AppCount))
AppCount = 1;
else
if(increment)
AppCount = plus(AppCount,1);
else
AppCount = minus(AppCount,1);
if AppCount < 0
AppCount = 0;
end
end
end
count = AppCount;
end
end
methods
% Create the application object
function obj = BehDEPOT_v045App()
obj.CurrClass = metaclass(obj);
startApp(obj)
end
function value = get.AppPath(obj)
myAppsLocation = char(com.mathworks.appmanagement.MlappinstallUtil.getAppInstallationFolder);
value = cellfun(@(x) fullfile(myAppsLocation, x), obj.AppPathBase, 'UniformOutput', false);
end
% Start the application
function startApp(obj)
% Increment the reference count by one and lock the file
mlock;
BehDEPOT_v045App.refcount(obj.Increment);
% Verify we are about to execute the correct function - if not we
% should error and exit now. We need to make sure the paths are
% equal using canonical paths.
existVal = exist(fullfile(pwd,'BehDep_GUI_current')); %#ok<EXIST>
doesShadowExist = existVal >= 2 && existVal <= 6;
pathOne = java.io.File(pwd);
pathOne = pathOne.getCanonicalPath();
pathTwo = java.io.File(obj.AppPath{1});
pathTwo = pathTwo.getCanonicalPath();
if (doesShadowExist && ~pathOne.equals(pathTwo))
% We are trying to execute the wrong BehDep_GUI_current
errordlg(message('MATLAB:apps:runapp:WrongEntryPoint', 'BehDep_GUI_current').getString, ...
message('MATLAB:apps:runapp:WrongEntryPointTitle').getString);
appinstall.internal.stopapp([],[],obj)
return;
end
% Must load function (force by using function handle) or nargout lies.
% Check if the app is a GUIDE app
if nargout(@BehDep_GUI_current) == 0
eval('BehDep_GUI_current');
else
obj.AppHandle = eval('BehDep_GUI_current');
end
if(ishandle(obj.AppHandle))
% Traditional graphics handle based app
obj.attachOncleanupToFigure(obj.AppHandle);
elseif isa(obj.AppHandle, 'matlab.apps.AppBase')
% appdesigner based app
obj.attachOncleanupToFigure(appdesigner.internal.service.AppManagementService.getFigure(obj.AppHandle));
elseif isa(obj.AppHandle,'handle') && ~isvalid(obj.AppHandle)
% Cleanup in the case where the handle was invalidated before here
appinstall.internal.stopapp([],[],obj)
else
% There will be no call to stopapp, instead decrease the refcount
% now to prevent future clearing issues
BehDEPOT_v045App.refcount(obj.Increment);
munlock;
end
end
function attachOncleanupToFigure(obj, fig)
% Setup cleanup code on figure handle using onCleanup object
cleanupObj = onCleanup(@()appinstall.internal.stopapp([],[],obj));
appdata = getappdata(fig);
appfields = fields(appdata);
found = cellfun(@(x) strcmp(x,'AppCleanupCode'), appfields);
if(~any(found))
setappdata(fig, 'AppCleanupCode', cleanupObj);
end
end
end
end