macos: update AFS prefs pane
[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         [tableViewCellIP setDelegate:self];
20         [tableViewCellIP setDataSource:self];
21 }
22
23 // -------------------------------------------------------------------------------
24 //  initWhitCell:
25 // -------------------------------------------------------------------------------
26 - (void) setWorkCell:(DBCellElement*)cell {
27         cellElement = cell;
28         currentSelectedIP = nil;
29 }
30
31 // -------------------------------------------------------------------------------
32 //  save:
33 // -------------------------------------------------------------------------------
34 - (IBAction) save:(id) sender
35 {
36         hasSaved = YES;
37         [self commitModify];
38         [NSApp endSheet:confPanel];
39 }
40
41 // -------------------------------------------------------------------------------
42 //  commitModify:
43 // -------------------------------------------------------------------------------
44 - (void) commitModify
45 {
46         //store the cell name
47         [cellElement setCellName:[textFieldCellName stringValue]];
48         [cellElement setCellComment:[textFieldComment stringValue]];
49         [bkIPArray removeAllObjects];
50         [bkIPArray setArray:workIPArray];
51         [workIPArray release];
52 }
53
54 // -------------------------------------------------------------------------------
55 //  cancel:
56 // -------------------------------------------------------------------------------
57 - (IBAction) cancel:(id) sender
58 {
59         hasSaved = NO;
60         [self rollbackModify];
61         [NSApp endSheet:confPanel];
62 }
63
64
65 // -------------------------------------------------------------------------------
66 //  rollbackModify:
67 // -------------------------------------------------------------------------------
68 - (void) rollbackModify
69 {
70         // take bkarray
71         [workIPArray release];
72         workIPArray = nil;
73 }
74
75 // -------------------------------------------------------------------------------
76 //  createNewIP:
77 // -------------------------------------------------------------------------------
78 - (IBAction) createNewIP:(id) sender
79 {
80         CellIp *ip = [[CellIp alloc] init];
81         [workIPArray addObject:ip];
82         [ip release];
83         currentSelectedIP = ip;
84         [tableViewCellIP reloadData];
85         [tableViewCellIP scrollRowToVisible:[[cellElement getIp] count]-1];
86 }
87
88 // -------------------------------------------------------------------------------
89 //  cancelIP:
90 // -------------------------------------------------------------------------------
91 - (IBAction) cancelIP:(id) sender
92 {
93         [workIPArray removeObjectAtIndex:[tableViewCellIP selectedRow]];
94         [tableViewCellIP deselectAll:nil];
95         [self manageTableSelection:-1];
96         [tableViewCellIP reloadData];
97
98 }
99
100 // -------------------------------------------------------------------------------
101 //  hasSaved:
102 // -------------------------------------------------------------------------------
103 - (BOOL)saved
104 {
105         return hasSaved;
106 }
107
108 // -------------------------------------------------------------------------------
109 //  loadValueFromCellIPClass:
110 // -------------------------------------------------------------------------------
111 - (void) loadValueFromCellIPClass
112 {
113         [textFieldCellName setStringValue:[cellElement getCellName]];
114         [textFieldComment setStringValue:[cellElement getCellComment]];
115         [tableViewCellIP reloadData];
116 }
117
118 // -------------------------------------------------------------------------------
119 //  manageTableSelection:
120 // -------------------------------------------------------------------------------
121 - (void) manageTableSelection:(int)row
122 {
123         //[((NSControl*) modifyButton) setEnabled:row>=0];
124         [deleteButton setEnabled:row>=0];
125 }
126
127 - (id) getPanel
128 {
129         return confPanel;
130 }
131 @end
132
133 //Windows Delegator
134 @implementation IpConfiguratorCommander(PanelDelegator)
135 // -------------------------------------------------------------------------------
136 //  windowDidBecomeKey:
137 // -------------------------------------------------------------------------------
138 - (void)windowDidBecomeKey:(NSNotification *)aNotification
139 {
140         if(!cellElement){
141                 [NSApp endSheet:confPanel];
142                 return;
143         }
144         
145         bkIPArray = [cellElement getIp];
146         workIPArray = [[NSMutableArray alloc] initWithArray:bkIPArray];
147         [self loadValueFromCellIPClass];
148 }
149
150 // -------------------------------------------------------------------------------
151 //  windowWillClose:
152 // -------------------------------------------------------------------------------
153 - (void)windowWillClose:(NSNotification *)aNotification 
154 {
155 }
156 @end
157
158 //Table datasource
159 @implementation IpConfiguratorCommander (NSTableDataSource)
160 // -------------------------------------------------------------------------------
161 //  tableView:
162 // -------------------------------------------------------------------------------
163 - (id)  tableView:(NSTableView *) aTableView
164         objectValueForTableColumn:(NSTableColumn *) aTableColumn
165                                                   row:(int) rowIndex
166 {  
167         
168         NSString *result = nil;
169         //NSMutableArray *cellArray = [cellElement getIp];
170         CellIp *ipElement =  (CellIp*)[workIPArray objectAtIndex:rowIndex];
171         NSString *identifier = (NSString*)[aTableColumn identifier];
172         switch([identifier intValue]){
173                 case 1:
174                         result = [ipElement getCellIp];
175                         break;
176                         
177                 case 2:
178                         result = [ipElement getCellComment];
179                         break;
180                         
181         }
182         return result;  
183 }
184
185 // -------------------------------------------------------------------------------
186 //  numberOfRowsInTableView:
187 // -------------------------------------------------------------------------------
188 - (int)numberOfRowsInTableView:(NSTableView *)aTableView
189 {
190         return [workIPArray count];  
191 }
192
193 - (void)tableView:(NSTableView *)aTable setObjectValue:(id)aData 
194    forTableColumn:(NSTableColumn *)aCol row:(int)aRow
195 {
196         CellIp *ipElement =  (CellIp*)[workIPArray objectAtIndex:aRow];
197         switch([[aCol identifier] intValue])
198         {
199                 case 1:
200                         [ipElement setCellIp:[aData description]];
201                         break;
202                         
203                 case 2:
204                         [ipElement setCellComment:[aData description]];
205                         break;
206         }
207 }
208 @end
209
210 // Table delegator
211 @implementation IpConfiguratorCommander (TableDelegate)
212 // -------------------------------------------------------------------------------
213 //  selectionShouldChangeInTableView:
214 // -------------------------------------------------------------------------------
215 - (BOOL)selectionShouldChangeInTableView:(NSTableView *)aTable
216 {
217         [self manageTableSelection:[aTable selectedRow]];
218         return YES;
219 }
220
221 // -------------------------------------------------------------------------------
222 //  tableView:
223 // -------------------------------------------------------------------------------
224 - (BOOL)tableView:(NSTableView *)aTable shouldSelectRow:(int)aRow
225 {
226         [self manageTableSelection:aRow];
227         return YES;
228 }
229 @end