]> source.dussan.org Git - gitblit.git/commitdiff
Fixed focus for keystore password prompt
authorJames Moger <james.moger@gitblit.com>
Wed, 28 Nov 2012 22:39:12 +0000 (17:39 -0500)
committerJames Moger <james.moger@gitblit.com>
Wed, 28 Nov 2012 22:39:12 +0000 (17:39 -0500)
src/com/gitblit/authority/GitblitAuthority.java
src/com/gitblit/authority/RequestFocusListener.java [new file with mode: 0644]

index 441fa13f50c2e0d9b94cdff0fe798436763245dd..59f13206239d80cf4de6c443cd92ee59e6cfe4b2 100644 (file)
@@ -307,16 +307,9 @@ public class GitblitAuthority extends JFrame implements X509Log {
        \r
        private boolean prepareX509Infrastructure() {\r
                if (caKeystorePassword == null) {\r
-                       JPasswordField pass = new JPasswordField(10){\r
-                               private static final long serialVersionUID = 1L;\r
-\r
-                               public void addNotify()             \r
-                           {                 \r
-                               super.addNotify();\r
-                               requestFocusInWindow();             \r
-                           }         \r
-                       }; \r
+                       JPasswordField pass = new JPasswordField(10);\r
                        pass.setText(caKeystorePassword);\r
+                       pass.addAncestorListener(new RequestFocusListener());\r
                        JPanel panel = new JPanel(new BorderLayout());\r
                        panel.add(new JLabel(Translation.get("gb.enterKeystorePassword")), BorderLayout.NORTH);\r
                        panel.add(pass, BorderLayout.CENTER);\r
diff --git a/src/com/gitblit/authority/RequestFocusListener.java b/src/com/gitblit/authority/RequestFocusListener.java
new file mode 100644 (file)
index 0000000..e936868
--- /dev/null
@@ -0,0 +1,79 @@
+/*\r
+ * Copyright 2012 gitblit.com.\r
+ *\r
+ * Licensed under the Apache License, Version 2.0 (the "License");\r
+ * you may not use this file except in compliance with the License.\r
+ * You may obtain a copy of the License at\r
+ *\r
+ *     http://www.apache.org/licenses/LICENSE-2.0\r
+ *\r
+ * Unless required by applicable law or agreed to in writing, software\r
+ * distributed under the License is distributed on an "AS IS" BASIS,\r
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+ * See the License for the specific language governing permissions and\r
+ * limitations under the License.\r
+ */\r
+package com.gitblit.authority;\r
+import javax.swing.*;\r
+import javax.swing.event.*;\r
+\r
+/**\r
+ *  Convenience class to request focus on a component.\r
+ *\r
+ *  When the component is added to a realized Window then component will\r
+ *  request focus immediately, since the ancestorAdded event is fired\r
+ *  immediately.\r
+ *\r
+ *  When the component is added to a non realized Window, then the focus\r
+ *  request will be made once the window is realized, since the\r
+ *  ancestorAdded event will not be fired until then.\r
+ *\r
+ *  Using the default constructor will cause the listener to be removed\r
+ *  from the component once the AncestorEvent is generated. A second constructor\r
+ *  allows you to specify a boolean value of false to prevent the\r
+ *  AncestorListener from being removed when the event is generated. This will\r
+ *  allow you to reuse the listener each time the event is generated.\r
+ *  \r
+ *  @author Rob Camick\r
+ */\r
+public class RequestFocusListener implements AncestorListener\r
+{\r
+       private boolean removeListener;\r
+\r
+       /*\r
+        *  Convenience constructor. The listener is only used once and then it is\r
+        *  removed from the component.\r
+        */\r
+       public RequestFocusListener()\r
+       {\r
+               this(true);\r
+       }\r
+\r
+       /*\r
+        *  Constructor that controls whether this listen can be used once or\r
+        *  multiple times.\r
+        *\r
+        *  @param removeListener when true this listener is only invoked once\r
+        *                        otherwise it can be invoked multiple times.\r
+        */\r
+       public RequestFocusListener(boolean removeListener)\r
+       {\r
+               this.removeListener = removeListener;\r
+       }\r
+\r
+       @Override\r
+       public void ancestorAdded(AncestorEvent e)\r
+       {\r
+               JComponent component = e.getComponent();\r
+               component.requestFocusInWindow();\r
+\r
+               if (removeListener)\r
+                       component.removeAncestorListener( this );\r
+       }\r
+\r
+       @Override\r
+       public void ancestorMoved(AncestorEvent e) {}\r
+\r
+       @Override\r
+       public void ancestorRemoved(AncestorEvent e) {}\r
+}\r