OSDN Git Service

SimpleHTMLEditorを追加。
[chnosproject/CHNOSProject.git] / CHNOSProject / SimpleHTMLEditor / SimpleHTMLEditor / AppDelegate.m
diff --git a/CHNOSProject/SimpleHTMLEditor/SimpleHTMLEditor/AppDelegate.m b/CHNOSProject/SimpleHTMLEditor/SimpleHTMLEditor/AppDelegate.m
new file mode 100644 (file)
index 0000000..b9df623
--- /dev/null
@@ -0,0 +1,84 @@
+//
+//  AppDelegate.m
+//  SimpleHTMLEditor
+//
+//  Created by 西田 耀 on 13/04/13.
+//  Copyright (c) 2013年 CHNOSProject. All rights reserved.
+//
+
+#import "AppDelegate.h"
+
+@implementation AppDelegate
+
+NSString *strNotOpened = @"Not opened a file.";
+
+- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
+{
+    // Insert code here to initialize your application
+    [_filePathLabel setStringValue:strNotOpened];
+}
+
+- (IBAction)display:(id)sender {
+    if(![[_filePathLabel stringValue] isEqualToString:strNotOpened]){
+        [mainWebView setMainFrameURL:[_filePathLabel stringValue]];
+        [_addressBar setStringValue:[_filePathLabel stringValue]];
+    }
+}
+
+- (BOOL)applicationShouldTerminateAfterLastWindowClosed:(NSApplication *)sender
+{
+    //最後のwindowが閉じたときに終了するか否か
+    return YES;
+}
+
+- (void)saveHTMLForFile:(id)sender
+{
+    NSSavePanel *savePanel;
+    NSArray *allowedFileType;
+    NSInteger pressedButton;
+    NSURL *path;
+    NSError *error;
+    
+    if([[_filePathLabel stringValue] isEqualToString:strNotOpened] || sender == _menuSaveAsButton){
+        savePanel = [NSSavePanel savePanel];
+        allowedFileType = [NSArray arrayWithObjects:@"htm", @"html", nil];
+        [savePanel setAllowedFileTypes:allowedFileType];
+        
+        pressedButton = [savePanel runModal];
+        
+        switch(pressedButton){
+            case NSOKButton:
+                path = [savePanel URL];
+                error = nil;
+                [[_editBox string] writeToURL:path atomically:YES encoding:NSUTF8StringEncoding error:&error];
+                if(error != nil){
+                    NSRunAlertPanel(@"SimpleHTMLEditor-Error-", [error localizedDescription], @"OK", nil, nil);
+                } else{
+                    [_filePathLabel setStringValue:[path path]];
+                }
+                
+                break;
+            case NSCancelButton:
+                
+                break;
+        };
+    } else{
+        path = [NSURL fileURLWithPath:[_filePathLabel stringValue]];
+        
+        error = nil;
+        [[_editBox string] writeToURL:path atomically:YES encoding:NSUTF8StringEncoding error:&error];
+        if(error != nil){
+            NSRunAlertPanel(@"SimpleHTMLEditor-Error-", [error localizedDescription], @"OK", nil, nil);
+        }
+        
+        
+    }
+}
+
+- (void)webViewDidChange:(NSNotification *)notification
+{
+    [_addressBar setStringValue:[[[[[mainWebView mainFrame] dataSource] request] URL] absoluteString]];
+
+}
+
+@end