-
Notifications
You must be signed in to change notification settings - Fork 2
/
TNMessageBoard.j
186 lines (152 loc) · 5.19 KB
/
TNMessageBoard.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
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
/*
* TNMessageBoard.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/CPTableColumn.j>
@import <AppKit/CPTableView.j>
@import "TNMessageView.j"
/*! @ingroup messageboard
This class allows to stack TNMessageView in a variable
row height CPTableView with shortcuts. Result will display
a sort of chat view
*/
@implementation TNMessageBoard : CPTableView
{
CPArray _messages;
TNMessageView _dataView;
}
#pragma mark -
#pragma mark Initialization
/*! initialize a new TNMessageBoard
@param aFrame the frame of the TNMessageBoard
@return an initialized TNMessageBoard
*/
- (TNMessageBoard)initWithFrame:(CPRect)aFrame
{
if (self = [super initWithFrame:aFrame])
{
_messages = [CPArray array];
[self setAutoresizingMask: CPViewWidthSizable | CPViewHeightSizable];
[self setColumnAutoresizingStyle:CPTableViewLastColumnOnlyAutoresizingStyle];
[self setAllowsColumnReordering:NO];
[self setAllowsColumnResizing:NO];
[self setAllowsEmptySelection:YES];
[self setAllowsMultipleSelection:NO];
[self setDataSource:self];
[self setDelegate:self];
[self setHeaderView:nil];
[self setCornerView:nil];
var messageColumn = [[CPTableColumn alloc] initWithIdentifier:@"messages"];
[messageColumn setWidth:800];
[[messageColumn headerView] setStringValue:@"Messages"];
[self addTableColumn:messageColumn];
_dataView = [[TNMessageView alloc] initWithFrame:CPRectMake(0, 0, 100, 100)];
}
return self;
}
#pragma mark -
#pragma mark Controls
/*! stack a new message
@param aMessage the content of the message
@param anAuthor sender of the message
@param aColor a CPColor that will be used as background
@param aDate the date of the message
*/
- (void)addMessage:(CPString)aMessage from:(CPString)anAuthor date:(CPDate)aDate color:(int)aColor
{
[self addMessage:aMessage from:anAuthor date:aDate color:aColor avatar:nil position:nil];
}
/*! stack a new message
@param aMessage the content of the message
@param anAuthor sender of the message
@param aColor a CPColor that will be used as background
@param aDate the date of the message
@param anAvatar CPImage containing anAvatar
*/
- (void)addMessage:(CPString)aMessage from:(CPString)anAuthor date:(CPDate)aDate color:(int)aColor avatar:(CPImage)anAvatar position:(int)aPosition
{
var messageInfo = [CPDictionary dictionaryWithObjectsAndKeys:anAuthor, @"author",
aMessage, @"message",
aDate, @"date",
aPosition, @"position",
aColor, @"color",
anAvatar, @"avatar"];
[_messages addObject:messageInfo];
[self reloadData];
}
/*! compatibility
@deprecated
*/
- (void)reload
{
CPLog.warn("TNMessageBoard reload is deprecated. please use reloadData")
[self reloadData];
}
#pragma mark -
#pragma mark Actions
/*! remove all message.
@deprecated
*/
- (IBAction)removeAllMessages:(id)aSender
{
CPLog.warn("TNMessageBoard removeAllMessages: is deprecated. please use removeAllViews:")
[self removeAllViews:aSender];
}
/*! remove all items as an IBAction
*/
- (IBAction)removeAllViews:(id)aSender
{
[_messages removeAllObjects];
[self reloadData];
}
#pragma mark -
#pragma mark DataSource
/*! tableview data source protocol
*/
- (void)numberOfRowsInTableView:(CPTableView)aTableView
{
return [_messages count];
}
/*! tableview data source protocol
*/
- (void)tableView:(CPTableView)aTableView objectValueForTableColumn:(CPTableColumn)aColumnu row:(int)aRow
{
return [_messages objectAtIndex:aRow];
}
#pragma mark -
#pragma mark Delegate
/*! tableview delegate
*/
- (void)tableView:(CPTableView)aTableView dataViewForTableColumn:(CPTableColumn)aColumn row:(id)aRow
{
return _dataView;
}
/*! tableview delegate
*/
- (void)tableView:(CPTableView)aTableView heightOfRow:(int)aRow
{
return [TNMessageView sizeOfMessageViewWithText:[[_messages objectAtIndex:aRow] objectForKey:@"message"] inWidth:([self frameSize].width)];
}
/*! tableview delegate
*/
- (void)tableView:(CPTableView)aTableView shouldSelectRow:(int)aRow
{
return NO;
}
@end