-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDiagramView.m
executable file
·844 lines (664 loc) · 21.8 KB
/
DiagramView.m
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
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
#import "DiagramView.h"
#import"Graphic.h"
#import "Headers.h"
#import"SelData.h"
static void *PropertyObservationContext = (void *)1091;
static void *GraphicsObservationContext = (void *)1092;
static void *SelectionIndexesObservationContext = (void *)1093;
NSString *GRAPHICS_BINDING_NAME = @"graphics";
NSString *SELECTIONINDEXES_BINDING_NAME = @"selectionIndexes";
@implementation DiagramView
+ (void)initialize
{
[self exposeBinding:GRAPHICS_BINDING_NAME];
[self exposeBinding:SELECTIONINDEXES_BINDING_NAME];
}
- (NSArray *)exposedBindings
{
return [NSArray arrayWithObjects:GRAPHICS_BINDING_NAME, @"selectedObjects", nil];
}
- (id)initWithFrame:(NSRect)frameRect
{
// float axisLength;
// axisLength = 400.0;
if ((self = [super initWithFrame:frameRect]) != nil) {
// Add initialization code here
[self setBoundsOrigin:NSMakePoint(-300.0,-480.0)];
selArray = [[NSMutableArray alloc] init];
NSLog(@"DiagramView initWithFrame %@", self);
// [NSApp setDelegate: self];
[self setSaveable: YES];
[self setNeedsDisplay: YES];
bindingInfo = [[NSMutableDictionary alloc] init];
[self prepareAttributes];
// header = [[Headers alloc]init];
}
return self;
}
- (void)unbind:(NSString *)bindingName
{
if ([bindingName isEqualToString:GRAPHICS_BINDING_NAME])
{
id graphicsContainer = [self graphicsContainer];
NSString *graphicsKeyPath = [self graphicsKeyPath];
[graphicsContainer removeObserver:self forKeyPath:graphicsKeyPath];
[bindingInfo removeObjectForKey:GRAPHICS_BINDING_NAME];
[self setOldGraphics:nil];
}
else
if ([bindingName isEqualToString:SELECTIONINDEXES_BINDING_NAME])
{
id selectionIndexesContainer = [self selectionIndexesContainer];
NSString *selectionIndexesKeyPath = [self selectionIndexesKeyPath];
[selectionIndexesContainer removeObserver:self forKeyPath:selectionIndexesKeyPath];
[bindingInfo removeObjectForKey:SELECTIONINDEXES_BINDING_NAME];
}
else
{
[super unbind:bindingName];
}
[self setNeedsDisplay:YES];
}
-(void)dealloc
{
[bindingInfo release];
[oldGraphics release];
[super dealloc];
}
- (void)observeValueForKeyPath:(NSString *)keyPath
ofObject:(id)object
change:(NSDictionary *)change
context:(void *)context
{
if (context == GraphicsObservationContext)
{
/*
Should be able to use
NSArray *oldGraphics = [change objectForKey:NSKeyValueChangeOldKey];
etc. but the dictionary doesn't contain old and new arrays.
*/
NSArray *newGraphics = [object valueForKeyPath:[self graphicsKeyPath]];
NSMutableArray *onlyNew = [newGraphics mutableCopy];
[onlyNew removeObjectsInArray:oldGraphics];
[self startObservingGraphics:onlyNew];
[onlyNew release];
NSMutableArray *removed = [oldGraphics mutableCopy];
[removed removeObjectsInArray:newGraphics];
[self stopObservingGraphics:removed];
[removed release];
[self setOldGraphics:newGraphics];
// could check drawingBounds of old and new, but...
[self setNeedsDisplay:YES];
return;
}
if (context == PropertyObservationContext)
{
NSRect updateRect;
if ([keyPath isEqualToString:@"drawingBounds"])
{
NSRect newBounds = [[change objectForKey:NSKeyValueChangeNewKey] rectValue];
NSRect oldBounds = [[change objectForKey:NSKeyValueChangeOldKey] rectValue];
updateRect = NSUnionRect(newBounds,oldBounds);
}
else
{
updateRect = [(NSObject <Graphic> *)object drawingBounds];
}
updateRect = NSMakeRect(updateRect.origin.x-1.0,
updateRect.origin.y-1.0,
updateRect.size.width+2.0,
updateRect.size.height+2.0);
[self setNeedsDisplayInRect:updateRect];
return;
}
if (context == SelectionIndexesObservationContext)
{
[self setNeedsDisplay:YES];
return;
}
}
- (void)startObservingGraphics:(NSArray *)graphics
{
if ([graphics isEqual:[NSNull null]])
{
return;
}
/*
Register to observe each of the new graphics, and each of their observable properties -- we need old and new values for drawingBounds to figure out what our dirty rect
*/
NSEnumerator *graphicsEnumerator = [graphics objectEnumerator];
/*
Declare newGraphic as NSObject * to get key value observing methods
Add Graphic protocol for drawing
*/
NSObject <Graphic> *newGraphic;
/*
Register as observer for all the drawing-related properties
*/
while (newGraphic = [graphicsEnumerator nextObject])
{
[newGraphic addObserver:self
forKeyPath:GraphicDrawingBoundsKey
options:(NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld)
context:PropertyObservationContext];
[newGraphic addObserver:self
forKeyPath:GraphicDrawingContentsKey
options:0
context:PropertyObservationContext];
}
}
- (void)stopObservingGraphics:(NSArray *)graphics
{
if ([graphics isEqual:[NSNull null]])
{
return;
}
NSEnumerator *graphicsEnumerator = [graphics objectEnumerator];
id oldGraphic;
while (oldGraphic = [graphicsEnumerator nextObject])
{
[oldGraphic removeObserver:self forKeyPath:GraphicDrawingBoundsKey];
[oldGraphic removeObserver:self forKeyPath:GraphicDrawingContentsKey];
}
}
- (void)bind:(NSString *)bindingName
toObject:(id)observableObject
withKeyPath:(NSString *)observableKeyPath
options:(NSDictionary *)options
{
if ([bindingName isEqualToString:GRAPHICS_BINDING_NAME])
{
if ([bindingInfo objectForKey:GRAPHICS_BINDING_NAME] != nil)
{
[self unbind:GRAPHICS_BINDING_NAME];
}
/*
observe the controller for changes -- note, pass binding identifier as the context, so we get that back in observeValueForKeyPath:... -- that way we can determine what needs to be updated
*/
NSDictionary *bindingsData = [NSDictionary dictionaryWithObjectsAndKeys:
observableObject, NSObservedObjectKey,
[[observableKeyPath copy] autorelease], NSObservedKeyPathKey,
[[options copy] autorelease], NSOptionsKey, nil];
[bindingInfo setObject:bindingsData forKey:GRAPHICS_BINDING_NAME];
[observableObject addObserver:self
forKeyPath:observableKeyPath
options:(NSKeyValueObservingOptionNew |
NSKeyValueObservingOptionOld)
context:GraphicsObservationContext];
[self startObservingGraphics:[observableObject valueForKeyPath:observableKeyPath]];
}
else
if ([bindingName isEqualToString:SELECTIONINDEXES_BINDING_NAME])
{
if ([bindingInfo objectForKey:SELECTIONINDEXES_BINDING_NAME] != nil)
{
[self unbind:SELECTIONINDEXES_BINDING_NAME];
}
/*
observe the controller for changes -- note, pass binding identifier as the context, so we get that back in observeValueForKeyPath:... -- that way we can determine what needs to be updated
*/
NSDictionary *bindingsData = [NSDictionary dictionaryWithObjectsAndKeys:
observableObject, NSObservedObjectKey,
[[observableKeyPath copy] autorelease], NSObservedKeyPathKey,
[[options copy] autorelease], NSOptionsKey, nil];
[bindingInfo setObject:bindingsData forKey:SELECTIONINDEXES_BINDING_NAME];
[observableObject addObserver:self
forKeyPath:observableKeyPath
options:0
context:SelectionIndexesObservationContext];
}
else
{
/*
For every binding except "graphics" and "selectionIndexes" just use NSObject's default implementation. It will start observing the bound-to property. When a KVO notification is sent for the bound-to property, this object will be sent a [self setValue:theNewValue forKey:theBindingName] message, so this class just has to be KVC-compliant for a key that is the same as the binding name. Also, NSView supports a few simple bindings of its own, and there's no reason to get in the way of those.
*/
[super bind:bindingName toObject:observableObject withKeyPath:observableKeyPath options:options];
}
[self setNeedsDisplay:YES];
}
-(void)drawRect:(NSRect)rect
{
int i;
NSPoint a, b, c, aPoint, bPoint, cPoint;
NSRect bounds = [self bounds];
// bounds.origin = NSMakePoint(150.0, 150.0);
//
// These are for converting raw data coordinates into
// display coordinates.
//
float tickLength;
float axisLength;
tickLength = 3;
axisLength = 500.0;
[[NSColor whiteColor] set];
[NSBezierPath fillRect:bounds];
[[NSColor blackColor] set];
NSBezierPath *tickPath;
NSEraseRect(rect);
NSBezierPath *path = [NSBezierPath bezierPath];
// NSLog(@"frame %f %f %f %f", bounds.size.width, bounds.size.height, bounds.origin.x, bounds.origin.y);
// NSLog(@"boundsRect = %@", NSStringFromRect(bounds));
// NSLog(@"frameRect = %@", NSStringFromRect([self frame]));
// Draw Ternary
// path = [[NSBezierPath alloc] init];
[path setLineWidth: 1.0];
a.x = 0;
a.y = 0;
b.x = a.x + axisLength;
b.y = a.y;
c.x = b.x / 2;
c.y = axisLength * 0.8660254;
[path moveToPoint: a];
[path lineToPoint: b];
[path lineToPoint: c];
[path closePath];
[path stroke];
// Draw Ticks
if (showTicks) {
tickPath = [[NSBezierPath alloc] init];
for (i=0; i < 9; i++) {
[tickPath moveToPoint: NSMakePoint( (i+1) * 10 / 2 * axisLength / 100, (i+1) * 10 * 0.8660254 * axisLength / 100)];
[tickPath lineToPoint: NSMakePoint( (tickLength + (i+1) * 10 / 2) * axisLength / 100, (i+1) * 10 * 0.8660254 * axisLength / 100)];
[tickPath moveToPoint: NSMakePoint( (i+1) * 10 / 2 * axisLength / 100, (i+1) * 10 * 0.8660254 * axisLength / 100)];
[tickPath lineToPoint: NSMakePoint( (tickLength + ((i+1) * 10 - tickLength) / 2) * axisLength / 100, ((i+1) * 10 - tickLength) * 0.8660254 * axisLength / 100)];
[tickPath moveToPoint: NSMakePoint( (i+1) * 10 * axisLength / 100, 0)];
[tickPath lineToPoint: NSMakePoint( ((i+1) * 10 + tickLength/2) * axisLength / 100, tickLength * 0.8660254 * axisLength / 100)];
[tickPath moveToPoint: NSMakePoint( (i+1) * 10 * axisLength / 100, 0)];
[tickPath lineToPoint: NSMakePoint( ((i+1) * 10 - tickLength + tickLength/2) * axisLength / 100, tickLength * 0.8660254 * axisLength / 100)];
[tickPath moveToPoint: NSMakePoint( (100 - (i+1) * 10 + (i+1) * 10 / 2) * axisLength / 100, (i+1) * 10 * 0.8660254 * axisLength / 100)];
[tickPath lineToPoint: NSMakePoint( (100 - (i+1) * 10 + ((i+1) * 10 - tickLength) / 2) * axisLength / 100, ((i+1) * 10 - tickLength) * 0.8660254 * axisLength / 100)];
[tickPath moveToPoint: NSMakePoint( (100 - (i+1) * 10 + (i+1) * 10 / 2) * axisLength / 100, (i+1) * 10 * 0.8660254 * axisLength / 100)];
[tickPath lineToPoint: NSMakePoint( (100 - (i+1) * 10 - tickLength + (i+1) * 10 / 2) * axisLength / 100, (i+1) * 10 * 0.8660254 * axisLength / 100)];
}
[tickPath stroke];
}
/*
Draw graphics
*/
NSArray *graphicsArray = [self graphics];
NSEnumerator *graphicsEnumerator = [graphicsArray objectEnumerator];
NSObject <Graphic> *graphic;
while (graphic = [graphicsEnumerator nextObject])
{
//NSLog(@"DiagramView drawRect %@", self);
// NSLog(@"DiagramView xxxxxxxxx %@", self);
// NSRect graphicDrawingBounds = [graphic drawingBounds];
// if (NSIntersectsRect(rect, graphicDrawingBounds))
//{
[graphic drawInView:self];
// }
}
NSIndexSet *currentSelectionIndexes = [self selectionIndexes];
if (currentSelectionIndexes != nil)
{
NSBezierPath *path = [NSBezierPath bezierPath];
NSUInteger index = [currentSelectionIndexes firstIndex];
while (index != NSNotFound)
{
graphic = [graphicsArray objectAtIndex:index];
// NSLog(@"reingegangen");
NSRect graphicDrawingBounds = [graphic drawingBounds];
if (NSIntersectsRect(rect, graphicDrawingBounds))
{
[path appendBezierPathWithRect:graphicDrawingBounds];
// NSLog(@"DiagramView 2 graphicDrawingBounds r%@ db %@", NSStringFromRect(rect), NSStringFromRect(graphicDrawingBounds));
}
index = [currentSelectionIndexes indexGreaterThanIndex:index];
}
[[NSColor redColor] set];
[path setLineWidth:1.0];
[path stroke];
}
if (connectPointsYN) {
for (i = 0; i < [selArray count]; i++) {
// NSIndexSet *selection = [[selArray objectAtIndex:i] sel];
NSLog(@"sel no %u", i);
SelData *aSel = [selArray objectAtIndex:i];
NSArray *points2Connect = [aSel sel];
if ( [points2Connect count] > 1) {
unsigned int index = 0;
NSLog(@"first index %u", index);
if ([graphicsArray containsObject:[points2Connect objectAtIndex:index]])
graphic = [points2Connect objectAtIndex:index];
// index = [selection indexGreaterThanIndex:index];
NSBezierPath *tieline = [NSBezierPath bezierPath];
[[NSColor blueColor] set];
[tieline setLineWidth:0.5];
[tieline moveToPoint: NSMakePoint([graphic xLoc], [graphic yLoc])];
index++;
NSLog(@"first index %u", index);
while (index < [points2Connect count])
{
NSLog(@"loop index %u", index);
graphic = [points2Connect objectAtIndex:index];
NSPoint a = NSMakePoint([graphic xLoc], [graphic yLoc]);
[tieline lineToPoint: a];
index++;
NSLog(@"looped to %u", index);
}
NSLog(@"broken with %u", index);
[tieline closePath];
if ([points2Connect count] > 3) {
NSLog(@"inside routine at index %u", index);
graphic = [points2Connect objectAtIndex:0];
a = NSMakePoint([graphic xLoc], [graphic yLoc]);
[tieline moveToPoint: a];
graphic = [points2Connect objectAtIndex:2];
a = NSMakePoint([graphic xLoc], [graphic yLoc]);
[tieline lineToPoint: a];
// This procedure should be optimised soon!!!! very quick & dirty
graphic = [points2Connect objectAtIndex:3];
a = NSMakePoint([graphic xLoc], [graphic yLoc]);
[tieline moveToPoint: a];
graphic = [points2Connect objectAtIndex:1];
a = NSMakePoint([graphic xLoc], [graphic yLoc]);
[tieline lineToPoint: a];
}
NSLog(@"outsinde routine at index %u", index);
[tieline stroke];
}
} // for - Loop
} // if (connectPointsYN)
if (showLabelsYN) {
aPoint.x = -10.0;
aPoint.y = -25.0,
[aString drawAtPoint:aPoint withAttributes:attributes];
bPoint.x = axisLength + 10.0 - [bString sizeWithAttributes:attributes].width;
bPoint.y = -25.0,
[bString drawAtPoint:bPoint withAttributes:attributes];
cPoint.x = axisLength/2 - ([cString sizeWithAttributes:attributes].width/2);
cPoint.y = axisLength * 0.8660254;
[cString drawAtPoint:cPoint withAttributes:attributes];
}
NSLog(@"drawRect %@", self);
[self retain];
}
- (void)prepareAttributes
{
attributes = [[NSMutableDictionary alloc] init];
[attributes setObject:[NSFont fontWithName:@"Helvetica" size:16]
forKey:NSFontAttributeName];
[attributes setObject:[NSColor blackColor]
forKey:NSForegroundColorAttributeName];
}
- (void)setSaveable:(BOOL)yn
{
saveable = yn;
}
- (BOOL)saveable
{
return saveable;
}
- (BOOL)validateMenuItem:(NSMenuItem *)menuItem
{
NSString *selectorString;
selectorString = NSStringFromSelector([menuItem action]);
NSLog(@"validateCalled for %@", selectorString);
// By using the action instead of the title, we do not
// have to worry about whether the menu item is localized
if ([menuItem action] == @selector(savePDF:)) {
return saveable;
} else {
return YES;
}
}
- (void) setTicks:(id)sender
{
showTicks = [sender state];
[self setNeedsDisplay:YES];
NSLog(@"DiagramView setTicks %@", self);
}
- (void)mouseDown:(NSEvent *)event
{
/*
Fairly simple just to illustrate the point
*/
// find out if we hit anything
NSPoint p = [self convertPoint:[event locationInWindow] fromView:nil];
NSEnumerator *gEnum = [[self graphics] reverseObjectEnumerator];
id aGraphic;
while (aGraphic = [gEnum nextObject])
{
if ([aGraphic hitTest:p isSelected:NO])
{
break;
}
}
/*
if no graphic hit, then if extending selection do nothing else set selection to nil
*/
if (aGraphic == nil)
{
if (!([event modifierFlags] & NSEventModifierFlagShift))
{
[[self selectionIndexesContainer] setValue:nil forKeyPath:[self selectionIndexesKeyPath]];
}
return;
}
/*
graphic hit
if not extending selection (Shift key down) then set selection to this graphic
if extending selection, then:
- if graphic in selection remove it
- if not in selection add it
*/
NSIndexSet *selection = nil;
unsigned int graphicIndex = [[self graphics] indexOfObject:aGraphic];
if (!([event modifierFlags] & NSEventModifierFlagShift))
{
selection = [NSIndexSet indexSetWithIndex:graphicIndex];
}
else
{
if ([[self selectionIndexes] containsIndex:graphicIndex])
{
selection = [[[self selectionIndexes] mutableCopy] autorelease];
[(NSMutableIndexSet *)selection removeIndex:graphicIndex];
}
else
{
selection = [[[self selectionIndexes] mutableCopy] autorelease];
[(NSMutableIndexSet *)selection addIndex:graphicIndex];
}
}
[[self selectionIndexesContainer] setValue:selection forKeyPath:[self selectionIndexesKeyPath]];
}
- (NSArray *)oldGraphics
{
return oldGraphics;
}
- (void)setOldGraphics:(NSArray *)anOldGraphics
{
if (oldGraphics != anOldGraphics) {
[oldGraphics release];
oldGraphics = [anOldGraphics mutableCopy];
}
}
// bindings-related -- infoForBinding and convenience methods
- (NSDictionary *)infoForBinding:(NSString *)bindingName
{
NSDictionary *info = [bindingInfo objectForKey:bindingName];
if (info == nil) {
info = [super infoForBinding:bindingName];
}
return info;
}
- (id)graphicsContainer
{
return [[self infoForBinding:GRAPHICS_BINDING_NAME] objectForKey:NSObservedObjectKey];
}
- (NSString *)graphicsKeyPath {
return [[self infoForBinding:GRAPHICS_BINDING_NAME] objectForKey:NSObservedKeyPathKey];
}
- (id)selectionIndexesContainer
{
return [[self infoForBinding:SELECTIONINDEXES_BINDING_NAME] objectForKey:NSObservedObjectKey];
}
- (NSString *)selectionIndexesKeyPath {
return [[self infoForBinding:SELECTIONINDEXES_BINDING_NAME] objectForKey:NSObservedKeyPathKey];
}
- (NSArray *)graphics
{
return [[self graphicsContainer] valueForKeyPath:[self graphicsKeyPath]];
}
- (void) setShowLabelsYN:(BOOL)yn
{
showLabelsYN = yn;
// [self setNeedsDisplay:YES];
// NSLog(@"DiagramView setTicks %@", self);
}
-(BOOL)showLabelsYN {return showLabelsYN;}
-(void)setConnectPointsYN:(BOOL)yn
{
connectPointsYN = yn;
}
-(BOOL)connectPointsYN { return connectPointsYN; }
- (NSMutableArray *)selArray {return selArray;}
- (void)setSelArray:(NSMutableArray *)aSelArray
{
if (selArray != aSelArray) {
[selArray release];
selArray = [aSelArray mutableCopy];
[selTable reloadData];
}
}
- (NSIndexSet *)selectionIndexes
{
return [[self selectionIndexesContainer] valueForKeyPath:[self selectionIndexesKeyPath]];
}
- (void)viewWillMoveToSuperview:(NSView *)newSuperview
{
[super viewWillMoveToSuperview:newSuperview];
if (newSuperview == nil)
{
[self stopObservingGraphics:[self graphics]];
[self unbind:GRAPHICS_BINDING_NAME];
[self unbind:SELECTIONINDEXES_BINDING_NAME];
}
}
- (void)setaString:(NSString *)x
{
x = [x copy];
[aString release];
aString = x;
[self setNeedsDisplay:YES];
}
- (void)setbString:(NSString *)x
{
x = [x copy];
[bString release];
bString = x;
[self setNeedsDisplay:YES];
}
- (void)setcString:(NSString *)x
{
x = [x copy];
[cString release];
cString = x;
[self setNeedsDisplay:YES];
}
- (NSString *)aString
{
return aString;
}
- (NSString *)bString
{
return bString;
}
- (NSString *)cString
{
return cString;
}
// Delegate methods
- (int)numberOfRowsInTableView:(NSTableView *)aTable
{
return [selArray count];
}
- (id)tableView:(NSTableView *)aTableView
objectValueForTableColumn:(NSTableColumn *)aTableColumn
row:(int)rowIndex
{
NSString *columnKey = [aTableColumn identifier];
SelData *aSel = [selArray objectAtIndex:rowIndex];
return [aSel valueForKey:columnKey];
}
- (void)tableView:(NSTableView *)aTableView
setObjectValue:(id)anObject
forTableColumn:(NSTableColumn *)aTableColumn
row:(int)rowIndex
{
NSString *columnKey = [aTableColumn identifier];
SelData *aSel = [selArray objectAtIndex:rowIndex];
[aSel setValue:anObject forKey:columnKey];
}
- (void)broadcastSelAdd
{
NSNotificationCenter *notify;
notify =[NSNotificationCenter defaultCenter];
[notify postNotificationName:@"selectionAdded" object:nil];
}
- (void)broadcastSelRemoved
{
NSNotificationCenter *notify;
notify =[NSNotificationCenter defaultCenter];
[notify postNotificationName:@"selectionRemoved" object:nil];
}
-(IBAction)createSel:(id)sender
{
[self broadcastSelAdd];
SelData *newSel = [[SelData alloc] init];
[newSel setCountOfSel:[selArray count]+1];
[newSel setNoOfPoints:[[symbolController selectionIndexes] count]];
[newSel setSel:[symbolController selectedObjects]];
NSLog(@"name %i", [newSel countOfSel] );
[selArray addObject:newSel];
[newSel release];
[self setNeedsDisplay:YES];
[selTable reloadData];
}
-(IBAction)deleteSelectedSel:(id)sender
{
[self broadcastSelRemoved];
NSIndexSet *rows = [selTable selectedRowIndexes];
if ([rows count] > 0) {
unsigned int row = [rows lastIndex];
while (row != NSNotFound) {
[selArray removeObjectAtIndex:row];
row = [rows indexLessThanIndex:row];
}
[self setNeedsDisplay:YES];
[selTable reloadData];
} else {
NSBeep();
}
}
- (void)tableViewSelectionDidChange:(NSNotification *)aNotification;
{
// NSLog(@"tableViewSelectionDidChange");
if ([aNotification object] == selTable) {
// NSLog(@"true");
[self updateSel];
}
else {
// NSLog(@"else");
[selTable deselectAll:nil];
}
}
-(void)updateSel
{
unsigned selectedRow = [selTable selectedRow];
NSLog(@"vgfr %i", selectedRow );
if (selectedRow == -1)
{
// fill code to handle null selection
[selTable deselectAll:nil];
}
else
{
[symbolController setSelectedObjects:[[selArray objectAtIndex:selectedRow] sel] ];
// decrepated [selTable selectRow:selectedRow byExtendingSelection:NO];
[selTable selectRowIndexes:[NSIndexSet indexSetWithIndex:selectedRow] byExtendingSelection:NO];
NSLog(@"selextion %lu, %i", (unsigned long)[[[selArray objectAtIndex:selectedRow] sel] count], selectedRow );
}
}
@end