/*
 * CODENVY CONFIDENTIAL
 * __________________
 *
 * [2013] - [2014] Codenvy, S.A.
 * All Rights Reserved.
 *
 * NOTICE:  All information contained herein is, and remains
 * the property of Codenvy S.A. and its suppliers,
 * if any.  The intellectual and technical concepts contained
 * herein are proprietary to Codenvy S.A.
 * and its suppliers and may be covered by U.S. and Foreign Patents,
 * patents in process, and are protected by trade secret or copyright law.
 * Dissemination of this information or reproduction of this material
 * is strictly forbidden unless prior written permission is obtained
 * from Codenvy S.A..
 */
package com.codenvy.ide.ext.datasource.client.sqllauncher;

import com.codenvy.ide.api.preferences.PreferencesManager;
import com.codenvy.ide.api.ui.workspace.AbstractPartPresenter;
import com.codenvy.ide.util.loging.Log;
import com.google.gwt.resources.client.ImageResource;
import com.google.gwt.user.client.ui.AcceptsOneWidget;
import com.google.inject.Inject;

public class SqlRequestLauncherPresenter extends AbstractPartPresenter implements SqlRequestLauncherView.ActionDelegate {

    /** Preference property name for default result limit. */
    private static final String               PREFERENCE_KEY_DEFAULT_REQUEST_LIMIT = "SqlEditor_default_request_limit";

    /** Default value for request limit (when no pref is set). */
    private static final int                  DEFAULT_REQUEST_LIMIT                = 20;
    /** The matching view. */
    private final SqlRequestLauncherView      view;
    /** The i18n-able constants. */
    private final SqlRequestLauncherConstants constants;

    private String                            selectedDatasourceId                 = null;
    private int                               resultLimit                          = DEFAULT_REQUEST_LIMIT;

    @Inject
    public SqlRequestLauncherPresenter(final SqlRequestLauncherView view,
                                       final SqlRequestLauncherConstants constants,
                                       final PreferencesManager preferencesManager) {
        this.view = view;
        this.view.setDelegate(this);
        this.constants = constants;

        final String prefRequestLimit = preferencesManager.getValue(PREFERENCE_KEY_DEFAULT_REQUEST_LIMIT);

        if (prefRequestLimit != null) {
            try {
                int prefValue = Integer.valueOf(prefRequestLimit);
                if (prefValue > 0) {
                    this.resultLimit = prefValue;
                } else {
                    Log.warn(SqlRequestLauncherPresenter.class, "negative value stored in preference "
                                                                + PREFERENCE_KEY_DEFAULT_REQUEST_LIMIT);
                }
            } catch (final NumberFormatException e) {
                StringBuilder sb = new StringBuilder("Preference stored in ")
                                                                             .append(PREFERENCE_KEY_DEFAULT_REQUEST_LIMIT)
                                                                             .append(" is not an integer (")
                                                                             .append(resultLimit)
                                                                             .append(").");
                Log.warn(SqlRequestLauncherPresenter.class, sb.toString());
            }
        }
    }

    @Override
    public String getTitle() {
        return this.constants.sqlEditorWindowTitle();
    }

    @Override
    public ImageResource getTitleImage() {
        return null;
    }

    @Override
    public String getTitleToolTip() {
        return null;
    }

    @Override
    public void go(final AcceptsOneWidget container) {
        container.setWidget(view);
    }

    @Override
    public void datasourceChanged(final String newDataSourceId) {
        this.selectedDatasourceId = newDataSourceId;
    }

    @Override
    public void resultLimitChanged(final int newResultLimit) {
        if (newResultLimit > 0) {
            this.resultLimit = newResultLimit;
        } else {
            this.view.setResultLimit(this.resultLimit);
        }
    }

    @Override
    public void executeRequested(final String request) {

    }
}

@@ -690,16 +690,71 @@
 ncher;%0A%0A
+import com.codenvy.ide.api.editor.EditorPartPresenter;%0A
 import c
@@ -859,32 +859,106 @@
 tPartPresenter;%0A
+import com.codenvy.ide.ext.datasource.client.sqleditor.SqlEditorProvider;%0A
 import com.coden
@@ -1961,16 +1961,71 @@
 LIMIT;%0A%0A
+    private EditorPartPresenter               editor;%0A%0A
     @Inj
@@ -2268,16 +2268,98 @@
 sManager
+,%0A                                       final SqlEditorProvider sqlEditorProvider
 ) %7B%0A    
@@ -2450,24 +2450,78 @@
 constants;%0A%0A
+        this.editor = sqlEditorProvider.getEditor();%0A%0A
         fina
@@ -4115,16 +4115,230 @@
 (view);%0A
+        editor.go(this.view.getEditorZone());%0A    %7D%0A%0A    @Override%0A    public boolean onClose() %7B%0A        return this.editor.onClose();%0A    %7D%0A%0A    @Override%0A    public void onOpen() %7B%0A        this.editor.onOpen();%0A
     %7D%0A%0A 

Add the SQL editor in the SQL launcher