jdk/src/solaris/classes/sun/awt/X11/XMSelection.java
author duke
Sat, 01 Dec 2007 00:00:00 +0000
changeset 2 90ce3da70b43
child 439 3488710b02f8
permissions -rw-r--r--
Initial load
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * Copyright 2003-2007 Sun Microsystems, Inc.  All Rights Reserved.
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.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.util.logging.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
public class  XMSelection {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
     * A method for a subsytem to express its interest in a certain
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
     * manager selection.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
     * If owner changes, the ownerChanged of the XMSelectionListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
     * will be called with the screen
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
     * number and the new owning window when onwership is established, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
     * None if the owner is gone.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
     * Events in extra_mask are selected for on owning windows (exsiting
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
     * ones and on new owners when established) and otherEvent of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
     * XMWSelectionListener will be called with the screen number and an event.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
     * The function returns an array of current owners.  The size of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
     * array is ScreenCount(awt_display).  The array is "owned" by this
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
     * module and should be considered by the caller as read-only.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    private static Logger log = Logger.getLogger("sun.awt.X11.XMSelection");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    /* Name of the selection */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    String selectionName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    /* list of listeners to be called for events */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    Vector listeners;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    /* X atom array (one per screen) for this selection */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    XAtom atoms[];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    /* Window ids of selection owners */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    long owners[];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    /* event mask to set */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    long eventMask;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    static int numScreens;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    static XAtom XA_MANAGER;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    static HashMap selectionMap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    static {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        long display = XToolkit.getDisplay();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        XToolkit.awtLock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
            numScreens = XlibWrapper.ScreenCount(display);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
            XToolkit.awtUnlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        XA_MANAGER = XAtom.get("MANAGER");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        for (int screen = 0; screen < numScreens ; screen ++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
            initScreen(display,screen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        selectionMap = new HashMap();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    static void initScreen(long display, final int screen) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        XToolkit.awtLock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
            long root = XlibWrapper.RootWindow(display,screen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
            XlibWrapper.XSelectInput(display, root, XlibWrapper.StructureNotifyMask);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
            XToolkit.addEventDispatcher(root,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
                    new XEventDispatcher() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
                        public void dispatchEvent(XEvent ev) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
                                processRootEvent(ev, screen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
                        });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
            XToolkit.awtUnlock();
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    public int getNumberOfScreens() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        return numScreens;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    void select(long extra_mask) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        eventMask = extra_mask;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        for (int screen = 0; screen < numScreens ; screen ++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
            selectPerScreen(screen,extra_mask);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    void resetOwner(long owner, final int screen) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        XToolkit.awtLock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
            long display = XToolkit.getDisplay();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
            synchronized(this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
                setOwner(owner, screen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
                if (log.isLoggable(Level.FINE)) log.fine("New Selection Owner for screen " + screen + " = " + owner );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
                XlibWrapper.XSelectInput(display, owner, XlibWrapper.StructureNotifyMask | eventMask);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
                XToolkit.addEventDispatcher(owner,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
                        new XEventDispatcher() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
                            public void dispatchEvent(XEvent ev) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
                                dispatchSelectionEvent(ev, screen);
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
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
            XToolkit.awtUnlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
    void selectPerScreen(final int screen, long extra_mask) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        XToolkit.awtLock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
                long display = XToolkit.getDisplay();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
                if (log.isLoggable(Level.FINE)) log.fine("Grabbing XServer");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
                XlibWrapper.XGrabServer(display);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
                synchronized(this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
                    String selection_name = getName()+"_S"+screen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
                    if (log.isLoggable(Level.FINE)) log.fine("Screen = " + screen + " selection name = " + selection_name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
                    XAtom atom = XAtom.get(selection_name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
                    selectionMap.put(Long.valueOf(atom.getAtom()),this); // add mapping from atom to the instance of XMSelection
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
                    setAtom(atom,screen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
                    long owner = XlibWrapper.XGetSelectionOwner(display, atom.getAtom());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
                    if (owner != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
                        setOwner(owner, screen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
                        if (log.isLoggable(Level.FINE)) log.fine("Selection Owner for screen " + screen + " = " + owner );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
                        XlibWrapper.XSelectInput(display, owner, XlibWrapper.StructureNotifyMask | extra_mask);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
                        XToolkit.addEventDispatcher(owner,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
                                new XEventDispatcher() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
                                        public void dispatchEvent(XEvent ev) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
                                            dispatchSelectionEvent(ev, screen);
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
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
            catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
                e.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
            finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
                if (log.isLoggable(Level.FINE)) log.fine("UnGrabbing XServer");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
                XlibWrapper.XUngrabServer(XToolkit.getDisplay());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
            XToolkit.awtUnlock();
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
    static boolean processClientMessage(XEvent xev, int screen) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        XClientMessageEvent xce = xev.get_xclient();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        if (xce.get_message_type() == XA_MANAGER.getAtom()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
            if (log.isLoggable(Level.FINE)) log.fine("client messags = " + xce);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
            long timestamp = xce.get_data(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
            long atom = xce.get_data(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
            long owner = xce.get_data(2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
            long data = xce.get_data(3);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
            XMSelection sel = getInstance(atom);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
            if (sel != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
                sel.resetOwner(owner,screen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
                sel.dispatchOwnerChangedEvent(xev,screen,owner,data, timestamp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
    static  boolean processRootEvent(XEvent xev, int screen) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
        switch (xev.get_type()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
            case XlibWrapper.ClientMessage: {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
                return processClientMessage(xev, screen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
        return false;
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
    static XMSelection getInstance(long selection) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
        return (XMSelection) selectionMap.get(Long.valueOf(selection));
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
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
     * Default constructor specifies PropertyChangeMask as well
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
    public XMSelection (String selname) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
        this(selname, XlibWrapper.PropertyChangeMask);
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
   /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
    * Some users may not need to know about selection changes,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
    * just owner ship changes, They would specify a zero extra mask.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
    */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
    public XMSelection (String selname, long extraMask) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
        synchronized (this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
            selectionName = selname;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
            atoms = new XAtom[getNumberOfScreens()];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
            owners = new long[getNumberOfScreens()];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
        select(extraMask);
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
    public synchronized void addSelectionListener(XMSelectionListener listener) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
        if (listeners == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
            listeners = new Vector();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
        listeners.add(listener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
    public synchronized void removeSelectionListener(XMSelectionListener listener) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
        if (listeners != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
            listeners.remove(listener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
    synchronized Collection getListeners() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
        return listeners;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
    synchronized XAtom getAtom(int screen) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
        if (atoms != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
            return atoms[screen];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
    synchronized void setAtom(XAtom a, int screen) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
        if (atoms != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
            atoms[screen] = a;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
    synchronized long getOwner(int screen) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
        if (owners != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
            return owners[screen];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
        return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
    synchronized void setOwner(long owner, int screen) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
        if (owners != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
            owners[screen] = owner;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
    synchronized String getName() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
        return selectionName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
    synchronized void dispatchSelectionChanged( XPropertyEvent ev, int screen) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
        if (log.isLoggable(Level.FINE)) log.fine("Selection Changed : Screen = " + screen + "Event =" + ev);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
        if (listeners != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
            Iterator iter = listeners.iterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
            while (iter.hasNext()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
                XMSelectionListener disp = (XMSelectionListener) iter.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
                disp.selectionChanged(screen, this, ev.get_window(), ev);
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
    synchronized void dispatchOwnerDeath(XDestroyWindowEvent de, int screen) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
        if (log.isLoggable(Level.FINE)) log.fine("Owner dead : Screen = " + screen + "Event =" + de);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
        if (listeners != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
            Iterator iter = listeners.iterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
            while (iter.hasNext()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
                XMSelectionListener disp = (XMSelectionListener) iter.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
                disp.ownerDeath(screen, this, de.get_window());
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
    void dispatchSelectionEvent(XEvent xev, int screen) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
        if (log.isLoggable(Level.FINE)) log.fine("Event =" + xev);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
        if (xev.get_type() == XlibWrapper.DestroyNotify) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
            XDestroyWindowEvent de = xev.get_xdestroywindow();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
            dispatchOwnerDeath( de, screen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
        else if (xev.get_type() == XlibWrapper.PropertyNotify)  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
            XPropertyEvent xpe = xev.get_xproperty();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
            dispatchSelectionChanged( xpe, screen);
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
    synchronized void dispatchOwnerChangedEvent(XEvent ev, int screen, long owner, long data, long timestamp) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
        if (listeners != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
            Iterator iter = listeners.iterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
            while (iter.hasNext()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
                XMSelectionListener disp = (XMSelectionListener) iter.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
                disp.ownerChanged(screen,this, owner, data, timestamp);
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
}