1/*
2 *  Licensed to the Apache Software Foundation (ASF) under one or more
3 *  contributor license agreements.  See the NOTICE file distributed with
4 *  this work for additional information regarding copyright ownership.
5 *  The ASF licenses this file to You under the Apache License, Version 2.0
6 *  (the "License"); you may not use this file except in compliance with
7 *  the License.  You may obtain a copy of the License at
8 *
9 *     http://www.apache.org/licenses/LICENSE-2.0
10 *
11 *  Unless required by applicable law or agreed to in writing, software
12 *  distributed under the License is distributed on an "AS IS" BASIS,
13 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 *  See the License for the specific language governing permissions and
15 *  limitations under the License.
16 */
17/**
18 * @author Pavel Dolgov
19 * @version $Revision$
20 */
21package org.apache.harmony.awt;
22
23import java.awt.*;
24
25//???AWT
26//import org.apache.harmony.awt.datatransfer.*;
27import org.apache.harmony.awt.internal.nls.Messages;
28import org.apache.harmony.awt.wtk.*;
29
30
31public final class ContextStorage {
32
33    private static volatile boolean multiContextMode = false;
34    private volatile boolean shutdownPending = false;
35
36    private static final ContextStorage globalContext = new ContextStorage();
37
38    private Toolkit toolkit;
39    private ComponentInternals componentInternals;
40    //???AWT: private DTK dtk;
41    private WTK wtk;
42    private GraphicsEnvironment graphicsEnvironment;
43
44    private class ContextLock {}
45    private final Object contextLock = new ContextLock();
46    private final Synchronizer synchronizer = new Synchronizer();
47
48    public static void activateMultiContextMode() {
49        // TODO: checkPermission
50        multiContextMode = true;
51    }
52
53    public static void setDefaultToolkit(Toolkit newToolkit) {
54        // TODO: checkPermission
55        getCurrentContext().toolkit = newToolkit;
56    }
57
58    public static Toolkit getDefaultToolkit() {
59        return getCurrentContext().toolkit;
60    }
61
62    //???AWT
63    /*
64    public static void setDTK(DTK dtk) {
65        // TODO: checkPermission
66        getCurrentContext().dtk = dtk;
67    }
68
69    public static DTK getDTK() {
70        return getCurrentContext().dtk;
71    }
72    */
73
74    public static Synchronizer getSynchronizer() {
75        return getCurrentContext().synchronizer;
76    }
77
78    public static ComponentInternals getComponentInternals() {
79        return getCurrentContext().componentInternals;
80    }
81
82    static void setComponentInternals(ComponentInternals internals) {
83        // TODO: checkPermission
84        getCurrentContext().componentInternals = internals;
85    }
86
87    public static Object getContextLock() {
88        return getCurrentContext().contextLock;
89    }
90
91    public static WindowFactory getWindowFactory() {
92        return getCurrentContext().wtk.getWindowFactory();
93    }
94
95    public static void setWTK(WTK wtk) {
96        getCurrentContext().wtk = wtk;
97    }
98
99    public static NativeIM getNativeIM() {
100        return getCurrentContext().wtk.getNativeIM();
101    }
102
103    public static NativeEventQueue getNativeEventQueue() {
104        return getCurrentContext().wtk.getNativeEventQueue();
105    }
106
107    public static GraphicsEnvironment getGraphicsEnvironment() {
108        return getCurrentContext().graphicsEnvironment;
109    }
110
111    public static void setGraphicsEnvironment(GraphicsEnvironment environment) {
112        getCurrentContext().graphicsEnvironment = environment;
113    }
114
115    private static ContextStorage getCurrentContext() {
116        return multiContextMode ? getContextThreadGroup().context : globalContext;
117    }
118
119    private static ContextThreadGroup getContextThreadGroup() {
120
121        Thread thread = Thread.currentThread();
122        ThreadGroup group = thread.getThreadGroup();
123        while (group != null) {
124            if (group instanceof ContextThreadGroup) {
125                return (ContextThreadGroup)group;
126            }
127            group = group.getParent();
128        }
129        // awt.59=Application has run out of context thread group
130        throw new RuntimeException(Messages.getString("awt.59")); //$NON-NLS-1$
131    }
132
133    public static boolean shutdownPending() {
134        return getCurrentContext().shutdownPending;
135    }
136
137    void shutdown() {
138        if (!multiContextMode) {
139            return;
140        }
141        shutdownPending = true;
142
143        //???AWT: componentInternals.shutdown();
144
145        synchronized(contextLock) {
146            toolkit = null;
147            componentInternals = null;
148            //???AWT: dtk = null;
149            wtk = null;
150            graphicsEnvironment = null;
151        }
152    }
153
154}
155