MongoView/MongoView/View Controllers/ServerConnectViewController...

90 lines
2.6 KiB
Swift

//
// ServerConnectViewController.swift
// MongoView
//
// Created by Shadowfacts on 1/11/20.
// Copyright © 2020 Shadowfacts. All rights reserved.
//
import Cocoa
protocol ServerConnectViewControllerDelegate: class {
func connectToServer(mongoController: MongoController)
}
class ServerConnectViewController: NSViewController {
weak var delegate: ServerConnectViewControllerDelegate?
@IBOutlet weak var hostTextField: NSTextField!
@IBOutlet weak var portTextField: NSTextField!
@IBOutlet weak var usernameTextField: NSTextField!
@IBOutlet weak var passwordTextField: NSSecureTextField!
@IBOutlet weak var connectButton: NSButton!
init() {
super.init(nibName: "ServerConnectViewController", bundle: .main)
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func viewDidLoad() {
super.viewDidLoad()
hostTextField.delegate = self
portTextField.delegate = self
hostTextField.nextKeyView = portTextField
portTextField.nextKeyView = usernameTextField
usernameTextField.nextKeyView = passwordTextField
passwordTextField.nextKeyView = hostTextField
updateConnectButtonEnabled()
}
override func viewWillAppear() {
super.viewWillAppear()
view.window!.initialFirstResponder = hostTextField
}
func updateConnectButtonEnabled() {
connectButton.isEnabled = !hostTextField.stringValue.isEmpty && !portTextField.stringValue.isEmpty
}
@IBAction func connect(_ sender: Any) {
connectButton.isEnabled = false
hostTextField.isEnabled = false
portTextField.isEnabled = false
usernameTextField.isEnabled = false
passwordTextField.isEnabled = false
var str = "mongodb://"
if !usernameTextField.stringValue.isEmpty {
str += usernameTextField.stringValue
if !passwordTextField.stringValue.isEmpty {
str.append(":")
str.append(passwordTextField.stringValue)
}
str.append("@")
}
str.append(hostTextField.stringValue)
str.append(":")
str.append(portTextField.stringValue)
let mongoController = MongoController(connectionString: str)
mongoController.setup()
delegate?.connectToServer(mongoController: mongoController)
}
}
extension ServerConnectViewController: NSTextFieldDelegate {
func controlTextDidChange(_ obj: Notification) {
updateConnectButtonEnabled()
}
}