Show Objects when they are added to Clipboard

From AIBenchWiki

Jump to: navigation, search

This code fragment illustrates how to "auto-open" specific datatypes just when they are added to the Clipboard, avoiding the user to make a click over them.

This is done by adding a ClipboardListener on your PluginLifecycle and, when some Object arrives, check if it is an instance of a desired class, and request the Workbench to show it.

Steps

1. Create your PluginLifecycle by configuring it in the plugin.xml:

<plugin start="true">
	<uid>myplugin</uid>
	<name>MyPlugin</name>
	   <version>0.1</version>
	<lifecycleclass>myplugin.Lifecycle</lifecycleclass>

2. Add a ClipboardListener at the start() method of the lifecycle class:

package myplugin;
import es.uvigo.ei.aibench.core.Core;
import es.uvigo.ei.aibench.core.clipboard.ClipboardItem;
import es.uvigo.ei.aibench.core.clipboard.ClipboardListener;
import es.uvigo.ei.aibench.workbench.Workbench;

public class Lifecycle extends PluginLifecycle {
  public void start(){
     Core.getInstance().getClipboard().addClipboardListener(new BringViewListener());
  }
  private class BringViewListener implements ClipboardListener{
    public void elementAdded(ClipboardItem item) {
      Class autoOpen = SimpleDocument.class; //which class we want to autoopen 
      if (autoOpen.isAssignableFrom(item.getUserData().getClass())){
        Workbench.getInstance().showData(item); //bring up the view 
      }						
    }
    public void elementRemoved(ClipboardItem item) {}
  }
}
Personal tools
Documentation