-
Notifications
You must be signed in to change notification settings - Fork 13
/
TNAlert.j
159 lines (131 loc) · 4.78 KB
/
TNAlert.j
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
/*
* TNAlert.j
*
* Copyright (C) 2010 Antoine Mercadal <[email protected]>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3.0 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
@import <Foundation/Foundation.j>
@import <AppKit/CPAlert.j>
/*! @ingroup tnkit
A very easy to use alert without all the NSAlert mess
*/
@implementation TNAlert : CPAlert
{
CPArray _actions @accessors(getter=actions);
id _helpTarget @accessors(getter=helpTarget);
id _target @accessors(property=target);
id _userInfo @accessors(property=userInfo);
SEL _helpAction @accessors(getter=helpAction);
}
#pragma mark -
#pragma mark Initialization
/*! create a TNAlert and show alert with given parameters
@param aTitle CPString containing the title
@param aMessage CPString containing the message
@return ready to use TNAlert
*/
+ (void)showAlertWithMessage:(CPString)aTitle informative:(CPString)aMessage
{
var tnalert = [[TNAlert alloc] initWithMessage:aTitle informative:aMessage target:nil actions:[["Ok", nil]]];
[tnalert runModal];
}
/*! create a TNAlert and show alert with given parameters
@param aTitle CPString containing the title
@param aMessage CPString containing the message
@param aStyle the style of the alert
@return ready to use TNAlert
*/
+ (void)showAlertWithMessage:(CPString)aTitle informative:(CPString)aMessage style:(int)aStyle
{
var tnalert = [[TNAlert alloc] initWithMessage:aTitle informative:aMessage target:nil actions:[["Ok", nil]]];
[tnalert setAlertStyle:aStyle];
[tnalert runModal];
}
/*! create a TNAlert alert with given parameters
@param aTitle CPString containing the title
@param aMessage CPString containing the message
@param aTarget the target of the actions
@param actions CPArray containing name of buttons and associated actions
(ex [["Ok", @selector(performOK:)],["NOK", @selector(performNOK:)]])
@return ready to use TNAlert
*/
+ (void)alertWithMessage:(CPString)aTitle informative:(CPString)aMessage target:(id)aTarget actions:(CPArray)someActions
{
var tnalert = [[TNAlert alloc] initWithMessage:aTitle informative:aMessage target:aTarget actions:someActions];
return tnalert;
}
/*! create a TNAlert alert with given parameters
@param aTitle CPString containing the title
@param aMessage CPString containing the message
@param aTarget the target of the actions
@param actions CPArray containing name of buttons and associated actions
(ex [["Ok", @selector(performOK:)],["NOK", @selector(performNOK:)]])
@return ready to use TNAlert
*/
- (TNAlert)initWithMessage:(CPString)aTitle informative:(CPString)aMessage target:(id)aTarget actions:(CPArray)someActions
{
if (self = [super init])
{
_actions = someActions;
_target = aTarget;
[self setMessageText:aTitle];
[self setInformativeText:aMessage];
[self setDelegate:self];
[self setAlertStyle:CPInformationalAlertStyle];
for (var i = 0, c = [_actions count]; i < c; i++)
[self addButtonWithTitle:_actions[i][0]];
}
return self;
}
/*! define the target and action for help button
if set, help button will be automatically set.
to remove it, simply set aTarget of anAction to nil
@param aTarget the target of the help action
@param anAction the action to execute if user click the help button
*/
- (void)setHelpTarget:(id)aTarget action:(SEL)anAction
{
if (aTarget && anAction)
{
_helpTarget = aTarget;
_helpAction = anAction;
[self setShowsHelp:YES];
}
else
{
_helpTarget = nil;
_helpAction = nil;
[self setShowsHelp:NO];
}
}
#pragma mark -
#pragma mark Delegates
/*! @ignore
*/
- (void)alertDidEnd:(CPAlert)theAlert returnCode:(int)returnCode
{
var selector = _actions[returnCode][1];
if ([_target respondsToSelector:selector])
[_target performSelector:selector withObject:_userInfo];
}
/*! @ignore
*/
- (void)alertShowHelp:(CPAlert)anAlert
{
if ([_helpTarget respondsToSelector:_helpAction])
[_helpTarget performSelector:_helpAction withObject:anAlert];
}
@end