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

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

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

import java.awt.BorderLayout;
import java.awt.Component;

import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JTextField;

import org.apache.commons.lang3.tuple.ImmutablePair;
import org.apache.commons.lang3.tuple.Pair;

import net.sf.gluebooster.java.booster.basic.gui.DialogConfiguration;
import net.sf.gluebooster.java.booster.essentials.eventsCommands.CallableByConstant;
import net.sf.gluebooster.java.booster.essentials.eventsCommands.CallableByReflection;
import net.sf.gluebooster.java.booster.essentials.eventsCommands.CallableChain;
import net.sf.gluebooster.java.booster.essentials.eventsCommands.MultiListener;
import net.sf.gluebooster.java.booster.essentials.meta.objects.ObjectHolder;
import net.sf.gluebooster.java.booster.essentials.utils.Constants;

/**
 * Factory for swing objects.
 * 
 * TODO try to refactor the methods into the swing boost utils
 * 
 * @author CBauer
 * 
 */
public class SwingBoostUtils extends net.sf.gluebooster.java.booster.essentials.utils.SwingBoostUtils {

	/**
	 * Create a text field containing a file name together with a file chooser to select the file
	 * 
	 * @return the holder of the filename and the display component
	 */
	public static Pair createFileTextfieldWithFileChooser() throws Exception {
		JButton button = new JButton(">");
		JTextField textfield = new JTextField();
		ObjectHolder holder = ObjectHolder.createValueHolder(textfield);

		JPanel panel = new JPanel(new BorderLayout());
		panel.add(textfield, BorderLayout.CENTER);
		panel.add(button, BorderLayout.EAST);

		DialogConfiguration dialog = DialogConfiguration.chooseFile("Select the file", false);
		dialog.setReturnValueIfCancelled(true);

		CallableChain fieldValueSetter = new CallableChain<>("fieldValueSetter", 				new CallableByConstant("get the holder of the field", holder), //
				new CallableByReflection("extract the field value", null, "getValue"), // TransformerByInvoking.createMethodInvokingTransformer(null,
																													// "getValue"), // extract the field value
				new CallableByReflection("put the field value into the dialog as default", dialog, "setInitialValue"),
				new CallableByConstant("get the dialog configuration", dialog), //
				new CallableByReflection("display the dialog", new UserInteractionWithSwing(), "showFileDialog"), //

				new CallableByReflection("get the file path", null, "getCanonicalPath"), //
				new CallableByReflection("put it into the field", holder, "setValue") //
);
		
		MultiListener listener = new MultiListener(fieldValueSetter);
		
		// TransformationGraph fieldValueSetter = TransformationGraph.createChain(//
		// new CallableByConstant("get the holder of the field", holder), //
		// new CallableByReflection("extract the field value", null, "getValue"), // TransformerByInvoking.createMethodInvokingTransformer(null,
		// // "getValue"), // extract the field value
		// new CallableByReflection("put the field value into the dialog as default", dialog, "setInitialValue"),
		// new CallableByConstant("get the dialog configuration", dialog), //
		// new CallableByReflection("display the dialog", new UserInteractionWithSwing(), "showFileDialog"), //
		//
		// new CallableByReflection("get the file path", null, "getCanonicalPath"), //
		// new CallableByReflection("put it into the field", holder, "setValue") //
		// );
		// EventMulticaster listener = new EventMulticaster();
		// listener.addCommandHandler(fieldValueSetter);
		button.setActionCommand(Constants.START.toString());
		button.addActionListener(listener);

		return new ImmutablePair(holder, panel);
	}

	
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy