forked from Alos/xyzradio
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSFTable.j
244 lines (211 loc) · 7.52 KB
/
SFTable.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
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
//
// SFTable.j
// XYZRadio
//
// Created by Alos on 10/2/08.
// Copyright __MyCompanyName__ 2008. All rights reserved.
//
import <Foundation/CPObject.j>
import "Song.j"
SongsDragType = @"SongsDragType";
@implementation SFTable : CPView
{
/*Para las tablas*/
CPCollectionView collectionView;
CPArray model;
/*Cosas para los titulos*/
CPArray columnModel
/*Cosas para las celdas*/
SFCell celdas;
var pos=0;
}
-(void) initWithColumnModel:(CPArray)aColumnModel model:(CPArray)aModel frame:(CGRect)bounds{
self = [super initWithFrame:bounds];
[self setModel:aModel];
//para nuestro grid
collectionView = [[CPCollectionView alloc] initWithFrame: CGRectMake(0, 30, CGRectGetWidth(bounds), CGRectGetHeight(bounds))];
//los scrolls por si son muchos
var scrollView = [[CPScrollView alloc] initWithFrame: CGRectMake(0, 30, CGRectGetWidth(bounds), CGRectGetHeight(bounds))];
[scrollView setAutohidesScrollers: YES];
[scrollView setDocumentView: collectionView];
[[scrollView contentView] setBackgroundColor: NULL];
[scrollView setHasHorizontalScroller:NO]
[scrollView setAutoresizesSubviews:YES];
//los items q representan los renglones
var listItem = [[CPCollectionViewItem alloc] init];
celdas = [[SFCell alloc] initWithFrame:CGRectMakeZero()];
[listItem setView: celdas];
[collectionView setItemPrototype: listItem];
[collectionView setMaxNumberOfColumns:1];
[collectionView setMinItemSize:CPSizeMake(CGRectGetWidth(bounds), 20)];
[collectionView setMaxItemSize:CPSizeMake(CGRectGetWidth(bounds), 20)];
[collectionView setContent: model];
[self addSubview:scrollView];
//DELEGATE?
[collectionView setDelegate: self];
//la q esta arriba del Collectionview
var borderArriba = [[CPView alloc] initWithFrame:CGRectMake(0, 30 , CGRectGetWidth(bounds), 1)];
[borderArriba setBackgroundColor: [CPColor colorWithHexString:"33FF00"]];
[self addSubview: borderArriba];
//la de arriba
var borderTop = [[CPView alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(bounds), 1)];
[borderTop setBackgroundColor: [CPColor colorWithHexString:"33FF00"]];
[self addSubview: borderTop];
[self setColumnModel:aColumnModel];
[self registerForDraggedTypes:[SongsDragType]];
return self;
}
- (void)performDragOperation:(id <CPDraggingInfo>)aSender
{
// If this is us, don't add it.
if ([aSender draggingSource] == collectionView)
return;
var pasteboard = [aSender draggingPasteboard];
if ([[pasteboard types] containsObject:SongsDragType])
{
songs = [CPKeyedUnarchiver unarchiveObjectWithData:[pasteboard dataForType:SongsDragType]];
var index = 0,
count = songs.length;
for (; index < count; ++index){
if(![[collectionView content]containsObject:songs[index]])
[self addItem:songs[index]];
else
console.log("Repetido!");
}
}
}
-(void)setColumnModel:(CPArray)aColumnModel{
console.log("Setting the column model...");
columnModel = aColumnModel;
for(var i=0; i<[columnModel count];i++){
var thisColumn = [columnModel objectAtIndex:i];
[self addSubview: thisColumn];
if(i>0 && i<[columnModel count]){
pos= pos+CGRectGetWidth([[columnModel objectAtIndex: i-1] bounds])+1;
var border = [[CPView alloc] initWithFrame:CGRectMake(pos, 0, 1, CGRectGetHeight([self bounds]))];
console.log("Setting line at: %d",pos);
[border setBackgroundColor: [CPColor colorWithHexString:"33FF00"]];
[self addSubview: border];
}
}
}
-(void)setModel:(CPArray)aModel{
model = aModel;
[collectionView setContent: model];
[collectionView reloadContent];
}
/**
Adds an item to the table
@param the item to add to the table
*/
-(void)addItem:(CPObject)anItem{
[model addObject:anItem];
// [collectionView setModel: model];
[collectionView reloadContent];
}
/**
@param anIndex the value where the item you want t remove is
*/
-(void)removeItem:(int)anIndex{
[model removeObjectAtIndex: anIndex];
[collectionView reloadContent];
}
/**
Returns the item that is currently selected
@return
*/
-(int)getSelectedItem{
return [[collectionView selectionIndexes] firstIndex];
}
/**
Removes selected items
*/
-(void)removeSelectedItems{
console.log("removeSelectedItems in SFTable got the msg");
var indexes= [collectionView selectionIndexes];
var a = [indexes firstIndex];
[model removeObjectAtIndex: a];
[collectionView reloadContent];
}
-(CPIndexSet)getSelectedItems{
return [collectionView selectionIndexes];
}
-(CPArray)collectionView:(CPCollectionView)collectionView dragTypesForItemsAtIndexes:(CPIndexSet)indices{
return [SongsDragType];
}
- (CPData)collectionView:(CPCollectionView)aCollectionView dataForItemsAtIndexes:(CPIndexSet)indexes forType:(CPString)aType
{
var index = CPNotFound,
content = [aCollectionView content],
songs = [];
while ((index = [indexes indexGreaterThanIndex:index]) != CPNotFound)
songs.push(content[index]);
return [CPKeyedArchiver archivedDataWithRootObject:songs];
}
@end
@implementation SFCell : CPView
{
CPTextField titleView;
CPTextField authorView;
CPTextField time;
CPView highlightView;
Song theSong;
CPPasteboard aPasteboard;
}
- (void)setRepresentedObject:(JSObject)anObject
{
theSong = anObject;
if(!titleView)
{
titleView = [[CPTextField alloc] initWithFrame:CGRectInset( [self bounds], 4, 4)];
[titleView setFont: [CPFont systemFontOfSize: 12.0]];
[titleView setTextColor: [CPColor colorWithHexString:"33FF00"]];
[self addSubview: titleView];
}
[titleView setStringValue: [anObject songTitle]];
[titleView sizeToFit];
[titleView setFrameOrigin: CGPointMake(5,0.0)];
if(!authorView)
{
authorView = [[CPTextField alloc] initWithFrame:CGRectInset([self bounds], 4, 4)];
[authorView setFont: [CPFont systemFontOfSize: 12.0]];
[authorView setTextColor: [CPColor colorWithHexString:"33FF00"]];
[self addSubview: authorView];
}
[authorView setStringValue: [anObject artist]];
[authorView sizeToFit];
[authorView setFrameOrigin: CGPointMake(251,0.0)];
if(!time)
{
time = [[CPTextField alloc] initWithFrame:CGRectInset([self bounds], 4, 4)];
[time setFont: [CPFont systemFontOfSize: 12.0]];
[time setTextColor: [CPColor colorWithHexString:"33FF00"]];
[self addSubview: time];
}
[time setStringValue: [anObject time]];
[time sizeToFit];
[time setFrameOrigin: CGPointMake(500,0.0)];
}
- (void)setSelected:(BOOL)flag
{
if(!highlightView)
{
highlightView = [[CPView alloc] initWithFrame:CGRectCreateCopy([self bounds])];
[highlightView setBackgroundColor: [CPColor greenColor]];
}
if(flag)
{
[self addSubview:highlightView positioned:CPWindowBelow relativeTo: titleView];
[titleView setTextColor: [CPColor blackColor]];
[authorView setTextColor: [CPColor blackColor]];
[time setTextColor: [CPColor blackColor]];
}
else
{
[highlightView removeFromSuperview];
[titleView setTextColor: [CPColor colorWithHexString:"33FF00"]];
[authorView setTextColor: [CPColor colorWithHexString:"33FF00"]];
[time setTextColor: [CPColor colorWithHexString:"33FF00"]];
}
}
@end