From 4a95c7f69e8b308612c8fde4c748f148cdde6598 Mon Sep 17 00:00:00 2001 From: Brian Hinz Date: Sat, 1 Sep 2012 19:24:26 +0000 Subject: [PATCH] Forces all dialogs except for the clipboard to be modal. Fixes cases where dialogs are not brought to the front while in fullscreen mode. Corrects a few corner cases where viewer did not exit properly. git-svn-id: svn://svn.code.sf.net/p/tigervnc/code/trunk@4988 3789f03b-4d11-0410-bbf8-ca57d06f2519 --- java/com/tigervnc/vncviewer/CConn.java | 20 ++++-- .../tigervnc/vncviewer/ClipboardDialog.java | 2 - java/com/tigervnc/vncviewer/Dialog.java | 71 +++++-------------- java/com/tigervnc/vncviewer/F8Menu.java | 6 ++ .../com/tigervnc/vncviewer/OptionsDialog.java | 5 +- java/com/tigervnc/vncviewer/PasswdDialog.java | 10 +-- java/com/tigervnc/vncviewer/ServerDialog.java | 69 +++++++++--------- java/com/tigervnc/vncviewer/Viewport.java | 24 ++++++- 8 files changed, 104 insertions(+), 103 deletions(-) diff --git a/java/com/tigervnc/vncviewer/CConn.java b/java/com/tigervnc/vncviewer/CConn.java index 7ad2b020..9adb9675 100644 --- a/java/com/tigervnc/vncviewer/CConn.java +++ b/java/com/tigervnc/vncviewer/CConn.java @@ -122,14 +122,13 @@ public class CConn extends CConnection serverPort = Hostname.getPort(vncServerName); } else { ServerDialog dlg = new ServerDialog(options, vncServerName, this); - if (!dlg.showDialog() || dlg.server.getSelectedItem().equals("")) { - vlog.info("No server name specified!"); + boolean ret = dlg.showDialog(); + if (!ret) { close(); return; } - vncServerName = (String)dlg.server.getSelectedItem(); - serverHost = Hostname.getHost(vncServerName); - serverPort = Hostname.getPort(vncServerName); + serverHost = viewer.vncServerName.getValueStr(); + serverPort = viewer.vncServerPort.getValue(); } try { @@ -727,15 +726,24 @@ public class CConn extends CConnection pkgDate = attributes.getValue("Package-Date"); pkgTime = attributes.getValue("Package-Time"); } catch (IOException e) { } + + Window fullScreenWindow = Viewport.getFullScreenWindow(); + if (fullScreenWindow != null) + Viewport.setFullScreenWindow(null); JOptionPane.showMessageDialog((viewport != null ? viewport : null), String.format(VncViewer.aboutText, VncViewer.version, VncViewer.build, VncViewer.buildDate, VncViewer.buildTime), "About TigerVNC Viewer for Java", JOptionPane.INFORMATION_MESSAGE, logo); + if (fullScreenWindow != null) + Viewport.setFullScreenWindow(fullScreenWindow); } void showInfo() { + Window fullScreenWindow = Viewport.getFullScreenWindow(); + if (fullScreenWindow != null) + Viewport.setFullScreenWindow(null); JOptionPane.showMessageDialog(viewport, "Desktop name: "+cp.name()+"\n" +"Host: "+sock.getPeerName()+":"+sock.getPeerPort()+"\n" @@ -750,6 +758,8 @@ public class CConn extends CConnection +" ["+csecurity.description()+"]", "VNC connection info", JOptionPane.PLAIN_MESSAGE); + if (fullScreenWindow != null) + Viewport.setFullScreenWindow(fullScreenWindow); } public void refresh() { diff --git a/java/com/tigervnc/vncviewer/ClipboardDialog.java b/java/com/tigervnc/vncviewer/ClipboardDialog.java index 69fc7114..41f92b1a 100644 --- a/java/com/tigervnc/vncviewer/ClipboardDialog.java +++ b/java/com/tigervnc/vncviewer/ClipboardDialog.java @@ -97,12 +97,10 @@ class ClipboardDialog extends Dialog implements ActionListener { current = ""; textArea.setText(current); } else if (s instanceof JButton && (JButton)s == sendButton) { - ok = true; current = textArea.getText(); cc.writeClientCutText(current, current.length()); endDialog(); } else if (s instanceof JButton && (JButton)s == cancelButton) { - ok = false; endDialog(); } } diff --git a/java/com/tigervnc/vncviewer/Dialog.java b/java/com/tigervnc/vncviewer/Dialog.java index 274cdffe..5e6790fe 100644 --- a/java/com/tigervnc/vncviewer/Dialog.java +++ b/java/com/tigervnc/vncviewer/Dialog.java @@ -29,22 +29,20 @@ package com.tigervnc.vncviewer; import java.awt.*; +import java.awt.Dialog.*; import javax.swing.*; -//class Dialog extends JFrame implements WindowListener { -class Dialog extends JFrame { +class Dialog extends JDialog { - protected boolean ok, done; - boolean modal; - - public Dialog(boolean modal_) { - modal = modal_; - //addWindowListener(this); + public Dialog(boolean modal) { + if (modal) { + setModalityType(ModalityType.APPLICATION_MODAL); + } else { + setModalityType(ModalityType.MODELESS); + } } public boolean showDialog(Component c) { - ok = false; - done = false; initDialog(); if (c != null) { setLocationRelativeTo(c); @@ -58,20 +56,14 @@ class Dialog extends JFrame { ClassLoader cl = this.getClass().getClassLoader(); ImageIcon icon = new ImageIcon(cl.getResource("com/tigervnc/vncviewer/tigervnc.ico")); setIconImage(icon.getImage()); - //setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE); - //setFont(new Font("SansSerif", Font.PLAIN, 11)); + fullScreenWindow = Viewport.getFullScreenWindow(); + if (fullScreenWindow != null) + Viewport.setFullScreenWindow(null); setVisible(true); setFocusable(true); - if (!modal) return true; - synchronized(this) { - try { - while (!done) - wait(); - } catch (InterruptedException e) { - } - } - return ok; + setAlwaysOnTop(true); + return ret; } public boolean showDialog() { @@ -79,14 +71,11 @@ class Dialog extends JFrame { } public void endDialog() { - done = true; setVisible(false); setFocusable(false); - if (modal) { - synchronized (this) { - notify(); - } - } + setAlwaysOnTop(false); + if (fullScreenWindow != null) + Viewport.setFullScreenWindow(fullScreenWindow); } // initDialog() can be overridden in a derived class. Typically it is used @@ -94,23 +83,6 @@ class Dialog extends JFrame { public void initDialog() { } - //------------------------------------------------------------------ - // implemented blank methods - //public void windowClosed(WindowEvent event){} - //public void windowDeiconified(WindowEvent event){} - //public void windowIconified(WindowEvent event){} - //public void windowActivated(WindowEvent event){} - //public void windowDeactivated(WindowEvent event){} - //public void windowOpened(WindowEvent event){} - - //------------------------------------------------------------------ - - // method to check which window was closing - //public void windowClosing(WindowEvent event) { - // ok = false; - // endDialog(); - //} - public void addGBComponent(JComponent c, JComponent cp, int gx, int gy, int gw, int gh, @@ -134,14 +106,7 @@ class Dialog extends JFrame { cp.add(c, gbc); } - final public String getFileSeperator() { - String seperator = System.getProperties().get("file.separator").toString(); - return seperator; - } - - final public String getUserName() { - String userName = (String)System.getProperties().get("user.name"); - return userName; - } + private Window fullScreenWindow; + protected boolean ret = true; } diff --git a/java/com/tigervnc/vncviewer/F8Menu.java b/java/com/tigervnc/vncviewer/F8Menu.java index 07e8974e..ee7ae872 100644 --- a/java/com/tigervnc/vncviewer/F8Menu.java +++ b/java/com/tigervnc/vncviewer/F8Menu.java @@ -19,6 +19,7 @@ package com.tigervnc.vncviewer; +import java.awt.*; import java.awt.Cursor; import java.awt.event.*; import javax.swing.JFrame; @@ -122,7 +123,12 @@ public class F8Menu extends JPopupMenu implements ActionListener { fc.setDialogTitle("Save current configuration as:"); fc.setApproveButtonText("OK"); fc.setFileHidingEnabled(false); + Window fullScreenWindow = Viewport.getFullScreenWindow(); + if (fullScreenWindow != null) + Viewport.setFullScreenWindow(null); int ret = fc.showOpenDialog(this); + if (fullScreenWindow != null) + Viewport.setFullScreenWindow(fullScreenWindow); if (ret == JFileChooser.APPROVE_OPTION) { String filename = fc.getSelectedFile().toString(); if (filename != null) diff --git a/java/com/tigervnc/vncviewer/OptionsDialog.java b/java/com/tigervnc/vncviewer/OptionsDialog.java index 49757335..80d9f40d 100644 --- a/java/com/tigervnc/vncviewer/OptionsDialog.java +++ b/java/com/tigervnc/vncviewer/OptionsDialog.java @@ -52,7 +52,7 @@ class OptionsDialog extends Dialog implements JButton cfLoadButton, cfSaveAsButton, defSaveButton, defReloadButton, defClearButton; public OptionsDialog(OptionsDialogCallback cb_) { - super(false); + super(true); cb = cb_; setResizable(false); setTitle("VNC Viewer Options"); @@ -292,7 +292,6 @@ class OptionsDialog extends Dialog implements } private void updatePreferences() { - //UserPreferences.clear("global"); if (autoSelect.isSelected()) { UserPreferences.set("global", "AutoSelect", true); } else { @@ -472,11 +471,9 @@ class OptionsDialog extends Dialog implements public void actionPerformed(ActionEvent e) { Object s = e.getSource(); if (s instanceof JButton && (JButton)s == okButton) { - ok = true; if (cb != null) cb.getOptions(); endDialog(); } else if (s instanceof JButton && (JButton)s == cancelButton) { - ok = false; endDialog(); } else if (s instanceof JButton && (JButton)s == cfLoadButton) { JFileChooser fc = new JFileChooser(); diff --git a/java/com/tigervnc/vncviewer/PasswdDialog.java b/java/com/tigervnc/vncviewer/PasswdDialog.java index c12fad54..10281e0d 100644 --- a/java/com/tigervnc/vncviewer/PasswdDialog.java +++ b/java/com/tigervnc/vncviewer/PasswdDialog.java @@ -32,7 +32,11 @@ class PasswdDialog extends Dialog implements KeyListener, super(true); setResizable(false); setTitle(title); - setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); + addWindowListener(new WindowAdapter() { + public void windowClosing(WindowEvent e) { + endDialog(); + } + }); JPanel p1 = new JPanel(); userLabel = new JLabel("Username:"); @@ -73,18 +77,16 @@ class PasswdDialog extends Dialog implements KeyListener, Object s = event.getSource(); if (s instanceof JTextField && (JTextField)s == userEntry) { if (event.getKeyCode() == KeyEvent.VK_ENTER) { - ok = true; endDialog(); } } else if (s instanceof JPasswordField && (JPasswordField)s == passwdEntry) { if (event.getKeyCode() == KeyEvent.VK_ENTER) { - ok = true; endDialog(); } } } - public String getPassword(){ + public String getPassword() { return new String(passwdEntry.getPassword()); } public String getPassphrase(){ return null; } diff --git a/java/com/tigervnc/vncviewer/ServerDialog.java b/java/com/tigervnc/vncviewer/ServerDialog.java index 3efc5859..976dc7f2 100644 --- a/java/com/tigervnc/vncviewer/ServerDialog.java +++ b/java/com/tigervnc/vncviewer/ServerDialog.java @@ -47,7 +47,7 @@ class ServerDialog extends Dialog implements if (cc.viewer.nViewers == 1) { cc.viewer.exit(1); } else { - ok = false; + ret = false; endDialog(); } } @@ -117,21 +117,18 @@ class ServerDialog extends Dialog implements } public void itemStateChanged(ItemEvent e) { - //Object s = e.getSource(); + return; } public void actionPerformed(ActionEvent e) { Object s = e.getSource(); if (s instanceof JButton && (JButton)s == okButton) { - ok = true; - String serverName = (String)server.getSelectedItem(); - if (serverName != null) { - Configuration.setParam("Server", Hostname.getHost(serverName)); - Configuration.setParam("Port", Integer.toString(Hostname.getPort(serverName))); - } + commit(); endDialog(); } else if (s instanceof JButton && (JButton)s == cancelButton) { - ok = false; + if (cc.viewer.nViewers == 1) + cc.viewer.exit(1); + ret = false; endDialog(); } else if (s instanceof JButton && (JButton)s == optionsButton) { options.showDialog(this); @@ -141,38 +138,42 @@ class ServerDialog extends Dialog implements if (e.getActionCommand().equals("comboBoxEdited")) { server.insertItemAt(editor.getItem(), 0); server.setSelectedIndex(0); - ok = true; + commit(); endDialog(); } } } - - public void endDialog() { - if (ok) { - if (!server.getSelectedItem().toString().equals("")) { - String valueStr = UserPreferences.get("ServerDialog", "history"); - String t = (valueStr == null) ? "" : valueStr; - StringTokenizer st = new StringTokenizer(t, ","); - StringBuffer sb = new StringBuffer().append((String)server.getSelectedItem()); - while (st.hasMoreTokens()) { - String s = st.nextToken(); - if (!s.equals((String)server.getSelectedItem()) && !s.equals("")) { - sb.append(','); - sb.append(s); - } - } - UserPreferences.set("ServerDialog", "history", sb.toString()); - UserPreferences.save("ServerDialog"); - } - } - done = true; - if (modal) { - synchronized (this) { - notify(); + private void commit() { + String serverName = (String)server.getSelectedItem(); + if (serverName == null || serverName.equals("")) { + vlog.error("Invalid servername specified"); + if (cc.viewer.nViewers == 1) + cc.viewer.exit(1); + ret = false; + endDialog(); + } + // set params + Configuration.setParam("Server", Hostname.getHost(serverName)); + Configuration.setParam("Port", Integer.toString(Hostname.getPort(serverName))); + // Update the history list + String valueStr = UserPreferences.get("ServerDialog", "history"); + String t = (valueStr == null) ? "" : valueStr; + StringTokenizer st = new StringTokenizer(t, ","); + StringBuffer sb = new StringBuffer().append((String)server.getSelectedItem()); + while (st.hasMoreTokens()) { + String str = st.nextToken(); + if (!str.equals((String)server.getSelectedItem()) && !str.equals("")) { + sb.append(','); + sb.append(str); } } - this.dispose(); + UserPreferences.set("ServerDialog", "history", sb.toString()); + UserPreferences.save("ServerDialog"); + } + + public void endDialog() { + super.endDialog(); } CConn cc; diff --git a/java/com/tigervnc/vncviewer/Viewport.java b/java/com/tigervnc/vncviewer/Viewport.java index 503a653e..c148416b 100644 --- a/java/com/tigervnc/vncviewer/Viewport.java +++ b/java/com/tigervnc/vncviewer/Viewport.java @@ -24,6 +24,10 @@ import java.awt.Color; import java.awt.event.*; import java.awt.Dimension; import java.awt.Event; +import java.awt.GraphicsConfiguration; +import java.awt.GraphicsDevice; +import java.awt.GraphicsEnvironment; +import java.awt.Window; import javax.swing.*; import com.tigervnc.rdr.*; @@ -49,7 +53,11 @@ public class Viewport extends JFrame }); addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { - cc.close(); + if (cc.viewer.nViewers == 1) { + cc.viewer.exit(1); + } else { + cc.close(); + } } }); addComponentListener(new ComponentAdapter() { @@ -105,6 +113,20 @@ public class Viewport extends JFrame setBackground(Color.BLACK); } + public static Window getFullScreenWindow() { + GraphicsEnvironment ge = + GraphicsEnvironment.getLocalGraphicsEnvironment(); + GraphicsDevice gd = ge.getDefaultScreenDevice(); + Window fullScreenWindow = gd.getFullScreenWindow(); + return fullScreenWindow; + } + + public static void setFullScreenWindow(Window fullScreenWindow) { + GraphicsEnvironment ge = + GraphicsEnvironment.getLocalGraphicsEnvironment(); + GraphicsDevice gd = ge.getDefaultScreenDevice(); + gd.setFullScreenWindow(fullScreenWindow); + } CConn cc; JScrollPane sp; -- 2.39.5