~ubuntu-branches/ubuntu/precise/classpath/precise

« back to all changes in this revision

Viewing changes to java/awt/Toolkit.java

  • Committer: Bazaar Package Importer
  • Author(s): Michael Koch
  • Date: 2006-05-27 16:11:15 UTC
  • mfrom: (1.1.3 upstream)
  • Revision ID: james.westby@ubuntu.com-20060527161115-h6e39eposdt5snb6
Tags: 2:0.91-3
* Install header files to /usr/include/classpath.
* debian/control: classpath: Conflict with jamvm < 1.4.3 and
  cacao < 0.96 (Closes: #368172).

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/* Toolkit.java -- AWT Toolkit superclass
2
 
   Copyright (C) 1999, 2000, 2001, 2002, 2003, 2004, 2005
 
2
   Copyright (C) 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006
3
3
   Free Software Foundation, Inc.
4
4
 
5
5
This file is part of GNU Classpath.
39
39
 
40
40
package java.awt;
41
41
 
 
42
import gnu.classpath.SystemProperties;
 
43
import gnu.java.awt.peer.GLightweightPeer;
 
44
 
42
45
import java.awt.datatransfer.Clipboard;
43
46
import java.awt.dnd.DragGestureEvent;
44
47
import java.awt.dnd.DragGestureListener;
46
49
import java.awt.dnd.DragSource;
47
50
import java.awt.dnd.peer.DragSourceContextPeer;
48
51
import java.awt.event.AWTEventListener;
 
52
import java.awt.event.AWTEventListenerProxy;
49
53
import java.awt.event.KeyEvent;
50
54
import java.awt.im.InputMethodHighlight;
51
55
import java.awt.image.ColorModel;
75
79
import java.awt.peer.WindowPeer;
76
80
import java.beans.PropertyChangeListener;
77
81
import java.beans.PropertyChangeSupport;
 
82
import java.io.File;
 
83
import java.io.FileInputStream;
78
84
import java.net.URL;
 
85
import java.security.AccessController;
 
86
import java.security.PrivilegedAction;
 
87
import java.util.ArrayList;
79
88
import java.util.Map;
80
89
import java.util.Properties;
 
90
import java.util.StringTokenizer;
81
91
 
82
92
/**
83
93
 * The AWT system uses a set of native peer objects to implement its
114
124
    = new PropertyChangeSupport(this);
115
125
 
116
126
  /**
 
127
   * All registered AWTEventListener objects. This is package private, so the
 
128
   * event queue can efficiently access this list.
 
129
   */
 
130
  AWTEventListenerProxy[] awtEventListeners;
 
131
 
 
132
  /**
117
133
   * Default constructor for subclasses.
118
134
   */
119
135
  public Toolkit()
120
136
  {
 
137
    awtEventListeners = new AWTEventListenerProxy[0];
121
138
  }
122
139
 
123
140
  /**
349
366
   */
350
367
  protected LightweightPeer createComponent(Component target)
351
368
  {
352
 
    return new gnu.java.awt.peer.GLightweightPeer (target);
 
369
    return new GLightweightPeer(target);
353
370
  }
354
371
 
355
372
  /**
462
479
   */
463
480
  public Insets getScreenInsets(GraphicsConfiguration gc)
464
481
  {
465
 
    return null;
 
482
    return new Insets(0, 0, 0, 0);
466
483
  }
467
484
 
468
485
  /**
514
531
  {
515
532
    if (toolkit != null)
516
533
      return toolkit;
517
 
    String toolkit_name = System.getProperty("awt.toolkit",
518
 
                                             default_toolkit_name);
 
534
    String toolkit_name = SystemProperties.getProperty("awt.toolkit",
 
535
                                                       default_toolkit_name);
519
536
    try
520
537
      {
521
 
        Class cls = Class.forName(toolkit_name);
 
538
        ClassLoader cl;
 
539
        cl = (ClassLoader) AccessController.doPrivileged
 
540
        (new PrivilegedAction()
 
541
          {
 
542
            public Object run()
 
543
              {
 
544
                return ClassLoader.getSystemClassLoader();
 
545
              }
 
546
          });
 
547
        Class cls = cl.loadClass(toolkit_name);
522
548
        Object obj = cls.newInstance();
523
549
        if (!(obj instanceof Toolkit))
524
550
          throw new AWTError(toolkit_name + " is not a subclass of " +
525
551
                             "java.awt.Toolkit");
526
552
        toolkit = (Toolkit) obj;
 
553
 
 
554
        initAccessibility();
527
555
        return toolkit;
528
556
      }
529
557
    catch (ThreadDeath death)
685
713
  public abstract Clipboard getSystemClipboard();
686
714
 
687
715
  /**
688
 
   * Gets the singleton instance of the system selection as a Clipboard object.
689
 
   *
690
 
   * @exception HeadlessException If GraphicsEnvironment.isHeadless() is true.
 
716
   * Gets the singleton instance of the system selection as a
 
717
   * Clipboard object. The system selection contains the selected text
 
718
   * of the last component/widget that had focus and a text selection.
 
719
   * The default implementation returns null.
 
720
   *
 
721
   * @return The Clipboard holding the system (text) selection or null
 
722
   * if the Toolkit or system doesn't support a selection clipboard.
 
723
   *
 
724
   * @exception HeadlessException If GraphicsEnvironment.isHeadless()
 
725
   * is true.
 
726
   * @exception SecurityException If the current security manager
 
727
   * checkSystemClipboardAccess() doesn't allow access.
691
728
   *
692
729
   * @since 1.4
693
730
   */
965
1002
    return desktopPropsSupport.getPropertyChangeListeners(name);
966
1003
  }
