File: Order.m

package info (click to toggle)
paje.app 1.98-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 4,428 kB
  • sloc: objc: 24,517; ansic: 6,998; makefile: 134; sh: 42; java: 31
file content (408 lines) | stat: -rw-r--r-- 11,449 bytes parent folder | download | duplicates (3)
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
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
/*
    Copyright (c) 1998, 1999, 2000, 2001, 2003, 2004 Benhur Stein
    
    This file is part of Paj.

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

    Paj 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 Lesser General Public License
    for more details.

    You should have received a copy of the GNU Lesser General Public License
    along with Paj; if not, write to the Free Software Foundation, Inc.,
    59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
*/
/* Order.m created by benhur on 20-mar-2001 */

/*
 * A Paje filter to change the order of containers
 */

#include "Order.h"
#include "OrderKey.h"

@implementation Order

- (id)initWithController:(PajeTraceController *)c
{
    NSWindow *win;
    
    self = [super initWithController:c];

    if (self != nil) {
        containers = [[NSMutableDictionary alloc] init];

        if (![NSBundle loadNibNamed:@"Order" owner:self]) {
            NSRunAlertPanel(@"Order", @"Couldn't load interface file",
                            nil, nil, nil);
            return self;
        }

        win = [view window];

        // view is an NSBox. we need its contents.
        view = [[(NSBox *)view contentView] retain];

        hierarchyBrowser = [[HierarchyBrowser alloc] initWithFilter:self];
        [hierarchyBrowser setContainersOnly:YES];
        [[hierarchyBrowser view] setFrame:[browserBox frame]];
        [[browserBox superview] replaceSubview:browserBox
                                          with:[hierarchyBrowser view]];

        [self registerFilter:self];

#ifndef GNUSTEP
        [win release];
#endif

    }
    return self;
}

- (void)dealloc
{
    [containers release];
    [hierarchyBrowser release];
#ifndef __APPLE__
    [view release];
#endif
    [super dealloc];
}

- (NSView *)filterView
{
    return view;
}

- (NSString *)filterName
{
    return @"Order";
}

- (id)filterDelegate
{
    return self;
}

- (NSArray *)defaultsForKey:(OrderKey *)key
{
    NSString *defaultKey;
    
    defaultKey = [NSString stringWithFormat:@"%@ in %@ Order", 
                           [key entityType], [key container]];

    return [[NSUserDefaults standardUserDefaults] arrayForKey:defaultKey];
}

- (void)registerDefaultsForKey:(OrderKey *)key
{
    NSString *defaultKey;
    NSMutableArray *containerNames;
    NSEnumerator *containerEnum;
    PajeContainer *container;
    
    containerNames = [NSMutableArray array];
    containerEnum = [[containers objectForKey:key] objectEnumerator];
    
    while ((container = [containerEnum nextObject]) != nil) {
        [containerNames addObject:[container name]];
    }
    
    defaultKey = [NSString stringWithFormat:@"%@ in %@ Order", 
                           [key entityType], [key container]];

    [[NSUserDefaults standardUserDefaults] setObject:containerNames
                                              forKey:defaultKey];
}

//
// Notifications sent by other filters
//

- (PajeContainer *)removeContainerNamed:(NSString *)containerName
                              fromArray:(NSMutableArray *)array
{
    PajeContainer *container;
    int index;
    
    index = [array count];
    while (index > 0) {
        container = [array objectAtIndex:--index];
        if ([[container name] isEqual:containerName]) {
            [array removeObjectAtIndex:index];
            return container;
        }
    }
    return nil;
}

- (void)synchronizeKey:(OrderKey *)key
        toOrderedNames:(NSArray *)containerNames
{
    NSMutableArray *orderedContainers;
    NSMutableArray *allContainers;
    NSEnumerator *containerNamesEnum;
    NSString *containerName;
    
    orderedContainers = [NSMutableArray array];
    allContainers = [[[super enumeratorOfContainersTyped:[key entityType]
                                             inContainer:[key container]]
                                allObjects] mutableCopy];
    containerNamesEnum = [containerNames objectEnumerator];
    while ((containerName = [containerNamesEnum nextObject]) != nil) {
        PajeContainer *container;
        container = [self removeContainerNamed:containerName
                                     fromArray:allContainers];
        if (container != nil) {
            [orderedContainers addObject:container];
        }
    }
    [orderedContainers addObjectsFromArray:allContainers];
    [containers setObject:orderedContainers forKey:key];
    [allContainers release];
    //[self registerDefaultsForKey:key];
}

- (void)synchronizeKey:(OrderKey *)key
{
    NSArray *orderFromDefaults;
    
    orderFromDefaults = [self defaultsForKey:key];
    if (orderFromDefaults == nil) {
        return;
    }

    [self synchronizeKey:key toOrderedNames:orderFromDefaults];
}

- (void)dataChangedForEntityType:(PajeEntityType *)entityType
{
    if ([entityType isContainer]) {
        NSEnumerator *keyEnum;
        OrderKey *key;
        
        keyEnum = [containers keyEnumerator];
        while ((key = [keyEnum nextObject]) != nil) {
            if ([[key entityType] isEqual:entityType]) {
                [self synchronizeKey:key];
            }
        }
    }
    [super dataChangedForEntityType:entityType];
}

- (void)hierarchyChanged
{
    [containers removeAllObjects];
    [hierarchyBrowser refreshLastColumn];
    [super hierarchyChanged];
}

