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

net.sf.gluebooster.java.booster.basic.io.IoBoostUtilsBasic Maven / Gradle / Ivy

package net.sf.gluebooster.java.booster.basic.io;

import java.io.BufferedWriter;
import java.io.File;
import java.io.PrintStream;
import java.io.Writer;

import javax.swing.JOptionPane;

import net.sf.gluebooster.java.booster.basic.gui.DialogConfiguration;
import net.sf.gluebooster.java.booster.basic.gui.UserInteraction;
import net.sf.gluebooster.java.booster.basic.gui.UserInteractionBoostUtils;
import net.sf.gluebooster.java.booster.basic.meta.ObjectAnalyzer;
import net.sf.gluebooster.java.booster.basic.transformation.CallableByCalling;
import net.sf.gluebooster.java.booster.basic.transformation.CallableByMap;
import net.sf.gluebooster.java.booster.basic.transformation.CallableBySelecting;
import net.sf.gluebooster.java.booster.basic.transformation.CallableChain;
import net.sf.gluebooster.java.booster.basic.transformation.CallableToBoolean;
import net.sf.gluebooster.java.booster.essentials.eventsCommands.Callable;
import net.sf.gluebooster.java.booster.essentials.eventsCommands.CallableAbstraction;
import net.sf.gluebooster.java.booster.essentials.eventsCommands.CallableByConstant;
import net.sf.gluebooster.java.booster.essentials.utils.ContainerBoostUtils;

/**
 * Additional methods for I/O-operations.
 * 
 * @author CBauer
 * 
 * @defaultParamText printStream The involved print stream.
 */
@SuppressWarnings("unused")
public class IoBoostUtilsBasic {

	/**
	 * Get the text-out writer of a print stream.
	 * 
	 * @return the writer of the stream
	 */
	public static BufferedWriter getTextOut(PrintStream printStream)
			throws Exception {
		return ObjectAnalyzer.getPrivateField(printStream, "textOut");

	}
	
	/**
	 * Redirect the text-out of the print stream Optimization: The
	 * OutputStreamWriter of the PrintStream is not yet replaced
	 * 
	 * @param newTarget
	 *            the new target of the stream
	 * @param returnOldValue
	 *            should the current text-out-writer be returned
	 * @return null if the returnOldValue is not set
	 */
	public static BufferedWriter redirect(PrintStream printStream,
			Writer newTarget, boolean returnOldValue)
			throws Exception {

		BufferedWriter oldValue = null;
		if (returnOldValue)
			oldValue = getTextOut(printStream);
		BufferedWriter newTextOut = new BufferedWriter(newTarget);
		ObjectAnalyzer.setPrivateField(printStream, "textOut", newTextOut);
		return oldValue;
		
	}

	/**
	 * Enhances the output of a print stream
	 * 
	 * @param prefixOfNonWhitespaceText
	 *            additional prefix that is written if the text is more than
	 *            whitespaces.
	 * @param addStackTrace
	 *            should the stack trace be added when writing.
	 */
	public static void boostPrintStream(PrintStream printStream,
			String prefixOfNonWhitespaceText, boolean addStackTrace)
			throws Exception {
		WriterDelegate delegate = new WriterDelegate(
				IoBoostUtilsBasic.getTextOut(printStream));
		delegate.setPrefix(prefixOfNonWhitespaceText);
		delegate.setLogWritingStacktrace(addStackTrace);
		IoBoostUtilsBasic.redirect(printStream, delegate, false);

	}

	/**
	 * Input of the callable is [user interaction, default directory (optional)].
	 * 
	 * @param title
	 *            the title of the dialog
	 * @param overwritePossible
	 *            is overwriting possible (not yet used)
	 * @param selectFileOrDirectory
	 *            true = select file, false = select directory
	 * @return the selected file (null if the dialog is cancelled)
	 */
	public static CallableAbstraction saveFileDialog(String title, boolean overwritePossible, boolean selectFileOrDirectory) throws Exception {
		Callable selectSaveFile = UserInteractionBoostUtils.displayDialog(title, new CallableBySelecting("select user interaction", 0, UserInteraction.class),
				DialogConfiguration.chooseFile(title, false), false, File.class);

		Callable overwriteDialog = UserInteractionBoostUtils.displayDialog("Do you really want to overwrite?",
				new CallableBySelecting("select user interaction", 1, UserInteraction.class),
				DialogConfiguration.okCancel("Attention", "Do you really want to overwrite?" /* missing filename */), false, Integer.class);

		// Object precondition = selectSaveFile.getPrecondition();
		// Object postcondition = selectSaveFile.getPostcondition(null);
		DialogConfiguration overwrite = DialogConfiguration.okCancel("Overwrite", "File exists. Overwrite?");

		CallableChain result = new CallableChain("save file: " + title, //
				// user interaction, default directory (optional)
				new CallableByCalling("select file, keep user interaction",
						new Callable[] { selectSaveFile, new CallableBySelecting("select user interaction", 0) }),
				// File, user interaction
				CallableByCalling.doIf("save only if file is not null", CallableToBoolean.notNull("file must not be null", File.class),
						/* then */new Callable[] { CallableByCalling.doIf("if file exists ask to overwrite", CallableToBoolean.exists("if file exists"),
														/* then */ new Callable[] { CallableByCalling.doIf("ask to overwrite",
										new CallableChain("overwrite dialog, change result to boolean true if overwrite", overwriteDialog,
												CallableByMap.getFromMap("change to boolean",
														ContainerBoostUtils.createMap(JOptionPane.OK_OPTION, Boolean.TRUE), Boolean.FALSE)),
																new Callable[] { new CallableBySelecting("return the file (which will be overwritten", 0) },
																new Callable[] {
																		new CallableByConstant("else return null (file will not be overwritten)", null) }) },
										// DialogConfiguration.message("Overwrite?", "Do you really want to overwrite?")
										// TODO delete
										new Callable[] { new CallableBySelecting("else return the file", 0) }) },
								new Callable[] { new CallableByConstant("else return null (no file selected)", null) }

				));

		// new CallableByConstant(
		// new IllegalStateException("overwrite dialog not not yet implemented in saveFileDialog")) },
		// Object precondition = result.getPrecondition();
		return result;

	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy