package com.onaro.sanscreen.client.view.actions; import java.awt.event.ActionEvent; import java.util.Map; import java.util.ResourceBundle; import javax.swing.Action; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import com.onaro.sanscreen.client.view.ActionFactory; import com.onaro.sanscreen.client.view.selection.ObjectSelectionModel; import com.onaro.util.IllegalInitException; import com.onaro.util.jfc.ConfigurableAction; import com.onaro.util.jfc.RoleRestrictedAction; /** * An actoin that redirects to another action. Used when multiple instances of * a single actions are used in different cotexts. */ public class Redirect extends ConfigurableAction { private static final long serialVersionUID = 1L; /** * The target action that is invoked when this action is invoked. Should be * resolved before its first usage. */ private Action targetAction; /** * Name of the target action. Used to resolve the action to redirect to. */ private String targetActionName; /** * Static reference to the system logger. */ static Logger logger = LogManager.getLogger(Redirect.class); /** * Constucts a redirecting action. * * @param resources contains the action's properties * */ public Redirect(ActionFactory.ActionInitInfo actionInitInfo, ResourceBundle resources) throws IllegalInitException { super(actionInitInfo, resources); targetActionName = actionInitInfo.getProperty("targetAction"); //$NON-NLS-1$ if (targetActionName == null || targetActionName.length() == 0) { throw new IllegalInitException("Missing target action name in redirect action " + actionInitInfo); //$NON-NLS-1$ } } /** * Constucts a redirecting action. * * @param name the name of this action * @param targetActionName the name of the action to invoke when this action is invoked * @param resources contains the action's properties * */ public Redirect(String name, String targetActionName, ResourceBundle resources) throws IllegalInitException { super(name, resources); assert targetActionName != null : "Target action name cannot be null"; //$NON-NLS-1$ this.targetActionName = targetActionName; } /** * When the action is performed, switch the views. * * @param event */ public void actionPerformed(ActionEvent event) { assert targetAction != null : "Target action for name '" + targetActionName + "' wasn't resolved"; //$NON-NLS-1$ //$NON-NLS-2$ targetAction.actionPerformed(event); } /** * the redirect action forwards the enable by selection, and sets its enable state according * to the result * @param selection */ @Override public void setEnableBySelection(ObjectSelectionModel selection) { assert targetAction != null : "Target action for name '" + targetActionName + "' wasn't resolved"; //$NON-NLS-1$ //$NON-NLS-2$ if( targetAction instanceof ConfigurableAction ) { if (targetAction instanceof RoleRestrictedAction && ((RoleRestrictedAction)targetAction).isRestricted()) { setEnabled(false); } else { ConfigurableAction configurableAction = (ConfigurableAction)targetAction; configurableAction.setEnableBySelection(selection); setEnabled( configurableAction.isEnabled() ); } } } /** * Resolve the target action by searching it in the given map. * * @param actions map of action names to actions * */ public void resolve(Map actions) throws IllegalInitException { targetAction = actions.get(targetActionName); if (targetAction == null) { throw new IllegalInitException("Action '" + targetActionName + "' wasn't found at " + actions); //$NON-NLS-1$ //$NON-NLS-2$ } } }