source: trunk/macosx/GroupsWindowController.m @ 4189

Last change on this file since 4189 was 4189, checked in by livings124, 15 years ago

sort groups by their order in the table

File size: 11.9 KB
Line 
1/******************************************************************************
2 * $Id$
3 *
4 * Copyright (c) 2007 Transmission authors and contributors
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 * DEALINGS IN THE SOFTWARE.
23 *****************************************************************************/
24
25#import "GroupsWindowController.h"
26#import "GradientCell.h"
27#import "CTGradient.h"
28#import "NSBezierPathAdditions.h"
29#import "NSApplicationAdditions.h"
30
31typedef enum
32{
33    ADD_TAG = 0,
34    REMOVE_TAG = 1
35} controlTag;
36
37@interface GroupsWindowController (Private)
38
39- (void) saveGroups;
40
41- (CTGradient *) gradientForColor: (NSColor *) color;
42- (void) changeColor: (id) sender;
43
44@end
45
46@implementation GroupsWindowController
47
48GroupsWindowController * fGroupsWindowInstance = nil;
49+ (GroupsWindowController *) groupsController
50{
51    if (!fGroupsWindowInstance)
52        fGroupsWindowInstance = [[GroupsWindowController alloc] init];
53    return fGroupsWindowInstance;
54}
55
56- (id) init
57{
58    if ((self = [super initWithWindowNibName: @"GroupsWindow"]))
59    {
60        NSData * data;
61        if ((data = [[NSUserDefaults standardUserDefaults] dataForKey: @"Groups"]))
62            fGroups = [[NSUnarchiver unarchiveObjectWithData: data] retain];
63        else
64        {
65            //default groups
66            NSMutableDictionary * red = [NSMutableDictionary dictionaryWithObjectsAndKeys:
67                                            [NSColor redColor], @"Color",
68                                            NSLocalizedString(@"Red", "Groups -> Name"), @"Name",
69                                            [NSNumber numberWithInt: 0], @"Index", nil];
70           
71            NSMutableDictionary * orange = [NSMutableDictionary dictionaryWithObjectsAndKeys:
72                                            [NSColor orangeColor], @"Color",
73                                            NSLocalizedString(@"Orange", "Groups -> Name"), @"Name",
74                                            [NSNumber numberWithInt: 1], @"Index", nil];
75           
76            NSMutableDictionary * yellow = [NSMutableDictionary dictionaryWithObjectsAndKeys:
77                                            [NSColor yellowColor], @"Color",
78                                            NSLocalizedString(@"Yellow", "Groups -> Name"), @"Name",
79                                            [NSNumber numberWithInt: 2], @"Index", nil];
80           
81            NSMutableDictionary * green = [NSMutableDictionary dictionaryWithObjectsAndKeys:
82                                            [NSColor greenColor], @"Color",
83                                            NSLocalizedString(@"Green", "Groups -> Name"), @"Name",
84                                            [NSNumber numberWithInt: 3], @"Index", nil];
85           
86            NSMutableDictionary * blue = [NSMutableDictionary dictionaryWithObjectsAndKeys:
87                                            [NSColor blueColor], @"Color",
88                                            NSLocalizedString(@"Blue", "Groups -> Name"), @"Name",
89                                            [NSNumber numberWithInt: 4], @"Index", nil];
90           
91            NSMutableDictionary * purple = [NSMutableDictionary dictionaryWithObjectsAndKeys:
92                                            [NSColor purpleColor], @"Color",
93                                            NSLocalizedString(@"Purple", "Groups -> Name"), @"Name",
94                                            [NSNumber numberWithInt: 5], @"Index", nil];
95           
96            NSMutableDictionary * gray = [NSMutableDictionary dictionaryWithObjectsAndKeys:
97                                            [NSColor grayColor], @"Color",
98                                            NSLocalizedString(@"Gray", "Groups -> Name"), @"Name",
99                                            [NSNumber numberWithInt: 6], @"Index", nil];
100           
101            fGroups = [[NSMutableArray alloc] initWithObjects: red, orange, yellow, green, blue, purple, gray, nil];
102            [self saveGroups]; //make sure this is saved right away
103        }
104    }
105   
106    return self;
107}
108
109- (void) awakeFromNib
110{
111    GradientCell * cell = [[GradientCell alloc] init];
112    [[fTableView tableColumnWithIdentifier: @"Color"] setDataCell: cell];
113    [cell release];
114   
115    if ([NSApp isOnLeopardOrBetter])
116        [[self window] setContentBorderThickness: [[fTableView enclosingScrollView] frame].origin.y forEdge: NSMinYEdge];
117   
118    [fAddRemoveControl setEnabled: NO forSegment: REMOVE_TAG];
119}
120
121- (void) dealloc
122{
123    [fGroups release];
124    [super dealloc];
125}
126
127- (CTGradient *) gradientForIndex: (int) index
128{
129    if (index < 0)
130        return nil;
131   
132    NSEnumerator * enumerator = [fGroups objectEnumerator];
133    NSDictionary * dict;
134    while ((dict = [enumerator nextObject]))
135        if ([[dict objectForKey: @"Index"] intValue] == index)
136            return [self gradientForColor: [dict objectForKey: @"Color"]];
137   
138    return nil;
139}
140
141- (int) orderValueForIndex: (int) index
142{
143    if (index != -1)
144    {
145        int i;
146        for (i = 0; i < [fGroups count]; i++)
147            if (index == [[[fGroups objectAtIndex: i] objectForKey: @"Index"] intValue])
148                return i;
149    }
150    return -1;
151}
152
153- (NSInteger) numberOfRowsInTableView: (NSTableView *) tableview
154{
155    return [fGroups count];
156}
157
158- (id) tableView: (NSTableView *) tableView objectValueForTableColumn: (NSTableColumn *) tableColumn row: (NSInteger) row
159{
160    #warning consider color column ?
161    NSString * identifier = [tableColumn identifier];
162    if ([identifier isEqualToString: @"Color"])
163        return [self gradientForColor: [[fGroups objectAtIndex: row] objectForKey: @"Color"]];
164    else
165        return [[fGroups objectAtIndex: row] objectForKey: @"Name"];
166}
167
168- (void) tableView: (NSTableView *) tableView setObjectValue: (id) object forTableColumn: (NSTableColumn *) tableColumn
169    row: (NSInteger) row
170{
171    NSString * identifier = [tableColumn identifier];
172    if ([identifier isEqualToString: @"Name"])
173    {
174        [[fGroups objectAtIndex: row] setObject: object forKey: @"Name"];
175        [self saveGroups];
176    }
177    else if ([identifier isEqualToString: @"Button"])
178    {
179        fCurrentColorDict = [fGroups objectAtIndex: row];
180       
181        NSColorPanel * colorPanel = [NSColorPanel sharedColorPanel];
182        [colorPanel setContinuous: YES];
183        [colorPanel setColor: [[fGroups objectAtIndex: row] objectForKey: @"Color"]];
184       
185        [colorPanel setTarget: self];
186        [colorPanel setAction: @selector(changeColor:)];
187       
188        [colorPanel orderFront: self];
189    }
190    else;
191}
192
193- (void) tableViewSelectionDidChange: (NSNotification *) notification
194{
195    [fAddRemoveControl setEnabled: [fTableView numberOfSelectedRows] > 0 forSegment: REMOVE_TAG];
196}
197
198- (void) addRemoveGroup: (id) sender
199{
200    NSEnumerator * enumerator;
201    NSDictionary * dict;
202    int index;
203    BOOL found;
204    NSIndexSet * rowIndexes;
205    NSMutableIndexSet * indexes;
206   
207    switch ([[sender cell] tagForSegment: [sender selectedSegment]])
208    {
209        case ADD_TAG:
210           
211            //find the lowest index
212            for (index = 0; index < [fGroups count]; index++)
213            {
214                found = NO;
215                enumerator = [fGroups objectEnumerator];
216                while ((dict = [enumerator nextObject]))
217                    if ([[dict objectForKey: @"Index"] intValue] == index)
218                    {
219                        found = YES;
220                        break;
221                    }
222               
223                if (!found)
224                    break;
225            }
226           
227            [fGroups addObject: [NSMutableDictionary dictionaryWithObjectsAndKeys: [NSNumber numberWithInt: index], @"Index",
228                                    [NSColor cyanColor], @"Color", @"", @"Name", nil]];
229            [fTableView reloadData];
230            [fTableView deselectAll: self];
231           
232            [fTableView editColumn: [fTableView columnWithIdentifier: @"Name"] row: [fTableView numberOfRows]-1 withEvent: nil
233                        select: NO];
234            break;
235       
236        case REMOVE_TAG:
237           
238            rowIndexes = [fTableView selectedRowIndexes];
239            indexes = [NSMutableIndexSet indexSet];
240            for (index = [rowIndexes firstIndex]; index != NSNotFound; index = [rowIndexes indexGreaterThanIndex: index])
241                [indexes addIndex: [[[fGroups objectAtIndex: index] objectForKey: @"Index"] intValue]];
242           
243            [fGroups removeObjectsAtIndexes: rowIndexes];
244            [fTableView deselectAll: self];
245            [fTableView reloadData];
246           
247            [[NSNotificationCenter defaultCenter] postNotificationName: @"GroupValueRemoved" object: self userInfo:
248                [NSDictionary dictionaryWithObject: indexes forKey: @"Indexes"]];
249            break;
250       
251        default:
252            return;
253    }
254   
255    [[NSNotificationCenter defaultCenter] postNotificationName: @"UpdateUI" object: self];
256    [self saveGroups];
257}
258
259- (NSMenu *) groupMenuWithTarget: (id) target action: (SEL) action
260{
261    NSMenu * menu = [[NSMenu alloc] initWithTitle: @"Groups"];
262   
263    NSMenuItem * item = [[NSMenuItem alloc] initWithTitle: @"None" action: action keyEquivalent: @""];
264    [item setTarget: target];
265    [item setRepresentedObject: [NSNumber numberWithInt: -1]];
266    [menu addItem: item];
267    [item release];
268   
269    [menu addItem: [NSMenuItem separatorItem]];
270   
271    NSBezierPath * bp = [NSBezierPath bezierPathWithRoundedRect: NSMakeRect(0.0, 0.0, 16.0, 16.0) radius: 4.0];
272   
273    NSEnumerator * enumerator = [fGroups objectEnumerator];
274    NSDictionary * dict;
275    while ((dict = [enumerator nextObject]))
276    {
277        item = [[NSMenuItem alloc] initWithTitle: [dict objectForKey: @"Name"] action: action keyEquivalent: @""];
278        [item setTarget: target];
279       
280        NSImage * icon = [[NSImage alloc] initWithSize: NSMakeSize(16.0, 16.0)];
281       
282        [icon lockFocus];
283        [[self gradientForColor: [dict objectForKey: @"Color"]] fillBezierPath: bp angle: 90];
284        [icon unlockFocus];
285       
286        [item setImage: icon];
287        [icon release];
288       
289        [item setRepresentedObject: [dict objectForKey: @"Index"]];
290       
291        [menu addItem: item];
292        [item release];
293    }
294   
295    return [menu autorelease];
296}
297
298@end
299
300@implementation GroupsWindowController (Private)
301
302- (void) saveGroups
303{
304    [[NSUserDefaults standardUserDefaults] setObject: [NSArchiver archivedDataWithRootObject: fGroups] forKey: @"Groups"];
305}
306
307- (CTGradient *) gradientForColor: (NSColor *) color
308{
309    return [CTGradient gradientWithBeginningColor: [color blendedColorWithFraction: 0.7 ofColor: [NSColor whiteColor]]
310            endingColor: [color blendedColorWithFraction: 0.4 ofColor: [NSColor whiteColor]]];
311}
312
313- (void) changeColor: (id) sender
314{
315    [fCurrentColorDict setObject: [sender color] forKey: @"Color"];
316    [fTableView reloadData];
317   
318    [self saveGroups];
319    [[NSNotificationCenter defaultCenter] postNotificationName: @"UpdateUI" object: self];
320}
321
322@end
Note: See TracBrowser for help on using the repository browser.