/*
 * DBeaver - Universal Database Manager
 * Copyright (C) 2010-2017 Serge Rider (serge@jkiss.org)
 * Copyright (C) 2011-2012 Eugene Fradkin (eugene.fradkin@gmail.com)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.jkiss.dbeaver.ui.preferences;

import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.*;
import org.jkiss.dbeaver.ModelPreferences;
import org.jkiss.dbeaver.model.DBPDataSourceContainer;
import org.jkiss.dbeaver.model.preferences.DBPPreferenceStore;
import org.jkiss.dbeaver.ui.UIUtils;
import org.jkiss.dbeaver.utils.PrefUtils;

/**
 * PrefPageConnections
 */
public class PrefPageConnections extends TargetPrefPage
{
    public static final String PAGE_ID = "org.jkiss.dbeaver.preferences.main.connections"; //$NON-NLS-1$

    private Button overrideClientApplicationNameCheck;
    private Text clientApplicationNameText;

    public PrefPageConnections()
    {
        super();
    }

    @Override
    protected boolean hasDataSourceSpecificOptions(DBPDataSourceContainer dataSourceDescriptor)
    {
        DBPPreferenceStore store = dataSourceDescriptor.getPreferenceStore();
        return
            store.contains(ModelPreferences.META_CLIENT_NAME_OVERRIDE) ||
            store.contains(ModelPreferences.META_CLIENT_NAME_VALUE)
            ;
    }

    @Override
    protected boolean supportsDataSourceSpecificOptions()
    {
        return true;
    }

    @Override
    protected Control createPreferenceContent(Composite parent)
    {
        Composite composite = UIUtils.createPlaceholder(parent, 1, 5);
        {
            Group clientNameGroup = UIUtils.createControlGroup(composite, "Client Application Name", 2, GridData.FILL_HORIZONTAL, 0);

            final Label label = UIUtils.createLabel(clientNameGroup,
                "Client application name is passed to database server on connect to identify client connections.\n" +
                "By default it is set to product name + product version. You can set it to any custom value.");
            GridData gd = new GridData();
            gd.horizontalSpan = 2;
            label.setLayoutData(gd);
            overrideClientApplicationNameCheck = UIUtils.createCheckbox(clientNameGroup, "Override client application name", null, false, 2);
            overrideClientApplicationNameCheck.addSelectionListener(new SelectionAdapter() {
                @Override
                public void widgetSelected(SelectionEvent e) {
                    updateClientAppEnablement();
                }
            });
            clientApplicationNameText = UIUtils.createLabelText(clientNameGroup, "Client Application Name", "");
        }

        return composite;
    }

    private void updateClientAppEnablement() {
        clientApplicationNameText.setEnabled(overrideClientApplicationNameCheck.getSelection());
    }

    @Override
    protected void loadPreferences(DBPPreferenceStore store)
    {
        try {
            overrideClientApplicationNameCheck.setSelection(store.getBoolean(ModelPreferences.META_CLIENT_NAME_OVERRIDE));
            clientApplicationNameText.setText(store.getString(ModelPreferences.META_CLIENT_NAME_VALUE));

            updateClientAppEnablement();
        } catch (Exception e) {
            log.warn(e);
        }
    }

    @Override
    protected void savePreferences(DBPPreferenceStore store)
    {
        try {
            store.setValue(ModelPreferences.META_CLIENT_NAME_OVERRIDE, overrideClientApplicationNameCheck.getSelection());
            store.setValue(ModelPreferences.META_CLIENT_NAME_VALUE, clientApplicationNameText.getText());
        } catch (Exception e) {
            log.warn(e);
        }
        PrefUtils.savePreferenceStore(store);
    }

    @Override
    protected void clearPreferences(DBPPreferenceStore store)
    {
        store.setToDefault(ModelPreferences.META_CLIENT_NAME_OVERRIDE);
        store.setToDefault(ModelPreferences.META_CLIENT_NAME_VALUE);
    }

    @Override
    protected String getPropertyPageID()
    {
        return PAGE_ID;
    }

}
@@ -1174,16 +1174,60 @@
 efUtils;
+%0Aimport org.jkiss.dbeaver.core.CoreMessages;
 %0A%0A/**%0A *
@@ -2299,33 +2299,57 @@
 te, 
-%22Client Application Name%22
+CoreMessages.pref_page_database_client_name_group
 , 2,
@@ -2469,219 +2469,69 @@
     
-%22Client application name is passed to database server on connect to identify client connections.%5Cn%22 +%0A                %22By default it is set to product name + product version. You can set it to any custom value.%22
+CoreMessages.pref_page_database_client_name_group_description
 );%0A 
@@ -2736,25 +2736,62 @@
 up, 
-%22O
+CoreMessages.pref_page_database_label_o
 verride
- 
+_
 client
- 
+_
 appl
@@ -2801,14 +2801,13 @@
 tion
- 
+_
 name
-%22
 , nu
@@ -3171,17 +3171,54 @@
 up, 
-%22
 C
+oreMessages.pref_page_database_label_c
 lient
- A
+_a
 ppli
@@ -3227,14 +3227,13 @@
 tion
- N
+_n
 ame
-%22
 , %22%22
@@ -4636,8 +4636,9 @@
     %7D%0A%0A%7D
+%0A

Update PrefPageConnections.java