OpenConcerto

Dépôt officiel du code source de l'ERP OpenConcerto
sonarqube

svn://code.openconcerto.org/openconcerto

Rev

Rev 73 | Rev 94 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
17 ilm 1
/*
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3
 *
4
 * Copyright 2011 OpenConcerto, by ILM Informatique. All rights reserved.
5
 *
6
 * The contents of this file are subject to the terms of the GNU General Public License Version 3
7
 * only ("GPL"). You may not use this file except in compliance with the License. You can obtain a
8
 * copy of the License at http://www.gnu.org/licenses/gpl-3.0.html See the License for the specific
9
 * language governing permissions and limitations under the License.
10
 *
11
 * When distributing the software, include this License Header Notice in each file.
12
 */
13
 
14
 package org.openconcerto.ui;
15
 
83 ilm 16
import org.openconcerto.utils.OSFamily;
17
 
17 ilm 18
import java.awt.Dimension;
19
import java.awt.Frame;
83 ilm 20
import java.awt.GraphicsConfiguration;
21
import java.awt.GraphicsDevice;
73 ilm 22
import java.awt.GraphicsEnvironment;
23
import java.awt.Rectangle;
17 ilm 24
import java.awt.Window;
25
 
26
import javax.swing.UIManager;
27
import javax.swing.UIManager.LookAndFeelInfo;
28
 
29
public class FrameUtil {
30
 
31
    public static void show(final Window frame) {
32
        frame.setVisible(true);
33
        if (frame instanceof Frame)
34
            ((Frame) frame).setState(Frame.NORMAL);
35
        frame.toFront();
36
    }
37
 
38
    // only available from sun's release 6u10
39
    public static String getNimbusClassName() {
40
        // http://java.sun.com/javase/6/docs/technotes/guides/jweb/otherFeatures/nimbus_laf.html
41
        for (LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
42
            if ("Nimbus".equals(info.getName())) {
43
                return info.getClassName();
44
            }
45
        }
46
        return null;
47
    }
48
 
49
    public static void showPacked(Frame frame) {
50
        frame.pack();
51
        frame.setMinimumSize(new Dimension(frame.getWidth(), frame.getHeight()));
52
        FrameUtil.show(frame);
53
    }
54
 
73 ilm 55
    public static final void setBounds(Window w) {
56
        w.setBounds(getWindowBounds());
57
    }
58
 
83 ilm 59
    private static final int getArea(final GraphicsConfiguration gc) {
60
        final Rectangle dm = gc.getBounds();
61
        return dm.width * dm.height;
62
    }
63
 
73 ilm 64
    public static final Rectangle getWindowBounds() {
65
        final GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
83 ilm 66
        final Rectangle dm;
67
        // see https://bugs.launchpad.net/ubuntu/+source/openjdk-7/+bug/1171563
68
        // Incorrectly calculated screen insets, e.g. Insets[top=24,left=1345,bottom=0,right=0] for
69
        // Toolkit.getDefaultToolkit().getScreenInsets(GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().getDefaultConfiguration());
70
        // see https://bugs.openjdk.java.net/browse/JDK-8020443
71
        final GraphicsDevice[] screens = ge.getScreenDevices();
72
        if (screens.length > 1 && OSFamily.getInstance() == OSFamily.Linux) {
73
            GraphicsConfiguration largest = null;
74
            for (final GraphicsDevice screen : screens) {
75
                final GraphicsConfiguration current = screen.getDefaultConfiguration();
76
                if (largest == null || getArea(current) > getArea(largest))
77
                    largest = current;
78
            }
79
            assert largest != null;
80
            dm = new Rectangle(largest.getBounds());
81
        } else {
82
            dm = new Rectangle(ge.getMaximumWindowBounds());
83
        }
73 ilm 84
        // don't use ge.getDefaultScreenDevice().getDisplayMode(); sometimes null in NX
85
        // see http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6921661
86
 
87
        // Always leave some space to see other windows (plus at least on Ubuntu with
83 ilm 88
        // 1.6.0_24 dm is the screen size ; also with the getMaximumWindowBounds() workaround)
73 ilm 89
        dm.grow(dm.getWidth() <= 800 ? -20 : -50, dm.getHeight() <= 600 ? -20 : -50);
90
 
91
        return dm;
92
    }
83 ilm 93
 
94
    // easily debug systems
95
    public static void main(String[] args) {
96
        System.out.println("getWindowBounds(): " + getWindowBounds());
97
    }
17 ilm 98
}