jscape.domain = jscape.domain || {};

/** @class */
jscape.DlpRule = Class.extend(
    /** @lends {jscape.DlpRule.prototype} */
{
    /** @constructor */
    initialize: function(a, b, c, d) {
        this.name = a || "";
        this.description = b || "";
        this.scope = c === jscape.DlpRule.SCOPE.FILE_CONTENTS ? c : jscape.DlpRule.SCOPE.FILE_NAME;
        this.expression = d || ".*";
    }
});
/** @const */
jscape.DlpRule.SCOPE = {FILE_NAME: "FILE_NAME", FILE_CONTENTS: "FILE_CONTENTS"};
jscape.DlpRule.fromJSON = function(data) {
    return $.extend(new jscape.DlpRule(), data);
};

/** @class */
jscape.DlpIcapConfiguration = Class.extend(
    /** @lends {jscape.DlpIcapConfiguration.prototype} */
{
    /** @constructor */
    initialize: function(a, b, c, d, e, f, g, h, i, j) {
        this.host = a || "";
        this.port = parseInt(b) || 0;
        this.timeout = parseInt(c) || 0;
        this.path = d || "";
        this.method = e === jscape.DlpIcapConfiguration.Method.RESPMOD ? e : jscape.DlpIcapConfiguration.Method.REQMOD;
        this.headersFile = f || null;
        this.statusViolationRegexp = g || "";
        this.headersViolationRegexp = h || null;
        this.bodyViolationRegexp = i || null;
        this.debugFile = j || null;
    }
});
jscape.DlpIcapConfiguration.Method = {REQMOD: "REQUEST_MODIFICATION", RESPMOD: "RESPONSE_MODIFICATION"};
jscape.DlpIcapConfiguration.fromJSON = function (data) {
    return data
            ? new jscape.DlpIcapConfiguration(data.host,
                                              data.port,
                                              data.timeout,
                                              data.path,
                                              data.method,
                                              data.headersFile,
                                              data.statusViolationRegexp,
                                              data.headersViolationRegexp,
                                              data.bodyViolationRegexp,
                                              data.debugFile)
            : null;
};

/** @class */
jscape.domain.DlpModule = jscape.domain.DomainModule.extend(
    /** @lends {jscape.domain.DlpModule.prototype} */
{
    onCreate: function () {
        this._super.apply(this, arguments);

        this.ruleController = new jscape.domain.DlpRulesController();
        this.ruleController.onCreate(this.managementService, this.domainName);

        this.icapController = new jscape.domain.DlpIcapController();
        this.icapController.onCreate(this.managementService, this.domainName);
    },

    onStart: function () {
        this.ruleController.onStart();
        this.icapController.onStart();
    },

    onPause: function() {
        if (this.icapController.hasChanged()) {
            this._confirmAndApply([this.icapController]);
        }
    },

    onResume: function() {
        this.ruleController.onResume();
        this.icapController.onResume();
    }
});

/** @class */
jscape.domain.DlpRulesController = jscape.ui.DatagridController.extend(
    /** @lends {jscape.domain.DlpRulesController.prototype} */
{
    view: null,

    /** @constructor */
    initialize: function() {
        this._initValidators();
    },

    onCreate: function(managementService, domainName) {
        this.managementService = managementService;
        this.domainName = domainName;

        this.addController = new jscape.domain.AddDlpRuleController();
        this.editController = new jscape.domain.EditDlpRuleController();
        this.deleteController = new jscape.domain.DeleteDlpRuleController();
    },

    onStart: function () {
        if (this.view == null) {
            this.view = new jscape.domain.DlpRulesView();
        }
        this.view.show(this);
    },

    onResume: function() {
        this.view.refresh();
    },

    onRefresh: function() {
        return this.managementService.getDlpRules(this.domainName);
    },

    onAddRule: function() {
        this.addController.start(this.managementService, this.domainName)
                .done($.proxy(this._ruleAdded, this));
    },

    onEditRule: function(rule) {
        var index = $.inArray(rule, this.view.getRules());
        if (index != -1) {
            this.editController.start(this.managementService, this.domainName, rule)
                    .done($.proxy(this._ruleEdited, this, index));
        }
    },

    onDeleteRule: function(rules) {
        this.deleteController.start(this.managementService, this.domainName, rules)
                .done($.proxy(this._ruleDeleted, this, rules));
    },

    _ruleAdded: function(rule) {
        this._fireApplySuccess();
        this._recordAdded(rule);
    },

    _ruleEdited: function(index, rule) {
        this._fireApplySuccess();
        this._reloadAndSelect(rule);
    },

    _ruleDeleted: function (rules) {
        this._fireApplySuccess();
        this._recordDeleted(rules)
    },

    _initValidators: function() {
        var v = new jscape.Validator();
        v.nonEmptyRule("dlp_name_nonempty", window.R.string.DLP_ERROR_BAD_NAME);
        v.rule("dlp_rule_unique", $.proxy(this._ruleNotExists, this), window.R.string.DLP_ERROR_ALREADY_EXIST);
        v.rule("dlp_expression", $.proxy(this._expressionValid, this), window.R.string.DLP_ERROR_BAD_EXPRESSION);
    },

    _ruleNotExists: function (value, excludes) {
        var names = $.map(this.view.getRules(), function(rule) { return excludes && $.inArray(rule.name, excludes) != -1 ? null : rule.name; } );
        return $.inArray(value, names);
    },

    _expressionValid: function(value) {
        return value.trim().length != 0;
    },

    _fireApplySuccess: function () {
        $(document).triggerHandler("broadcast", [window.R.string.APPLICATION_SUCCESS_APPLY]);
    }
});

/** @class */
jscape.domain.DlpRulesView = jscape.ui.DatagridView.extend(
    /** @lends {jscape.domain.DlpRulesView.prototype} */
{
    selection: null,

    /** @constructor */
    initialize: function(container) {
        var initializing = true;

        this.fieldset = $("fieldset[name=dlprulesfields]", container);

        this.data = $("table[role=grid][aria-label=dlprules]", this.fieldset);
        this.data.datagrid({
            fit: true,
            fitColumns: true,
            singleSelect: true,
            pagination: true,
            search: true,
            remoteSort: true,
            sortName: "name",
            idField: "name",
            columns: [[
                {field: "name", title: $("thead th:nth-child(1)", this.data).text(), width: 70, sortable: true},
                {field: "description", title: $("thead th:nth-child(2)", this.data).text(), width: 70},
                {field: "scope", title: $("thead th:nth-child(3)", this.data).text(), width: 40, resizable: false, formatter: $.proxy(this._formatScope, this)},
                {field: "expression", title: $("thead th:nth-child(4)", this.data).text(), resizable: true, width: 120}
            ]],
            onSelect: $.proxy(this._adjustRowSelection, this),
            onUnselect: $.proxy(this._adjustRowSelection, this),
            onDblClickRow: $.proxy(this._onClick$EditButton, this),
            loader: $.proxy(this._onLoad$Data, this),
            onBeforeLoad: function () { return !initializing; },
            onLoadSuccess: $.proxy(this._adjustButtonsState, this)
        });

        this.addButton = $("a[role=button][name=add]", this.fieldset).linkbutton({onClick: $.proxy(this._onClick$AddButton, this)});
        this.editButton = $("a[role=button][name=edit]", this.fieldset).linkbutton({disabled: true, onClick: $.proxy(this._onClick$EditButton, this)});
        this.deleteButton = $("a[role=button][name=delete]", this.fieldset).linkbutton({disabled: true, onClick: $.proxy(this._onClick$DeleteButton, this)});

        initializing = false;
    },

    show: function(listener) {
        this.listener = listener || jscape.domain.DlpRulesView.LISTENER;
        this._adjustRowSelection();
    },

    getRules: function() {
        return this.getRecords();
    },

    _formatScope: function(value) {
        return window.R.string[("DLP_RULES_SCOPE_" + value).toUpperCase()] || "";
    },

    _onLoad$Data: function(params, success, error) {
        this.listener.onRefresh(params).done(success).fail(error);
    },

    _onClick$AddButton: function() {
        this.listener.onAddRule();
    },

    _onClick$EditButton: function() {
        this.listener.onEditRule(this.selection[0]);
    },

    _onClick$DeleteButton: function() {
        this.listener.onDeleteRule(this.selection);
    },

    _adjustRowSelection: function() {
        this.selection = this.data.datagrid("getSelections");
        this._adjustButtonsState();
    },

    _adjustButtonsState: function () {
        this.editButton.linkbutton(this.selection.length == 1 ? "enable" : "disable");
        this.deleteButton.linkbutton(this.selection.length != 0 ? "enable" : "disable");
    }
});
jscape.domain.DlpRulesView.LISTENER = {
    onRefresh: $.noop,
    onAddRule: $.noop,
    onEditRule: $.noop,
    onDeleteRule: $.noop
};

/** @class */
jscape.domain.AddDlpRuleController = Class.extend(
    /** @lends {jscape.domain.AddDlpRuleController.prototype} */
{
    deferred: null,

    start: function(managementService, domainName) {
        this.managementService = managementService;
        this.domainName = domainName;
        this.deferred = $.Deferred();
        this._setupDialog().show(this);
        return this.deferred.promise();
    },

    onSubmit: function(dialog) {
        var rule = this._createRule(dialog);
        dialog.destroy();

        this.managementService.addDlpRule(this.domainName, rule)
                .done($.proxy(function(rule) { this.deferred.resolve(rule); this.deferred = null; }, this))
                .fail($.proxy(function() { this.deferred.reject(); this.deferred = null; }, this));
    },

    onCancel: function(dialog) {
        dialog.destroy();
        this.deferred.reject();
    },

    _createRule: function(dialog) {
        return new jscape.DlpRule(dialog.getName(),
                                  dialog.getDescription(),
                                  dialog.getScope(),
                                  dialog.getExpression());
    },

    _setupDialog: function() {
        var dialog = new jscape.domain.DlpRuleDialog(true);
        dialog.setTitle(window.R.string.DLP_DIALOG_ADD_RULE_TITLE);
        dialog.setScope(jscape.DlpRule.SCOPE.FILE_NAME);
        return dialog;
    }
});

/** @class */
jscape.domain.EditDlpRuleController = Class.extend(
    /** @lends {jscape.domain.EditDlpRuleController.prototype} */
{
    deferred: null,

    start: function(managementService, domainName, rule) {
        this.managementService = managementService;
        this.domainName = domainName;
        this.ruleName = rule.name;
        this.deferred = $.Deferred();
        this.managementService.getDlpRule(this.domainName, this.ruleName)
                .done($.proxy(function(rule) { this._setupDialog(rule).show(this); }, this))
                .fail($.proxy(function() { this.deferred.reject(); this.deferred = null; }, this));
        return this.deferred.promise();
    },

    onSubmit: function(dialog) {
        var rule = this._createRule(dialog);
        dialog.destroy();

        this.managementService.setDlpRule(this.domainName, this.ruleName, rule)
                .done($.proxy(function() { this.deferred.resolve(rule); this.deferred = null; }, this))
                .fail($.proxy(function() { this.deferred.reject(); this.deferred = null; }, this));
    },

    onCancel: function(dialog) {
        dialog.destroy();
        this.deferred.reject();
        this.deferred = null;
    },

    _createRule: function(dialog) {
        return new jscape.DlpRule(dialog.getName(),
                                  dialog.getDescription(),
                                  dialog.getScope(),
                                  dialog.getExpression());
    },

    _setupDialog: function (rule) {
        var dialog = new jscape.domain.DlpRuleDialog(false);
        dialog.setTitle(window.R.string.DLP_DIALOG_EDIT_RULE_TITLE.supplant({0: rule.name}));
        dialog.setName(rule.name);
        dialog.setDescription(rule.description);
        dialog.setScope(rule.scope);
        dialog.setExpression(rule.expression);
        return dialog;
    }
});


/** @class */
jscape.domain.DeleteDlpRuleController = Class.extend(
    /** @lends {jscape.vfs.DeleteDlpRuleController.prototype} */
{
    deferred: null,

    start: function (managementService, domainName, rules) {
        this.managementService = managementService;
        this.domainName = domainName;
        this.deferred = $.Deferred();
        this._confirmDelete(rules)
                .done($.proxy(this._deleteConfirmed, this, rules))
                .fail($.proxy(function() { this.deferred.reject(); this.deferred = null; }, this));
        return this.deferred.promise();
    },

    _confirmDelete: function(rules) {
        var text = $.map(rules, function(rule) { return rule.name; }).join(", ");
        return jscape.ConfirmDialog.confirm(
                window.R.string.DLP_CONFIRM_DELETE_TITLE,
                window.R.string.DLP_CONFIRM_DELETE_MESSAGE.supplant({0: text}));
    },

    _deleteConfirmed: function(rules) {
        $.when.apply(jQuery, $.map(rules, $.proxy(function(rule) { return this.managementService.deleteDlpRule(this.domainName, rule.name); }, this)))
                .done($.proxy(function () { this.deferred.resolve(rules); this.deferred = null; }, this))
                .fail($.proxy(function () { this.deferred.reject(); this.deferred = null; }, this));
    }
});

