File: PreferencesWindowController.m

package info (click to toggle)
poe.app 0.5.1-3
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 560 kB
  • ctags: 53
  • sloc: objc: 1,272; ansic: 438; makefile: 40
file content (376 lines) | stat: -rw-r--r-- 10,476 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
/*
    PreferencesWindowController.m - Preferences window controller for Poe.app
    Copyright (C) 2003,2004,2005 Rob Burns

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02111, USA.
*/

#include "PreferencesWindowController.h"

static PreferencesWindowController *singleInstance = nil;

@implementation PreferencesWindowController

- (void) dealloc
{
  RELEASE(genreView);
  RELEASE(addButton);
  RELEASE(deleteButton);
  RELEASE(genreBrowser);
  RELEASE(genreText);

  RELEASE(_commentTags);
  RELEASE(_genres);

  RELEASE(label);
  
  [super dealloc];
} 

- (id) init
{
  self = [super initWithWindowNibName: @"Preferences" owner: self];

  [[self window] setTitle: _(@"Preferences")];
  [label setStringValue: _(@"Default comments to use:")];
  [tagDesc setString: _(@"If you find you want to use a tag that you haven't set to default, you can add any of the comment tags to a file, by selecting 'Add comment' from the 'Edit' menu when you are editing a file.")];

  [[self window] setFrameAutosaveName: @"Preferences"];
  [[self window] setFrameUsingName: @"Preferences"];

  [self _setupData];

  [self _setupTable];

  [self _setupGenreView];

  return self;
}

+ (id) singleInstance
{
  if( !singleInstance )
    {
      singleInstance = [[PreferencesWindowController alloc] init];
    }

  return singleInstance;
}

- (void) addGenre: (id)sender
{
  NSMutableArray *temp;

  temp = [[NSMutableArray alloc] initWithArray: 
    [[NSUserDefaults standardUserDefaults] arrayForKey: @"genresAdded"]];

  [temp addObject: [genreText stringValue]]; 
  [[NSUserDefaults standardUserDefaults]
    setObject: temp forKey: @"genresAdded"];

  [[NSUserDefaults standardUserDefaults] synchronize];

  [_genres insertObjectAlphabetically: [genreText stringValue]];

  [[NSNotificationCenter defaultCenter]
    postNotificationName: @"GenresDidChange" object: nil];

  RELEASE(temp);

  [genreBrowser reloadColumn: 0];
}

- (void) removeGenre: (id)sender;
{
  NSMutableArray *temp;
  NSString *theObject;
  int index;

  temp = [[NSMutableArray alloc] initWithArray:
    [[NSUserDefaults standardUserDefaults] arrayForKey: @"genresRemoved"]];

  theObject = [[genreBrowser selectedCell] stringValue];
	if (!theObject) return;

  index = [_genres indexOfObject: theObject];

  [temp addObject: theObject];
  [[NSUserDefaults standardUserDefaults]
    setObject: temp forKey: @"genresRemoved"];

  [[NSUserDefaults standardUserDefaults] synchronize];

  [_genres removeObjectAtIndex: index];

  [[NSNotificationCenter defaultCenter]
    postNotificationName: @"GenresDidChange" object: nil];

  RELEASE(temp);

  [genreBrowser reloadColumn: 0];
}

// Private methods
//***************** 

- (void) _setupData
{
  int i;

  NSArray *standardTags = [NSArray arrayWithArray: 
    [[Util singleInstance] tagsTitle]];
 
  NSArray *defaultTags = [[NSUserDefaults standardUserDefaults] 
    arrayForKey: @"defaultTags"];  
                                            
  _commentTags = [[NSMutableArray alloc] init];

  for(i=0;i<[standardTags count];i++)
    {
      if([defaultTags containsObject: [[Util singleInstance] tagAtIndex: i]])
        {
          [_commentTags addObject: [NSMutableDictionary 
            dictionaryWithObjectsAndKeys: [standardTags objectAtIndex: i], @"tag",
                                          [NSNumber numberWithInt: 1], @"used",
                                          nil]];
        }
      else
        {
          [_commentTags addObject: [NSMutableDictionary
            dictionaryWithObjectsAndKeys: [standardTags objectAtIndex: i], @"tag",
                                          [NSNumber numberWithInt: 0], @"used",
                                          nil]];
        }
    }

  _genres = [[NSMutableArray alloc] initWithArray: [[Util singleInstance] genres]];
}

- (void) _setupTable
{
  ExtendedTableColumn *tags,*used;
  NSButtonCell *cell;

  tags = AUTORELEASE([[NSTableColumn alloc] initWithIdentifier: @"tags"]);
  [tags setEditable: NO];
  [tags setWidth: 120];

  used = AUTORELEASE([[ExtendedTableColumn alloc] initWithIdentifier: @"used"]);
  [used setShouldUseAndSetState: YES];
  [used setShouldUseMouse: YES];
  [used setEditable: NO];
  [used setWidth: 40];
  cell = AUTORELEASE([[NSButtonCell alloc] init]);
  [cell setButtonType: NSSwitchButton];
  [cell setImagePosition: NSImageOnly];
  [used setDataCell: cell];

  tagsTable = [[SwitchTableView alloc] init];
  [tagsTable setDataSource: self];
  [tagsTable setDelegate: self];

  [tagsTable addTableColumn: tags];
  [tagsTable addTableColumn: used];

  [tagsTable setAllowsMultipleSelection: NO];
  [tagsTable setAllowsColumnSelection: NO];
  [tagsTable setDrawsGrid: NO];

  [tagsTable setRowHeight: [[NSFont systemFontOfSize:
    [NSFont systemFontSize]] boundingRectForFont].size.height+5];

  [tagsTableSV setDocumentView: tagsTable];
  [tagsTableSV setHasHorizontalScroller: NO];
  [tagsTableSV setHasVerticalScroller: YES];
  [tagsTableSV setBorderType: NSBezelBorder];
  
  [tagsTable setCornerView: nil];
  [tagsTable setHeaderView: nil];
}

