package com.onaro.util.jfc; import java.awt.event.ActionEvent; import java.beans.PropertyChangeListener; import java.util.ResourceBundle; import javax.swing.AbstractAction; import org.apache.commons.lang3.StringUtils; import com.onaro.client.leekui.jobs.SingleRunnableContext; import com.onaro.commons.util.IResourceCommonProperties; import com.onaro.sanscreen.client.view.ActionFactory; import com.onaro.sanscreen.client.view.selection.ObjectSelectionModel; import com.onaro.util.IllegalInitException; import com.onaro.util.InvalidConfigException; import com.onaro.util.jfc.SelectableCheckBoxMenuItem.JCheckBoxMenuItemPropertyChangeListener; /** * An Abstract action that reads its properties from a resource bundle. */ public abstract class ConfigurableAction extends AbstractAction { private static final long serialVersionUID = 1L; /** * The action's id identifying this action. */ private final String id; /** * The locale dependent resources used for prompting the user. */ protected ResourceBundle resources; private final SingleRunnableContext singleRunnableContext = new SingleRunnableContext(); public ConfigurableAction(String id) throws IllegalInitException { this(id, null); } /** * Constructs an action with the given id. * */ public ConfigurableAction(String id, ResourceBundle resources) throws IllegalInitException { if (StringUtils.isBlank(id)) throw new IllegalArgumentException("id"); //$NON-NLS-1$ this.resources = resources; this.id = id; readFromResources(); } /** * Constructs an action with the name specified by the configuration (in the "name" attribute). * * @param resources * resource bundle */ public ConfigurableAction(ActionFactory.ActionInitInfo actionInitInfo, ResourceBundle resources) throws IllegalInitException { this(actionInitInfo.getActionName(), resources); // this.id = actionInitInfo.getActionName(); // this.resources = resources; // // if (resources != null) { // readFromResources(); // } } /** * For unit testing only */ protected ConfigurableAction() { //do nothing id = null; } /** * Read's action properties from the given resource bundle. * * @throws InvalidConfigException if the resources cannot be retrieved for the action. */ private void readFromResources() throws IllegalInitException { IResourceCommonProperties props = getResourceCommonProperties(); if (props == null) { throw new IllegalInitException("Configurable Action missing resources for display"); //$NON-NLS-1$ } putValue(AbstractAction.NAME, props.getName()); putValue(AbstractAction.SHORT_DESCRIPTION, props.getShortDescription()); putValue(AbstractAction.SMALL_ICON, props.getIcon()); if (props.getMnemonicKey() != null) { putValue(AbstractAction.MNEMONIC_KEY, props.getMnemonicKey()); } if (props.getAcceleratorKey() != null) { putValue(AbstractAction.ACCELERATOR_KEY, props.getAcceleratorKey()); } } /** * Retrieve the resources to use for this configurable action. The IResourceCommonProperties specifies * the display name, tool tip, icon, mnemonic and accelerator to use for the action. The default * implementation reads its resources from the ResourceBundle returned from getResources(). Subclasses * may override this method to provide alternate means to retrieve the resources. * * @return the resource common properties for this action. */ protected IResourceCommonProperties getResourceCommonProperties() { ResourceBundle localResources = getResources(); if (localResources == null) { return null; } IResourceCommonProperties props = ResourceCommonPropertiesFactory.create(localResources, id); return props; } public final String getId() { return id; } /** * Gets the id of this action. * * @see ConfigurableAction#getId() * * @return this action's name */ @Deprecated public String getName() { return getId(); } /** * @return The locale dependant resources used for prompting the user. */ public ResourceBundle getResources() { return resources; } /** * the default implemention is setEnabled(true); * * @param selection * the current selection */ public void setEnableBySelection(ObjectSelectionModel selection) { setEnabled(true); } @Override public boolean isEnabled() { // Need to check role here for cases when setEnableBySelection() is not called. return super.isEnabled() && ! (this instanceof RoleRestrictedAction && ((RoleRestrictedAction)this).isRestricted()); } /** * Perform the action given the specified selection model. The default implementation * simply delegates to the normal actionPerformed(ActionEvent e) method. Subclasses * can override to provide more specific behavior reacting to the selection. * * @param e the action event to process * @param selection the selection to use for the action */ public void actionPerformed(ActionEvent e, ObjectSelectionModel selection) { actionPerformed(e); } public void setSelected(boolean selected) { PropertyChangeListener[] propertyChangeListeners = getPropertyChangeListeners(); for (PropertyChangeListener listener : propertyChangeListeners) { if (listener instanceof JCheckBoxMenuItemPropertyChangeListener) { ((JCheckBoxMenuItemPropertyChangeListener)listener).setSelected(selected); } } } public final SingleRunnableContext getSingleRunnableContext() { return singleRunnableContext; } }