How to write GUI plugins
Create a plugin component
The example below creates a JMenuItem component, which is meant to be added in the popup menu opened when user clicks with the right mouse button over a contact in the contact list. When selected the menu item will open a dialog, which will simply show the name of the “clicked” contact.
In our case the plugin needs to know, which contact was clicked, when the right button menu was opened. For this it should implement the ContactAwareComponent interface. If the plugin is not interested in knowing the contact or the group it doesn’t need this interface.
To summarize until here we simply create a JComponent with some action associated and we hope to add it in the GUI.
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JMenuItem;
import net.java.sip.communicator.service.contactlist.MetaContact;
import net.java.sip.communicator.service.contactlist.MetaContactGroup;
import net.java.sip.communicator.service.gui.*;
public class ExamplePluginMenuItem
extends JMenuItem
implements
PluginComponent,
ActionListener
{
private MetaContact metaContact;
public ExamplePluginMenuItem()
{
super("Example plugin");
this.addActionListener(this);
}
public void setCurrentContact(MetaContact metaContact)
{
this.metaContact = metaContact;
}
public void setCurrentContactGroup(MetaContactGroup metaGroup)
{}
public void actionPerformed(ActionEvent e)
{
PluginDialog pluginDialog = new PluginDialog(metaContact);
pluginDialog.setLocation(
Toolkit.getDefaultToolkit().getScreenSize().width/2
- pluginDialog.getWidth()/2,
Toolkit.getDefaultToolkit().getScreenSize().height/2
- pluginDialog.getHeight()/2
);
pluginDialog.setVisible(true);
}
public String getConstraints()
{
return null;
}
public Container getContainer()
{
return Container.CONTAINER_CONTACT_RIGHT_BUTTON_MENU;
}
public int getPositionIndex()
{
return -1;
}
public boolean isNativeComponent()
{
return false;
}
}
Creating additional classes within the plugin
This example also uses an implementation of JDialog called PluginDialog. The PluginDialog class is not a necessary part of a plugin, but rather a feature of this particular plugin, demonstrating that you can add other class files to a plugin
import java.awt.*;
import javax.swing.*;
import net.java.sip.communicator.service.contactlist.*;
public class PluginDialog
extends JDialog
{
private JTextArea infoTextArea = new JTextArea();
private JPanel mainPanel = new JPanel();
private JLabel contactLabel = new JLabel();
private JLabel nameLabel = new JLabel();
public PluginDialog(MetaContact metaContact)
{
this.setTitle("Example plugin");
this.infoTextArea.setPreferredSize(new Dimension(250, 70));
this.infoTextArea.setText("This is an example plugin that shows the "
+ "currently selected contact"
+ " in a separate window.");
this.nameLabel.setText("The name of the selected contact is:");
this.contactLabel.setText(metaContact.getDisplayName());
this.mainPanel.add(infoTextArea);
this.mainPanel.add(nameLabel);
this.mainPanel.add(contactLabel);
this.getContentPane().add(mainPanel);
this.setStyles();
this.setResizable(false);
this.pack();
}
private void setStyles()
{
this.mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.Y_AXIS));
this.mainPanel.setBorder(
BorderFactory.createEmptyBorder(10, 10, 10, 10));
this.infoTextArea.setEditable(false);
this.infoTextArea.setOpaque(false);
this.infoTextArea.setWrapStyleWord(true);
this.infoTextArea.setLineWrap(true);
this.infoTextArea.setFont(infoTextArea.getFont().deriveFont(Font.BOLD));
this.infoTextArea.setAlignmentX(JTextArea.CENTER_ALIGNMENT);
this.nameLabel.setAlignmentX(JLabel.CENTER_ALIGNMENT);
this.contactLabel.setAlignmentX(JLabel.CENTER_ALIGNMENT);
this.contactLabel.setAlignmentY(JLabel.TOP_ALIGNMENT);
this.contactLabel.setFont(contactLabel.getFont().deriveFont(Font.BOLD));
}
}