Browse Source

ct

feature/big-screen
neil 5 years ago
parent
commit
e26b97cdc4
  1. 452
      designer-base/src/main/java/com/fr/design/gui/ibutton/UIColorButton.java

452
designer-base/src/main/java/com/fr/design/gui/ibutton/UIColorButton.java

@ -1,19 +1,5 @@
package com.fr.design.gui.ibutton; package com.fr.design.gui.ibutton;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.AbstractButton;
import javax.swing.ButtonModel;
import javax.swing.Icon;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import javax.swing.event.EventListenerList;
import com.fr.base.Utils; import com.fr.base.Utils;
import com.fr.design.constants.UIConstants; import com.fr.design.constants.UIConstants;
import com.fr.design.event.GlobalNameListener; import com.fr.design.event.GlobalNameListener;
@ -21,231 +7,223 @@ import com.fr.design.event.GlobalNameObserver;
import com.fr.design.event.UIObserver; import com.fr.design.event.UIObserver;
import com.fr.design.event.UIObserverListener; import com.fr.design.event.UIObserverListener;
import com.fr.design.gui.ipoppane.PopupHider; import com.fr.design.gui.ipoppane.PopupHider;
import com.fr.general.ComparatorUtils;
import com.fr.design.style.color.ColorControlWindow; import com.fr.design.style.color.ColorControlWindow;
import com.fr.design.utils.gui.GUICoreUtils; import com.fr.design.utils.gui.GUICoreUtils;
import com.fr.general.ComparatorUtils;
import javax.swing.AbstractButton;
import javax.swing.ButtonModel;
import javax.swing.Icon;
import javax.swing.JComponent;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import javax.swing.event.EventListenerList;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
public class UIColorButton extends UIButton implements PopupHider, UIObserver, GlobalNameObserver { public class UIColorButton extends UIButton implements PopupHider, UIObserver, GlobalNameObserver {
private static final int SIZE = 16; private static final int SIZE = 16;
private static final int SIZE_2 = 2; private static final int SIZE_2 = 2;
private static final int SIZE_4 = 4; private static final int SIZE_4 = 4;
private static final int SIZE_6 = 6; private static final int SIZE_6 = 6;
private static final int POPUP_MENU_SHIFT = -70; private static final int POPUP_MENU_SHIFT = -70;
private Color color = Color.BLACK; private Color color = Color.BLACK;
private ColorControlWindow popupWin; private ColorControlWindow popupWin;
private EventListenerList colorChangeListenerList = new EventListenerList(); private EventListenerList colorChangeListenerList = new EventListenerList();
private boolean isEventBanned = false; private boolean isEventBanned = false;
private String colorButtonName = ""; private String colorButtonName = "";
private UIObserverListener uiColorObserverListener; private UIObserverListener uiObserverListener;
private GlobalNameListener globalNameListener = null; private GlobalNameListener globalNameListener = null;
public UIColorButton() { public UIColorButton() {
this(UIConstants.FONT_ICON); this(UIConstants.FONT_ICON);
} }
public UIColorButton(Icon icon) { public UIColorButton(Icon icon) {
super(icon); super(icon);
setUI(getButtonUI()); setUI(getButtonUI());
addMouseListener(new MouseAdapter() { addMouseListener(new MouseAdapter() {
@Override @Override
public void mouseClicked(MouseEvent e) { public void mouseClicked(MouseEvent e) {
showPopupMenu(); showPopupMenu();
} }
}); });
iniListener(); iniListener();
} }
private void iniListener() { private void iniListener() {
if (shouldResponseChangeListener()) { if (shouldResponseChangeListener()) {
this.addColorChangeListener(new ChangeListener() { this.addColorChangeListener(new ChangeListener() {
@Override @Override
public void stateChanged(ChangeEvent e) { public void stateChanged(ChangeEvent e) {
if (uiColorObserverListener == null) { if (uiObserverListener == null) {
return; return;
} }
if (globalNameListener != null && shouldResponseNameListener()) { if (globalNameListener != null && shouldResponseNameListener()) {
globalNameListener.setGlobalName(colorButtonName); globalNameListener.setGlobalName(colorButtonName);
} }
uiColorObserverListener.doChange(); uiObserverListener.doChange();
} }
}); });
} }
} }
private UIButtonUI getButtonUI() { private UIButtonUI getButtonUI() {
return new UIButtonUI() { return new UIButtonUI() {
@Override @Override
protected void paintIcon(Graphics g, JComponent c) { protected void paintIcon(Graphics g, JComponent c) {
super.paintIcon(g, c); super.paintIcon(g, c);
AbstractButton b = (AbstractButton) c; AbstractButton b = (AbstractButton) c;
ButtonModel model = b.getModel(); ButtonModel model = b.getModel();
if (model.isEnabled()) { if (model.isEnabled()) {
g.setColor(UIColorButton.this.getColor()); g.setColor(UIColorButton.this.getColor());
} else { } else {
g.setColor(new Color(Utils.filterRGB(UIColorButton.this.getColor().getRGB(), 50))); g.setColor(new Color(Utils.filterRGB(UIColorButton.this.getColor().getRGB(), 50)));
} }
g.fillRect((b.getWidth() - SIZE) / SIZE_2, b.getHeight() - SIZE_6, SIZE, SIZE_4); g.fillRect((b.getWidth() - SIZE) / SIZE_2, b.getHeight() - SIZE_6, SIZE, SIZE_4);
} }
}; };
} }
public void setEventBanned(boolean isEventBanned) { public void setEventBanned(boolean isEventBanned) {
this.isEventBanned = isEventBanned; this.isEventBanned = isEventBanned;
} }
public void setGlobalName(String name) { public void setGlobalName(String name) {
colorButtonName = name; colorButtonName = name;
} }
public Color getColor() { public Color getColor() {
return color; return color;
} }
public void setColor(Color color) { public void setColor(Color color) {
if (ComparatorUtils.equals(this.color, color)) { if (ComparatorUtils.equals(this.color, color)) {
return; return;
} }
this.color = color; this.color = color;
hidePopupMenu(); hidePopupMenu();
fireColorStateChanged(); fireColorStateChanged();
} }
private void showPopupMenu() { private void showPopupMenu() {
if (isEventBanned) { if (isEventBanned) {
return; return;
} }
if (popupWin != null && popupWin.isVisible()) { if (popupWin != null && popupWin.isVisible()) {
hidePopupMenu(); hidePopupMenu();
return; return;
} }
if (!this.isEnabled()) { if (!this.isEnabled()) {
return; return;
} }
popupWin = this.getColorControlWindow(); popupWin = this.getColorControlWindow();
GUICoreUtils.showPopupMenu(popupWin, this, POPUP_MENU_SHIFT, this.getSize().height); GUICoreUtils.showPopupMenu(popupWin, this, POPUP_MENU_SHIFT, this.getSize().height);
} }
/** /**
* 隐藏popupmenu * 隐藏popupmenu
*/ */
public void hidePopupMenu() { public void hidePopupMenu() {
if (popupWin != null) { if (popupWin != null) {
popupWin.setVisible(false); popupWin.setVisible(false);
repaint(); }
} popupWin = null;
repaint();
popupWin = null; }
}
private ColorControlWindow getColorControlWindow() {
private ColorControlWindow getColorControlWindow() { //find parant.
//find parant. if (this.popupWin == null) {
if (this.popupWin == null) { this.popupWin = new ColorControlWindow(UIColorButton.this) {
this.popupWin = new ColorControlWindow(UIColorButton.this) { @Override
@Override protected void colorChanged() {
protected void colorChanged() { UIColorButton.this.setColor(this.getColor());
UIColorButton.this.setColor(this.getColor()); }
}
};
}; }
}
return popupWin;
return popupWin; }
}
/**
/** * 添加监听
* 添加监听 *
* * @param changeListener 监听列表
* @param changeListener 监听列表 */
*/ public void addColorChangeListener(ChangeListener changeListener) {
public void addColorChangeListener(ChangeListener changeListener) { colorChangeListenerList.add(ChangeListener.class, changeListener);
colorChangeListenerList.add(ChangeListener.class, changeListener); }
}
/**
/** * 移除监听
* 移除监听 * Removes an old ColorChangeListener.
* Removes an old ColorChangeListener. *
* @param changeListener 监听列表 * @param changeListener 监听列表
*/ */
public void removeColorChangeListener(ChangeListener changeListener) { public void removeColorChangeListener(ChangeListener changeListener) {
colorChangeListenerList.remove(ChangeListener.class, changeListener); colorChangeListenerList.remove(ChangeListener.class, changeListener);
} }
/** /**
* 颜色状态改变 * 颜色状态改变
*/ */
public void fireColorStateChanged() { public void fireColorStateChanged() {
Object[] listeners = colorChangeListenerList.getListenerList(); Object[] listeners = colorChangeListenerList.getListenerList();
ChangeEvent e = null; ChangeEvent e = null;
for (int i = listeners.length - 2; i >= 0; i -= 2) { for (int i = listeners.length - 2; i >= 0; i -= 2) {
if (listeners[i] == ChangeListener.class) { if (listeners[i] == ChangeListener.class) {
if (e == null) { if (e == null) {
e = new ChangeEvent(this); e = new ChangeEvent(this);
} }
((ChangeListener) listeners[i + 1]).stateChanged(e); ((ChangeListener) listeners[i + 1]).stateChanged(e);
} }
} }
} }
/** /**
* 注册状态改变监听 * 注册状态改变监听
* *
* @param listener 观察者监听事件 * @param listener 观察者监听事件
*/ */
public void registerChangeListener(UIObserverListener listener) { public void registerChangeListener(UIObserverListener listener) {
uiColorObserverListener = listener; uiObserverListener = listener;
} }
/** /**
* 是否需要响应监听 * 是否需要响应监听
* *
* @return 是否响应 * @return 是否响应
*/ */
public boolean shouldResponseChangeListener() { public boolean shouldResponseChangeListener() {
return true; return true;
} }
/** /**
* 注册监听 * 注册监听
* *
* @param listener 观察者监听事件 * @param listener 观察者监听事件
*/ */
public void registerNameListener(GlobalNameListener listener) { public void registerNameListener(GlobalNameListener listener) {
globalNameListener = listener; globalNameListener = listener;
} }
/** /**
* 是否需要相应 * 是否需要相应
* *
* @return 是否响应 * @return 是否响应
*/ */
public boolean shouldResponseNameListener() { public boolean shouldResponseNameListener() {
return true; return true;
} }
/**
* 主函数
*
* @param args 参数
*/
public static void main(String... args) {
LayoutManager layoutManager = null;
JFrame jf = new JFrame("test");
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel content = (JPanel) jf.getContentPane();
content.setLayout(layoutManager);
UIColorButton bb = new UIColorButton(UIConstants.FONT_ICON);
bb.setBounds(20, 20, bb.getPreferredSize().width, bb.getPreferredSize().height);
content.add(bb);
GUICoreUtils.centerWindow(jf);
jf.setSize(400, 400);
jf.setVisible(true);
}
} }
Loading…
Cancel
Save