|
|
|
@ -29,6 +29,7 @@ public class UIToggleButton extends UIButton implements GlobalNameObserver{
|
|
|
|
|
private boolean isEventBannded = false; |
|
|
|
|
private String toggleButtonName = ""; |
|
|
|
|
private GlobalNameListener globalNameListener = null; |
|
|
|
|
private Icon[] icons; |
|
|
|
|
|
|
|
|
|
public UIToggleButton() { |
|
|
|
|
this(StringUtils.EMPTY); |
|
|
|
@ -53,9 +54,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 +74,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 +93,7 @@ public class UIToggleButton extends UIButton implements GlobalNameObserver{
|
|
|
|
|
addMouseListener(getMouseListener()); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
public void setGlobalName(String name){ |
|
|
|
|
toggleButtonName = name ; |
|
|
|
|
} |
|
|
|
@ -100,6 +102,7 @@ public class UIToggleButton extends UIButton implements GlobalNameObserver{
|
|
|
|
|
* |
|
|
|
|
* @return |
|
|
|
|
*/ |
|
|
|
|
@Override |
|
|
|
|
public boolean isSelected() { |
|
|
|
|
return isSelected; |
|
|
|
|
} |
|
|
|
@ -115,9 +118,11 @@ 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() { |
|
|
|
@ -138,10 +143,29 @@ 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() { |
|
|
|
|
if (icons != null) { |
|
|
|
|
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 +181,7 @@ public class UIToggleButton extends UIButton implements GlobalNameObserver{
|
|
|
|
|
this.isEventBannded = ban; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
protected void fireStateChanged() { |
|
|
|
|
|
|
|
|
|
} |
|
|
|
@ -187,6 +212,7 @@ public class UIToggleButton extends UIButton implements GlobalNameObserver{
|
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
protected void paintOtherBorder(Graphics g) { |
|
|
|
|
Graphics2D g2d = (Graphics2D) g; |
|
|
|
|
g2d.setStroke(UIConstants.BS); |
|
|
|
@ -200,6 +226,7 @@ public class UIToggleButton extends UIButton implements GlobalNameObserver{
|
|
|
|
|
* |
|
|
|
|
* @return 如果需要响应观察者事件则返回true,否则返回false |
|
|
|
|
*/ |
|
|
|
|
@Override |
|
|
|
|
public boolean shouldResponseChangeListener() { |
|
|
|
|
return true; |
|
|
|
|
} |
|
|
|
@ -208,6 +235,7 @@ public class UIToggleButton extends UIButton implements GlobalNameObserver{
|
|
|
|
|
* |
|
|
|
|
* @param listener 观察者监听事件 |
|
|
|
|
*/ |
|
|
|
|
@Override |
|
|
|
|
public void registerNameListener(GlobalNameListener listener) { |
|
|
|
|
globalNameListener = listener; |
|
|
|
|
} |
|
|
|
|