OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
180 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
 
16
import org.openconcerto.utils.Platform;
17
 
18
import java.awt.Toolkit;
19
import java.lang.reflect.Field;
20
import java.util.Objects;
21
import java.util.function.Supplier;
22
 
23
public class WindowUtils {
24
    private WindowUtils() {
25
    }
26
 
27
    /**
28
     * Allow to set WM_CLASS for X11 windows.
29
     *
30
     * @param nameSupplier which name to use.
31
     * @param pidSuffix <code>true</code> if the name should be suffixed by the PID of the VM. That
32
     *        way, there is one group of windows per launch.
33
     */
34
    static public void setWMClass(final Supplier<String> nameSupplier, final boolean pidSuffix) {
35
        // Set WM_CLASS to have a meaningful name and avoid grouping together all windows of all VMs
36
        final Class<?> xtoolkit = Toolkit.getDefaultToolkit().getClass();
37
        // https://bugs.openjdk.java.net/browse/JDK-6528430
38
        // need system property to override default WM_CLASS
39
        // #183739 - provide proper app name on Linux
40
        // org/netbeans/core/windows/view/ui/MainWindow.java
41
        if (xtoolkit.getName().equals("sun.awt.X11.XToolkit")) {
42
            try {
43
                final Field awtAppClassName = xtoolkit.getDeclaredField("awtAppClassName");
44
                awtAppClassName.setAccessible(true);
45
 
46
                String pid = null;
47
                // use PID to have one group of windows per launch
48
                if (pidSuffix) {
49
                    try {
50
                        final Platform p = Platform.getInstance();
51
                        if (p.supportsPID())
52
                            pid = p.getPID();
53
                    } catch (Exception e1) {
54
                        e1.printStackTrace();
55
                    }
56
                }
57
                final String suppliedName = Objects.requireNonNull(nameSupplier.get(), "Null name").trim();
58
                if (suppliedName.isEmpty())
59
                    throw new IllegalArgumentException("Empty name");
60
                final String name = suppliedName + (pid == null ? "" : " [" + pid + "]");
61
 
62
                awtAppClassName.set(null, name);
63
            } catch (Exception x) {
64
                x.printStackTrace();
65
            }
66
        }
67
    }
68
}