Enable and disable operations
From AIBenchWiki
In order to make the AIBench applications more user-friendly, we have added the possibility to enable and disable operations via these 2 changes:
- Added two methods to the Core: enableOperation(String uid) and disableOperation(String uid), that can be used to change the operation state at run-time.
- Added the boolean "enabled" attribute to the @Operation annotation. This attribute controls if the operation is enabled/disabled at startup.
Example
A typical usage of this new functionality is to enable an operation only when some object of a specific datatype is available in the Clipboard. You have to follow these steps:
1. Disable the operation by default with:
@Operation(name="operation", enabled=false)
2. Add a PluginLifecycle class configuring it in the plugin.xml
<plugin start="true"> <uid>myplugin</uid> <name>MyPlugin</name> <version>0.1</version> <lifecycleclass>myplugin.Lifecycle</lifecycleclass> ...
3. In the lifecycle class, add your custom ClipboardListener where you can enable and disable operations.
public class Lifecycle extends PluginLifecycle {
...
public void start(){
Core.getInstance().getClipboard().addClipboardListener(new ClipboardListener(){ //the clipboard listener
public void elementAdded(ClipboardItem arg0) {
//Enable the operation if an object of class String is in the clipboard
if (Core.getInstance().getClipboard().getItemsByClass(String.class).size()>0){
Core.getInstance().enableOperation("operationuid");
}
}
public void elementRemoved(ClipboardItem arg0) {
//Disable the operation if no object of class String is in the clipboard
if (Core.getInstance().getClipboard().getItemsByClass(String.class).size()==0){
Core.getInstance().disableOperation("operationuid");
}
}
});
}
}
Clipboard-based enabling/disabling operations
Here is a utility class to enable/disabling operations, based on the existence of objects of a given class in the clipboard. This class has to be connected as a ClipboardListener (the programmer is responsible of doing this!)
class ClipboardBasedOperationActivator implements ClipboardListener{
HashMap<String, HashSet<Class>> operationRequirements = new HashMap<String, HashSet<Class>>();
public void addRequirement(String uid, Class c){
HashSet<Class> reqs = operationRequirements.get(uid);
if (reqs == null){
reqs = new HashSet<Class>();
operationRequirements.put(uid, reqs);
}
reqs.add(c);
}
private void processClipboard(){
for (String uid: operationRequirements.keySet()){
boolean requirementsSatisfied = true;
for (Class c: operationRequirements.get(uid)){
if (Core.getInstance().getClipboard().getItemsByClass(c).size()==0){
requirementsSatisfied = false;
break;
}
}
if (requirementsSatisfied){
Core.getInstance().enableOperation(uid);
}else{
Core.getInstance().disableOperation(uid);
}
}
}
public void elementAdded(ClipboardItem arg0) {
processClipboard();
}
public void elementRemoved(ClipboardItem arg0) {
processClipboard();
}
}