/** @class */
jscape.domain.DlpRuleDialog = Class.extend(
    /** @lends {jscape.domain.DlpRuleDialog.prototype} */
{
    /** @constructor */
    initialize: function(newRule) {
        this.dialog = $("#d-dlp-rule").clone().attr("id", $.now().toString(36));
        this.dialog.dialog({
            width: 700, height: "auto",
            modal: true, resizable: false, closable: true, closed: true,
            buttons: [
                {text: window.R.string.DIALOG_BUTTON_OK || "Ok", handler: $.proxy(this._onSubmit, this)},
                {text: window.R.string.DIALOG_BUTTON_CANCEL || "Cancel", handler: $.proxy(this._onCancel, this)}
            ],
            onClose: $.proxy(function () {if (!this.closing) { this._onCancel(); }}, this)
        }).show();

        this.fieldset = $("fieldset[name=dlprulefields]", this.dialog);

        this.name = $("input[name=name]", this.fieldset).textbox({required: true, disabled: !newRule, validType: ["dlp_name_nonempty", "dlp_rule_unique"], missingMessage: window.R.string.DLP_ERROR_BAD_NAME, width: "60%"});
        this.description = $("input[name=description]", this.fieldset).textbox({multiline: true, width: "60%"});
        this.scope = $("input[name=scope]:radio", this.fieldset);
        this.expression = $("textarea[name=expression]", this.fieldset).textbox({required: true, multiline: true, validType: "dlp_expression", missingMessage: window.R.string.DLP_ERROR_BAD_EXPRESSION, width: "60%", height: 120});
    },

    show: function (listener) {
        this.listener = listener;
        this.dialog.dialog("center").dialog("open");
        this.fieldset.form("validate");
        this.expression.textbox("textbox").focus();
    },

    hide: function () {
        if (!this._closed()) {
            this.closing = true;
            this.dialog.dialog("close");
            this.fieldset.form("disableValidation");
            this.closing = false;
        }
    },

    destroy: function() {
        this.dialog.dialog("destroy");
    },

    setTitle: function(value) {
        this.dialog.dialog("setTitle", value);
    },

    getName: function() {
        return this.name.textbox("getValue");
    },

    setName: function(value) {
        this.name.textbox("setValue", value);
        if (!this.name.textbox("options").disabled && value && value.trim().length) {
            var opts = this.name.textbox("textbox").validatebox("options");
            opts.validType = ["dlp_name_nonempty", "dlp_rule_unique['{0}']".supplant({0: value})];
        }
    },

    getDescription: function() {
        return this.description.textbox("getValue");
    },

    setDescription: function(value) {
        this.description.textbox("setValue", value);
    },

    getScope: function() {
        return this.scope.filter(":checked").val();
    },

    setScope: function(value) {
        this.scope.filter("[value=" + value + "]").prop("checked", true);
    },

    getExpression: function() {
        return this.expression.textbox("getValue");
    },

    setExpression: function(value) {
        this.expression.textbox("setValue", value);
    },

    _onSubmit: function () {
        if (this.fieldset.form("validate")) {
            this.hide();
            this.listener.onSubmit(this);
        }
    },

    _onCancel: function () {
        this.hide();
        this.listener.onCancel(this);
    },

    _closed: function () {
        return !!this.dialog.dialog("options").closed;
    }
});

