package ie.dcu.apps.ist.actions; import java.net.*; import java.util.*; import java.util.logging.*; import org.eclipse.jface.action.*; import org.eclipse.jface.resource.*; /** * Hover Action configured from a properties file. * * @author Kevin McGuinness */ public class ConfiguredAction extends HoverAction { private static final Logger log = Logger.getLogger("ConfiguredAction"); protected final Properties properties; private final String id; public ConfiguredAction(Properties properties) { this(properties, null, AS_PUSH_BUTTON); } public ConfiguredAction(Properties properties, int style) { this(properties, null, style); } public ConfiguredAction(Properties properties, String id, int style) { super(null, style); assert (properties != null); this.properties = properties; if (id != null) { this.id = id; } else { this.id = getClass().getSimpleName(); } configure(); } private void configure() { // Set id setId(id); // Set text setText(string("text")); // Set description String description; if ((description = string("description")) != null) { setDescription(description); } // Set tooltip String tooltip; if ((tooltip = string("tooltip")) != null) { setToolTipText(tooltip); } // Set accelerator Integer keycode; if ((keycode = accelerator("accelerator")) != null) { setAccelerator(keycode); } // Set images ImageDescriptor im; if ((im = image("image")) != null) { setImageDescriptor(im); } if ((im = image("hoverImage")) != null) { setHoverImageDescriptor(im); } if ((im = image("disabledImage")) != null) { setDisabledImageDescriptor(im); } // Set status Boolean status; if ((status = bool("checked") != null)) { setChecked(status); } if ((status = bool("enabled") != null)) { setEnabled(status); } } protected final Integer accelerator(String key) { String value = string(key); if (value == null) { return null; } int code = Action.convertAccelerator(value); if (code == 0) { log.warning("No accelerator: " + value); return null; } if (code == -1) { log.warning("Not a valid accelerator keycode: " + value); return null; } return code; } protected final Boolean bool(String key) { String value = string(key); if (value == null) { return null; } if (value.trim().equals("true")) { return true; } else if (value.trim().equals("false")) { return false; } else { log.warning("Not a boolean value: " + value); return null; } } protected final ImageDescriptor image(String key) { String url = string(key); if (url == null) { return null; } try { return ImageDescriptor.createFromURL(new URL(url)); } catch (MalformedURLException e) { log.warning("Invalid URL for image " + url); return null; } } protected final String string(String key) { return properties.getProperty(key(key)); } protected final String key(String name) { return String.format("action.%s.%s", id, name); } }