Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2010 BSI Business Systems Integration AG.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     BSI Business Systems Integration AG - initial API and implementation
 ******************************************************************************/

import java.awt.Component;
import java.awt.Container;

import java.awt.Dimension;

import java.awt.Insets;

import java.awt.Point;
import java.awt.Rectangle;

import java.awt.event.MouseEvent;

import javax.swing.JComponent;

import javax.swing.JToolTip;

public class Main {
    /**
     * Returns an appropriate location for a component's tool tip that <i>always</i>
     * lies within the specified frame.
     * <p>
     * Intended be used in custom implementations of {@link JComponent#getToolTipLocation(MouseEvent)}.
     *
     * @param e
     *          the event that caused the display of the tool tip
     * @param c
     *          the parent component of the tool tip
     * @param frame
     *          a component in which the tool tip has to fit (usually the surrounding window of "c")
     * @return
     */
    public static Point getAdjustedToolTipLocation(MouseEvent e, JComponent c, Component frame) {
        JToolTip tip = new JToolTip();
        tip.setTipText(c.getToolTipText(e));
        Dimension tipSize = tip.getPreferredSize();
        // Tool tip will be positioned within the bounds of the specified component (+ 5px inset)
        Rectangle frameR = frame.getBounds();
        if (frame instanceof Container) {
            Container container = (Container) frame;
            Insets insets = container.getInsets();
            frameR.x += insets.left;
            frameR.y += insets.top;
            frameR.width -= (insets.left + insets.right);
            frameR.height -= (insets.top + insets.bottom);
        }
        frameR.x += 5;
        frameR.y += 5;
        frameR.width -= 10;
        frameR.height -= 10;
        // Initial try for the tool tip's position
        Rectangle r = new Rectangle(e.getXOnScreen(), c.getLocationOnScreen().y + c.getSize().height + 1,
                tipSize.width, tipSize.height);
        // Check if it fits within the frame
        Rectangle intersection = frameR.intersection(r);
        if (r.equals(intersection)) {
            // Tool tip is fully visible within the frame --> use default behaviour
            //
            // Note: The implementation of ToolTipManager.showTipWindow() is not always
            // correct in dual screen mode. The tool tip is _always_ put on that screen,
            // where the most part of the frame lies upon, even if we return coordinates
            // that clearly belong to the other screen. Unfortunately we cannot change
            // that behavior... (bsh 2010-11-24)
            return null;
        }
        // Otherwise, move the tool tip
        int correction = 0;
        if (r.height == intersection.height) {
            // Height is okay, just move left. To make it look better, position the
            // tip 5px below the component.
            r = new Rectangle(r.x, c.getLocationOnScreen().y + c.getSize().height + 5, tipSize.width,
                    tipSize.height);
            correction = -5; // needed to make the ToolTipManager use a lightweight pop-up
        } else {
            // The height does not fit. Position the tool tip above the component.
            r = new Rectangle(c.getLocationOnScreen().x + 10, c.getLocationOnScreen().y - tipSize.height - 1,
                    tipSize.width, tipSize.height);
        }
        // Adjust to frame bounds
        intersection = frameR.intersection(r);
        intersection.x -= (r.width - intersection.width);
        intersection.y -= (r.height - intersection.height);
        // Return value is expected to be relative to the component's position
        return new Point((-c.getLocationOnScreen().x) + intersection.x + correction,
                (-c.getLocationOnScreen().y) + intersection.y);
    }
}