changeset 6906:db8fb9db7d60

Follow-up to RT-28136 (removing code duplication and file cleanup).
author jgiles
date Mon, 28 Apr 2014 15:21:18 +1200
parents c5322f1ee55e
children 410ba5840447
files modules/controls/src/main/java/com/sun/javafx/scene/control/ControlAcceleratorSupport.java
diffstat 1 files changed, 80 insertions(+), 82 deletions(-) [+]
line wrap: on
line diff
--- a/modules/controls/src/main/java/com/sun/javafx/scene/control/ControlAcceleratorSupport.java	Mon Apr 28 15:07:28 2014 +1200
+++ b/modules/controls/src/main/java/com/sun/javafx/scene/control/ControlAcceleratorSupport.java	Mon Apr 28 15:21:18 2014 +1200
@@ -49,49 +49,16 @@
 
 public class ControlAcceleratorSupport {
 
+    // --- Add
+
     public static void addAcceleratorsIntoScene(ObservableList<MenuItem> items, Tab anchor) {
         // with Tab, we first need to wait until the Tab has a TabPane associated with it
-        if (anchor == null) {
-            throw new IllegalArgumentException("Anchor cannot be null");
-        }
-
-        TabPane tabPane = anchor.getTabPane();
-        if (tabPane == null) {
-            anchor.tabPaneProperty().addListener(new InvalidationListener() {
-                @Override public void invalidated(Observable observable) {
-                    TabPane tabPane = anchor.getTabPane();
-                    if (tabPane != null) {
-                        anchor.tabPaneProperty().removeListener(this);
-                        addAcceleratorsIntoScene(items, tabPane);
-                    }
-                }
-            });
-        } else {
-            addAcceleratorsIntoScene(items, tabPane);
-        }
+        addAcceleratorsIntoScene(items, (Object)anchor);
     }
 
     public static void addAcceleratorsIntoScene(ObservableList<MenuItem> items, TableColumnBase<?,?> anchor) {
-        // with Tab, we first need to wait until the Tab has a TabPane associated with it
-        if (anchor == null) {
-            throw new IllegalArgumentException("Anchor cannot be null");
-        }
-
-        final ReadOnlyObjectProperty<Control> controlProperty = getTableControlProperty(anchor);
-        final Control control = controlProperty.get();
-        if (control == null) {
-            controlProperty.addListener(new InvalidationListener() {
-                @Override public void invalidated(Observable observable) {
-                    final Control control = controlProperty.get();
-                    if (control != null) {
-                        controlProperty.removeListener(this);
-                        addAcceleratorsIntoScene(items, control);
-                    }
-                }
-            });
-        } else {
-            addAcceleratorsIntoScene(items, control);
-        }
+        // with TableColumnBase, we first need to wait until it has a TableView/TreeTableView associated with it
+        addAcceleratorsIntoScene(items, (Object)anchor);
     }
 
     public static void addAcceleratorsIntoScene(ObservableList<MenuItem> items, Node anchor) {
@@ -121,50 +88,29 @@
         }
     }
 
-    public static void removeAcceleratorsFromScene(List<? extends MenuItem> items, Tab anchor) {
-        Scene scene = anchor.getTabPane().getScene();
-        removeAcceleratorsFromScene(items, scene);
-    }
-
-    public static void removeAcceleratorsFromScene(List<? extends MenuItem> items, TableColumnBase<?,?> anchor) {
-        ReadOnlyObjectProperty<Control> controlProperty = getTableControlProperty(anchor);
-        if (controlProperty == null) return;
-
-        Control control = controlProperty.get();
-        if (control == null) return;
-
-        Scene scene = control.getScene();
-        removeAcceleratorsFromScene(items, scene);
-    }
-
-    public static void removeAcceleratorsFromScene(List<? extends MenuItem> items, Node anchor) {
-        Scene scene = anchor.getScene();
-        removeAcceleratorsFromScene(items, scene);
-    }
-
-    public static void removeAcceleratorsFromScene(List<? extends MenuItem> items, Scene scene) {
-        if (scene == null) {
-            return;
+    private static void addAcceleratorsIntoScene(ObservableList<MenuItem> items, Object anchor) {
+        // with TableColumnBase, we first need to wait until it has a TableView/TreeTableView associated with it
+        if (anchor == null) {
+            throw new IllegalArgumentException("Anchor cannot be null");
         }
 
-        for (final MenuItem menuitem : items) {
-            if (menuitem instanceof Menu) {
-                // TODO remove the menu listener from the menu.items list
-
-                // remove the accelerators of items contained within the menu
-                removeAcceleratorsFromScene(((Menu)menuitem).getItems(), scene);
-            } else {
-                // remove the removed MenuItem accelerator KeyCombination from
-                // the scene accelerators map
-                final Map<KeyCombination, Runnable> accelerators = scene.getAccelerators();
-                accelerators.remove(menuitem.getAccelerator());
-            }
+        final ReadOnlyObjectProperty<? extends Control> controlProperty = getControlProperty(anchor);
+        final Control control = controlProperty.get();
+        if (control == null) {
+            controlProperty.addListener(new InvalidationListener() {
+                @Override public void invalidated(Observable observable) {
+                    final Control control = controlProperty.get();
+                    if (control != null) {
+                        controlProperty.removeListener(this);
+                        addAcceleratorsIntoScene(items, control);
+                    }
+                }
+            });
+        } else {
+            addAcceleratorsIntoScene(items, control);
         }
     }
 
-
-
-
     private static void doAcceleratorInstall(final ObservableList<MenuItem> items, final Scene scene) {
         // we're given an observable list of menu items, which we will add an observer to
         // so that when menu items are added or removed we can properly handle
@@ -237,12 +183,64 @@
         }
     }
 
-    private static ReadOnlyObjectProperty<Control> getTableControlProperty(TableColumnBase<?,?> column) {
-        if (column instanceof TableColumn) {
-            return ((TableColumn)column).tableViewProperty();
-        } else if (column instanceof TreeTableColumn) {
-            return ((TreeTableColumn)column).treeTableViewProperty();
+
+
+    // --- Remove
+
+    public static void removeAcceleratorsFromScene(List<? extends MenuItem> items, Tab anchor) {
+        Scene scene = anchor.getTabPane().getScene();
+        removeAcceleratorsFromScene(items, scene);
+    }
+
+    public static void removeAcceleratorsFromScene(List<? extends MenuItem> items, TableColumnBase<?,?> anchor) {
+        ReadOnlyObjectProperty<? extends Control> controlProperty = getControlProperty(anchor);
+        if (controlProperty == null) return;
+
+        Control control = controlProperty.get();
+        if (control == null) return;
+
+        Scene scene = control.getScene();
+        removeAcceleratorsFromScene(items, scene);
+    }
+
+    public static void removeAcceleratorsFromScene(List<? extends MenuItem> items, Node anchor) {
+        Scene scene = anchor.getScene();
+        removeAcceleratorsFromScene(items, scene);
+    }
+
+    public static void removeAcceleratorsFromScene(List<? extends MenuItem> items, Scene scene) {
+        if (scene == null) {
+            return;
         }
+
+        for (final MenuItem menuitem : items) {
+            if (menuitem instanceof Menu) {
+                // TODO remove the menu listener from the menu.items list
+
+                // remove the accelerators of items contained within the menu
+                removeAcceleratorsFromScene(((Menu)menuitem).getItems(), scene);
+            } else {
+                // remove the removed MenuItem accelerator KeyCombination from
+                // the scene accelerators map
+                final Map<KeyCombination, Runnable> accelerators = scene.getAccelerators();
+                accelerators.remove(menuitem.getAccelerator());
+            }
+        }
+    }
+
+
+
+    // --- Utilities
+
+    private static ReadOnlyObjectProperty<? extends Control> getControlProperty(Object obj) {
+        if (obj instanceof TableColumn) {
+            return ((TableColumn)obj).tableViewProperty();
+        } else if (obj instanceof TreeTableColumn) {
+            return ((TreeTableColumn)obj).treeTableViewProperty();
+        } else if (obj instanceof Tab) {
+            return ((Tab)obj).tabPaneProperty();
+        }
+
         return null;
     }
 }