<commit_before>/*
 * PanmirrorHRefSelect.java
 *
 * Copyright (C) 2020 by RStudio, PBC
 *
 * Unless you have received this program directly from RStudio pursuant
 * to the terms of a commercial license agreement with RStudio, then
 * 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.panmirror.dialogs;

import org.rstudio.core.client.widget.CanFocus;
import org.rstudio.core.client.widget.FocusHelper;
import org.rstudio.studio.client.panmirror.dialogs.model.PanmirrorLinkCapabilities;
import org.rstudio.studio.client.panmirror.dialogs.model.PanmirrorLinkHeadingTarget;
import org.rstudio.studio.client.panmirror.dialogs.model.PanmirrorLinkTargets;
import org.rstudio.studio.client.panmirror.dialogs.model.PanmirrorLinkType;

import com.google.gwt.dom.client.Style.Unit;
import com.google.gwt.event.dom.client.ChangeHandler;
import com.google.gwt.event.shared.HandlerRegistration;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Focusable;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.SimplePanel;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;



public class PanmirrorHRefSelect extends Composite implements CanFocus
{
   public PanmirrorHRefSelect(PanmirrorLinkTargets targets, 
                              PanmirrorLinkCapabilities capabilities)
   {
      
      targets_ = targets;
      capabilities_ = capabilities;
       
      controls_ = new HorizontalPanel();
      controls_.setWidth("100%");
      
      type_ = new ListBox();
      type_.addItem("URL", Integer.toString(PanmirrorLinkType.URL));
      
      if (capabilities.headings && targets.headings.length > 0)
         type_.addItem("Heading", Integer.toString(PanmirrorLinkType.Heading));
      if (targets.ids.length > 0)
         type_.addItem("ID",Integer.toString(PanmirrorLinkType.ID));
      controls_.add(type_);
      type_.getElement().getStyle().setMarginRight(6, Unit.PX);
      controls_.setCellHorizontalAlignment(type_, HasHorizontalAlignment.ALIGN_LEFT);
        
      hrefContainer_ = new SimplePanel();
      hrefContainer_.setWidth("100%");
      controls_.add(hrefContainer_);
      controls_.setCellWidth(hrefContainer_, "100%");
      
      href_ = new TextBox();
      href_.setWidth("100%");
      
      headings_ = new ListBox(); 
      headings_.setWidth("100%");
      for (PanmirrorLinkHeadingTarget target : targets_.headings)
      {
         if (target.text.trim().length() > 0)
            headings_.addItem(target.text);
      }
      
      ids_ = new ListBox();
      ids_.setWidth("100%");
      for (String id : targets_.ids)
      {
         ids_.addItem("#" + id);
      }
      
      syncHRefType();
      type_.addChangeHandler((event) -> {
         syncHRefType();
      });
      
      VerticalPanel container = new VerticalPanel();
      container.add(new Label("Link To:"));
      container.add(controls_);
      
      initWidget(container);
   }
   
   
   public void setHRef(int type, String href)
   {
      if (type == PanmirrorLinkType.Heading && !capabilities_.headings) {
         type = PanmirrorLinkType.URL;
      }
      
      type_.setSelectedIndex(type);
      syncHRefType();
      
      if (getType() == PanmirrorLinkType.URL) 
      {
         href_.setText(href);
      }
      else if (getType() == PanmirrorLinkType.Heading)
      {
         for (int i = 0; i<targets_.headings.length; i++)
         {
            if (targets_.headings[i].text.equals(href))
            {
               headings_.setSelectedIndex(i);
               break;
            }
         }
      }
      else if (getType() == PanmirrorLinkType.ID)
      {
         for (int i = 0; i<targets_.ids.length; i++)
         {
            if (targets_.ids[i].equals(href))
            {
               ids_.setSelectedIndex(i);
               break;
            }
         }
      }  
   }
   <commit_msg>link dialog: improve handling of unknown headings/ids<commit_after>/*
 * PanmirrorHRefSelect.java
 *
 * Copyright (C) 2020 by RStudio, PBC
 *
 * Unless you have received this program directly from RStudio pursuant
 * to the terms of a commercial license agreement with RStudio, then
 * 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.panmirror.dialogs;

import org.rstudio.core.client.widget.CanFocus;
import org.rstudio.core.client.widget.FocusHelper;
import org.rstudio.studio.client.panmirror.dialogs.model.PanmirrorLinkCapabilities;
import org.rstudio.studio.client.panmirror.dialogs.model.PanmirrorLinkHeadingTarget;
import org.rstudio.studio.client.panmirror.dialogs.model.PanmirrorLinkTargets;
import org.rstudio.studio.client.panmirror.dialogs.model.PanmirrorLinkType;

import com.google.gwt.dom.client.Style.Unit;
import com.google.gwt.event.dom.client.ChangeHandler;
import com.google.gwt.event.shared.HandlerRegistration;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Focusable;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.SimplePanel;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;



public class PanmirrorHRefSelect extends Composite implements CanFocus
{
   public PanmirrorHRefSelect(PanmirrorLinkTargets targets, 
                              PanmirrorLinkCapabilities capabilities)
   {
      
      targets_ = targets;
      capabilities_ = capabilities;
       
      controls_ = new HorizontalPanel();
      controls_.setWidth("100%");
      
      type_ = new ListBox();
      type_.addItem("URL", Integer.toString(PanmirrorLinkType.URL));
      
      if (capabilities.headings && targets.headings.length > 0)
         type_.addItem("Heading", Integer.toString(PanmirrorLinkType.Heading));
      if (targets.ids.length > 0)
         type_.addItem("ID",Integer.toString(PanmirrorLinkType.ID));
      controls_.add(type_);
      type_.getElement().getStyle().setMarginRight(6, Unit.PX);
      controls_.setCellHorizontalAlignment(type_, HasHorizontalAlignment.ALIGN_LEFT);
        
      hrefContainer_ = new SimplePanel();
      hrefContainer_.setWidth("100%");
      controls_.add(hrefContainer_);
      controls_.setCellWidth(hrefContainer_, "100%");
      
      href_ = new TextBox();
      href_.setWidth("100%");
      
      headings_ = new ListBox(); 
      headings_.setWidth("100%");
      for (PanmirrorLinkHeadingTarget target : targets_.headings)
      {
         if (target.text.trim().length() > 0)
            headings_.addItem(target.text);
      }
      
      ids_ = new ListBox();
      ids_.setWidth("100%");
      for (String id : targets_.ids)
      {
         ids_.addItem("#" + id);
      }
      
      syncHRefType();
      type_.addChangeHandler((event) -> {
         syncHRefType();
      });
      
      VerticalPanel container = new VerticalPanel();
      container.add(new Label("Link To:"));
      container.add(controls_);
      
      initWidget(container);
   }
   
   
   public void setHRef(int type, String href)
   {
      // screen out sets of extended types if our capabilities don't support them
      if (type == PanmirrorLinkType.ID && (!capabilities_.attributes || targets_.ids.length == 0)) {
         type = PanmirrorLinkType.URL;
      } else if (type == PanmirrorLinkType.Heading && (!capabilities_.headings || targets_.headings.length == 0)) {
         type = PanmirrorLinkType.URL;
      }
      
      type_.setSelectedIndex(type);
      syncHRefType();
      
      if (getType() == PanmirrorLinkType.URL) 
      {
         href_.setText(href);
      }
      else if (getType() == PanmirrorLinkType.Heading)
      {   
         for (int i = 0; i<targets_.headings.length; i++)
         {
            if (targets_.headings[i].text.equals(href))
            {
               headings_.setSelectedIndex(i);
               break;
            }
         }
         if (!headings_.getSelectedValue().equalsIgnoreCase(href)) {
            headings_.addItem(href);
            headings_.setSelectedIndex(headings_.getItemCount() - 1); 
         }
      }
      else if (getType() == PanmirrorLinkType.ID)
      {
         for (int i = 0; i<targets_.ids.length; i++)
         {
            if (targets_.ids[i].equals(href))
            {
               ids_.setSelectedIndex(i);
               break;
            }
         }
         if (!ids_.getSelectedValue().equalsIgnoreCase(href)) {
            ids_.addItem(href);
            ids_.setSelectedIndex(ids_.getItemCount() - 1); 
         }
      }  
   }
   
{'fraction_non_alphanumeric': 0.11424100156494522, 'fraction_numerical': 0.006885758998435055, 'mean_word_length': 2.64209726443769, 'pattern_counts': {'":': 0, '<': 7, '<?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': '1922101', 'n_tokens_mistral': 2943, 'n_tokens_neox': 2861, 'n_words': 533}