<commit_before>package com.niafikra.meddela.ui.vaadin.dashboard.settings.notifications

import com.vaadin.ui.Window
import org.vaadin.aceeditor.AceEditor
import com.vaadin.ui.Button
import com.vaadin.ui.themes.Reindeer
import com.vaadin.ui.Label
import com.vaadin.ui.Panel
import org.vaadin.aceeditor.gwt.ace.AceMode
import com.niafikra.meddela.utilities.SqlUtil

import groovy.sql.Sql
import groovy.sql.GroovyRowResult
import com.niafikra.meddela.utilities.GStringUtil
import com.vaadin.ui.Window.CloseEvent
import com.vaadin.ui.Window.CloseListener
import com.niafikra.meddela.data.Notification
import com.vaadin.ui.VerticalLayout

/**
 * Pops out a window that allows the user to edit and test his code
 *
 * @author mbw<NAME>
 * Date: 14/08/12
 * Time: 14:44
 */
class CodeAreaPopupWindow extends Window implements Button.ClickListener, CloseListener {
    private AceEditor aceEditor;
    private Button testButton
    private Label resultsLabel;
    private AceEditor originalEditor
    Notification notification

    CodeAreaPopupWindow(AceEditor originalEditor, Notification notification) {
        this.notification = notification

        setCaption("edit code")
        setWidth("705px")
        setHeight("600px")
        setModal(true)
        setContent(new VerticalLayout())
        getContent().setSizeFull()

        addListener((CloseListener) this)

        this.originalEditor = originalEditor
        aceEditor = new AceEditor()
        aceEditor.setWidth("700px")
        aceEditor.setHeight("350px")
        aceEditor.setMode(originalEditor.mode)
        aceEditor.setValue(originalEditor.value)
        addComponent(aceEditor)

        testButton = new Button("test")
        testButton.setStyleName(Reindeer.BUTTON_SMALL)
        testButton.setWidth("100%")
        testButton.addListener(this)
        addComponent(testButton)

        Panel resultsPanel = new Panel()
        resultsPanel.setSizeFull()
        resultsLabel = new Label()
        resultsLabel.setSizeFull()
        resultsPanel.addComponent(resultsLabel)
        addComponent(resultsPanel)
        getContent().setExpandRatio(resultsPanel, 1)

    }

    @Override
    void buttonClick(Button.ClickEvent clickEvent) {

        SqlUtil.runWithSqlConnection(notification) { Sql sql, notification ->
            try {
                def results
                if (originalEditor.mode == AceMode.sql) {
                    results = sql.rows(aceEditor.value)

                } else {
                    def bindings = GStringUtil.setUpDates()
                    bindings[sql] = sql

                    GroovyShell shell = new GroovyShell(new Binding(bindings))
                    results = shell.evaluate(aceEditor.value)
                }
                renderResults(results)

            } catch (Exception e) {
                resultsLabel.setContentMode(Label.CONTENT_PREFORMATTED)
                resultsLabel.setValue(e.printStackTrace())
            }
        }

    }<commit_msg>made code area popup window present show errors when they occur<commit_after>package com.niafikra.meddela.ui.vaadin.dashboard.settings.notifications

import com.vaadin.ui.Window
import org.vaadin.aceeditor.AceEditor
import com.vaadin.ui.Button
import com.vaadin.ui.themes.Reindeer
import com.vaadin.ui.Label
import com.vaadin.ui.Panel
import org.vaadin.aceeditor.gwt.ace.AceMode
import com.niafikra.meddela.utilities.SqlUtil

import groovy.sql.Sql
import groovy.sql.GroovyRowResult
import com.niafikra.meddela.utilities.GStringUtil
import com.vaadin.ui.Window.CloseEvent
import com.vaadin.ui.Window.CloseListener
import com.niafikra.meddela.data.Notification
import com.vaadin.ui.VerticalLayout

/**
 * Pops out a window that allows the user to edit and test his code
 *
 * @author mbwana <NAME>
 * Date: 14/08/12
 * Time: 14:44
 */
class CodeAreaPopupWindow extends Window implements Button.ClickListener, CloseListener {
    private AceEditor aceEditor;
    private Button testButton
    private Label resultsLabel;
    private AceEditor originalEditor
    Notification notification

    CodeAreaPopupWindow(AceEditor originalEditor, Notification notification) {
        this.notification = notification

        setCaption("edit code")
        setWidth("705px")
        setHeight("600px")
        setModal(true)
        setContent(new VerticalLayout())
        getContent().setSizeFull()

        addListener((CloseListener) this)

        this.originalEditor = originalEditor
        aceEditor = new AceEditor()
        aceEditor.setWidth("700px")
        aceEditor.setHeight("350px")
        aceEditor.setMode(originalEditor.mode)
        aceEditor.setValue(originalEditor.value)
        addComponent(aceEditor)

        testButton = new Button("test")
        testButton.setStyleName(Reindeer.BUTTON_SMALL)
        testButton.setWidth("100%")
        testButton.addListener(this)
        addComponent(testButton)

        Panel resultsPanel = new Panel()
        resultsPanel.setSizeFull()
        resultsLabel = new Label()
        resultsLabel.setSizeFull()
        resultsPanel.addComponent(resultsLabel)
        addComponent(resultsPanel)
        getContent().setExpandRatio(resultsPanel, 1)

    }

    @Override
    void buttonClick(Button.ClickEvent clickEvent) {

        SqlUtil.runWithSqlConnection(notification) { Sql sql, notification ->
            try {
                def results
                if (originalEditor.mode == AceMode.sql) {
                    results = sql.rows(aceEditor.value)

                } else {
                    def bindings = GStringUtil.setUpDates()
                    bindings['sql'] = sql

                    GroovyShell shell = new GroovyShell(new Binding(bindings))
                    results = shell.evaluate(aceEditor.value)
                }
                renderResults(results)

            } catch (Exception e) {
                resultsLabel.setContentMode(Label.CONTENT_PREFORMATTED)
                resultsLabel.setValue(e)
            }
        }

    }
{'fraction_non_alphanumeric': 0.08305592543275632, 'fraction_numerical': 0.008655126498002663, 'mean_word_length': 2.6220614828209765, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, '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': '7156672', 'n_tokens_mistral': 1751, 'n_tokens_neox': 1616, 'n_words': 319}