|
|
|
@ -1,39 +1,42 @@
|
|
|
|
|
package com.fr.design.foldablepane; |
|
|
|
|
|
|
|
|
|
import com.fine.theme.icon.LazyIcon; |
|
|
|
|
import com.fr.base.GraphHelper; |
|
|
|
|
import com.fr.design.constants.UIConstants; |
|
|
|
|
|
|
|
|
|
import javax.swing.BorderFactory; |
|
|
|
|
import javax.swing.Icon; |
|
|
|
|
import javax.swing.JFrame; |
|
|
|
|
import javax.swing.JPanel; |
|
|
|
|
import javax.swing.UIManager; |
|
|
|
|
import java.awt.AlphaComposite; |
|
|
|
|
import java.awt.BorderLayout; |
|
|
|
|
import java.awt.Color; |
|
|
|
|
import java.awt.Dimension; |
|
|
|
|
import java.awt.Font; |
|
|
|
|
import java.awt.FontMetrics; |
|
|
|
|
import java.awt.Graphics; |
|
|
|
|
import java.awt.Graphics2D; |
|
|
|
|
import java.awt.Image; |
|
|
|
|
import java.awt.Insets; |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Created by MoMeak on 2017/7/5. |
|
|
|
|
*/ |
|
|
|
|
public class HeaderPane extends JPanel { |
|
|
|
|
private static final long serialVersionUID = 1L; |
|
|
|
|
private static final int TITLE_X = 5; |
|
|
|
|
private static final int LEFT_X = 16; |
|
|
|
|
private static final int LEFT_Y = 6; |
|
|
|
|
private static final int NORMAL_FONTSIZE = 12; |
|
|
|
|
private int headWidth; |
|
|
|
|
private int headHeight; |
|
|
|
|
private Color bgColor; |
|
|
|
|
private boolean isShow; |
|
|
|
|
private boolean isPressed = false; |
|
|
|
|
private String title; |
|
|
|
|
private Image image; |
|
|
|
|
private int fontSize; |
|
|
|
|
private final Icon triangleDown; |
|
|
|
|
private final Icon triangleRight; |
|
|
|
|
private final int hGap; |
|
|
|
|
|
|
|
|
|
public void setPressed(boolean pressed) { |
|
|
|
|
this.isPressed = pressed; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public void setShow(boolean isShow) { |
|
|
|
|
this.isShow = isShow; |
|
|
|
|
} |
|
|
|
@ -58,45 +61,44 @@ public class HeaderPane extends JPanel {
|
|
|
|
|
@Override |
|
|
|
|
protected void paintComponent(Graphics g) { |
|
|
|
|
Graphics2D g2d = (Graphics2D) g.create(); |
|
|
|
|
// g2d.setColor(isPressed ? UIConstants.POPUP_TITLE_BACKGROUND : UIConstants.COMPONENT_BACKGROUND_COLOR);
|
|
|
|
|
headWidth = this.getWidth(); |
|
|
|
|
g2d.setColor(getBackground()); |
|
|
|
|
g2d.fillRect(0, 0, headWidth, headHeight); |
|
|
|
|
// g2d.setFont(new Font("SimSun", 0, fontSize));
|
|
|
|
|
g2d.setPaint(getForeground()); |
|
|
|
|
int leftWdith = headWidth - LEFT_X; |
|
|
|
|
g2d.fillRect(0, 0, this.getWidth(), this.getHeight()); |
|
|
|
|
|
|
|
|
|
int iconY = (this.getHeight() - triangleDown.getIconHeight()) / 2; |
|
|
|
|
if (this.isShow) { |
|
|
|
|
image = UIConstants.DRAG_DOWN_SELECTED_SMALL; |
|
|
|
|
g2d.drawImage(image, leftWdith, LEFT_Y, null); |
|
|
|
|
triangleDown.paintIcon(this, g2d, 0, iconY); |
|
|
|
|
} else { |
|
|
|
|
image = UIConstants.DRAG_LEFT_NORMAL_SMALL; |
|
|
|
|
g2d.drawImage(image, leftWdith, LEFT_Y, null); |
|
|
|
|
triangleRight.paintIcon(this, g2d, 0, iconY); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
GraphHelper.drawString(g2d, this.title, TITLE_X, headHeight - fontSize / 2 - 1); |
|
|
|
|
} |
|
|
|
|
g2d.setFont(getFont()); |
|
|
|
|
g2d.setPaint(getForeground()); |
|
|
|
|
g2d.setComposite(AlphaComposite.SrcOver.derive((float) getForeground().getAlpha() / 255)); |
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
public Dimension getPreferredSize() { |
|
|
|
|
return new Dimension(this.getWidth(), headHeight); |
|
|
|
|
} |
|
|
|
|
FontMetrics metrics = g2d.getFontMetrics(); |
|
|
|
|
int ascent = metrics.getAscent(); |
|
|
|
|
int descent = metrics.getDescent(); |
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
public Dimension getSize() { |
|
|
|
|
return new Dimension(this.getWidth(), headHeight); |
|
|
|
|
double titleY = (double) (getHeight() - (ascent + descent)) / 2 + ascent; |
|
|
|
|
GraphHelper.drawString(g2d, this.title, triangleDown.getIconWidth() + this.hGap, titleY); |
|
|
|
|
g2d.dispose(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public HeaderPane(Color bgColor) { |
|
|
|
|
this.bgColor = bgColor; |
|
|
|
|
this.isShow = true; |
|
|
|
|
|
|
|
|
|
triangleDown = new LazyIcon("triangle_down"); |
|
|
|
|
triangleRight = new LazyIcon("triangle_right"); |
|
|
|
|
this.hGap = UIManager.getInt("ExpandablePane.HeaderPane.hGap"); |
|
|
|
|
this.setPreferredSize(new Dimension(UIManager.getInt("HeaderPane.width"), UIManager.getInt("HeaderPane.height"))); |
|
|
|
|
Insets insets = UIManager.getInsets("ExpandablePane.HeaderPane.borderInsets"); |
|
|
|
|
this.setForeground(UIManager.getColor("color.text.highlight")); |
|
|
|
|
this.setFont(getFont().deriveFont(Font.BOLD)); |
|
|
|
|
this.setBorder(BorderFactory.createEmptyBorder(insets.top, insets.left, insets.bottom, insets.right)); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public HeaderPane(Color bgColor, String title, int headHeight) { |
|
|
|
|
this(bgColor); |
|
|
|
|
this.title = title; |
|
|
|
|
this.headHeight = headHeight; |
|
|
|
|
this.fontSize = NORMAL_FONTSIZE; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public static void main(String[] args) { |
|
|
|
|