|
|
|
@ -1,6 +1,8 @@
|
|
|
|
|
package com.fr.design.gui.ibutton; |
|
|
|
|
|
|
|
|
|
import com.fr.design.constants.UIConstants; |
|
|
|
|
import com.fine.theme.light.ui.FineRoundBorder; |
|
|
|
|
import com.fine.theme.light.ui.RectangleButtonUI; |
|
|
|
|
import com.formdev.flatlaf.ui.FlatUIUtils; |
|
|
|
|
import com.fr.design.event.GlobalNameListener; |
|
|
|
|
import com.fr.design.event.GlobalNameObserver; |
|
|
|
|
import com.fr.design.event.UIObserver; |
|
|
|
@ -15,14 +17,14 @@ import javax.swing.JPanel;
|
|
|
|
|
import javax.swing.border.Border; |
|
|
|
|
import javax.swing.event.ChangeEvent; |
|
|
|
|
import javax.swing.event.ChangeListener; |
|
|
|
|
import javax.swing.UIManager; |
|
|
|
|
import javax.swing.JComponent; |
|
|
|
|
import javax.swing.plaf.ComponentUI; |
|
|
|
|
import java.awt.Dimension; |
|
|
|
|
import java.awt.Graphics; |
|
|
|
|
import java.awt.Graphics2D; |
|
|
|
|
import java.awt.GridLayout; |
|
|
|
|
import java.awt.Insets; |
|
|
|
|
import java.awt.LayoutManager; |
|
|
|
|
import java.awt.RenderingHints; |
|
|
|
|
import java.awt.Shape; |
|
|
|
|
import java.awt.event.ActionListener; |
|
|
|
|
import java.awt.event.MouseAdapter; |
|
|
|
|
import java.awt.event.MouseEvent; |
|
|
|
@ -44,6 +46,7 @@ public class UIButtonGroup<T> extends JPanel implements GlobalNameObserver, UIOb
|
|
|
|
|
private String buttonGroupName = StringUtils.EMPTY; |
|
|
|
|
private boolean isToolBarComponent = false; |
|
|
|
|
private boolean isClick; |
|
|
|
|
protected int totalButtonSize = 0; |
|
|
|
|
|
|
|
|
|
private UIObserverListener uiObserverListener; |
|
|
|
|
private boolean autoFireStateChanged = true; |
|
|
|
@ -65,6 +68,7 @@ public class UIButtonGroup<T> extends JPanel implements GlobalNameObserver, UIOb
|
|
|
|
|
this.objectList = Arrays.asList(objects); |
|
|
|
|
} |
|
|
|
|
labelButtonList = new ArrayList<UIToggleButton>(iconArray.length); |
|
|
|
|
totalButtonSize = iconArray.length; |
|
|
|
|
this.setLayout(getGridLayout(iconArray.length)); |
|
|
|
|
this.setBorder(getGroupBorder()); |
|
|
|
|
for (int i = 0; i < iconArray.length; i++) { |
|
|
|
@ -92,7 +96,7 @@ public class UIButtonGroup<T> extends JPanel implements GlobalNameObserver, UIOb
|
|
|
|
|
return false; |
|
|
|
|
} |
|
|
|
|
}; |
|
|
|
|
initButton(labelButton); |
|
|
|
|
initButton(labelButton, i); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -100,6 +104,7 @@ public class UIButtonGroup<T> extends JPanel implements GlobalNameObserver, UIOb
|
|
|
|
|
if (!ArrayUtils.isEmpty(objects) && iconArray.length == objects.length) { |
|
|
|
|
this.objectList = Arrays.asList(objects); |
|
|
|
|
} |
|
|
|
|
totalButtonSize = iconArray.length; |
|
|
|
|
labelButtonList = new ArrayList<UIToggleButton>(iconArray.length); |
|
|
|
|
this.setLayout(getGridLayout(iconArray.length)); |
|
|
|
|
this.setBorder(getGroupBorder()); |
|
|
|
@ -128,7 +133,7 @@ public class UIButtonGroup<T> extends JPanel implements GlobalNameObserver, UIOb
|
|
|
|
|
return false; |
|
|
|
|
} |
|
|
|
|
}; |
|
|
|
|
initButton(labelButton); |
|
|
|
|
initButton(labelButton, i); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -143,15 +148,14 @@ public class UIButtonGroup<T> extends JPanel implements GlobalNameObserver, UIOb
|
|
|
|
|
public void setForToolBarButtonGroup(boolean isToolBarComponent) { |
|
|
|
|
this.isToolBarComponent = isToolBarComponent; |
|
|
|
|
if (isToolBarComponent) { |
|
|
|
|
for (int i = 0; i < labelButtonList.size(); i++) { |
|
|
|
|
labelButtonList.get(i).set4ToolbarButton(); |
|
|
|
|
for (UIToggleButton uiToggleButton : labelButtonList) { |
|
|
|
|
uiToggleButton.set4ToolbarButton(); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
repaint(); |
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* setEnabled |
|
|
|
|
* |
|
|
|
@ -168,6 +172,7 @@ public class UIButtonGroup<T> extends JPanel implements GlobalNameObserver, UIOb
|
|
|
|
|
if (!ArrayUtils.isEmpty(objects) && textArray.length == objects.length) { |
|
|
|
|
this.objectList = Arrays.asList(objects); |
|
|
|
|
} |
|
|
|
|
totalButtonSize = textArray.length; |
|
|
|
|
currentButtonSize = textArray.length; |
|
|
|
|
labelButtonList = new ArrayList<UIToggleButton>(textArray.length); |
|
|
|
|
this.setLayout(getGridLayout(textArray.length)); |
|
|
|
@ -191,51 +196,102 @@ public class UIButtonGroup<T> extends JPanel implements GlobalNameObserver, UIOb
|
|
|
|
|
}; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
public Insets getInsets() { |
|
|
|
|
return new Insets(0, 2, 0, 2); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public boolean shouldResponseNameListener() { |
|
|
|
|
return false; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
}; |
|
|
|
|
// labelButton.setUI(new UIButtonUI() {
|
|
|
|
|
// protected void paintText(Graphics g, AbstractButton b, String text, Rectangle textRec) {
|
|
|
|
|
// View v = (View) b.getClientProperty(BasicHTML.propertyKey);
|
|
|
|
|
// if (v != null) {
|
|
|
|
|
// v.paint(g, textRec);
|
|
|
|
|
// return;
|
|
|
|
|
// }
|
|
|
|
|
// FontMetrics fm = SwingUtilities2.getFontMetrics(b, g);
|
|
|
|
|
// int mnemonicIndex = b.getDisplayedMnemonicIndex();
|
|
|
|
|
// if (isPressed(b)) {
|
|
|
|
|
// g.setColor(Color.white);
|
|
|
|
|
// } else {
|
|
|
|
|
// g.setColor(b.isEnabled() ? Color.black : UIConstants.LINE_COLOR);
|
|
|
|
|
// }
|
|
|
|
|
//
|
|
|
|
|
// SwingUtilities2.drawStringUnderlineCharAt(b, g, text, mnemonicIndex, textRec.x + getTextShiftOffset(), textRec.y + fm.getAscent() + getTextShiftOffset());
|
|
|
|
|
// }
|
|
|
|
|
// });
|
|
|
|
|
initButton(labelButton); |
|
|
|
|
initButton(labelButton, i); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private static class TabButtonUI extends RectangleButtonUI { |
|
|
|
|
|
|
|
|
|
protected int minimumWidth; |
|
|
|
|
protected int minimumHeight; |
|
|
|
|
|
|
|
|
|
protected TabButtonUI(boolean shared) { |
|
|
|
|
super(shared); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public static ComponentUI createUI(JComponent c) { |
|
|
|
|
return new TabButtonUI(false); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
public void installUI(JComponent c) { |
|
|
|
|
super.installUI(c); |
|
|
|
|
background = UIManager.getColor("Button.group.background"); |
|
|
|
|
pressedBackground = UIManager.getColor("Button.group.pressedBackground"); |
|
|
|
|
selectedBackground = UIManager.getColor("Button.group.selectedBackground"); |
|
|
|
|
pressedForeground = UIManager.getColor("Button.group.pressedForeground"); |
|
|
|
|
selectedForeground = UIManager.getColor("Button.group.selectedForeground"); |
|
|
|
|
minimumWidth = UIManager.getInt("Button.group.minimumWidth"); |
|
|
|
|
minimumHeight = UIManager.getInt("Button.group.minimumHeight"); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
public Dimension getMinimumSize(JComponent c) { |
|
|
|
|
return new Dimension(minimumWidth, minimumHeight); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public void setGlobalName(String name) { |
|
|
|
|
buttonGroupName = name; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
protected void initButton(UIToggleButton labelButton) { |
|
|
|
|
labelButton.setBorderPainted(false); |
|
|
|
|
adjustButton(labelButton); |
|
|
|
|
initButton(labelButton, 0); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
protected void initButton(UIToggleButton labelButton, int buttonIndex) { |
|
|
|
|
labelButton.setUI(new TabButtonUI(false)); |
|
|
|
|
paintInnerBorder(labelButton, buttonIndex); |
|
|
|
|
adjustButton(labelButton, buttonIndex); |
|
|
|
|
UIComponentUtils.setLineWrap(labelButton); |
|
|
|
|
labelButtonList.add(labelButton); |
|
|
|
|
this.add(labelButton); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private void adjustButton(UIToggleButton labelButton) { |
|
|
|
|
/** |
|
|
|
|
* 绘制按钮组内边框 |
|
|
|
|
* |
|
|
|
|
* @param labelButton 按钮 |
|
|
|
|
* @param index 按钮序号 |
|
|
|
|
*/ |
|
|
|
|
protected void paintInnerBorder(UIToggleButton labelButton, int index) { |
|
|
|
|
LayoutManager layout = getLayout(); |
|
|
|
|
int leftBorder = 0; |
|
|
|
|
int bottomBorder = 0; |
|
|
|
|
|
|
|
|
|
if (layout instanceof GridLayout) { |
|
|
|
|
GridLayout gridLayout = (GridLayout) layout; |
|
|
|
|
int rows = gridLayout.getRows(); |
|
|
|
|
int columns = gridLayout.getColumns(); |
|
|
|
|
rows = getActualColumnRow(rows, columns, totalButtonSize); |
|
|
|
|
columns = getActualColumnRow(columns, rows, totalButtonSize); |
|
|
|
|
|
|
|
|
|
int columnIndex = index % columns; |
|
|
|
|
int rowIndex = index / columns; |
|
|
|
|
// 非首列,绘制左边框
|
|
|
|
|
leftBorder = (columnIndex != 0) ? 1 : 0; |
|
|
|
|
// 非末行,绘制底边框
|
|
|
|
|
bottomBorder = (rows > 1 && rowIndex < rows - 1) ? 1 : 0; |
|
|
|
|
} else { |
|
|
|
|
leftBorder = (index != 0) ? 1 : 0; |
|
|
|
|
} |
|
|
|
|
labelButton.setBorderPainted((leftBorder | bottomBorder) != 0); |
|
|
|
|
labelButton.setBorder((leftBorder | bottomBorder) != 0 ? |
|
|
|
|
BorderFactory.createMatteBorder(0, leftBorder, bottomBorder, 0, UIManager.getColor("defaultBorderColor")) : |
|
|
|
|
null); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private int getActualColumnRow(int origin, int divider, int total) { |
|
|
|
|
return (origin == 0 || divider == 0) ? (int) Math.ceil((double) total / divider) : origin; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private void adjustButton(UIToggleButton labelButton, int index) { |
|
|
|
|
if (labelButton.getText().length() > TEXT_LENGTH && currentButtonSize > BUTTON_SIZE) { |
|
|
|
|
Dimension dimension = labelButton.getPreferredSize(); |
|
|
|
|
dimension.height <<= 1; |
|
|
|
@ -244,24 +300,20 @@ public class UIButtonGroup<T> extends JPanel implements GlobalNameObserver, UIOb
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
protected Border getGroupBorder() { |
|
|
|
|
return BorderFactory.createEmptyBorder(1, 1, 1, 1); |
|
|
|
|
return new FineRoundBorder(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
protected LayoutManager getGridLayout(int number) { |
|
|
|
|
return new GridLayout(0, number, 0, 0); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* paintComponent |
|
|
|
|
* |
|
|
|
|
* @param g |
|
|
|
|
*/ |
|
|
|
|
public void paintComponents(Graphics g) { |
|
|
|
|
@Override |
|
|
|
|
public void paint(Graphics g) { |
|
|
|
|
Graphics2D g2d = (Graphics2D) g; |
|
|
|
|
Shape oldClip = g2d.getClip(); |
|
|
|
|
g2d.clip(new RoundRectangle2D.Double(1, 1, getWidth(), getHeight(), UIConstants.ARC, UIConstants.ARC)); |
|
|
|
|
super.paintComponents(g); |
|
|
|
|
g2d.setClip(oldClip); |
|
|
|
|
FlatUIUtils.setRenderingHints(g2d); |
|
|
|
|
int arc = UIManager.getInt("Button.group.arc"); |
|
|
|
|
g2d.clip(new RoundRectangle2D.Double(0, 0, getWidth(), getHeight(), arc, arc)); |
|
|
|
|
super.paint(g); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public void setAutoFireStateChanged(boolean autoFireStateChanged) { |
|
|
|
@ -353,7 +405,7 @@ public class UIButtonGroup<T> extends JPanel implements GlobalNameObserver, UIOb
|
|
|
|
|
/** |
|
|
|
|
* 给所有的Button添加Tips |
|
|
|
|
* |
|
|
|
|
* @param tips |
|
|
|
|
* @param tips 标签 |
|
|
|
|
*/ |
|
|
|
|
public void setAllToolTips(String[] tips) { |
|
|
|
|
for (int i = 0; i < labelButtonList.size(); i++) { |
|
|
|
@ -438,34 +490,6 @@ public class UIButtonGroup<T> extends JPanel implements GlobalNameObserver, UIOb
|
|
|
|
|
fireStateChanged(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* 重载Border画法 |
|
|
|
|
* |
|
|
|
|
* @param g |
|
|
|
|
*/ |
|
|
|
|
@Override |
|
|
|
|
protected void paintBorder(Graphics g) { |
|
|
|
|
if (isToolBarComponent) { |
|
|
|
|
return; |
|
|
|
|
} |
|
|
|
|
Graphics2D g2d = (Graphics2D) g; |
|
|
|
|
g2d.setColor(UIConstants.SHADOW_GREY); |
|
|
|
|
|
|
|
|
|
int width = 0; |
|
|
|
|
for (int i = 0; i < labelButtonList.size() - 1; i++) { |
|
|
|
|
width += labelButtonList.get(i).getWidth() + 1; |
|
|
|
|
int height = labelButtonList.get(i).getHeight(); |
|
|
|
|
g.drawLine(width, 0, width, height); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
width += labelButtonList.get(labelButtonList.size() - 1).getWidth() + 1; |
|
|
|
|
|
|
|
|
|
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); |
|
|
|
|
g2d.drawRoundRect(0, 0, width, getHeight() - 1, UIConstants.BUTTON_GROUP_ARC, UIConstants.BUTTON_GROUP_ARC); |
|
|
|
|
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_OFF); |
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* main |
|
|
|
|
* |
|
|
|
|