967
1004
 
 
1005
  /**
 
1006
   * Adds an AWTEventListener to this toolkit. This listener is informed about
 
1007
   * all events that pass the eventqueue that match the specified
 
1008
   * <code>evenMask</code>. The <code>eventMask</code> is an ORed combination
 
1009
   * of event masks as defined in {@link AWTEvent}.
 
1010
   *
 
1011
   * If a security manager is installed, it is asked first if an
 
1012
   * <code>AWTPermission(&quot;listenToAllAWTEvents&quot;)</code> is allowed.
 
1013
   * This may result in a <code>SecurityException</code> beeing thrown.
 
1014
   *
 
1015
   * It is not recommended to use this kind of notification for normal
 
1016
   * applications. It is intended solely for the purpose of debugging and to
 
1017
   * support special facilities.
 
1018
   *
 
1019
   * @param listener the listener to add
 
1020
   * @param eventMask the event mask of event types which the listener is
 
1021
   *        interested in
 
1022
   *
 
1023
   * @since 1.2
 
1024
   *
 
1025
   * @throws SecurityException if there is a <code>SecurityManager</code> that
 
1026
   *         doesn't grant
 
1027
   *         <code>AWTPermission(&quot;listenToAllAWTEvents&quot;)</code>
 
1028
   *
 
1029
   * @see #getAWTEventListeners()
 
1030
   * @see #getAWTEventListeners(long)
 
1031
   * @see #removeAWTEventListener(AWTEventListener)
 
1032
   */
968
1033
  public void addAWTEventListener(AWTEventListener listener, long eventMask)
969
1034
  {
970
 
    // SecurityManager s = System.getSecurityManager();
971
 
    // if (s != null)
972
 
    //  s.checkPermission(AWTPermission("listenToAllAWTEvents"));
973
 
    // FIXME
 
1035
    // First we must check the security permissions.
 
1036
    SecurityManager s = System.getSecurityManager();
 
1037
    if (s != null)
 
1038
      s.checkPermission(new AWTPermission("listenToAllAWTEvents"));
 
1039
 
 
1040
    // Go through the list and check if the requested listener is already
 
1041
    // registered.
 
1042
    boolean found = false;
 
1043
    for (int i = 0; i < awtEventListeners.length; ++i)
 
1044
      {
 
1045
        AWTEventListenerProxy proxy = awtEventListeners[i];
 
1046
        if (proxy.getListener() == listener)
 
1047
          {
 
1048
            found = true;
 
1049
            // Modify the proxies event mask to include the new event mask.
 
1050
            AWTEventListenerProxy newProxy =
 
1051
              new AWTEventListenerProxy(proxy.getEventMask() | eventMask,
 
1052
                                        listener);
 
1053
            awtEventListeners[i] = newProxy;
 
1054
            break;
 
1055
          }
 
1056
      }
 
1057
 
 
1058
    // If that listener was not found, then add it.
 
1059
    if (! found)
 
1060
      {
 
1061
        AWTEventListenerProxy proxy =
 
1062
          new AWTEventListenerProxy(eventMask, listener);
 
1063
        AWTEventListenerProxy[] newArray =
 
1064
          new AWTEventListenerProxy[awtEventListeners.length + 1];
 
1065
        System.arraycopy(awtEventListeners, 0, newArray, 0,
 
1066
                         awtEventListeners.length);
 
1067
        newArray[newArray.length - 1] = proxy;
 
1068
        awtEventListeners = newArray;
 
1069
      }
974
1070
  }
