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

net.sf.gluebooster.java.booster.basic.gui.swing.UserInteractionWithSwing Maven / Gradle / Ivy

package net.sf.gluebooster.java.booster.basic.gui.swing;

import java.awt.BorderLayout;
import java.io.File;
import java.util.concurrent.CancellationException;

import javax.swing.JDialog;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

import net.sf.gluebooster.java.booster.basic.gui.DialogConfiguration;
import net.sf.gluebooster.java.booster.essentials.utils.Constants;
import net.sf.gluebooster.java.booster.essentials.utils.IoBoostUtils;

/**
 * User interaction by using the JOptionPane and JFileChooser.
 * 
 * TODO move the code from JOptionPaneUserInteraction into this class and remove SwingBoostUtils
 * 
 * @defaultParamText dialog the configuration of the dialog
 * 
 * @author cbauer
 *
 */
public class UserInteractionWithSwing extends JOptionPaneUserInteraction {

	/**
	 * Should the clipboard be used to get the input.
	 */
	private boolean useClipboard;

	public UserInteractionWithSwing() {
	}

	public UserInteractionWithSwing(boolean useClipboard) {
		super();
		this.useClipboard = useClipboard;
	}


	@Override
	protected Object callImpl(DialogConfiguration... parameters) throws Exception {
		// Object command = parameters[0];
		DialogConfiguration dialog = parameters[0];
		Object command = dialog.getType();

		if (Constants.FILE.is(command) || Constants.DIRECTORY.is(command)) {
			return showFileDialog(dialog);
		} else if (Constants.TEXT.is(command)){
			return showTextDialog(dialog);
		} else {
			return super.callImpl(parameters);
		}

	}

	/**
	 * Shows a file dialog
	 * 
	 * @return the result of the dialog
	 */
	public File showFileDialog(DialogConfiguration dialog) throws Exception {
		File file = null;

		JFileChooser chooser = new JFileChooser();
		chooser.setDialogTitle(dialog.getTitle());
		File initialFile = null;
		Object value = dialog.getInitialValue();
		if (value != null) {
			File dir = null;
			if (value instanceof File) {
				dir = (File) value;
			} else {
				dir = new File(value.toString());
			}
			initialFile = dir;

			if (!dir.isDirectory()) {
				chooser.setSelectedFile(dir);
				dir = dir.getParentFile();
			}
			chooser.setCurrentDirectory(dir);

		}
		if (Constants.DIRECTORY.is(dialog.getType()))
			chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
		int returnVal = chooser.showOpenDialog(null);
		if (returnVal == JFileChooser.APPROVE_OPTION) {
			file = chooser.getSelectedFile();
		}

		if (file != null && !file.exists() && dialog.isResultMustExist()) {
			file = null;
		}

		if (file == null && dialog.isThrowExceptionIfNothingSelected()) {
			throw new CancellationException("No (existing) file selected");
		} else {
			if (file == null && dialog.isReturnValueIfCancelled()) {
				file = initialFile;
			}
			return file;
		}

	}

	/**
	 * Shows a text dialog
	 * 
	 * @return the input of the dialog
	 */
	public String showTextDialog(DialogConfiguration dialog) throws Exception {

		final JDialog jdialog = new JDialog();
		jdialog.setTitle(dialog.getTitle());
		jdialog.setModal(true);
		jdialog.setSize(600, 600);
		JTextArea area = new JTextArea();
		Object initial = dialog.getInitialValue();
		if (initial != null) {
			area.setText("" + initial);
		}
		jdialog.getContentPane().add(new JScrollPane(area), BorderLayout.CENTER);
		// jdialog.getContentPane().add(area, BorderLayout.CENTER);

		// jdialog.pack();

		jdialog.setVisible(true);
		if (useClipboard) {
			return IoBoostUtils.getClipboardString();
		} else {
			return area.getText();
		}
	}

	public boolean isUseClipboard() {
		return useClipboard;
	}

	public void setUseClipboard(boolean useClipboard) {
		this.useClipboard = useClipboard;
	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy