📄 xpinternalframetitlepane.java
字号:
// Beta
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* XP Look and Feel *
* *
* (C) Copyright 2002, by Stefan Krause, Taufik Romdhane and Contributors *
* *
* *
* The XP Look and Feel started as as extension to the Metouia Look and Feel. *
* The original header of this file was: *
** * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
** * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* Metouia Look And Feel: a free pluggable look and feel for java *
* http://mlf.sourceforge.net *
* (C) Copyright 2002, by Taoufik Romdhane and Contributors. *
* *
* This library is free software; you can redistribute it and/or modify it *
* under the terms of the GNU Lesser General Public License as published by *
* the Free Software Foundation; either version 2.1 of the License, or (at *
* your option) any later version. *
* *
* This library is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. *
* See the GNU Lesser General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License along *
* with this program; if not, write to the Free Software Foundation, Inc., *
* 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA. *
* *
* Original Author: Taoufik Romdhane *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
package com.stefankrause.xplookandfeel;
import java.awt.Color;
import java.awt.Component;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Event;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.LayoutManager;
import java.awt.event.ActionEvent;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.Action;
import javax.swing.Icon;
import javax.swing.JButton;
import javax.swing.JInternalFrame;
import javax.swing.UIManager;
import javax.swing.plaf.metal.MetalInternalFrameTitlePane;
import javax.swing.plaf.metal.MetalLookAndFeel;
import com.stefankrause.xplookandfeel.skin.Skin;
/**
* This class represents the title pane for the JInternalFrame components.
*
* @author Taoufik Romdhane
*/
public class XPInternalFrameTitlePane extends MetalInternalFrameTitlePane
implements LayoutManager
{
/**
* Color for the title in a normal sized internal frame
*/
Color normalTitleColor=Color.white;
/**
* Color for the shadow of the title in a normal sized internal frame
*/
Color shadowColor=new Color(10,24,131);
/**
* Color for the title in a normal sized internal frame that is not enabled
*/
Color disabledTitleColor=new Color(216,228,244);
static Skin skin;
/**
* The frame's title height, read from the UIDefaults table.
*/
protected int frameTitleHeight;
/**
* The buttons width, calculated at runtime.
*/
private int buttonsWidth;
/**
* Installs some default values.
* Reads the internalframe title height from the ui defaults table.
*/
protected void installDefaults()
{
super.installDefaults();
frameTitleHeight = UIManager.getInt("InternalFrame.frameTitleHeight");
}
/**
* This constructor creates a title pane for the given internal frame
* instance.
*
* @param frame The internal frame that needs a title pane.
*/
public XPInternalFrameTitlePane(JInternalFrame frame)
{
super(frame);
// if (XPInternalFrameUI.allowRoundedWindows)
// setOpaque(false);
}
protected void paintTitleBackground(Graphics g) {
return;
}
/**
* Paints this component.
*
* @param g The graphics context to use.
*/
public void paintComponent(Graphics g)
{
// if (isPalette)
// {
// paintPalette(g);
// return;
// }
boolean leftToRight = frame.getComponentOrientation().isLeftToRight();
boolean isSelected = frame.isSelected();
int width = getWidth();
int height = getHeight();
Color foreground = MetalLookAndFeel.getWindowTitleInactiveForeground();
getSkin().draw(g, isSelected ? 0 : 1, width, height);
int titleLength = 0;
int xOffset = leftToRight ? 2 : width - 2;
String frameTitle = frame.getTitle();
Icon icon = frame.getFrameIcon();
if (icon != null)
{
if (!leftToRight)
xOffset -= icon.getIconWidth();
int iconY = ((height / 2) - (icon.getIconHeight() / 2));
icon.paintIcon(frame, g, xOffset, iconY);
xOffset += leftToRight ? icon.getIconWidth() + 2 : -2;
}
if (frameTitle != null)
{
Font f = getFont();
g.setFont(f);
FontMetrics fm = g.getFontMetrics();
titleLength = fm.stringWidth(frameTitle);
if (isPalette) {
g.setColor(foreground);
int yOffset = ((height - fm.getHeight()) / 2) + fm.getAscent();
if (!leftToRight)
xOffset -= titleLength;
g.drawString(frameTitle, xOffset, yOffset);
xOffset += leftToRight ? titleLength + 2 : -2;
} else {
// a shadow effect for the titles in normal sized internal frames
int yOffset = ((height - fm.getHeight()) / 2) + fm.getAscent() +1;
if (!leftToRight)
xOffset -= titleLength;
if (isSelected) {
// for an active window
g.setColor( shadowColor );
g.drawString(frameTitle, xOffset+1, yOffset+1);
g.setColor( normalTitleColor);
g.drawString(frameTitle, xOffset, yOffset);
xOffset += leftToRight ? titleLength + 2 : -2;
} else {
// for an inactive window
g.setColor(disabledTitleColor );
g.drawString(frameTitle, xOffset, yOffset);
xOffset += leftToRight ? titleLength + 2 : -2;
}
}
}
}
/**
* Creates the layout manager for the title pane.
*
* @return The layout manager for the title pane.
*/
protected LayoutManager createLayout()
{
return this;
}
protected void addSubComponents() {
super.addSubComponents();
if (menuBar!=null) {
menuBar.setOpaque(false);
}
}
protected void setButtonIcons() {}
/**
* This listener is added to the maximize, minimize and close button to
* manage the rollover status of the buttons
*
*/
class RolloverListener implements MouseListener
{
JButton button;
Action action;
public RolloverListener(JButton b,Action a) {
button=b;
action=a;
}
public void mouseClicked(MouseEvent e) {
action.actionPerformed( new ActionEvent(this,Event.ACTION_EVENT,button.getText()) );
}
public void mousePressed(MouseEvent e) {
}
public void mouseReleased(MouseEvent e) {
}
public void mouseEntered(MouseEvent e) {
button.getModel().setRollover(true);
if (!button.isEnabled())
{
button.setEnabled(true);
}
button.repaint();
}
public void mouseExited(MouseEvent e) {
button.getModel().setRollover(false);
if (!frame.isSelected())
{
button.setEnabled(false);
}
button.repaint();
}
}
static XPWindowButtonUI iconButtonUI;
static XPWindowButtonUI maxButtonUI;
static XPWindowButtonUI closeButtonUI;
/**
* Creates the buttons of the title pane and initilizes their actions.
*/
protected void createButtons()
{
// Boolean paintActive = frame.isSelected() ? Boolean.TRUE:Boolean.FALSE;
if (iconButtonUI==null)
{
iconButtonUI=XPWindowButtonUI.createButtonUIForType(XPWindowButtonUI.MINIMIZE);
maxButtonUI=XPWindowButtonUI.createButtonUIForType(XPWindowButtonUI.MAXIMIZE);
closeButtonUI=XPWindowButtonUI.createButtonUIForType(XPWindowButtonUI.CLOSE);
}
iconButton = new SpecialUIButton(iconButtonUI);
iconButton.addActionListener(iconifyAction);
iconButton.setRolloverEnabled(true);
iconButton.addMouseListener(new RolloverListener(iconButton,iconifyAction));
maxButton = new SpecialUIButton(maxButtonUI);
maxButton.addActionListener(maximizeAction);
maxButton.setRolloverEnabled(true);
maxButton.addMouseListener(new RolloverListener(maxButton,maximizeAction));
closeButton = new SpecialUIButton(closeButtonUI);
closeButton.addActionListener(closeAction);
closeButton.setRolloverEnabled(true);
closeButton.addMouseListener(new RolloverListener(closeButton,closeAction));
iconButton.getAccessibleContext().setAccessibleName(
UIManager.getString(
"InternalFrameTitlePane.iconifyButtonAccessibleName"));
maxButton.getAccessibleContext().setAccessibleName(
UIManager.getString(
"InternalFrameTitlePane.maximizeButtonAccessibleName"));
closeButton.getAccessibleContext().setAccessibleName(
UIManager.getString(
"InternalFrameTitlePane.closeButtonAccessibleName"));
if (frame.isSelected()) {
activate();
} else {
deactivate();
}
}
/**
* Paints the title pane for a palette.
*
* @param g The graphics context to use.
*/
public void paintPalette(Graphics g)
{
return;
}
/**
* Adds the specified component with the specified name to the layout.
*
* @param name the component name
* @param c the component to be added
*/
public void addLayoutComponent(String name, Component c)
{
}
/**
* Removes the specified component from the layout.
*
* @param c the component to be removed
*/
public void removeLayoutComponent(Component c)
{
}
/**
* Calculates the preferred size dimensions for the specified
* panel given the components in the specified parent container.
*
* @param c the component to be laid out
*/
public Dimension preferredLayoutSize(Container c)
{
return getPreferredSize(c);
}
/**
* Gets the preferred size of the given container.
*
* @param c The container to gets its preferred size.
* @return The preferred size of the given container.
*/
public Dimension getPreferredSize(Container c)
{
return
new Dimension(
c.getSize().width,
(isPalette ? paletteTitleHeight : frameTitleHeight));
}
/**
* The minimum size of the frame.
* This is used, for example, during resizing to
* find the minimum allowable size.
* Providing at least some minimum size fixes a bug
* which breaks horizontal resizing.
* <b>Note</b>: the Motif plaf allows for a 0,0 min size,
* but we provide a reasonable minimum here.
* <b>Future</b>: calculate min size based upon contents.
*/
public Dimension getMinimumSize() {
return new Dimension(70,25);
}
/**
* Calculates the minimum size dimensions for the specified
* panel given the components in the specified parent container.
*
* @param c the component to be laid out
*/
public Dimension minimumLayoutSize(Container c)
{
return preferredLayoutSize(c);
}
/**
* Lays out the container in the specified panel.
*
* @param c the component which needs to be laid out
*/
public void layoutContainer(Container c)
{
boolean leftToRight = frame.getComponentOrientation().isLeftToRight();
int buttonHeight = closeButton.getPreferredSize().height;
int w = getWidth();
int x = leftToRight ? w : 0;
int y = (getHeight()-buttonHeight)/2+1;
int spacing;
// assumes all buttons have the same dimensions
// these dimensions include the borders
int buttonWidth = 18; //closeButton.getIcon().getIconWidth();
if (frame.isClosable())
{
if (isPalette)
{
spacing = 2;
x += leftToRight ? -spacing - (buttonWidth) : spacing;
closeButton.setBounds(x, y, buttonWidth, buttonHeight);
if (!leftToRight)
x += (buttonWidth);
}
else
{
spacing = 2;
x += leftToRight ? -spacing - buttonWidth : spacing;
closeButton.setBounds(x, y, buttonWidth, buttonHeight);
if (!leftToRight)
x += buttonWidth;
}
}
if (frame.isMaximizable() && !isPalette)
{
spacing = 2; //frame.isClosable() ? 2;
x += leftToRight ? -spacing - buttonWidth : spacing;
maxButton.setBounds(x, y, buttonWidth, buttonHeight);
if (!leftToRight)
x += buttonWidth;
}
if (frame.isIconifiable() && !isPalette)
{
spacing = 2; //frame.isMaximizable() ? 0 : (frame.isClosable() ? 0 : 2);
x += leftToRight ? -spacing - buttonWidth : spacing;
iconButton.setBounds(x, y, buttonWidth, buttonHeight);
if (!leftToRight)
x += buttonWidth;
}
buttonsWidth = leftToRight ? w - x : x;
}
public void activate() {
closeButton.setEnabled(true);
iconButton.setEnabled(true);
maxButton.setEnabled(true);
}
public void deactivate() {
closeButton.setEnabled(false);
iconButton.setEnabled(false);
maxButton.setEnabled(false);
}
/**
* @see java.awt.Component#getFont()
*/
public Font getFont() {
return isPalette ? super.getFont() : UIManager.getFont("InternalFrame.titleFont");
}
public Skin getSkin()
{
if (skin == null) {
skin = new Skin("XPFrameCaption.res", 2, 5,5, 5,3);
}
return skin;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -