-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQSEditorController.m
More file actions
106 lines (72 loc) · 2.64 KB
/
QSEditorController.m
File metadata and controls
106 lines (72 loc) · 2.64 KB
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
#import "QSEditorController.h"
#import "QSObject_FileHandling.h"
#import "QSObject.h"
#define textTypes [NSArray arrayWithObjects:@"'TEXT'",@"txt",nil]
#define rtfTypes [NSArray arrayWithObjects:@"rtf",nil]
NSMutableDictionary *editorDictionary;
@implementation QSEditorController
+ (void)initialize{
editorDictionary=[[NSMutableDictionary alloc]initWithCapacity:1];
}
+ (id)editorForObject:(QSObject *)anObject{
QSEditorController *editor=[editorDictionary objectForKey:[anObject identifier]];
if (!editor)
editor=[[QSEditorController alloc]initWithObject:anObject];
return editor;
}
- (id)init {
return [self initWithWindowNibName:@"Editor"];
}
- (id)initWithObject:(QSObject *)anObject{
[self init];
object = anObject;
filePath=nil;
modificationDate=nil;
[editorDictionary setObject:self forKey:[anObject identifier]];
return self;
}
- (void)awakeFromNib{
[[self window] addInternalWidgetsForStyleMask:NSUtilityWindowMask];
}
- (void)windowDidLoad {
[super windowDidLoad];
[[self window] setMovableByWindowBackground:YES];
[self openDocument:[object singleFilePath]];
}
- (void) openDocument:(NSString *)path {
NSLog(@"open: %@",path);
NSString *type=[[NSFileManager defaultManager]typeOfFile:path];
if ([textTypes containsObject:type]){
NSString *text=[NSString stringWithContentsOfFile:path];
[textView setString:(text?text:@"")];
}else if ([rtfTypes containsObject:type]){
[textView readRTFDFromFile:path];
} else NSLog(@"Unknown Type: %@",type);
if ([[textView string]length]) filePath=path;
[titleField setStringValue:([path lastPathComponent])];
}
- (IBAction) saveDocument:(id)sender{
NSString *type=[[NSFileManager defaultManager]typeOfFile:filePath];
if (!filePath){
NSLog(@"No File Path to save to");
return;
}
if ([textTypes containsObject:type])
[[textView string]writeToFile:filePath atomically:NO];
else if ([rtfTypes containsObject:type])
[[textView RTFFromRange:NSMakeRange(0,[[textView string]length])]writeToFile:filePath atomically:NO];
// [textView writeRTFDToFile:filePath atomically:NO];
else NSLog(@"Unknown Type: %@",type);
}
- (void)windowWillClose:(NSNotification *)aNotification{
[self saveDocument:self];
[editorDictionary removeObjectForKey:[object identifier]];
[self autorelease];
}
- (void)windowDidResignKey:(NSNotification *)aNotification{
// [[self window] setAlphaValue:0.8];
}
- (void)windowDidBecomeKey:(NSNotification *)aNotification{
// [[self window] setAlphaValue:1.0];
}
@end