//
// Handle interface messages
//
- (void)moveSelectedContainersBy:(int)moveAmount
{
    PajeEntityType *containerType;
    PajeContainer *container;
    OrderKey *key;
    NSMutableArray *containerArray;
    NSArray *selectedContainers;
    PajeContainer *firstSelectedContainer;
    NSUInteger index, maxIndex;

    selectedContainers = [[self selectedContainers] allObjects];
    if ((selectedContainers == nil) || ([selectedContainers count] == 0)) {
        NSBeep();
        return;
    }
    firstSelectedContainer = [selectedContainers objectAtIndex:0];

    containerType = [super entityTypeForEntity:firstSelectedContainer];
    container = [super containerForEntity:firstSelectedContainer];
    key = [OrderKey keyWithEntityType:containerType container:container];
    containerArray = [containers objectForKey:key];

    // if not being filtered, initialize a filter for them.
    if (containerArray == nil) {
        NSEnumerator *unfilteredEnum;
        unfilteredEnum = [super enumeratorOfContainersTyped:containerType
                                                inContainer:container];
        containerArray = [NSMutableArray arrayWithArray:[unfilteredEnum allObjects]];
        [containers setObject:containerArray forKey:key];
    }

    index = [containerArray indexOfObjectIdenticalTo:firstSelectedContainer];
    if (index == NSNotFound) {
        [NSException raise:@"Order: Internal Inconsistency"
                    format:@"Container Not Found: %@", firstSelectedContainer];
    }
    index += moveAmount;
    maxIndex = [containerArray count] - [selectedContainers count];
    if (index > maxIndex) {
        index = maxIndex;
    }
    [containerArray removeObjectsInArray:selectedContainers];
    [containerArray replaceObjectsInRange:NSMakeRange(index, 0)
                     withObjectsFromArray:selectedContainers];

    [self setOrder:containerArray
 ofContainersTyped:containerType
       inContainer:container];
}

- (void)setOrder:(NSArray *)containerOrder
ofContainersTyped:(PajeEntityType *)containerType
     inContainer:(PajeContainer *)container
{
    OrderKey *key;
    key = [OrderKey keyWithEntityType:containerType container:container];
    [containers setObject:containerOrder forKey:key];
    [self registerDefaultsForKey:key];
    [hierarchyBrowser refreshLastColumn];
    [hierarchyBrowser selectContainers:[[self selectedContainers] allObjects]];
    [super orderChangedForContainerType:containerType];
}

- (void)containerSelectionChanged
{
    // TODO make last column the one that contains the selected containers
    [hierarchyBrowser refreshLastColumn];
    [hierarchyBrowser selectContainers:[[self selectedContainers] allObjects]];
    [super containerSelectionChanged];
}

- (void)hierarchyBrowserContainerSelected:(HierarchyBrowser *)browser 
{
    [super setSelectedContainers:[NSSet setWithArray:[browser selectedContainers]]];
}

- (IBAction)moveUp:(id)sender
{
    [self moveSelectedContainersBy:-1];
}

- (IBAction)moveDown:(id)sender
{
    [self moveSelectedContainersBy:1];
}


/*
- (void)entityTypeSelected:(id)sender
{
    [self synchronizeMatrix];
}

- (void)synchronizeMatrix
{
    [hierarchyBrowser setFilter:self];
}
*/

//
// interact with interface
//

/*
- (void)viewWillBeSelected:(NSView *)selectedView
// message sent by PajeController when a view will be selected for display
{
    [self synchronizeMatrix];        
}
*/

//
// Queries that are filtered
//
- (NSEnumerator *)enumeratorOfContainersTyped:(PajeContainerType *)entityType
                                  inContainer:(PajeContainer *)container
{
    OrderKey *key;
    NSArray *containerArray;

    if (container == nil) {
        return [super enumeratorOfContainersTyped:entityType
                                      inContainer:container];
    }

    key = [OrderKey keyWithEntityType:entityType container:container];
    containerArray = [containers objectForKey:key];
    if (containerArray == nil) {
        [self synchronizeKey:key];
        containerArray = [containers objectForKey:key];
    }
    
    if (containerArray == nil) {
        return [super enumeratorOfContainersTyped:entityType
                                      inContainer:container];
    } else {
        return [containerArray objectEnumerator];
    }
}

- (id)configuration
{
    return containers;
}

- (OrderKey *)keyFromDescription:(NSString *)description
{
    NSArray *keyAsArray;
    PajeEntityType *entityType;
    PajeContainer *container;

    NS_DURING
        keyAsArray = [description propertyList];
    NS_HANDLER
        NS_VALUERETURN(nil, id);
    NS_ENDHANDLER

    if (![keyAsArray isKindOfClass:[NSArray class]]
        || [keyAsArray count] != 2) {
        return nil;
    }

    entityType = [self entityTypeWithName:[keyAsArray objectAtIndex:0]];
    if (entityType == nil) {
        return nil;
    }
    
    container = [self containerWithName:[keyAsArray objectAtIndex:1]
                                   type:[self containerTypeForType:entityType]];
    if (container == nil) {
        return nil;
    }

    return [OrderKey keyWithEntityType:entityType container:container];
}

- (void)setConfiguration:(id)cfg
{
    NSEnumerator *keyDescriptionEnumerator;
    NSString *keyDescription;
    OrderKey *key;
    NSDictionary *config;

    config = cfg;
    if (![config isKindOfClass:[NSDictionary class]]) {
        return;
    }

    keyDescriptionEnumerator = [config keyEnumerator];
    while ((keyDescription = [keyDescriptionEnumerator nextObject]) != nil) {
        key = [self keyFromDescription:keyDescription];
        if (key != nil) {
            [self synchronizeKey:key
                  toOrderedNames:[config objectForKey:keyDescription]];
        }
    }
    [hierarchyBrowser refreshLastColumn];
    [super hierarchyChanged];
}
@end