📄 basictoolbarui.java
字号:
* @param t The JToolBar this DockingListener is being used for. */ public DockingListener(JToolBar t) { toolBar = t; } /** * This method is called when the mouse is clicked. * * @param e The MouseEvent. */ public void mouseClicked(MouseEvent e) { // Don't care. } /** * This method is called when the mouse is dragged. It delegates the drag * painting to the dragTo method. * * @param e The MouseEvent. */ public void mouseDragged(MouseEvent e) { if (isDragging) dragTo(e.getPoint(), origin); } /** * This method is called when the mouse enters the JToolBar. * * @param e The MouseEvent. */ public void mouseEntered(MouseEvent e) { // Don't care (yet). } /** * This method is called when the mouse exits the JToolBar. * * @param e The MouseEvent. */ public void mouseExited(MouseEvent e) { // Don't care (yet). } /** * This method is called when the mouse is moved in the JToolBar. * * @param e The MouseEvent. */ public void mouseMoved(MouseEvent e) { // TODO: What should be done here, if anything? } /** * This method is called when the mouse is pressed in the JToolBar. If the * press doesn't occur in a place where it causes the JToolBar to be * dragged, it returns. Otherwise, it starts a drag session. * * @param e The MouseEvent. */ public void mousePressed(MouseEvent e) { if (! toolBar.isFloatable()) return; Point ssd = e.getPoint(); Insets insets = toolBar.getInsets(); // Verify that this click occurs in the top inset. if (toolBar.getOrientation() == SwingConstants.HORIZONTAL) { if (e.getX() > insets.left) return; } else { if (e.getY() > insets.top) return; } origin = new Point(0, 0); if (toolBar.isShowing()) SwingUtilities.convertPointToScreen(ssd, toolBar); if (! (SwingUtilities.getAncestorOfClass(Window.class, toolBar) instanceof UIResource)) // Need to know who keeps the toolBar if it gets dragged back into it. origParent = toolBar.getParent(); if (toolBar.isShowing()) SwingUtilities.convertPointToScreen(origin, toolBar); isDragging = true; if (dragWindow != null) dragWindow.setOffset(new Point(e.getX(), e.getY())); dragTo(e.getPoint(), origin); } /** * This method is called when the mouse is released from the JToolBar. * * @param e The MouseEvent. */ public void mouseReleased(MouseEvent e) { if (! isDragging || ! toolBar.isFloatable()) return; isDragging = false; floatAt(e.getPoint(), origin); dragWindow.hide(); } } /** * This is the window that appears when the JToolBar is being dragged * around. */ protected class DragWindow extends Window { /** * The current border color. It changes depending on whether the JToolBar * is over a place that allows it to dock. */ private Color borderColor; /** The between the mouse and the top left corner of the window. */ private Point offset; /** * Creates a new DragWindow object. * This is package-private to avoid an accessor method. */ DragWindow() { super(owner); } /** * The color that the border should be. * * @return The border color. */ public Color getBorderColor() { if (borderColor == null) return Color.BLACK; return borderColor; } /** * This method returns the insets for the DragWindow. * * @return The insets for the DragWindow. */ public Insets getInsets() { // This window has no decorations, so insets are empty. return new Insets(0, 0, 0, 0); } /** * This method returns the mouse offset from the top left corner of the * DragWindow. * * @return The mouse offset. */ public Point getOffset() { return offset; } /** * This method paints the DragWindow. * * @param g The Graphics object to paint with. */ public void paint(Graphics g) { // No visiting children necessary. Color saved = g.getColor(); Rectangle b = getBounds(); g.setColor(getBorderColor()); g.drawRect(0, 0, b.width - 1, b.height - 1); g.setColor(saved); } /** * This method changes the border color. * * @param c The new border color. */ public void setBorderColor(Color c) { borderColor = c; } /** * This method changes the mouse offset. * * @param p The new mouse offset. */ public void setOffset(Point p) { offset = p; } /** * FIXME: Do something. * * @param o DOCUMENT ME! */ public void setOrientation(int o) { // FIXME: implement. } } /** * This helper class listens for Window events from the floatable window and * if it is closed, returns the JToolBar to the last known good location. */ protected class FrameListener extends WindowAdapter { /** * This method is called when the floating window is closed. * * @param e The WindowEvent. */ public void windowClosing(WindowEvent e) { Container parent = toolBar.getParent(); parent.remove(toolBar); if (origParent != null) { origParent.add(toolBar, (constraintBeforeFloating != null) ? constraintBeforeFloating : BorderLayout.NORTH); toolBar.setOrientation(lastGoodOrientation); } origParent.invalidate(); origParent.validate(); origParent.repaint(); } } /** * This helper class listens for PropertyChangeEvents from the JToolBar. */ protected class PropertyListener implements PropertyChangeListener { /** * This method is called when a property from the JToolBar is changed. * * @param e The PropertyChangeEvent. */ public void propertyChange(PropertyChangeEvent e) { // FIXME: need name properties so can change floatFrame title. if (e.getPropertyName().equals("rollover")) setRolloverBorders(toolBar.isRollover()); } } /** * This helper class listens for components added to and removed from the * JToolBar. */ protected class ToolBarContListener implements ContainerListener { /** * This method is responsible for setting rollover or non rollover for new * buttons added to the JToolBar. * * @param e The ContainerEvent. */ public void componentAdded(ContainerEvent e) { if (e.getChild() instanceof JButton) { JButton b = (JButton) e.getChild(); if (b.getBorder() != null) borders.put(b, b.getBorder()); } if (isRolloverBorders()) setBorderToRollover(e.getChild()); else setBorderToNonRollover(e.getChild()); cachedBounds = toolBar.getPreferredSize(); cachedOrientation = toolBar.getOrientation(); } /** * This method is responsible for giving the child components their * original borders when they are removed. * * @param e The ContainerEvent. */ public void componentRemoved(ContainerEvent e) { setBorderToNormal(e.getChild()); cachedBounds = toolBar.getPreferredSize(); cachedOrientation = toolBar.getOrientation(); } } /** * This is the floating window that is returned when getFloatingWindow is * called. */ private class ToolBarDialog extends JDialog implements UIResource { /** * Creates a new ToolBarDialog object with the name given by the JToolBar. */ public ToolBarDialog() { super(); setName((toolBar.getName() != null) ? toolBar.getName() : ""); } } /** * DOCUMENT ME! */ protected class ToolBarFocusListener implements FocusListener { /** * Creates a new ToolBarFocusListener object. */ protected ToolBarFocusListener() { // FIXME: implement. } /** * DOCUMENT ME! * * @param e DOCUMENT ME! */ public void focusGained(FocusEvent e) { // FIXME: implement. } /** * DOCUMENT ME! * * @param e DOCUMENT ME! */ public void focusLost(FocusEvent e) { // FIXME: implement. } } /** * This helper class acts as the border for the JToolBar. */ private static class ToolBarBorder implements Border { /** The size of the larger, draggable side of the border. */ private static final int offset = 10; /** The other sides. */ private static final int regular = 2; /** * This method returns the border insets for the JToolBar. * * @param c The Component to find insets for. * * @return The border insets. */ public Insets getBorderInsets(Component c) { if (c instanceof JToolBar) { JToolBar tb = (JToolBar) c; int orientation = tb.getOrientation(); if (! tb.isFloatable()) return new Insets(regular, regular, regular, regular); else if (orientation == SwingConstants.HORIZONTAL) return new Insets(regular, offset, regular, regular); else return new Insets(offset, regular, regular, regular); } return new Insets(0, 0, 0, 0); } /** * This method returns whether the border is opaque. * * @return Whether the border is opaque. */ public boolean isBorderOpaque() { return false; } /** * This method paints the ribbed area of the border. * * @param g The Graphics object to paint with. * @param x The x coordinate of the area. * @param y The y coordinate of the area. * @param w The width of the area. * @param h The height of the area. * @param size The size of the bump. * @param c The color of the bumps. */ private void paintBumps(Graphics g, int x, int y, int w, int h, int size, Color c) { Color saved = g.getColor(); g.setColor(c); int hgap = 2 * size; int vgap = 4 * size; int count = 0; for (int i = x; i < (w + x); i += hgap) for (int j = ((count++ % 2) == 0) ? y : (y + (2 * size)); j < (h + y); j += vgap) g.fillRect(i, j, size, size); g.setColor(saved); } /** * This method paints the border around the given Component. * * @param c The Component whose border is being painted. * @param g The Graphics object to paint with. * @param x The x coordinate of the component. * @param y The y coordinate of the component. * @param width The width of the component. * @param height The height of the component. */ public void paintBorder(Component c, Graphics g, int x, int y, int width, int height) { if (c instanceof JToolBar) { JToolBar tb = (JToolBar) c; int orientation = tb.getOrientation(); if (orientation == SwingConstants.HORIZONTAL) { paintBumps(g, x, y, offset, height, 1, Color.WHITE); paintBumps(g, x + 1, y + 1, offset - 1, height - 1, 1, Color.GRAY); } else { paintBumps(g, x, y, width, offset, 1, Color.WHITE); paintBumps(g, x + 1, y + 1, width - 1, offset - 1, 1, Color.GRAY); } } } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -