tooltipmanager.java
来自「Mac OS X 10.4.9 for x86 Source Code gcc」· Java 代码 · 共 642 行 · 第 1/2 页
JAVA
642 行
/** * This method sets the amount of delay where if the mouse re-enters a * Component, the tooltip will be shown immediately. * * @param delay The reshow delay. */ public void setReshowDelay(int delay) { exitTimer.setDelay(delay); } /** * This method registers a JComponent with the ToolTipManager. * * @param component The JComponent to register with the ToolTipManager. */ public void registerComponent(JComponent component) { component.addMouseListener(this); component.addMouseMotionListener(this); } /** * This method unregisters a JComponent with the ToolTipManager. * * @param component The JComponent to unregister with the ToolTipManager. */ public void unregisterComponent(JComponent component) { component.removeMouseMotionListener(this); component.removeMouseListener(this); } /** * This method is called whenever the mouse enters a JComponent registered * with the ToolTipManager. When the mouse enters within the period of time * specified by the reshow delay, the tooltip will be displayed * immediately. Otherwise, it must wait for the initial delay before * displaying the tooltip. * * @param event The MouseEvent. */ public void mouseEntered(MouseEvent event) { if (currentComponent != null && getContentPaneDeepestComponent(event) == currentComponent) return; currentPoint = event.getPoint(); currentComponent = (Component) event.getSource(); if (exitTimer.isRunning()) { exitTimer.stop(); showTip(); insideTimer.start(); return; } // This should always be stopped unless we have just fake-exited. if (! enterTimer.isRunning()) enterTimer.start(); } /** * This method is called when the mouse exits a JComponent registered with * the ToolTipManager. When the mouse exits, the tooltip should be hidden * immediately. * * @param event The MouseEvent. */ public void mouseExited(MouseEvent event) { if (getContentPaneDeepestComponent(event) == currentComponent) return; currentPoint = event.getPoint(); currentComponent = null; hideTip(); if (! enterTimer.isRunning() && insideTimer.isRunning()) exitTimer.start(); if (enterTimer.isRunning()) enterTimer.stop(); if (insideTimer.isRunning()) insideTimer.stop(); } /** * This method is called when the mouse is pressed on a JComponent * registered with the ToolTipManager. When the mouse is pressed, the * tooltip (if it is shown) must be hidden immediately. * * @param event The MouseEvent. */ public void mousePressed(MouseEvent event) { currentPoint = event.getPoint(); if (enterTimer.isRunning()) enterTimer.restart(); else if (insideTimer.isRunning()) { insideTimer.stop(); hideTip(); } currentComponent.invalidate(); currentComponent.validate(); currentComponent.repaint(); } /** * This method is called when the mouse is dragged in a JComponent * registered with the ToolTipManager. * * @param event The MouseEvent. */ public void mouseDragged(MouseEvent event) { currentPoint = event.getPoint(); if (enterTimer.isRunning()) enterTimer.restart(); } /** * This method is called when the mouse is moved in a JComponent registered * with the ToolTipManager. * * @param event The MouseEvent. */ public void mouseMoved(MouseEvent event) { currentPoint = event.getPoint(); if (currentTip != null) { if (currentComponent == null) currentComponent = (Component) event.getSource(); String text = ((JComponent) currentComponent).getToolTipText(event); currentTip.setTipText(text); } if (enterTimer.isRunning()) enterTimer.restart(); } /** * This method displays the ToolTip. It can figure out the method needed to * show it as well (whether to display it in heavyweight/lightweight panel * or a window.) */ private void showTip() { if (! enabled || currentComponent == null) return; if (currentTip == null || currentTip.getComponent() != currentComponent && currentComponent instanceof JComponent) currentTip = ((JComponent) currentComponent).createToolTip(); Point p = currentPoint; Dimension dims = currentTip.getPreferredSize(); if (canToolTipFit(currentTip)) { JLayeredPane pane = ((JRootPane) SwingUtilities.getAncestorOfClass(JRootPane.class, currentComponent)) .getLayeredPane(); // This should never happen, but just in case. if (pane == null) return; if (containerPanel != null) hideTip(); if (isLightWeightPopupEnabled()) { containerPanel = new Panel(); JRootPane root = new JRootPane(); root.getContentPane().add(currentTip); containerPanel.add(root); } else { containerPanel = new JPanel(); containerPanel.add(currentTip); } LayoutManager lm = containerPanel.getLayout(); if (lm instanceof FlowLayout) { FlowLayout fm = (FlowLayout) lm; fm.setVgap(0); fm.setHgap(0); } p = getGoodPoint(p, pane, currentTip, dims); pane.add(containerPanel); containerPanel.setBounds(p.x, p.y, dims.width, dims.height); currentTip.setBounds(0, 0, dims.width, dims.height); pane.revalidate(); pane.repaint(); } else { SwingUtilities.convertPointToScreen(p, currentComponent); tooltipWindow = new JWindow(); tooltipWindow.getContentPane().add(currentTip); tooltipWindow.setFocusable(false); tooltipWindow.pack(); tooltipWindow.setBounds(p.x, p.y, dims.width, dims.height); tooltipWindow.show(); } currentTip.setVisible(true); } /** * This method hides the ToolTip. */ private void hideTip() { if (currentTip == null || ! currentTip.isVisible() || ! enabled) return; currentTip.setVisible(false); if (containerPanel != null) { Container parent = containerPanel.getParent(); if (parent == null) return; parent.remove(containerPanel); parent.invalidate(); parent.validate(); parent.repaint(); parent = currentTip.getParent(); if (parent == null) return; parent.remove(currentTip); containerPanel = null; } if (tooltipWindow != null) { tooltipWindow.hide(); tooltipWindow.dispose(); tooltipWindow = null; } } /** * This method returns a point in the LayeredPane where the ToolTip can be * shown. The point returned (if the ToolTip is to be displayed at the * preferred dimensions) will always place the ToolTip inside the * currentComponent if possible. * * @param p The last known good point for the mouse. * @param c The JLayeredPane in the first RootPaneContainer up from the * currentComponent. * @param tip The ToolTip to display. * @param dims The ToolTip preferred dimensions (can be null). * * @return A good point to place the ToolTip. */ private Point getGoodPoint(Point p, JLayeredPane c, JToolTip tip, Dimension dims) { if (dims == null) dims = tip.getPreferredSize(); Rectangle bounds = currentComponent.getBounds(); if (p.x + dims.width > bounds.width) p.x = bounds.width - dims.width; if (p.y + dims.height > bounds.height) p.y = bounds.height - dims.height; p = SwingUtilities.convertPoint(currentComponent, p, c); return p; } /** * This method returns the deepest component in the content pane for the * first RootPaneContainer up from the currentComponent. This method is * used in conjunction with one of the mouseXXX methods. * * @param e The MouseEvent. * * @return The deepest component in the content pane. */ private Component getContentPaneDeepestComponent(MouseEvent e) { Component source = (Component) e.getSource(); Container parent = (Container) SwingUtilities.getAncestorOfClass(JRootPane.class, currentComponent); if (parent == null) return null; parent = ((JRootPane) parent).getContentPane(); Point p = e.getPoint(); p = SwingUtilities.convertPoint(source, p, parent); Component target = SwingUtilities.getDeepestComponentAt(parent, p.x, p.y); return target; } /** * This method returns whether the ToolTip can fit in the first * RootPaneContainer up from the currentComponent. * * @param tip The ToolTip. * * @return Whether the ToolTip can fit. */ private boolean canToolTipFit(JToolTip tip) { JRootPane root = (JRootPane) SwingUtilities.getAncestorOfClass(JRootPane.class, currentComponent); if (root == null) return false; Dimension pref = tip.getPreferredSize(); Dimension rootSize = root.getSize(); if (rootSize.width > pref.width && rootSize.height > pref.height) return true; return false; }}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?