File: ScreenGeometry.java

package info (click to toggle)
libglazedlists-java 1.9.0%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 3,024 kB
  • ctags: 4,252
  • sloc: java: 22,561; xml: 818; sh: 51; makefile: 5
file content (98 lines) | stat: -rw-r--r-- 3,724 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
/* Glazed Lists                                                 (c) 2003-2006 */
/* http://publicobject.com/glazedlists/                      publicobject.com,*/
/*                                                     O'Dell Engineering Ltd.*/
package ca.odell.glazedlists.impl.swing;

import javax.swing.*;
import java.awt.*;

/**
 * Figure out the dimensions of our screen.
 *
 * <p>This code is inspired by similar in
 * <code>JPopupMenu.adjustPopupLocationToFitScreen()</code>.
 *
 * @author <a href="mailto:jesse@swank.ca">Jesse Wilson</a>
 */
public class ScreenGeometry {

    final GraphicsConfiguration graphicsConfiguration;
    final boolean aqua;

    public ScreenGeometry(JComponent component) {
        this.aqua = UIManager.getLookAndFeel().getName().indexOf("Aqua") != -1;
        this.graphicsConfiguration = graphicsConfigurationForComponent(component);
    }

    /**
     * Get the best graphics configuration for the specified point and component.
     */
    private GraphicsConfiguration graphicsConfigurationForComponent(Component component) {
        Point point = component.getLocationOnScreen();

        // try to find the graphics configuration for our point of interest
        GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
        GraphicsDevice[] gd = ge.getScreenDevices();
        for(int i = 0; i < gd.length; i++) {
            if(gd[i].getType() != GraphicsDevice.TYPE_RASTER_SCREEN) continue;
            GraphicsConfiguration defaultGraphicsConfiguration = gd[i].getDefaultConfiguration();
            if(!defaultGraphicsConfiguration.getBounds().contains(point)) continue;
            return defaultGraphicsConfiguration;
        }

        // we couldn't find a graphics configuration, use the component's
        return component.getGraphicsConfiguration();
    }

    /**
     * Get the bounds of where we can put a popup.
     */
    public Rectangle getScreenBounds() {
        Rectangle screenSize = getScreenSize();
        Insets screenInsets = getScreenInsets();

        return new Rectangle(
            screenSize.x + screenInsets.left,
            screenSize.y + screenInsets.top,
            screenSize.width - screenInsets.left - screenInsets.right,
            screenSize.height - screenInsets.top - screenInsets.bottom
        );
    }

    /**
     * Get the bounds of the screen currently displaying the component.
     */
    public Rectangle getScreenSize() {
        // get the screen bounds and insets via the graphics configuration
        if(graphicsConfiguration != null) {
            return graphicsConfiguration.getBounds();
        }

        // just use the toolkit bounds, it's less awesome but sufficient
        return new Rectangle(Toolkit.getDefaultToolkit().getScreenSize());
    }

    /**
     * Fetch the screen insets, the off limits areas around the screen such
     * as menu bar, dock or start bar.
     */
    public Insets getScreenInsets() {
        Insets screenInsets;
        if(graphicsConfiguration != null) {
            screenInsets = Toolkit.getDefaultToolkit().getScreenInsets(graphicsConfiguration);
        } else {
            screenInsets = new Insets(0, 0, 0, 0);
        }

        // tweak the insets for aqua, they're reported incorrectly there
        if(aqua) {
            int aquaBottomInsets = 21; // unreported insets, shown in screenshot, https://glazedlists.dev.java.net/issues/show_bug.cgi?id=332
            int aquaTopInsets = 22; // for Apple menu bar, found via debugger

            screenInsets.bottom = Math.max(screenInsets.bottom, aquaBottomInsets);
            screenInsets.top = Math.max(screenInsets.top, aquaTopInsets);
        }

        return screenInsets;
    }
}