changeset 637:a4125097db18

6554743: JFileChooser dn't close after pressing escape key after changing the views Summary: Restore focus after changing the views in JFileChooser Reviewed-by: loneid
author rupashka
date Sat, 30 Aug 2008 17:29:59 +0400
parents 8c34b54ef09c
children 291feed36076
files src/share/classes/sun/swing/FilePane.java
diffstat 1 files changed, 54 insertions(+), 54 deletions(-) [+]
line wrap: on
line diff
--- a/src/share/classes/sun/swing/FilePane.java	Fri Aug 29 18:58:17 2008 +0400
+++ b/src/share/classes/sun/swing/FilePane.java	Sat Aug 30 17:29:59 2008 +0400
@@ -308,44 +308,80 @@
     }
 
     public void setViewType(int viewType) {
-        int oldValue = this.viewType;
-        if (viewType == oldValue) {
+        if (viewType == this.viewType) {
             return;
         }
+
+        int oldValue = this.viewType;
         this.viewType = viewType;
 
+        JPanel createdViewPanel = null;
+        Component newFocusOwner = null;
+
         switch (viewType) {
           case VIEWTYPE_LIST:
             if (viewPanels[viewType] == null) {
-                JPanel p = fileChooserUIAccessor.createList();
-                if (p == null) {
-                    p = createList();
+                createdViewPanel = fileChooserUIAccessor.createList();
+                if (createdViewPanel == null) {
+                    createdViewPanel = createList();
                 }
-                setViewPanel(viewType, p);
+
+                list = (JList) findChildComponent(createdViewPanel, JList.class);
+                if (listSelectionModel == null) {
+                    listSelectionModel = list.getSelectionModel();
+                    if (detailsTable != null) {
+                        detailsTable.setSelectionModel(listSelectionModel);
+                    }
+                } else {
+                    list.setSelectionModel(listSelectionModel);
+                }
             }
             list.setLayoutOrientation(JList.VERTICAL_WRAP);
+            newFocusOwner = list;
             break;
 
           case VIEWTYPE_DETAILS:
             if (viewPanels[viewType] == null) {
-                JPanel p = fileChooserUIAccessor.createDetailsView();
-                if (p == null) {
-                    p = createDetailsView();
+                createdViewPanel = fileChooserUIAccessor.createDetailsView();
+                if (createdViewPanel == null) {
+                    createdViewPanel = createDetailsView();
                 }
-                setViewPanel(viewType, p);
+
+                detailsTable = (JTable) findChildComponent(createdViewPanel, JTable.class);
+                detailsTable.setRowHeight(Math.max(detailsTable.getFont().getSize() + 4, 16 + 1));
+                if (listSelectionModel != null) {
+                    detailsTable.setSelectionModel(listSelectionModel);
+                }
             }
+            newFocusOwner = detailsTable;
             break;
         }
-        JPanel oldViewPanel = currentViewPanel;
+
+        if (createdViewPanel != null) {
+            viewPanels[viewType] = createdViewPanel;
+            recursivelySetInheritsPopupMenu(createdViewPanel, true);
+        }
+
+        boolean isFocusOwner = false;
+
+        if (currentViewPanel != null) {
+            Component owner = DefaultKeyboardFocusManager.
+                    getCurrentKeyboardFocusManager().getPermanentFocusOwner();
+
+            isFocusOwner = owner == detailsTable || owner == list;
+
+            remove(currentViewPanel);
+        }
+
         currentViewPanel = viewPanels[viewType];
-        if (currentViewPanel != oldViewPanel) {
-            if (oldViewPanel != null) {
-                remove(oldViewPanel);
-            }
-            add(currentViewPanel, BorderLayout.CENTER);
-            revalidate();
-            repaint();
+        add(currentViewPanel, BorderLayout.CENTER);
+
+        if (isFocusOwner && newFocusOwner != null) {
+            newFocusOwner.requestFocusInWindow();
         }
+
+        revalidate();
+        repaint();
         updateViewMenu();
         firePropertyChange("viewType", oldValue, viewType);
     }
@@ -385,42 +421,6 @@
         }
     }
 
-    public void setViewPanel(int viewType, JPanel viewPanel) {
-        viewPanels[viewType] = viewPanel;
-        recursivelySetInheritsPopupMenu(viewPanel, true);
-
-        switch (viewType) {
-          case VIEWTYPE_LIST:
-            list = (JList)findChildComponent(viewPanels[viewType], JList.class);
-            if (listSelectionModel == null) {
-                listSelectionModel = list.getSelectionModel();
-                if (detailsTable != null) {
-                    detailsTable.setSelectionModel(listSelectionModel);
-                }
-            } else {
-                list.setSelectionModel(listSelectionModel);
-            }
-            break;
-
-          case VIEWTYPE_DETAILS:
-            detailsTable = (JTable)findChildComponent(viewPanels[viewType], JTable.class);
-            detailsTable.setRowHeight(Math.max(detailsTable.getFont().getSize() + 4, 16+1));
-            if (listSelectionModel != null) {
-                detailsTable.setSelectionModel(listSelectionModel);
-            }
-            break;
-        }
-        if (this.viewType == viewType) {
-            if (currentViewPanel != null) {
-                remove(currentViewPanel);
-            }
-            currentViewPanel = viewPanel;
-            add(currentViewPanel, BorderLayout.CENTER);
-            revalidate();
-            repaint();
-        }
-    }
-
     protected void installDefaults() {
         Locale l = getFileChooser().getLocale();