Browse Source

btn group

master
hzzz 7 years ago
parent
commit
554ca42e54
  1. 1
      designer_base/src/com/fr/design/constants/UIConstants.java
  2. 814
      designer_base/src/com/fr/design/gui/ibutton/UIButtonGroup.java

1
designer_base/src/com/fr/design/constants/UIConstants.java

@ -144,6 +144,7 @@ public interface UIConstants {
public static final Color GRDIENT_DARK_GRAY = new Color(45, 45, 45); public static final Color GRDIENT_DARK_GRAY = new Color(45, 45, 45);
public static final Color BARNOMAL = new Color(232, 232, 233); public static final Color BARNOMAL = new Color(232, 232, 233);
public static final int ARC = 0; public static final int ARC = 0;
public static final int BUTTON_GROUP_ARC = 6;
public static final int LARGEARC = 6; public static final int LARGEARC = 6;
public static final Stroke BS = new BasicStroke(1f, BasicStroke.CAP_BUTT, BasicStroke.JOIN_ROUND, 2f, new float[]{3, 1}, 0); public static final Stroke BS = new BasicStroke(1f, BasicStroke.CAP_BUTT, BasicStroke.JOIN_ROUND, 2f, new float[]{3, 1}, 0);
public static final Icon PREVIEW_DOWN = BaseUtils.readIcon("com/fr/design/images/buttonicon/prevew_down_icon.png"); public static final Icon PREVIEW_DOWN = BaseUtils.readIcon("com/fr/design/images/buttonicon/prevew_down_icon.png");

814
designer_base/src/com/fr/design/gui/ibutton/UIButtonGroup.java

@ -1,5 +1,17 @@
package com.fr.design.gui.ibutton; package com.fr.design.gui.ibutton;
import com.fr.base.BaseUtils;
import com.fr.design.constants.UIConstants;
import com.fr.design.event.GlobalNameListener;
import com.fr.design.event.GlobalNameObserver;
import com.fr.design.utils.gui.GUICoreUtils;
import com.fr.stable.ArrayUtils;
import com.fr.stable.Constants;
import javax.swing.*;
import javax.swing.border.Border;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import java.awt.*; import java.awt.*;
import java.awt.event.ActionListener; import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter; import java.awt.event.MouseAdapter;
@ -10,419 +22,403 @@ import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.List; import java.util.List;
import javax.swing.BorderFactory;
import javax.swing.Icon;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.Border;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import com.fr.base.BaseUtils;
import com.fr.design.constants.UIConstants;
import com.fr.design.event.GlobalNameListener;
import com.fr.design.event.GlobalNameObserver;
import com.fr.stable.ArrayUtils;
import com.fr.stable.Constants;
import com.fr.design.utils.gui.GUICoreUtils;
public class UIButtonGroup<T> extends JPanel implements GlobalNameObserver { public class UIButtonGroup<T> extends JPanel implements GlobalNameObserver {
private boolean isTwoLine = false; private boolean isTwoLine = false;
private boolean isFourLine = false; private boolean isFourLine = false;
private static final long serialVersionUID = 1L; private static final long serialVersionUID = 1L;
protected List<UIToggleButton> labelButtonList; protected List<UIToggleButton> labelButtonList;
protected int selectedIndex = -1; protected int selectedIndex = -1;
private List<T> objectList;// 起到一个render的作用 private List<T> objectList;// 起到一个render的作用
private GlobalNameListener globalNameListener = null; private GlobalNameListener globalNameListener = null;
private String buttonGroupName = ""; private String buttonGroupName = "";
private boolean isToolBarComponent = false; private boolean isToolBarComponent = false;
private boolean isClick; private boolean isClick;
public UIButtonGroup(String[] textArray) { public UIButtonGroup(String[] textArray) {
this(textArray, null); this(textArray, null);
} }
public UIButtonGroup(Icon[] iconArray) { public UIButtonGroup(Icon[] iconArray) {
this(iconArray, null); this(iconArray, null);
} }
public UIButtonGroup(Icon[] iconArray, T[] objects) { public UIButtonGroup(Icon[] iconArray, T[] objects) {
if (!ArrayUtils.isEmpty(objects) && iconArray.length == objects.length) { if (!ArrayUtils.isEmpty(objects) && iconArray.length == objects.length) {
this.objectList = Arrays.asList(objects); this.objectList = Arrays.asList(objects);
} }
labelButtonList = new ArrayList<UIToggleButton>(iconArray.length); labelButtonList = new ArrayList<UIToggleButton>(iconArray.length);
this.setLayout(getGridLayout(iconArray.length)); this.setLayout(getGridLayout(iconArray.length));
this.setBorder(getGroupBorder()); this.setBorder(getGroupBorder());
for (int i = 0; i < iconArray.length; i++) { for (int i = 0; i < iconArray.length; i++) {
final int index = i; final int index = i;
Icon icon = iconArray[i]; Icon icon = iconArray[i];
final UIToggleButton labelButton = new UIToggleButton(icon) { final UIToggleButton labelButton = new UIToggleButton(icon) {
@Override @Override
protected MouseListener getMouseListener() { protected MouseListener getMouseListener() {
return new MouseAdapter() { return new MouseAdapter() {
@Override @Override
public void mousePressed(MouseEvent e) { public void mousePressed(MouseEvent e) {
isClick = true; isClick = true;
if (!isEnabled()) { if (!isEnabled()) {
return; return;
} }
if (globalNameListener != null) { if (globalNameListener != null) {
globalNameListener.setGlobalName(buttonGroupName); globalNameListener.setGlobalName(buttonGroupName);
} }
setSelectedWithFireChanged(index); setSelectedWithFireChanged(index);
} }
}; };
} }
public boolean shouldResponseNameListener() { public boolean shouldResponseNameListener() {
return false; return false;
} }
}; };
initButton(labelButton); initButton(labelButton);
} }
} }
public boolean hasClick(){ public boolean hasClick() {
return isClick; return isClick;
} }
public void setClickState(boolean changeFlag){ public void setClickState(boolean changeFlag) {
isClick = changeFlag; isClick = changeFlag;
} }
public void setForToolBarButtonGroup(boolean isToolBarComponent) { public void setForToolBarButtonGroup(boolean isToolBarComponent) {
this.isToolBarComponent = isToolBarComponent; this.isToolBarComponent = isToolBarComponent;
if (isToolBarComponent) { if (isToolBarComponent) {
for (int i = 0; i < labelButtonList.size(); i++) { for (int i = 0; i < labelButtonList.size(); i++) {
labelButtonList.get(i).set4ToolbarButton(); labelButtonList.get(i).set4ToolbarButton();
} }
} }
repaint(); repaint();
} }
/** /**
* setEnabled * setEnabled
* *
* @param enabled * @param enabled
*/ */
public void setEnabled(boolean enabled) { public void setEnabled(boolean enabled) {
super.setEnabled(enabled); super.setEnabled(enabled);
for (int i = 0; i < labelButtonList.size(); i++) { for (int i = 0; i < labelButtonList.size(); i++) {
labelButtonList.get(i).setEnabled(enabled); labelButtonList.get(i).setEnabled(enabled);
} }
} }
public UIButtonGroup(String[] textArray, T[] objects) { public UIButtonGroup(String[] textArray, T[] objects) {
if (!ArrayUtils.isEmpty(objects) && textArray.length == objects.length) { if (!ArrayUtils.isEmpty(objects) && textArray.length == objects.length) {
this.objectList = Arrays.asList(objects); this.objectList = Arrays.asList(objects);
} }
labelButtonList = new ArrayList<UIToggleButton>(textArray.length); labelButtonList = new ArrayList<UIToggleButton>(textArray.length);
this.setLayout(getGridLayout(textArray.length)); this.setLayout(getGridLayout(textArray.length));
this.setBorder(getGroupBorder()); this.setBorder(getGroupBorder());
for (int i = 0; i < textArray.length; i++) { for (int i = 0; i < textArray.length; i++) {
final int index = i; final int index = i;
final UIToggleButton labelButton = new UIToggleButton(textArray[i]) { final UIToggleButton labelButton = new UIToggleButton(textArray[i]) {
@Override @Override
protected MouseListener getMouseListener() { protected MouseListener getMouseListener() {
return new MouseAdapter() { return new MouseAdapter() {
@Override @Override
public void mousePressed(MouseEvent e) { public void mousePressed(MouseEvent e) {
if (!isEnabled()) { if (!isEnabled()) {
return; return;
} }
if(globalNameListener != null) { if (globalNameListener != null) {
globalNameListener.setGlobalName(buttonGroupName); globalNameListener.setGlobalName(buttonGroupName);
} }
setSelectedWithFireChanged(index); setSelectedWithFireChanged(index);
} }
}; };
} }
@Override @Override
public Insets getInsets() { public Insets getInsets() {
return new Insets(0, 2, 0, 2); return new Insets(0, 2, 0, 2);
} }
public boolean shouldResponseNameListener() { public boolean shouldResponseNameListener() {
return false; return false;
} }
}; };
initButton(labelButton); initButton(labelButton);
} }
} }
public void setGlobalName(String name) { public void setGlobalName(String name) {
buttonGroupName = name; buttonGroupName = name;
} }
protected void initButton(UIToggleButton labelButton) { protected void initButton(UIToggleButton labelButton) {
labelButton.setRoundBorder(false); labelButton.setRoundBorder(false);
labelButton.setBorderPainted(false); labelButton.setBorderPainted(false);
labelButtonList.add(labelButton); labelButtonList.add(labelButton);
this.add(labelButton); this.add(labelButton);
} }
protected Border getGroupBorder() { protected Border getGroupBorder() {
return BorderFactory.createEmptyBorder(1, 1, 1, 1); return BorderFactory.createEmptyBorder(1, 1, 1, 1);
} }
protected LayoutManager getGridLayout(int number) { protected LayoutManager getGridLayout(int number) {
return new GridLayout(0, number, 1, 0); return new GridLayout(0, number, 1, 0);
} }
public void setTwoLine() { public void setTwoLine() {
this.isTwoLine = true; this.isTwoLine = true;
} }
public void setFourLine() { public void setFourLine() {
this.isFourLine = true; this.isFourLine = true;
} }
/** /**
* paintComponent * paintComponent
* *
* @param g * @param g
*/ */
public void paintComponents(Graphics g) { public void paintComponents(Graphics g) {
Graphics2D g2d = (Graphics2D) g; Graphics2D g2d = (Graphics2D) g;
Shape oldClip = g2d.getClip(); Shape oldClip = g2d.getClip();
g2d.clip(new RoundRectangle2D.Double(1, 1, getWidth(), getHeight(), UIConstants.ARC, UIConstants.ARC)); g2d.clip(new RoundRectangle2D.Double(1, 1, getWidth(), getHeight(), UIConstants.ARC, UIConstants.ARC));
super.paintComponents(g); super.paintComponents(g);
g2d.setClip(oldClip); g2d.setClip(oldClip);
} }
/** /**
* 重载Border画法 * 重载Border画法
* *
* @param g * @param g
*/ */
@Override @Override
protected void paintBorder(Graphics g) { protected void paintBorder(Graphics g) {
if(isToolBarComponent){ if (isToolBarComponent) {
return; return;
} }
Graphics2D g2d = (Graphics2D) g; Graphics2D g2d = (Graphics2D) g;
g2d.setColor(UIConstants.LINE_COLOR); g2d.setColor(UIConstants.LINE_COLOR);
if (isTwoLine) { if (isTwoLine) {
int width = 0; int width = 0;
int upCount = (labelButtonList.size() - 1) / 2 + 1; int upCount = (labelButtonList.size() - 1) / 2 + 1;
for (int i = 0; i < upCount - 1; i++) { for (int i = 0; i < upCount - 1; i++) {
width += labelButtonList.get(i).getWidth() + 1; width += labelButtonList.get(i).getWidth() + 1;
int height = labelButtonList.get(i).getHeight() * 2 + 1; int height = labelButtonList.get(i).getHeight() * 2 + 1;
g.drawLine(width, 0, width, height); g.drawLine(width, 0, width, height);
} }
width += labelButtonList.get(upCount).getWidth() + 1; width += labelButtonList.get(upCount).getWidth() + 1;
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2d.drawRoundRect(0, 0, width, getHeight() - 1, UIConstants.ARC, UIConstants.ARC); g2d.drawRoundRect(0, 0, width, getHeight() - 1, UIConstants.ARC, UIConstants.ARC);
g2d.drawLine(0, getHeight() / 2, width, getHeight() / 2); g2d.drawLine(0, getHeight() / 2, width, getHeight() / 2);
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_OFF); g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_OFF);
} else if (isFourLine) { } else if (isFourLine) {
// 4 * 1 // 4 * 1
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2d.drawRoundRect(0, 0, getWidth() - 1, getHeight() - 1, UIConstants.ARC, UIConstants.ARC); g2d.drawRoundRect(0, 0, getWidth() - 1, getHeight() - 1, UIConstants.ARC, UIConstants.ARC);
for (int i = 1; i <= 3; i++) { for (int i = 1; i <= 3; i++) {
g2d.drawLine(0, getHeight() / 4 * i, getWidth() - 1, getHeight() / 4 * i); g2d.drawLine(0, getHeight() / 4 * i, getWidth() - 1, getHeight() / 4 * i);
} }
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_OFF); g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_OFF);
} else { } else {
int width = 0; int width = 0;
for (int i = 0; i < labelButtonList.size() - 1; i++) { for (int i = 0; i < labelButtonList.size() - 1; i++) {
width += labelButtonList.get(i).getWidth() + 1; width += labelButtonList.get(i).getWidth() + 1;
int height = labelButtonList.get(i).getHeight(); int height = labelButtonList.get(i).getHeight();
g.drawLine(width, 0, width, height); g.drawLine(width, 0, width, height);
} }
width += labelButtonList.get(labelButtonList.size() - 1).getWidth() + 1; width += labelButtonList.get(labelButtonList.size() - 1).getWidth() + 1;
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2d.drawRoundRect(0, 0, width, getHeight() - 1, UIConstants.ARC, UIConstants.ARC); g2d.drawRoundRect(0, 0, width, getHeight() - 1, UIConstants.ARC, UIConstants.ARC);
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_OFF); g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_OFF);
} }
} }
/** /**
* setSelectedItem * setSelectedItem
* *
* @param ob * @param ob
*/ */
public void setSelectedItem(T ob) { public void setSelectedItem(T ob) {
if (objectList == null) { if (objectList == null) {
setSelectedIndex(-1); setSelectedIndex(-1);
return; return;
} }
setSelectedIndex(objectList.indexOf(ob)); setSelectedIndex(objectList.indexOf(ob));
} }
/** /**
* getSelectedItem * getSelectedItem
* *
* @return * @return
*/ */
public T getSelectedItem() { public T getSelectedItem() {
if (selectedIndex == -1) { if (selectedIndex == -1) {
return null; return null;
} }
return objectList.get(selectedIndex); return objectList.get(selectedIndex);
} }
/** /**
* getSelectedIndex * getSelectedIndex
* *
* @return * @return
*/ */
public int getSelectedIndex() { public int getSelectedIndex() {
return selectedIndex; return selectedIndex;
} }
protected void setSelectedWithFireChanged(int newSelectedIndex) { protected void setSelectedWithFireChanged(int newSelectedIndex) {
selectedIndex = newSelectedIndex; selectedIndex = newSelectedIndex;
for (int i = 0; i < labelButtonList.size(); i++) { for (int i = 0; i < labelButtonList.size(); i++) {
if (i == selectedIndex) { if (i == selectedIndex) {
labelButtonList.get(i).setSelectedWithFireListener(true); labelButtonList.get(i).setSelectedWithFireListener(true);
} else { } else {
labelButtonList.get(i).setSelected(false); labelButtonList.get(i).setSelected(false);
} }
} }
} }
/** /**
* setSelectedIndex * setSelectedIndex
* *
* @param newSelectedIndex * @param newSelectedIndex
*/ */
public void setSelectedIndex(int newSelectedIndex) { public void setSelectedIndex(int newSelectedIndex) {
selectedIndex = newSelectedIndex; selectedIndex = newSelectedIndex;
for (int i = 0; i < labelButtonList.size(); i++) { for (int i = 0; i < labelButtonList.size(); i++) {
labelButtonList.get(i).setSelected(i == selectedIndex); labelButtonList.get(i).setSelected(i == selectedIndex);
} }
} }
private void fireStateChanged() { private void fireStateChanged() {
Object[] listeners = listenerList.getListenerList(); Object[] listeners = listenerList.getListenerList();
ChangeEvent e = null; ChangeEvent e = null;
for (int i = 0; i < listeners.length; i++) { for (int i = 0; i < listeners.length; i++) {
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);
} }
} }
} }
/** /**
* getButton * getButton
* *
* @param index * @param index
* @return * @return
*/ */
public UIToggleButton getButton(int index) { public UIToggleButton getButton(int index) {
return labelButtonList.get(index); return labelButtonList.get(index);
} }
/** /**
* 给所有的Button添加Tips * 给所有的Button添加Tips
* *
* @param tips * @param tips
*/ */
public void setAllToolTips(String[] tips) { public void setAllToolTips(String[] tips) {
for (int i = 0; i < labelButtonList.size(); i++) { for (int i = 0; i < labelButtonList.size(); i++) {
labelButtonList.get(i).setToolTipText(tips[i]); labelButtonList.get(i).setToolTipText(tips[i]);
} }
} }
/** /**
* 注册全局属性名字监听器 * 注册全局属性名字监听器
* *
* @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 l * @param l
*/ */
public void addChangeListener(ChangeListener l) { public void addChangeListener(ChangeListener l) {
for (int i = 0; i < labelButtonList.size(); i++) { for (int i = 0; i < labelButtonList.size(); i++) {
labelButtonList.get(i).addChangeListener(l); labelButtonList.get(i).addChangeListener(l);
listenerList.add(ChangeListener.class, l); listenerList.add(ChangeListener.class, l);
} }
} }
/** /**
* @param l * @param l
*/ */
public void removeChangeListener(ChangeListener l) { public void removeChangeListener(ChangeListener l) {
this.listenerList.remove(ChangeListener.class, l); this.listenerList.remove(ChangeListener.class, l);
} }
/** /**
* @param l * @param l
*/ */
public void addActionListener(ActionListener l) { public void addActionListener(ActionListener l) {
for (int i = 0; i < labelButtonList.size(); i++) { for (int i = 0; i < labelButtonList.size(); i++) {
labelButtonList.get(i).addActionListener(l); labelButtonList.get(i).addActionListener(l);
} }
} }
/** /**
* @param l * @param l
*/ */
public void removeActionListener(ActionListener l) { public void removeActionListener(ActionListener l) {
for (int i = 0; i < labelButtonList.size(); i++) { for (int i = 0; i < labelButtonList.size(); i++) {
labelButtonList.get(i).removeActionListener(l); labelButtonList.get(i).removeActionListener(l);
} }
} }
/** /**
* populate * populate
*/ */
public void populateBean() { public void populateBean() {
fireStateChanged(); fireStateChanged();
} }
/** /**
* main * main
* *
* @param args * @param args
*/ */
public static void main(String... args) { public static void main(String... args) {
JFrame jf = new JFrame("test"); JFrame jf = new JFrame("test");
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel content = (JPanel) jf.getContentPane(); JPanel content = (JPanel) jf.getContentPane();
content.setLayout(new BorderLayout()); content.setLayout(new BorderLayout());
Icon[] a1 = {BaseUtils.readIcon("/com/fr/design/images/m_format/cellstyle/h_left_normal.png"), BaseUtils.readIcon("/com/fr/design/images/m_format/cellstyle/h_center_normal.png"), Icon[] a1 = {BaseUtils.readIcon("/com/fr/design/images/m_format/cellstyle/h_left_normal.png"), BaseUtils.readIcon("/com/fr/design/images/m_format/cellstyle/h_center_normal.png"),
BaseUtils.readIcon("/com/fr/design/images/m_format/cellstyle/h_right_normal.png")}; BaseUtils.readIcon("/com/fr/design/images/m_format/cellstyle/h_right_normal.png")};
Integer[] a2 = new Integer[]{Constants.LEFT, Constants.CENTER, Constants.RIGHT}; Integer[] a2 = new Integer[]{Constants.LEFT, Constants.CENTER, Constants.RIGHT};
UIButtonGroup<Integer> bb = new UIButtonGroup<Integer>(a1, a2); UIButtonGroup<Integer> bb = new UIButtonGroup<Integer>(a1, a2);
bb.setBounds(20, 20, bb.getPreferredSize().width, bb.getPreferredSize().height); bb.setBounds(20, 20, bb.getPreferredSize().width, bb.getPreferredSize().height);
bb.setSelectedIndex(0); bb.setSelectedIndex(0);
bb.setEnabled(false); bb.setEnabled(false);
content.add(bb); content.add(bb);
GUICoreUtils.centerWindow(jf); GUICoreUtils.centerWindow(jf);
jf.setSize(400, 400); jf.setSize(400, 400);
jf.setVisible(true); jf.setVisible(true);
} }
} }
Loading…
Cancel
Save