77//
88
99import Cocoa
10+ import SwiftyJSON
11+
1012fileprivate func < < T: Comparable > ( lhs: T ? , rhs: T ? ) -> Bool {
1113 switch ( lhs, rhs) {
1214 case let ( l? , r? ) :
@@ -39,11 +41,10 @@ class SJEditorViewController: NSViewController, NSTextViewDelegate {
3941 @IBOutlet var prefixClassTextField : NSTextField !
4042 @IBOutlet var companyNameTextField : NSTextField !
4143 @IBOutlet var authorNameTextField : NSTextField !
42- @IBOutlet var includeSwiftyCheckbox : NSButton !
43- @IBOutlet var supportNSCodingCheckbox : NSButton !
44- @IBOutlet var supportSwiftyJSONCheckbox : NSButton !
45- @IBOutlet var supportObjectMapperCheckbox : NSButton !
46- @IBOutlet var includeObjectMapperCheckbox : NSButton !
44+ @IBOutlet var includeHeaderImportCheckbox : NSButton !
45+ @IBOutlet var enableNSCodingSupportCheckbox : NSButton !
46+ @IBOutlet var librarySelector : NSPopUpButton !
47+ @IBOutlet var modelTypeSelectorSegment : NSSegmentedControl !
4748
4849 // MARK: View methods
4950 override func loadView( ) {
@@ -89,7 +90,9 @@ class SJEditorViewController: NSViewController, NSTextViewDelegate {
8990 handleError ( error)
9091 textView!. lnv_textDidChange ( Notification . init ( name: NSNotification . Name. NSTextDidChange, object: nil ) )
9192 return false
92- }
93+ } else {
94+ genericJSONError ( )
95+ }
9396 return false
9497 }
9598
@@ -101,13 +104,13 @@ class SJEditorViewController: NSViewController, NSTextViewDelegate {
101104 // The base class field is blank, cannot proceed without it.
102105 // Possibly can have a default value in the future.
103106 if baseClassTextField? . stringValue. characters. count <= 0 {
104- let alert : NSAlert = NSAlert ( )
107+ let alert = NSAlert ( )
105108 alert. messageText = " Enter a base class name to continue. "
106109 alert. runModal ( )
107110 return
108111 }
109112
110- let filePath : String ? = openFile ( )
113+ let filePath = openFile ( )
111114
112115 // No file path was selected, go back!
113116 if filePath == nil {
@@ -119,27 +122,28 @@ class SJEditorViewController: NSViewController, NSTextViewDelegate {
119122 // Checks for validity of the content, else can cause crashes.
120123 if object != nil {
121124
122- // let swiftyState = self.includeSwiftyCheckbox?.state == 1 ? true : false
123- // let supportSwiftyState = self.supportSwiftyJSONCheckbox?.state == 1 ? true : false
124- //
125- // let nscodingState = self.supportNSCodingCheckbox?.state == 1 ? true : false
126- //
127- // let objectMapperState = self.includeObjectMapperCheckbox?.state == 1 ? true : false
128- // let supportObjectMapperState = self.supportObjectMapperCheckbox?.state == 1 ? true : false
129-
130- // let generator: ModelGenerator = ModelGenerator.init(baseContent: JSON(object!), baseClassName: baseClassTextField.stringValue, filePath: filePath!)
131- //
132- // generator.prefix = prefixClassTextField.stringValue
133- // generator.authorName = authorNameTextField.stringValue
134- // generator.companyName = companyNameTextField.stringValue
135- // generator.type = ModelType.kClassType
136- // generator.supportSwiftyJSON = supportSwiftyState
137- // generator.includeSwiftyJSON = swiftyState
138- // generator.supportObjectMapper = supportObjectMapperState
139- // generator.includeObjectMapper = objectMapperState
140- // generator.supportNSCoding = nscodingState
141- //
142- // generator.generate()
125+ let nsCodingState = self . enableNSCodingSupportCheckbox. state == 1 && ( modelTypeSelectorSegment. selectedSegment == 1 )
126+ let constructType = self . modelTypeSelectorSegment. selectedSegment == 0 ? ConstructType . ClassType : ConstructType . StructType
127+ let libraryType = self . librarySelector. indexOfSelectedItem == 0 ? JSONMappingLibrary . SwiftyJSON : JSONMappingLibrary . ObjectMapper
128+ let configuration = ModelGenerationConfiguration . init (
129+ filePath: filePath!. appending ( " / " ) ,
130+ baseClassName: baseClassTextField. stringValue,
131+ authorName: authorNameTextField. stringValue,
132+ companyName: companyNameTextField. stringValue,
133+ prefix: prefixClassTextField. stringValue,
134+ constructType: constructType,
135+ modelMappingLibrary: libraryType,
136+ supportNSCoding: nsCodingState)
137+ let modelGenerator = ModelGenerator . init ( JSON ( object!) , configuration)
138+ let filesGenerated = modelGenerator. generate ( )
139+ var successState = true
140+ for file in filesGenerated {
141+ let content = FileGenerator . generateFileContentWith ( file, configuration: configuration)
142+ let name = file. fileName
143+ let path = configuration. filePath
144+ successState = FileGenerator . writeToFileWith ( name, content: content, path: path)
145+ }
146+ notify ( completionState: successState, fileCount: filesGenerated. count)
143147 } else {
144148 let alert : NSAlert = NSAlert ( )
145149 alert. messageText = " Unable to save the file check the content. "
@@ -148,23 +152,21 @@ class SJEditorViewController: NSViewController, NSTextViewDelegate {
148152 }
149153
150154 @IBAction func recalcEnabledBoxes( _ sender: AnyObject ) {
155+ self . enableNSCodingSupportCheckbox. isEnabled = ( modelTypeSelectorSegment. selectedSegment == 1 )
156+ }
151157
152- let supportSwiftyState = self . supportSwiftyJSONCheckbox? . state == 1 ? true : false
153- let supportObjectMapperState = self . supportObjectMapperCheckbox? . state == 1 ? true : false
154-
155- if supportSwiftyState {
156- self . includeSwiftyCheckbox? . isEnabled = true
157- } else {
158- self . includeSwiftyCheckbox? . isEnabled = false
159- }
160158
161- if supportObjectMapperState {
162- self . includeObjectMapperCheckbox? . isEnabled = true
163- } else {
164- self . includeObjectMapperCheckbox? . isEnabled = false
159+ func notify( completionState: Bool , fileCount: Int ) {
160+ let notification : NSUserNotification = NSUserNotification ( )
161+ notification. title = " SwiftyJSONAccelerator "
162+ if completionState && fileCount > 0 {
163+ notification. subtitle = " Completed - \( fileCount) Generated. "
164+ } else {
165+ notification. subtitle = " No files were generated, cannot model arrays inside arrays. "
166+ }
167+ NSUserNotificationCenter . default. deliver ( notification)
165168 }
166- }
167-
169+
168170 // MARK: Internal Methods
169171
170172 /**
@@ -224,9 +226,18 @@ class SJEditorViewController: NSViewController, NSTextViewDelegate {
224226 } else {
225227 invalidJSONError ( message)
226228 }
229+ } else {
230+ genericJSONError ( )
227231 }
228232 }
229233
234+ /**
235+ Shows a generic error about JSON in case the system is not able to figure out what is wrong.
236+ */
237+ func genericJSONError( ) {
238+ invalidJSONError ( " The JSON seems to be invalid! " )
239+ }
240+
230241 /// MARK: Resetting and showing error messages
231242
232243 /**
@@ -258,7 +269,10 @@ class SJEditorViewController: NSViewController, NSTextViewDelegate {
258269
259270 // MARK: TextView Delegate
260271 func textDidChange( _ notification: Notification ) {
261- validateAndFormat ( false )
272+ let isValid = validateAndFormat ( false )
273+ if isValid {
274+ resetErrorImage ( )
275+ }
262276 }
263277
264278 // MARK: Internal Methods
0 commit comments