975
1071
 
 
1072
  /**
 
1073
   * Removes an AWT event listener from this toolkit. This listener is no
 
1074
   * longer informed of any event types it was registered in.
 
1075
   *
 
1076
   * If a security manager is installed, it is asked first if an
 
1077
   * <code>AWTPermission(&quot;listenToAllAWTEvents&quot;)</code> is allowed.
 
1078
   * This may result in a <code>SecurityException</code> beeing thrown.
 
1079
   *
 
1080
   * It is not recommended to use this kind of notification for normal
 
1081
   * applications. It is intended solely for the purpose of debugging and to
 
1082
   * support special facilities.
 
1083
   *
 
1084
   * @param listener the listener to remove
 
1085
   *
 
1086
   * @throws SecurityException if there is a <code>SecurityManager</code> that
 
1087
   *         doesn't grant
 
1088
   *         <code>AWTPermission(&quot;listenToAllAWTEvents&quot;)</code>
 
1089
   *
 
1090
   * @since 1.2
 
1091
   *
 
1092
   * @see #addAWTEventListener(AWTEventListener, long)
 
1093
   * @see #getAWTEventListeners()
 
1094
   * @see #getAWTEventListeners(long)
 
1095
   */
976
1096
  public void removeAWTEventListener(AWTEventListener listener)
977
1097
  {
978
 
    // FIXME
 
1098
    // First we must check the security permissions.
 
1099
    SecurityManager s = System.getSecurityManager();
 
1100
    if (s != null)
 
1101
      s.checkPermission(new AWTPermission("listenToAllAWTEvents"));
 
1102
 
 
1103
 
 
1104
    // Find the index of the listener.
 
1105
    int index = -1;
 
1106
    for (int i = 0; i < awtEventListeners.length; ++i)
 
1107
      {
 
1108
        AWTEventListenerProxy proxy = awtEventListeners[i];
 
1109
        if (proxy.getListener() == listener)
 
1110
          {
 
1111
            index = i;
 
1112
            break;
 
1113
          }
 
1114
      }
 
1115
 
 
1116
    // Copy over the arrays and leave out the removed element.
 
1117
    if (index != -1)
 
1118
      {
 
1119
        AWTEventListenerProxy[] newArray =
 
1120
          new AWTEventListenerProxy[awtEventListeners.length - 1];
 
1121
        if (index > 0)
 
1122
          System.arraycopy(awtEventListeners, 0, newArray, 0, index);
 
1123
        if (index < awtEventListeners.length - 1)
 
1124
          System.arraycopy(awtEventListeners, index + 1, newArray, index,
 
1125
                           awtEventListeners.length - index - 1);
 
1126
        awtEventListeners = newArray;
 
1127
      }
979
1128
  }
980
1129
 
981
1130
  /**
 
1131
   * Returns all registered AWT event listeners. This method returns a copy of
 
1132
   * the listener array, so that application cannot trash the listener list.
 
1133
   *
 
1134
   * If a security manager is installed, it is asked first if an
 
1135
   * <code>AWTPermission(&quot;listenToAllAWTEvents&quot;)</code> is allowed.
 
1136
   * This may result in a <code>SecurityException</code> beeing thrown.
 
1137
   *
 
1138
   * It is not recommended to use this kind of notification for normal
 
1139
   * applications. It is intended solely for the purpose of debugging and to
 
1140
   * support special facilities.
 
1141
   *
 
1142
   * @return all registered AWT event listeners
 
1143
   *
 
1144
   * @throws SecurityException if there is a <code>SecurityManager</code> that
 
1145
   *         doesn't grant
 
1146
   *         <code>AWTPermission(&quot;listenToAllAWTEvents&quot;)</code>
 
1147
   *
982
1148
   * @since 1.4
 
1149
   *
 
1150
   * @see #addAWTEventListener(AWTEventListener, long)
 
1151
   * @see #removeAWTEventListener(AWTEventListener)
 
1152
   * @see #getAWTEventListeners(long)
983
1153
   */
984
1154
  public AWTEventListener[] getAWTEventListeners()
985
1155
  {
986
 
    return null;
 
1156
    // First we must check the security permissions.
 
1157
    SecurityManager s = System.getSecurityManager();
 
1158
    if (s != null)
 
1159
      s.checkPermission(new AWTPermission("listenToAllAWTEvents"));
 
1160
 
 
1161
    // Create a copy of the array.
 
1162
    AWTEventListener[] copy = new AWTEventListener[awtEventListeners.length];
 
1163
    System.arraycopy(awtEventListeners, 0, copy, 0, awtEventListeners.length);
 
1164
    return copy;
987
1165
  }
