OSX Preference Pane Source Import
[openafs.git] / src / platform / DARWIN / AFSPreference / IpConfiguratorCommander.m
1 //
2 //  IpConfiguratorCommander.m
3 //  AFSCommander
4 //
5 //  Created by Claudio Bisegni on 18/06/07.
6 //  Copyright 2007 INFN - National Institute of Nuclear Physics. All rights reserved.
7 //
8
9 #import "IpConfiguratorCommander.h"
10 #import "AFSCommanderPref.h"
11
12 @implementation IpConfiguratorCommander
13
14 // -------------------------------------------------------------------------------
15 //  awakeFromNib:
16 // -------------------------------------------------------------------------------
17 - (void)awakeFromNib
18 {
19         NSLog(@"awakeFromNib");
20         [((NSTableView*)tableViewCellIP) setDelegate:self];
21         [((NSTableView*)tableViewCellIP) setDataSource:self];
22 }
23
24 // -------------------------------------------------------------------------------
25 //  initWhitCell:
26 // -------------------------------------------------------------------------------
27 - (void) setWorkCell:(DBCellElement*)cell {
28         cellElement = cell;
29         currentSelectedIP = nil;
30 }
31
32 // -------------------------------------------------------------------------------
33 //  save:
34 // -------------------------------------------------------------------------------
35 - (IBAction) save:(id) sender
36 {
37         hasSaved = YES;
38         [self commitModify];
39         [NSApp endSheet:confPanel];
40 }
41
42 // -------------------------------------------------------------------------------
43 //  commitModify:
44 // -------------------------------------------------------------------------------
45 - (void) commitModify
46 {
47         //store the cell name
48         [cellElement setCellName:[((NSControl*) textFieldCellName) stringValue]];
49         [cellElement setCellComment:[((NSControl*) textFieldComment) stringValue]];
50         
51         NSLog(@"Deleting:\n%s",[[bkIPArray description] cString]);
52         [bkIPArray removeAllObjects];
53         NSLog(@"deleted:\n");
54         NSLog(@"Coping:\n%s",[[workIPArray description] cString]);
55         [bkIPArray setArray:workIPArray];
56         NSLog(@"Copied:\n%s",[[workIPArray description] cString]);
57         NSLog(@"Releasing:\n%s",[[workIPArray description] cString]);
58         [workIPArray release];
59         NSLog(@"Released:\n%s",[[workIPArray description] cString]);
60 }
61
62 // -------------------------------------------------------------------------------
63 //  cancel:
64 // -------------------------------------------------------------------------------
65 - (IBAction) cancel:(id) sender
66 {
67         hasSaved = NO;
68         [self rollbackModify];
69         [NSApp endSheet:confPanel];
70 }
71
72
73 // -------------------------------------------------------------------------------
74 //  rollbackModify:
75 // -------------------------------------------------------------------------------
76 - (void) rollbackModify
77 {
78         // take bkarray
79         [workIPArray release];
80         workIPArray = nil;
81 }
82
83 // -------------------------------------------------------------------------------
84 //  createNewIP:
85 // -------------------------------------------------------------------------------
86 - (IBAction) createNewIP:(id) sender
87 {
88         CellIp *ip = [[CellIp alloc] init];
89         [workIPArray addObject:ip];
90         [ip release];
91         currentSelectedIP = ip;
92         [((NSTableView*)tableViewCellIP) reloadData];
93         [((NSTableView *) tableViewCellIP)  scrollRowToVisible:[[cellElement getIp] count]-1];
94 }
95
96 // -------------------------------------------------------------------------------
97 //  cancelIP:
98 // -------------------------------------------------------------------------------
99 - (IBAction) cancelIP:(id) sender
100 {
101         [workIPArray removeObjectAtIndex:[((NSTableView*)tableViewCellIP) selectedRow]];
102         [((NSTableView*)tableViewCellIP) deselectAll:nil];
103         [self manageTableSelection:-1];
104         [((NSTableView*)tableViewCellIP) reloadData];
105
106 }
107
108 // -------------------------------------------------------------------------------
109 //  hasSaved:
110 // -------------------------------------------------------------------------------
111 - (BOOL)saved
112 {
113         return hasSaved;
114 }
115
116 // -------------------------------------------------------------------------------
117 //  loadValueFromCellIPClass:
118 // -------------------------------------------------------------------------------
119 - (void) loadValueFromCellIPClass
120 {
121         [((NSTextField*)textFieldCellName) setStringValue:[cellElement getCellName]]; 
122         [((NSTextField*)textFieldComment) setStringValue:[cellElement getCellComment]]; 
123         [((NSTableView*)tableViewCellIP) reloadData];
124 }
125
126 // -------------------------------------------------------------------------------
127 //  manageTableSelection:
128 // -------------------------------------------------------------------------------
129 - (void) manageTableSelection:(int)row
130 {
131         //[((NSControl*) modifyButton) setEnabled:row>=0];
132         [((NSControl*) deleteButton) setEnabled:row>=0];
133 }
134
135 - (id) getPanel
136 {
137         return confPanel;
138 }
139 @end
140
141 //Windows Delegator
142 @implementation IpConfiguratorCommander(PanelDelegator)
143 // -------------------------------------------------------------------------------
144 //  windowDidBecomeKey:
145 // -------------------------------------------------------------------------------
146 - (void)windowDidBecomeKey:(NSNotification *)aNotification
147 {
148         if(!cellElement){
149                 [NSApp endSheet:confPanel];
150                 return;
151         }
152         
153         bkIPArray = [cellElement getIp];
154         workIPArray = [[NSMutableArray alloc] initWithArray:bkIPArray];
155         [self loadValueFromCellIPClass];
156 }
157
158 // -------------------------------------------------------------------------------
159 //  windowWillClose:
160 // -------------------------------------------------------------------------------
161 - (void)windowWillClose:(NSNotification *)aNotification 
162 {
163 }
164 @end
165
166 //Table datasource
167 @implementation IpConfiguratorCommander (NSTableDataSource)
168 // -------------------------------------------------------------------------------
169 //  tableView:
170 // -------------------------------------------------------------------------------
171 - (id)  tableView:(NSTableView *) aTableView
172         objectValueForTableColumn:(NSTableColumn *) aTableColumn
173                                                   row:(int) rowIndex
174 {  
175         
176         NSString *result = nil;
177         //NSMutableArray *cellArray = [cellElement getIp];
178         CellIp *ipElement =  (CellIp*)[workIPArray objectAtIndex:rowIndex];
179         NSString *identifier = (NSString*)[aTableColumn identifier];
180         switch([identifier intValue]){
181                 case 1:
182                         result = [ipElement getCellIp];
183                         break;
184                         
185                 case 2:
186                         result = [ipElement getCellComment];
187                         break;
188                         
189         }
190         return result;  
191 }
192
193 // -------------------------------------------------------------------------------
194 //  numberOfRowsInTableView:
195 // -------------------------------------------------------------------------------
196 - (int)numberOfRowsInTableView:(NSTableView *)aTableView
197 {
198         return [workIPArray count];  
199 }
200
201 - (void)tableView:(NSTableView *)aTable setObjectValue:(id)aData 
202    forTableColumn:(NSTableColumn *)aCol row:(int)aRow
203 {
204         NSLog([aData description]);
205         CellIp *ipElement =  (CellIp*)[workIPArray objectAtIndex:aRow];
206         switch([((NSNumber*)[aCol identifier]) intValue])
207         {
208                 case 1:
209                         [ipElement setCellIp:[aData description]];
210                         break;
211                         
212                 case 2:
213                         [ipElement setCellComment:[aData description]];
214                         break;
215         }
216 }
217 @end
218
219 // Table delegator
220 @implementation IpConfiguratorCommander (TableDelegate)
221 // -------------------------------------------------------------------------------
222 //  selectionShouldChangeInTableView:
223 // -------------------------------------------------------------------------------
224 - (BOOL)selectionShouldChangeInTableView:(NSTableView *)aTable
225 {
226         [self manageTableSelection:[aTable selectedRow]];
227         return YES;
228 }
229
230 // -------------------------------------------------------------------------------
231 //  tableView:
232 // -------------------------------------------------------------------------------
233 - (BOOL)tableView:(NSTableView *)aTable shouldSelectRow:(int)aRow
234 {
235         [self manageTableSelection:aRow];
236         return YES;
237 }
238 @end