pvpanel.java
来自「导出ORACLE数据库对象DDL语句的程序」· Java 代码 · 共 460 行
JAVA
460 行
package com.icbcsdc.ddlexp.ui.model;
import java.awt.AWTEvent;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Insets;
import java.awt.Rectangle;
import javax.swing.JFrame;
import javax.swing.border.Border;
import javax.swing.border.EtchedBorder;
//import pv.util.*;
/**
The PVPanel provides capabilities for drawing a custom border, shadow and additional
gaps between the border and the component.
<br>This class extends the JScrollPane and supports all of its
methods and properties.
<br><H3>Features:</H3>
<UL>
<LI>Border: swing or PVBorder class
<LI>Border color
<LI>Shadow: none/5 styles with any width
<LI>Shadow color
<LI>Extra gaps between JPVTree and its container with custom paintings
</UL>
It is designed for the JPVTreeView and JPVTableView;
though, it can be used with any other component as well.
@version 6.00 07/06/01
@author Viktor Snezhko
*/
public class PVPanel extends javax.swing.JScrollPane
{
/**
Border color.
@serial
*/
protected Color m_clrB;
/**
Shadow color.
@serial
*/
protected Color m_clrS = Color.gray;
/**
Border style.
@serial
*/
protected int m_iBodr = -1;
//--------------------------
// 0x00000007 - shadow style
// 0x000000f8 - shadow width
// 0x00003f00 - gap on the top
// 0x000fc000 - gap on the left
// 0x03f00000 - gap on the bottom
// 0x3c000000 - gap on the right
// 0x40000000 - preferred size is set
// 0x80000000 - table
/**
Internal flags.
@serial
*/
int m_0 = 0x00000028;
/**
Background image.
<br>It is painted only if it is an instance of Icon or Image
class and isOpaque() is false.
<br><I>Examples:</I>
<OL>
<LI>pv.jfcx.JPVTreeView pvTree1 = new pv.jfcx.JPVTreeView();<br>
pvTree1.setOpaque(false);<br>
pvTree1.m_image = new Icon()<br>
{
<UL>
public void paintIcon(Component c, Graphics g, int x, int y)<br>
{
<UL>
Rectangle r = c.getBounds();<br>
((pv.jfcx.PVPanel)c).adjustRect(r, false);<br>
g.setColor(Color.red);<br>
g.fillOval(x, y, r.width, r.height);
</UL>
}<br>
public int getIconWidth(){return 0;}//dummy<br>
public int getIconHeight(){return 0;}//dummy<br>
</UL>
};
<LI>pv.jfcx.JPVTreeView pvTree1 = new pv.jfcx.JPVTreeView();<br>
// assume that the "this" is instance of Applet or JApplet<br>
pvTree1.setOpaque(false);<br>
pvTree1.m_image = pv.util.PVImage.file(this, "imageFile.gif"));
</OL>
@serial
*/
public Object m_image;
/**
Constructor.
*/
public PVPanel(){ this(null); }
/**
Constructor.
@param c component that is set to the viewport of the scroll-pane.
*/
public PVPanel(Component c){ super(c); }
/**
Set preferred size.
@param d preferred size.
@see #getPreferredSize
*/
public void setPreferredSize(Dimension d)
{ m_0 |= 0x40000000; if(d == null) m_0 &= 0xbfffffff; super.setPreferredSize(d); }
/**
Get preferred size.
<br><I>Notes:</I>
<UL>
<LI>If the "setPreferredSize" was not set, then the "getSize" is used.
<LI>If width is less than 2, then the value 200 is used.
<LI>If height is less than 2, then the value 200 is used.
</UL>
@see #setPreferredSize
*/
public Dimension getPreferredSize()
{
//-----------
// 0x40000000 - preferred size is set
Dimension s = new Dimension(((m_0 & 0x40000000) == 0) ? getSize() : super.getPreferredSize());
if(s.width < 2) s.width = 200;
if(s.height < 2) s.height = 200;
return s;
}
/**
Adjust rectangle for Shadow, Border/BorderStyle, and Gaps.
<br>It is similar to "adjustRect(r, true)".
@param r bounds of PVPanel.
*/
public void adjustRect(Rectangle r){ adjustRect(r, true); }
/**
Calculate client area.
@param r bounds of PVPanel.
@param gap True: including gaps, false: only border and shadow.
*/
public void adjustRect(Rectangle r, boolean gap)
{
int i = getShadow();
PVBorder.shadow(null, r, null, null, i, getShadowWidth());
if((i = getBorderStyle()) == -1)
{
Border b = getBorder();
if(b != null)
{
i = b.getBorderInsets(this).left;
r.x += i; r.y += i; r.width -= (i += i); r.height -= i;
}
}
else if(i != 0) PVBorder.draw(null, r, i, null);
if(gap)
{
r.y += (i = getGap(0)); r.height -= i;
r.x += (i = getGap(1)); r.width -= i;
r.height -= getGap(2);
r.width -= getGap(3);
}
}
/**
Adjust insets for Shadow, Border/BorderStyle, and Gaps.
*/
public Insets getInsets()
{
Rectangle r = new Rectangle(getSize());
Insets ins = new Insets(0, 0, r.height, r.width);
adjustRect(r, true);
ins.right -= (r.x + r.width);
ins.bottom -= (r.y + r.height);
ins.top = r.y; ins.left = r.x;
return ins;
}
/**
Set values of gaps that are painted between component and border.
<I>Notes:</I>
<OL>
<LI>The method "paintGap" can be used for custom painting.
<LI><I>Example of use.</I><br>
The JTree starts to paint nodes from the very top-left corner.
To shift the whole tree painting to the right and down by 3 pixels,-
the following can be used:<br>
pvPanel1.setGaps(new Insets(3, 3, 0, 0));
</OL>
@param gaps spaces. The null is equivalent to the "new Insets(0,0,0,0)".
<UL>
<LI>"gaps.top" - height of the gap at the top. Range: 0..63.
<LI>"gaps.left" - width of the gap on the left. Range: 0..63.
<LI>"gaps.bottom" - height of the gap at the bottom. Range: 0..63.
<LI>"gaps.right" - width of gap on the right. Range: 0..15.
</UL>
@see #getGaps
@see #getGap
@see #paintGap
*/
public void setGaps(Insets gaps)
{
//-----------------------------
// 0x00003f00 - gap on the top
// 0x000fc000 - gap on the left
// 0x03f00000 - gap on the bottom
// 0x3c000000 - gap on the right
m_0 &= 0xc00000ff;
if(gaps != null) m_0 |= (((gaps.top & 0x3f) << 8) | ((gaps.left & 0x3f) << 14) |
((gaps.bottom & 0x3f) << 20) | ((gaps.right & 0xf) << 26));
doLayout();
}
/**
Get values of gaps that are painted between the component and border.
@return Gaps between the inner edges of the border and the bounds
of the component.
@see #setGaps
*/
public Insets getGaps(){ return new Insets(getGap(0), getGap(1), getGap(2), getGap(3)); }
/**
Get value of the gap for a particular side.
@param side position of a gap. Possible values:
<UL>
0 - top<br>
1 - left<br>
2 - bottom<br>
3 - right
</UL>
*/
//-----------------------------
// 0x00003f00 - gap on the top
// 0x000fc000 - gap on the left
// 0x03f00000 - gap on the bottom
// 0x3c000000 - gap on the right
public int getGap(int side){ return (m_0 >> (8 + side * 6)) & ((side == 3) ? 0xf : 0x3f); }
/**
Set background color to null.
(fix a misbehavior within the Metal LookAndFeel).
*/
public void updateUI()
{
super.updateUI();
enableEvents(AWTEvent.MOUSE_EVENT_MASK | AWTEvent.MOUSE_MOTION_EVENT_MASK);
setBackground(null);
}
/**
Draw shadow, border and gaps.
*/
protected void paintBorder(Graphics g)
{
try
{
Rectangle r = new Rectangle(getSize());
int i = getShadow();
//--------------------------------------
// draw shadow
PVBorder.shadow(g, r, getShadowColor(), (getParent() == null) ? Color.lightGray : getParent().getBackground(), i, getShadowWidth());
if((i = getBorderStyle()) == -1)
{
Border b = getBorder();
if(b != null)
{
b.paintBorder(this, g, r.x, r.y, r.width, r.height);
i = b.getBorderInsets(this).left;
r.x += i; r.y += i; r.height -= (i += i); r.width -= i;
}
}
else if(i != 0) PVBorder.draw(g, r, i, getBorderColor());
if((i = getGap(0)) > 0)
{
paintGap(g, 0, r.x, r.y, r.width, i);
r.y += i;
r.height -= i;
}
if((i = getGap(1)) > 0)
{
paintGap(g, 1, r.x, r.y, i, r.height);
r.x += i;
r.width -= i;
}
if((i = getGap(2)) > 0) paintGap(g, 2, r.x, r.y + (r.height -= i), r.width, i);
if((i = getGap(3)) > 0) paintGap(g, 3, r.x + r.width - i, r.y, i, r.height);
}catch(Exception e){}
}
/**
Paint gap.
<br>Default implementation:
<UL>
<LI>g.setColor(getViewport().getView().getBackground());
<LI>g.fillRect(left, top, width, height);
</UL>
@param g graphics to paint.
@param side position of a gap. Possible values:
<UL>
0 - top<br>
1 - left<br>
2 - bottom<br>
3 - right
</UL>
@param left of rectangle.
@param top of rectangle.
@param width of rectangle.
@param height of rectangle.
*/
protected void paintGap(Graphics g, int side, int left, int top, int width, int height)
{
if(isOpaque()) try
{
g.setColor(getViewport().getView().getBackground());
g.fillRect(left, top, width, height);
}catch(Exception ex){}
}
/**
Get the style of the border.
@return Border style.
@see #setBorderStyle
*/
public int getBorderStyle(){ return m_iBodr; }
/**
Set the style of the border. Paint border using the PVBorder class.
<br><I>Note:</I> To disable BorderStyle and enable Border class,-
use "setBorderStyle(-1)".
@param style new border style. Constants of the PVBorder class
can be used.<br>
<I>Examples:</I>
<OL>
<LI>Border that resembles the frame of a picture:<br>
pvPanel1.setBorderStyle(pv.util.PVBorder.PICTURE);
<LI>The same as above with doubled width:<br>
pvPanel1.setBorderStyle(pv.util.PVBorder.PICTURE + 0x100);
<LI>Sunken #3 (standard Windows edit-field):<br>
pvPanel1.setBorderStyle(pv.util.PVBorder.SUNKEN + 2);
<LI>Raised #4<br>
pvPanel1.setBorderStyle(pv.util.PVBorder.RAISED + 3);
<LI>Advanced style (two symmetric dark lines devided by one light line):<br>
pvPanel1.setBorderStyle(025200252);
<LI>Set a border from the Border class:<br>
pvPanel1.setBorder(new BevelBorder(BevelBorder.RAISED));<br>
// if BorderStyle was used before, then an additional method is required:<br>
// pvPanel1.setBorderStyle(-1);
</OL>
Default value is set to -1.
@see #getBorderStyle
@see #setBorderColor
@see pv.util.PVBorder
*/
public void setBorderStyle(int style){ m_iBodr = style; doLayout(); }
/**
Get shadow style.
@return Shadow style.
@see #setShadow
*/
public int getShadow(){ return m_0 & 7; }
/**
Set shadow style.
@param style shadow style. Possible values:
<OL>
0 - no shadow;<br>
1 - shadow at the right-bottom;<br>
2 - shadow at the left-bottom;<br>
3 - shadow at the left-top;<br>
4 - shadow at the right-top.
</OL>
Default value is set to 0 (no shadow).
@see #getShadow
@see #setShadowWidth
@see #setShadowColor
*/
public void setShadow(int style){ m_0 = (m_0 & 0xfffffff8) | (style & 7); doLayout(); }
/**
Get shadow width.
@return Shadow width in pixels.
@see #setShadowWidth
*/
public int getShadowWidth(){ return (m_0 >> 3) & 0x1f; }
/**
Set shadow width.
<br><I>Note:</I> To enable shadow,- use "setShadow(1..4)".
@param w shadow width. Range: 0..31.<br>
Default value is set to 5.
@see #getShadowWidth
@see #setShadow
*/
public void setShadowWidth(int w){ m_0 = (m_0 & 0xffffff07) | ((w & 0x1f) << 3); doLayout(); }
/**
Get border color when the BorderStyle is used.
@return Color.
@see #setBorderColor
*/
public Color getBorderColor(){ return (m_clrB == null) ? getBackground() : m_clrB; }
/**
Set border color when the BorderStyle is used.
<br>The color of the border is not supported when the
Border class is used.
@param c border color. If it is null then the
background color is used.<br>
Default value is set to null.
@see #getBorderColor
@see #setBorderStyle
@see #setShadowColor
*/
public void setBorderColor(Color c){ m_clrB = c; repaint(); }
/**
Get shadow color.
@return Color.
@see #setShadowColor
*/
public Color getShadowColor(){ return (m_clrS == null) ? Color.gray : m_clrS; }
/**
Set shadow color.
@param c color of the shadow. If it is null, then
the gray color is used.<br>
Default value is set to the gray color.
@see #getShadowColor
@see #setBorderColor
*/
public void setShadowColor(Color c){ m_clrS = c; repaint(); }
public static void main(String[] args) {
JFrame f=new JFrame("PVPanel Test");
// f.setLayout(new FlowLayout());
PVPanel p1=new PVPanel();
PVPanel p2=new PVPanel();
PVPanel p3=new PVPanel();
PVPanel p4=new PVPanel();
//PVBorder border=new PVBorder();
//panel.setBorder(new BevelBorder(BevelBorder.RAISED));
p1.setBounds(1,1,100,100);
p1.setBorderColor(Color.blue);
p1.setBorderStyle(PVBorder.SUNKEN2+3);
p2.setBounds(110,10,100,100);
//p2.setBorderColor(Color.LIGHT_GRAY);
//p2.setBorderStyle(PVBorder.PICTURE);
EtchedBorder bd=new EtchedBorder(EtchedBorder.LOWERED);
p2.setBorder(bd);
p3.setBounds(110,110,100,100);
//p3.setBorderColor(Color.MAGENTA);
//p3.setBorderStyle(PVBorder.RAISED2+3);
p3.setBorderStyle(PVBorder.INDENT+3);
//p3.setBorderStyle(PVBorder.OUTDENT+5);
f.getContentPane().add(p1);
f.getContentPane().add(p2);
f.getContentPane().add(p3);
f.setSize(300, 300);
f.addWindowListener(new java.awt.event.WindowAdapter() {
public void windowClosing(java.awt.event.WindowEvent evt) {
System.exit(0);
}
});
f.setVisible(true);
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?