|
|
|
@ -25,10 +25,13 @@ import com.fr.stable.StringUtils;
|
|
|
|
|
* @since 2012-5-11下午4:28:24 |
|
|
|
|
*/ |
|
|
|
|
public class UIToggleButton extends UIButton implements GlobalNameObserver{ |
|
|
|
|
|
|
|
|
|
private static final int ICON_COUNT = 2; |
|
|
|
|
private boolean isSelected; |
|
|
|
|
private boolean isEventBannded = false; |
|
|
|
|
private String toggleButtonName = ""; |
|
|
|
|
private GlobalNameListener globalNameListener = null; |
|
|
|
|
private Icon[] icons; |
|
|
|
|
|
|
|
|
|
public UIToggleButton() { |
|
|
|
|
this(StringUtils.EMPTY); |
|
|
|
@ -53,9 +56,9 @@ public class UIToggleButton extends UIButton implements GlobalNameObserver{
|
|
|
|
|
* @param icons |
|
|
|
|
*/ |
|
|
|
|
public UIToggleButton(Icon[] icons) { |
|
|
|
|
super(icons[0], null, icons[1]); |
|
|
|
|
setSelectedIcon(icons[1]); |
|
|
|
|
super(icons[0], null, null); |
|
|
|
|
setExtraPainted(true); |
|
|
|
|
this.icons = icons; |
|
|
|
|
addActionListener(new AbstractAction() { |
|
|
|
|
@Override |
|
|
|
|
public void actionPerformed(ActionEvent e) { |
|
|
|
@ -73,10 +76,10 @@ public class UIToggleButton extends UIButton implements GlobalNameObserver{
|
|
|
|
|
* @param icons |
|
|
|
|
*/ |
|
|
|
|
public UIToggleButton(Icon[] icons, boolean needRelease) { |
|
|
|
|
super(icons[0], null, icons[1]); |
|
|
|
|
super(icons[0], null, null); |
|
|
|
|
setBorderPainted(true); |
|
|
|
|
setSelectedIcon(icons[1]); |
|
|
|
|
setExtraPainted(true); |
|
|
|
|
this.icons = icons; |
|
|
|
|
if (!needRelease) { |
|
|
|
|
addActionListener(new AbstractAction() { |
|
|
|
|
@Override |
|
|
|
@ -92,6 +95,7 @@ public class UIToggleButton extends UIButton implements GlobalNameObserver{
|
|
|
|
|
addMouseListener(getMouseListener()); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
public void setGlobalName(String name){ |
|
|
|
|
toggleButtonName = name ; |
|
|
|
|
} |
|
|
|
@ -100,6 +104,7 @@ public class UIToggleButton extends UIButton implements GlobalNameObserver{
|
|
|
|
|
* |
|
|
|
|
* @return |
|
|
|
|
*/ |
|
|
|
|
@Override |
|
|
|
|
public boolean isSelected() { |
|
|
|
|
return isSelected; |
|
|
|
|
} |
|
|
|
@ -115,12 +120,15 @@ public class UIToggleButton extends UIButton implements GlobalNameObserver{
|
|
|
|
|
super.setSelected(isSelected); |
|
|
|
|
if (this.isSelected != isSelected) { |
|
|
|
|
this.isSelected = isSelected; |
|
|
|
|
repaint(); |
|
|
|
|
refresh(isSelected); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
protected void initListener(){ |
|
|
|
|
if(shouldResponseChangeListener()){ |
|
|
|
|
this.addChangeListener(new ChangeListener() { |
|
|
|
|
@Override |
|
|
|
|
public void stateChanged(ChangeEvent e) { |
|
|
|
|
if (uiObserverListener == null) { |
|
|
|
|
return; |
|
|
|
@ -138,10 +146,30 @@ public class UIToggleButton extends UIButton implements GlobalNameObserver{
|
|
|
|
|
if (this.isSelected != isSelected) { |
|
|
|
|
this.isSelected = isSelected; |
|
|
|
|
fireSelectedChanged(); |
|
|
|
|
repaint(); |
|
|
|
|
refresh(isSelected); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private void refresh(final boolean isSelected) { |
|
|
|
|
SwingUtilities.invokeLater(new Runnable() { |
|
|
|
|
@Override |
|
|
|
|
public void run() { |
|
|
|
|
Icon[] icons = UIToggleButton.this.icons; |
|
|
|
|
if (icons != null && icons.length == ICON_COUNT) { |
|
|
|
|
if (isSelected) { |
|
|
|
|
UIToggleButton.this.setIcon(icons[1]); |
|
|
|
|
} else { |
|
|
|
|
UIToggleButton.this.setIcon(icons[0]); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
UIToggleButton.this.repaint(); |
|
|
|
|
} |
|
|
|
|
}); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
protected MouseListener getMouseListener() { |
|
|
|
|
return new MouseAdapter() { |
|
|
|
|
@Override |
|
|
|
@ -157,6 +185,7 @@ public class UIToggleButton extends UIButton implements GlobalNameObserver{
|
|
|
|
|
this.isEventBannded = ban; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
protected void fireStateChanged() { |
|
|
|
|
|
|
|
|
|
} |
|
|
|
@ -187,6 +216,7 @@ public class UIToggleButton extends UIButton implements GlobalNameObserver{
|
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
protected void paintOtherBorder(Graphics g) { |
|
|
|
|
Graphics2D g2d = (Graphics2D) g; |
|
|
|
|
g2d.setStroke(UIConstants.BS); |
|
|
|
@ -200,6 +230,7 @@ public class UIToggleButton extends UIButton implements GlobalNameObserver{
|
|
|
|
|
* |
|
|
|
|
* @return 如果需要响应观察者事件则返回true,否则返回false |
|
|
|
|
*/ |
|
|
|
|
@Override |
|
|
|
|
public boolean shouldResponseChangeListener() { |
|
|
|
|
return true; |
|
|
|
|
} |
|
|
|
@ -208,6 +239,7 @@ public class UIToggleButton extends UIButton implements GlobalNameObserver{
|
|
|
|
|
* |
|
|
|
|
* @param listener 观察者监听事件 |
|
|
|
|
*/ |
|
|
|
|
@Override |
|
|
|
|
public void registerNameListener(GlobalNameListener listener) { |
|
|
|
|
globalNameListener = listener; |
|
|
|
|
} |
|
|
|
|