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.gui.util.ComponentUtil; 023import icy.resource.icon.IcyIcon; 024import icy.util.StringUtil; 025 026import java.awt.Dimension; 027import java.awt.Image; 028 029import javax.swing.Action; 030import javax.swing.Icon; 031import javax.swing.JButton; 032import javax.swing.SwingConstants; 033 034/** 035 * @author Stephane 036 */ 037public class IcyButton extends JButton 038{ 039 private static final long serialVersionUID = -2259114067015863508L; 040 041 private boolean flat; 042 043 /** 044 * Create a button with specified text and icon 045 */ 046 public IcyButton(String text, IcyIcon icon) 047 { 048 super(text, icon); 049 050 flat = false; 051 init(); 052 } 053 054 /** 055 * Create a button with specified icon. 056 */ 057 public IcyButton(IcyIcon icon) 058 { 059 this(null, icon); 060 } 061 062 /** 063 * Create a button with specified text. 064 */ 065 public IcyButton(String text) 066 { 067 this(text, (IcyIcon) null); 068 } 069 070 /** 071 * Create a button with specified action. 072 */ 073 public IcyButton(IcyAbstractAction action) 074 { 075 super(action); 076 077 flat = false; 078 init(); 079 } 080 081 /** 082 * @deprecated User {@link #IcyButton(IcyAbstractAction)} instead. 083 */ 084 @Deprecated 085 public IcyButton(icy.common.IcyAbstractAction action) 086 { 087 super(action); 088 089 flat = false; 090 init(); 091 } 092 093 /** 094 * @deprecated Use {@link #IcyButton(String, IcyIcon)} instead. 095 */ 096 @Deprecated 097 public IcyButton(String text, Image iconImage, int iconSize) 098 { 099 this(text, new IcyIcon(iconImage, iconSize)); 100 } 101 102 /** 103 * @deprecated Use {@link #IcyButton(String, IcyIcon)} instead. 104 */ 105 @Deprecated 106 public IcyButton(String text, Image iconImage) 107 { 108 this(text, iconImage, IcyIcon.DEFAULT_SIZE); 109 } 110 111 /** 112 * @deprecated Use {@link #IcyButton(IcyIcon)} instead. 113 */ 114 @Deprecated 115 public IcyButton(Image iconImage, int iconSize) 116 { 117 this(null, iconImage, iconSize); 118 } 119 120 /** 121 * @deprecated Use {@link #IcyButton(IcyIcon)} instead. 122 */ 123 @Deprecated 124 public IcyButton(Image iconImage) 125 { 126 this(null, iconImage); 127 } 128 129 /** 130 * @deprecated Use {@link #IcyButton(String, IcyIcon)} instead. 131 */ 132 @Deprecated 133 public IcyButton(String text, String iconName, int iconSize) 134 { 135 this(text, new IcyIcon(iconName, iconSize)); 136 } 137 138 /** 139 * @deprecated Use {@link #IcyButton(String, IcyIcon)} instead. 140 */ 141 @Deprecated 142 public IcyButton(String text, String iconName) 143 { 144 this(text, iconName, IcyIcon.DEFAULT_SIZE); 145 } 146 147 private void init() 148 { 149 setHorizontalAlignment(SwingConstants.CENTER); 150 setVerticalAlignment(SwingConstants.CENTER); 151 152 if (flat) 153 { 154 setBorderPainted(false); 155 setFocusPainted(false); 156 setFocusable(false); 157 } 158 159 // manual change notify 160 updateSize(); 161 } 162 163 @Override 164 public void setAction(Action a) 165 { 166 super.setAction(a); 167 168 // override tooltip set from action 169 IcyAbstractAction.setToolTipTextFromAction(this, a); 170 } 171 172 /** 173 * @return the flat 174 */ 175 public boolean isFlat() 176 { 177 return flat; 178 } 179 180 /** 181 * @param flat 182 * the flat to set 183 */ 184 public void setFlat(boolean flat) 185 { 186 if (this.flat != flat) 187 { 188 this.flat = flat; 189 190 setBorderPainted(!flat); 191 setFocusPainted(!flat); 192 setFocusable(!flat); 193 194 updateSize(); 195 } 196 } 197 198 /** 199 * Return the icon as IcyIcon 200 */ 201 public IcyIcon getIcyIcon() 202 { 203 final Icon icon = getIcon(); 204 205 if (icon instanceof IcyIcon) 206 return (IcyIcon) icon; 207 208 return null; 209 } 210 211 /** 212 * @return the icon name 213 */ 214 public String getIconName() 215 { 216 final IcyIcon icon = getIcyIcon(); 217 218 if (icon != null) 219 return icon.getName(); 220 221 return null; 222 } 223 224 /** 225 * @param iconName 226 * the iconName to set 227 */ 228 public void setIconName(String iconName) 229 { 230 final IcyIcon icon = getIcyIcon(); 231 232 if (icon != null) 233 { 234 icon.setName(iconName); 235 updateSize(); 236 } 237 } 238 239 /** 240 * @return the icon size 241 */ 242 public int getIconSize() 243 { 244 final IcyIcon icon = getIcyIcon(); 245 246 if (icon != null) 247 return icon.getSize(); 248 249 return -1; 250 } 251 252 /** 253 * @param iconSize 254 * the iconSize to set 255 */ 256 public void setIconSize(int iconSize) 257 { 258 final IcyIcon icon = getIcyIcon(); 259 260 if (icon != null) 261 { 262 icon.setSize(iconSize); 263 updateSize(); 264 } 265 } 266 267 @Override 268 public void setText(String text) 269 { 270 super.setText(text); 271 272 updateSize(); 273 } 274 275 public void updateSize() 276 { 277 final IcyIcon icon = getIcyIcon(); 278 boolean noText = StringUtil.isEmpty(getText()); 279 noText |= (getAction() != null) && getHideActionText(); 280 281 // adjust size to icon size if no text 282 if (flat && (icon != null) && noText) 283 { 284 final Dimension dim = icon.getDimension(); 285 dim.height += 2; 286 dim.width += 2; 287 ComponentUtil.setFixedSize(this, dim); 288 } 289 } 290 291 @Override 292 protected void actionPropertyChanged(Action action, String propertyName) 293 { 294 // override tooltip set from action 295 if ((propertyName == Action.LONG_DESCRIPTION) || (propertyName == Action.SHORT_DESCRIPTION)) 296 IcyAbstractAction.setToolTipTextFromAction(this, action); 297 else 298 super.actionPropertyChanged(action, propertyName); 299 } 300}