\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
--- /dev/null
+/*\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