📄 basetabbedpanel.java
字号:
return;
}
int col = TF_LEFT;
int row = 0;
if (iTabsPosition == TOP) {
row = TF_TOP;
} else {
row = TF_TOP - TF_BTN_HEIGHT;
}
if (userPanel != null) {
userPanel.setBounds(col + 3, row + 3, width - 6, height - 5);
userPanel.invalidate();
userPanel.validate();
if (userPanel instanceof Canvas || userPanel instanceof Panel) {
userPanel.repaint();
} else {
triggerRepaint();
}
}
}
public void paint(Graphics g) {
if (imageBuffer == null) {
imageBuffer = createImage(getSize().width, getSize().height);
graphicsImage = imageBuffer.getGraphics();
}
Rectangle r = getBounds();
//Make sure cached colors are correct.
Color curForeground = getForeground();
Color curBackground = getBackground();
if (curForeground != cachedForeground) {//MstUtil.objectsEqual(curForeground, cachedForeground)) {
cachedForeground = curForeground;
calculateDisabledTextColor(curForeground);
}
if (curBackground != cachedBackground) {//!MstUtil.objectsEqual(curBackground, cachedBackground)) {
cachedBackground = curBackground;
calculateBorderColors(curBackground);
}
// --------------------------------------------------------------------------------
// paint the box
// --------------------------------------------------------------------------------
int width = r.width - TF_LEFT + TF_RIGHT;
if (width < 0) {
return;
}
int height = r.height - TF_TOP + TF_BOTTOM;
if (height < 0) {
return;
}
if (r.width > lastWidth) {
firstVisibleTab = 0;
}
lastWidth = r.width;
int col = TF_LEFT;
int row;
Color c = graphicsImage.getColor();
graphicsImage.setColor(curBackground);
graphicsImage.fillRect(0, 0, r.width, r.height);
if (iTabsPosition == TOP) {
row = TF_TOP;
} else {
row = TF_TOP - TF_BTN_HEIGHT;
}
// --------------------------------------------------------------------------------
// draw border
// --------------------------------------------------------------------------------
graphicsImage.setColor(borderLightColor);
graphicsImage.drawLine(col, row, (col + width - 1), row);
graphicsImage.drawLine(col, row, col, (row + height - 1));
graphicsImage.setColor(borderDarkColor);
graphicsImage.drawLine((col + 2), (row + height - 2), (col + width - 2), (row + height - 2));
graphicsImage.drawLine((col + width - 2), (row + 2), (col + width - 2), (row + height - 2));
graphicsImage.setColor(borderDarkerColor);
graphicsImage.drawLine((col + 1), (row + height - 1), (col + width - 1), (row + height - 1));
graphicsImage.drawLine((col + width - 1), (row + 1), (col + width - 1), (row + height - 1));
// --------------------------------------------------------------------------------
// paint the tabs, and record areas
// --------------------------------------------------------------------------------
int x1;
int x2 = TF_LEFT + 8;
int y1;
int y2;
int x3 = 0;
int x4 = TF_LEFT;
int sze = vLabels.size();
String sLabel;
vPolys.removeAllElements();
Font f = graphicsImage.getFont();
//make sure the font size get changed
int fontSize = f.getSize();
if (fontSize != fReg.getSize()) {
fReg = new Font(fReg.getName(), fReg.getStyle(), fontSize);
}
if (fontSize != fSel.getSize()) {
fSel = new Font(fSel.getName(), fSel.getStyle(), fontSize);
}
FontMetrics fm = getFontMetrics(fReg);
FontMetrics fms = getFontMetrics(fSel);
int labelWidth = 0;
Polygon p;
int w;
// make sure there is a polygon for each tab
for (w = 0; w < firstVisibleTab; w++) {
vPolys.addElement(nullPoly);
}
if (w > 0) {
x4 += 2;
}
for (; w < sze; w++) {
p = new Polygon();
try {
sLabel = (String) vLabels.elementAt(w);
if (w == curIndex) {
labelWidth = fms.stringWidth(sLabel);
} else {
labelWidth = fm.stringWidth(sLabel);
}
if (iTabsPosition == TOP) {
y1 = TF_TOP - TF_BTN_HEIGHT;
y2 = TF_TOP - 1;
} else {
y1 = r.height + TF_BOTTOM + 1;
y2 = r.height + TF_BOTTOM - TF_BTN_HEIGHT;
}
if (iTabsStyle == ROUNDED) {
x1 = x4 + 2;
x2 = x1 + labelWidth + 13;
} else {
x1 = x2 - 7;
x2 = x1 + labelWidth + 28;
}
// check to see if this tab would draw too far
if ((x2 + 36 - TF_RIGHT) > r.width) {
break;
}
// draw the outside edge of the tab
if (iTabsPosition == TOP) {
// if current tab, it extends further
if (w == curIndex) {
y1 -= 3;
x1 -= 2;
}
graphicsImage.setColor(borderLightColor);
if (curIndex == (w + 1)) {
graphicsImage.drawLine(x1 + 2, y1, x2 - 2, y1);
} else {
graphicsImage.drawLine(x1 + 2, y1, x2, y1);
}
// draw the border between tabs if not covered by the current one
if (curIndex != (w - 1)) {
graphicsImage.drawLine(x1, y1 + 2, x1, y2);
x3 = x1;
} else {
x3 = x1 + 1;
}
graphicsImage.drawLine(x1 + 1, y1 + 1, x1 + 1, y1 + 1);
if (curIndex != (w + 1)) {
graphicsImage.setColor(borderDarkColor);
graphicsImage.drawLine(x2, y1, x2, y2);
graphicsImage.setColor(borderDarkerColor);
graphicsImage.drawLine(x2 + 1, y1 + 2, x2 + 1, y2);
x4 = x2;
} else {
x4 = x2 - 1;
}
} else {
if (iTabsStyle == SQUARE) {
graphicsImage.setColor(borderDarkColor);
graphicsImage.drawLine(x1 + 9, y1, x2 - 9, y1);
graphicsImage.setColor(borderDarkerColor);
// left \ slanted line
if (w == 0 || w == curIndex) {
graphicsImage.drawLine(x1, y2, x1 + 9, y1);
p.addPoint(x1, y2);
} else {
graphicsImage.drawLine(x1 + 4, y1 - 9, x1 + 9, y1);
p.addPoint(x1 + 9, y2);
p.addPoint(x1 + 4, y1 - 9);
}
p.addPoint(x1 + 9, y1);
p.addPoint(x2 - 9, y1);
if ((w + 1) == curIndex) {
graphicsImage.drawLine(x2 - 5, y1 - 9, x2 - 9, y1);
p.addPoint(x2 - 5, y1);
p.addPoint(x2 - 9, y2);
} else {
graphicsImage.drawLine(x2, y2, x2 - 9, y1);
p.addPoint(x2, y2);
}
if (w == 1 || w == curIndex) {
p.addPoint(x1, y2);
} else {
p.addPoint(x1+9, y2);
}
} else {
// if current tab, it extends further
if (w == curIndex) {
y1 += 3;
x1 -= 2;
}
graphicsImage.setColor(borderLightColor);
if (curIndex == (w + 1)) {
graphicsImage.drawLine(x1 + 2, y1, x2 - 2, y1);
} else {
graphicsImage.drawLine(x1 + 2, y1, x2, y1);
}
// draw the border between tabs if not covered by the current one
if (curIndex != (w - 1)) {
graphicsImage.drawLine(x1, y1 - 2, x1, y2);
x3 = x1;
} else {
x3 = x1 + 1;
}
graphicsImage.drawLine(x1 + 1, y1 - 1, x1 + 1, y1 - 1);
if (curIndex != (w + 1)) {
graphicsImage.setColor(borderDarkColor);
graphicsImage.drawLine(x2, y1, x2, y2);
graphicsImage.setColor(borderDarkerColor);
graphicsImage.drawLine(x2 + 1, y1 - 2, x2 + 1, y2);
x4 = x2;
} else {
x4 = x2 - 1;
}
}
}
// draw the inside edge of the tab
if (w == curIndex) {
if (iTabsPosition == TOP) {
y2++;
} else {
y2--;
}
graphicsImage.setColor(curBackground);
graphicsImage.drawLine(x1 + 1, y2, x2, y2);
if (iTabsPosition == BOTTOM) {
graphicsImage.drawLine(x1 + 1, y2 - 1, x2, y2 - 1);
}
graphicsImage.setFont(fSel);
} else {
graphicsImage.setFont(fReg);
}
if (iTabsStyle == ROUNDED) {
p.addPoint(x3, y2);
p.addPoint(x4, y2);
p.addPoint(x4, y1);
p.addPoint(x3, y1);
p.addPoint(x3, y2);
}
vPolys.addElement(p);
Boolean bool = (Boolean)vEnabled.elementAt(w);
if (bool.booleanValue()) {
graphicsImage.setColor(curForeground);
} else {
graphicsImage.setColor(disabledTextColor);
}
if (iTabsPosition == TOP) {
graphicsImage.drawString(sLabel, x1 + 8, y1 + 15);
} else {
if (iTabsStyle == ROUNDED) {
graphicsImage.drawString(sLabel, x1 + 8, y1 - 6);
} else {
graphicsImage.drawString(sLabel, x1 + 14, y1 - 4);
}
}
} catch (ArrayIndexOutOfBoundsException e) {
}
}
// do I need to show arrows because there are too many tabs???
if ( (firstVisibleTab > 0) || (w < sze) ) {
arbLeft.setVisible(true);
arbRight.setVisible(true);
if (firstVisibleTab > 0) {
arbLeft.setEnabled(true);
} else {
arbLeft.setEnabled(false);
}
if (w < sze) {
arbRight.setEnabled(true);
} else {
arbRight.setEnabled(false);
}
if (iTabsPosition == TOP) {
arbLeft.setBounds (r.width - 33 + TF_RIGHT, TF_TOP - 16, 16, 15);
arbRight.setBounds(r.width - 16 + TF_RIGHT, TF_TOP - 16, 16, 15);
} else {
arbLeft.setBounds (r.width - 33 + TF_RIGHT, r.height + TF_BOTTOM - TF_BTN_HEIGHT, 16, 15);
arbRight.setBounds(r.width - 16 + TF_RIGHT, r.height + TF_BOTTOM - TF_BTN_HEIGHT, 16, 15);
}
} else {
arbLeft.setVisible(false);
arbRight.setVisible(false);
}
// make sure there is a polygon for each tab
for (; w < sze; w++) {
vPolys.addElement(nullPoly);
}
graphicsImage.setFont(f);
graphicsImage.setColor(c);
super.paint(graphicsImage);
g.drawImage(imageBuffer, 0, 0, this);
}
public void update(Graphics g) {
paint(g);
}
public Component add(Component comp) {
add(comp, -1);
return comp;
}
public synchronized Component add(Component comp, int pos) {
super.add(comp, pos);
return comp;
}
public synchronized Component add(String name, Component comp) {
return comp;
}
public synchronized void remove(Component comp) {
if (comp == arbLeft || comp == arbRight) {
return;
}
super.remove(comp);
if (comp == (Component) userPanel) {
userPanel = null;
}
}
public synchronized void removeAll() {
super.removeAll();
super.add(arbLeft, -1);
super.add(arbRight, -1);
userPanel = null;
}
public void setLayout(LayoutManager mgr) {
}
public Insets getInsets() {
btpInsets = super.getInsets();
btpInsets.left += (TF_LEFT + 3);
btpInsets.right += (6 - TF_RIGHT);
if (iTabsPosition == TOP) {
btpInsets.top += (TF_TOP + 3);
btpInsets.bottom += (5 - TF_BOTTOM);
} else {
btpInsets.top += TF_TOP - TF_BTN_HEIGHT + 3;
btpInsets.bottom += (TF_BTN_HEIGHT + 5 - TF_BOTTOM);
}
return btpInsets;
}
// public Dimension preferredSize() {
// return getPreferredSize();
// }
public Dimension getMinimumSize() {
if (userPanel != null) {
Dimension s = userPanel.getMinimumSize();
return new Dimension( (s.width + btpInsets.left + btpInsets.right),
(s.height + btpInsets.top + btpInsets.bottom) );
}
return new Dimension(100, 100);
}
// public Dimension minimumSize() {
// return getMinimumSize();
// }
public synchronized void addNotify() {
super.addNotify();
//Hook up listeners
if (mouseAdapter == null) {
mouseAdapter = new MyMouseAdapter();
addMouseListener(mouseAdapter);
}
if (actionAdapter == null) {
actionAdapter = new MyActionAdapter();
arbLeft.addActionListener(actionAdapter);
arbRight.addActionListener(actionAdapter);
}
}
public synchronized void removeNotify() {
//Unhook listeners
if (mouseAdapter != null) {
removeMouseListener(mouseAdapter);
mouseAdapter = null;
}
if (actionAdapter != null) {
arbLeft.removeActionListener(actionAdapter);
arbRight.removeActionListener(actionAdapter);
actionAdapter = null;
}
super.removeNotify();
}
class MyMouseAdapter extends MouseAdapter implements Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
public void mousePressed(MouseEvent e) {
int sizeR = vPolys.size();
Polygon p;
for (int x = 0; x < sizeR; x++) {
try {
p = (Polygon) vPolys.elementAt(x);
if ((p != nullPoly) && p.contains(e.getX(), e.getY())) {
try {
setCurrentTab(x);
} catch (java.beans.PropertyVetoException ee) {
}
}
} catch (ArrayIndexOutOfBoundsException exc) {
}
}
}
}
class MyActionAdapter implements ActionListener, Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
public void actionPerformed(ActionEvent e) {
if (e.getSource() == arbLeft) {
if (--firstVisibleTab < 0) {
firstVisibleTab = 0;
} else {
triggerRepaint();
}
} else if (e.getSource() == arbRight) {
int sze = vLabels.size();
if (++firstVisibleTab == sze) {
firstVisibleTab--;
} else {
triggerRepaint();
}
}
}
}
protected void calculateBorderColors(Color c) {
borderLightColor = c.brighter();//MstUtil.calculateHilightColor(c);
borderDarkColor = c.darker();//MstUtil.calculateShadowColor(c);
borderDarkerColor = borderDarkColor.darker();//MstUtil.darken(borderDarkColor, 0.200);
}
protected void calculateDisabledTextColor(Color c) {
try {
disabledTextColor = c.darker();//MstUtil.fade(c, Color.lightGray, 0.50);
} catch (IllegalArgumentException exc) {
}
}
public synchronized void addPropertyChangeListener(PropertyChangeListener listener) {
changes.addPropertyChangeListener(listener);
}
public synchronized void removePropertyChangeListener(PropertyChangeListener listener) {
changes.removePropertyChangeListener(listener);
}
public synchronized void addVetoableChangeListener(VetoableChangeListener listener) {
vetos.addVetoableChangeListener(listener);
}
public synchronized void removeVetoableChangeListener(VetoableChangeListener listener) {
vetos.removeVetoableChangeListener(listener);
}
public synchronized void addCurrentTabListener(PropertyChangeListener listener) {
changes.addPropertyChangeListener(listener);
}
public synchronized void removeCurrentTabListener(PropertyChangeListener listener) {
changes.removePropertyChangeListener(listener);
}
public synchronized void addCurrentTabListener(VetoableChangeListener listener) {
vetos.addVetoableChangeListener(listener);
}
public synchronized void removeCurrentTabListener(VetoableChangeListener listener) {
vetos.removeVetoableChangeListener(listener);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -