📄 toolbarui.java
字号:
{
if (p == null)
return constraintBeforeFloating;
String s= BorderLayout.NORTH;
if (c.contains(p))
{
dockingSensitivity=
(toolBar.getOrientation() == JToolBar.HORIZONTAL)
? toolBar.getSize().height
: toolBar.getSize().width;
if (p.y >= dockingSource.getSize().height - dockingSensitivity)
s= BorderLayout.SOUTH;
// West (Base distance on height for now!)
else if (
p.x < dockingSensitivity
&& (toolBar.getOrientation() == JToolBar.VERTICAL))
s= BorderLayout.WEST;
// East (Base distance on height for now!)
else if (
p.x >= dockingSource.getSize().width - dockingSensitivity
&& (toolBar.getOrientation() == JToolBar.VERTICAL))
s= BorderLayout.EAST;
// North (Base distance on height for now!)
else if (p.y < dockingSensitivity)
s= BorderLayout.NORTH;
}
return s;
}
private String getDockingConstraint(Component c, Point p)
{
if (p == null)
return constraintBeforeFloating;
String s= BorderLayout.NORTH;
if (c.contains(p))
{
dockingSensitivity=
(toolBar.getOrientation() == JToolBar.HORIZONTAL)
? toolBar.getSize().height
: toolBar.getSize().width;
if (p.y >= dockingSource.getSize().height - dockingSensitivity)
s= BorderLayout.SOUTH;
// West (Base distance on height for now!)
if (p.x < dockingSensitivity)
s= BorderLayout.WEST;
// East (Base distance on height for now!)
if (p.x >= dockingSource.getSize().width - dockingSensitivity)
s= BorderLayout.EAST;
// North (Base distance on height for now!)
if (p.y < dockingSensitivity)
s= BorderLayout.NORTH;
}
return s;
}
protected void dragTo(Point position, Point origin)
{
if (toolBar.isFloatable() == true)
{
try
{
if (dragWindow == null)
dragWindow= createMyDragWindow(toolBar);
Point offset= dragWindow.getOffset();
if (offset == null)
{
Dimension size= toolBar.getPreferredSize();
offset= new Point(size.width / 2, size.height / 2);
dragWindow.setOffset(offset);
}
Point global=
new Point(origin.x + position.x, origin.y + position.y);
Point dragPoint=
new Point(global.x - offset.x, global.y - offset.y);
if (dockingSource == null)
dockingSource= toolBar.getParent();
Point p= new Point(origin);
SwingUtilities.convertPointFromScreen(p, toolBar.getParent());
constraintBeforeFloating= calculateConstraint(dockingSource, p);
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.setOrientation(JToolBar.HORIZONTAL);
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)
{
}
}
}
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();
ToolBarUI ui= (ToolBarUI) toolBar.getUI();
ui.navigateFocusedComp(EAST);
}
};
private static class LeftAction extends KeyAction
{
public void actionPerformed(ActionEvent e)
{
JToolBar toolBar= (JToolBar) e.getSource();
ToolBarUI ui= (ToolBarUI) toolBar.getUI();
ui.navigateFocusedComp(WEST);
}
};
private static class UpAction extends KeyAction
{
public void actionPerformed(ActionEvent e)
{
JToolBar toolBar= (JToolBar) e.getSource();
ToolBarUI ui= (ToolBarUI) toolBar.getUI();
ui.navigateFocusedComp(NORTH);
}
};
private static class DownAction extends KeyAction
{
public void actionPerformed(ActionEvent e)
{
JToolBar toolBar= (JToolBar) e.getSource();
ToolBarUI ui= (ToolBarUI) 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);
}
setButtonSize(c);
if (isRolloverBorders())
{
setBorderToRollover(c);
}
else
{
setBorderToNonRollover(c);
}
}
public void componentRemoved(ContainerEvent e)
{
Component c= e.getChild();
if (toolBarFocusListener != null)
{
c.removeFocusListener(toolBarFocusListener);
}
// Revert the button border
setBorderToNormal(c);
}
} // end class ToolBarContListener
protected void setButtonSize(Component c)
{
if(c instanceof AbstractButton)
{
//((AbstractButton)c).setPreferredSize(new Dimension(c.getPreferredSize().width+4, c.getPreferredSize().height+4));
((AbstractButton)c).setRolloverEnabled(true);
((AbstractButton)c).setBackground(toolBar.getBackground());
}
}
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 (propertyName.equals("lookAndFeel"))
{
toolBar.updateUI();
}
else if (propertyName.equals("orientation"))
{
setOrientedBorder();
// 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);
}
}
}
}
else if (propertyName.equals(IS_ROLLOVER))
{
setRolloverBorders(((Boolean) e.getNewValue()).booleanValue());
}
}
}
/**
* This inner class is marked "public" due to a compiler bug.
* This class should be treated as a "protected" inner class.
* Instantiate it only within subclasses of BasicToolBarUI.
*/
public 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
{
Color borderColor= Color.gray;
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 (TonicUtils.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)
{
Color temp= g.getColor();
g.setColor(getBackground());
Dimension size= getSize();
g.fillRect(0, 0, size.width, size.height);
g.setColor(UIManager.getColor("Button.borderColor"));
switch(orientation)
{
case WEST:
case NORTH:
g.drawLine(0, size.height-1, size.width-1, size.height-1);
break;
case SOUTH:
case EAST:
}
//super.paint(g);
}
public Insets getInsets()
{
return new Insets(1, 1, 1, 1);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -