You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
245 lines
7.3 KiB
245 lines
7.3 KiB
package com.fanruan.api.design.ui.component; |
|
|
|
import com.fanruan.api.design.macro.UIConstants; |
|
import com.fanruan.api.util.StringKit; |
|
import com.fr.design.event.GlobalNameListener; |
|
import com.fr.design.event.GlobalNameObserver; |
|
import com.fr.design.gui.ibutton.UIButtonUI; |
|
import com.fr.design.roleAuthority.ReportAndFSManagePane; |
|
import com.fr.design.utils.gui.GUIPaintUtils; |
|
|
|
import javax.swing.*; |
|
import javax.swing.event.ChangeEvent; |
|
import javax.swing.event.ChangeListener; |
|
import java.awt.*; |
|
import java.awt.event.ActionEvent; |
|
import java.awt.event.MouseAdapter; |
|
import java.awt.event.MouseEvent; |
|
import java.awt.event.MouseListener; |
|
import java.awt.geom.RoundRectangle2D; |
|
|
|
/** |
|
* @author richie |
|
* @version 10.0 |
|
* Created by richie on 2019/10/25 |
|
* 可切换状态的按钮 |
|
*/ |
|
public class UIToggleButton extends UIButton implements GlobalNameObserver { |
|
|
|
private static final int ICON_COUNT = 2; |
|
private boolean selected; |
|
private boolean eventBanned = false; |
|
private String toggleButtonName = StringKit.EMPTY; |
|
private GlobalNameListener globalNameListener = null; |
|
private Icon[] icons; |
|
|
|
public UIToggleButton() { |
|
this(StringKit.EMPTY); |
|
} |
|
|
|
public UIToggleButton(Icon image) { |
|
this(StringKit.EMPTY, image); |
|
} |
|
|
|
public UIToggleButton(String text) { |
|
this(text, null); |
|
} |
|
|
|
public UIToggleButton(String text, Icon image) { |
|
super(text, image); |
|
addMouseListener(getMouseListener()); |
|
} |
|
|
|
/** |
|
* 需要反白的按钮接口(组合按钮情况-UIButtonGroup) |
|
* support icons[normalIcon, selectedIcon] |
|
* |
|
* @param icons 图标 |
|
*/ |
|
public UIToggleButton(Icon[] icons) { |
|
super(icons[0], null, null); |
|
setExtraPainted(true); |
|
this.icons = icons; |
|
addActionListener(new AbstractAction() { |
|
@Override |
|
public void actionPerformed(ActionEvent e) { |
|
if (!UIToggleButton.super.isSelected()) { |
|
UIToggleButton.super.setSelected(!UIToggleButton.super.isSelected()); |
|
} |
|
} |
|
}); |
|
addMouseListener(getMouseListener()); |
|
} |
|
|
|
/** |
|
* 需要反白的按钮接口(单个按钮情况)-再次点击取消选中状态 |
|
* |
|
* @param icons 图标 |
|
* @param needRelease 是否能够点击之后释放 |
|
*/ |
|
public UIToggleButton(Icon[] icons, boolean needRelease) { |
|
super(icons[0], null, null); |
|
setBorderPainted(true); |
|
setExtraPainted(true); |
|
this.icons = icons; |
|
if (!needRelease) { |
|
addActionListener(new AbstractAction() { |
|
@Override |
|
public void actionPerformed(ActionEvent e) { |
|
if (UIToggleButton.super.isSelected()) { |
|
UIToggleButton.super.setSelected(false); |
|
} else { |
|
UIToggleButton.super.setSelected(true); |
|
} |
|
} |
|
}); |
|
} |
|
addMouseListener(getMouseListener()); |
|
} |
|
|
|
@Override |
|
public final void setGlobalName(String name) { |
|
toggleButtonName = name; |
|
} |
|
|
|
@Override |
|
public final boolean isSelected() { |
|
return selected; |
|
} |
|
|
|
@Override |
|
public final void setSelected(boolean isSelected) { |
|
super.setSelected(isSelected); |
|
if (this.selected != isSelected) { |
|
this.selected = isSelected; |
|
refresh(isSelected); |
|
} |
|
} |
|
|
|
@Override |
|
protected void initListener() { |
|
if (shouldResponseChangeListener()) { |
|
this.addChangeListener(new ChangeListener() { |
|
@Override |
|
public void stateChanged(ChangeEvent e) { |
|
if (uiObserverListener == null) { |
|
return; |
|
} |
|
if (globalNameListener != null && shouldResponseNameListener()) { |
|
globalNameListener.setGlobalName(toggleButtonName); |
|
} |
|
uiObserverListener.doChange(); |
|
} |
|
}); |
|
} |
|
} |
|
|
|
public final void setSelectedWithFireListener(boolean isSelected) { |
|
if (this.selected != isSelected) { |
|
this.selected = isSelected; |
|
fireSelectedChanged(); |
|
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 |
|
public void mouseClicked(MouseEvent e) { |
|
if (isEnabled() && !eventBanned) { |
|
setSelectedWithFireListener(!isSelected()); |
|
} |
|
} |
|
}; |
|
} |
|
|
|
public void setEventBanned(boolean ban) { |
|
this.eventBanned = ban; |
|
} |
|
|
|
@Override |
|
protected void fireStateChanged() { |
|
|
|
} |
|
|
|
|
|
protected final void fireSelectedChanged() { |
|
Object[] listeners = listenerList.getListenerList(); |
|
|
|
for (int i = listeners.length - 2; i >= 0; i -= 2) { |
|
if (listeners[i] == ChangeListener.class) { |
|
((ChangeListener) listeners[i + 1]).stateChanged(new ChangeEvent(this)); |
|
} |
|
} |
|
} |
|
|
|
@Override |
|
protected final void paintBorder(Graphics g) { |
|
if (!isBorderPainted()) { |
|
return; |
|
} |
|
boolean isBorderPainted = isBorderPaintedOnlyWhenPressed && (getModel().isPressed() || selected); |
|
if (isBorderPainted || !isBorderPaintedOnlyWhenPressed) { |
|
if (ui instanceof UIButtonUI) { |
|
drawRoleName((Graphics2D) g); |
|
} else { |
|
super.paintBorder(g); |
|
} |
|
} |
|
} |
|
|
|
private void drawRoleName(Graphics2D g) { |
|
String roleName = ReportAndFSManagePane.getInstance().getRoleTree().getSelectedRoleName(); |
|
GUIPaintUtils.drawBorder(g, 0, 0, getWidth(), getHeight(), isRoundBorder(), getRectDirection(), isDoneAuthorityEdited(roleName)); |
|
} |
|
|
|
@Override |
|
protected final void paintOtherBorder(Graphics g) { |
|
Graphics2D g2d = (Graphics2D) g; |
|
g2d.setStroke(UIConstants.BS); |
|
Shape shape = new RoundRectangle2D.Float(0.5f, 0.5f, getWidth() - 1F, getHeight() - 1F, UIConstants.ARC, UIConstants.ARC); |
|
g2d.setColor(UIConstants.LINE_COLOR); |
|
g2d.draw(shape); |
|
} |
|
|
|
/** |
|
* 组件是否需要响应添加的观察者事件 |
|
* |
|
* @return 如果需要响应观察者事件则返回true,否则返回false |
|
*/ |
|
@Override |
|
public final boolean shouldResponseChangeListener() { |
|
return true; |
|
} |
|
|
|
/** |
|
* @param listener 观察者监听事件 |
|
*/ |
|
@Override |
|
public final void registerNameListener(GlobalNameListener listener) { |
|
globalNameListener = listener; |
|
} |
|
|
|
public final boolean shouldResponseNameListener() { |
|
return true; |
|
} |
|
}
|
|
|