Writing a New Component for Editing Attribute Values

Preliminaries

This document explains how a new editing component for editing attribute values can be realized using the plugin mechanism of Gravisto. For the following example(s) it is necessary to have Graffiti_Core and Graffiti_Editor in your classpath.
For reading this document it is assumed that the reader is familiar with Writing an Attribute Plugin.

Getting Started

Like for every plugin it is assumed that you provide a valid plugin description file first. The next step is creating a new plugin adapter, e.g. EditComponentTestPlugin. For an edit component plugin which is member of the package de.chris.plugins.editcomponents.test it looks similar to the following:

package de.chris.plugins.editcomponents.test;

import org.graffiti.plugin.EditorPluginAdapter;

public class EditComponentTestPlugin
    extends EditorPluginAdapter
{
    public EditComponentTestPlugin()
    {
        this.attributes = new Class[1];
        this.attributes[0] = TestAttribute.class;

        this.valueEditComponents.put(TestAttribute.class,
            TestEditComponent.class);
    }
}
      

Because a single edit component plugin can contain more than one edit component, the inherited member map valueEditComponents can be filled with an arbitrary number of edit component classes. For each edit component, which must implement the interface ValueEditComponent, the class must be specified which defines the attribute which should be edited. The above example adds only TestEditComponent.class for editing the attribute TestAttribute.class which is described in Writing an Attribute Plugin. Here it is assumed that TestAttribute is in the same package as TestEditComponent. Every edit component plugin class must extend org.graffiti.plugin.EditorPluginAdapter.

Writing an Edit Component

The following class TestEditComponent is an implementation of an edit component for TestAttribute. Please note that TestEditComponent indirectly implements org.graffiti.plugin.editcomponent.ValueEditComponent because it extends for convenience reasons org.graffiti.plugin.editcomponent.AbstractValueEditComponent which implements itself some standard methods of the interface ValueEditComponent. TestEditComponent uses beside some Java graphic packages org.graffiti.plugin.Displayable.

package de.chris.plugins.editcomponents.test;

import java.awt.Dimension;

import javax.swing.JComponent;
import javax.swing.JTextField;
import javax.swing.text.JTextComponent;

import org.graffiti.plugin.Displayable;
import org.graffiti.plugin.editcomponent.AbstractValueEditComponent;

public class TestEditComponent
    extends AbstractValueEditComponent
{
    protected JTextComponent textComp;

    public TestEditComponent(Displayable disp)
    {
        super(disp);

        textComp = new JTextField(disp.getValue().toString());
    }

    public JComponent getComponent()
    {
        textComp.setMinimumSize(new Dimension(0, 30));
        textComp.setPreferredSize(new Dimension(50, 30));
        textComp.setMaximumSize(new Dimension(2000, 30));

        return this.textComp;
    }

    public void setEditFieldValue()
    {
        if (showEmpty)
        {
            this.textComp.setText(EMPTY_STRING);
        }
        else
        {
            this.textComp.setText(displayable.getValue().toString());
        }
    }

    public void setValue()
    {
        String text = this.textComp.getText();

        if(!text.equals(EMPTY_STRING) &&
            !this.displayable.getValue().toString().equals(text))
        {
            this.displayable.setValue(text);
        }
    }
}
      

This example is a new edit component which allows to edit a TestAttribute attribute, i.e., the wrapped string. It provides a constructor which gets a Displayable object as parameter which is the attribute object (the interface Attribute extends the Displayable interface). Here the content of the edit component, in our case the JTextComponent textComp is filled with the value of the attribute at creation time. After the dimensions of the JTextComponent textComp are set the method getComponent returns it as JComponent. The method setEditFieldValue sets the content of our text component according to the Boolean flag showEmpty which is handled by AbstractValueEditComponent. It indicates whether the component should be shown with content. The variable displayable is inherited via AbstractValueEditComponent from ValueEditComponentAdapter and is our attribute as set by the constructors. The last method setValue sets the value entered in the text field in the attribute, but only if it is different. This is the contrary direction of the information flow in setEditFieldValue.

Using Edit Components

In order to use the newly written edit component load this plugin via the plugin manager. Afterwards load the attribute of the edit component as described in Using Attributes. In the case of the above example the attributes class name to load is de.chris.plugins.editcomponents.test.TestAttribute.