File: SwingUtils.java

package info (click to toggle)
icedtea-web 1.8.8-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 16,400 kB
  • sloc: java: 101,426; cpp: 8,561; javascript: 4,297; makefile: 2,367; sh: 500; xml: 50
file content (187 lines) | stat: -rw-r--r-- 6,536 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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
/* SwingUtils.java
   Copyright (C) 2018 Red Hat, Inc.

This file is part of IcedTea.

IcedTea is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.

IcedTea is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
General Public License for more details.

You should have received a copy of the GNU General Public License
along with IcedTea; see the file COPYING.  If not, write to the
Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301 USA.

Linking this library statically or dynamically with other modules is
making a combined work based on this library.  Thus, the terms and
conditions of the GNU General Public License cover the whole
combination.

As a special exception, the copyright holders of this library give you
permission to link this library with independent modules to produce an
executable, regardless of the license terms of these independent
modules, and to copy and distribute the resulting executable under
terms of your choice, provided that you also meet, for each linked
independent module, the terms and conditions of the license of that
module.  An independent module is a module which is not derived from
or based on this library.  If you modify this library, you may extend
this exception to your version of the library, but you are not
obligated to do so.  If you do not wish to do so, delete this
exception statement from your version. */
package net.sourceforge.swing;

import java.awt.Component;
import java.awt.EventQueue;
import java.awt.Window;
import java.lang.reflect.InvocationTargetException;
import javax.swing.JWindow;
import javax.swing.RepaintManager;
import javax.swing.SwingUtilities;
import net.sourceforge.jnlp.runtime.Translator;
import net.sourceforge.jnlp.util.logging.OutputController;

/**
 * Swing / AWT utility class
 */
public final class SwingUtils {

    private static final boolean DEBUG_EDT = System.getProperty("icedtea-web.edt.debug", "false").equalsIgnoreCase("true");

    // debugging flags:
    private static final boolean INFO_DIALOG = false;
    private static final boolean TRACE_INVOKE_EDT = false;
    private static final boolean TRACE_TG = false;

    // internals:
    private static boolean DO_SETUP = true;

    /** main thread group (initialized at startup) */
    private static final ThreadGroup MAIN_GROUP = Thread.currentThread().getThreadGroup();

    /* shared Window owner */
    private static Window window = null;

    private SwingUtils() {
        // forbidden
    }

    public static void setup() {
        if (DO_SETUP) {
            DO_SETUP = false; // avoid reentrance

            if (DEBUG_EDT) {
                trace("Using ThreadCheckingRepaintManager");
                RepaintManager.setCurrentManager(new ThreadCheckingRepaintManager());
            }
        }
    }

    static void trace(final String msg) {
        // Use System.err directly for debuging EDT without any conflict with console / logging system
        System.err.println(msg);
    }

    static void traceWithStack(final String msg) {
        trace(msg);
        new Throwable().printStackTrace();
    }

    public static void info(final Window dialog) {
        if (INFO_DIALOG) {
            trace("Dialog[" + dialog.getName() + "]"
                    + " in TG [" + Thread.currentThread().getThreadGroup() + "]");
            checkEDT();
        }
    }

    public static void checkEDT() {
        if (!isEventDispatchThread()) {
            OutputController.getLogger().log(OutputController.Level.ERROR_ALL, new Exception("EDT violation"));
        }
    }

    private static boolean isMainThreadGroup() {
        final Thread t = Thread.currentThread();
        final ThreadGroup g = t.getThreadGroup();

        if (g != MAIN_GROUP) {
            if (TRACE_TG) {
                traceWithStack("----------\ncheckThreadGroup: " + t);
            }
            return false;
        }
        return true;
    }

    // --- SwingUtilities wrapper ---
    public static Window getWindowAncestor(Component c) {
        return SwingUtilities.getWindowAncestor(c);
    }

    public static boolean isEventDispatchThread() {
        return EventQueue.isDispatchThread();
    }

    public static void invokeLater(final Runnable doRun) {
        if (isMainThreadGroup()) {
            if (TRACE_INVOKE_EDT && isEventDispatchThread()) {
                traceWithStack("invokeLater() from EDT: MAY be fixed (useless) ?");
            }
        }
        EventQueue.invokeLater(doRun);
    }

    public static void invokeRunnableOrEnqueueLater(final Runnable runnable) {
        if (isEventDispatchThread()) {
            runnable.run();
        } else {
            invokeLater(runnable);
        }
    }

    public static void callOnAppContext(final Runnable doRun) throws InterruptedException, InvocationTargetException {
        EventQueue.invokeAndWait(doRun);
    }

    public static void invokeAndWait(final Runnable doRun) {
        if (isEventDispatchThread()) {
            if (TRACE_INVOKE_EDT) {
                traceWithStack("invokeAndWait() from EDT: to be fixed (illegal) ?");
            }
            // Direct invocation:
            doRun.run();
        } else {
            try {
                callOnAppContext(doRun);
            } catch (InterruptedException ie) {
                OutputController.getLogger().log(OutputController.Level.ERROR_ALL, ie);
            } catch (InvocationTargetException ite) {
                OutputController.getLogger().log(OutputController.Level.ERROR_ALL, ite);
            }
        }
    }

    public static synchronized Window getOrCreateWindowOwner() {
        if (window == null) {
            invokeAndWait(new Runnable() {
                @Override
                public void run() {
                    try {
                        window = new JWindow().getOwner();
                        window.setName("getOrCreateWindowOwner");
                    } catch (Exception ex) {
                        OutputController.getLogger().log(ex);
                        OutputController.getLogger().log(OutputController.Level.MESSAGE_ALL, Translator.R("HEADLESS_MISSCONFIGURED"));
                    }
                }
            });
        }
        return window;
    }
}