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.component.button; 020 021import icy.action.IcyAbstractAction; 022import icy.resource.icon.IcyIcon; 023import icy.util.StringUtil; 024 025import java.beans.PropertyChangeEvent; 026import java.beans.PropertyChangeListener; 027 028import javax.swing.Icon; 029 030import org.pushingpixels.flamingo.api.common.JCommandMenuButton; 031 032/** 033 * @author Stephane 034 */ 035public class IcyCommandMenuButton extends JCommandMenuButton 036{ 037 /** 038 * 039 */ 040 private static final long serialVersionUID = -8129025104172266942L; 041 042 /** 043 * internals 044 */ 045 private boolean internalEnabled; 046 private IcyAbstractAction action; 047 private final PropertyChangeListener actionPropertyChangeListener; 048 049 public IcyCommandMenuButton(String title, IcyIcon icon) 050 { 051 super(title, icon); 052 053 action = null; 054 internalEnabled = isEnabled(); 055 056 actionPropertyChangeListener = new PropertyChangeListener() 057 { 058 @Override 059 public void propertyChange(PropertyChangeEvent evt) 060 { 061 if (StringUtil.equals("enabled", evt.getPropertyName())) 062 refreshEnabled(); 063 } 064 }; 065 } 066 067 /** 068 * @deprecated Use {@link #IcyCommandMenuButton(String, IcyIcon)} instead. 069 */ 070 @Deprecated 071 public IcyCommandMenuButton(String title, String iconName) 072 { 073 this(title, new IcyIcon(iconName)); 074 } 075 076 public IcyCommandMenuButton(IcyIcon icon) 077 { 078 this(null, icon); 079 } 080 081 public IcyCommandMenuButton(String title) 082 { 083 this(title, (IcyIcon) null); 084 } 085 086 public IcyCommandMenuButton(IcyAbstractAction action) 087 { 088 this(null, (IcyIcon) null); 089 090 setAction(action); 091 } 092 093 /** 094 * @deprecated User {@link #IcyCommandMenuButton(IcyAbstractAction)} instead. 095 */ 096 @Deprecated 097 public IcyCommandMenuButton(icy.common.IcyAbstractAction action) 098 { 099 this(null, (IcyIcon) null); 100 101 setAction(action); 102 } 103 104 public IcyCommandMenuButton() 105 { 106 this(null, (IcyIcon) null); 107 } 108 109 /** 110 * Return the icon as IcyIcon 111 */ 112 public IcyIcon getIcyIcon() 113 { 114 final Icon icon = getIcon(); 115 116 if (icon instanceof IcyIcon) 117 return (IcyIcon) icon; 118 119 return null; 120 } 121 122 /** 123 * @return the icon name 124 */ 125 public String getIconName() 126 { 127 final IcyIcon icon = getIcyIcon(); 128 129 if (icon != null) 130 return icon.getName(); 131 132 return null; 133 } 134 135 /** 136 * @param iconName 137 * the icon name to set 138 */ 139 public void setIconName(String iconName) 140 { 141 final IcyIcon icon = getIcyIcon(); 142 143 if (icon != null) 144 icon.setName(iconName); 145 } 146 147 @Override 148 public void setEnabled(boolean b) 149 { 150 internalEnabled = b; 151 refreshEnabled(); 152 } 153 154 protected void refreshEnabled() 155 { 156 super.setEnabled(internalEnabled && ((action == null) || action.isEnabled())); 157 } 158 159 /** 160 * Sets the {@link IcyAbstractAction} attached to this button. 161 */ 162 public void setAction(IcyAbstractAction value) 163 { 164 if (action != value) 165 { 166 // remove listener from previous action 167 if (action != null) 168 { 169 removeActionListener(action); 170 action.removePropertyChangeListener(actionPropertyChangeListener); 171 } 172 173 action = value; 174 175 setText(action.getName()); 176 177 final IcyIcon icon = action.getIcon(); 178 179 if (icon != null) 180 setIcon(new IcyIcon(icon)); 181 else 182 setIcon(null); 183 184 setCommandButtonKind(CommandButtonKind.ACTION_ONLY); 185 186 if (value != null) 187 { 188 // set tooltip 189 setActionRichTooltip(action.getRichToolTip()); 190 191 // add listeners 192 addActionListener(value); 193 value.addPropertyChangeListener(actionPropertyChangeListener); 194 } 195 196 refreshEnabled(); 197 } 198 } 199 200 /** 201 * Returns the {@link IcyAbstractAction} attached to this button. 202 */ 203 public IcyAbstractAction getAction() 204 { 205 return action; 206 } 207}