static {
   Clipboard systemClipboard = null;
   try {
     // if we don't have access to the system clipboard, will throw
     // a security exception
     SecurityManager mgr = System.getSecurityManager();
     if (mgr != null) {
       mgr.checkSystemClipboardAccess();
     }
     systemClipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
   } catch (SecurityException e) {
     // means we can't get to system clipboard, so create app level one
     systemClipboard = new Clipboard("UIResourceMgr");
   } catch (Exception e) {
     e.printStackTrace();
   }
   SYSTEM_CLIPBOARD = systemClipboard;
 }
Пример #2
0
  private boolean canAccessSystemClipboard() {
    boolean b = false;

    if (!GraphicsEnvironment.isHeadless()) {
      SecurityManager sm = System.getSecurityManager();
      if (sm != null) {
        try {
          sm.checkSystemClipboardAccess();
          b = true;
        } catch (SecurityException se) {
          if (log.isLoggable(Level.FINE)) {
            log.log(Level.FINE, "InputEvent.canAccessSystemClipboard() got SecurityException ", se);
          }
        }
      } else {
        b = true;
      }
    }

    return b;
  }
Пример #3
0
    public void selectionDone(SelectionEvent evt) {
      if (!useUnixTextSelection) return;

      Object o = evt.getSelection();
      if (!(o instanceof CharacterIterator)) return;
      CharacterIterator iter = (CharacterIterator) o;

      // first see if we can access the clipboard
      SecurityManager securityManager;
      securityManager = System.getSecurityManager();
      if (securityManager != null) {
        try {
          securityManager.checkSystemClipboardAccess();
        } catch (SecurityException e) {
          return; // Can't access clipboard.
        }
      }

      int sz = iter.getEndIndex() - iter.getBeginIndex();
      if (sz == 0) return;

      char[] cbuff = new char[sz];
      cbuff[0] = iter.first();
      for (int i = 1; i < cbuff.length; ++i) {
        cbuff[i] = iter.next();
      }
      final String strSel = new String(cbuff);
      // HACK: getSystemClipboard sometimes deadlocks on
      // linux when called from the AWT Thread. The Thread
      // creation prevents that.
      new Thread() {
        public void run() {
          Clipboard cb;
          cb = Toolkit.getDefaultToolkit().getSystemClipboard();
          StringSelection sel;
          sel = new StringSelection(strSel);
          cb.setContents(sel, sel);
        }
      }.start();
    }
 @Override
 public void checkSystemClipboardAccess() {
   if (finalSecurityManager != null) finalSecurityManager.checkSystemClipboardAccess();
 }