<commit_before>/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.micromanager.utils;

import java.awt.Component;
import java.awt.event.ActionEvent;
import javax.swing.AbstractCellEditor;
import javax.swing.JComboBox;
import javax.swing.JTextField;
import javax.swing.table.TableCellEditor;


import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.Arrays;
import javax.swing.JTable;
import org.micromanager.ConfigGroupPad.StateTableData;

/**
 *
 * @author arthur
 */
////////////////////////////////////////////////////////////////////////////
/**
 * Cell editing using either JTextField or JComboBox depending on whether the
 * property enforces a set of allowed values.
 */
public class StatePresetCellEditor extends AbstractCellEditor implements TableCellEditor {

    /**
     *
     */
    private static final long serialVersionUID = 1L;
// This is the component that will handle the editing of the cell value
    JTextField text_ = new JTextField();
    JComboBox combo_ = new JComboBox();
    StateItem item_;
    SliderPanel slider_ = new SliderPanel();

    public StatePresetCellEditor() {
        super();
        slider_.addEditActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                fireEditingStopped();
            }
        });

        slider_.addSliderMouseListener(new MouseAdapter() {

            public void mouseReleased(MouseEvent e) {
                fireEditingStopped();
            }
        });
    }

// This method is called when a cell value is edited by the user.
    public Component getTableCellEditorComponent(JTable table, Object value,
            boolean isSelected, int rowIndex, int vColIndex) {

        if (isSelected) {
            // cell (and perhaps other cells) are selected
        }

        StateTableData data = (StateTableData) table.getModel();
        item_ = data.getPropertyItem(rowIndex);
        // Configure the component with the specified value

        if (item_.allowed.length == 0) {
            text_.setText((String) value);
            return text_;
        }

        if (item_.allowed.length == 1) {
            if (item_.singleProp) {
                if (item_.hasLimits) {
                    // slider editing
                    if (item_.isInteger()) {
                        slider_.setLimits((int) item_.lowerLimit, (int) item_.upperLimit);
                    } else {
                        slider_.setLimits(item_.lowerLimit, item_.upperLimit);
                    }
                    slider_.setText((String) value);
                    return slider_;

                } else if (item_.singlePropAllowed != null && item_.singlePropAllowed.length > 0) {
                    setComboBox(item_.allowed);
                    return combo_;
                } else {
                    text_.setText((String) value);
                    return text_;
                }
            }
        }


        if( 1 < item_.allowed.length ){
           boolean allNumeric2 = true;
           // test that first character of every possible value is a numeral
           // if so, show user the list sorted by the numeric prefix
           for (int k = 0; k < item_.allowed.length; k++) {
              if (!Character.isDigit(item_.allowed[k].charAt(0))) {
                allNumeric2 = false;
                break;
              }
           }
           if (allNumeric2) {
            Arrays.sort(item_.allowed, new SortFunctionObjects.NumericPrefixStringComp());
           }
           else{<commit_msg>Fixed bug causing property groups with zero length strings to throw an error upon selection<commit_after>/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.micromanager.utils;

import java.awt.Component;
import java.awt.event.ActionEvent;
import javax.swing.AbstractCellEditor;
import javax.swing.JComboBox;
import javax.swing.JTextField;
import javax.swing.table.TableCellEditor;


import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.Arrays;
import javax.swing.JTable;
import org.micromanager.ConfigGroupPad.StateTableData;

/**
 *
 * @author arthur
 */
////////////////////////////////////////////////////////////////////////////
/**
 * Cell editing using either JTextField or JComboBox depending on whether the
 * property enforces a set of allowed values.
 */
public class StatePresetCellEditor extends AbstractCellEditor implements TableCellEditor {

    /**
     *
     */
    private static final long serialVersionUID = 1L;
// This is the component that will handle the editing of the cell value
    JTextField text_ = new JTextField();
    JComboBox combo_ = new JComboBox();
    StateItem item_;
    SliderPanel slider_ = new SliderPanel();

    public StatePresetCellEditor() {
        super();
        slider_.addEditActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                fireEditingStopped();
            }
        });

        slider_.addSliderMouseListener(new MouseAdapter() {

            public void mouseReleased(MouseEvent e) {
                fireEditingStopped();
            }
        });
    }

// This method is called when a cell value is edited by the user.
    public Component getTableCellEditorComponent(JTable table, Object value,
            boolean isSelected, int rowIndex, int vColIndex) {

        if (isSelected) {
            // cell (and perhaps other cells) are selected
        }

        StateTableData data = (StateTableData) table.getModel();
        item_ = data.getPropertyItem(rowIndex);
        // Configure the component with the specified value

        if (item_.allowed.length == 0) {
            text_.setText((String) value);
            return text_;
        }

        if (item_.allowed.length == 1) {
            if (item_.singleProp) {
                if (item_.hasLimits) {
                    // slider editing
                    if (item_.isInteger()) {
                        slider_.setLimits((int) item_.lowerLimit, (int) item_.upperLimit);
                    } else {
                        slider_.setLimits(item_.lowerLimit, item_.upperLimit);
                    }
                    slider_.setText((String) value);
                    return slider_;

                } else if (item_.singlePropAllowed != null && item_.singlePropAllowed.length > 0) {
                    setComboBox(item_.allowed);
                    return combo_;
                } else {
                    text_.setText((String) value);
                    return text_;
                }
            }
        }


        if( 1 < item_.allowed.length ){
           boolean allNumeric2 = true;
           // test that first character of every possible value is a numeral
           // if so, show user the list sorted by the numeric prefix
           for (int k = 0; k < item_.allowed.length; k++) {
              if (item_.allowed[k].length() > 0 && !Character.isDigit(item_.allowed[k].charAt(0))) {
                allNumeric2 = false;
                break;
              }
           }
           if (allNumeric2) {
            Arrays.sort(item_.allowed, new SortFunctionObjects.NumericPrefixStringComp());
           }
           else{
{'fraction_non_alphanumeric': 0.10672573499798631, 'fraction_numerical': 0.002819170358437374, 'mean_word_length': 1.8166351606805293, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 6, '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': '2275077', 'n_tokens_mistral': 1918, 'n_tokens_neox': 1820, 'n_words': 523}