/** @class */
jscape.domain.DlpIcapController = jscape.domain.DomainModule.extend(
    /** @lends {jscape.domain.DlpIcapController.prototype} */
{
    DEFAULT_CONFIG: new jscape.DlpIcapConfiguration("127.0.0.1", 1344, 60000, "/", jscape.DlpIcapConfiguration.Method.RESPMOD),

    view: null,
    config: null,

    onCreate: function(managementService, domainName) {
        this._super.apply(this, arguments);

        this.managementService = managementService;
        this.domainName = domainName;
    },

    onStart: function() {
        if (this.view == null) {
            this.view = new jscape.domain.DlpIcapServerView();
        }
        this.view.show(this);
    },

    hasChanged: function () {
        return this.config != null && !Object._equals(this.config, this._createConfiguration());
    },

    onResume: function () {
        this.managementService.getDlpIcapConfiguration(this.domainName)
                .done($.proxy(function (config) { this.config = config; }, this))
                .fail($.proxy(function () { this.config = this.DEFAULT_CONFIG; }, this))
                .always($.proxy(function () { this.view && this._setupView(this.config); }, this));
    },

    onApply: function () {
        return this.managementService.setDlpIcapConfiguration(this.domainName, this._createConfiguration())
                .done($.proxy(function (config) { this.config = config; }, this))
                .done($.proxy(this._fireApplySuccess, this));
    },

    onDiscard: function() {
        this._setupView(this.config || this.DEFAULT_CONFIG);
    },

    onTestServer: function () {
        return this.managementService.testDlpIcapConfiguration(this.domainName, this._createConfiguration())
                .done(function () {
                          $.messager.alert(window.R.string.DLP_ICAP_TEST_SUCCESS_TITLE,
                                           window.R.string.DLP_ICAP_TEST_SUCCESS_MESSAGE,
                                           "info");
                      });
    },

    onSelectHeadersFile: function () {
        return new SelectPathController($.proxy(this.managementService.listFileSystem, this.managementService),
                                        $.proxy(this.managementService.createFileSystemDir, this.managementService))
                .selectFile().then(function (entry) { return entry.path});
    },

    onSelectDebugFile: function () {
        return new SelectPathController($.proxy(this.managementService.listDebugDir, this.managementService),
                                        $.proxy(this.managementService.createDebugDir, this.managementService))
                .selectFile().then(function (entry) { return entry.path; });
    },

    _createConfiguration: function() {
        return new jscape.DlpIcapConfiguration(this.view.getHost(),
                                               this.view.getPort(),
                                               this.view.getTimeout(),
                                               this.view.getPath(),
                                               this.view.getMethod(),
                                               this.view.getHeadersFile(),
                                               this.view.getStatusRegexp(),
                                               this.view.getHeadersRegexp(),
                                               this.view.getBodyRegexp(),
                                               this.view.getDebugFile());
    },

    _setupView: function(config) {
        if (config && this.view) {
            this.view.setHost(config.host);
            this.view.setPort(config.port);
            this.view.setTimeout(config.timeout);
            this.view.setPath(config.path);
            this.view.setMethod(config.method);
            this.view.setHeadersFile(config.headersFile);
            this.view.setStatusRegexp(config.statusViolationRegexp);
            this.view.setHeadersRegexp(config.headersViolationRegexp);
            this.view.setBodyRegexp(config.bodyViolationRegexp);
            this.view.setDebugFile(config.debugFile);
        }
    }
});

