📄 panelwindowcontainer.java
字号:
color3 = MetalLookAndFeel.getControl(); } else { color1 = color2 = color3 = null; } } //}}} } //}}} //{{{ RotatedTextIcon class public static class RotatedTextIcon implements Icon { public static final int NONE = 0; public static final int CW = 1; public static final int CCW = 2; //{{{ RotatedTextIcon constructor public RotatedTextIcon(int rotate, Font font, String text) { this.rotate = rotate; this.font = font; FontRenderContext fontRenderContext = new FontRenderContext(null,true,true); this.text = text; glyphs = font.createGlyphVector(fontRenderContext,text); width = (int)glyphs.getLogicalBounds().getWidth() + 4; //height = (int)glyphs.getLogicalBounds().getHeight(); LineMetrics lineMetrics = font.getLineMetrics(text,fontRenderContext); ascent = lineMetrics.getAscent(); height = (int)lineMetrics.getHeight(); renderHints = new RenderingHints( RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); renderHints.put(RenderingHints.KEY_FRACTIONALMETRICS, RenderingHints.VALUE_FRACTIONALMETRICS_ON); renderHints.put(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY); } //}}} //{{{ getIconWidth() method public int getIconWidth() { return (int)(rotate == RotatedTextIcon.CW || rotate == RotatedTextIcon.CCW ? height : width); } //}}} //{{{ getIconHeight() method public int getIconHeight() { return (int)(rotate == RotatedTextIcon.CW || rotate == RotatedTextIcon.CCW ? width : height); } //}}} //{{{ paintIcon() method public void paintIcon(Component c, Graphics g, int x, int y) { Graphics2D g2d = (Graphics2D)g; g2d.setFont(font); AffineTransform oldTransform = g2d.getTransform(); RenderingHints oldHints = g2d.getRenderingHints(); g2d.setRenderingHints(renderHints); g2d.setColor(c.getForeground()); //{{{ No rotation if(rotate == RotatedTextIcon.NONE) { g2d.drawGlyphVector(glyphs,x + 2,y + ascent); } //}}} //{{{ Clockwise rotation else if(rotate == RotatedTextIcon.CW) { AffineTransform trans = new AffineTransform(); trans.concatenate(oldTransform); trans.translate(x,y + 2); trans.rotate(Math.PI / 2, height / 2, width / 2); g2d.setTransform(trans); g2d.drawGlyphVector(glyphs,(height - width) / 2, (width - height) / 2 + ascent); } //}}} //{{{ Counterclockwise rotation else if(rotate == RotatedTextIcon.CCW) { AffineTransform trans = new AffineTransform(); trans.concatenate(oldTransform); trans.translate(x,y - 2); trans.rotate(Math.PI * 3 / 2, height / 2, width / 2); g2d.setTransform(trans); g2d.drawGlyphVector(glyphs,(height - width) / 2, (width - height) / 2 + ascent); } //}}} g2d.setTransform(oldTransform); g2d.setRenderingHints(oldHints); } //}}} //{{{ Private members private int rotate; private Font font; private String text; private GlyphVector glyphs; private float width; private float height; private float ascent; private RenderingHints renderHints; //}}} } //}}} //{{{ ButtonLayout class class ButtonLayout implements LayoutManager { //{{{ addLayoutComponent() method public void addLayoutComponent(String name, Component comp) {} //}}} //{{{ removeLayoutComponent() method public void removeLayoutComponent(Component comp) {} //}}} //{{{ getWrappedDimension() method /** * Returns the width or height of wrapped rows or columns. */ int getWrappedDimension(JComponent parent, int dimension) { Insets insets = ((JComponent)parent).getBorder() .getBorderInsets((JComponent)parent); Component[] comp = parent.getComponents(); if(comp.length <= 2) return 0; Dimension dim = comp[2].getPreferredSize(); if(position.equals(DockableWindowManager.TOP) || position.equals(DockableWindowManager.BOTTOM)) { int width = dimension - insets.right; int rowHeight = Math.max(dim.height,closeBox.getPreferredSize().width); int x = rowHeight * 2 + insets.left; Dimension returnValue = new Dimension(0,rowHeight + insets.top + insets.bottom); for(int i = 2; i < comp.length; i++) { int btnWidth = comp[i].getPreferredSize().width; if(btnWidth + x > width) { returnValue.height += rowHeight; x = insets.left; } x += btnWidth; } return returnValue.height; } else { int height = dimension - insets.bottom; int colWidth = Math.max(dim.width,closeBox.getPreferredSize().height); int y = colWidth * 2 + insets.top; Dimension returnValue = new Dimension(colWidth + insets.left + insets.right,0); for(int i = 2; i < comp.length; i++) { int btnHeight = comp[i].getPreferredSize().height; if(btnHeight + y > height) { returnValue.width += colWidth; y = insets.top; } y += btnHeight; } return returnValue.width; } } //}}} //{{{ preferredLayoutSize() method public Dimension preferredLayoutSize(Container parent) { Insets insets = ((JComponent)parent).getBorder() .getBorderInsets((JComponent)parent); Component[] comp = parent.getComponents(); if(comp.length <= 2) { // nothing 'cept close box return new Dimension(0,0); } Dimension dim = comp[2].getPreferredSize(); if(position.equals(DockableWindowManager.TOP) || position.equals(DockableWindowManager.BOTTOM)) { int width = parent.getWidth() - insets.right; int rowHeight = Math.max(dim.height,closeBox.getPreferredSize().width); int x = rowHeight * 2 + insets.left; Dimension returnValue = new Dimension(0,rowHeight + insets.top + insets.bottom); for(int i = 2; i < comp.length; i++) { int btnWidth = comp[i].getPreferredSize().width; if(btnWidth + x > width) { returnValue.height += rowHeight; x = insets.left; } x += btnWidth; } return returnValue; } else { int height = parent.getHeight() - insets.bottom; int colWidth = Math.max(dim.width,closeBox.getPreferredSize().height); int y = colWidth * 2 + insets.top; Dimension returnValue = new Dimension(colWidth + insets.left + insets.right,0); for(int i = 2; i < comp.length; i++) { int btnHeight = comp[i].getPreferredSize().height; if(btnHeight + y > height) { returnValue.width += colWidth; y = insets.top; } y += btnHeight; } return returnValue; } } //}}} //{{{ minimumLayoutSize() method public Dimension minimumLayoutSize(Container parent) { return preferredLayoutSize(parent); } //}}} //{{{ layoutContainer() method public void layoutContainer(Container parent) { Insets insets = ((JComponent)parent).getBorder() .getBorderInsets((JComponent)parent); Component[] comp = parent.getComponents(); if(comp.length <= 2) { for(int i = 0; i < comp.length; i++) { comp[i].setVisible(false); } return; } comp[0].setVisible(true); comp[1].setVisible(true); Dimension dim = comp[2].getPreferredSize(); if(position.equals(DockableWindowManager.TOP) || position.equals(DockableWindowManager.BOTTOM)) { int width = parent.getWidth() - insets.right; int rowHeight = Math.max(dim.height,closeBox.getPreferredSize().width); int x = rowHeight * 2 + insets.left; int y = insets.top; closeBox.setBounds(insets.left,insets.top,rowHeight,rowHeight); menuBtn.setBounds(insets.left + rowHeight,insets.top,rowHeight,rowHeight); for(int i = 2; i < comp.length; i++) { int btnWidth = comp[i].getPreferredSize().width; if(btnWidth + x > width) { x = insets.left; y += rowHeight; } comp[i].setBounds(x,y,btnWidth,rowHeight); x += btnWidth; } /* if(y + rowHeight != parent.getHeight()) { parent.setSize( parent.getWidth(), y + rowHeight); ((JComponent)parent).revalidate(); } */ } else { int height = parent.getHeight() - insets.bottom; int colWidth = Math.max(dim.width,closeBox.getPreferredSize().height); int x = insets.left; int y = colWidth * 2 + insets.top; closeBox.setBounds(insets.left,insets.top,colWidth,colWidth); menuBtn.setBounds(insets.left,insets.top + colWidth,colWidth,colWidth); for(int i = 2; i < comp.length; i++) { int btnHeight = comp[i].getPreferredSize().height; if(btnHeight + y > height) { x += colWidth; y = insets.top; } comp[i].setBounds(x,y,colWidth,btnHeight); y += btnHeight; } /* if(x + colWidth != parent.getWidth()) { parent.setSize(x + colWidth, parent.getHeight()); ((JComponent)parent).revalidate(); } */ } } //}}} } //}}} //{{{ DockablePanel class class DockablePanel extends JPanel { //{{{ DockablePanel constructor DockablePanel() { super(new CardLayout()); ResizeMouseHandler resizeMouseHandler = new ResizeMouseHandler(); addMouseListener(resizeMouseHandler); addMouseMotionListener(resizeMouseHandler); } //}}} //{{{ getWindowContainer() method PanelWindowContainer getWindowContainer() { return PanelWindowContainer.this; } //}}} //{{{ showDockable() method void showDockable(String name) { ((CardLayout)getLayout()).show(this,name); } //}}} //{{{ getMinimumSize() method public Dimension getMinimumSize() { return new Dimension(0,0); } //}}} //{{{ getPreferredSize() method public Dimension getPreferredSize() { if(current == null) return new Dimension(0,0); else { if(position.equals(DockableWindowManager.TOP) || position.equals(DockableWindowManager.BOTTOM)) { if(dimension <= 0) { int height = super.getPreferredSize().height; dimension = height - SPLITTER_WIDTH; } return new Dimension(0, dimension + SPLITTER_WIDTH); } else { if(dimension <= 0) { int width = super.getPreferredSize().width; dimension = width - SPLITTER_WIDTH; } return new Dimension(dimension + SPLITTER_WIDTH, 0); } } } //}}} //{{{ setBounds() method public void setBounds(int x, int y, int width, int height) { if(position.equals(DockableWindowManager.TOP) || position.equals(DockableWindowManager.BOTTOM)) { if(dimension != 0 && height <= SPLITTER_WIDTH) PanelWindowContainer.this.show(null); else dimension = height - SPLITTER_WIDTH; } else { if(dimension != 0 && width <= SPLITTER_WIDTH) PanelWindowContainer.this.show(null); else dimension = width - SPLITTER_WIDTH; } super.setBounds(x,y,width,height); } //}}} //{{{ ResizeMouseHandler class class ResizeMouseHandler extends MouseAdapter implements MouseMotionListener { boolean canDrag; Point dragStart; //{{{ mousePressed() method public void mousePressed(MouseEvent evt) { if(canDrag) { wm.setResizePos(dimension,PanelWindowContainer.this); dragStart = evt.getPoint(); } } //}}} //{{{ mouseReleased() method public void mouseReleased(MouseEvent evt) { if(canDrag) { dimension = wm.resizePos; wm.finishResizing(); dragStart = null; wm.revalidate(); } } //}}} //{{{ mouseMoved() method public void mouseMoved(MouseEvent evt) { Border border = getBorder(); if(border == null) { // collapsed return; } Insets insets = border.getBorderInsets(DockablePanel.this); canDrag = false; //{{{ Top... if(position.equals(DockableWindowManager.TOP)) { if(evt.getY() >= getHeight() - insets.bottom) canDrag = true; } //}}} //{{{ Left... else if(position.equals(DockableWindowManager.LEFT)) { if(evt.getX() >= getWidth() - insets.right) canDrag = true; } //}}} //{{{ Bottom... else if(position.equals(DockableWindowManager.BOTTOM)) { if(evt.getY() <= insets.top) canDrag = true; } //}}} //{{{ Right... else if(position.equals(DockableWindowManager.RIGHT)) { if(evt.getX() <= insets.left) canDrag = true; } //}}} if(canDrag) { wm.setCursor(Cursor.getPredefinedCursor( getAppropriateCursor())); } else { wm.setCursor(Cursor.getPredefinedCursor( Cursor.DEFAULT_CURSOR)); } } //}}} //{{{ mouseDragged() method public void mouseDragged(MouseEvent evt) { if(!canDrag) return; if(dragStart == null) // can't happen? return; wm.setCursor(Cursor.getPredefinedCursor( getAppropriateCursor())); //{{{ Top... if(position.equals(DockableWindowManager.TOP)) { wm.setResizePos( evt.getY() - dragStart.y + dimension, PanelWindowContainer.this); } //}}} //{{{ Left... else if(position.equals(DockableWindowManager.LEFT)) { wm.setResizePos(evt.getX() - dragStart.x + dimension, PanelWindowContainer.this); } //}}} //{{{ Bottom... else if(position.equals(DockableWindowManager.BOTTOM)) { wm.setResizePos(dimension - evt.getY() + dragStart.y, PanelWindowContainer.this); } //}}} //{{{ Right... else if(position.equals(DockableWindowManager.RIGHT)) { wm.setResizePos(dimension - evt.getX() + dragStart.x, PanelWindowContainer.this); } //}}} } //}}} //{{{ mouseExited() method public void mouseExited(MouseEvent evt) { wm.setCursor(Cursor.getPredefinedCursor( Cursor.DEFAULT_CURSOR)); } //}}} //{{{ getCursor() method private int getAppropriateCursor() { if(position.equals(DockableWindowManager.TOP)) return Cursor.N_RESIZE_CURSOR; else if(position.equals(DockableWindowManager.LEFT)) return Cursor.W_RESIZE_CURSOR; else if(position.equals(DockableWindowManager.BOTTOM)) return Cursor.S_RESIZE_CURSOR; else if(position.equals(DockableWindowManager.RIGHT)) return Cursor.E_RESIZE_CURSOR; else throw new InternalError(); } //}}} } //}}} } //}}} //}}}}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -