Writing Java Plugins

This website contains links to software which is either no longer maintained or will be supported only until the end of 2019 (CKFinder 2). For the latest documentation about current CKSource projects, including software like CKEditor 4/CKEditor 5, CKFinder 3, Cloud Services, Letters, Accessibility Checker, please visit the new documentation website.

If you look for an information about very old versions of CKEditor, FCKeditor and CKFinder check also the CKEditor forum, which was closed in 2015. If not, please head to StackOverflow for support.

(Creating a Plugin: Two steps added)
(Plugin Definition & Event Types sections added)
Line 55: Line 55:
 
<param name="smallThumb" value="90x90" />
 
<param name="smallThumb" value="90x90" />
 
</source>
 
</source>
 +
 +
== Adding the Plugin Definition ==
 +
When the CKFinder application starts, it reads plugins declared in the XML configuration file and binds their commands to particular event types. In other words, event handlers are registered for plugins.
 +
 +
To make the registration process work, a plugin class has to be created first. This class has to implement the <code>registerEventHandlers</code> method inside which the plugin command is joined with an appropriate event type. This binding is possible thanks to the application of the <code>addEventHandler</code> method:
 +
<source lang="java">
 +
eventHandler.addEventHandler(EventTypes.BeforeExecuteCommand, FileSizeCommand.class);
 +
</source>
 +
 +
This method has two parameters: the event type, which determines when the command should be invoked, and <code>''Command''.class</code> (<code>FileSizeCommand.class</code> in this case), which serves as an event handler.
 +
 +
Below is the source code of the <code>FileSizePlugin</code>:
 +
<source lang="java">
 +
package com.ckfinder.connector.plugins;
 +
 +
import com.ckfinder.connector.configuration.Events;
 +
import com.ckfinder.connector.configuration.Events.EventTypes;
 +
import com.ckfinder.connector.configuration.Plugin;
 +
 +
public class FileSizePlugin extends Plugin {
 +
@Override
 +
public void registerEventHandlers(Events eventHandler) {
 +
eventHandler.addEventHandler(EventTypes.BeforeExecuteCommand, FileSizeCommand.class);
 +
}
 +
}
 +
</source>
 +
 +
=== Event Types ===
 +
There are three types of events. You might think of them as flags indicating when the event handlers should be invoked. The table below presents CKFinder event types.
 +
<table summary="CKFinder event types" class="borders">
 +
<caption>CKFinder Event Types</caption>
 +
<tr>
 +
<th>Event Type</th>
 +
<th>Description</th>
 +
</tr>
 +
<tr>
 +
<td><code>EventTypes.AfterFileUpload</code></td>
 +
<td>Event handlers registered for this type of event will be executed after a successful file upload.</td>
 +
</tr>
 +
<tr>
 +
<td><code>EventsTypes.BeforeExecuteCommand</code></td>
 +
<td>Event handlers registered for this type of event will be executed before a standard server-side command is executed.</td>
 +
</tr>
 +
<tr>
 +
<td><code>EventTypes.InitCommand</code></td>
 +
<td>Event handlers registered for this type of event will be executed on CKFinder client-side startup, right before sending the result of the <code>Init</code> command.</td>
 +
</tr>
 +
</table>
 +
 +
 +
 +
 +
 +
  
 
== Java Plugin System ==
 
== Java Plugin System ==

Revision as of 21:58, 16 May 2011

CKFinder functionality can be extended with server-side plugins. Although the full source code of the CKFinder server connector is available and can be modified in any way desired, a much better way of enhancing the CKFinder connector is to create a plugin.

The main advantages of plugins are:

  • Upgrades are much easier.
  • The plugin code is stored in a single place.
  • Plugins can be easily disabled when they are not needed anymore.

Common use cases:

  • Adding a new server-side command (i.e. fileditor and imageresize plugin).
  • Working with uploaded files (i.e. watermark plugin).
  • Extending information returned by the Init command (i.e. imageresize plugin).

Creating CKFinder for Java Plugins

As mentioned in the Using CKFinder Plugins article, the functionality of CKFinder can be extended with code that is delegated to dedicated plugins. This architecture ensures that you do not need to modify CKFinder core to change the behavior of the application — using plugins is much more convenient.

This tutorial shows how to create a CKFinder plugin on the example of a FileSizePlugin which, as the name suggests, returns the size of the file.

In order to create a CKFinder plugin, you will need to follow the steps below:

  • Step 1: Create the plugin folder and file.
  • Step 2: Add the plugin definition.
  • Step 3: Make CKFinder aware of the new plugin.

Please note that for plugins that just add server-side functionality and do not contain any client-side scripts you can skip the first step.

Creating Plugin Folder and File

For a start, you need to create a directory for your plugin inside the /CKFinderJava-X.Y/ckfinder/plugins directory. Please note that by default CKFinder comes with three client-side plugins: dummy, fileeditor, and imagresize.

important note
Remember that for CKFinder the name of the plugin folder is important and has to be the same as the name of the plugin, otherwise the application will not be able to recognize it.


Since our plugin is named filesizeplugin, the same name will be used for its folder. Inside the newly created filesizeplugin folder we are going to place the plugin.js file that will contain the plugin logic. Open the plugin.js file in an editor and add the following source code:

CKFinder.addPlugin( 'filesizeplugin', function( api ) {		// The name of the plugin.
	api.addFileContextMenuOption( { label : 'File Size', command : "FileSize" } , function( api, file )
	{
		api.connector.sendCommand( 'FileSize', { fileName : api.getSelectedFile().name }, function( xml )
		{
			if ( xml.checkError() )
				return; 
			var size = xml.selectSingleNode( 'Connector/FileSize/@size' );
			api.openMsgDialog( "", "The exact size of a file is: " + size.value + " bytes");
		} );
	});
});

Since the purpose of this tutorial is to show how to integrate plugins with CKFinder, we are not going to go into details of the JavaScript code. To sum it up, the plugin code above adds a new menu item to the user interface. This item, when selected, sends the FileSize command to the server, reads the XML response and displays the size of the queried file in a dialog window.

For more information on how to create JavaScript plugins refer to the Writing JavaScript Plugins article.

Making CKFinder Aware of the Plugin

Fast forward one step, this section describes how to make CKFinder aware that the plugin exists. Open the config.xml file and add a new plugin node to the plugins section.

<plugin>
	<name>filesizeplugin</name>
	<class>com.ckfinder.connector.plugins.FileSizePlugin</class>
	<params></params>
</plugin>

A plugin entry contains the following elements:

  • <name> – the name of the plugin that should be the same as the plugin folder name on the client side.
  • <class> – the plugin class extended from com.ckfinder.connector.configuration.Plugin.
  • <params> – additional plugin parameters. A parameter is added with a param node:
    <param name="smallThumb" value="90x90" />

Adding the Plugin Definition

When the CKFinder application starts, it reads plugins declared in the XML configuration file and binds their commands to particular event types. In other words, event handlers are registered for plugins.

To make the registration process work, a plugin class has to be created first. This class has to implement the registerEventHandlers method inside which the plugin command is joined with an appropriate event type. This binding is possible thanks to the application of the addEventHandler method:

eventHandler.addEventHandler(EventTypes.BeforeExecuteCommand, FileSizeCommand.class);

This method has two parameters: the event type, which determines when the command should be invoked, and Command.class (FileSizeCommand.class in this case), which serves as an event handler.

Below is the source code of the FileSizePlugin:

package com.ckfinder.connector.plugins;

import com.ckfinder.connector.configuration.Events;
import com.ckfinder.connector.configuration.Events.EventTypes;
import com.ckfinder.connector.configuration.Plugin;

public class FileSizePlugin extends Plugin {
	@Override
	public void registerEventHandlers(Events eventHandler) {
		eventHandler.addEventHandler(EventTypes.BeforeExecuteCommand, FileSizeCommand.class);
	}
}

Event Types

There are three types of events. You might think of them as flags indicating when the event handlers should be invoked. The table below presents CKFinder event types.

CKFinder Event Types
Event Type Description
EventTypes.AfterFileUpload Event handlers registered for this type of event will be executed after a successful file upload.
EventsTypes.BeforeExecuteCommand Event handlers registered for this type of event will be executed before a standard server-side command is executed.
EventTypes.InitCommand Event handlers registered for this type of event will be executed on CKFinder client-side startup, right before sending the result of the Init command.




Java Plugin System

Events

CKFinder provides several events that can be used to extend the functionality of a CKFinder application. Assigning a class (also known as an event handler) to an event will cause that function to be called at an appropriate point in the main CKFinder code to perform whatever additional task(s) the developer considers useful at that point. Each event can have multiple event listeners assigned to it.

Available Events

Hook Since Description
AfterFileUpload 2.0 Executed after successful file upload.
BeforeExecuteCommand 2.0 Executed before a server side command is executed.
InitCommand 2.0 Executed straight before sending the result of the Init command.