Ejemplo n.º 1
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();
          }
        });
  }
Ejemplo n.º 2
0
  public ServerDialog(OptionsDialog options_, Options opts_, CConn cc_) {

    super(true);
    cc = cc_;
    opts = opts_;

    options = options_;

    JLabel serverLabel = new JLabel("VNC server:", JLabel.RIGHT);
    String valueStr = null;
    if (opts.serverName != null) {
      String[] s = new String[1];
      s[0] = opts.serverName;
      server = new JComboBox(s);
    } else if ((valueStr = UserPreferences.get("ServerDialog", "history")) != null) {
      server = new JComboBox(valueStr.split(","));
    } else {
      server = new JComboBox();
    }

    // Hack to set the left inset on editable JComboBox
    if (UIManager.getLookAndFeel().getID() == "Windows") {
      server.setBorder(
          BorderFactory.createCompoundBorder(
              server.getBorder(), BorderFactory.createEmptyBorder(0, 2, 0, 0)));
    } else if (UIManager.getLookAndFeel().getID() == "Metal") {
      ComboBoxEditor editor = server.getEditor();
      JTextField jtf = (JTextField) editor.getEditorComponent();
      jtf.setBorder(new CompoundBorder(jtf.getBorder(), new EmptyBorder(0, 2, 0, 0)));
    }

    server.setEditable(true);
    editor = server.getEditor();
    editor
        .getEditorComponent()
        .addKeyListener(
            new KeyListener() {
              public void keyTyped(KeyEvent e) {}

              public void keyReleased(KeyEvent e) {}

              public void keyPressed(KeyEvent e) {
                if (e.getKeyCode() == KeyEvent.VK_ENTER) {
                  server.insertItemAt(editor.getItem(), 0);
                  server.setSelectedIndex(0);
                  commit();
                  endDialog();
                }
              }
            });

    topPanel = new JPanel(new GridBagLayout());

    Dialog.addGBComponent(
        new JLabel(VncViewer.logoIcon),
        topPanel,
        0,
        0,
        1,
        1,
        0,
        0,
        0,
        1,
        GridBagConstraints.HORIZONTAL,
        GridBagConstraints.LINE_START,
        new Insets(5, 5, 5, 15));
    Dialog.addGBComponent(
        serverLabel,
        topPanel,
        1,
        0,
        1,
        1,
        0,
        0,
        0,
        1,
        GridBagConstraints.HORIZONTAL,
        GridBagConstraints.LINE_END,
        new Insets(10, 0, 5, 5));
    Dialog.addGBComponent(
        server,
        topPanel,
        2,
        0,
        1,
        1,
        0,
        0,
        1,
        1,
        GridBagConstraints.HORIZONTAL,
        GridBagConstraints.CENTER,
        new Insets(10, 0, 5, 20));

    optionsButton = new JButton("Options...");
    aboutButton = new JButton("About...");
    okButton = new JButton("Connect");
    cancelButton = new JButton("Cancel");
    buttonPanel = new JPanel(new GridBagLayout());
    buttonPanel.setPreferredSize(new Dimension(350, 40));
    Dialog.addGBComponent(
        aboutButton,
        buttonPanel,
        0,
        3,
        1,
        1,
        0,
        0,
        0.8,
        1,
        GridBagConstraints.HORIZONTAL,
        GridBagConstraints.CENTER,
        new Insets(0, 2, 0, 5));
    Dialog.addGBComponent(
        optionsButton,
        buttonPanel,
        1,
        3,
        1,
        1,
        0,
        0,
        1,
        1,
        GridBagConstraints.HORIZONTAL,
        GridBagConstraints.CENTER,
        new Insets(0, 2, 0, 5));
    Dialog.addGBComponent(
        okButton,
        buttonPanel,
        2,
        3,
        1,
        1,
        0,
        0,
        0.7,
        1,
        GridBagConstraints.HORIZONTAL,
        GridBagConstraints.CENTER,
        new Insets(0, 2, 0, 5));
    Dialog.addGBComponent(
        cancelButton,
        buttonPanel,
        3,
        3,
        1,
        1,
        0,
        0,
        0.6,
        1,
        GridBagConstraints.HORIZONTAL,
        GridBagConstraints.CENTER,
        new Insets(0, 2, 0, 5));

    server.addActionListener(this);
    optionsButton.addActionListener(this);
    aboutButton.addActionListener(this);
    okButton.addActionListener(this);
    cancelButton.addActionListener(this);
  }