<commit_before>/*
 * ScalarEdit.java
 *
 * Copyright (C) 2009-11 by RStudio, Inc.
 *
 * This program is licensed to you under the terms of version 3 of the
 * GNU Affero General Public License. This program is distributed WITHOUT
 * ANY EXPRESS OR IMPLIED WARRANTY, INCLUDING THOSE OF NON-INFRINGEMENT,
 * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. Please refer to the
 * AGPL (http://www.gnu.org/licenses/agpl-3.0.txt) for more details.
 *
 */
package org.rstudio.studio.client.workbench.views.workspace.table;

import com.google.gwt.event.dom.client.*;
import com.google.gwt.event.shared.HandlerRegistration;
import com.google.gwt.user.client.ui.HTML;
import org.rstudio.core.client.Debug;
import org.rstudio.core.client.dom.ElementEx;
import org.rstudio.core.client.theme.res.ThemeStyles;
import org.rstudio.core.client.widget.Operation;
import org.rstudio.studio.client.common.GlobalDisplay;
import org.rstudio.studio.client.workbench.views.workspace.events.ValueChangeRequestEvent;
import org.rstudio.studio.client.workbench.views.workspace.events.ValueChangeRequestHandler;

public class ScalarEdit<TValue> extends HTML implements InlineEditor.Display<TValue>,
                                                        ClickHandler
{
   public ScalarEdit(GlobalDisplay globalDisplay,
                     ScalarConversionStrategy<TValue> convert, 
                     TValue initialValue)
   {
      globalDisplay_ = globalDisplay ;
      convert_ = convert ;
      
      setStylePrimaryName(ThemeStyles.INSTANCE.scalarEdit()) ;
      
      addClickHandler(this) ;
      addDomHandler(new BlurHandler() {
         public void onBlur(BlurEvent event)
         {
            if (!ignoreBlur_)
               endEdit(true, false, false) ;
         }
      }, BlurEvent.getType()) ;
      addDomHandler(new KeyDownHandler() {
         public void onKeyDown(KeyDownEvent event)
         {
            switch (event.getNativeKeyCode())
            {
            case KeyCodes.KEY_ENTER:
               if (convert_.allowEnter())
                  return ;
               endEdit(true, true, true) ;
               event.preventDefault() ;
               break ;
            case KeyCodes.KEY_ESCAPE:
               endEdit(false, false, true) ;
               event.preventDefault() ;
               break ;
            }
         }
      }, KeyDownEvent.getType()) ;
      
      value_ = initialValue ;
      setText(convert_.convertToDisplayString(value_)) ;
   }
   
   public HandlerRegistration addValueChangeRequestHandler(
                                      ValueChangeRequestHandler<TValue> handler)
   {
      return addHandler(handler, ValueChangeRequestEvent.TYPE) ;
   }

   private void beginEdit()
   {
      if (!isEditing() && !pending_)
      {
         setText(convert_.convertToEditString(value_)) ;
         setEditMode(true) ;
      }
      getElement().focus() ;
   }
   
   private void endEdit(boolean attemptCommit, 
                        boolean commitEvenIfNoChange, 
                        boolean forceBlur)
   {
      if (!isEditing())
         return ;
      
      if (!attemptCommit)
      {
         setEditMode(false) ;
         setText(convert_.convertToDisplayString(value_)) ;
         
         if (forceBlur)
            ((ElementEx) getElement()).blur() ;
      }
      else
      {
         TValue newValue = null;
         String errorMsg = null ;
         try
         {
             newValue = convert_.convertToValue(getText()) ;
             if (!commitEvenIfNoChange && value_.equals(newValue))
             {
                endEdit(false, false, forceBlur) ;<commit_msg>1022: investigate scaler edit unhandled exception<commit_after>/*
 * ScalarEdit.java
 *
 * Copyright (C) 2009-11 by RStudio, Inc.
 *
 * This program is licensed to you under the terms of version 3 of the
 * GNU Affero General Public License. This program is distributed WITHOUT
 * ANY EXPRESS OR IMPLIED WARRANTY, INCLUDING THOSE OF NON-INFRINGEMENT,
 * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. Please refer to the
 * AGPL (http://www.gnu.org/licenses/agpl-3.0.txt) for more details.
 *
 */
package org.rstudio.studio.client.workbench.views.workspace.table;

import com.google.gwt.event.dom.client.*;
import com.google.gwt.event.shared.HandlerRegistration;
import com.google.gwt.user.client.ui.HTML;
import org.rstudio.core.client.Debug;
import org.rstudio.core.client.dom.ElementEx;
import org.rstudio.core.client.theme.res.ThemeStyles;
import org.rstudio.core.client.widget.Operation;
import org.rstudio.studio.client.common.GlobalDisplay;
import org.rstudio.studio.client.workbench.views.workspace.events.ValueChangeRequestEvent;
import org.rstudio.studio.client.workbench.views.workspace.events.ValueChangeRequestHandler;

public class ScalarEdit<TValue> extends HTML implements InlineEditor.Display<TValue>,
                                                        ClickHandler
{
   public ScalarEdit(GlobalDisplay globalDisplay,
                     ScalarConversionStrategy<TValue> convert, 
                     TValue initialValue)
   {
      globalDisplay_ = globalDisplay ;
      convert_ = convert ;
      
      setStylePrimaryName(ThemeStyles.INSTANCE.scalarEdit()) ;
      
      addClickHandler(this) ;
      addDomHandler(new BlurHandler() {
         public void onBlur(BlurEvent event)
         {
            if (!ignoreBlur_)
               endEdit(true, false, false) ;
         }
      }, BlurEvent.getType()) ;
      addDomHandler(new KeyDownHandler() {
         public void onKeyDown(KeyDownEvent event)
         {
            switch (event.getNativeKeyCode())
            {
            case KeyCodes.KEY_ENTER:
               if (convert_.allowEnter())
                  return ;
               endEdit(true, true, true) ;
               event.preventDefault() ;
               break ;
            case KeyCodes.KEY_ESCAPE:
               endEdit(false, false, true) ;
               event.preventDefault() ;
               break ;
            }
         }
      }, KeyDownEvent.getType()) ;
      
      value_ = initialValue ;
      setText(convert_.convertToDisplayString(value_)) ;
   }
   
   public HandlerRegistration addValueChangeRequestHandler(
                                      ValueChangeRequestHandler<TValue> handler)
   {
      return addHandler(handler, ValueChangeRequestEvent.TYPE) ;
   }

   private void beginEdit()
   {
      if (!isEditing() && !pending_)
      {
         setText(convert_.convertToEditString(value_)) ;
         setEditMode(true) ;
      }
      getElement().focus() ;
   }
   
   private void endEdit(boolean attemptCommit, 
                        boolean commitEvenIfNoChange, 
                        boolean forceBlur)
   {
      if (!isEditing())
         return ;
      
      if (!attemptCommit)
      {
         setEditMode(false) ;
         setText(convert_.convertToDisplayString(value_)) ;
         
         if (forceBlur)
            ((ElementEx) getElement()).blur() ;
      }
      else
      {
         TValue newValue = null;
         String errorMsg = null ;
         try
         {
             newValue = convert_.convertToValue(getText()) ;
             if (newValue == null ||
                 (!commitEvenIfNoChange && value_.equals(newValue)))
             {
                endEdit(false, false, forceBlur) ;
{'fraction_non_alphanumeric': 0.09182424159978234, 'fraction_numerical': 0.0029927900965854987, 'mean_word_length': 2.0493571132310247, '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': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '143984', 'n_tokens_mistral': 2017, 'n_tokens_neox': 1978, 'n_words': 393}