⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 titlebarui.java

📁 定要上载质量高而定要上载质量高而定要上载质量高而定要上载质量高而定要上载质量高而
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/*
 * Created on Feb 27, 2005
 */
package org.flexdock.plaf.theme;

import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Insets;
import java.awt.Rectangle;
import java.awt.RenderingHints;

import javax.swing.Action;
import javax.swing.Icon;
import javax.swing.JComponent;
import javax.swing.border.Border;
import javax.swing.plaf.FontUIResource;
import javax.swing.plaf.UIResource;

import org.flexdock.plaf.FlexViewComponentUI;
import org.flexdock.plaf.icons.IconMap;
import org.flexdock.plaf.icons.IconResource;
import org.flexdock.plaf.icons.IconResourceFactory;
import org.flexdock.plaf.resources.paint.Painter;
import org.flexdock.view.Button;
import org.flexdock.view.Titlebar;
import org.flexdock.view.View;

/**
 * @author Christopher Butler
 */
public class TitlebarUI extends FlexViewComponentUI {

    public static final String DEFAULT_HEIGHT = "default.height";
    public static final String FONT = "font";
    public static final String FONT_COLOR = "font.color";
    public static final String FONT_COLOR_ACTIVE = "font.color.active";
    public static final String BACKGROUND_COLOR = "bgcolor";
    public static final String BACKGROUND_COLOR_ACTIVE = "bgcolor.active";
    public static final String BORDER = "border";
    public static final String BORDER_ACTIVE = "border.active";
    public static final String PAINTER = "painter";
    public static final String INSETS = "insets";
    private static final String BUTTON_MARGIN = "button.margin";
    public static final String ICON_INSETS = "icon.insets";
    public static final String ANTIALIASING = "antialiasing";
    public static final int MINIMUM_HEIGHT = 12;
    
    protected Font font;
    protected Color activeFont;
    protected Color inactiveFont;
    protected Color activeBackground;
    protected Color inactiveBackground;
    protected Border activeBorder;
    protected Border inactiveBorder;
    protected IconMap defaultIcons;
    protected Painter painter;
    protected Insets insets;
    protected int buttonMargin;
    protected Insets iconInsets;
    protected Object antialiasing;
    protected int defaultHeight = MINIMUM_HEIGHT;
    

    public void installUI(JComponent c) {
        super.installUI(c);
        Dimension d = c.getPreferredSize();
        d.height = getDefaultHeight();
        c.setPreferredSize(d);

        reconfigureActions(c);

        if (font != null) {
            Font current = c.getFont();

            if (current == null || current instanceof UIResource) {
                c.setFont(new FontUIResource(font));
            }
        }
    }

    public void uninstallUI(JComponent c) {
        super.uninstallUI(c);
    }

    public void paint(Graphics g, JComponent jc) {
        Titlebar titlebar = (Titlebar) jc;
        paintBackground(g, titlebar);
        paintIcon(g, titlebar);
        paintTitle(g, titlebar);
        paintBorder(g, titlebar);
    }

    protected void paintBackground(Graphics g, Titlebar titlebar) {
        Rectangle paintArea = getPaintRect(titlebar);
        g.translate(paintArea.x, paintArea.y);
        painter.paint(g, paintArea.width, paintArea.height, titlebar.isActive(), titlebar);
        g.translate(-paintArea.x, -paintArea.y);
    }

    protected Rectangle getPaintRect(Titlebar titlebar) {
        if (getInsets() == null)
            return new Rectangle(0, 0, titlebar.getWidth(), titlebar.getHeight());

        Insets paintInsets = getInsets();
        return new Rectangle(paintInsets.left, paintInsets.top,
                (titlebar.getWidth() - paintInsets.right - paintInsets.left), (titlebar.getHeight()
                        - paintInsets.bottom - paintInsets.top));
    }

    protected void paintTitle(Graphics g, Titlebar titlebar) {
        if (titlebar.getText() == null)
            return;

        Graphics2D g2 = (Graphics2D) g;
        Object oldAAValue = g2.getRenderingHint(RenderingHints.KEY_ANTIALIASING);
        g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, antialiasing);

        g2.setFont(titlebar.getFont());
        Rectangle iconRect = getIconRect(titlebar);
        Rectangle paintRect = getPaintRect(titlebar);

        int x = getTextLocation(iconRect);
        
        //Center text vertically.
	    FontMetrics fm = g.getFontMetrics();
        int y = (paintRect.height + fm.getAscent() - fm.getLeading() -
                    fm.getDescent()) / 2;
        
        Color c = getFontColor(titlebar.isActive());
        g2.setColor(c);
        g.translate(paintRect.x, paintRect.y);
        g2.drawString(titlebar.getText(), x, y);
        g.translate(-paintRect.x, -paintRect.y);
        

        g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, oldAAValue);
    }

    protected int getTextLocation(Rectangle iconRect) {
        if (iconRect.width > 0)
            return iconRect.x + iconRect.width + getRightIconMargin();
        return 5;
    }

    protected void paintIcon(Graphics g, Titlebar titlebar) {
        if (titlebar.getIcon() == null)
            return;

        Icon icon = titlebar.getIcon();
        Rectangle r = getIconRect(titlebar);

        Rectangle paintRect = getPaintRect(titlebar);
        g.translate(paintRect.x, paintRect.y);
        icon.paintIcon(titlebar, g, r.x, r.y);
        g.translate(-paintRect.x, -paintRect.y);
    }

    protected Rectangle getIconRect(Titlebar titlebar) {
        Icon icon = titlebar.getIcon();
        Rectangle r = new Rectangle(0, 0, 0, 0);
        if (icon == null)
            return r;

        Rectangle paintRect = getPaintRect(titlebar);

        r.x = getLeftIconMargin();
        r.width = icon.getIconWidth();
        r.height = icon.getIconHeight();
        r.y = (paintRect.height - r.height) / 2;
        return r;
    }

    protected int getLeftIconMargin() {
        return getIconInsets() == null ? 2 : getIconInsets().right;
    }

    protected int getRightIconMargin() {
        return getIconInsets() == null ? 2 : getIconInsets().right;
    }

    protected void paintBorder(Graphics g, Titlebar titlebar) {
        Border border = getBorder(titlebar);
        if (border != null) {
            Rectangle rectangle = getPaintRect(titlebar);
            g.translate(rectangle.x, rectangle.y);
            border.paintBorder(titlebar, g, 0, 0, rectangle.width, rectangle.height);
            g.translate(-rectangle.x, -rectangle.y);
        }
    }

    public void layoutComponents(Titlebar titlebar) {
        Rectangle rectangle = getPaintRect(titlebar);
        int margin = getButtonMargin();
        int h = rectangle.height - 2 * margin;
        int x = rectangle.width - margin - h;

        View view = titlebar.getView();
        Component[] c = titlebar.getComponents();
        for (int i = 0; i < c.length; i++) {
        	// start out with the preferred width
        	int width = c[i].getPreferredSize().width;
            if (c[i] instanceof Button) {
            	// org.flexdock.view.Buttons will be rendered as squares
            	width = h;
            	// don't show the button if its corresponding action is blocked
                if(view!=null && view.isActionBlocked(((Button) c[i]).getActionName())) {
                	c[i].setBounds(0, 0, 0, 0);
                	continue;
                }
            }
            // layout the component over to the right
            c[i].setBounds(x, margin + rectangle.y, width, h);
            // move x to the left for the next component
           	x -= width;
        }
    }

    public void configureAction(Action action) {
        if (action == null)
            return;

        IconResource icons = getIcons(action);
        if (icons != null) {
            action.putValue(ICON_RESOURCE, icons);
        }
    }

    private void reconfigureActions(JComponent c) {
        Component[] c1 = c.getComponents();
        for (int i = 0; i < c1.length; i++) {
            if (!(c1[i] instanceof Button))
                continue;
            Button b = (Button) c1[i];
            configureAction(b.getAction());
        }
    }

    protected Color getFontColor(boolean active) {
        Color c = active ? activeFont : inactiveFont;
        return c == null ? inactiveFont : c;
    }

    protected Color getBackgroundColor(boolean active) {
        Color color = active ? activeBackground : inactiveBackground;
        return color == null ? inactiveBackground : color;
    }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -