<commit_before>/*
 * Copyright (c) 2007-2012 The Broad Institute, Inc.
 * SOFTWARE COPYRIGHT NOTICE
 * This software and its documentation are the copyright of the Broad Institute, Inc. All rights are reserved.
 *
 * This software is supplied without any warranty or guaranteed support whatsoever. The Broad Institute is not responsible for its use, misuse, or functionality.
 *
 * This software is licensed under the terms of the GNU Lesser General Public License (LGPL),
 * Version 2.1 which is available at http://www.opensource.org/licenses/lgpl-2.1.php.
 */

/*
 * Created by JFormDesigner on Fri Sep 07 13:22:05 EDT 2012
 */

package org.broad.igv.ui;

import org.broad.igv.PreferenceManager;
import org.broad.igv.feature.genome.GenomeListItem;
import org.broad.igv.feature.genome.GenomeManager;
import org.broad.igv.ui.util.MessageUtils;

import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.awt.*;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.StringSelection;
import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * @author <NAME>
 */
public class ManageGenomesDialog extends JDialog {

    private List<GenomeListItem> allListItems;
    private boolean cancelled = true;
    private List<GenomeListItem> removedValuesList = new ArrayList<GenomeListItem>();
    private GenomeListItem currentGenomeItem = null;

    private boolean haveLocalGenomes = false;
    private static final String LOCAL_SEQUENCE_CHAR = "\u002A";

    public ManageGenomesDialog(Frame owner) {
        super(owner);
        initComponents();

        initData();

        genomeList.setCellRenderer(new GenomeCellRenderer());
    }

    private void initData() {
        allListItems = new ArrayList<GenomeListItem>(GenomeManager.getInstance().getGenomes());
        for(GenomeListItem item: allListItems){
            if(item.hasDownloadedSequence()){
                haveLocalGenomes = true;
                break;
            }
        }
        String genomeId = GenomeManager.getInstance().getGenomeId();
        currentGenomeItem = GenomeManager.getInstance().getLoadedGenomeListItemById(genomeId);
        buildList();
        genomeList.setTransferHandler(new SimpleTransferHandler());

        addButton.setEnabled(!GenomeManager.getInstance().isServerGenomeListUnreachable());
        label2.setVisible(haveLocalGenomes);
    }


    private void buildList() {
        genomeList.setListData(allListItems.toArray());
    }

    private void cancelButtonActionPerformed(ActionEvent e) {
        cancelled = true;
        removedValuesList = null;
        setVisible(false);
    }

    private void saveButtonActionPerformed(ActionEvent e) {
        if(removedValuesList.size() > 0){
            int countHasSeq = 0;
            for(GenomeListItem item: removedValuesList){
                if(item.hasDownloadedSequence()){
                    countHasSeq++;
                }
            }

            if(countHasSeq > 0){
                String msg = String.format("%d of the genomes you chose to remove have downloaded sequences. Those will be deleted as well. Are you sure?", countHasSeq);
                boolean sure = MessageUtils.confirm(msg);
                if(!sure) return;
            }
        }

        cancelled = false;
        PreferenceManager.getInstance().saveGenomeIdDisplayList(allListItems);
        setVisible(false);
    }

    private void removeSelected() {
        List<GenomeListItem> selectedValuesList = genomeList.getSelectedValuesList();
        if (selectedValuesList.contains(currentGenomeItem)) {
            MessageUtils.showMessage("Cannot remove currently selected genome " + currentGenomeItem.getDisplayableName());
            return;
        }
        removedValuesList.addAll(selectedValuesList);
        allListItems.removeAll(selectedValuesList);
        buildList();
    }

    public List<GenomeListItem> getRemovedValuesList() {
        return removedValuesList;
    }

    private void genomeListKeyReleased(KeyEvent e) {
        if (e.getKeyCode() == KeyEvent.VK_DELETE || e.getKeyCode() == KeyEvent.VK_BACK_SPACE) {
            removeSelected();
        }
    }

    private void removeButtonActionPerformed(ActionEvent e) {
        removeSelected();
    }

