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

xy.reflect.ui.info.method.ChainedMethodsInfo Maven / Gradle / Ivy

/*******************************************************************************
 * Copyright (C) 2018 OTK Software
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * The GNU General Public License allows you also to freely redistribute 
 * the libraries under the same license, if you provide the terms of the 
 * GNU General Public License with them and add the following 
 * copyright notice at the appropriate place (with a link to 
 * http://javacollection.net/reflectionui/ web site when possible).
 ******************************************************************************/
package xy.reflect.ui.info.method;

import java.util.Collections;
import java.util.List;
import java.util.Map;

import xy.reflect.ui.ReflectionUI;
import xy.reflect.ui.info.InfoCategory;
import xy.reflect.ui.info.ResourcePath;
import xy.reflect.ui.info.ValueReturnMode;
import xy.reflect.ui.info.parameter.IParameterInfo;
import xy.reflect.ui.info.type.ITypeInfo;
import xy.reflect.ui.info.type.source.SpecificitiesIdentifier;
import xy.reflect.ui.info.type.source.TypeInfoSourceProxy;
import xy.reflect.ui.undo.IrreversibleModificationException;
import xy.reflect.ui.util.FututreActionBuilder;
import xy.reflect.ui.util.ReflectionUIError;
import xy.reflect.ui.util.ReflectionUIUtils;

public class ChainedMethodsInfo implements IMethodInfo {

	protected ReflectionUI reflectionUI;
	protected IMethodInfo method1;
	protected IMethodInfo method2;
	protected FututreActionBuilder undoJobBuilder;
	protected ITypeInfo containingType;
	private boolean returnValueVoid = false;
	private ITypeInfo returnValueType;

	public ChainedMethodsInfo(ReflectionUI reflectionUI, IMethodInfo method1, IMethodInfo method2,
			ITypeInfo containingType) {
		this.reflectionUI = reflectionUI;
		if (method2.getParameters().size() != 1) {
			throw new ReflectionUIError("Failed to chain methods '" + method1.getSignature() + "' AND '"
					+ method2.getSignature() + "': The 2nd method must have 1 and only 1 parameter");
		}
		this.method1 = method1;
		this.method2 = method2;
		this.containingType = containingType;
	}

	@Override
	public String getName() {
		return method1.getName() + "+" + method2.getName();
	}

	@Override
	public boolean isHidden() {
		return false;
	}

	@Override
	public String getCaption() {
		return ReflectionUIUtils.composeMessage(method1.getCaption(), method2.getCaption());
	}

	@Override
	public String getParametersValidationCustomCaption() {
		return method1.getParametersValidationCustomCaption();
	}

	@Override
	public String getOnlineHelp() {
		return null;
	}

	@Override
	public Map getSpecificProperties() {
		return Collections.emptyMap();
	}

	@Override
	public String getSignature() {
		return ReflectionUIUtils.buildMethodSignature(this);
	}

	@Override
	public ITypeInfo getReturnValueType() {
		if (returnValueVoid) {
			return null;
		}
		if (returnValueType == null) {
			if (method2.getReturnValueType() == null) {
				returnValueVoid = true;
			} else {
				returnValueType = reflectionUI
						.getTypeInfo(new TypeInfoSourceProxy(method2.getReturnValueType().getSource()) {
							@Override
							public SpecificitiesIdentifier getSpecificitiesIdentifier() {
								return null;
							}
						});
			}
		}
		return returnValueType;
	}

	@Override
	public List getParameters() {
		return method1.getParameters();
	}

	@Override
	public Object invoke(Object object, InvocationData invocationData) {

		Runnable method1UndoJob = method1.getNextInvocationUndoJob(object, invocationData);
		Object result = method1.invoke(object, invocationData);

		invocationData = new InvocationData(object, method2, result);

		Runnable method2UndoJob = method2.getNextInvocationUndoJob(object, invocationData);
		result = method2.invoke(object, invocationData);

		if (undoJobBuilder != null) {
			undoJobBuilder.setOption("method1UndoJob", method1UndoJob);
			undoJobBuilder.setOption("method2UndoJob", method2UndoJob);
			undoJobBuilder.build();
			undoJobBuilder = null;
		}

		return result;
	}

	@Override
	public Runnable getNextInvocationUndoJob(Object object, InvocationData invocationData) {
		undoJobBuilder = new FututreActionBuilder();
		return undoJobBuilder.will(new FututreActionBuilder.FuturePerformance() {
			@Override
			public void perform(Map options) {
				Runnable method1UndoJob = (Runnable) options.get("method1UndoJob");
				Runnable method2UndoJob = (Runnable) options.get("method2UndoJob");
				if (method1UndoJob == null) {
					throw new IrreversibleModificationException();
				}
				if (method2UndoJob == null) {
					throw new IrreversibleModificationException();
				}
				method2UndoJob.run();
				method1UndoJob.run();
			}
		});
	}

	@Override
	public boolean isReadOnly() {
		return method1.isReadOnly() && method2.isReadOnly();
	}

	@Override
	public String getNullReturnValueLabel() {
		return method2.getNullReturnValueLabel();
	}

	@Override
	public InfoCategory getCategory() {
		return null;
	}

	@Override
	public void validateParameters(Object object, InvocationData invocationData) throws Exception {
		method1.validateParameters(object, invocationData);
	}

	@Override
	public ValueReturnMode getValueReturnMode() {
		return ValueReturnMode.combine(method1.getValueReturnMode(), method2.getValueReturnMode());
	}

	@Override
	public ResourcePath getIconImagePath() {
		return method1.getIconImagePath();
	}

	@Override
	public boolean isReturnValueDetached() {
		return method1.isReturnValueDetached() || method2.isReturnValueDetached();
	}

	@Override
	public boolean isNullReturnValueDistinct() {
		return method2.isNullReturnValueDistinct();
	}

	@Override
	public boolean isReturnValueIgnored() {
		return method2.isReturnValueIgnored();
	}

	@Override
	public String getConfirmationMessage(Object object, InvocationData invocationData) {
		return method1.getConfirmationMessage(object, invocationData);
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((method1 == null) ? 0 : method1.hashCode());
		result = prime * result + ((method2 == null) ? 0 : method2.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		ChainedMethodsInfo other = (ChainedMethodsInfo) obj;
		if (method1 == null) {
			if (other.method1 != null)
				return false;
		} else if (!method1.equals(other.method1))
			return false;
		if (method2 == null) {
			if (other.method2 != null)
				return false;
		} else if (!method2.equals(other.method2))
			return false;
		return true;
	}

	@Override
	public String toString() {
		return "ChainedMethodsInfo [method1=" + method1 + ", method2=" + method2 + "]";
	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy