001/* 002 * Copyright 2010-2015 Institut Pasteur. 003 * 004 * This file is part of Icy. 005 * 006 * Icy is free software: you can redistribute it and/or modify 007 * it under the terms of the GNU General Public License as published by 008 * the Free Software Foundation, either version 3 of the License, or 009 * (at your option) any later version. 010 * 011 * Icy is distributed in the hope that it will be useful, 012 * but WITHOUT ANY WARRANTY; without even the implied warranty of 013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 014 * GNU General Public License for more details. 015 * 016 * You should have received a copy of the GNU General Public License 017 * along with Icy. If not, see <http://www.gnu.org/licenses/>. 018 */ 019package icy.gui.menu; 020 021import icy.plugin.PluginDescriptor; 022import icy.plugin.PluginLauncher; 023import icy.resource.ResourceUtil; 024import icy.system.thread.ThreadUtil; 025 026import java.awt.event.ActionEvent; 027import java.awt.event.ActionListener; 028 029import javax.swing.ImageIcon; 030import javax.swing.JMenuItem; 031 032/** 033 * This class represent a MenuItem which launch a plugin when pressed. 034 */ 035public class PluginMenuItem extends JMenuItem implements ActionListener 036{ 037 private static final long serialVersionUID = 2508924050709990008L; 038 039 private static final int ICON_SIZE = 24; 040 041 final PluginDescriptor pluginDescriptor; 042 043 public PluginMenuItem(PluginDescriptor pluginDescriptor) 044 { 045 super(pluginDescriptor.getSimpleClassName()); 046 047 this.pluginDescriptor = pluginDescriptor; 048 049 // do it in background as loading icon can take sometime 050 ThreadUtil.bgRun(new Runnable() 051 { 052 @Override 053 public void run() 054 { 055 final ImageIcon icon = PluginMenuItem.this.pluginDescriptor.getIcon(); 056 if (icon != null) 057 setIcon(ResourceUtil.scaleIcon(icon, ICON_SIZE)); 058 } 059 }); 060 061 addActionListener(this); 062 } 063 064 @Override 065 public void actionPerformed(ActionEvent e) 066 { 067 PluginLauncher.start(pluginDescriptor); 068 } 069}