    private void addButtonActionPerformed(ActionEvent e) {
        Collection<GenomeListItem> inputListItems = GenomeManager.getInstance().getGenomeArchiveList();
        if(inputListItems == null){
            IOException exc = new IOException("Unable to reach genome server");
            MessageUtils.showErrorMessage(exc.getMessage(), exc);
            return;
        }<commit_msg>Allow removal of current genome<commit_after>/*
 * Copyright (c) 2007-2012 The Broad Institute, Inc.
 * SOFTWARE COPYRIGHT NOTICE
 * This software and its documentation are the copyright of the Broad Institute, Inc. All rights are reserved.
 *
 * This software is supplied without any warranty or guaranteed support whatsoever. The Broad Institute is not responsible for its use, misuse, or functionality.
 *
 * This software is licensed under the terms of the GNU Lesser General Public License (LGPL),
 * Version 2.1 which is available at http://www.opensource.org/licenses/lgpl-2.1.php.
 */

/*
 * Created by JFormDesigner on Fri Sep 07 13:22:05 EDT 2012
 */

package org.broad.igv.ui;

import org.broad.igv.PreferenceManager;
import org.broad.igv.feature.genome.GenomeListItem;
import org.broad.igv.feature.genome.GenomeManager;
import org.broad.igv.ui.util.MessageUtils;

import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.awt.*;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.StringSelection;
import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * @author <NAME>
 */
public class ManageGenomesDialog extends JDialog {

    private List<GenomeListItem> allListItems;
    private boolean cancelled = true;
    private List<GenomeListItem> removedValuesList = new ArrayList<GenomeListItem>();
    private GenomeListItem currentGenomeItem = null;

    private boolean haveLocalGenomes = false;
    private static final String LOCAL_SEQUENCE_CHAR = "\u002A";

    public ManageGenomesDialog(Frame owner) {
        super(owner);
        initComponents();

        initData();

        genomeList.setCellRenderer(new GenomeCellRenderer());
    }

    private void initData() {
        allListItems = new ArrayList<GenomeListItem>(GenomeManager.getInstance().getGenomes());
        for(GenomeListItem item: allListItems){
            if(item.hasDownloadedSequence()){
                haveLocalGenomes = true;
                break;
            }
        }
        String genomeId = GenomeManager.getInstance().getGenomeId();
        currentGenomeItem = GenomeManager.getInstance().getLoadedGenomeListItemById(genomeId);
        buildList();
        genomeList.setTransferHandler(new SimpleTransferHandler());

        addButton.setEnabled(!GenomeManager.getInstance().isServerGenomeListUnreachable());
        label2.setVisible(haveLocalGenomes);
    }


    private void buildList() {
        genomeList.setListData(allListItems.toArray());
    }

    private void cancelButtonActionPerformed(ActionEvent e) {
        cancelled = true;
        removedValuesList = null;
        setVisible(false);
    }

    private void saveButtonActionPerformed(ActionEvent e) {
        if(removedValuesList.size() > 0){
            int countHasSeq = 0;
            for(GenomeListItem item: removedValuesList){
                if(item.hasDownloadedSequence()){
                    countHasSeq++;
                }
            }

            if(countHasSeq > 0){
                String msg = String.format("%d of the genomes you chose to remove have downloaded sequences. Those will be deleted as well. Are you sure?", countHasSeq);
                boolean sure = MessageUtils.confirm(msg);
                if(!sure) return;
            }
        }

        cancelled = false;
        PreferenceManager.getInstance().saveGenomeIdDisplayList(allListItems);
        setVisible(false);
    }

    private void removeSelected() {
        List<GenomeListItem> selectedValuesList = genomeList.getSelectedValuesList();
//        if (selectedValuesList.contains(currentGenomeItem)) {
//            MessageUtils.showMessage("Cannot remove currently selected genome " + currentGenomeItem.getDisplayableName());
//            return;
//        }
        removedValuesList.addAll(selectedValuesList);
        allListItems.removeAll(selectedValuesList);
        buildList();
    }

    public List<GenomeListItem> getRemovedValuesList() {
        return removedValuesList;
    }

    private void genomeListKeyReleased(KeyEvent e) {
        if (e.getKeyCode() == KeyEvent.VK_DELETE || e.getKeyCode() == KeyEvent.VK_BACK_SPACE) {
            removeSelected();
        }
    }

    private void removeButtonActionPerformed(ActionEvent e) {
        removeSelected();
    }

    private void addButtonActionPerformed(ActionEvent e) {
        Collection<GenomeListItem> inputListItems = GenomeManager.getInstance().getGenomeArchiveList();
        if(inputListItems == null){
            IOException exc = new IOException("Unable to reach genome server");
            MessageUtils.showErrorMessage(exc.getMessage(), exc);
            return;
        }
{'fraction_non_alphanumeric': 0.09280226445612616, 'fraction_numerical': 0.006267691063485645, 'mean_word_length': 3.0930906081919733, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 23, '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': '3129205', 'n_tokens_mistral': 2724, 'n_tokens_neox': 2599, 'n_words': 616}