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.plugin; 020 021import icy.gui.component.button.IcyCommandButton; 022import icy.gui.component.button.IcyCommandToggleButton; 023import icy.plugin.PluginDescriptor; 024import icy.plugin.PluginLauncher; 025import icy.plugin.PluginLoader; 026import icy.resource.icon.IcyIcon; 027import icy.system.thread.ThreadUtil; 028 029import java.awt.event.ActionEvent; 030import java.awt.event.ActionListener; 031 032import org.pushingpixels.flamingo.api.common.AbstractCommandButton; 033 034/** 035 * Class helper to create plugin command button 036 * 037 * @author Stephane 038 */ 039public class PluginCommandButton 040{ 041 /** 042 * Set a plugin button with specified action 043 */ 044 public static void setButton(final AbstractCommandButton button, final PluginDescriptor plugin, boolean doAction, boolean alpha) 045 { 046 final String name = plugin.getName(); 047 final String className = plugin.getClassName(); 048 049 // update text & icon 050 button.setText(name); 051 // set icon 052// button.setIcon(new BasicResizableIcon(plugin.getIcon())); 053 button.setIcon(new IcyIcon(plugin.getIconAsImage(), alpha)); 054 // save class name here 055 button.setName(className); 056 057 // do it asynchronously as image loading can take sometime 058 ThreadUtil.bgRun(new Runnable() 059 { 060 @Override 061 public void run() 062 { 063 button.setActionRichTooltip(new PluginRichToolTip(plugin)); 064 } 065 }); 066 067 // remove previous listener on button 068 final ActionListener[] listeners = button.getListeners(ActionListener.class); 069 for (ActionListener listener : listeners) 070 button.removeActionListener(listener); 071 072 if (doAction) 073 { 074 button.addActionListener(new ActionListener() 075 { 076 @Override 077 public void actionPerformed(ActionEvent e) 078 { 079 final AbstractCommandButton button = (AbstractCommandButton) e.getSource(); 080 final PluginDescriptor plugin = PluginLoader.getPlugin(button.getName()); 081 082 if (plugin != null) 083 PluginLauncher.start(plugin); 084 } 085 }); 086 } 087 } 088 089 /** 090 * Set a plugin button with default action 091 */ 092 public static void setButton(AbstractCommandButton button, PluginDescriptor plugin) 093 { 094 setButton(button, plugin, true, false); 095 } 096 097 /** 098 * Build a plugin button 099 */ 100 public static AbstractCommandButton createButton(PluginDescriptor plugin, boolean toggle, boolean doAction, boolean alpha) 101 { 102 final AbstractCommandButton result; 103 104 // build command button 105 if (toggle) 106 result = new IcyCommandToggleButton(); 107 else 108 result = new IcyCommandButton(); 109 110 setButton(result, plugin, doAction, alpha); 111 112 return result; 113 } 114 115 /** 116 * Build a plugin button with default action (execute plugin) 117 */ 118 public static IcyCommandButton createButton(PluginDescriptor plugin) 119 { 120 // build with default action listener 121 return (IcyCommandButton) createButton(plugin, false, true, false); 122 } 123 124 /** 125 * Build a plugin toggle button with default action (execute plugin) if enable. 126 */ 127 public static IcyCommandToggleButton createToggleButton(PluginDescriptor plugin, boolean doAction, boolean alpha) 128 { 129 return (IcyCommandToggleButton) createButton(plugin, true, doAction, alpha); 130 } 131}