jdk/src/solaris/classes/sun/awt/X11/XMSelection.java
author mchung
Tue, 29 Sep 2009 16:03:03 -0700
changeset 3938 ef327bd847c0
parent 439 3488710b02f8
child 5506 202f599c92aa
permissions -rw-r--r--
6879044: Eliminate the dependency on logging from the AWT/2D/Swing classes Summary: Replace calls to Logger with sun.util.logging.PlatformLogger Reviewed-by: prr, art, alexp, dcherepanov, igor, dav, anthony
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
439
3488710b02f8 6623459: Get rid of XConstant, XProtocolConstants and XUtilConstants antipattern
dav
parents: 2
diff changeset
     2
 * Copyright 2003-2008 Sun Microsystems, Inc.  All Rights Reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * have any questions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
 /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
   * This code is ported to XAWT from MAWT based on awt_mgrsel.c
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
   * code written originally by Valeriy Ushakov
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
   * Author : Bino George
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
   */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
package sun.awt.X11;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.util.*;
3938
ef327bd847c0 6879044: Eliminate the dependency on logging from the AWT/2D/Swing classes
mchung
parents: 439
diff changeset
    35
import sun.util.logging.PlatformLogger;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
public class  XMSelection {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
     * A method for a subsytem to express its interest in a certain
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
     * manager selection.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
     * If owner changes, the ownerChanged of the XMSelectionListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
     * will be called with the screen
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
     * number and the new owning window when onwership is established, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
     * None if the owner is gone.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
     * Events in extra_mask are selected for on owning windows (exsiting
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
     * ones and on new owners when established) and otherEvent of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
     * XMWSelectionListener will be called with the screen number and an event.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
     * The function returns an array of current owners.  The size of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
     * array is ScreenCount(awt_display).  The array is "owned" by this
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
     * module and should be considered by the caller as read-only.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
3938
ef327bd847c0 6879044: Eliminate the dependency on logging from the AWT/2D/Swing classes
mchung
parents: 439
diff changeset
    58
    private static PlatformLogger log = PlatformLogger.getLogger("sun.awt.X11.XMSelection");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    /* Name of the selection */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    String selectionName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    /* list of listeners to be called for events */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    Vector listeners;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    /* X atom array (one per screen) for this selection */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    XAtom atoms[];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    /* Window ids of selection owners */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    long owners[];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    /* event mask to set */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    long eventMask;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    static int numScreens;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    static XAtom XA_MANAGER;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    static HashMap selectionMap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    static {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        long display = XToolkit.getDisplay();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        XToolkit.awtLock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
            numScreens = XlibWrapper.ScreenCount(display);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
            XToolkit.awtUnlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        XA_MANAGER = XAtom.get("MANAGER");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        for (int screen = 0; screen < numScreens ; screen ++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
            initScreen(display,screen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        selectionMap = new HashMap();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    static void initScreen(long display, final int screen) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        XToolkit.awtLock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
            long root = XlibWrapper.RootWindow(display,screen);
439
3488710b02f8 6623459: Get rid of XConstant, XProtocolConstants and XUtilConstants antipattern
dav
parents: 2
diff changeset
   100
            XlibWrapper.XSelectInput(display, root, XConstants.StructureNotifyMask);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
            XToolkit.addEventDispatcher(root,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
                    new XEventDispatcher() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
                        public void dispatchEvent(XEvent ev) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
                                processRootEvent(ev, screen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
                        });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
            XToolkit.awtUnlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    public int getNumberOfScreens() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        return numScreens;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    void select(long extra_mask) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        eventMask = extra_mask;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        for (int screen = 0; screen < numScreens ; screen ++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
            selectPerScreen(screen,extra_mask);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    void resetOwner(long owner, final int screen) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        XToolkit.awtLock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
            long display = XToolkit.getDisplay();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
            synchronized(this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
                setOwner(owner, screen);
3938
ef327bd847c0 6879044: Eliminate the dependency on logging from the AWT/2D/Swing classes
mchung
parents: 439
diff changeset
   131
                if (log.isLoggable(PlatformLogger.FINE)) log.fine("New Selection Owner for screen " + screen + " = " + owner );
439
3488710b02f8 6623459: Get rid of XConstant, XProtocolConstants and XUtilConstants antipattern
dav
parents: 2
diff changeset
   132
                XlibWrapper.XSelectInput(display, owner, XConstants.StructureNotifyMask | eventMask);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
                XToolkit.addEventDispatcher(owner,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
                        new XEventDispatcher() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
                            public void dispatchEvent(XEvent ev) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
                                dispatchSelectionEvent(ev, screen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
                        });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
            XToolkit.awtUnlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
    void selectPerScreen(final int screen, long extra_mask) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        XToolkit.awtLock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
                long display = XToolkit.getDisplay();
3938
ef327bd847c0 6879044: Eliminate the dependency on logging from the AWT/2D/Swing classes
mchung
parents: 439
diff changeset
   151
                if (log.isLoggable(PlatformLogger.FINE)) log.fine("Grabbing XServer");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
                XlibWrapper.XGrabServer(display);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
                synchronized(this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
                    String selection_name = getName()+"_S"+screen;
3938
ef327bd847c0 6879044: Eliminate the dependency on logging from the AWT/2D/Swing classes
mchung
parents: 439
diff changeset
   156
                    if (log.isLoggable(PlatformLogger.FINE)) log.fine("Screen = " + screen + " selection name = " + selection_name);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
                    XAtom atom = XAtom.get(selection_name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
                    selectionMap.put(Long.valueOf(atom.getAtom()),this); // add mapping from atom to the instance of XMSelection
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
                    setAtom(atom,screen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
                    long owner = XlibWrapper.XGetSelectionOwner(display, atom.getAtom());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
                    if (owner != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
                        setOwner(owner, screen);
3938
ef327bd847c0 6879044: Eliminate the dependency on logging from the AWT/2D/Swing classes
mchung
parents: 439
diff changeset
   163
                        if (log.isLoggable(PlatformLogger.FINE)) log.fine("Selection Owner for screen " + screen + " = " + owner );
439
3488710b02f8 6623459: Get rid of XConstant, XProtocolConstants and XUtilConstants antipattern
dav
parents: 2
diff changeset
   164
                        XlibWrapper.XSelectInput(display, owner, XConstants.StructureNotifyMask | extra_mask);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
                        XToolkit.addEventDispatcher(owner,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
                                new XEventDispatcher() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
                                        public void dispatchEvent(XEvent ev) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
                                            dispatchSelectionEvent(ev, screen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
                                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
                                    });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
            catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
                e.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
            finally {
3938
ef327bd847c0 6879044: Eliminate the dependency on logging from the AWT/2D/Swing classes
mchung
parents: 439
diff changeset
   178
                if (log.isLoggable(PlatformLogger.FINE)) log.fine("UnGrabbing XServer");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
                XlibWrapper.XUngrabServer(XToolkit.getDisplay());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
            XToolkit.awtUnlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
    static boolean processClientMessage(XEvent xev, int screen) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        XClientMessageEvent xce = xev.get_xclient();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        if (xce.get_message_type() == XA_MANAGER.getAtom()) {
3938
ef327bd847c0 6879044: Eliminate the dependency on logging from the AWT/2D/Swing classes
mchung
parents: 439
diff changeset
   190
            if (log.isLoggable(PlatformLogger.FINE)) log.fine("client messags = " + xce);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
            long timestamp = xce.get_data(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
            long atom = xce.get_data(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
            long owner = xce.get_data(2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
            long data = xce.get_data(3);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
            XMSelection sel = getInstance(atom);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
            if (sel != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
                sel.resetOwner(owner,screen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
                sel.dispatchOwnerChangedEvent(xev,screen,owner,data, timestamp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
    static  boolean processRootEvent(XEvent xev, int screen) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
        switch (xev.get_type()) {
439
3488710b02f8 6623459: Get rid of XConstant, XProtocolConstants and XUtilConstants antipattern
dav
parents: 2
diff changeset
   207
            case XConstants.ClientMessage: {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
                return processClientMessage(xev, screen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
    static XMSelection getInstance(long selection) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
        return (XMSelection) selectionMap.get(Long.valueOf(selection));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
     * Default constructor specifies PropertyChangeMask as well
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
    public XMSelection (String selname) {
439
3488710b02f8 6623459: Get rid of XConstant, XProtocolConstants and XUtilConstants antipattern
dav
parents: 2
diff changeset
   227
        this(selname, XConstants.PropertyChangeMask);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
   /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
    * Some users may not need to know about selection changes,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
    * just owner ship changes, They would specify a zero extra mask.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
    */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
    public XMSelection (String selname, long extraMask) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
        synchronized (this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
            selectionName = selname;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
            atoms = new XAtom[getNumberOfScreens()];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
            owners = new long[getNumberOfScreens()];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
        select(extraMask);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
    public synchronized void addSelectionListener(XMSelectionListener listener) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
        if (listeners == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
            listeners = new Vector();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
        listeners.add(listener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
    public synchronized void removeSelectionListener(XMSelectionListener listener) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
        if (listeners != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
            listeners.remove(listener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
    synchronized Collection getListeners() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
        return listeners;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
    synchronized XAtom getAtom(int screen) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
        if (atoms != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
            return atoms[screen];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
    synchronized void setAtom(XAtom a, int screen) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
        if (atoms != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
            atoms[screen] = a;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
    synchronized long getOwner(int screen) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
        if (owners != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
            return owners[screen];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
        return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
    synchronized void setOwner(long owner, int screen) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
        if (owners != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
            owners[screen] = owner;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
    synchronized String getName() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
        return selectionName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
    synchronized void dispatchSelectionChanged( XPropertyEvent ev, int screen) {
3938
ef327bd847c0 6879044: Eliminate the dependency on logging from the AWT/2D/Swing classes
mchung
parents: 439
diff changeset
   297
        if (log.isLoggable(PlatformLogger.FINE)) log.fine("Selection Changed : Screen = " + screen + "Event =" + ev);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
        if (listeners != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
            Iterator iter = listeners.iterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
            while (iter.hasNext()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
                XMSelectionListener disp = (XMSelectionListener) iter.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
                disp.selectionChanged(screen, this, ev.get_window(), ev);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
    synchronized void dispatchOwnerDeath(XDestroyWindowEvent de, int screen) {
3938
ef327bd847c0 6879044: Eliminate the dependency on logging from the AWT/2D/Swing classes
mchung
parents: 439
diff changeset
   308
        if (log.isLoggable(PlatformLogger.FINE)) log.fine("Owner dead : Screen = " + screen + "Event =" + de);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
        if (listeners != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
            Iterator iter = listeners.iterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
            while (iter.hasNext()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
                XMSelectionListener disp = (XMSelectionListener) iter.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
                disp.ownerDeath(screen, this, de.get_window());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
    void dispatchSelectionEvent(XEvent xev, int screen) {
3938
ef327bd847c0 6879044: Eliminate the dependency on logging from the AWT/2D/Swing classes
mchung
parents: 439
diff changeset
   320
        if (log.isLoggable(PlatformLogger.FINE)) log.fine("Event =" + xev);
439
3488710b02f8 6623459: Get rid of XConstant, XProtocolConstants and XUtilConstants antipattern
dav
parents: 2
diff changeset
   321
        if (xev.get_type() == XConstants.DestroyNotify) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
            XDestroyWindowEvent de = xev.get_xdestroywindow();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
            dispatchOwnerDeath( de, screen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
        }
439
3488710b02f8 6623459: Get rid of XConstant, XProtocolConstants and XUtilConstants antipattern
dav
parents: 2
diff changeset
   325
        else if (xev.get_type() == XConstants.PropertyNotify)  {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
            XPropertyEvent xpe = xev.get_xproperty();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
            dispatchSelectionChanged( xpe, screen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
    synchronized void dispatchOwnerChangedEvent(XEvent ev, int screen, long owner, long data, long timestamp) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
        if (listeners != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
            Iterator iter = listeners.iterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
            while (iter.hasNext()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
                XMSelectionListener disp = (XMSelectionListener) iter.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
                disp.ownerChanged(screen,this, owner, data, timestamp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
}