All Downloads are FREE. Search and download functionalities are using the official Maven repository.

de.uni_leipzig.asv.util.commonFileChooser.CommonFileChooser Maven / Gradle / Ivy

Go to download

ASV Toolbox is a modular collection of tools for the exploration of written language data. They work either on word lists or text and solve several linguistic classification and clustering tasks. The topics covered contain language detection, POS-tagging, base form reduction, named entity recognition, and terminology extraction. On a more abstract level, the algorithms deal with various kinds of word similarity, using pattern based and statistical approaches. The collection can be used to work on large real world data sets as well as for studying the underlying algorithms. The ASV Toolbox can work on plain text files and connect to a MySQL database. While it is especially designed to work with corpora of the Leipzig Corpora Collection, it can easily be adapted to other sources.

The newest version!
/*******************************************************************************
 * The MIT License (MIT)

 * Copyright (c) 2007, University of Leipzig, Institut für Informatik,
 * Abteilung Autmatische Sprachverarbeitung

 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to
 * deal in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:

 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.

 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
******************************************************************************/
package de.uni_leipzig.asv.util.commonFileChooser;

import java.io.File;

import javax.swing.JFileChooser;
import javax.swing.JPanel;

/**
 * This class is to define a quasi standardized JFileChooser, so the
 * look-and-feel will be always the same.
 *
 * @author Daniel Zimmermann
 */
public class CommonFileChooser {

	private final JFileChooser chooser;

	private final ExtensionFileFilter eff;

	private String currentDirectory;

	/**
	 * This constructor creates the JFileChooser and the Extension-Filter.
	 *
	 * @param extensions
	 *            The extensions that have to be accepted by the program
	 * @param description
	 *            The description for these extensions
	 */
	public CommonFileChooser(String[] extensions, String description) {
		chooser = new JFileChooser();
		eff = new ExtensionFileFilter();

		currentDirectory = ".";

		if (extensions != null) {
			int l = extensions.length;
			for (int i = 0; i < l; i++) {
				addExtension(extensions[i]);
			}
		}

		if (description != null) {
			setDescription(description);
		}

		if (extensions != null) {
            chooser.setFileFilter(eff);
        }
	}

	/**
	 * Adds an extension to the extension-filter.
	 *
	 * @param extension
	 *            The extension to add
	 */
	public void addExtension(String extension) {
		eff.addExtension(extension);

		chooser.setFileFilter(eff);
	}

	/**
	 * Deletes all extensions from the extension-filter.
	 */
	public void clearExtensions() {
		eff.clearExtensions();

		chooser.setFileFilter(chooser.getAcceptAllFileFilter());
		// chooser.setFileFilter( eff );
	}

	/**
	 * Sets the description for the extensions.
	 *
	 * @param description
	 *            The description the user want so set up
	 */
	public void setDescription(String description) {
		eff.setDescription(description);

		chooser.setFileFilter(eff);
	}

	/**
	 * This method shows the JFileChooser for a parent JPanel and returns the
	 * path of the selected file.
	 *
	 * @param parent
	 *            The parent JPanel
	 * @param label
	 *            This parameter is to define the label of the dialog
	 * @return The name/path of the selected file
	 */
	public String showDialogAndReturnFilename(JPanel parent, String label) {
		String filename = new String();

		chooser.setCurrentDirectory(new File(currentDirectory));

		int result = chooser.showDialog(parent, label);

		if (result == JFileChooser.APPROVE_OPTION) {
			// if selected file approved the filter, store its path to an
			// attribute
			filename = chooser.getSelectedFile().getPath();

			currentDirectory = chooser.getSelectedFile().getAbsolutePath();
			if (isMSWindowsPlatform()) {
				currentDirectory = currentDirectory.substring(0,
						currentDirectory.lastIndexOf("\\"));
			} else {
				currentDirectory = currentDirectory.substring(0,
						currentDirectory.lastIndexOf("/"));
			}
			// System.out.println(currentDirectory);
		}
        else {
            return null;
        }

		return filename;
	}

	/**
	 * This method checks, whether the Operating System is Windows or not.
	 *
	 * @return true, if the OS is Windows, else false
	 */
	private boolean isMSWindowsPlatform() {
		String os = System.getProperty("os.name");
		if (os != null && os.startsWith("Windows")) {
            return true;
        }
        else {
            return false;
        }
	}

	public void setCurrentDirectory(String currentDirectory) {
		this.currentDirectory = currentDirectory;
	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy