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

org.fife.ui.rtextfilechooser.Actions Maven / Gradle / Ivy

/*
 * 09/30/2009
 *
 * Actions.java - Actions used in a file chooser.
 * Copyright (C) 2009 Robert Futrell
 * http://fifesoft.com/rtext
 * Licensed under a modified BSD license.
 * See the included license file for details.
 */
package org.fife.ui.rtextfilechooser;

import java.awt.Component;
import java.awt.Toolkit;
import java.awt.Window;
import java.awt.datatransfer.Clipboard;
import java.awt.event.ActionEvent;
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;
import java.io.File;
import java.lang.reflect.Method;
import java.text.MessageFormat;
import java.util.Arrays;
import java.util.List;
import java.util.ResourceBundle;
import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.JOptionPane;
import javax.swing.KeyStroke;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;

import org.fife.ui.rtextfilechooser.extras.FileIOExtras;


/**
 * Actions for the file chooser.
 *
 * @author Robert Futrell
 * @version 1.0
 */
public interface Actions {


	/**
	 * Adds the currently-viewed directory to the file chooser "favorites."
	 */
	static class AddToFavoritesAction extends FileChooserAction {

		public AddToFavoritesAction(RTextFileChooser chooser) {
			super(chooser);
			putValue(Action.NAME, chooser.getString("AddToFavorites"));
		}

		public void actionPerformed(ActionEvent e) {
			File dir = chooser.getCurrentDirectory();
			chooser.addToFavorites(dir.getAbsolutePath());
			chooser.focusFileNameField(false);
		}

	}


	/**
	 * Copies any files selected in the file chooser's view.
	 */
	static class CopyAction extends FileChooserAction {

		private FileSelector chooser;

		public CopyAction(FileSelector chooser) {
			super(null);
			this.chooser = chooser;
			putValue(Action.NAME, getString("Copy"));
			int mod = Toolkit.getDefaultToolkit().getMenuShortcutKeyMask();
			putValue(Action.ACCELERATOR_KEY,
					KeyStroke.getKeyStroke(KeyEvent.VK_C, mod));
		}

		public void actionPerformed(ActionEvent e) {

			// Get the selected files.  If there are no selected files (i.e.,
			// they pressed "Ctrl+C" when no files were selected), beep.
			File[] files = null;
			if (chooser instanceof RTextFileChooser) {
				// Horrible hack!!!  File chooser shouldn't actually
				// implement FileSelector!  But it's view does...
				files = ((RTextFileChooser)chooser).getView().getSelectedFiles();
			}
			else { // FileSystemTree
				files = chooser.getSelectedFiles();
			}
			if (files==null || files.length==0) {
				UIManager.getLookAndFeel().provideErrorFeedback(null);
				return;
			}

			List fileList = Arrays.asList(files);
			FileListTransferable flt = new FileListTransferable(fileList);
			Clipboard clipboard = Toolkit.getDefaultToolkit().
												getSystemClipboard();
			clipboard.setContents(flt, flt);

		}

	}


	/**
	 * Action that handles deleting files.
	 */
	static class DeleteAction extends FileChooserAction {

		private boolean hard;

		/**
		 * Constructor.
		 *
		 * @param chooser The file chooser.
		 * @param hard Whether this is a "hard" delete (i.e. permanently delete,
		 *        rather than go through OS means and possibly put into a
		 *        Recycle Bin).
		 */
		public DeleteAction(RTextFileChooser chooser, boolean hard) {
			super(chooser);
			putValue(Action.NAME, getString("Delete"));
			this.hard = hard;
			int modifiers = hard ? InputEvent.SHIFT_MASK : 0;
			putValue(Action.ACCELERATOR_KEY,
					KeyStroke.getKeyStroke(KeyEvent.VK_DELETE, modifiers));
		}

		public void actionPerformed(ActionEvent e) {

			// Get the selected files.  If there are no selected files (i.e.,
			// they pressed "delete" when no files were selected), beep.
			File[] files = chooser.getView().getSelectedFiles();
			if (files==null || files.length==0) {
				UIManager.getLookAndFeel().provideErrorFeedback(chooser);
				return;
			}

			FileIOExtras extras = FileIOExtras.getInstance();
			if (!hard && extras!=null) {
				handleDeleteNative(files, extras);
			}
			else {
				handleDeleteViaJava(files);
			}

		}

		/**
		 * Uses the native means for deleting a file.  This allows us to use
		 * Windows' Recycle Bin, for example.
		 *
		 * @param files The files to delete.
		 * @param extras The native class that actually does the deletion.
		 */
		private void handleDeleteNative(File[] files, FileIOExtras extras) {
			Window parent = SwingUtilities.getWindowAncestor(chooser);
			if (extras.moveToRecycleBin(parent, files, true, true)) {
				refresh();
			}
			else {
				UIManager.getLookAndFeel().provideErrorFeedback(chooser);
			}
		}

		/**
		 * Deletes files with pure Java.  Only does a hard delete.
		 *
		 * @param files The files to delete.
		 */
		private void handleDeleteViaJava(File[] files) {

			// Prompt to confirm the file deletion.
			int count = files.length;
			int choice;
			if (count==1) {
				String fileName = files[0].getName();
				choice = JOptionPane.showConfirmDialog(chooser,
					chooser.getString("DeleteConfirmPrompt") + fileName + "?");
			}
			else { // count>1
				choice = JOptionPane.showConfirmDialog(chooser,
					chooser.getString("DeleteMultipleConfirmPrompt"));
			}

			// If they chose "yes," delete the files.
			if (choice==JOptionPane.YES_OPTION) {
				for (int i=0; i




© 2015 - 2024 Weber Informatics LLC | Privacy Policy