source: trunk/macosx/GroupsWindowController.m @ 4188

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

when table values are changed, reload the table immediately

File size: 11.6 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- (NSInteger) numberOfRowsInTableView: (NSTableView *) tableview
142{
143    return [fGroups count];
144}
145
146- (id) tableView: (NSTableView *) tableView objectValueForTableColumn: (NSTableColumn *) tableColumn row: (NSInteger) row
147{
148    #warning consider color column ?
149    NSString * identifier = [tableColumn identifier];
150    if ([identifier isEqualToString: @"Color"])
151        return [self gradientForColor: [[fGroups objectAtIndex: row] objectForKey: @"Color"]];
152    else
153        return [[fGroups objectAtIndex: row] objectForKey: @"Name"];
154}
155
156- (void) tableView: (NSTableView *) tableView setObjectValue: (id) object forTableColumn: (NSTableColumn *) tableColumn
157    row: (NSInteger) row
158{
159    NSString * identifier = [tableColumn identifier];
160    if ([identifier isEqualToString: @"Name"])
161    {
162        [[fGroups objectAtIndex: row] setObject: object forKey: @"Name"];
163        [self saveGroups];
164    }
165    else if ([identifier isEqualToString: @"Button"])
166    {
167        fCurrentColorDict = [fGroups objectAtIndex: row];
168       
169        NSColorPanel * colorPanel = [NSColorPanel sharedColorPanel];
170        [colorPanel setContinuous: YES];
171        [colorPanel setColor: [[fGroups objectAtIndex: row] objectForKey: @"Color"]];
172       
173        [colorPanel setTarget: self];
174        [colorPanel setAction: @selector(changeColor:)];
175       
176        [colorPanel orderFront: self];
177    }
178    else;
179}
180
181- (void) tableViewSelectionDidChange: (NSNotification *) notification
182{
183    [fAddRemoveControl setEnabled: [fTableView numberOfSelectedRows] > 0 forSegment: REMOVE_TAG];
184}
185
186- (void) addRemoveGroup: (id) sender
187{
188    NSEnumerator * enumerator;
189    NSDictionary * dict;
190    int index;
191    BOOL found;
192    NSIndexSet * rowIndexes;
193    NSMutableIndexSet * indexes;
194   
195    switch ([[sender cell] tagForSegment: [sender selectedSegment]])
196    {
197        case ADD_TAG:
198           
199            //find the lowest index
200            for (index = 0; index < [fGroups count]; index++)
201            {
202                found = NO;
203                enumerator = [fGroups objectEnumerator];
204                while ((dict = [enumerator nextObject]))
205                    if ([[dict objectForKey: @"Index"] intValue] == index)
206                    {
207                        found = YES;
208                        break;
209                    }
210               
211                if (!found)
212                    break;
213            }
214           
215            [fGroups addObject: [NSMutableDictionary dictionaryWithObjectsAndKeys: [NSNumber numberWithInt: index], @"Index",
216                                    [NSColor cyanColor], @"Color", @"", @"Name", nil]];
217            [fTableView reloadData];
218            [fTableView deselectAll: self];
219           
220            [fTableView editColumn: [fTableView columnWithIdentifier: @"Name"] row: [fTableView numberOfRows]-1 withEvent: nil
221                        select: NO];
222            break;
223       
224        case REMOVE_TAG:
225           
226            rowIndexes = [fTableView selectedRowIndexes];
227            indexes = [NSMutableIndexSet indexSet];
228            for (index = [rowIndexes firstIndex]; index != NSNotFound; index = [rowIndexes indexGreaterThanIndex: index])
229                [indexes addIndex: [[[fGroups objectAtIndex: index] objectForKey: @"Index"] intValue]];
230           
231            [fGroups removeObjectsAtIndexes: rowIndexes];
232            [fTableView deselectAll: self];
233            [fTableView reloadData];
234           
235            [[NSNotificationCenter defaultCenter] postNotificationName: @"GroupValueRemoved" object: self userInfo:
236                [NSDictionary dictionaryWithObject: indexes forKey: @"Indexes"]];
237            break;
238       
239        default:
240            return;
241    }
242   
243    [[NSNotificationCenter defaultCenter] postNotificationName: @"UpdateUI" object: self];
244    [self saveGroups];
245}
246
247- (NSMenu *) groupMenuWithTarget: (id) target action: (SEL) action
248{
249    NSMenu * menu = [[NSMenu alloc] initWithTitle: @"Groups"];
250   
251    NSMenuItem * item = [[NSMenuItem alloc] initWithTitle: @"None" action: action keyEquivalent: @""];
252    [item setTarget: target];
253    [item setRepresentedObject: [NSNumber numberWithInt: -1]];
254    [menu addItem: item];
255    [item release];
256   
257    [menu addItem: [NSMenuItem separatorItem]];
258   
259    NSBezierPath * bp = [NSBezierPath bezierPathWithRoundedRect: NSMakeRect(0.0, 0.0, 16.0, 16.0) radius: 4.0];
260   
261    NSEnumerator * enumerator = [fGroups objectEnumerator];
262    NSDictionary * dict;
263    while ((dict = [enumerator nextObject]))
264    {
265        item = [[NSMenuItem alloc] initWithTitle: [dict objectForKey: @"Name"] action: action keyEquivalent: @""];
266        [item setTarget: target];
267       
268        NSImage * icon = [[NSImage alloc] initWithSize: NSMakeSize(16.0, 16.0)];
269       
270        [icon lockFocus];
271        [[self gradientForColor: [dict objectForKey: @"Color"]] fillBezierPath: bp angle: 90];
272        [icon unlockFocus];
273       
274        [item setImage: icon];
275        [icon release];
276       
277        [item setRepresentedObject: [dict objectForKey: @"Index"]];
278       
279        [menu addItem: item];
280        [item release];
281    }
282   
283    return [menu autorelease];
284}
285
286@end
287
288@implementation GroupsWindowController (Private)
289
290- (void) saveGroups
291{
292    [[NSUserDefaults standardUserDefaults] setObject: [NSArchiver archivedDataWithRootObject: fGroups] forKey: @"Groups"];
293}
294
295- (CTGradient *) gradientForColor: (NSColor *) color
296{
297    return [CTGradient gradientWithBeginningColor: [color blendedColorWithFraction: 0.7 ofColor: [NSColor whiteColor]]
298            endingColor: [color blendedColorWithFraction: 0.4 ofColor: [NSColor whiteColor]]];
299}
300
301- (void) changeColor: (id) sender
302{
303    [fCurrentColorDict setObject: [sender color] forKey: @"Color"];
304    [fTableView reloadData];
305   
306    [self saveGroups];
307    [[NSNotificationCenter defaultCenter] postNotificationName: @"UpdateUI" object: self];
308}
309
310@end
Note: See TracBrowser for help on using the repository browser.