<commit_before>/*
 * Copyright 2014 Corpuslinguistic working group Humboldt University Berlin.
 *
 * 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 annis.gui.components.codemirror;

import annis.gui.components.ExceptionDialog;
import annis.libgui.Helper;
import annis.model.AqlParseError;
import com.google.common.collect.Lists;
import com.sun.jersey.api.client.AsyncWebResource;
import com.sun.jersey.api.client.ClientHandlerException;
import com.sun.jersey.api.client.UniformInterfaceException;
import com.vaadin.annotations.JavaScript;
import com.vaadin.annotations.StyleSheet;
import com.vaadin.data.Property;
import com.vaadin.data.util.ObjectProperty;
import com.vaadin.event.FieldEvents;
import com.vaadin.ui.AbstractJavaScriptComponent;
import com.vaadin.ui.JavaScriptFunction;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import org.json.JSONArray;
import org.json.JSONException;

/**
 * A code editor component for the ANNIQ Query Language.
 *
 * @author <NAME> <<EMAIL>>
 */
@JavaScript(
  {
    "vaadin://jquery.js",
    "lib/codemirror.js",
    "mode/aql/aql.js",
    "lib/edit/matchbrackets.js",
    "lib/lint/lint.js",
    "lib/display/placeholder.js",
    "AqlCodeEditor.js"
  })
@StyleSheet(
  {
    "lib/codemirror.css",
    "lib/lint/lint.css",
    "AqlCodeEditor.css"
  })
public class AqlCodeEditor extends AbstractJavaScriptComponent
  implements FieldEvents.TextChangeNotifier, Property.Viewer, Property.ValueChangeListener
{

  private int timeout;
  private Property<String> dataSource;

  public AqlCodeEditor()
  {
    addFunction("textChanged", new TextChangedFunction());
    addStyleName("aql-code-editor");
    
    AqlCodeEditor.this.setPropertyDataSource(new ObjectProperty("", String.class));
  }

  @Override
  public void setPropertyDataSource(Property newDataSource)
  {
    if(newDataSource == null)
    {
      throw new IllegalArgumentException("Data source must not be null");
    }
    
    if(this.dataSource instanceof Property.ValueChangeNotifier)
    {
      ((Property.ValueChangeNotifier) this.dataSource).removeValueChangeListener(this);
    }
        
    this.dataSource = newDataSource;
   
    if (newDataSource instanceof Property.ValueChangeNotifier)
    {
      ((Property.ValueChangeNotifier) this.dataSource).
        addValueChangeListener(this);
    }

  }

  @Override
  public Property getPropertyDataSource()
  {
    return this.dataSource;
  }

  @Override
  public void valueChange(Property.ValueChangeEvent event)
  {
    getState().text = this.dataSource.getValue();
    markAsDirty();
  }

  private class TextChangedFunction implements JavaScriptFunction
  {

    @Override
    public void call(JSONArray args) throws JSONException
    {
      getState().text = args.getString(0);
      getState().clientText = getState().text;
      
      validate(getState().text);
      final String textCopy = getState().text;
      final int cursorPos = args.getInt(1);
      fireEvent(new FieldEvents.TextChangeEvent(AqlCodeEditor.this)
      {

        @Override
        public String getText()
        {
          return textCopy;
        }

        @Override
        public int getCursorPosition()
        {
          return cursorPos;
        }
      });
    }
  }<commit_msg>don't only change the internal JS state but also the datasource when the text was updated from the client<commit_after>/*
 * Copyright 2014 Corpuslinguistic working group Humboldt University Berlin.
 *
 * 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 annis.gui.components.codemirror;

import annis.gui.components.ExceptionDialog;
import annis.libgui.Helper;
import annis.model.AqlParseError;
import com.google.common.collect.Lists;
import com.sun.jersey.api.client.AsyncWebResource;
import com.sun.jersey.api.client.ClientHandlerException;
import com.sun.jersey.api.client.UniformInterfaceException;
import com.vaadin.annotations.JavaScript;
import com.vaadin.annotations.StyleSheet;
import com.vaadin.data.Property;
import com.vaadin.data.util.ObjectProperty;
import com.vaadin.event.FieldEvents;
import com.vaadin.ui.AbstractJavaScriptComponent;
import com.vaadin.ui.JavaScriptFunction;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import org.json.JSONArray;
import org.json.JSONException;

/**
 * A code editor component for the ANNIQ Query Language.
 *
 * @author <NAME> <<EMAIL>>
 */
@JavaScript(
  {
    "vaadin://jquery.js",
    "lib/codemirror.js",
    "mode/aql/aql.js",
    "lib/edit/matchbrackets.js",
    "lib/lint/lint.js",
    "lib/display/placeholder.js",
    "AqlCodeEditor.js"
  })
@StyleSheet(
  {
    "lib/codemirror.css",
    "lib/lint/lint.css",
    "AqlCodeEditor.css"
  })
public class AqlCodeEditor extends AbstractJavaScriptComponent
  implements FieldEvents.TextChangeNotifier, Property.Viewer, Property.ValueChangeListener
{

  private int timeout;
  private Property<String> dataSource;

  public AqlCodeEditor()
  {
    addFunction("textChanged", new TextChangedFunction());
    addStyleName("aql-code-editor");
    
    AqlCodeEditor.this.setPropertyDataSource(new ObjectProperty("", String.class));
  }

  @Override
  public void setPropertyDataSource(Property newDataSource)
  {
    if(newDataSource == null)
    {
      throw new IllegalArgumentException("Data source must not be null");
    }
    
    if(this.dataSource instanceof Property.ValueChangeNotifier)
    {
      ((Property.ValueChangeNotifier) this.dataSource).removeValueChangeListener(this);
    }
        
    this.dataSource = newDataSource;
   
    if (newDataSource instanceof Property.ValueChangeNotifier)
    {
      ((Property.ValueChangeNotifier) this.dataSource).
        addValueChangeListener(this);
    }

  }

  @Override
  public Property getPropertyDataSource()
  {
    return this.dataSource;
  }

  @Override
  public void valueChange(Property.ValueChangeEvent event)
  {
    String oldText = getState().text;
    getState().text = this.dataSource.getValue();
    if(oldText == null || !oldText.equals(getState().text))
    {
      markAsDirty();
    }
  }

  private class TextChangedFunction implements JavaScriptFunction
  {

    @Override
    public void call(JSONArray args) throws JSONException
    {
      getState().text = args.getString(0);
      getPropertyDataSource().setValue(args.getString(0));
      getState().clientText = getState().text;
      
      validate(dataSource.getValue());
      final String textCopy = dataSource.getValue();
      final int cursorPos = args.getInt(1);
      fireEvent(new FieldEvents.TextChangeEvent(AqlCodeEditor.this)
      {

        @Override
        public String getText()
        {
          return textCopy;
        }

        @Override
        public int getCursorPosition()
        {
          return cursorPos;
        }
      });
    }
  }
{'fraction_non_alphanumeric': 0.10030506406345333, 'fraction_numerical': 0.0025625381330079315, 'mean_word_length': 3.609673790776153, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '1221212', 'n_tokens_mistral': 2380, 'n_tokens_neox': 2321, 'n_words': 555}