<commit_before>/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you 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 org.apache.uima.caseditor.editor;

import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.uima.cas.CAS;
import org.apache.uima.cas.Type;
import org.apache.uima.cas.TypeSystem;
import org.apache.uima.cas.text.AnnotationFS;
import org.eclipse.jface.dialogs.PopupDialog;
import org.eclipse.jface.viewers.ILabelProvider;
import org.eclipse.jface.viewers.ILabelProviderListener;
import org.eclipse.jface.viewers.IOpenListener;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.ITreeContentProvider;
import org.eclipse.jface.viewers.OpenEvent;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.jface.viewers.TreeViewer;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.jface.viewers.ViewerFilter;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.KeyEvent;
import org.eclipse.swt.events.KeyListener;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.MouseMoveListener;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.widgets.Tree;
import org.eclipse.swt.widgets.TreeItem;

/**
 * This is a lightweight popup dialog which creates an annotation of the
 * chosen type.
 */
class QuickTypeSelectionDialog extends PopupDialog {

  private final AnnotationEditor editor;

  private Text filterText;

  private Map<Character, Type> shortcutTypeMap = new HashMap<Character, Type>();

  private Map<Type, Character> typeShortcutMap = new HashMap<Type, Character>();

  /**
   * Initializes the current instance.
   *
   * @param parent
   * @param editor
   */
  QuickTypeSelectionDialog(Shell parent, AnnotationEditor editor) {
    super(parent, PopupDialog.INFOPOPUPRESIZE_SHELLSTYLE, true, true,
    			false, true, null, null);

    this.editor = editor;

    // key shortcuts are assigned automatically to types, the shortcut
    // mapping may change if the type system is modified

    String shortcutsString = "qwertzuiopasdfghjklyxcvbnm1234567890";

    Set<Character> shortcuts = new HashSet<Character>();

    for (int i = 0; i < shortcutsString.length(); i++) {
    	shortcuts.add(shortcutsString.charAt(i));
    }

    Set<Type> types = new HashSet<Type>();
   	Collections.addAll(types, getTypes());

   	// Try to create mappings with first letter of the type name as shortcut
   	for (Iterator<Type> it = types.iterator(); it.hasNext();) {

   		Type type = it.next();

   		String name = type.getShortName();

   		Character candidateChar = Character.toLowerCase(name.charAt(0));

   		if (shortcuts.contains(candidateChar)) {
   			putShortcut(candidateChar, type);

   			shortcuts.remove(candidateChar);
   			it.remove();
   		}
   	}

   	// Now assign letters to the remaining types
   	for (Iterator<Type> it = types.iterator(); it.hasNext();) {

   		if (shortcuts.size() > 0) {

   			Character candidateChar = shortcuts.iterator().next();

   			putShortcut(candidateChar, it.next());

   			shortcuts.remove(candidateChar);
   			it.remove();
   		}
   	}
  }

  private void putShortcut(Character shortcut, Type type) {
	  shortcutTypeMap.put(shortcut, type);
	  typeShortcutMap.put(type, shortcut);
  }

  @SuppressWarnings("unchecked")
  private Type[] getTypes() {

	  TypeSystem typeSystem = editor.getDocument().getCAS().getTypeSystem();

      List<Type> types = typeSystem.getProperlySubsumedTypes(typeSystem
              .getType(CAS.TYPE_NAME_ANNOTATION));

      return types.toArray(new Type[types.size()]);
  }<commit_msg>UIMA-1098 Improved the way the shortcuts are assigned <commit_after>/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you 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 org.apache.uima.caseditor.editor;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.uima.cas.CAS;
import org.apache.uima.cas.Type;
import org.apache.uima.cas.TypeSystem;
import org.apache.uima.cas.text.AnnotationFS;
import org.eclipse.jface.dialogs.PopupDialog;
import org.eclipse.jface.viewers.ILabelProvider;
import org.eclipse.jface.viewers.ILabelProviderListener;
import org.eclipse.jface.viewers.IOpenListener;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.ITreeContentProvider;
import org.eclipse.jface.viewers.OpenEvent;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.jface.viewers.TreeViewer;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.jface.viewers.ViewerFilter;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.KeyEvent;
import org.eclipse.swt.events.KeyListener;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.MouseMoveListener;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.widgets.Tree;
import org.eclipse.swt.widgets.TreeItem;

/**
 * This is a lightweight popup dialog which creates an annotation of the
 * chosen type.
 */
class QuickTypeSelectionDialog extends PopupDialog {

