Ejemplo n.º 1
0
 @Override
 public void keyPressed(KeyEvent e) {
   // Accept "copy" key strokes
   KeyStroke ks = KeyStroke.getKeyStroke(e.getKeyCode(), e.getModifiers());
   JComponent comp = (JComponent) e.getSource();
   for (int i = 0; i < 3; i++) {
     InputMap im = comp.getInputMap(i);
     Object key = im.get(ks);
     if (defaultEditorKitCopyActionName.equals(key)
         || transferHandlerCopyActionName.equals(key)) {
       return;
     }
   }
   // Accept JTable navigation key strokes
   if (!tableNavigationKeys.contains(e.getKeyCode())) {
     e.consume();
   }
 }
Ejemplo n.º 2
0
  public Viewport(CConn cc_) {
    cc = cc_;
    updateTitle();
    setFocusable(false);
    setFocusTraversalKeysEnabled(false);
    setIconImage(VncViewer.frameImage);
    UIManager.getDefaults()
        .put("ScrollPane.ancestorInputMap", new UIDefaults.LazyInputMap(new Object[] {}));
    sp = new JScrollPane();
    sp.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 0));
    sp.getViewport().setBackground(Color.BLACK);
    InputMap im = sp.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
    int ctrlAltShiftMask = Event.SHIFT_MASK | Event.CTRL_MASK | Event.ALT_MASK;
    if (im != null) {
      im.put(KeyStroke.getKeyStroke(KeyEvent.VK_UP, ctrlAltShiftMask), "unitScrollUp");
      im.put(KeyStroke.getKeyStroke(KeyEvent.VK_DOWN, ctrlAltShiftMask), "unitScrollDown");
      im.put(KeyStroke.getKeyStroke(KeyEvent.VK_LEFT, ctrlAltShiftMask), "unitScrollLeft");
      im.put(KeyStroke.getKeyStroke(KeyEvent.VK_RIGHT, ctrlAltShiftMask), "unitScrollRight");
      im.put(KeyStroke.getKeyStroke(KeyEvent.VK_PAGE_UP, ctrlAltShiftMask), "scrollUp");
      im.put(KeyStroke.getKeyStroke(KeyEvent.VK_PAGE_DOWN, ctrlAltShiftMask), "scrollDown");
      im.put(KeyStroke.getKeyStroke(KeyEvent.VK_HOME, ctrlAltShiftMask), "scrollLeft");
      im.put(KeyStroke.getKeyStroke(KeyEvent.VK_END, ctrlAltShiftMask), "scrollRight");
    }
    tb = new Toolbar(cc);
    add(tb, BorderLayout.PAGE_START);
    getContentPane().add(sp);
    if (VncViewer.os.startsWith("mac os x")) {
      macMenu = new MacMenuBar(cc);
      setJMenuBar(macMenu);
      if (VncViewer.getBooleanProperty("turbovnc.lionfs", true)) enableLionFS();
    }
    // NOTE: If Lion FS mode is enabled, then the viewport is only created once
    // as a non-full-screen viewport, so we tell showToolbar() to ignore the
    // full-screen state.
    showToolbar(cc.showToolbar, canDoLionFS);

    addWindowFocusListener(
        new WindowAdapter() {
          public void windowGainedFocus(WindowEvent e) {
            if (sp.getViewport().getView() != null)
              sp.getViewport().getView().requestFocusInWindow();
            if (isVisible() && keyboardTempUngrabbed) {
              vlog.info("Keyboard focus regained. Re-grabbing keyboard.");
              grabKeyboardHelper(true);
              keyboardTempUngrabbed = false;
            }
          }

          public void windowLostFocus(WindowEvent e) {
            if (cc.keyboardGrabbed && isVisible()) {
              vlog.info("Keyboard focus lost. Temporarily ungrabbing keyboard.");
              grabKeyboardHelper(false);
              keyboardTempUngrabbed = true;
            }
          }
        });

    addWindowListener(
        new WindowAdapter() {
          public void windowClosing(WindowEvent e) {
            cc.close();
          }
        });

    addComponentListener(
        new ComponentAdapter() {
          public void componentResized(ComponentEvent e) {
            if (cc.opts.scalingFactor == Options.SCALE_AUTO
                || cc.opts.scalingFactor == Options.SCALE_FIXEDRATIO) {
              if ((sp.getSize().width != cc.desktop.scaledWidth)
                  || (sp.getSize().height != cc.desktop.scaledHeight)) {
                cc.desktop.setScaledSize();
                sp.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
                sp.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_NEVER);
                sp.validate();
                if (getExtendedState() != JFrame.MAXIMIZED_BOTH && !cc.opts.fullScreen) {
                  sp.setSize(new Dimension(cc.desktop.scaledWidth, cc.desktop.scaledHeight));
                  int w = cc.desktop.scaledWidth + VncViewer.insets.left + VncViewer.insets.right;
                  int h = cc.desktop.scaledHeight + VncViewer.insets.top + VncViewer.insets.bottom;
                  if (tb.isVisible()) h += tb.getHeight();
                  if (cc.opts.scalingFactor == Options.SCALE_FIXEDRATIO) setSize(w, h);
                }
              }
            } else if (cc.opts.desktopSize.mode == Options.SIZE_AUTO
                && !cc.firstUpdate
                && !cc.pendingServerResize) {
              Dimension availableSize = cc.viewport.getAvailableSize();
              if (availableSize.width >= 1
                  && availableSize.height >= 1
                  && (availableSize.width != cc.desktop.scaledWidth
                      || availableSize.height != cc.desktop.scaledHeight)) {
                sp.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
                sp.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED);
                sp.validate();
                if (timer != null) timer.stop();
                ActionListener actionListener =
                    new ActionListener() {
                      public void actionPerformed(ActionEvent e) {
                        Dimension availableSize = cc.viewport.getAvailableSize();
                        if (availableSize.width < 1 || availableSize.height < 1)
                          throw new ErrorException("Unexpected zero-size component");
                        cc.sendDesktopSize(availableSize.width, availableSize.height, true);
                      }
                    };
                timer = new Timer(500, actionListener);
                timer.setRepeats(false);
                timer.start();
              }
            } else {
              sp.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
              sp.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED);
              sp.validate();
            }
            if (cc.desktop.cursor != null) {
              Cursor cursor = cc.desktop.cursor;
              if (cursor.hotspot != null)
                // hotspot will be null until the first cursor update is received
                // from the server.
                cc.setCursor(
                    cursor.width(),
                    cursor.height(),
                    cursor.hotspot,
                    (int[]) cursor.data,
                    cursor.mask);
            }
            if (((sp.getSize().width > cc.desktop.scaledWidth)
                    || (sp.getSize().height > cc.desktop.scaledHeight))
                && cc.opts.desktopSize.mode != Options.SIZE_AUTO) {
              int w = sp.getSize().width - adjustWidth;
              int h = sp.getSize().height - adjustHeight;
              dx =
                  (w <= cc.desktop.scaledWidth)
                      ? 0
                      : (int) Math.floor((w - cc.desktop.scaledWidth) / 2);
              dy =
                  (h <= cc.desktop.scaledHeight)
                      ? 0
                      : (int) Math.floor((h - cc.desktop.scaledHeight) / 2);
            } else {
              dx = dy = 0;
            }
            repaint();
          }
        });
  }