<commit_before>//
//  IncidentController_edit.swift
//  Incidents
//
//  © 2015 <NAME> and its contributors. All rights reserved.
//  See the file COPYRIGHT.md for terms.
//

import Cocoa



extension IncidentController {

    func enableEditing() {
        statePopUp?.enabled = true
        priorityPopUp?.enabled = true
        summaryField?.enabled = true
        rangersTable?.enabled = true
        rangerToAddField?.enabled = true
        typesTable?.enabled = true
        typeToAddField?.enabled = true
        locationNameField?.enabled = true
        locationRadialAddressField?.enabled = true
        locationConcentricAddressField?.enabled = true
        locationDescriptionField?.enabled = true
        reportEntryToAddView?.editable = true

        // This updates the save button correctly
        updateEdited()
    }
    
    
    func disableEditing() {
        statePopUp?.enabled = false
        priorityPopUp?.enabled = false
        summaryField?.enabled = false
        rangersTable?.enabled = false
        rangerToAddField?.enabled = false
        typesTable?.enabled = false
        typeToAddField?.enabled = false
        locationNameField?.enabled = false
        locationRadialAddressField?.enabled = false
        locationConcentricAddressField?.enabled = false
        locationDescriptionField?.enabled = false
        reportEntryToAddView?.editable = false
        saveButton?.enabled = false
    }
    
    
    @IBAction func editSummary(sender: AnyObject?) {
        defer { updateView() }
        
        let oldSummary: String
        if let summary = incident!.summary { oldSummary = summary } else { oldSummary = "" }
        let newSummary = summaryField!.stringValue
        
        if newSummary == oldSummary { return }
        
        logDebug("Summary changed to: \(newSummary)")
        
        incident!.summary = newSummary
    }
    
    
    @IBAction func editState(sender: AnyObject?) {
        defer { updateView() }

        guard let selectedTag = statePopUp!.selectedItem?.tag else {
            logError("Unable to get selected state tag")
            return
        }
        
        guard let selectedState = IncidentStateTag(rawValue: selectedTag) else {
            logError("Unknown state tag: \(selectedTag)")
            return
        }
        
        let newState: IncidentState
        switch selectedState {
            case .New       : newState = .New
            case .OnHold    : newState = .OnHold
            case .Dispatched: newState = .Dispatched
            case .OnScene   : newState = .OnScene
            case .Closed    : newState = .Closed
        }
        
        if newState == incident!.state { return }
        
        logDebug("State changed to: \(newState)")
        
        incident!.state = newState
    }
    
    
    @IBAction func editPriority(sender: AnyObject?) {
        defer { updateView() }

        guard let selectedTag = priorityPopUp!.selectedItem?.tag else {
            logError("Unable to get selected priority tag")
            return
        }
        
        guard let selectedPriority = IncidentPriorityTag(rawValue: selectedTag) else {
            logError("Unknown state tag: \(selectedTag)")
            return
        }
        
        let newPriority: IncidentPriority
        switch selectedPriority {
            case .High  : newPriority = .High
            case .Normal: newPriority = .Normal
            case .Low   : newPriority = .Low
        }
        
        if newPriority == incident!.priority { return }
        
        logDebug("Priority changed to: \(newPriority)")
        
        incident!.priority = newPriority
    }
    
    
    @IBAction func editLocationName(sender: AnyObject?) {
        defer { updateView() }

        let oldName: String
        if let name = incident!.location?.name { oldName = name } else { oldName = "" }
        let newName = locationNameField!.stringValue
        
        if newName != oldName {
            let newLocation: Location
            if incident!.location == nil {
                newLocation = Location(name: newName)
            } else {
                newLocation = Location(
                    name: newName,
                    address: incident!.location!.address
                )
            }
            
            logDebug("Location changed to: \(newLocation)")
            
            incident!.location = newLocation
        }
    }
    
    
    @IBAction func editAddressDescription(sender: AnyObject?) {
        defer { updateView() }

        let oldDescription: String
        if let description = incident!.location?.address?.textDescription { oldDescription = description } else { oldDescription = "" }<commit_msg>Auto-fill the rest of the locations fields if we see a name we know about and the other fields are empty.<commit_after>//
//  IncidentController_edit.swift
//  Incidents
//
//  © 2015 <NAME> and its contributors. All rights reserved.
//  See the file COPYRIGHT.md for terms.
//

import Cocoa



extension IncidentController {

    func enableEditing() {
        statePopUp?.enabled = true
        priorityPopUp?.enabled = true
        summaryField?.enabled = true
        rangersTable?.enabled = true
        rangerToAddField?.enabled = true
        typesTable?.enabled = true
        typeToAddField?.enabled = true
        locationNameField?.enabled = true
        locationRadialAddressField?.enabled = true
        locationConcentricAddressField?.enabled = true
        locationDescriptionField?.enabled = true
        reportEntryToAddView?.editable = true

        // This updates the save button correctly
        updateEdited()
    }
    
    
    func disableEditing() {
        statePopUp?.enabled = false
        priorityPopUp?.enabled = false
        summaryField?.enabled = false
        rangersTable?.enabled = false
        rangerToAddField?.enabled = false
        typesTable?.enabled = false
        typeToAddField?.enabled = false
        locationNameField?.enabled = false
        locationRadialAddressField?.enabled = false
        locationConcentricAddressField?.enabled = false
        locationDescriptionField?.enabled = false
        reportEntryToAddView?.editable = false
        saveButton?.enabled = false
    }
    
    
    @IBAction func editSummary(sender: AnyObject?) {
        defer { updateView() }
        
        let oldSummary: String
        if let summary = incident!.summary { oldSummary = summary } else { oldSummary = "" }
        let newSummary = summaryField!.stringValue
        
        if newSummary == oldSummary { return }
        
        logDebug("Summary changed to: \(newSummary)")
        
        incident!.summary = newSummary
    }
    
    
    @IBAction func editState(sender: AnyObject?) {
        defer { updateView() }

        guard let selectedTag = statePopUp!.selectedItem?.tag else {
            logError("Unable to get selected state tag")
            return
        }
        
        guard let selectedState = IncidentStateTag(rawValue: selectedTag) else {
            logError("Unknown state tag: \(selectedTag)")
            return
        }
        
        let newState: IncidentState
        switch selectedState {
            case .New       : newState = .New
            case .OnHold    : newState = .OnHold
            case .Dispatched: newState = .Dispatched
            case .OnScene   : newState = .OnScene
            case .Closed    : newState = .Closed
        }
        
        if newState == incident!.state { return }
        
        logDebug("State changed to: \(newState)")
        
        incident!.state = newState
    }
    
    
    @IBAction func editPriority(sender: AnyObject?) {
        defer { updateView() }

        guard let selectedTag = priorityPopUp!.selectedItem?.tag else {
            logError("Unable to get selected priority tag")
            return
        }
        
        guard let selectedPriority = IncidentPriorityTag(rawValue: selectedTag) else {
            logError("Unknown state tag: \(selectedTag)")
            return
        }
        
        let newPriority: IncidentPriority
        switch selectedPriority {
            case .High  : newPriority = .High
            case .Normal: newPriority = .Normal
            case .Low   : newPriority = .Low
        }
        
        if newPriority == incident!.priority { return }
        
        logDebug("Priority changed to: \(newPriority)")
        
        incident!.priority = newPriority
    }
    
    
    @IBAction func editLocationName(sender: AnyObject?) {
        defer { updateView() }

        let oldName: String
        if let name = incident!.location?.name { oldName = name } else { oldName = "" }
        let newName = locationNameField!.stringValue
        
        func knownLocation() -> Location? {
            if (
                locationConcentricAddressField?.stringValue == "" &&
                locationRadialAddressField?.stringValue == "" &&
                locationDescriptionField?.stringValue == ""
            ) {
                if let knownLocation = dispatchQueueController?.ims.locationsByName[newName] {
                    return knownLocation
                }
            }
            
            return nil
        }

        let newLocation: Location?
        if let location = knownLocation() {
            newLocation = location
        }
        else if newName != oldName {
            if incident!.location == nil {
                newLocation = Location(name: newName)
            } else {
                newLocation = Location(
                    name: newName,
                    address: incident!.location!.address
                )
            }
        } else {
            newLocation = incident!.location
        }

        if newLocation != incident!.location {
            logDebug("Location changed to: \(newLocation)")
        
            incident!.location = newLocation
        }
    }
    
    
    @IBAction func editAddressDescription(sender: AnyObject?) {
        defer { updateView() }

        let oldDescription: String
        if let description = incident!.location?.address?.textDescription { oldDescription = description } else { oldDescription = "" }
{'fraction_non_alphanumeric': 0.08212890625, 'fraction_numerical': 0.00078125, 'mean_word_length': 1.7685861043525277, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 6, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2874389', 'n_tokens_mistral': 2534, 'n_tokens_neox': 2438, 'n_words': 638}