<commit_before>/*
 * Created on 14.07.2005
 *
 */
package org.jdesktop.swingx.decorator;

import javax.swing.DefaultListSelectionModel;
import javax.swing.ListSelectionModel;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

/**
 * Responsible for keeping track of selection in model coordinates.<p>
 * 
 * updates view selection on pipeline change.
 * updates model selection on view selection change.
 * 
 * @author <NAME>
 */
public class Selection {

    /** selection in view coordinates. */
    private ListSelectionModel viewSelection;
    
    /** selection in model coordinates. */
    private DefaultListSelectionModel modelSelection;

    /** mapping pipeline. */
    private FilterPipeline pipeline;

    /** listener to view selection. */
    private ListSelectionListener viewSelectionListener;
    /** state flag for locking non-listening phases. */
    private boolean isListening;

    /** listener to mapping pipeline. */
    private PipelineListener pipelineListener;


    /**
     * PRE: selection != null;
     * 
     * @param pipeline
     * @param selection
     */
    public Selection(FilterPipeline pipeline, ListSelectionModel selection) {
        modelSelection = new DefaultListSelectionModel();
        setViewSelectionModel(selection);
        setFilters(pipeline);
    }

    /**
     * sets the view selection model. Must not be null.
     * 
     * @param selection holding selected indices in view coordinates
     */
    public void setViewSelectionModel(ListSelectionModel selection) {
        ListSelectionModel old = this.viewSelection;
        if (old != null) {
            old.removeListSelectionListener(viewSelectionListener);
            modelSelection.clearSelection();
        }
        this.viewSelection = selection;
        mapTowardsModel();
        viewSelection.addListSelectionListener(getViewSelectionListener());
        isListening = true;
    }

    public void setFilters(FilterPipeline pipeline) {
        FilterPipeline old = this.pipeline;
        if (old != null) {
            old.removePipelineListener(pipelineListener);
        }
        this.pipeline = pipeline;
        if (pipeline != null) {
            pipeline.addPipelineListener(getPipelineListener());
        }
        restoreSelection();
    }


    public void restoreSelection() {
//        if (pipeline == null) return;
        lock();
        // JW - hmm... clearSelection doesn't reset the lead/anchor. Why not?
        viewSelection.clearSelection();

        int[] selected = getSelectedRows(modelSelection);
        for (int i = 0; i < selected.length; i++) {
          int index = convertToView(selected[i]);
          // index might be -1, but then addSelectionInterval ignores it. 
          viewSelection.addSelectionInterval(index, index);
        }
        int lead = modelSelection.getLeadSelectionIndex();
        lead = convertToView(lead);
        if (viewSelection instanceof DefaultListSelectionModel) {
            ((DefaultListSelectionModel) viewSelection).moveLeadSelectionIndex(lead);
        } else {
            // PENDING: not tested, don't have a non-DefaultXX handy
            viewSelection.removeSelectionInterval(lead, lead);
            viewSelection.addSelectionInterval(lead, lead);
        }
        unlock();
    }<commit_msg>quick hack for preventing ArrayIndexOOB for -1 lead index<commit_after>/*
 * Created on 14.07.2005
 *
 */
package org.jdesktop.swingx.decorator;

import javax.swing.DefaultListSelectionModel;
import javax.swing.ListSelectionModel;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

/**
 * Responsible for keeping track of selection in model coordinates.<p>
 * 
 * updates view selection on pipeline change.
 * updates model selection on view selection change.
 * 
 * @author <NAME>
 */
public class Selection {

    /** selection in view coordinates. */
    private ListSelectionModel viewSelection;
    
    /** selection in model coordinates. */
    private DefaultListSelectionModel modelSelection;

    /** mapping pipeline. */
    private FilterPipeline pipeline;

    /** listener to view selection. */
    private ListSelectionListener viewSelectionListener;
    /** state flag for locking non-listening phases. */
    private boolean isListening;

    /** listener to mapping pipeline. */
    private PipelineListener pipelineListener;


    /**
     * PRE: selection != null;
     * 
     * @param pipeline
     * @param selection
     */
    public Selection(FilterPipeline pipeline, ListSelectionModel selection) {
        modelSelection = new DefaultListSelectionModel();
        setViewSelectionModel(selection);
        setFilters(pipeline);
    }

    /**
     * sets the view selection model. Must not be null.
     * 
     * @param selection holding selected indices in view coordinates
     */
    public void setViewSelectionModel(ListSelectionModel selection) {
        ListSelectionModel old = this.viewSelection;
        if (old != null) {
            old.removeListSelectionListener(viewSelectionListener);
            modelSelection.clearSelection();
        }
        this.viewSelection = selection;
        mapTowardsModel();
        viewSelection.addListSelectionListener(getViewSelectionListener());
        isListening = true;
    }

    public void setFilters(FilterPipeline pipeline) {
        FilterPipeline old = this.pipeline;
        if (old != null) {
            old.removePipelineListener(pipelineListener);
        }
        this.pipeline = pipeline;
        if (pipeline != null) {
            pipeline.addPipelineListener(getPipelineListener());
        }
        restoreSelection();
    }


    public void restoreSelection() {
//        if (pipeline == null) return;
        lock();
        // JW - hmm... clearSelection doesn't reset the lead/anchor. Why not?
        viewSelection.clearSelection();

        int[] selected = getSelectedRows(modelSelection);
        for (int i = 0; i < selected.length; i++) {
          int index = convertToView(selected[i]);
          // index might be -1, but then addSelectionInterval ignores it. 
          viewSelection.addSelectionInterval(index, index);
        }
        int lead = modelSelection.getLeadSelectionIndex();
        // PENDING: JW - this is a quick hack for spurious AIOB - need to enquire why 
        // they happen in the first place
        if (lead >= 0) {
            lead = convertToView(lead);
        }
        if (viewSelection instanceof DefaultListSelectionModel) {
            ((DefaultListSelectionModel) viewSelection).moveLeadSelectionIndex(lead);
        } else {
            // PENDING: not tested, don't have a non-DefaultXX handy
            viewSelection.removeSelectionInterval(lead, lead);
            viewSelection.addSelectionInterval(lead, lead);
        }
        unlock();
    }
{'fraction_non_alphanumeric': 0.09088269454123113, 'fraction_numerical': 0.003193960511033682, 'mean_word_length': 2.6315234580917237, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 8, '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': '1786541', 'n_tokens_mistral': 1712, 'n_tokens_neox': 1654, 'n_words': 471}