<commit_before>package ontopoly.components;

import net.ontopia.topicmaps.core.OccurrenceIF;
import net.ontopia.utils.ObjectUtils;
import ontopoly.model.FieldInstance;
import ontopoly.models.FieldValueModel;
import ontopoly.pages.AbstractOntopolyPage;
import ontopoly.resources.Resources;

import org.apache.wicket.ResourceReference;
import org.apache.wicket.markup.ComponentTag;
import org.apache.wicket.markup.MarkupStream;
import org.apache.wicket.markup.html.IHeaderContributor;
import org.apache.wicket.markup.html.IHeaderResponse;
import org.apache.wicket.markup.html.WebComponent;
import org.apache.wicket.markup.html.form.TextArea;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.model.Model;

public class FieldInstanceHTMLArea extends Panel implements IHeaderContributor {

  private ResourceReference reference = new ResourceReference(Resources.class, "tiny_mce/tiny_mce.js");

  protected final FieldValueModel fieldValueModel;
  protected String oldValue;

  protected String cols = "70";
  protected String rows = "20";

  protected TextArea<String> textArea;
  
  public FieldInstanceHTMLArea(String id, FieldValueModel _fieldValueModel) {
    super(id);
    this.fieldValueModel = _fieldValueModel;
    
    OccurrenceIF occ = (OccurrenceIF)fieldValueModel.getObject();
    this.oldValue = (occ == null ? null : occ.getValue());
    setDefaultModel(new Model<String>(oldValue));
    
    this.textArea = new TextArea<String>("field", new Model<String>(oldValue)) {      
      @Override
      protected void onComponentTag(ComponentTag tag) {
        tag.setName("textarea");
        tag.put("cols", cols);
        tag.put("rows", rows);
        tag.put("class", getMarkupId());
        super.onComponentTag(tag);
      }
      
      @Override
      protected void onModelChanged() {
        super.onModelChanged();
        String newValue = (String)getModelObject();
        if (ObjectUtils.equals(newValue, oldValue)) return;
        AbstractOntopolyPage page = (AbstractOntopolyPage)getPage();
        FieldInstance fieldInstance = fieldValueModel.getFieldInstanceModel().getFieldInstance();
        if (fieldValueModel.isExistingValue() && oldValue != null)
          fieldInstance.removeValue(oldValue, page.getListener());
        if (newValue != null && !newValue.equals("")) {
          fieldInstance.addValue(newValue, page.getListener());
          fieldValueModel.setExistingValue(newValue);
        }
        oldValue = newValue;
      }      
    };
    textArea.setOutputMarkupId(true);
    add(textArea);
    
    add(new WebComponent("fieldScript") {
      protected void onComponentTagBody(final MarkupStream markupStream, final ComponentTag openTag) {
        StringBuffer sb = new StringBuffer();
        //sb.append("\ntinyMCE.onLoad();");
        sb.append("\ntinyMCE.execCommand('mceAddControl', true, '" + textArea.getMarkupId() + "');");
        replaceComponentTagBody(markupStream, openTag, sb.toString());
      }
    });
  }

  public void renderHead(IHeaderResponse response) {
    // import script    
    response.renderJavascriptReference(reference);
    // initializer script
    StringBuffer sb = new StringBuffer();
    sb.append("function onchangeTinyMCE(inst) {\n");
    sb.append("  if (inst.isDirty()) {\n");
    sb.append("    var value = inst.getBody().innerHTML;\n");<commit_msg>Added a work-around for issue 459.<commit_after>package ontopoly.components;

import net.ontopia.topicmaps.core.OccurrenceIF;
import net.ontopia.utils.ObjectUtils;
import ontopoly.model.FieldInstance;
import ontopoly.models.FieldValueModel;
import ontopoly.pages.AbstractOntopolyPage;
import ontopoly.resources.Resources;

import org.apache.wicket.ResourceReference;
import org.apache.wicket.markup.ComponentTag;
import org.apache.wicket.markup.MarkupStream;
import org.apache.wicket.markup.html.IHeaderContributor;
import org.apache.wicket.markup.html.IHeaderResponse;
import org.apache.wicket.markup.html.WebComponent;
import org.apache.wicket.markup.html.form.TextArea;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.model.Model;

public class FieldInstanceHTMLArea extends Panel implements IHeaderContributor {

  private ResourceReference reference = new ResourceReference(Resources.class, "tiny_mce/tiny_mce.js");

  protected final FieldValueModel fieldValueModel;
  protected String oldValue;

  protected String cols = "70";
  protected String rows = "20";

  protected TextArea<String> textArea;
  
  public FieldInstanceHTMLArea(String id, FieldValueModel _fieldValueModel) {
    super(id);
    this.fieldValueModel = _fieldValueModel;
    
    OccurrenceIF occ = (OccurrenceIF)fieldValueModel.getObject();
    this.oldValue = (occ == null ? null : occ.getValue());
    setDefaultModel(new Model<String>(oldValue));
    
    this.textArea = new TextArea<String>("field", new Model<String>(oldValue)) {      
      @Override
      protected void onComponentTag(ComponentTag tag) {
        tag.setName("textarea");
        tag.put("cols", cols);
        tag.put("rows", rows);
        tag.put("class", getMarkupId());
        super.onComponentTag(tag);
      }
      
      @Override
      protected void onModelChanged() {
        super.onModelChanged();
        String newValue = (String)getModelObject();
        if (ObjectUtils.equals(newValue, oldValue)) return;
        AbstractOntopolyPage page = (AbstractOntopolyPage)getPage();
        FieldInstance fieldInstance = fieldValueModel.getFieldInstanceModel().getFieldInstance();
        if (fieldValueModel.isExistingValue() && oldValue != null)
          fieldInstance.removeValue(oldValue, page.getListener());
        if (newValue != null && !newValue.equals("")) {
          fieldInstance.addValue(newValue, page.getListener());
          fieldValueModel.setExistingValue(newValue);
        }
        oldValue = newValue;
      }      
    };
    textArea.setOutputMarkupId(true);
    add(textArea);
    
    add(new WebComponent("fieldScript") {
      protected void onComponentTagBody(final MarkupStream markupStream, final ComponentTag openTag) {
        StringBuffer sb = new StringBuffer();
        //sb.append("\ntinyMCE.onLoad();");
        sb.append("\ntinyMCE.execCommand('mceAddControl', true, '" + textArea.getMarkupId() + "');");

        // the next two lines are a work-around for issue 459
        // http://code.google.com/p/ontopia/issues/detail?id=459
        // motivated by this posting
        // http://apache-wicket.1842946.n4.nabble.com/tinymce-textarea-in-a-modal-window-not-letting-to-type-td1886534.html
        sb.append("\ntinyMCE.execCommand('mceRemoveControl', false, '" + textArea.getMarkupId() + "');");
        sb.append("\ntinyMCE.execCommand('mceAddControl', true, '" + textArea.getMarkupId() + "');");
        // end of workaround
        
        replaceComponentTagBody(markupStream, openTag, sb.toString());
      }
    });
  }

  public void renderHead(IHeaderResponse response) {
    // import script    
    response.renderJavascriptReference(reference);
    // initializer script
    StringBuffer sb = new StringBuffer();
    sb.append("function onchangeTinyMCE(inst) {\n");
    sb.append("  if (inst.isDirty()) {\n");
    sb.append("    var value = inst.getBody().innerHTML;\n");
{'fraction_non_alphanumeric': 0.12077957727147955, 'fraction_numerical': 0.0043919846280538015, 'mean_word_length': 3.719559585492228, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, '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': '2018064', 'n_tokens_mistral': 2171, 'n_tokens_neox': 2118, 'n_words': 387}