📄 basictoolbarui.java
字号:
Point dragPoint = new Point(global.x- offset.x, global.y- offset.y); if (dockingSource == null) dockingSource = toolBar.getParent(); constraintBeforeFloating = calculateConstraint(); Point dockingPosition = dockingSource.getLocationOnScreen(); Point comparisonPoint = new Point(global.x-dockingPosition.x, global.y-dockingPosition.y); if (canDock(dockingSource, comparisonPoint)) { dragWindow.setBackground(getDockingColor()); String constraint = getDockingConstraint(dockingSource, comparisonPoint); int orientation = mapConstraintToOrientation(constraint); dragWindow.setOrientation(orientation); dragWindow.setBorderColor(dockingBorderColor); } else { dragWindow.setBackground(getFloatingColor()); dragWindow.setBorderColor(floatingBorderColor); } dragWindow.setLocation(dragPoint.x, dragPoint.y); if (dragWindow.isVisible() == false) { Dimension size = toolBar.getPreferredSize(); dragWindow.setSize(size.width, size.height); dragWindow.show(); } } catch ( IllegalComponentStateException e ) { } } } protected void floatAt(Point position, Point origin) { if(toolBar.isFloatable() == true) { try { Point offset = dragWindow.getOffset(); if (offset == null) { offset = position; dragWindow.setOffset(offset); } Point global = new Point(origin.x+ position.x, origin.y+position.y); setFloatingLocation(global.x-offset.x, global.y-offset.y); if (dockingSource != null) { Point dockingPosition = dockingSource.getLocationOnScreen(); Point comparisonPoint = new Point(global.x-dockingPosition.x, global.y-dockingPosition.y); if (canDock(dockingSource, comparisonPoint)) { setFloating(false, comparisonPoint); } else { setFloating(true, null); } } else { setFloating(true, null); } dragWindow.setOffset(null); } catch ( IllegalComponentStateException e ) { } } } private Handler getHandler() { if (handler == null) { handler = new Handler(); } return handler; } protected ContainerListener createToolBarContListener( ) { return getHandler(); } protected FocusListener createToolBarFocusListener( ) { return getHandler(); } protected PropertyChangeListener createPropertyListener() { return getHandler(); } protected MouseInputListener createDockingListener( ) { getHandler().tb = toolBar; return getHandler(); } protected WindowListener createFrameListener() { return new FrameListener(); } /** * Paints the contents of the window used for dragging. * * @param g Graphics to paint to. * @throws NullPointerException is <code>g</code> is null * @since 1.5 */ protected void paintDragWindow(Graphics g) { g.setColor(dragWindow.getBackground()); int w = dragWindow.getWidth(); int h = dragWindow.getHeight(); g.fillRect(0, 0, w, h); g.setColor(dragWindow.getBorderColor()); g.drawRect(0, 0, w - 1, h - 1); } private static class Actions extends UIAction { private static final String NAVIGATE_RIGHT = "navigateRight"; private static final String NAVIGATE_LEFT = "navigateLeft"; private static final String NAVIGATE_UP = "navigateUp"; private static final String NAVIGATE_DOWN = "navigateDown"; public Actions(String name) { super(name); } public void actionPerformed(ActionEvent evt) { String key = getName(); JToolBar toolBar = (JToolBar)evt.getSource(); BasicToolBarUI ui = (BasicToolBarUI)BasicLookAndFeel.getUIOfType( toolBar.getUI(), BasicToolBarUI.class); if (NAVIGATE_RIGHT == key) { ui.navigateFocusedComp(EAST); } else if (NAVIGATE_LEFT == key) { ui.navigateFocusedComp(WEST); } else if (NAVIGATE_UP == key) { ui.navigateFocusedComp(NORTH); } else if (NAVIGATE_DOWN == key) { ui.navigateFocusedComp(SOUTH); } } } private class Handler implements ContainerListener, FocusListener, MouseInputListener, PropertyChangeListener { // // ContainerListener // public void componentAdded(ContainerEvent evt) { Component c = evt.getChild(); if (toolBarFocusListener != null) { c.addFocusListener(toolBarFocusListener); } if (isRolloverBorders()) { setBorderToRollover(c); } else { setBorderToNonRollover(c); } } public void componentRemoved(ContainerEvent evt) { Component c = evt.getChild(); if (toolBarFocusListener != null) { c.removeFocusListener(toolBarFocusListener); } // Revert the button border setBorderToNormal(c); } // // FocusListener // public void focusGained(FocusEvent evt) { Component c = evt.getComponent(); focusedCompIndex = toolBar.getComponentIndex(c); } public void focusLost(FocusEvent evt) { } // // MouseInputListener (DockingListener) // JToolBar tb; boolean isDragging = false; Point origin = null; public void mousePressed(MouseEvent evt) { if (!tb.isEnabled()) { return; } isDragging = false; } public void mouseReleased(MouseEvent evt) { if (!tb.isEnabled()) { return; } if (isDragging == true) { Point position = evt.getPoint(); if (origin == null) origin = evt.getComponent().getLocationOnScreen(); floatAt(position, origin); } origin = null; isDragging = false; } public void mouseDragged(MouseEvent evt) { if (!tb.isEnabled()) { return; } isDragging = true; Point position = evt.getPoint(); if (origin == null) { origin = evt.getComponent().getLocationOnScreen(); } dragTo(position, origin); } public void mouseClicked(MouseEvent evt) {} public void mouseEntered(MouseEvent evt) {} public void mouseExited(MouseEvent evt) {} public void mouseMoved(MouseEvent evt) {} // // PropertyChangeListener // public void propertyChange(PropertyChangeEvent evt) { String propertyName = evt.getPropertyName(); if (propertyName == "lookAndFeel") { toolBar.updateUI(); } else if (propertyName == "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)evt.getNewValue()).intValue(); JToolBar.Separator separator; for (int i = 0; i < components.length; ++i) { if (components[i] instanceof JToolBar.Separator) { separator = (JToolBar.Separator)components[i]; if ((orientation == JToolBar.HORIZONTAL)) { separator.setOrientation(JSeparator.VERTICAL); } else { separator.setOrientation(JSeparator.HORIZONTAL); } Dimension size = separator.getSeparatorSize(); if (size != null && size.width != size.height) { // Flip the orientation. Dimension newSize = new Dimension(size.height, size.width); separator.setSeparatorSize(newSize); } } } } else if (propertyName == IS_ROLLOVER) { installNormalBorders(toolBar); setRolloverBorders(((Boolean)evt.getNewValue()).booleanValue()); } } } 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; if (toolBar.getOrientation() == JToolBar.HORIZONTAL) { if (constraint == "West" || constraint == "East") { constraint = "North"; } } else { if (constraint == "North" || constraint == "South") { constraint = "West"; } } if (dockingSource == null) dockingSource = toolBar.getParent(); if (propertyListener != null) UIManager.removePropertyChangeListener(propertyListener); dockingSource.add(toolBar, constraint); dockingSource.invalidate(); Container dockingSourceParent = dockingSource.getParent(); if (dockingSourceParent != null) dockingSourceParent.validate(); dockingSource.repaint(); } } } protected class ToolBarContListener implements ContainerListener { // NOTE: This class exists only for backward compatability. All // its functionality has been moved into Handler. If you need to add // new functionality add it to the Handler, but make sure this // class calls into the Handler. public void componentAdded( ContainerEvent e ) { getHandler().componentAdded(e); } public void componentRemoved( ContainerEvent e ) { getHandler().componentRemoved(e); } } protected class ToolBarFocusListener implements FocusListener { // NOTE: This class exists only for backward compatability. All // its functionality has been moved into Handler. If you need to add // new functionality add it to the Handler, but make sure this // class calls into the Handler. public void focusGained( FocusEvent e ) { getHandler().focusGained(e); } public void focusLost( FocusEvent e ) { getHandler().focusLost(e); } } protected class PropertyListener implements PropertyChangeListener { // NOTE: This class exists only for backward compatability. All // its functionality has been moved into Handler. If you need to add // new functionality add it to the Handler, but make sure this // class calls into the Handler. public void propertyChange( PropertyChangeEvent e ) { getHandler().propertyChange(e); } } /** * This class should be treated as a "protected" inner class. * Instantiate it only within subclasses of BasicToolBarUI. */ public class DockingListener implements MouseInputListener { // NOTE: This class exists only for backward compatability. All // its functionality has been moved into Handler. If you need to add // new functionality add it to the Handler, but make sure this // class calls into the Handler. protected JToolBar toolBar; protected boolean isDragging = false; protected Point origin = null; public DockingListener(JToolBar t) { this.toolBar = t; getHandler().tb = t; } public void mouseClicked(MouseEvent e) { getHandler().mouseClicked(e); } public void mousePressed(MouseEvent e) { getHandler().tb = toolBar; getHandler().mousePressed(e); isDragging = getHandler().isDragging; } public void mouseReleased(MouseEvent e) { getHandler().tb = toolBar; getHandler().isDragging = isDragging; getHandler().origin = origin; getHandler().mouseReleased(e); isDragging = getHandler().isDragging; origin = getHandler().origin; } public void mouseEntered(MouseEvent e) { getHandler().mouseEntered(e); } public void mouseExited(MouseEvent e) { getHandler().mouseExited(e); } public void mouseDragged(MouseEvent e) { getHandler().tb = toolBar; getHandler().origin = origin; getHandler().mouseDragged(e); isDragging = getHandler().isDragging; origin = getHandler().origin; } public void mouseMoved(MouseEvent e) { getHandler().mouseMoved(e); } } protected class DragWindow extends Window { Color borderColor = Color.gray; int orientation = toolBar.getOrientation(); Point offset; // offset of the mouse cursor inside the DragWindow DragWindow(Window w) { super(w); } /** * Returns the orientation of the toolbar window when the toolbar is * floating. The orientation is either one of <code>JToolBar.HORIZONTAL</code> * or <code>JToolBar.VERTICAL</code>. * * @return the orientation of the toolbar window * @since 1.6 */ public int getOrientation() { return orientation; } 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( BasicGraphicsUtils.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 setBorderColor(Color c) { if (this.borderColor == c) return; this.borderColor = c; repaint(); } public Color getBorderColor() { return this.borderColor; } public void paint(Graphics g) { paintDragWindow(g); // Paint the children super.paint(g); } public Insets getInsets() { return new Insets(1,1,1,1); } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -