~ubuntu-branches/ubuntu/maverick/transmission/maverick

« back to all changes in this revision

Viewing changes to macosx/GroupsWindowController.m

  • Committer: Bazaar Package Importer
  • Author(s): Krzysztof Klimonda
  • Date: 2009-05-22 21:57:30 UTC
  • mfrom: (1.2.2 upstream)
  • mto: (2.1.18 sid) (1.3.8 upstream)
  • mto: This revision was merged to the branch mainline in revision 36.
  • Revision ID: james.westby@ubuntu.com-20090522215730-ly5kgv5aw9ig2u82
Tags: upstream-1.61
ImportĀ upstreamĀ versionĀ 1.61

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/******************************************************************************
2
 
 * $Id: GroupsWindowController.m 7030 2008-11-04 00:34:13Z livings124 $
3
 
 *
4
 
 * Copyright (c) 2007-2008 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 "GroupsController.h"
27
 
#import "NSApplicationAdditions.h"
28
 
 
29
 
#define GROUP_TABLE_VIEW_DATA_TYPE @"GroupTableViewDataType"
30
 
 
31
 
#define ADD_TAG 0
32
 
#define REMOVE_TAG 1
33
 
 
34
 
@interface GroupsWindowController (Private)
35
 
 
36
 
- (void) changeColor: (id) sender;
37
 
 
38
 
@end
39
 
 
40
 
@implementation GroupsWindowController
41
 
 
42
 
GroupsWindowController * fGroupsWindowInstance = nil;
43
 
+ (GroupsWindowController *) groupsWindow
44
 
{
45
 
    if (!fGroupsWindowInstance)
46
 
        fGroupsWindowInstance = [[GroupsWindowController alloc] initWithWindowNibName: @"GroupsWindow"];
47
 
    return fGroupsWindowInstance;
48
 
}
49
 
 
50
 
- (void) awakeFromNib
51
 
{
52
 
    [[self window] setTitle: NSLocalizedString(@"Groups", "Groups -> window title")];
53
 
    [[[fTableView tableColumnWithIdentifier: @"Button"] dataCell] setTitle: NSLocalizedString(@"Color", "Groups -> color button")];
54
 
    
55
 
    [fTableView registerForDraggedTypes: [NSArray arrayWithObject: GROUP_TABLE_VIEW_DATA_TYPE]];
56
 
    
57
 
    if ([NSApp isOnLeopardOrBetter])
58
 
        [[self window] setContentBorderThickness: [[fTableView enclosingScrollView] frame].origin.y forEdge: NSMinYEdge];
59
 
    else
60
 
    {
61
 
        [fAddRemoveControl sizeToFit];
62
 
        [fAddRemoveControl setLabel: @"+" forSegment: ADD_TAG];
63
 
        [fAddRemoveControl setLabel: @"-" forSegment: REMOVE_TAG];
64
 
    }
65
 
    
66
 
    [fAddRemoveControl setEnabled: NO forSegment: REMOVE_TAG];
67
 
}
68
 
 
69
 
- (void) windowWillClose: (id) sender
70
 
{
71
 
    [[NSColorPanel sharedColorPanel] close];
72
 
    
73
 
        [fGroupsWindowInstance release];
74
 
    fGroupsWindowInstance = nil;
75
 
}
76
 
 
77
 
- (NSInteger) numberOfRowsInTableView: (NSTableView *) tableview
78
 
{
79
 
    return [[GroupsController groups] numberOfGroups];
80
 
}
81
 
 
82
 
- (id) tableView: (NSTableView *) tableView objectValueForTableColumn: (NSTableColumn *) tableColumn row: (NSInteger) row
83
 
{
84
 
    GroupsController * groupsController = [GroupsController groups];
85
 
    NSInteger groupsIndex = [groupsController indexForRow: row];
86
 
    
87
 
    NSString * identifier = [tableColumn identifier];
88
 
    if ([identifier isEqualToString: @"Color"])
89
 
        return [groupsController imageForIndex: groupsIndex];
90
 
    else
91
 
        return [groupsController nameForIndex: groupsIndex];
92
 
}
93
 
 
94
 
- (void) tableView: (NSTableView *) tableView setObjectValue: (id) object forTableColumn: (NSTableColumn *) tableColumn
95
 
    row: (NSInteger) row
96
 
{
97
 
    NSString * identifier = [tableColumn identifier];
98
 
    if ([identifier isEqualToString: @"Name"])
99
 
        [[GroupsController groups] setName: object forIndex: [[GroupsController groups] indexForRow: row]];
100
 
    else if ([identifier isEqualToString: @"Button"])
101
 
    {
102
 
        fCurrentColorIndex = [[GroupsController groups] indexForRow: row];
103
 
        
104
 
        NSColorPanel * colorPanel = [NSColorPanel sharedColorPanel];
105
 
        [colorPanel setContinuous: YES];
106
 
        [colorPanel setColor: [[GroupsController groups] colorForIndex: fCurrentColorIndex]];
107
 
        
108
 
        [colorPanel setTarget: self];
109
 
        [colorPanel setAction: @selector(changeColor:)];
110
 
        
111
 
        [colorPanel orderFront: self];
112
 
    }
113
 
    else;
114
 
}
115
 
 
116
 
- (void) tableViewSelectionDidChange: (NSNotification *) notification
117
 
{
118
 
    [fAddRemoveControl setEnabled: [fTableView numberOfSelectedRows] > 0 forSegment: REMOVE_TAG];
119
 
}
120
 
 
121
 
- (BOOL) tableView: (NSTableView *) tableView writeRowsWithIndexes: (NSIndexSet *) rowIndexes toPasteboard: (NSPasteboard *) pboard
122
 
{
123
 
    [pboard declareTypes: [NSArray arrayWithObject: GROUP_TABLE_VIEW_DATA_TYPE] owner: self];
124
 
    [pboard setData: [NSKeyedArchiver archivedDataWithRootObject: rowIndexes] forType: GROUP_TABLE_VIEW_DATA_TYPE];
125
 
    return YES;
126
 
}
127
 
 
128
 
- (NSDragOperation) tableView: (NSTableView *) tableView validateDrop: (id <NSDraggingInfo>) info
129
 
    proposedRow: (NSInteger) row proposedDropOperation: (NSTableViewDropOperation) operation
130
 
{
131
 
    NSPasteboard * pasteboard = [info draggingPasteboard];
132
 
    if ([[pasteboard types] containsObject: GROUP_TABLE_VIEW_DATA_TYPE])
133
 
    {
134
 
        [fTableView setDropRow: row dropOperation: NSTableViewDropAbove];
135
 
        return NSDragOperationGeneric;
136
 
    }
137
 
    
138
 
    return NSDragOperationNone;
139
 
}
140
 
 
141
 
- (BOOL) tableView: (NSTableView *) tableView acceptDrop: (id <NSDraggingInfo>) info row: (NSInteger) newRow
142
 
    dropOperation: (NSTableViewDropOperation) operation
143
 
{
144
 
    NSPasteboard * pasteboard = [info draggingPasteboard];
145
 
    if ([[pasteboard types] containsObject: GROUP_TABLE_VIEW_DATA_TYPE])
146
 
    {
147
 
        NSIndexSet * indexes = [NSKeyedUnarchiver unarchiveObjectWithData: [pasteboard dataForType: GROUP_TABLE_VIEW_DATA_TYPE]],
148
 
            * selectedIndexes = [[GroupsController groups] moveGroupsAtRowIndexes: indexes toRow: newRow
149
 
                                        oldSelected: [fTableView selectedRowIndexes]];
150
 
        
151
 
        [fTableView selectRowIndexes: selectedIndexes byExtendingSelection: NO];
152
 
        [fTableView reloadData];
153
 
    }
154
 
    
155
 
    return YES;
156
 
}
157
 
 
158
 
- (void) addRemoveGroup: (id) sender
159
 
{
160
 
    NSIndexSet * indexes;
161
 
    
162
 
    switch ([[sender cell] tagForSegment: [sender selectedSegment]])
163
 
    {
164
 
        case ADD_TAG:
165
 
            [[GroupsController groups] addNewGroup];
166
 
            
167
 
            [fTableView reloadData];
168
 
            [fTableView deselectAll: self];
169
 
            
170
 
            [fTableView editColumn: [fTableView columnWithIdentifier: @"Name"] row: [fTableView numberOfRows]-1 withEvent: nil
171
 
                        select: NO];
172
 
            break;
173
 
        
174
 
        case REMOVE_TAG:
175
 
            //close color picker if corresponding row is removed
176
 
            indexes = [fTableView selectedRowIndexes];
177
 
            if ([[NSColorPanel sharedColorPanel] isVisible]
178
 
                && [indexes containsIndex: [[GroupsController groups] rowValueForIndex: fCurrentColorIndex]])
179
 
                [[NSColorPanel sharedColorPanel] close];
180
 
            
181
 
            [[GroupsController groups] removeGroupWithRowIndexes: indexes];
182
 
            
183
 
            [fTableView deselectAll: self];
184
 
            [fTableView reloadData];
185
 
            
186
 
            break;
187
 
    }
188
 
}
189
 
 
190
 
@end
191
 
 
192
 
@implementation GroupsWindowController (Private)
193
 
 
194
 
- (void) changeColor: (id) sender
195
 
{
196
 
    [[GroupsController groups] setColor: [sender color] forIndex: fCurrentColorIndex];
197
 
    [fTableView reloadData];
198
 
}
199
 
 
200
 
@end