/** @class */
jscape.domain.DlpIcapServerView = Class.extend(
    /** @lends {jscape.domain.DlpIcapServerView.prototype} */
{
    TIMEOUT_FACTOR: 1000,

    /** @constructor */
    initialize: function() {
        this.fieldset = this.fieldset = $("fieldset[name=dlpicapfields]");

        this.host = $("input[name=host]", this.fieldset).textbox({required: true, validType: "non_empty", missingMessage: window.R.string.DLP_ICAP_ERROR_BAD_HOST, invalidMessage: window.R.string.DLP_ICAP_ERROR_BAD_HOST, width: "30%"});
        this.port = $("input[name=port]", this.fieldset).numberspinner({required: true, min: 1, max: 65535, width: 90});
        this.testButton = $("a[role=button][name=test]", this.fieldset).linkbutton({onClick: $.proxy(this._onClick$TestButton, this)});

        this.timeout = $("input[name=timeout]", this.fieldset).numberspinner({required: true, min: 1, max: 999, width: 60});
        this.path = $("input[name=path]", this.fieldset).textbox({required: true, validType: "non_empty", missingMessage: window.R.string.DLP_ICAP_ERROR_BAD_PATH, invalidMessage: window.R.string.DLP_ICAP_ERROR_BAD_PATH, width: "40%"});
        this.method = $("select[name=method]", this.fieldset).combobox({required: true, editable: false, width: 250, panelHeight: "auto"});

        this.headersFile= $("input[name=headersfile]", this.fieldset).textbox({width: "40%"});
        this.browseHeadersFileButton = $("a[role=button][name=browseheadersfile]", this.fieldset).linkbutton({onClick: $.proxy(this._onClick$BrowseHeadersFileButton, this)});

        this.debugFile = $("input[name=debugfile]", this.fieldset).textbox({width: "40%"});
        this.browseDebugFileButton = $("a[role=button][name=browsedebugfile]", this.fieldset).linkbutton({onClick: $.proxy(this._onClick$BrowseDebugFileButton, this)});

        this.statusRegexp = $("input[name=statusregexp]", this.fieldset).textbox({required: true, missingMessage: window.R.string.DLP_ICAP_BAD_REGEXP, invalidMessage: window.R.string.DLP_ICAP_BAD_REGEXP, width: "50%"});
        this.headersRegexp = $("textarea[name=headersregexp]", this.fieldset).textbox({multiline: true, validType: "non_empty", missingMessage: window.R.string.DLP_ICAP_BAD_REGEXP, invalidMessage: window.R.string.DLP_ICAP_BAD_REGEXP, width: "50%", height: 90});
        this.bodyRegexp = $("textarea[name=bodyregexp]", this.fieldset).textbox({multiline: true, validType: "non_empty", missingMessage: window.R.string.DLP_ICAP_BAD_REGEXP, invalidMessage: window.R.string.DLP_ICAP_BAD_REGEXP, width: "50%", height: 90});

        this.applyButton = $("a[role=button][name=apply]", this.fieldset).linkbutton({onClick: $.proxy(this._onClick$ApplyButton, this)});
        this.discardButton = $("a[role=button][name=discard]", this.fieldset).linkbutton({onClick: $.proxy(this._onClick$DiscardButton, this)});
    },

    getHost: function() {
        return this.host.textbox("getValue");
    },

    setHost: function(value) {
        this.host.textbox("setValue", value);
    },

    getPort: function() {
        return parseInt(this.port.numberspinner("getValue"));
    },

    setPort: function(value) {
        this.port.numberspinner("setValue", parseInt(value)).numberspinner("validate");
    },

    getTimeout: function() {
        return parseInt(this.timeout.numberspinner("getValue")) * this.TIMEOUT_FACTOR;
    },

    setTimeout: function(value) {
        this.timeout.numberspinner("setValue", value / this.TIMEOUT_FACTOR).numberspinner("validate");
    },

    getPath: function () {
        return this.path.textbox("getValue");
    },

    setPath: function (value) {
        this.path.textbox("setValue", value);
    },

    getMethod: function() {
        return this.method.combobox("getValue");
    },

    setMethod: function(value) {
        this.method.combobox("select", value);
    },

    getHeadersFile: function() {
        return this.headersFile.textbox("getValue");
    },

    setHeadersFile: function(value) {
        this.headersFile.textbox("setValue", value);
    },

    getDebugFile: function() {
        return this.debugFile.textbox("getValue");
    },

    setDebugFile: function(value) {
        this.debugFile.textbox("setValue", value);
    },

    getStatusRegexp: function() {
        return this.statusRegexp.textbox("getValue");
    },

    setStatusRegexp: function(value) {
        this.statusRegexp.textbox("setValue", value);
    },

    getHeadersRegexp: function() {
        return this.headersRegexp.textbox("getValue");
    },

    setHeadersRegexp: function(value) {
        this.headersRegexp.textbox("setValue", value);
    },

    getBodyRegexp: function() {
        return this.bodyRegexp.textbox("getValue");
    },

    setBodyRegexp: function(value) {
        this.bodyRegexp.textbox("setValue", value);
    },

    show: function(listener) {
        this.listener = listener || jscape.domain.DlpIcapServerView.LISTENER;
        this.fieldset.form("validate");
    },

    _onClick$BrowseHeadersFileButton: function() {
        this.listener.onSelectHeadersFile()
                .done($.proxy(function(path) {
                                  this.setHeadersFile(path);
                                  this.headersFile.textbox("textbox").select();
                              }, this));
    },

    _onClick$BrowseDebugFileButton: function() {
        this.listener.onSelectDebugFile()
                .done($.proxy(function (path) {
                          this.setDebugFile(path);
                          this.debugFile.textbox("textbox").select();
                      }, this));
    },

    _onClick$TestButton: function() {
        if (this.fieldset.form("validate")) {
            this.testButton.linkbutton("disable");
            this.listener.onTestServer()
                    .always($.proxy(function() { this.testButton.linkbutton("enable"); }, this));
        }
    },

    _onClick$ApplyButton: function() {
        if (this.fieldset.form("validate")) {
            this.applyButton.linkbutton("disable");
            this.listener.onApply()
                    .always($.proxy(function() { this.applyButton.linkbutton("enable"); }, this));
        }
    },

    _onClick$DiscardButton: function() {
        this.listener.onDiscard();
    }
});
jscape.domain.DlpIcapServerView.LISTENER = {
    onTestServer: $.noop,
    onSelectHeadersFile: $.noop,
    onSelectDebugFile: $.noop,
    onApply: $.noop,
    onDiscard: $.noop
};