988
1166
 
989
1167
  /**
 
1168
   * Returns all registered AWT event listeners that listen for events with
 
1169
   * the specified <code>eventMask</code>. This method returns a copy of
 
1170
   * the listener array, so that application cannot trash the listener list.
 
1171
   *
 
1172
   * If a security manager is installed, it is asked first if an
 
1173
   * <code>AWTPermission(&quot;listenToAllAWTEvents&quot;)</code> is allowed.
 
1174
   * This may result in a <code>SecurityException</code> beeing thrown.
 
1175
   *
 
1176
   * It is not recommended to use this kind of notification for normal
 
1177
   * applications. It is intended solely for the purpose of debugging and to
 
1178
   * support special facilities.
 
1179
   *
 
1180
   * @param mask the event mask
 
1181
   *
 
1182
   * @throws SecurityException if there is a <code>SecurityManager</code> that
 
1183
   *         doesn't grant
 
1184
   *         <code>AWTPermission(&quot;listenToAllAWTEvents&quot;)</code>
 
1185
   *
 
1186
   *
990
1187
   * @since 1.4
 
1188
   *
 
1189
   * @see #addAWTEventListener(AWTEventListener, long)
 
1190
   * @see #removeAWTEventListener(AWTEventListener)
 
1191
   * @see #getAWTEventListeners()
991
1192
   */
992
1193
  public AWTEventListener[] getAWTEventListeners(long mask)
993
1194
  {
994
 
    return null;
 
1195
    // First we must check the security permissions.
 
1196
    SecurityManager s = System.getSecurityManager();
 
1197
    if (s != null)
 
1198
      s.checkPermission(new AWTPermission("listenToAllAWTEvents"));
 
1199
 
 
1200
    // Create a copy of the array with only the requested listeners in it.
 
1201
    ArrayList l = new ArrayList(awtEventListeners.length);
 
1202
    for (int i = 0; i < awtEventListeners.length; ++i)
 
1203
      {
 
1204
        if ((awtEventListeners[i].getEventMask() & mask) != 0)
 
1205
          l.add(awtEventListeners[i]);
 
1206
      }
 
1207
 
 
1208
    return (AWTEventListener[] ) l.toArray(new AWTEventListener[l.size()]);
 
1209
  }
 
1210
 
 
1211
 
 
1212
  /**
 
1213
   * Dispatches events to listeners registered to this Toolkit. This is called
 
1214
   * by {@link Component#dispatchEventImpl(AWTEvent)} in order to dispatch
 
1215
   * events globally.
 
1216
   *
 
1217
   * @param ev the event to dispatch
 
1218
   */
 
1219
  void globalDispatchEvent(AWTEvent ev)
 
1220
  {
 
1221
    // We do not use the accessor methods here because they create new
 
1222
    // arrays each time. We must be very efficient, so we access this directly.
 
1223
    for (int i = 0; i < awtEventListeners.length; ++i)
 
1224
      {
 
1225
        AWTEventListenerProxy proxy = awtEventListeners[i];
 
1226
        if ((proxy.getEventMask() & AWTEvent.eventIdToMask(ev.getID())) != 0)
 
1227
          proxy.eventDispatched(ev);
 
1228
      }
995
1229
  }
996
1230
 
997
1231
  /**
998
1232
   * @since 1.3
999
1233
   */
1000
1234
  public abstract Map mapInputMethodHighlight(InputMethodHighlight highlight);
 
1235
 
 
1236
  /**
 
1237
   * Initializes the accessibility framework. In particular, this loads the
 
1238
   * properties javax.accessibility.screen_magnifier_present and
 
1239
   * javax.accessibility.screen_reader_present and loads
 
1240
   * the classes specified in javax.accessibility.assistive_technologies.
 
1241
   */
 
1242
  private static void initAccessibility()
 
