synthtoolbarui.java

来自「java jdk 1.4的源码」· Java 代码 · 共 1,086 行 · 第 1/3 页

JAVA
1,086
字号
	  }	}    }    protected ContainerListener createToolBarContListener( )    {	return new ToolBarContListener( );    }    protected FocusListener createToolBarFocusListener( )    {	return new ToolBarFocusListener( );    }    protected PropertyChangeListener createPropertyListener()    {        return new PropertyListener();    }    protected MouseInputListener createDockingListener( ) {	return new DockingListener(toolBar);    }        protected WindowListener createFrameListener() {	return new FrameListener();    }    // The private inner classes below should be changed to protected the    // next time API changes are allowed.      private static abstract class KeyAction extends AbstractAction {        public boolean isEnabled() {             return true;        }    };    private static class RightAction extends KeyAction {        public void actionPerformed(ActionEvent e) {	    JToolBar toolBar = (JToolBar)e.getSource();            // PENDING:	    SynthToolBarUI ui = (SynthToolBarUI)toolBar.getUI();            ui.navigateFocusedComp(EAST);        }    };        private static class LeftAction extends KeyAction {        public void actionPerformed(ActionEvent e) {	    JToolBar toolBar = (JToolBar)e.getSource();	    SynthToolBarUI ui = (SynthToolBarUI)toolBar.getUI();            ui.navigateFocusedComp(WEST);        }    };    private static class UpAction extends KeyAction {        public void actionPerformed(ActionEvent e) {	    JToolBar toolBar = (JToolBar)e.getSource();	    SynthToolBarUI ui = (SynthToolBarUI)toolBar.getUI();            ui.navigateFocusedComp(NORTH);        }    };    private static class DownAction extends KeyAction {        public void actionPerformed(ActionEvent e) {	    JToolBar toolBar = (JToolBar)e.getSource();	    SynthToolBarUI ui = (SynthToolBarUI)toolBar.getUI();            ui.navigateFocusedComp(SOUTH);        }    };    protected class FrameListener extends WindowAdapter {	public void windowClosing(WindowEvent w) {	    	    if (toolBar.isFloatable() == true) {		if (dragWindow != null)		    dragWindow.setVisible(false);		floating = false;		if (floatingToolBar == null)		    floatingToolBar = createFloatingWindow(toolBar);		if (floatingToolBar instanceof Window) ((Window)floatingToolBar).setVisible(false);		floatingToolBar.getContentPane().remove(toolBar);		String constraint = constraintBeforeFloating;		int orientation = mapConstraintToOrientation(constraint);		setOrientation(orientation);		if (dockingSource == null)		    dockingSource = toolBar.getParent();		if (propertyListener != null)		    UIManager.removePropertyChangeListener(propertyListener);		dockingSource.add(constraint, toolBar);		dockingSource.invalidate();		Container dockingSourceParent = dockingSource.getParent();		if (dockingSourceParent != null)			dockingSourceParent.validate();		dockingSource.repaint();		setFloating(false,null);	    }	}    }     protected class ToolBarContListener implements ContainerListener    {        public void componentAdded( ContainerEvent e )	{	    Component c = e.getChild();	    if ( toolBarFocusListener != null ) {	        c.addFocusListener( toolBarFocusListener );	    }	}        public void componentRemoved( ContainerEvent e ) {	    Component c = e.getChild();	    if ( toolBarFocusListener != null ) {	        c.removeFocusListener( toolBarFocusListener );	    }	}    } // end class ToolBarContListener    protected class ToolBarFocusListener implements FocusListener    {        public void focusGained( FocusEvent e )	{	    Component c = e.getComponent();	    focusedCompIndex = toolBar.getComponentIndex( c );	}        public void focusLost( FocusEvent e )	{	}    } // end class ToolBarFocusListener    protected class PropertyListener implements PropertyChangeListener    {        public void propertyChange( PropertyChangeEvent e ) {	    String propertyName = e.getPropertyName();            if (SynthLookAndFeel.shouldUpdateStyle(e)) {                fetchStyle(toolBar);            }	    if ( propertyName.equals("lookAndFeel") ) {	        toolBar.updateUI();            } else if (propertyName.equals("orientation")) {		// Search for JSeparator components and change it's orientation		// to match the toolbar and flip it's orientation.		Component[] components = toolBar.getComponents();		int orientation = ((Integer)e.getNewValue()).intValue();		JToolBar.Separator separator;		for( int i = 0; i < components.length; ++i ) {		    if (components[i] instanceof JToolBar.Separator) {			separator = (JToolBar.Separator)components[i];			separator.setOrientation(orientation);			Dimension size = separator.getSize();			if (size.width != size.height) {			    // Flip the orientation.			    Dimension newSize = new Dimension(size.height, size.width);			    separator.setSeparatorSize(newSize);			}		    }		}	    }	}    }    class SynthToolBarLayoutManager implements LayoutManager {        public void addLayoutComponent(String name, Component comp) {}        public void removeLayoutComponent(Component comp) {}        public Dimension minimumLayoutSize(Container parent) {            JToolBar tb = (JToolBar)parent;            Dimension dim = new Dimension();            SynthContext context = getContext(tb);            if (tb.getOrientation() == JToolBar.HORIZONTAL) {                dim.width = SynthIcon.getIconWidth(handleIcon, context);                Dimension compDim;                for (int i = 0; i < tb.getComponentCount(); i++) {                    compDim = tb.getComponent(i).getMinimumSize();                    dim.width += compDim.width;                    dim.height = Math.max(dim.height, compDim.height);                }            } else {                dim.height =                    SynthIcon.getIconHeight(handleIcon, context);                Dimension compDim;                for (int i = 0; i < tb.getComponentCount(); i++) {                    compDim = tb.getComponent(i).getMinimumSize();                    dim.width = Math.max(dim.width, compDim.width);                    dim.height += compDim.height;                }            }            context.dispose();            return dim;        }        public Dimension preferredLayoutSize(Container parent) {            JToolBar tb = (JToolBar)parent;            Dimension dim = new Dimension();            SynthContext context = getContext(tb);            if (tb.getOrientation() == JToolBar.HORIZONTAL) {                dim.width = SynthIcon.getIconWidth(handleIcon, context);                Dimension compDim;                for (int i = 0; i < tb.getComponentCount(); i++) {                    compDim = tb.getComponent(i).getPreferredSize();                    dim.width += compDim.width;                    dim.height = Math.max(dim.height, compDim.height);                }            } else {                dim.height =                    SynthIcon.getIconHeight(handleIcon, context);                Dimension compDim;                for (int i = 0; i < tb.getComponentCount(); i++) {                    compDim = tb.getComponent(i).getPreferredSize();                    dim.width = Math.max(dim.width, compDim.width);                    dim.height += compDim.height;                }            }            context.dispose();            return dim;        }        public void layoutContainer(Container parent) {            JToolBar tb = (JToolBar)parent;            boolean ltr = tb.getComponentOrientation().isLeftToRight();            SynthContext context = getContext(tb);            int handleWidth = SynthIcon.getIconWidth(handleIcon, context);            Component c;            Dimension d;            if (tb.getOrientation() == JToolBar.HORIZONTAL) {                int x = ltr ? handleWidth : tb.getWidth() - handleWidth;                for (int i = 0; i < tb.getComponentCount(); i++) {                    c = tb.getComponent(i);                    d = c.getPreferredSize();                    c.setBounds(ltr ? x : x - d.width, 0, d.width, d.height);                    x = ltr ? x + d.width : x - d.width;                }                contentRect.x = ltr ?                        SynthIcon.getIconWidth(handleIcon, context) : 0;                contentRect.y = 0;                contentRect.width = tb.getWidth() - contentRect.x;                contentRect.height = tb.getHeight();            } else {                int y = SynthIcon.getIconHeight(handleIcon, context);                for (int i = 0; i < tb.getComponentCount(); i++) {                    c = tb.getComponent(i);                    d = c.getPreferredSize();                    c.setBounds(0, y, d.width, d.height);                    y += d.height;                }                contentRect.x = 0;                contentRect.y =                    SynthIcon.getIconHeight(handleIcon, context);                contentRect.width = tb.getWidth();                contentRect.height = tb.getHeight() - contentRect.y;            }            context.dispose();        }    }    class DockingListener implements MouseInputListener {	protected JToolBar toolBar;	protected boolean isDragging = false;	protected Point origin = null;	public DockingListener(JToolBar t) {	    this.toolBar = t;	} 	public void mouseClicked(MouseEvent e) {}	public void mousePressed(MouseEvent e) {             if (!toolBar.isEnabled()) {                return;            }	    isDragging = false;	}	public void mouseReleased(MouseEvent e) {            if (!toolBar.isEnabled()) {                return;            }	    if (isDragging == true) {		Point position = e.getPoint();		if (origin == null)		    origin = e.getComponent().getLocationOnScreen();		floatAt(position, origin);	    }	    origin = null;	    isDragging = false;	}	public void mouseEntered(MouseEvent e) { }	public void mouseExited(MouseEvent e) { }	public void mouseDragged(MouseEvent e) {            if (!toolBar.isEnabled()) {                return;            }	    isDragging = true;	    Point position = e.getPoint();	    if (origin == null)		origin = e.getComponent().getLocationOnScreen();	    dragTo(position, origin);	}	public void mouseMoved(MouseEvent e) {	}    }    protected class DragWindow extends Window    {	int orientation = toolBar.getOrientation();	Point offset; // offset of the mouse cursor inside the DragWindow	DragWindow(Window w) {	    super(w);	}	public void setOrientation(int o) {	    if(isShowing()) {		if (o == this.orientation)		    return;	    		this.orientation = o;		Dimension size = getSize();		setSize(new Dimension(size.height, size.width));		if (offset!=null) {                    if( SynthLookAndFeel.isLeftToRight(toolBar) ) {                        setOffset(new Point(offset.y, offset.x));                    } else if( o == JToolBar.HORIZONTAL ) {                        setOffset(new Point( size.height-offset.y, offset.x));                    } else {                        setOffset(new Point(offset.y, size.width-offset.x));                    }                }		repaint();	    }	}	public Point getOffset() {	    return offset;	}	public void setOffset(Point p) {	    this.offset = p;	}		public void paint(Graphics g) {            SynthContext context = getContext(toolBar,                                              Region.TOOL_BAR_DRAG_WINDOW);            SynthLookAndFeel.updateSubregion(context, g, new Rectangle(                                             0, 0, getWidth(), getHeight()));            context.dispose();	}	public Insets getInsets() {	    return new Insets(1,1,1,1);	}    }}

⌨️ 快捷键说明

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