  private final AnnotationEditor editor;

  private Text filterText;

  private Map<Character, Type> shortcutTypeMap = new HashMap<Character, Type>();

  private Map<Type, Character> typeShortcutMap = new HashMap<Type, Character>();

  /**
   * Initializes the current instance.
   *
   * @param parent
   * @param editor
   */
  QuickTypeSelectionDialog(Shell parent, AnnotationEditor editor) {
    super(parent, PopupDialog.INFOPOPUPRESIZE_SHELLSTYLE, true, true,
    			false, true, null, null);

    this.editor = editor;

    // key shortcuts are assigned automatically to types, the shortcut
    // mapping may change if the type system is modified

    String shortcutsString = "qwertzuiopasdfghjklyxcvbnm1234567890";

    Set<Character> shortcuts = new HashSet<Character>();

    for (int i = 0; i < shortcutsString.length(); i++) {
    	shortcuts.add(shortcutsString.charAt(i));
    }

    List<Type> types = new ArrayList<Type>();
   	Collections.addAll(types, getTypes());
   	Collections.sort(types, new Comparator<Type>() {
		public int compare(Type o1, Type o2) {
			return o1.getName().compareTo(o2.getName());
		}
   	});

   	// Try to create mappings with first letter of the type name as shortcut
   	for (Iterator<Type> it = types.iterator(); it.hasNext();) {

   		Type type = it.next();

   		String name = type.getShortName();

   		Character candidateChar = Character.toLowerCase(name.charAt(0));

   		if (shortcuts.contains(candidateChar)) {
   			putShortcut(candidateChar, type);

   			shortcuts.remove(candidateChar);
   			it.remove();
   		}
   	}

   	// Try to create mappings with second letter of the type name as shortcut
   	for (Iterator<Type> it = types.iterator(); it.hasNext();) {

   		Type type = it.next();

   		String name = type.getShortName();

   		if (name.length() > 2) {
	   		Character candidateChar = Character.toLowerCase(name.charAt(1));
	
	   		if (shortcuts.contains(candidateChar)) {
	   			putShortcut(candidateChar, type);
	
	   			shortcuts.remove(candidateChar);
	   			it.remove();
	   		}
   		}
   	}
   	
   	// Now assign letters to the remaining types
   	for (Iterator<Type> it = types.iterator(); it.hasNext();) {

   		if (shortcuts.size() > 0) {

   			Character candidateChar = shortcuts.iterator().next();

   			putShortcut(candidateChar, it.next());

   			shortcuts.remove(candidateChar);
   			it.remove();
   		}
   	}
  }

  private void putShortcut(Character shortcut, Type type) {
	  shortcutTypeMap.put(shortcut, type);
	  typeShortcutMap.put(type, shortcut);
  }

  @SuppressWarnings("unchecked")
  private Type[] getTypes() {

	  TypeSystem typeSystem = editor.getDocument().getCAS().getTypeSystem();

      List<Type> types = typeSystem.getProperlySubsumedTypes(typeSystem
              .getType(CAS.TYPE_NAME_ANNOTATION));

      return types.toArray(new Type[types.size()]);
  }
{'fraction_non_alphanumeric': 0.10789240319243275, 'fraction_numerical': 0.004335402502709627, 'mean_word_length': 4.00246426811237, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 30, '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': '4674099', 'n_tokens_mistral': 3257, 'n_tokens_neox': 3089, 'n_words': 795}