1243
  {
 
1244
    AccessController.doPrivileged
 
1245
    (new PrivilegedAction()
 
1246
     {
 
1247
       public Object run()
 
1248
       {
 
1249
         Properties props = new Properties();
 
1250
         String sep = File.separator;
 
1251
 
 
1252
         // Try the user configuration.
 
1253
         try
 
1254
           {
 
1255
             File propsFile = new File(System.getProperty("user.home") + sep
 
1256
                                       + ".accessibility.properties");
 
1257
             FileInputStream in = new FileInputStream(propsFile);
 
1258
             props.load(in);
 
1259
             in.close();
 
1260
           }
 
1261
         catch (Exception ex)
 
1262
           {
 
1263
             // User configuration not present, ignore.
 
1264
           }
 
1265
 
 
1266
         // Try the system configuration if there was no user configuration.
 
1267
         if (props.size() == 0)
 
1268
           {
 
1269
             try
 
1270
               {
 
1271
                 File propsFile =
 
1272
                   new File(System.getProperty("gnu.classpath.home.url")
 
1273
                            + sep + "accessibility.properties");
 
1274
                 FileInputStream in = new FileInputStream(propsFile);
 
1275
                 props.load(in);
 
1276
                 in.close();
 
1277
               }
 
1278
             catch (Exception ex)
 
1279
               {
 
1280
                 // System configuration not present, ignore.
 
1281
               }
 
1282
           }
 
1283
 
 
1284
       // Fetch the screen_magnifier_present property. Check systen properties
 
1285
       // first, then fallback to the configuration file.
 
1286
       String magPresent = SystemProperties.getProperty
 
1287
                              ("javax.accessibility.screen_magnifier_present");
 
1288
       if (magPresent == null)
 
1289
         {
 
1290
           magPresent = props.getProperty("screen_magnifier_present");
 
1291
           if (magPresent != null)
 
1292
             {
 
1293
               SystemProperties.setProperty
 
1294
                 ("javax.accessibility.screen_magnifier_present", magPresent);
 
1295
             }
 
1296
         }
 
1297
 
 
1298
       // Fetch the screen_reader_present property. Check systen properties
 
1299
       // first, then fallback to the configuration file.
 
1300
       String readerPresent = SystemProperties.getProperty
 
1301
                                ("javax.accessibility.screen_reader_present");
 
1302
       if (readerPresent == null)
 
1303
         {
 
1304
           readerPresent = props.getProperty("screen_reader_present");
 
1305
           if (readerPresent != null)
 
1306
             {
 
1307
               SystemProperties.setProperty
 
1308
                 ("javax.accessibility.screen_reader_present", readerPresent);
 
1309
             }
 
1310
         }
 
1311
 
 
1312
       // Fetch the list of classes to be loaded.
 
1313
       String classes = SystemProperties.getProperty
 
1314
         ("javax.accessibility.assistive_technologies");
 
1315
       if (classes == null)
 
1316
         {
 
1317
           classes = props.getProperty("assistive_technologies");
 
1318
           if (classes != null)
 
1319
             {
 
1320
               SystemProperties.setProperty
 
1321
               ("javax.accessibility.assistive_technologies", classes);
 
1322
             }
 
1323
         }
 
1324
 
 
1325
       // Try to load the assisitive_technologies classes.
 
1326
       if (classes != null)
 
1327
         {
 
1328
           ClassLoader cl = ClassLoader.getSystemClassLoader();
 
1329
           StringTokenizer tokenizer = new StringTokenizer(classes, ",");
 
1330
           while (tokenizer.hasMoreTokens())
 
1331
             {
 
1332
               String className = tokenizer.nextToken();
 
1333
               try
 
1334
                 {
 
1335
                   Class atClass = cl.loadClass(className);
 
1336
                   atClass.newInstance();
 
1337
                 }
 
1338
               catch (ClassNotFoundException ex)
 
1339
                 {
 
1340
                   AWTError err = new AWTError("Assistive Technology class not"
 
1341
                                               + " found: " + className);
 
1342
                   err.initCause(ex);
 
1343
                   throw err;
 
1344
                 }
 
1345
               catch (InstantiationException ex)
 
1346
                 {
 
1347
                   AWTError err =
 
1348
                     new AWTError("Assistive Technology class cannot be "
 
1349
                                  + "instantiated: " + className);
 
1350
                   err.initCause(ex);
 
1351
                   throw err;
 
1352
                 }
 
1353
               catch (IllegalAccessException ex)
 
1354
                 {
 
1355
                   AWTError err =
 
1356
                     new AWTError("Assistive Technology class cannot be "
 
1357
                                  + "accessed: " + className);
 
1358
                   err.initCause(err);
 
1359
                   throw err;
 
1360
                 }
 
1361
             }
 
1362
         }
 
1363
       return null;
 
1364
       }
 
1365
     });
 
1366
 
 
1367
  }
 
1368
 
1001
1369
} // class Toolkit