下面列出了怎么用sun.awt.AppContext的API类实例代码及写法,或者点击链接到github查看源代码。
private static Dimension[] getIconSizesMap() {
AppContext appContext = AppContext.getAppContext();
Dimension[] iconSizes = (Dimension[])appContext.get(ICON_SIZE_KEY);
if (iconSizes == null) {
iconSizes = new Dimension[7];
iconSizes[0] = null; // GTK_ICON_SIZE_INVALID
iconSizes[1] = new Dimension(16, 16); // GTK_ICON_SIZE_MENU
iconSizes[2] = new Dimension(18, 18); // GTK_ICON_SIZE_SMALL_TOOLBAR
iconSizes[3] = new Dimension(24, 24); // GTK_ICON_SIZE_LARGE_TOOLBAR
iconSizes[4] = new Dimension(20, 20); // GTK_ICON_SIZE_BUTTON
iconSizes[5] = new Dimension(32, 32); // GTK_ICON_SIZE_DND
iconSizes[6] = new Dimension(48, 48); // GTK_ICON_SIZE_DIALOG
appContext.put(ICON_SIZE_KEY, iconSizes);
}
return iconSizes;
}
void dispatch(final _NativeEvent event, final Object... args) {
// grab a local ref to the listeners and its contexts as an array of the map's entries
final ArrayList<Map.Entry<L, AppContext>> localEntries;
synchronized (this) {
if (listenerToAppContext.size() == 0) {
return;
}
localEntries = new ArrayList<Map.Entry<L, AppContext>>(listenerToAppContext.size());
localEntries.addAll(listenerToAppContext.entrySet());
}
for (final Map.Entry<L, AppContext> e : localEntries) {
final L listener = e.getKey();
final AppContext listenerContext = e.getValue();
SunToolkit.invokeLaterOnAppContext(listenerContext, new Runnable() {
public void run() {
performOnListener(listener, event);
}
});
}
}
private synchronized void initContext() {
final AppContext context = AppContext.getAppContext();
if (contentsContext != context) {
// Need to synchronize on the AppContext to guarantee that it cannot
// be disposed after the check, but before the listener is added.
synchronized (context) {
if (context.isDisposed()) {
throw new IllegalStateException("Can't set contents from disposed AppContext");
}
context.addPropertyChangeListener
(AppContext.DISPOSED_PROPERTY_NAME, this);
}
if (contentsContext != null) {
contentsContext.removePropertyChangeListener
(AppContext.DISPOSED_PROPERTY_NAME, this);
}
contentsContext = context;
}
}
public synchronized void removeFlavorListener(FlavorListener listener) {
if (listener == null) {
return;
}
AppContext appContext = AppContext.getAppContext();
EventListenerAggregate contextFlavorListeners = (EventListenerAggregate)
appContext.get(CLIPBOARD_FLAVOR_LISTENER_KEY);
if (contextFlavorListeners == null){
//else we throw NullPointerException, but it is forbidden
return;
}
if (contextFlavorListeners.remove(listener) &&
--numberOfFlavorListeners == 0) {
unregisterClipboardViewerChecked();
currentFormats = null;
}
}
/**
* Removes the specified <code>TrayIcon</code> from the
* <code>SystemTray</code>.
*
* <p> All icons added by the application are automatically
* removed from the <code>SystemTray</code> upon application exit
* and also when the desktop system tray becomes unavailable.
*
* <p> If <code>trayIcon</code> is <code>null</code> or was not
* added to the system tray, no exception is thrown and no action
* is performed.
*
* @param trayIcon the <code>TrayIcon</code> to be removed
* @see #add(TrayIcon)
* @see TrayIcon
*/
public void remove(TrayIcon trayIcon) {
if (trayIcon == null) {
return;
}
TrayIcon[] oldArray = null, newArray = null;
synchronized (this) {
oldArray = systemTray.getTrayIcons();
Vector<TrayIcon> icons = (Vector<TrayIcon>)AppContext.getAppContext().get(TrayIcon.class);
// TrayIcon with no peer is not contained in the array.
if (icons == null || !icons.remove(trayIcon)) {
return;
}
trayIcon.removeNotify();
newArray = systemTray.getTrayIcons();
}
firePropertyChange("trayIcons", oldArray, newArray);
}
private static Dimension[] getIconSizesMap() {
AppContext appContext = AppContext.getAppContext();
Dimension[] iconSizes = (Dimension[])appContext.get(ICON_SIZE_KEY);
if (iconSizes == null) {
iconSizes = new Dimension[7];
iconSizes[0] = null; // GTK_ICON_SIZE_INVALID
iconSizes[1] = new Dimension(16, 16); // GTK_ICON_SIZE_MENU
iconSizes[2] = new Dimension(18, 18); // GTK_ICON_SIZE_SMALL_TOOLBAR
iconSizes[3] = new Dimension(24, 24); // GTK_ICON_SIZE_LARGE_TOOLBAR
iconSizes[4] = new Dimension(20, 20); // GTK_ICON_SIZE_BUTTON
iconSizes[5] = new Dimension(32, 32); // GTK_ICON_SIZE_DND
iconSizes[6] = new Dimension(48, 48); // GTK_ICON_SIZE_DIALOG
appContext.put(ICON_SIZE_KEY, iconSizes);
}
return iconSizes;
}
void dispatch(final _NativeEvent event, final Object... args) {
// grab a local ref to the listeners and its contexts as an array of the map's entries
final ArrayList<Map.Entry<L, AppContext>> localEntries;
synchronized (this) {
if (listenerToAppContext.size() == 0) {
return;
}
localEntries = new ArrayList<Map.Entry<L, AppContext>>(listenerToAppContext.size());
localEntries.addAll(listenerToAppContext.entrySet());
}
for (final Map.Entry<L, AppContext> e : localEntries) {
final L listener = e.getKey();
final AppContext listenerContext = e.getValue();
SunToolkit.invokeLaterOnAppContext(listenerContext, new Runnable() {
public void run() {
performOnListener(listener, event);
}
});
}
}
/**
* Returns the ideal font style for the font identified by key.
*/
static int getDefaultFontStyle(int key) {
if (key != WINDOW_TITLE_FONT) {
Object boldMetal = null;
if (AppContext.getAppContext().get(
SwingUtilities2.LAF_STATE_KEY) != null) {
// Only access the boldMetal key if a look and feel has
// been loaded, otherwise we'll trigger loading the look
// and feel.
boldMetal = UIManager.get("swing.boldMetal");
}
if (boldMetal != null) {
if (Boolean.FALSE.equals(boldMetal)) {
return Font.PLAIN;
}
}
else if (PLAIN_FONTS) {
return Font.PLAIN;
}
}
return fontStyles[key];
}
/**
* Returns the default menu selection manager.
*
* @return a MenuSelectionManager object
*/
public static MenuSelectionManager defaultManager() {
synchronized (MENU_SELECTION_MANAGER_KEY) {
AppContext context = AppContext.getAppContext();
MenuSelectionManager msm = (MenuSelectionManager)context.get(
MENU_SELECTION_MANAGER_KEY);
if (msm == null) {
msm = new MenuSelectionManager();
context.put(MENU_SELECTION_MANAGER_KEY, msm);
// installing additional listener if found in the AppContext
Object o = context.get(SwingUtilities2.MENU_SELECTION_MANAGER_LISTENER_KEY);
if (o != null && o instanceof ChangeListener) {
msm.addChangeListener((ChangeListener) o);
}
}
return msm;
}
}
/**
* Removes the specified <code>TrayIcon</code> from the
* <code>SystemTray</code>.
*
* <p> All icons added by the application are automatically
* removed from the <code>SystemTray</code> upon application exit
* and also when the desktop system tray becomes unavailable.
*
* <p> If <code>trayIcon</code> is <code>null</code> or was not
* added to the system tray, no exception is thrown and no action
* is performed.
*
* @param trayIcon the <code>TrayIcon</code> to be removed
* @see #add(TrayIcon)
* @see TrayIcon
*/
public void remove(TrayIcon trayIcon) {
if (trayIcon == null) {
return;
}
TrayIcon[] oldArray = null, newArray = null;
synchronized (this) {
oldArray = systemTray.getTrayIcons();
Vector<TrayIcon> icons = (Vector<TrayIcon>)AppContext.getAppContext().get(TrayIcon.class);
// TrayIcon with no peer is not contained in the array.
if (icons == null || !icons.remove(trayIcon)) {
return;
}
trayIcon.removeNotify();
newArray = systemTray.getTrayIcons();
}
firePropertyChange("trayIcons", oldArray, newArray);
}
private static void scheduleDisplayChanges() {
// To avoid threading problems, we notify each RepaintManager
// on the thread it was created on.
for (AppContext context : AppContext.getAppContexts()) {
synchronized(context) {
if (!context.isDisposed()) {
EventQueue eventQueue = (EventQueue)context.get(
AppContext.EVENT_QUEUE_KEY);
if (eventQueue != null) {
eventQueue.postEvent(new InvocationEvent(
Toolkit.getDefaultToolkit(),
new DisplayChangedRunnable()));
}
}
}
}
}
/**
* Returns the ideal font style for the font identified by key.
*/
static int getDefaultFontStyle(int key) {
if (key != WINDOW_TITLE_FONT) {
Object boldMetal = null;
if (AppContext.getAppContext().get(
SwingUtilities2.LAF_STATE_KEY) != null) {
// Only access the boldMetal key if a look and feel has
// been loaded, otherwise we'll trigger loading the look
// and feel.
boldMetal = UIManager.get("swing.boldMetal");
}
if (boldMetal != null) {
if (Boolean.FALSE.equals(boldMetal)) {
return Font.PLAIN;
}
}
else if (PLAIN_FONTS) {
return Font.PLAIN;
}
}
return fontStyles[key];
}
private static HashMap<String,Keymap> getKeymapTable() {
synchronized (KEYMAP_TABLE) {
AppContext appContext = AppContext.getAppContext();
HashMap<String,Keymap> keymapTable =
(HashMap<String,Keymap>)appContext.get(KEYMAP_TABLE);
if (keymapTable == null) {
keymapTable = new HashMap<String,Keymap>(17);
appContext.put(KEYMAP_TABLE, keymapTable);
//initialize default keymap
Keymap binding = addKeymap(DEFAULT_KEYMAP, null);
binding.setDefaultAction(new
DefaultEditorKit.DefaultKeyTypedAction());
}
return keymapTable;
}
}
/**
* Checks change of the {@code DataFlavor}s and, if necessary,
* posts notifications on {@code FlavorEvent}s to the
* AppContexts' EDTs.
* The parameter {@code formats} is null iff we have just
* failed to get formats available on the clipboard.
*
* @param formats data formats that have just been retrieved from
* this clipboard
*/
protected final void checkChange(final long[] formats) {
if (Arrays.equals(formats, currentFormats)) {
// we've been able to successfully get available on the clipboard
// DataFlavors this and previous time and they are coincident;
// don't notify
return;
}
currentFormats = formats;
for (final AppContext appContext : AppContext.getAppContexts()) {
if (appContext == null || appContext.isDisposed()) {
continue;
}
Set<FlavorListener> flavorListeners = getFlavorListeners(appContext);
if (flavorListeners != null) {
for (FlavorListener listener : flavorListeners) {
if (listener != null) {
PeerEvent peerEvent = new PeerEvent(this,
() -> listener.flavorsChanged(new FlavorEvent(SunClipboard.this)),
PeerEvent.PRIORITY_EVENT);
SunToolkit.postEvent(appContext, peerEvent);
}
}
}
}
}
public static ComponentUI createUI(JComponent b) {
AppContext appContext = AppContext.getAppContext();
BasicToggleButtonUI toggleButtonUI =
(BasicToggleButtonUI) appContext.get(BASIC_TOGGLE_BUTTON_UI_KEY);
if (toggleButtonUI == null) {
toggleButtonUI = new BasicToggleButtonUI();
appContext.put(BASIC_TOGGLE_BUTTON_UI_KEY, toggleButtonUI);
}
return toggleButtonUI;
}
/**
* Tests if a client can get access to the AWT event queue.
* <p>
* This method calls <code>checkPermission</code> with the
* <code>AWTPermission("accessEventQueue")</code> permission.
*
* @since JDK1.1
* @exception SecurityException if the caller does not have
* permission to access the AWT event queue.
*/
public void checkAwtEventQueueAccess() {
AppContext appContext = AppContext.getAppContext();
AppletClassLoader appletClassLoader = currentAppletClassLoader();
if (AppContext.isMainContext(appContext) && (appletClassLoader != null)) {
// If we're about to allow access to the main EventQueue,
// and anything untrusted is on the class context stack,
// disallow access.
super.checkPermission(SecurityConstants.AWT.CHECK_AWT_EVENTQUEUE_PERMISSION);
}
}
SentEvent(AWTEvent nested, AppContext toNotify) {
super((nested != null)
? nested.getSource()
: Toolkit.getDefaultToolkit(),
ID);
this.nested = nested;
this.toNotify = toNotify;
}
private static DragListener getDragListener() {
synchronized(DragListener.class) {
DragListener listener =
(DragListener)AppContext.getAppContext().
get(DragListener.class);
if (listener == null) {
listener = new DragListener();
AppContext.getAppContext().put(DragListener.class, listener);
}
return listener;
}
}
static FetcherInfo getFetcherInfo() {
AppContext appContext = AppContext.getAppContext();
synchronized(appContext) {
FetcherInfo info = (FetcherInfo)appContext.get(FETCHER_INFO_KEY);
if (info == null) {
info = new FetcherInfo();
appContext.put(FETCHER_INFO_KEY, info);
}
return info;
}
}
private TrayIcon()
throws UnsupportedOperationException, HeadlessException, SecurityException
{
SystemTray.checkSystemTrayAllowed();
if (GraphicsEnvironment.isHeadless()) {
throw new HeadlessException();
}
if (!SystemTray.isSupported()) {
throw new UnsupportedOperationException();
}
SunToolkit.insertTargetMapping(this, AppContext.getAppContext());
}
public static ComponentUI createUI(JComponent c) {
AppContext appContext = AppContext.getAppContext();
WindowsLabelUI windowsLabelUI =
(WindowsLabelUI) appContext.get(WINDOWS_LABEL_UI_KEY);
if (windowsLabelUI == null) {
windowsLabelUI = new WindowsLabelUI();
appContext.put(WINDOWS_LABEL_UI_KEY, windowsLabelUI);
}
return windowsLabelUI;
}
@Override
public synchronized void removePropertyChangeListener(
String propertyName,
PropertyChangeListener listener)
{
PropertyChangeSupport pcs = (PropertyChangeSupport)
AppContext.getAppContext().get(PROP_CHANGE_SUPPORT_KEY);
if (null != pcs) {
pcs.removePropertyChangeListener(propertyName, listener);
}
}
public static ComponentUI createUI(JComponent c) {
if (System.getSecurityManager() != null) {
AppContext appContext = AppContext.getAppContext();
MetalLabelUI safeMetalLabelUI =
(MetalLabelUI) appContext.get(METAL_LABEL_UI_KEY);
if (safeMetalLabelUI == null) {
safeMetalLabelUI = new MetalLabelUI();
appContext.put(METAL_LABEL_UI_KEY, safeMetalLabelUI);
}
return safeMetalLabelUI;
}
return metalLabelUI;
}
/**
* Returns the default FlavorMap for this thread's ClassLoader.
*/
public static FlavorMap getDefaultFlavorMap() {
AppContext context = AppContext.getAppContext();
FlavorMap fm = (FlavorMap) context.get(FLAVOR_MAP_KEY);
if (fm == null) {
fm = new SystemFlavorMap();
context.put(FLAVOR_MAP_KEY, fm);
}
return fm;
}
void uninstall() {
synchronized (MOUSE_GRABBER_KEY) {
MenuSelectionManager.defaultManager().removeChangeListener(this);
ungrabWindow();
AppContext.getAppContext().remove(MOUSE_GRABBER_KEY);
}
}
@Override
public synchronized void removePropertyChangeListener(
String propertyName,
PropertyChangeListener listener)
{
PropertyChangeSupport pcs = (PropertyChangeSupport)
AppContext.getAppContext().get(PROP_CHANGE_SUPPORT_KEY);
if (null != pcs) {
pcs.removePropertyChangeListener(propertyName, listener);
}
}
private void showInputMethodMenuOnRequesterEDT(Component requester)
throws InterruptedException, InvocationTargetException {
if (requester == null){
return;
}
class AWTInvocationLock {}
Object lock = new AWTInvocationLock();
InvocationEvent event =
new InvocationEvent(requester,
new Runnable() {
public void run() {
showInputMethodMenu();
}
},
lock,
true);
AppContext requesterAppContext = SunToolkit.targetToAppContext(requester);
synchronized (lock) {
SunToolkit.postEvent(requesterAppContext, event);
while (!event.isDispatched()) {
lock.wait();
}
}
Throwable eventThrowable = event.getThrowable();
if (eventThrowable != null) {
throw new InvocationTargetException(eventThrowable);
}
}
protected static ArrayCache getArrayCache() {
ArrayCache cache = (ArrayCache)AppContext.getAppContext().get(ArrayCache.class);
if (cache == null){
cache = new ArrayCache();
AppContext.getAppContext().put(ArrayCache.class,cache);
}
return cache;
}
@Override
public synchronized PropertyChangeListener[] getPropertyChangeListeners()
{
PropertyChangeSupport pcs = (PropertyChangeSupport)
AppContext.getAppContext().get(PROP_CHANGE_SUPPORT_KEY);
if (null != pcs) {
return pcs.getPropertyChangeListeners();
} else {
return new PropertyChangeListener[0];
}
}
/**
* Get the most recent event time in the {@code EventQueue} which the {@code source}
* belongs to.
*
* @param source the source of the event
* @exception IllegalArgumentException if source is null.
* @return most recent event time in the {@code EventQueue}
*/
private static long getMostRecentEventTimeForSource(Object source) {
if (source == null) {
// throw the IllegalArgumentException to conform to EventObject spec
throw new IllegalArgumentException("null source");
}
AppContext appContext = SunToolkit.targetToAppContext(source);
EventQueue eventQueue = SunToolkit.getSystemEventQueueImplPP(appContext);
return AWTAccessor.getEventQueueAccessor().getMostRecentEventTime(eventQueue);
}