1
/******************************************************************************
2
* $Id: GroupsWindowController.m 7030 2008-11-04 00:34:13Z livings124 $
4
* Copyright (c) 2007-2008 Transmission authors and contributors
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:
13
* The above copyright notice and this permission notice shall be included in
14
* all copies or substantial portions of the Software.
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
*****************************************************************************/
25
#import "GroupsWindowController.h"
26
#import "GroupsController.h"
27
#import "NSApplicationAdditions.h"
29
#define GROUP_TABLE_VIEW_DATA_TYPE @"GroupTableViewDataType"
34
@interface GroupsWindowController (Private)
36
- (void) changeColor: (id) sender;
40
@implementation GroupsWindowController
42
GroupsWindowController * fGroupsWindowInstance = nil;
43
+ (GroupsWindowController *) groupsWindow
45
if (!fGroupsWindowInstance)
46
fGroupsWindowInstance = [[GroupsWindowController alloc] initWithWindowNibName: @"GroupsWindow"];
47
return fGroupsWindowInstance;
52
[[self window] setTitle: NSLocalizedString(@"Groups", "Groups -> window title")];
53
[[[fTableView tableColumnWithIdentifier: @"Button"] dataCell] setTitle: NSLocalizedString(@"Color", "Groups -> color button")];
55
[fTableView registerForDraggedTypes: [NSArray arrayWithObject: GROUP_TABLE_VIEW_DATA_TYPE]];
57
if ([NSApp isOnLeopardOrBetter])
58
[[self window] setContentBorderThickness: [[fTableView enclosingScrollView] frame].origin.y forEdge: NSMinYEdge];
61
[fAddRemoveControl sizeToFit];
62
[fAddRemoveControl setLabel: @"+" forSegment: ADD_TAG];
63
[fAddRemoveControl setLabel: @"-" forSegment: REMOVE_TAG];
66
[fAddRemoveControl setEnabled: NO forSegment: REMOVE_TAG];
69
- (void) windowWillClose: (id) sender
71
[[NSColorPanel sharedColorPanel] close];
73
[fGroupsWindowInstance release];
74
fGroupsWindowInstance = nil;
77
- (NSInteger) numberOfRowsInTableView: (NSTableView *) tableview
79
return [[GroupsController groups] numberOfGroups];
82
- (id) tableView: (NSTableView *) tableView objectValueForTableColumn: (NSTableColumn *) tableColumn row: (NSInteger) row
84
GroupsController * groupsController = [GroupsController groups];
85
NSInteger groupsIndex = [groupsController indexForRow: row];
87
NSString * identifier = [tableColumn identifier];
88
if ([identifier isEqualToString: @"Color"])
89
return [groupsController imageForIndex: groupsIndex];
91
return [groupsController nameForIndex: groupsIndex];
94
- (void) tableView: (NSTableView *) tableView setObjectValue: (id) object forTableColumn: (NSTableColumn *) tableColumn
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"])
102
fCurrentColorIndex = [[GroupsController groups] indexForRow: row];
104
NSColorPanel * colorPanel = [NSColorPanel sharedColorPanel];
105
[colorPanel setContinuous: YES];
106
[colorPanel setColor: [[GroupsController groups] colorForIndex: fCurrentColorIndex]];
108
[colorPanel setTarget: self];
109
[colorPanel setAction: @selector(changeColor:)];
111
[colorPanel orderFront: self];
116
- (void) tableViewSelectionDidChange: (NSNotification *) notification
118
[fAddRemoveControl setEnabled: [fTableView numberOfSelectedRows] > 0 forSegment: REMOVE_TAG];
121
- (BOOL) tableView: (NSTableView *) tableView writeRowsWithIndexes: (NSIndexSet *) rowIndexes toPasteboard: (NSPasteboard *) pboard
123
[pboard declareTypes: [NSArray arrayWithObject: GROUP_TABLE_VIEW_DATA_TYPE] owner: self];
124
[pboard setData: [NSKeyedArchiver archivedDataWithRootObject: rowIndexes] forType: GROUP_TABLE_VIEW_DATA_TYPE];
128
- (NSDragOperation) tableView: (NSTableView *) tableView validateDrop: (id <NSDraggingInfo>) info
129
proposedRow: (NSInteger) row proposedDropOperation: (NSTableViewDropOperation) operation
131
NSPasteboard * pasteboard = [info draggingPasteboard];
132
if ([[pasteboard types] containsObject: GROUP_TABLE_VIEW_DATA_TYPE])
134
[fTableView setDropRow: row dropOperation: NSTableViewDropAbove];
135
return NSDragOperationGeneric;
138
return NSDragOperationNone;
141
- (BOOL) tableView: (NSTableView *) tableView acceptDrop: (id <NSDraggingInfo>) info row: (NSInteger) newRow
142
dropOperation: (NSTableViewDropOperation) operation
144
NSPasteboard * pasteboard = [info draggingPasteboard];
145
if ([[pasteboard types] containsObject: GROUP_TABLE_VIEW_DATA_TYPE])
147
NSIndexSet * indexes = [NSKeyedUnarchiver unarchiveObjectWithData: [pasteboard dataForType: GROUP_TABLE_VIEW_DATA_TYPE]],
148
* selectedIndexes = [[GroupsController groups] moveGroupsAtRowIndexes: indexes toRow: newRow
149
oldSelected: [fTableView selectedRowIndexes]];
151
[fTableView selectRowIndexes: selectedIndexes byExtendingSelection: NO];
152
[fTableView reloadData];
158
- (void) addRemoveGroup: (id) sender
160
NSIndexSet * indexes;
162
switch ([[sender cell] tagForSegment: [sender selectedSegment]])
165
[[GroupsController groups] addNewGroup];
167
[fTableView reloadData];
168
[fTableView deselectAll: self];
170
[fTableView editColumn: [fTableView columnWithIdentifier: @"Name"] row: [fTableView numberOfRows]-1 withEvent: nil
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];
181
[[GroupsController groups] removeGroupWithRowIndexes: indexes];
183
[fTableView deselectAll: self];
184
[fTableView reloadData];
192
@implementation GroupsWindowController (Private)
194
- (void) changeColor: (id) sender
196
[[GroupsController groups] setColor: [sender color] forIndex: fCurrentColorIndex];
197
[fTableView reloadData];