<commit_before>package org.cytoscape.work.internal.tunables;

/*
 * #%L
 * Cytoscape Work Swing Impl (work-swing-impl)
 * $Id:$
 * $HeadURL:$
 * %%
 * Copyright (C) 2006 - 2013 The Cytoscape Consortium
 * %%
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as 
 * published by the Free Software Foundation, either version 2.1 of the 
 * License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Lesser Public License for more details.
 * 
 * You should have received a copy of the GNU General Lesser Public 
 * License along with this program.  If not, see
 * <http://www.gnu.org/licenses/lgpl-2.1.html>.
 * #L%
 */


import static javax.swing.GroupLayout.DEFAULT_SIZE;
import static javax.swing.GroupLayout.PREFERRED_SIZE;
import static org.cytoscape.work.internal.tunables.utils.GUIDefaults.setTooltip;
import static org.cytoscape.work.internal.tunables.utils.GUIDefaults.updateFieldPanel;

import java.awt.Component;
import java.awt.Dimension;
import java.awt.Window;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;

import javax.swing.BorderFactory;
import javax.swing.GroupLayout;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;

import org.cytoscape.io.DataCategory;
import org.cytoscape.util.swing.FileChooserFilter;
import org.cytoscape.util.swing.FileUtil;
import org.cytoscape.util.swing.LookAndFeelUtil;
import org.cytoscape.work.Tunable;
import org.cytoscape.work.internal.tunables.utils.SupportedFileTypesManager;
import org.cytoscape.work.swing.AbstractGUITunableHandler;
import org.cytoscape.work.swing.DirectlyPresentableTunableHandler;


/**
 * Handler for the type <i>File</i> of <code>Tunable</code>
 *
 * @author pasteur
 */
public class FileHandler extends AbstractGUITunableHandler implements DirectlyPresentableTunableHandler{
	
	private final FileUtil fileUtil;

	private JPanel controlPanel;
	private JButton browseButton;
	private JTextField textField;
	private ImageIcon image;
	private JLabel label;
	private SupportedFileTypesManager fileTypesManager;
	private boolean input;
	private List<FileChooserFilter> filters;

	private Window possibleParent;

	private String defaultString;


	/**
	 * Constructs the <code>GUIHandler</code> for the <code>File</code> type
	 *
	 * It creates the GUI which displays the path of the current file in a field, and provides
	 * access to a FileChooser with filtering parameters on
	 * <i>network</i>,<i>attributes</i>, or <i>session</i> (parameters are set in the
	 * <code>Tunable</code>'s annotations of the <code>File</code>)
	 *
	 * @param field the field that has been annotated
	 * @param obj object contained in <code>field</code>
	 * @param t the tunable associated to <code>field</code>
	 * @param fileTypesManager
	 */
	public FileHandler(final Field field, final Object obj, final Tunable t,
			final SupportedFileTypesManager fileTypesManager, final FileUtil fileUtil) {
		super(field, obj, t);
		this.fileTypesManager = fileTypesManager;
		this.fileUtil = fileUtil;
		init();
	}

	public FileHandler(final Method getter, final Method setter, final Object instance, final Tunable tunable,
			final SupportedFileTypesManager fileTypesManager, final FileUtil fileUtil) {
		super(getter, setter, instance, tunable);
		this.fileTypesManager = fileTypesManager;
		this.fileUtil = fileUtil;
		init();
	}

	private void init() {
		input = isInput();

		final String fileCategory = getFileCategory();
		final DataCategory dataCategory = DataCategory.valueOf(fileCategory.toUpperCase());
		filters = fileTypesManager.getSupportedFileTypes(dataCategory, input);
		String displayName = dataCategory.getDisplayName().toLowerCase();
		String a = isVowel( displayName.charAt(0) ) ? "an" : "a";
		defaultString = "Please select " + a + " " + displayName + " file...";

		setGui();
		
		updateFieldPanel(panel, label, controlPanel, horizontal);
		setTooltip(getTooltip(), textField, browseButton);
	}

	private boolean isVowel(char ch){
		ch=Character.toLowerCase(ch);
		return ch=='a' ||ch=='e' ||ch=='i' ||ch=='o' ||ch=='u';
	}