- (void) _setupGenreView
{
  genreView = [[NSView alloc] initWithFrame: 
    NSMakeRect(0,0,228,133)];

  genreText = AUTORELEASE([[NSTextField alloc] initWithFrame:
    NSMakeRect(0,0,170,24)]);
  [genreText setTarget: self];
  [genreText setAction: @selector(addGenre:)];

  addButton = AUTORELEASE([[NSButton alloc] initWithFrame:
    NSMakeRect(175,0,24,24)]);
  [addButton setImagePosition: NSImageOnly];
  [addButton setImage: [NSImage imageNamed: @"B_add"]];
  [addButton setAction: @selector(addGenre:)];

  deleteButton = AUTORELEASE([[NSButton alloc] initWithFrame:
    NSMakeRect(204,0,24,24)]);
  [deleteButton setImagePosition: NSImageOnly];
  [deleteButton setImage: [NSImage imageNamed: @"B_remove"]];
  [deleteButton setAction: @selector(removeGenre:)];

  genreBrowser = AUTORELEASE([[NSBrowser alloc] initWithFrame:
    NSMakeRect(0,29,228,105)]);
  [genreBrowser setDelegate: self];
  [genreBrowser setTitled: NO];
  [genreBrowser setHasHorizontalScroller: NO];
  [genreBrowser setTarget:self];
  [genreBrowser setAction:@selector(click:)];
  [genreBrowser setMaxVisibleColumns:1];
  [genreBrowser setAllowsMultipleSelection:NO];
  [genreBrowser loadColumnZero];

  [genreBrowser setNextKeyView: genreText];
  [genreText setNextKeyView: addButton];
  [addButton setNextKeyView: deleteButton];
  [deleteButton setNextKeyView: genreBrowser];

  [genreView addSubview: genreText];
  [genreView addSubview: addButton];
  [genreView addSubview: deleteButton];
  [genreView addSubview: genreBrowser];
}

// NSTable data source protocol methods
//**************************************

- (int) numberOfRowsInTableView: (NSTableView *)aTableView;
{
  return [_commentTags count];
}

- (id)            tableView: (NSTableView *)aTableView
  objectValueForTableColumn: (NSTableColumn *)aTableColumn
                        row: (int)rowIndex;
{
  if(aTableColumn != nil)
    {
      if([[aTableColumn identifier] isEqualToString: @"tags"])
        {
          return [[_commentTags objectAtIndex: rowIndex] objectForKey: @"tag"];
        }
    }

  return nil;
}


// SwitchTableView data source protocol methods
//**********************************************

- (int)    tableView: (NSTableView *)aTableView
 stateForTableColumn: (NSTableColumn *)aTableColumn
                 row: (int)rowIndex;
{
  if(aTableColumn != nil)
    {
      if([[aTableColumn identifier] isEqualToString: @"used"])
        {
          return [[[_commentTags objectAtIndex: rowIndex] objectForKey: @"used"] intValue];
        }
    }

  return 0;
}

- (void)   tableView: (NSTableView *)aTableView
            setState: (int)aState
      forTableColumn: (NSTableColumn *)aTableColumn
                 row: (int)rowIndex;
{
  NSMutableArray *dt;
  NSUserDefaults *defaults;
  NSString *item;

  defaults = [NSUserDefaults standardUserDefaults];
  dt = [NSMutableArray arrayWithArray: [defaults arrayForKey: @"defaultTags"]];
  item = [[Util singleInstance] tagAtIndex: rowIndex];

  if(aTableColumn !=nil && rowIndex >= 0)
    {
      if([[aTableColumn identifier] isEqualToString: @"used"])
        {
          [[_commentTags objectAtIndex: rowIndex] setObject:
            [NSNumber numberWithInt: aState] forKey: @"used"];

          if([dt containsObject: item])
            {
              [dt removeObject: item];
              [defaults setObject: dt forKey: @"defaultTags"];
            }
          else
            {
              [dt addObject: item];
              [defaults setObject: dt forKey: @"defaultTags"];
            }
          [defaults synchronize];
        }
    }
}

// NSTableView delegate methods
//******************************

- (void)tableViewSelectionIsChanging:(NSNotification *)aNotification
{
  if([[[Util singleInstance] tagAtIndex: [tagsTable selectedRow]] 
    isEqualToString: @"GENRE"])
    {
      descView = RETAIN([box contentView]);
      [box setContentView: genreView];
      [box display];
      gvFlag = 1;
    }
  else if(gvFlag)
    {
      RETAIN(genreView);
      [box setContentView: descView];
      [tagDesc setString: [[Util singleInstance] 
        descriptionAtIndex: [tagsTable selectedRow]]];
      [box display];
      gvFlag = 0;
    }
  else
    {
      [tagDesc setString: [[Util singleInstance]
        descriptionAtIndex: [tagsTable selectedRow]]];
    }
}

// NSBrowser delegate methods
//****************************

- (int)browser:(NSBrowser *)sender numberOfRowsInColumn:(int)column
{
  if(sender == genreBrowser)
    {
      return [_genres count];
    }

  return 0;
}

- (void)browser:(NSBrowser *)sender willDisplayCell:(id)cell
  atRow:(int)row column:(int)column
{
  if(sender == genreBrowser)
    {
      [cell setLeaf: YES];
      [cell setStringValue: [_genres objectAtIndex: row]];
    }
}

@end