	/**
	 * To set a path to the object <code>File</code> <code>o</code>
	 *
	 * It creates a new <code>File</code> from the selected file in the FileChooser, or from the path to a file, entered by the user in the field
	 * The initial <code>File</code> object <code>o</code> is set with this new file
	 */
	@Override
	public void handle() {
		try {
			if (textField.getText().equals(defaultString) || textField.getText().isEmpty()) {
				setValue(null);
			} else {
				String path = textField.getText();
				File file = null;
				
				if (path.contains(System.getProperty("file.separator"))) {
					file = new File(path);
				} else {
					file = new File(System.getProperty("user.home"), path);
				}
				setValue(file);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@Override
	public void update(){
		final int load_or_save = input ? FileUtil.LOAD : FileUtil.SAVE;

		// Use the panel's parent if we have it, otherwise use the possible
		// parent specified in setFileTunableDirectly. 
		Component parentComponent = SwingUtilities.getWindowAncestor(panel);
		
		if (parentComponent == null)
			parentComponent = possibleParent;
		
		final File file = fileUtil.getFile(parentComponent, label.getText(), load_or_save, filters);
		
		if (file != null) {
			textField.setText(file.getAbsolutePath());
		} else {
			textField.setText(defaultString);
		}<commit_msg>FileHandler's update() method updates the text field based on the Tunable value (as all other TunableHandlers do) instead of popping up a file browser. This fixes an issue where using TunableUIHelper.refresh caused the file browser to pop up due to running update().<commit_after>package org.cytoscape.work.internal.tunables;

/*
 * #%L
 * Cytoscape Work Swing Impl (work-swing-impl)
 * $Id:$
 * $HeadURL:$
 * %%
 * Copyright (C) 2006 - 2013 The Cytoscape Consortium
 * %%
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as 
 * published by the Free Software Foundation, either version 2.1 of the 
 * License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Lesser Public License for more details.
 * 
 * You should have received a copy of the GNU General Lesser Public 
 * License along with this program.  If not, see
 * <http://www.gnu.org/licenses/lgpl-2.1.html>.
 * #L%
 */


import static javax.swing.GroupLayout.DEFAULT_SIZE;
import static javax.swing.GroupLayout.PREFERRED_SIZE;
import static org.cytoscape.work.internal.tunables.utils.GUIDefaults.setTooltip;
import static org.cytoscape.work.internal.tunables.utils.GUIDefaults.updateFieldPanel;

import java.awt.Component;
import java.awt.Dimension;
import java.awt.Window;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;

import javax.swing.BorderFactory;
import javax.swing.GroupLayout;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;

import org.cytoscape.io.DataCategory;
import org.cytoscape.util.swing.FileChooserFilter;
import org.cytoscape.util.swing.FileUtil;
import org.cytoscape.util.swing.LookAndFeelUtil;
import org.cytoscape.work.Tunable;
import org.cytoscape.work.internal.tunables.utils.SupportedFileTypesManager;
import org.cytoscape.work.swing.AbstractGUITunableHandler;
import org.cytoscape.work.swing.DirectlyPresentableTunableHandler;


/**
 * Handler for the type <i>File</i> of <code>Tunable</code>
 *
 * @author pasteur
 */
public class FileHandler extends AbstractGUITunableHandler implements DirectlyPresentableTunableHandler{
	
	private final FileUtil fileUtil;

	private JPanel controlPanel;
	private JButton browseButton;
	private JTextField textField;
	private ImageIcon image;
	private JLabel label;
	private SupportedFileTypesManager fileTypesManager;
	private boolean input;
	private List<FileChooserFilter> filters;

	private Window possibleParent;

	private String defaultString;


	/**
	 * Constructs the <code>GUIHandler</code> for the <code>File</code> type
	 *
	 * It creates the GUI which displays the path of the current file in a field, and provides
	 * access to a FileChooser with filtering parameters on
	 * <i>network</i>,<i>attributes</i>, or <i>session</i> (parameters are set in the
	 * <code>Tunable</code>'s annotations of the <code>File</code>)
	 *
	 * @param field the field that has been annotated
	 * @param obj object contained in <code>field</code>
	 * @param t the tunable associated to <code>field</code>
	 * @param fileTypesManager
	 */
	public FileHandler(final Field field, final Object obj, final Tunable t,
			final SupportedFileTypesManager fileTypesManager, final FileUtil fileUtil) {
		super(field, obj, t);
		this.fileTypesManager = fileTypesManager;
		this.fileUtil = fileUtil;
		init();
	}

	public FileHandler(final Method getter, final Method setter, final Object instance, final Tunable tunable,
			final SupportedFileTypesManager fileTypesManager, final FileUtil fileUtil) {
		super(getter, setter, instance, tunable);
		this.fileTypesManager = fileTypesManager;
		this.fileUtil = fileUtil;
		init();
	}

	private void init() {
		input = isInput();

		final String fileCategory = getFileCategory();
		final DataCategory dataCategory = DataCategory.valueOf(fileCategory.toUpperCase());
		filters = fileTypesManager.getSupportedFileTypes(dataCategory, input);
		String displayName = dataCategory.getDisplayName().toLowerCase();
		String a = isVowel( displayName.charAt(0) ) ? "an" : "a";
		defaultString = "Please select " + a + " " + displayName + " file...";

		setGui();
		
		updateFieldPanel(panel, label, controlPanel, horizontal);
		setTooltip(getTooltip(), textField, browseButton);
	}

	private boolean isVowel(char ch){
		ch=Character.toLowerCase(ch);
		return ch=='a' ||ch=='e' ||ch=='i' ||ch=='o' ||ch=='u';
	}

	/**
	 * To set a path to the object <code>File</code> <code>o</code>
	 *
	 * It creates a new <code>File</code> from the selected file in the FileChooser, or from the path to a file, entered by the user in the field
	 * The initial <code>File</code> object <code>o</code> is set with this new file
	 */
	@Override
	public void handle() {
		try {
			if (textField.getText().equals(defaultString) || textField.getText().isEmpty()) {
				setValue(null);
			} else {
				String path = textField.getText();
				File file = null;
				
				if (path.contains(System.getProperty("file.separator"))) {
					file = new File(path);
				} else {
					file = new File(System.getProperty("user.home"), path);
				}
				setValue(file);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@Override
	public void update(){
		
		File file = null;
		try {
			file = (File) getValue();
		} catch (IllegalAccessException | InvocationTargetException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		if (file != null) {
			textField.setText(file.getAbsolutePath());
		} else {
			textField.setText(defaultString);
		}
{'fraction_non_alphanumeric': 0.1070331312795815, 'fraction_numerical': 0.0021589304990450886, 'mean_word_length': 4.512128146453089, 'pattern_counts': {'":': 0, '<': 71, '<?xml version=': 0, '>': 71, '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': '7063933', 'n_tokens_mistral': 3883, 'n_tokens_neox': 3561, 'n_words': 1082}